Repository: google/filament
File: filament/backend/src/webgpu/WebGPUPipelineCreation.cpp (C++)

Code before:
    }
}

}// namespace

wgpu::RenderPipeline createWebGPURenderPipeline(wgpu::Device const& device,
        WebGPUProgram const& program, WebGPUVertexBufferInfo const& vertexBufferInfo,
        wgpu::PipelineLayout const& layout, RasterState const& rasterState,
        StencilState const& stencilState, PolygonOffset const& polygonOffset,
        const PrimitiveType primitiveType, std::vector<wgpu::TextureFormat> const& colorFormats,
        wgpu::TextureFormat const& depthStencilFormat, const uint8_t samplesCount, const bool requestedDepth, const bool requestedStencil) {
    assert_invariant(program.vertexShaderModule);
    wgpu::DepthStencilState depthStencilState{};
    const bool depthOrStencilRequested = (requestedDepth || requestedStencil);

    if (depthOrStencilRequested) {
        FILAMENT_CHECK_PRECONDITION(depthStencilFormat != wgpu::TextureFormat::Undefined)
                << "Depth or Stencil requested for pipeline, but depthStencilFormat is "
                   "wgpu::TextureFormat::Undefined.";
        depthStencilState.format = depthStencilFormat;

        if (requestedDepth) {
            depthStencilState.depthWriteEnabled = rasterState.depthWrite;
            depthStencilState.depthCompare = toWebGPU(rasterState.depthFunc);

            if (polygonOffset.constant != 0.0f || polygonOffset.slope != 0.0f) {
                depthStencilState.depthBias = static_cast<int32_t>(polygonOffset.constant);
                depthStencilState.depthBiasSlopeScale = polygonOffset.slope;
                depthStencilState.depthBiasClamp = 0.0f;
            } else {
                depthStencilState.depthBias = 0;
                depthStencilState.depthBiasSlopeScale = 0.0f;
                depthStencilState.depthBiasClamp = 0.0f;
            }
        } else {
            depthStencilState.depthWriteEnabled = false;
            depthStencilState.depthCompare = wgpu::CompareFunction::Always;
            depthStencilState.depthBias = 0;
            depthStencilState.depthBiasSlopeScale = 0.0f;
            depthStencilState.depthBiasClamp = 0.0f;
        }

        if (hasStencilAspect(depthStencilFormat) && requestedStencil) {
            depthStencilState.stencilFront = {
                .compare = toWebGPU(stencilState.front.stencilFunc),
                .failOp = toWebGPU(stencilState.front.stencilOpStencilFail),
                .depthFailOp = toWebGPU(stencilState.front.stencilOpDepthFail),
                .passOp = toWebGPU(stencilState.front.stencilOpDepthStencilPass),
            };
            depthStencilState.stencilBack = {
                .compare = toWebGPU(stencilState.back.stencilFunc),

Review comment (suggestion):
i dont get why we need this check. 
if `polygonOffset.constant` is NOT 0 then we set `depthStencilState.depthBias` to that. However, if `polygonOffset.constant == 0` we still set `depthStencilState.depthBias` to 0. 
why not remove the if and just always set 
`depthStencilState.depthBias = polygonOffset.constant` (same for `.slope`)

Code after:
    }
}

}// namespace

wgpu::RenderPipeline createWebGPURenderPipeline(wgpu::Device const& device,
        WebGPUProgram const& program, WebGPUVertexBufferInfo const& vertexBufferInfo,
        wgpu::PipelineLayout const& layout, RasterState const& rasterState,
        StencilState const& stencilState, PolygonOffset const& polygonOffset,
        const PrimitiveType primitiveType, std::vector<wgpu::TextureFormat> const& colorFormats,
        wgpu::TextureFormat const& depthStencilFormat, const uint8_t samplesCount, const bool requestedDepth, const bool requestedStencil) {
    assert_invariant(program.vertexShaderModule);
    wgpu::DepthStencilState depthStencilState{};
    const bool depthOrStencilRequested = (requestedDepth || requestedStencil);

    if (depthOrStencilRequested) {
        FILAMENT_CHECK_PRECONDITION(depthStencilFormat != wgpu::TextureFormat::Undefined)
                << "Depth or Stencil requested for pipeline, but depthStencilFormat is "
                   "wgpu::TextureFormat::Undefined.";
        depthStencilState.format = depthStencilFormat;

        if (requestedDepth) {
            depthStencilState.depthWriteEnabled = rasterState.depthWrite;
            depthStencilState.depthCompare = toWebGPU(rasterState.depthFunc);
             depthStencilState.depthBias = static_cast<int32_t>(polygonOffset.constant);
             depthStencilState.depthBiasSlopeScale = polygonOffset.slope;
             depthStencilState.depthBiasClamp = 0.0f;
        } else {
            depthStencilState.depthWriteEnabled = false;
            depthStencilState.depthCompare = wgpu::CompareFunction::Undefined;
            depthStencilState.depthBias = 0;
            depthStencilState.depthBiasSlopeScale = 0.0f;
            depthStencilState.depthBiasClamp = 0.0f;
        }

        if (hasStencilAspect(depthStencilFormat) && requestedStencil) {
            depthStencilState.stencilFront = {
                .compare = toWebGPU(stencilState.front.stencilFunc),
                .failOp = toWebGPU(stencilState.front.stencilOpStencilFail),
                .depthFailOp = toWebGPU(stencilState.front.stencilOpDepthFail),
                .passOp = toWebGPU(stencilState.front.stencilOpDepthStencilPass),
            };
            depthStencilState.stencilBack = {
                .compare = toWebGPU(stencilState.back.stencilFunc),
                .failOp = toWebGPU(stencilState.back.stencilOpStencilFail),
                .depthFailOp = toWebGPU(stencilState.back.stencilOpDepthFail),
                .passOp = toWebGPU(stencilState.back.stencilOpDepthStencilPass),
            };
            depthStencilState.stencilReadMask = stencilState.front.readMask;
            depthStencilState.stencilWriteMask = stencilState.stencilWrite ? stencilState.front.writeMask : 0u;
        } else {