Differentiate render pass in WGSL

I’m overriding a shader chunk and want to only add shader code in the forward pass.

For GLSL I can do the following:

#ifdef FORWARD_PASS
<code>
#endif

How can I do the same with WGSL?

You can do the same. We apply pre-processing on WGSL shaders as well.

This could be also handy for you:

Oh I think you are right. I misinterpreted the error.

I describe in more detail:

I’m trying to override a shader chunk globally. To get dithering behaviour based on the distance to the camera. It works for GLSL, but not for WGSL.

Here is the WGSL code:

const chunksWGSL = ShaderChunks.get(app.graphicsDevice, SHADERLANGUAGE_WGSL);
chunksWGSL.set('opacityDitherPS', `
#if STD_OPACITY_DITHER == BAYER8
    #include "bayerPS"
#endif

uniform blueNoiseJitter: vec4f;

#if STD_OPACITY_DITHER == BLUENOISE
    var blueNoiseTex32 : texture_2d<f32>;
    var blueNoiseTex32Sampler : sampler;
#endif

fn opacityDither(alpha: f32, id: f32) {
    #if STD_OPACITY_DITHER == BAYER8

        var noise: f32 = bayer8(floor((pcPosition.xy + uniform.blueNoiseJitter.xy + id) % vec2f(8.0))) / 64.0;

    #else

        #if STD_OPACITY_DITHER == BLUENOISE
            var uv = fract(pcPosition.xy / 32.0 + uniform.blueNoiseJitter.xy + id);
            var noise: f32 = textureSampleLevel(blueNoiseTex32, blueNoiseTex32Sampler, uv, 0.0).y;
        #endif

        #if STD_OPACITY_DITHER == IGNNOISE
            // based on https://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare/
            var magic = vec3f(0.06711056, 0.00583715, 52.9829189);
            var noise: f32 = fract(magic.z * fract(dot(pcPosition.xy + uniform.blueNoiseJitter.xy + id, magic.xy)));
        #endif

    #endif

    // convert the noise to linear space, as that is specified in sRGB space (stores perceptual values)
    noise = pow(noise, 2.2);

    #ifdef FORWARD_PASS
    // Dither based on camera distance
    var lowerDitherThreshold : f32 = 0.25;
    var upperDitherThreshold : f32 = 0.4;
    alpha = (distance(view_position, vPositionW) - lowerDitherThreshold) / (upperDitherThreshold - lowerDitherThreshold);
    #endif

    if (alpha < noise) {
        discard;
    }
}
`);

And this is the error I receive:

WebGPU RenderPipeline creation validation error: [Invalid ShaderModule "Fragment:Shader Id 25 (WGSL) StandardShader-forward-proc"] is invalid.
- While validating fragment stage ([Invalid ShaderModule "Fragment:Shader Id 25 (WGSL) StandardShader-forward-proc"], entryPoint: "fragmentMain").
- While validating fragment state.
- While calling [Device "PlayCanvasWebGPUDevice"].CreateRenderPipeline([RenderPipelineDescriptor ""RenderPipelineDescr-8""]).

while rendering

Pass:RenderPassForward RT:SceneColor | Camera: Camera, Layer: World(OPAQUE) | Node: Blood_Platelet_mesh, Material: Blood_Platelet

[object Object]

WebGPU ShaderModule creation validation error: Error while parsing WGSL: :453:23 error: unresolved value 'view_position'
alpha = (distance(view_position, vPositionW) - lowerDitherThreshold) / (upperDitherThreshold - lowerDitherThreshold);
^^^^^^^^^^^^^


- While calling [Device "PlayCanvasWebGPUDevice"].CreateShaderModule([ShaderModuleDescriptor]).
- While rendering Pass:RenderPassForward RT:SceneColor | Camera: Camera, Layer: World(OPAQUE) | Node: Blood_Platelet_mesh, Material: Blood_Platelet

Seems like view_position isn’t resolved. Whats the problem here?

See this section on how uniforms are referenced:

you need

uniform.view_position
1 Like