Skip to content

Add matrix functions for generating infinite reversed-z perspective projection matrix #158

Open

Description

@TheRealMJP

Hello, I think it would be really great if DirectXMath added support for generating perspective projection matrices with infinite reversed-z clip planes. This style of projection matrix has become increasing popular in recent years since they result in excellent distribution of depth precision when used with floating-point depth buffers, and they effectively remove the need for having a maximum view distance. This blog post gives a good overview of why it's good and how it works, and this article has some great visualizations of why reversed-z is beneficial.

Activity

  1. walbourn commented on Jan 29, 2023

    @walbourn
    Collaborator

    Thanks for the feedback. It turns out you can trivially use the existing functions for reverse-z: swap the far/near values:

    m_proj = XMMatrixPerspectiveFovLH(XM_PIDIV4, aspect, c_farZ, c_nearZ);
    

    Be sure to clear the depth-buffer with 0.0 instead of 1.1, and use the reverse depth comparison operator for the Depth/Stencil state (i.e. D3Dxx_COMPARISON_GREATER_EQUAL instead of D3Dxx_COMPARISON_LESS_EQUAL).

    I have support for reverse-z in the DirectX Tool Kit CommonStates.

  2. TheRealMJP commented on Jan 30, 2023

    @TheRealMJP
    Author

    Hey Chuck Walbourn (@walbourn), thank you for replying! To be clear I was specifically referring to the "infinite" variant of a reversed-z projection, which cannot be done with the existing perspective matrix functions (finite reversed projections can be done as you suggested). The infinite reversed matrix has this form for the LH variant:

    xScale     0          0             0
    0        yScale       0             0
    0          0          0             1
    0          0          znear         0
    
  3. walbourn commented on Jan 30, 2023

    @walbourn
    Collaborator

    Thanks for the clarification.

  4. walbourn commented on Feb 14, 2024

    @walbourn
    Collaborator

    Thank you for the suggestion. I definitely see the value in this, but I'll need a bit more time to do the full validation/verification this warrants. In the meantime, you can of course easily create your own projection matrix with existing constructors and functions.

  5. Ossama-MOUSTAFA commented on Oct 25, 2026

    @Ossama-MOUSTAFA

    +1 for this feature.

    Please add built-in functions for a Right-Handed NDC (Reverse-Z).
    It is much simpler than swapping nearZ and farZ manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions