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

Reconstructing position from depth

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

```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
);//float4x4

struct VertIn {
float4 P			: POSITION0;
};//struct

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

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;

}//function

```

```float4x4 gMprojInv; // Inverse Perspective Projection Matrix

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

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

float4 PixFn(PixIn Input) : COLOR0 {

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

...

}//function```