Search code examples
webgpu

Does webGPU sort the fragments by depth?


I imagine this may be obvious and implicit to someone familiar with 3d graphics, but having read through the webgpu documentation (https://www.w3.org/TR/webgpu/) I have not been able to figure out the order in which fragment shader outputs are blended. It appears to be ordered from z=1 to z=0 (which makes sense as it makes opaque objects trivial to implement) but I was not able to find a definitive answer.


Solution

  • WebGPU doesn't do any sorting or ordering. Fragments are written (as if) in the order they are received. (I say "as if" because the GPU can do whatever it wants as long as it knows the results will end up exactly the same "as if" they'd been rendered in the order received).

    As for Z = 0 to Z = 1. The Z value of a fragment can be used for depth testing. Depth testing is something you enable by setting up your render pipeline to use depth testing, creating a depth texture, putting that depth texture in your render pass. Depth testing lets you choose a test such that a pixel is either written (passes the test) or discarded (does not pass the test). The most common test is "less" as in, if the Z value of the pixel being written is "less than" the corresponding value in the depth texture, then the test passes and the pixel is written, further, if writing to the depth texture is enabled, then that pixel's depth value in the depth texture is updated to the z value of the passing pixel.

    Note that this really is orthogonal to "blending". Blending is another pipeline setting. If you have a depth texture setup, and your pixel passes the depth test, then the pixel will blend with whatever blend settings you've setup

    You can read a little bit more about using a depth texture here