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

Code before:
            return wgpu::BlendFactor::OneMinusDst;
        case BlendFunction::SRC_ALPHA:
            return wgpu::BlendFactor::SrcAlpha;
        case BlendFunction::ONE_MINUS_SRC_ALPHA:
            return wgpu::BlendFactor::OneMinusSrcAlpha;
        case BlendFunction::DST_ALPHA:
            return wgpu::BlendFactor::DstAlpha;
        case BlendFunction::ONE_MINUS_DST_ALPHA:
            return wgpu::BlendFactor::OneMinusDstAlpha;
        case BlendFunction::SRC_ALPHA_SATURATE:
            return wgpu::BlendFactor::SrcAlphaSaturated;
    }
}

}// 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,
        const wgpu::TextureFormat depthStencilFormat, const uint8_t samplesCount, bool filamentRequestedStencil) {
    assert_invariant(program.vertexShaderModule);
    wgpu::DepthStencilState depthStencilState{};

      if (depthStencilFormat != wgpu::TextureFormat::Undefined) {
        depthStencilState.format = depthStencilFormat;
        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;
        }

        if (hasStencilAspect(depthStencilFormat) && filamentRequestedStencil) {
            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),

Review comment (suggestion):
shouldn't this be an `if` based on bool flag passed to this function? The render pipeline could potentially not have a depth bit (defensive at least).

Code after:
            return wgpu::BlendFactor::OneMinusDst;
        case BlendFunction::SRC_ALPHA:
            return wgpu::BlendFactor::SrcAlpha;
        case BlendFunction::ONE_MINUS_SRC_ALPHA:
            return wgpu::BlendFactor::OneMinusSrcAlpha;
        case BlendFunction::DST_ALPHA:
            return wgpu::BlendFactor::DstAlpha;
        case BlendFunction::ONE_MINUS_DST_ALPHA:
            return wgpu::BlendFactor::OneMinusDstAlpha;
        case BlendFunction::SRC_ALPHA_SATURATE:
            return wgpu::BlendFactor::SrcAlphaSaturated;
    }
}

}// 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),