I'm rendering simple 2D figures in DirectX 11.1 and I cannot get Z-values to be rendered; all pixels with Z != 0 get discarded. I'm doing proper world transform as far as I know. X/Y translations work. Rotation over Z works.

Do I need to setup a depth/stencil buffer to avoid DirectX to discard all primitives with Z coordinates?

Thanks, I'll supply any additional information ,code or screens if required.


I've fixed D3D11_VIEWPORT minDepth and MaxDepth members to 0.0/1.0 (I was clearing both to zero). Now pixels with 1.0 < z < 0.0 are discarded.


My platform is Windows Runtime App (Win8.1 / WinPhone 8.1).

Example screenshots.

Rotated triangle over the Y-axis (pixels with Z>1 discarded)

Rotated triangle over the Y-axis (pixels with Z>1 discarded)

Vertex at (-0.5,-0.5) with z=2. Clipped.

Triangle Vertex at (x=-0.5,y=-0.5) with z=2. Clipped and hidden.

  • 1
    \$\begingroup\$ I'd suggest that you try changing the parameters of the DepthStencilState and try introducing a depth buffer, to see if things change. You can easily construct a DSS with default parameters by using the CD3D11DEPTH_STENCIL_DESC helper constructed with an CD3D11_DEFAULT{} argument. \$\endgroup\$ – Lars Viklund Dec 28 '15 at 18:00
  • \$\begingroup\$ Yes, to be more precise only pixels with 0 < Z < 1 got rendered. I'll add this data to the question. Thanks for your suggestion. \$\endgroup\$ – Hernán Dec 28 '15 at 18:02
  • \$\begingroup\$ Are you using a projection matrix, and if so, is it properly made or does it project all but a sliver of your geometry out of bounds? \$\endgroup\$ – Lars Viklund Dec 28 '15 at 18:03
  • \$\begingroup\$ No projection matrix is enabled in the vertex shader, only the world transformation one. \$\endgroup\$ – Hernán Dec 28 '15 at 18:04
  • 1
    \$\begingroup\$ Note that Direct3D clip space is a halfcube with [-1 to +1] on X and Y, and (0 to +1] on Z. You're only going to see Z between 0 and Z/W, which if you're showing unprojected geometry with W=1, is 0 and 1. \$\endgroup\$ – Lars Viklund Dec 28 '15 at 22:39

A common mistake is to zero out MinDepth and MaxDepth of the D3D11_VIEWPORT, effectively clamping all depth to the near plane.

The default depth states are test enabled, write enabled, compare less. See the list at MSDN.

In Direct3D 11, normalized device coordinates in clip space after W division run from [-1.0, +1.0] on the X and Y axes, and (0.0, 1.0] on the Z axis.

If your post-projection geometry has a Z/W <= 0.0 or 1.0 < Z/W, the fragments will be clipped.

| improve this answer | |
  • \$\begingroup\$ I was thinking of the same issue regarding D3D11_VIEWPORT, it was zeroed in effect, I changed MinDepth to 0 and MaxDepth to 1, but no change. \$\endgroup\$ – Hernán Dec 28 '15 at 17:55

As @Lars Viklund stated, I didn't multiply my vertices in the shader to apply the projection transform, so I was constrained to the bounds of the clip space Z (0..1]. Adding a proper view and transformation matrix solved the problem.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.