• Advertisement
Sign in to follow this  

Space conversion question

This topic is 799 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I got stuck with simplest SSAO implementation.

The algorithm is simple: get depth values for current pixel and depth for the pixel with some offset.

I decided to compare depths in View Space.

 

posVS => reconstructed position from Depth buffer. (All values in it are in (0,1) range)

ReconstructPixelPos() does it.

 

sampleOffsetWS – vector from generated array data made by application. Each vector is in range ((-1,1), (-1,1), (0,1))

I need to load depth from depth texture for a sample.

 

So there are 2 options:
  1.Convert sampleOffsetWS to texture coordinates and add it to pixel texture coordinate.
  2.Sum posVS and sampleOffsetWS and transform them to texture space.

I know that solution is simple, but nothing came to my mind =(

 

Can anyone help?

 

float main(float4 ScreenPos : SV_Position, float2 TexCoord : TEXCOORD0) : SV_TARGET 
{
	float3 posVS    = ReconstructPixelPos(ScreenPos.xy);
	...
	float totalOcclusion = 0;
	for (int i = 0; i < g_OffsetSampleCount; ++i)
	{
		float3 sampleOffsetWS = OffsetSamples[i].xyz; //Get sample offset in range ((-1,1), (-1,1), (0,1))
		//TODO: reflect it over pixel normal.
		float3 sampleOffsetTS = ??????????
		float3 samplePosTS    = float3(TexCoord, 0) + sampleOffsetTS;

		float  sampleDepth  = SampleDepth(samplePosTS.xy);

		if (posVS.z > sampleDepth)
		{
			totalOcclusion += 1;
		}
		...
}


float3 ReconstructPixelPos(float2 svPos)
{
	int3 screenCoord = int3(svPos, 0);
	float clipDepth = DepthTexture.Load(screenCoord);

	//Unproject position in VS, InvM11, InvM22, M43 and M33 – are from projection matrix
	float3 posVS = float3(svPos.x * InvM11, svPos.y * InvM22, M43 / (clipDepth - M33));
	return posVS;
}
float SampleDepth(float2 texPos)
{
	float  clipDepth = DepthTexture.Sample(OffsetsSampler, texPos, 0);

	float  depth = M43 / (clipDepth - M33);
	return depth;
}

And here is a bonus question: smile.png

 

I have position reconstruction function.

The texture is a full-screen quad and I am interesting in current pixel without any offsets/sampling.

I have an assumption, that loading data via Texture2D::Load() directly from the texture is faster than sample it.

Is this assumption correct?
 

But svPos is always contains non-int value with 0.5 at the end.
In your opinion, is this a good idea to Load() data or it’s better to sample, using texture coordinates instead of SV_Position?
 

float3 ReconstructPixelPos(float2 svPos)
{
	int3 screenCoord = int3(svPos, 0);
	float clipDepth = DepthTexture.Load(screenCoord);
Edited by Happy SDE

Share this post


Link to post
Share on other sites
Advertisement

But when you pass coords to Load(), they are integers and in (0..width/height - 1) range, since you're indexing the texture directly!

Thank you, Venterberg!

 

I want to make one thing a little bit clearer:

When I have SV_Position in format float2{10.5, 120.5}, and after that I truncate it to int2{10,120}.

This truncation will remove 0.5 from each element.

 

There should no be  any image quality reduction after float->int cast.

Is this correct?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement