Understand Vulkan Clipping

Hello guys, me again :wink:
i am trying to implement shadow-mapping properly for a directional-light (with fitting).
So i first implemented ortho-graphic projection for my camera-class, but here is the weird-thing:
In the LunarG Vulkan-Examples there is this line of code:

     // Vulkan clip space has inverted Y and half Z.
            clip = Mat4f(1.0f, 0.0f, 0.0f, 0.0f,
                         0.0f, -1.0f, 0.0f, 0.0f,
                         0.0f, 0.0f, 0.5f, 0.0f,
                         0.0f, 0.0f, 0.0f, 1.0f);

I did the same thing for my perspective projection and i recognized after a few hours that this is the reason why my orthographic projection doesnt work properly.
I removed this when calculating the orthographic-projection and it works almost as intended.
When i manipulating the zNear/zFar value the clipping is not correct, but the projection matrix is correct.
By the way “depthClampEnable” in the Rasterization-State is set to false.

My question is how vulkan does the clipping and why we need that matrix above? I did not find anything about this in the spec.

YOU again! :wink:

That matrix is not strictly speaking necessary. The author probably used it just to make his old (OpenGL) code work. It just converts from [-1,1] to [0,1] space (by z=0.5oldZ + 1) and inverts the y (-1oldY).

As for clip-space and NDC you are not first to ask https://forums.khronos.org/showthread.php/13055-Which-matrix-order-handedness-and-Z-range-expects-Vulkan

Okay, but that doesnt explain why the culling is not correct. I tripple checked the matrices, they should be correct. When i modify zFar it affects somehow the zNear Plane (but does not modify the zNear value), look at the video: https://www.youtube.com/watch?v=EMFEEqF_BTM&feature=youtu.be

spongebob and patrick
Respect man :slight_smile:

PS: Your videocard AMD ?

[QUOTE=Ronniko;40768]spongebob and patrick
Respect man :slight_smile:

PS: Your videocard AMD ?[/QUOTE]

Yes. AMD Radeon HD 7870. But same thing on a NVIDIA card.

Does nobody have an idea why manipulating the zFar value affects somehow clipping on the zNear plane? Couldnt find a solution on my own so far :frowning:

BTW the matrix as you written it is wrong. There is missing 0.5 somewhere.

EDIT: which would of course affect that.

Im so happy! The matrix has to be transposed:

    // Vulkan clip space has inverted Y and half Z.
            clip = Mat4f(1.0f, 0.0f, 0.0f, 0.0f,
                0.0f, -1.0f, 0.0f, 0.0f,
                0.0f, 0.0f, 0.5f, 0.5f,
                0.0f, 0.0f, 0.0f, 1.0f);

In the LunarG examples there is this matrix:

    // Vulkan clip space has inverted Y and half Z.
            clip = Mat4f(1.0f, 0.0f, 0.0f, 0.0f,
                0.0f, -1.0f, 0.0f, 0.0f,
                0.0f, 0.0f, 0.5f, 0.0f,
                0.0f, 0.0f, 0.5f, 1.0f);

But i did not understand why. Can somebody explain it to me?
Thanks

Looks like a row-major column-major difference.

But which would be the proper projection matrix for Vulkan?

Instead of picking up a OpenGL projection matrix and transform it afterwards, which would be the proper projection matrix that would do the whole work in a single step?