# Space conversion question

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

## 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);

//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:

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 on other sites

The Load() function should be faster, because it bypasses filtering etc.

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

https://msdn.microsoft.com/en-us/library/windows/desktop/bb509694%28v=vs.85%29.aspx

Edited by vinterberg

##### Share on other sites

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 on other sites

That's true, just truncate it and it'll work fine ^^

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633767
• Total Posts
3013734
×