I have a conditional webGPU wgsl vertex shader set up like so:
@vertex
fn vs_main(input: VertexInput) -> VertexOutput {
if (cond A && cond B) {
// some logic dependent on instance index
...
return VertexOutput( ... );
}
// some other logic dependent on instance index
...
return VertexOutput( ... );
}
Where cond A is set via a uniform -
uRenderParam.renderPass, and the condition is uRenderParam.renderPass == 1u.
I then use two different render passes back to back like so:
methodToSetTheUniformToZero();
renderPass1 = commandEncoder.beginRenderPass();
...
renderPass1.drawIndirect(indirectDrawBuffer1);
renderPass1.end();
methodToSetTheUniformToOne();
renderPass2 = commandEncoder.beginRenderPass();
...
renderPass2.drawIndirect(indirectDrawBuffer2);
renderPass2.end();
Cond B is set to true via another uniform on the mouse down event, and set back to false on mouse up event.
But what Iām observing is that when cond B is set to true, itās as if the branch dependent on cond A and cond B is the only one being run but with the number of indirect draw calls that we would expect from indirectDrawBuffer1, which makes no sense.
Initially, I thought that maybe the driver āoptimizesā by hoisting or pruning away one code path. I realize this wouldnāt fully explain the observed behavior, but it was my initial suspicion that it was something of this nature causing the problem.
So I refactored the shader like so-
@vertex
fn vs_main(input: VertexInput) -> VertexOutput {
var output: VertexOutput;
let RED = vec4<f32>(1.0, 0.0, 0.0, 1.0);
let BLUE = vec4<f32>(0.0, 0.0, 1.0, 1.0);
if (cond A && cond B) {
// some logic dependent on instance index
...
output.vColor = RED;
} else {
// some other logic dependent on instance index
...
output.vColor = BLUE;
}
return output;
}
And still, while cond B is true (during the mouse down event), I only see RED, where I would expect to see both present due to both render passes running per frame back to back.
In both iterations, if Iām not holding the mouse down, I only see the output from the later branch, setting vColor to BLUE, which is expected. Itās when I am holding the mouse down that I would expect to see a mixture of both.
If anyone has had similar experiences, or understands what is happening in this scenario I would really appreciate hearing from you.
OS: MacOS
Hardware: Apple silicon
Browser: Chrome