• FEATURED

View more

View more

View more

\$40

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

## Reconstructing Position from Depth - Why?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1KoldGames  Members

Posted 25 February 2014 - 11:32 PM

Hello everyone! After finishing my deferred renderer, I was looking over MJP's article about reconstructing position from depth and was wondering, why do this?  Is it better to convert my code to this (because I tried to convert my shader code and had no luck in getting a working renderer lol)?  What are the advantages & disadvantages of doing this?  How will this help in the future?  I'm currently storing projection-space depth.  Thanks!

### #2MJP  Moderators

Posted 26 February 2014 - 12:42 AM

Ok so there's 2 issues here:

1. You don't want to store position in a render target, because that's a waste of memory. 16-bit will give you precision issues which means using 32-bit, which is a lot of memory. So instead you want to just use a single depth value. Ideally you just want to use the hardware depth buffer if you can access it, otherwise you can render the depth value yourself.
2. If you're going to use depth, there's a few ways of doing it. These different methods can have trade-offs between precision and the amount of math required.

The articles I wrote were mainly about #2. At the time I was working on PS3 where a bit of math optimization could go a long way, but depending on your target hardware it might not make any difference whatsoever. So if you're not worried about micro-optimizing, then just go with whatever is easiest. For most people this equates to using projection-space z/w, and multiplying by the inverse of your view * projection matrix to get world-space position. However if you want to get a little more performance, you can still use z/w from a hardware depth buffer and use a cheaper reconstruction to get view-space position. Working in view-space is also generally a little cheaper for lighting calculations as well. If you're working on a platform where you can't access a hardware depth buffer, then you can consider rendering out a linear depth value instead of z/w so that you can use a cheaper reconstruction.

### #3KoldGames  Members

Posted 26 February 2014 - 10:09 PM

Ok so there's 2 issues here:

1. You don't want to store position in a render target, because that's a waste of memory. 16-bit will give you precision issues which means using 32-bit, which is a lot of memory. So instead you want to just use a single depth value. Ideally you just want to use the hardware depth buffer if you can access it, otherwise you can render the depth value yourself.
2. If you're going to use depth, there's a few ways of doing it. These different methods can have trade-offs between precision and the amount of math required.

The articles I wrote were mainly about #2. At the time I was working on PS3 where a bit of math optimization could go a long way, but depending on your target hardware it might not make any difference whatsoever. So if you're not worried about micro-optimizing, then just go with whatever is easiest. For most people this equates to using projection-space z/w, and multiplying by the inverse of your view * projection matrix to get world-space position. However if you want to get a little more performance, you can still use z/w from a hardware depth buffer and use a cheaper reconstruction to get view-space position. Working in view-space is also generally a little cheaper for lighting calculations as well. If you're working on a platform where you can't access a hardware depth buffer, then you can consider rendering out a linear depth value instead of z/w so that you can use a cheaper reconstruction.

Hey MJP! Thanks for the reply.  I am all about optimization. Do you mind helping me out real fast?  I'm pretty confused on how I setup my shaders.  I have my depth shader working right but cannot get my Point Lights to work correctly. This is my GBuffer shader:

GBufferVS.hlsl

cbuffer MatrixBuffer : register(b0)
{
float4x4 worldMatrix;
float4x4 viewMatrix;
float4x4 projectionMatrix;
}

struct VertexShaderInput
{
float4 Position : POSITION0;
float3 Normal : NORMAL0;
float2 TexCoord : TEXCOORD0;
};
struct VertexShaderOutput
{
float4 Position : SV_POSITION;
float2 TexCoord : TEXCOORD0;
float3 Normal : TEXCOORD1;
float Depth : TEXCOORD2;
};

VertexShaderOutput main(VertexShaderInput input)
{
VertexShaderOutput output;

output.TexCoord = input.TexCoord;
output.Normal = mul(input.Normal, (float3x3)worldMatrix);

float4x4 matWorldView = mul(worldMatrix, viewMatrix);
float4 vPositionVS = mul(input.Position, matWorldView);
output.Position = mul(vPositionVS, projectionMatrix);
output.Depth = output.Position.z;

return output;
}


GBufferPS.hlsl

#include "common.hlsli"

float specularIntensity = 0.8f;
float specularPower = 0.5f;

Texture2D shaderTexture : register(t0);
sampler diffuseSampler = sampler_state
{
Texture = (shaderTexture);
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
MIPFILTER = LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};

struct VertexShaderOutput
{
float4 Position : SV_POSITION0;
float2 TexCoord : TEXCOORD0;
float3 Normal : TEXCOORD1;
float Depth : TEXCOORD2;
};

struct PixelShaderOutput
{
half4 Color : SV_TARGET0;
half4 Normal : SV_TARGET1;
half4 Depth : SV_TARGET2;
};

