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

Code before:
    outKey.stencilFrontDepthFailOperation = request.stencilState.front.stencilOpDepthFail;
    outKey.stencilFrontPassOperation = request.stencilState.front.stencilOpDepthStencilPass;
    outKey.stencilWrite = toUint8(request.stencilState.stencilWrite);
    outKey.stencilFrontReadMask = request.stencilState.front.readMask;
    outKey.stencilFrontWriteMask = request.stencilState.front.writeMask;
    outKey.stencilBackCompare = request.stencilState.back.stencilFunc;
    outKey.stencilBackFailOperation = request.stencilState.back.stencilOpStencilFail;
    outKey.stencilBackDepthFailOperation = request.stencilState.back.stencilOpDepthFail;
    outKey.stencilBackPassOperation = request.stencilState.back.stencilOpDepthStencilPass;
    outKey.cullingMode = request.rasterState.culling;
    outKey.inverseFrontFaces = toUint8(request.rasterState.inverseFrontFaces);
    outKey.depthWriteEnabled = toUint8(request.rasterState.depthWrite);
    outKey.depthCompare = request.rasterState.depthFunc;
    outKey.depthClamp = toUint8(request.rasterState.depthClamp);
    outKey.colorWrite = toUint8(request.rasterState.colorWrite);
    outKey.alphaToCoverageEnabled = toUint8(request.rasterState.alphaToCoverage);
    outKey.colorBlendOperation = request.rasterState.blendEquationRGB;
    outKey.colorBlendSourceFactor = request.rasterState.blendFunctionSrcRGB;
    outKey.colorBlendDestinationFactor = request.rasterState.blendFunctionDstRGB;
    outKey.alphaBlendOperation = request.rasterState.blendEquationAlpha;
    outKey.alphaBlendSourceFactor = request.rasterState.blendFunctionSrcAlpha;
    outKey.alphaBlendDestinationFactor = request.rasterState.blendFunctionDstAlpha;
    outKey.targetRenderFlags = request.targetRenderFlags;
    outKey.multisampleCount = request.multisampleCount;
    outKey.depthStencilFormat = request.depthStencilFormat;
    if (request.primitiveType == PrimitiveType::TRIANGLE_STRIP ||
            request.primitiveType == PrimitiveType::LINE_STRIP) {
        outKey.stripIndexFormat = request.stripIndexFormat;
    } else {
        outKey.stripIndexFormat = wgpu::IndexFormat::Undefined;
    }
    assert_invariant(request.colorFormatCount <= MRT::MAX_SUPPORTED_RENDER_TARGET_COUNT);
    outKey.colorFormatCount = request.colorFormatCount;
    for (size_t colorIndex{ 0 }; colorIndex < request.colorFormatCount; colorIndex++) {
        outKey.colorFormats[colorIndex] = request.colorFormats[colorIndex];
    }
    // vertex buffers...
    for (WebGPUVertexBufferInfo::WebGPUSlotBindingInfo const& vertexBufferSlot:
            request.vertexBufferSlots) {
        assert_invariant(vertexBufferSlot.sourceBufferIndex < MAX_VERTEX_BUFFER_COUNT);
        outKey.vertexBuffers[vertexBufferSlot.sourceBufferIndex].stride = vertexBufferSlot.stride;
        outKey.vertexBuffers[vertexBufferSlot.sourceBufferIndex].offset =
                vertexBufferSlot.bufferOffset;
    }
    // vertex attributes...
    uint8_t currentAttributeIndex{ 0 };
    for (size_t bufferIndex{ 0 }; bufferIndex < request.vertexBufferSlots.size(); bufferIndex++) {
        wgpu::VertexBufferLayout const& vertexBufferLayout{
            request.vertexBufferLayouts[bufferIndex]
        };
        for (size_t attributeIndex{ 0 }; attributeIndex < vertexBufferLayout.attributeCount;

Review comment (refactor):
I suggest a constexpr helper function, probably defined in DriverEnums.h, e.g.:
```cpp
[[nodiscard]] constexpr bool isStripType(const PrimitiveType type) {
    switch (type) {
        case PrimitiveType::POINTS:
        case PrimitiveType::LINES:
        case PrimitiveType::TRIANGLES:
            return false;
        case PrimitiveType::LINE_STRIP:
        case PrimitiveType::TRIANGLE_STRIP:
            return true;
    }
}
```

then:
```cpp
outKey.stripIndexFormat = isStripType(request.primitiveType) ? request.stripIndexFormat
                                                             : wgpu::IndexFormat::Undefined;
```

Code after:
    outKey.stencilFrontDepthFailOperation = request.stencilState.front.stencilOpDepthFail;
    outKey.stencilFrontPassOperation = request.stencilState.front.stencilOpDepthStencilPass;
    outKey.stencilWrite = toUint8(request.stencilState.stencilWrite);
    outKey.stencilFrontReadMask = request.stencilState.front.readMask;
    outKey.stencilFrontWriteMask = request.stencilState.front.writeMask;
    outKey.stencilBackCompare = request.stencilState.back.stencilFunc;
    outKey.stencilBackFailOperation = request.stencilState.back.stencilOpStencilFail;
    outKey.stencilBackDepthFailOperation = request.stencilState.back.stencilOpDepthFail;
    outKey.stencilBackPassOperation = request.stencilState.back.stencilOpDepthStencilPass;
    outKey.cullingMode = request.rasterState.culling;
    outKey.inverseFrontFaces = toUint8(request.rasterState.inverseFrontFaces);
    outKey.depthWriteEnabled = toUint8(request.rasterState.depthWrite);
    outKey.depthCompare = request.rasterState.depthFunc;
    outKey.depthClamp = toUint8(request.rasterState.depthClamp);
    outKey.colorWrite = toUint8(request.rasterState.colorWrite);
    outKey.alphaToCoverageEnabled = toUint8(request.rasterState.alphaToCoverage);
    outKey.colorBlendOperation = request.rasterState.blendEquationRGB;
    outKey.colorBlendSourceFactor = request.rasterState.blendFunctionSrcRGB;
    outKey.colorBlendDestinationFactor = request.rasterState.blendFunctionDstRGB;
    outKey.alphaBlendOperation = request.rasterState.blendEquationAlpha;
    outKey.alphaBlendSourceFactor = request.rasterState.blendFunctionSrcAlpha;
    outKey.alphaBlendDestinationFactor = request.rasterState.blendFunctionDstAlpha;
    outKey.targetRenderFlags = request.targetRenderFlags;
    outKey.multisampleCount = request.multisampleCount;
    outKey.depthStencilFormat = request.depthStencilFormat;
    assert_invariant(request.colorFormatCount <= MRT::MAX_SUPPORTED_RENDER_TARGET_COUNT);
    outKey.colorFormatCount = request.colorFormatCount;
    for (size_t colorIndex{ 0 }; colorIndex < request.colorFormatCount; colorIndex++) {
        outKey.colorFormats[colorIndex] = request.colorFormats[colorIndex];
    }
    // vertex buffers...
    for (WebGPUVertexBufferInfo::WebGPUSlotBindingInfo const& vertexBufferSlot:
            request.vertexBufferSlots) {
        assert_invariant(vertexBufferSlot.sourceBufferIndex < MAX_VERTEX_BUFFER_COUNT);
        outKey.vertexBuffers[vertexBufferSlot.sourceBufferIndex].stride = vertexBufferSlot.stride;
        outKey.vertexBuffers[vertexBufferSlot.sourceBufferIndex].offset =
                vertexBufferSlot.bufferOffset;
    }
    // vertex attributes...
    uint8_t currentAttributeIndex{ 0 };
    for (size_t bufferIndex{ 0 }; bufferIndex < request.vertexBufferSlots.size(); bufferIndex++) {
        wgpu::VertexBufferLayout const& vertexBufferLayout{
            request.vertexBufferLayouts[bufferIndex]
        };
        for (size_t attributeIndex{ 0 }; attributeIndex < vertexBufferLayout.attributeCount;
                attributeIndex++) {
            assert_invariant(attributeIndex < MAX_VERTEX_ATTRIBUTE_COUNT);
            wgpu::VertexAttribute const& vertexAttribute{
                vertexBufferLayout.attributes[attributeIndex]
            };
            outKey.vertexAttributes[currentAttributeIndex].bufferIndex = bufferIndex;