Early Z / DirectX 11 Depth Test

As a DirectX noob, I try to wrap my head around depth buffers and, in particular, how pixel shaders are called for hidden pixels.

From what I understand, the rasterizer calls a pixel shader for each pixel that covers the primitive beeing, and then at the merge stage, the merge results checks the depth buffer and either discards, writes, or mixes the pixel into the inverse buffer.

This seems wasteful, although if I create a simple opaque object in front of a very complex one, it would be useful if the rasterizer checked the depth map. Before even invoking pixel shaders for a complex object.

While doing research, I found links to early Z testing / conservative Z testing, etc., but there seems to be very little documentation about this either. I was looking for a way to set this to the state descriptor of a rasterizer object, but I only found something like this in the state of OM desc.

It also seems like it could be set using SetRenderState in DX9 (I have no experience with DX9, though)

From my research, it seems that this is what some hardware just does, if I render objects from front to back, is that right? How can I even tell? With all Control DirectX, it seems strange to you that there is no control over this, as it seems to be a good optimization :)

Any information or links to this are assigned

+4
source share
1 answer

As for depth testing, DirectX says it should appear that depth testing happens after the pixel shader, it does not say that it is actually necessary. In fact, early z has existed for many years on equipment from many manufacturers. Often there is even an early form of Z-testing called Hierarchical Z, which doesnโ€™t work on individual pixels, but on โ€œtilesโ€ of many pixels at a time to avoid the cost of early Z.

Early Z is not something you can turn on or off in any particular state that you have installed on the device. The hardware will perform z-testing as early as possible and in such a way that you do not know that it is somewhere else than after the pixel shader.

There are certain things you can do, although this may limit the hardware in its ability to perform z-testing, as it might otherwise. Alpha testing, the use of "drop" (killing a pixel) and alpha coverage will certainly disable the early z recordings, since pixel shaders must be run before the equipment can determine whether to write the depth value or not. If you use alpha testing / discarding and don't need z-write, turn them off and you will have the best opportunity for early z to be available.

Modifying / writing "depth" in a pixel shader is definitely a no-no if you want an early Z. In this situation, the hardware cannot even perform an early test, because it does not yet recognize the pixel depth until you solve it in the pixel shader, it cannot run an early z-test or an early z-record.

If you need to write depth from a pixel shader, but you can guarantee that you will only write a depth value greater than or equal to that created by the rasterizer, you can use semantics of semantics without the undocumented SV_DepthGreater . Since you promise not to write the depth value less than the interpolated depth, the hardware can still try and run the early z test, but then defer the z record to the end of the pixel shader. (There is the equivalent of SV_DepthLessEqual if you use inverted z-test / z-buffer).

Since it should appear that z-testing occurs at the end of the pipeline, the use of UAVs in the pixel shader will also disable the early z. Since the goal of rendering is now not the only way out, and since the DirectX specification says that it should appear that z-testing happens at the end, it follows that your entries in the UAV should occur even for pixels that eventually come out of for the z-test. For this reason, an attribute was added to Shader Model 5 called [earlydepthstencil] , which tells DirectX that you are happy at the beginning of z (if possible), and not start the pixel shader, even if you have UAV records.

So, if you are not doing some of the slightly bizarre things mentioned above (changing depth using SV_DEPTH, alpha coverage, using clip / reset, etc.), chances are you already get the advantage of early Z. Always turn off z- records if you do not want them, and avoid writing to SV_DEPTH, since you absolutely do not get early z with it turned on.

+16
source

Source: https://habr.com/ru/post/1493815/


All Articles