When I output a vertex from a vertex shader, it seems to have a world-space-ish mapping. Vertices about 1 unit away have about 1 unit as their Z value.

When we input into the pixel shader, the depth values are mapped from 0..1 as defined in the viewport state.

Where does this conversion occur? It's late and I'm forgetful, but I can't find a place where I told DirectX what my depth range even was (other than implicitly in the projection matrix). How does it know how to convert it?

  • \$\begingroup\$ if it's like openGL then with the perspective matrix plus perspective division. \$\endgroup\$ – ratchet freak Apr 13 '15 at 8:24

The Z coordinate has a different meaning when we're talking about viewport coordinates.

In most vertex shaders, at some point you are multiplying each vertex by a projection matrix in order to convert world coordinates into viewport coordinates.

After leaving the vertex shader, each vertex will be then divided by the W value. The resulting value is now in viewport coordinates, where triangles whose vertices are all outside of the (-1, -1, -1), (1, 1, 1) cube are culled.

In viewport coordinates, X and Y are mapped to X and Y in the screen ((0, 0) being the center of the screen), and Z to the depth of the vertex (-1 being the near plane, and 1 the far plane).

After interpolation, each fragment will then have a Z coordinate, which can be used for Z-buffering and Z-testing if you wish to.

  • \$\begingroup\$ Thanks for the info. I was still a bit confused how the divide-by-w converted the range into 0-1, but after looking into a bit more I believe I understand now. Thank you! \$\endgroup\$ – Raptormeat Apr 14 '15 at 1:17

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.