Hi recently i am trying to implement z ordering system into my directx framework. So every object will have z order property(int), and this value will have higher priority than depth checking for rendering. the current idea is from these three my assumptions that

  1. depth check happens after vertex shader
  2. value of SV_POSITION is used for depth checking
  3. modifying z component of the value of SV_POSITION doesn't affect anything except in depth checking, because it is in projection space.

so after calculating projection position(local pos * WVP mat), i did projective dividision(it seems to make problem) and modified only z component corresponding orders that i want to draw with.

This is brief Vertex shader code

VS_OUTPUT output;

output.pos = mul(WVPMat, input.pos); // projection pos
output.pos /= output.pos.w; 
output.pos.z -= renderPriority;

return output;

And !!! it kinds of works !!! but when i get my camera closer to any object, it weird thing happens enter image description here

it doesn't make any this weird thing unless projective division. i am just curious why it happens instead of knowing another alternative way for z ordering. but any further advices and informations would be really appreciated.

  • \$\begingroup\$ Would you not just do output.pos.z -= renderPriority * output.pos.w? Just a thought, I can't test it here myself. But you want to alter the Z Depth based upon the normalised value in your view space. What it looks like from your screenshot is that the amount of Z you are altering it by is very large. Also, can you clarrify your assumption 1, that depth checking occurs before Vertex shader? Are you saying you are ordering yourself then yes, but if you are referring to the GPU pipeline, it doesn't, it can occur before or after the pixel shader (Depending on what HLSL settings you do). \$\endgroup\$
    – ErnieDingo
    Aug 27, 2019 at 22:06
  • \$\begingroup\$ Thanks @ErnieDingo, that is really good idea. and i wonder how to change that rendering order?? is it really true that i can change the pipeline operation order? for example, depth checking even after pixel shader?!!! sounds cool! \$\endgroup\$ Aug 28, 2019 at 15:56
  • \$\begingroup\$ You can turn off optimistic z elimination prior to pixel shader. It's a directive you add in prior to your pixel shader main. \$\endgroup\$
    – ErnieDingo
    Aug 28, 2019 at 20:23

2 Answers 2


When the object is behind the camera, then the z coordinate is negative. The projection matrix uses that negative value and scales it and places it in the w coordinate. Since this is still negative, it will flip every coordinate. You need to clip the triangles before this happens


[Alternative Solution] Thanks for help guys. i just found out with your helps that my idea won't work since the depth in pipeline will be restricted only between 0~1.

Here is my another solution that i figured out. i post it here just in case for someone who need z order system. i came to modify projection matrix calculation. and have multiple of it, so that i have 5 hierarchy for rendering in my framework. you can set as many as you want btw.

// in class of application, not shader of GPU
#define Z_ORDER_MAX         5

#define Z_ORDER_UI          0
#define Z_ORDER_HIGHER      1
#define Z_ORDER_STANDARD    2
#define Z_ORDER_LOWER       3

projMats = new XMMATRIX[Z_ORDER_MAX];

for (int i = 0; i < Z_ORDER_MAX; ++i)
    float minD = i * 0.2f;
    float maxD = (i + 1)*0.2f;
    float A = (maxD * f - minD * n) / (f - 1));
    float B = n * (minD - A);
 // i can guess, after multiplication and perspective division, z value would be 'A+B/z'.  and then instead ranging them between 0~1, i does 0~0.2/0.2~0.4 so on.

    projMats[i] =
            1.0f / (aspectRatio*tan(verticalViewAngle*0.5f)), 0, 0, 0,
            0, 1.0f / tan(verticalViewAngle*0.5f), 0, 0,
            0, 0, A, 1,
            0, 0, B, 0);

and when you render any object, you choose and use the projMatrix corresponds to z order index that you want to set.


You must log in to answer this question.

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