PixelShaderOutput PS(VertexShaderOutput input)
{
PixelShaderOutput output;
output.Color = shaderTexture.Sample(diffuseSampler, input.TexCoord);
output.Color.a = specularIntensity;
output.Normal.rgb = 0.5f * (normalize(input.Normal) + 1.0f);
output.Normal.a = specularPower;
output.Depth = half4(input.Depth / g_farClip, 1.0f, 1.0f, 1.0f);

return output;
}


This seems to output the correct data.

I've trying to get my point lights to work all day.  I've tried many things but here's how my point light shader is currently setup:

PointLightVS.hlsl

cbuffer TransformBuffer : register(b0)
{
float4x4 World;
float4x4 View;
float4x4 Projection;
}

struct VSInput
{
float3 Position : POSITION;
};

struct VSOutput
{
float4 Position : SV_Position;
float4 ScreenPosition : TEXCOORD;
};

VSOutput VS(VSInput input)
{
VSOutput output;
float4 worldPosition = mul(float4(input.Position, 1), World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
output.ScreenPosition = mul(float4(input.Position, 1), mul(World, View));
return output;
}


PointLightPS.hlsl

#include "common.hlsli"

cbuffer LightDataBuffer : register(b0)
{
float2 halfPixel;
float3 color;
float3 cameraPosition;
float4x4 InvertViewProjection;
float3 lightPosition;
float lightRadius;
float lightIntensity = 1.0f;
}

Texture2D colorMap : register(t0);
Texture2D normalMap : register(t1);
Texture2D depthMap : register(t2);

sampler colorSampler = sampler_state
{
Texture = (colorMap);
AddressU = CLAMP;
AddressV = CLAMP;
MagFilter = LINEAR;
MinFilter = LINEAR;
Mipfilter = LINEAR;
};

sampler depthSampler = sampler_state
{
Texture = (depthMap);
AddressU = CLAMP;
AddressV = CLAMP;
MagFilter = POINT;
MinFilter = POINT;
Mipfilter = POINT;
};

sampler normalSampler = sampler_state
{
Texture = (normalMap);
AddressU = CLAMP;
AddressV = CLAMP;
MagFilter = POINT;
MinFilter = POINT;
Mipfilter = POINT;
};

struct VSOutput
{
float4 Position : SV_Position;
float4 ScreenPosition : TEXCOORD;
};

float4 PS(VSOutput input) : SV_TARGET
{
input.ScreenPosition.xy /= input.ScreenPosition.w;

float2 texCoord = 0.5f * (float2(input.ScreenPosition.x, -input.ScreenPosition.y) + 1);
texCoord -= halfPixel;

float4 normalData = normalMap.Sample(normalSampler, texCoord);
float3 normal = 2.0f * normalData.xyz - 1.0f;

float specularPower = normalData.a * 255;
float specularIntensity = colorMap.Sample(colorSampler, texCoord).a;

float3 position = VSPositionFromDepth(depthMap, depthSampler, input.ScreenPosition);

float3 lightVector = lightPosition - position.xyz;

float attenuation = saturate(1.0f - length(lightVector) / lightRadius);

lightVector = normalize(lightVector);

float NdL = max(0, dot(normal, lightVector));
float3 diffuseLight = NdL * color.rgb;

float3 reflectionVector = normalize(reflect(-lightVector, normal));
float3 directionToCamera = normalize(cameraPosition - position.xyz);
float specularLight = specularIntensity * pow(saturate(dot(reflectionVector, directionToCamera)), specularPower);

return attenuation * lightIntensity * float4(diffuseLight.rgb, specularLight);

}

float g_farClip = 1000.0;

float3 VSPositionFromDepth(Texture2D depth, sampler dSampler, float4 vTexCoord)
{
float3 viewRay = float3(vTexCoord.xy * (g_farClip / vTexCoord.z), g_farClip);
float normalizedDepth = depth.Sample(dSampler, vTexCoord).r;
return viewRay * normalizedDepth;
} 

It has been driving me crazy.  I can get everything working if I do what I was doing before, but I would like some micro optimization.  Nothing I tried has worked so far when trying to reconstruct from depth.  This is the output I get with the code above:

My texture coordinates seem to be messed up.  I am really not doing something right. Different parts of the screen are shown like above when I look around.  My latest attempt is a huge #fail. Any help? Thanks!

Edited by KoldGames, 26 February 2014 - 10:18 PM.

### #4MJP  Moderators

Posted 27 February 2014 - 06:05 PM

In your G-Buffer shader, you're using Position.z. You'll want to use Position.w instead. Position.w will contain the view-space z value, which is the same as vPositionVS.z.

### #5kalle_h  Members

Posted 28 February 2014 - 08:55 AM

In your G-Buffer shader, you're using Position.z. You'll want to use Position.w instead. Position.w will contain the view-space z value, which is the same as vPositionVS.z.

Did you mean -Position.w.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.