Texture sampler and non uniform control flow

Hello, it’s me again!

I am still in the process of porting some glsl code to WebGPU and was faced with non uniform control flow issues.

Basically it is when in a shader a texture might not get sampled every time it is ran.

This is not a problem with WebGL2 and allows for very important optimizations when doing volumetric rendering.

For example, in atmospheric rendering: for every pixel, shoot a ray from the camera, if the ray does not intersect the atmosphere, end the execution of the shader, and therefore skip heavy calculations and some texture samples as well.

Here is a reproduction of the issue with a small postprocess that does a conditional texture sample:

As I could not find a reference to this issue on WebGPU Breaking Changes | Babylon.js Documentation I wonder if it is not related to the evolution of the WebGPU spec that would have broken Babylon’s compatibility?

I read some discussions about the addition of a flag to bypass this error, but I could not find if it was actually implemented for WGSL and glsllang (which I believe is used to compile the GLSL to SPIR-V.

Does anyone have more information about that?

WebGL does not check for uniformity problems, but it does not mean you don’t have any! You could have some visual artifacts because of that, depending on the GPU and what you are doing in the shader. Sometimes, these artifacts simply don’t exist or are not really visible. In other cases, they could be visible but acceptable.

Uniformity analysis is new with WebGPU and help to make sure shader code is valid and will behave correctly everywhere, but you can opt to disable it if you want, in a specific shader by adding /* disable_uniformity_analysis */ somewhere in the source, or globally by doing BABYLON.WebGPUTintWASM.DisableUniformityAnalysis = true;.

Or, as suggested in the link you provided, you can use textureGrad:

We will see to update the docs to mention uniformity analysis and how to handle it.

1 Like

Thank you for your answer, I have a small preference for /* disable_uniformity_analysis */ personaly! Here is a playground using it:

I am glad that it can be disabled because the textureGrad solution only works when the texture coords can be computed uniformly. And in the case of atmosphere rendering, it is costly and hard to dissociate from the sampling itself