Linearized depth for shadowmap


for improving precision of my shadow maps I would like to use linear depth distribution of the z-buffer. I have tried to modify the projection matrix like this, also playing around with different signs:

mp[10] = 1 / (far - near)
mp[14] = -n / (far - near)

Additionally I am premultiplying the transformed z with w in the vertex shader but it doesn’t seem to work. Has anyone done this before?

Thanks a lot!

I think you shouldn’t modify the projection matrix at all as the outcome of multiplying the vertices with this matrix will be used for proper clipping. Changing it will only get you into trouble later on. Instead compute the linear depth value in the vertex shader based on camera and vertex position and store this into a floating point render target. I think with proper scaling into the [0, 1) range it would also be possible to use an RGBA8 target with some packing and unpacking.

[ | vector_math (3d math library) | software renderer ]