[SOLVED] HLSL - Getting the Normalized ScreenPosition

Hi !

I’m certainly doing something wrong. I’m drawings quads for particles and I also read from a depth texture (to not render them if needed to, and soft particles later)
It used to work in XNA, but not with MonoGame :confused:
Problem is: the UV coordinates in the input of the pixelshader correspond to the quad rendered. So the depth is applyed to each quad, one quad = one depth texture, not onto the whole screen.
So I wanted to get the normalized position of a pixel: in clipspace, x is [-1, 1], z [0, 1] (for opengl it is [-1, 1] right ?) , and y is in [0,1] with 0 bottom right.
Then I did this:

float GetScreenDepth(float4 projectedpos){
	//Scale-bias the clipping space pos from [-1,1] to [0,1]
	float2 NormalizedScreenPos = 0.5 * projectedpos.xy / projectedpos.w + 0.5;
	//Flip the y-coordinate as the origin of the window and the texture are different
	NormalizedScreenPos.y = 1 - NormalizedScreenPos.y;
	float dpth = depthModelsTexture.SampleLevel(DepthModelsSampler, NormalizedScreenPos, 0).r;
	return dpth;

Where projectedpos is computed in the vertex like this:
Output.Position = mul(float4(Output.Position.xyz, 1), mul(View, Projection));

then passed to GetScreenDepth() as


Where did I fail ?

As you can, the silhouette of the character is repeated for each quad and clip occurs on each of them too:

Here is the render of NormalizedScreenPos values

Top left is black as it should, red and green too, until yellow on the bottomright as expected:

indent preformatted text by 4 spacesreturn float4(NormalizedScreenPos, 0 ,1);

Ok, I have solved it myself ^^
Need to sleep more than 5 hours a night…

This is faster than I thought. But I did not test with more than 10000 particles… depth peeling will be overkill with such an amount.

EDIT: in fact this topic could be deleted…