• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
KoldGames

Reconstructing Position from Depth - Why?

4 posts in this topic

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! :)

0

Share this post


Link to post
Share on other sites

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.

m0q2.png

bxna.png

4hjj.png

 

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:

 

y4it.png

3jes.png

sc1g.png

z4jr.png

 

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! smile.png

Edited by KoldGames
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0