Repository: playcanvas/engine
File: src/platform/graphics/webgpu/webgpu-shader-processor-wgsl.js (JavaScript)

Code before:
     * Generates a shader code for a uniform buffer, something like:
     * ```
     *     struct ub_view { matrix_viewProjection : mat4x4f }
     *     @group(0) @binding(0) var<uniform> ubView : ub_view;
     * ```
     *
     * @param {UniformBufferFormat} ubFormat - Format of the uniform buffer.
     * @param {number} bindGroup - The bind group index.
     * @param {number} bindIndex - The bind index.
     * @returns {string} - The shader code for the uniform buffer.
     * @private
     */
    static getUniformShaderDeclaration(ubFormat, bindGroup, bindIndex) {

        const name = bindGroupNames[bindGroup];
        const structName = `struct_ub_${name}`;
        let code = `struct ${structName} {\n`;

        ubFormat.uniforms.forEach((uniform) => {
            let typeString = uniformTypeToNameWGSL[uniform.type][0];
            Debug.assert(typeString !== undefined, `Uniform type ${uniform.type} is not handled.`);

            // array uniforms
            if (uniform.count > 0) {

                // if the type is one of the onces that are not by default 16byte aligned, which is
                // a requirement for uniform buffers, we need to wrap them in a struct
                // for example: array<f32, 5> becomes  array<WrappedF32, 5>
                if (wrappedArrayTypes.hasOwnProperty(typeString)) {
                    typeString = wrappedArrayTypes[typeString];
                }

                code += `    ${uniform.shortName}: array<${typeString}, ${uniform.count}>,\n`;

            } else { // not arrays

                code += `    ${uniform.shortName}: ${typeString},\n`;
            }
        });

        code += '};\n';

        code += `@group(${bindGroup}) @binding(${bindIndex}) var<uniform> ub_${name} : ${structName};\n\n`;

        return code;
    }

    /**
     * Generates a shader code for a bind group, something like:
     * ```
     *    @group(0) @binding(0) var diffuseTexture: texture_2d<f32>;

Review comment (suggestion):
```suggestion
                // if the type is one of the ones that are not by default 16byte aligned, which is
```

Code after:
     * Generates a shader code for a uniform buffer, something like:
     * ```
     *     struct ub_view { matrix_viewProjection : mat4x4f }
     *     @group(0) @binding(0) var<uniform> ubView : ub_view;
     * ```
     *
     * @param {UniformBufferFormat} ubFormat - Format of the uniform buffer.
     * @param {number} bindGroup - The bind group index.
     * @param {number} bindIndex - The bind index.
     * @returns {string} - The shader code for the uniform buffer.
     * @private
     */
    static getUniformShaderDeclaration(ubFormat, bindGroup, bindIndex) {

        const name = bindGroupNames[bindGroup];
        const structName = `struct_ub_${name}`;
        let code = `struct ${structName} {\n`;

        ubFormat.uniforms.forEach((uniform) => {
            let typeString = uniformTypeToNameWGSL[uniform.type][0];
            Debug.assert(typeString !== undefined, `Uniform type ${uniform.type} is not handled.`);

            // array uniforms
            if (uniform.count > 0) {

                // if the type is one of the ones that are not by default 16byte aligned, which is
                // a requirement for uniform buffers, we need to wrap them in a struct
                // for example: array<f32, 5> becomes  array<WrappedF32, 5>
                if (wrappedArrayTypes.hasOwnProperty(typeString)) {
                    typeString = wrappedArrayTypes[typeString];
                }

                code += `    ${uniform.shortName}: array<${typeString}, ${uniform.count}>,\n`;

            } else { // not arrays

                code += `    ${uniform.shortName}: ${typeString},\n`;
            }
        });

        code += '};\n';

        code += `@group(${bindGroup}) @binding(${bindIndex}) var<uniform> ub_${name} : ${structName};\n\n`;

        return code;
    }

    /**
     * Generates a shader code for a bind group, something like:
     * ```
     *    @group(0) @binding(0) var diffuseTexture: texture_2d<f32>;