Deferred Shading with Reflections

Screenshot from my upcoming game Pool Shark Pro™ shows the GBuffer (Diffuse, Depth, Normal and Reflection color)

(Click the screenshot to see the whole thing)

Videos here:

YouTube Channel

Reconstructing position from depth

This is my technique for reconstructing position from depth *and* sampling the GBuffer:

Vertex Shader

static const float4x4 gMtexProj = float4x4(
	0.5, 0.0, 0.0, 0.0
,	0.0,-0.5, 0.0, 0.0
,	0.0, 0.0, 1.0, 0.0
,	0.5, 0.5, 0.0, 1.0

struct VertIn {
	float4 P			: POSITION0;

struct VertOut {
	float4 Poutput		: POSITION0;
	float4 P			: TEXCOORD0;
	float4 T			: TEXCOORD1;

VertOut VertFn(VertIn Input) {
	VertOut Output;

	float4 P		= mul(Input.P	, gMworldViewProj);		// Transform vertex into homogenous clip space
	float4 T		= mul(P			, gMtexProj);			// Transform into projective texture space
	Output.Poutput	= P;
	Output.P		= float4(P.xy, abs(P.w), P.w);			// Chuck on to far plane
	Output.T		= T;									// Projective texture coordinate enables non-dependent read

	return Output;


Pixel Shader

float4x4 gMprojInv; // Inverse Perspective Projection Matrix

float GetDepth(float4 T) {
	return tex2Dproj(gSmpDepth, T).r;

struct PixIn {
	float4 P			: TEXCOORD0;
	float4 T			: TEXCOORD1;

float4 PixFn(PixIn Input) : COLOR0 {

	float	linearUnitZ	= GetDepth(Input.T);
	float4	Ptemp		= mul(Input.P, gMprojInv);
	float3	ViewRay		= / Ptemp.w;
	float3	Pview		= ViewRay * linearUnitZ;



Non-dependent read
means that the texture units can start sampling before the pixel shaders begin executing.
Actually even before the vertex shader has finished executing.
This means by the time the pixel shader executes … the texture cache already contains the data
so the tex2D() takes no time at all …

You can enable this performance boosting parallelism by sampling using an (unaltered) texture coordinate from a vertex shader.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: