I poured over this for an hour or two last night, and I’m stumped.
Short story is, I was trying to convert a depth texture (grey scale, linear, 0-1) into the original screen space z and w coordinates. I did something like this (I’ve since deleted it and moved on, so this is just from memory):
Texture2D Depth;
float MaxDepth;
float4x4 Projection;
...
PixelShaderOutput PixelShaderFunction(VertexShaderOutput input)
{
...
float sample = tex2D(DepthSampler, input.TexCoord).z; // positive z, 0-1
float z0 = -z * MaxDepth; // original view space z coordinate, negative, up to -MaxDepth
float z = z0 * Projection._33 + Projection._43;
float w = z0 * Projection._34 + Projection._44;
output.Depth = z / w;
}
Now, looking this over, I notice that there’s a little bit of a logic error involving calculating the “z0” variable, but that’s not the point here. The point is, my z and w values come out to be 0. Logic error aside, that shouldn’t be the case. It’s as if the Projection variable contains all zeroes.
To further investigate, I tried replacing all of that logic with simply:
output.Depth = saturate((abs(Projection._33) + abs(Projection._34) + abs(Projection._43) + abs(Projection._44)) * 100);
…just to try to get something, anything, to show up, but the results were still 0.
The same thing happened when I tried using
mul(float4(0, 0, z0, 1), Projection)
…so it’s not just the _33 syntax (I used that in vertex shaders elsewhere, so that’s fine).
I also checked the constant buffers in the GraphicsDevice object, and, sure enough, I did see the 64-byte matrix data where it should be.
Finally, I gave up and passed those four necessary components in as a float4 vector, like so:
float4 Projection;
...
float z = z0 * Projection.x + Projection.z;
float w = z0 * Projection.y + Projection.w;
output.Depth = z / w;
And that worked just fine. So, it seems to me that there’s something wrong with transferring the matrix parameters/constants to the pixel shader, or I’ve overlooked some inherent limitation of HLSL’s pixel shaders’ capabilities in the first place.