2
\$\begingroup\$

In DirectX11, what does the depth that a Pixel Shader can output mean? Does a pixel shader need to output a depth so that we "write" the depth? If not, why would we output it?

For any doubts, I'm talking about the depth described in the last paragraph of this page of the MSDN documentation:

http://msdn.microsoft.com/en-us/library/windows/desktop/bb205146(v=vs.85).aspx#Pixel_Shader_Stage

\$\endgroup\$
1
  • \$\begingroup\$ Don't output depth unless your shader absolutely needs to! Most graphics cards do two depth culling passes, "early-Z" and "late-Z". If your shader doesn't write depth, the GPU can do "early-Z", which means that if the fragment is behind something or won't be shown, it can skip a lot of processing and never even run your shader. You don't need to know what all that means, but only write to SV_Depth if your algorithm absolutely requires it (or you're doing some screen space thing where depth hasn't been written at all yet). \$\endgroup\$ Commented Nov 20, 2013 at 2:50

2 Answers 2

2
\$\begingroup\$

The depth it is referring to is the depth value of a pixel, often (though not always) on a scale of 0 - 1 (relative, although usually nonlinearly, to the near plane and far planes).

This value is used for something called depth testing, or z-testing. Depth testing is an optimization that prevents pixels from being written to the buffer if they would be occluded by something that is closer to the near plane. This also makes ordering of opaque objects less important, and enables many post processing effects that are dependent on depth. This link provides additional information.

Depth testing is an optional step in the rendering pipeline that can be enabled or disabled at will. You will also frequently hear about the depth stencil buffer, which is a single buffer, that combines both depth and stencil testing values into separate channels as an optimization.

\$\endgroup\$
7
  • \$\begingroup\$ Ok! What I mean is, if we don't do this "output" described in the MSDN page I posted (for instance, I'm working now on a pixel shader that returns just a color), is the depth buffer written or not? I thought it was, because we have a vertex shader and we don't really need to define a pixel shader... Am I wrong there? I've also seen pixel shaders returning just the color and depth testing working as normal... \$\endgroup\$
    – c4sh
    Commented Nov 19, 2013 at 16:03
  • \$\begingroup\$ A pixel shader is always required, the only way you wont have one is if the particular system you have supports a fixed function alternative. Also, a pixel shader outputs a color, that is all (under normal usage), the actual depth buffer writing and comparison is done in the output merger stage, which is something you don't have control of. Enabling or disabling z-testing is done via render states. \$\endgroup\$
    – Evan
    Commented Nov 19, 2013 at 16:11
  • \$\begingroup\$ I stand corrected here, as it seems the depth writing is done at the end of the pixel shader stage, while only the depth test is performed in the output merger. You wont manually write out the depth value though, this will be done automatically if enabled. \$\endgroup\$
    – Evan
    Commented Nov 19, 2013 at 16:20
  • 1
    \$\begingroup\$ Yes. You may optionally override the "default" value for the fragment depth by writing to it in the shader, in which case that is the value used for further computation and tests later in the pipeline. \$\endgroup\$
    – user1430
    Commented Nov 19, 2013 at 16:37
  • 2
    \$\begingroup\$ Note: you don't want to override the value unless you really need to. Hardware supports an optimization called "early Z testing" that allows is to do the depth test before running the pixel shader, which is only enabled if the shader does not write its own depth values (because the assumption of early Z is that the depth is independent of the shader in use). Even if you just write the normal depth value, the driver generally doesn't know that; all it knows is whether you write to the depth register or not. \$\endgroup\$ Commented Nov 19, 2013 at 17:57
0
\$\begingroup\$

Depth Testing has it's advantages in Shadow Mapping and Light Mapping. If you were to create a render target at a point light location - you can have interesting effects with this sort of thing. I believe that's how ambient occlusion works but I've not yet ventured in that area yet. I want to maybe soon. I believe deferred lighting also uses depth buffer, normals, diffuse and another render target which I'm not sure what it is. In addition, I believe that's how sub-scattering surfaces work too! I have to dig deeper and get my hands into that stuff before I can say it's for certain.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .