Matrix parameter not recognised in pixel shader?

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.

How are you setting the Projection value from C#? That would be my best guess as to where the problem lies, as I’m setting my Projection as a matrix in pretty much all my shaders and not seeing this particular issue.

Shader snippet:

matrix Projection;
VSOutput VSQuad(VSInput input)
{
	VSOutput output;

	float4 worldPosition = mul(input.position, World);
	float4 viewPosition = mul(worldPosition, View);
	output.position = mul(viewPosition, Projection);

	// Lots of unrelated code

	return output;
}

And in code when setting:

UnitRenderEffect.Parameters["Projection"].SetValue( projMat );

Where projMat is a Matrix object and UnitRenderEffect is an Effect object.

I did a quick sanity check and changed my Projection variable in the shader from matrix to float4x4 but it still worked so I don’t think there is an issue there.