Graphics Reference
In-Depth Information
3.
The following function can then be called whenever we need to access the attributes
of the G-Buffer:
void ExtractGBufferAttributes(in PixelIn pixel,
in Texture2D<float4> t0,
in Texture2D<uint> t1,
in Texture2D<float4> t2,
in Texture2D<float> t3,
out GBufferAttributes attrs)
{
int3 screenPos = int3(pixel.Position.xy, 0);
// Load diffuse RGB
attrs.Diffuse = t0.Load(screenPos).xyz;
// Specular Intensity
attrs.SpecularInt = t0.Load(screenPos).w;
// Unpack and decode the normal
attrs.Normal = UnpackNormal(t1.Load(screenPos));
// Retrieve the emissive light
attrs.Emissive = t2.Load(screenPos).xyz;
// Load the specular power and rescale to 0-50
attrs.SpecularPower = t2.Load(screenPos).w * 50;
// Retrieve non-linear depth
float depth = t3.Load(screenPos);
// Reconstruct the view-space position from viewport
// position and depth
// Convert UV coords to normalized device coordinates
float x = pixel.UV.x * 2 - 1;
float y = (1 - pixel.UV.y) * 2 - 1;
// Unproject -> transform by inverse projection
float4 posVS = mul(float4(x, y, depth, 1.0f),
InverseProjection);
// Perspective divide to get final view-space position
attrs.Position = posVS.xyz / posVS.w;
}
4. Assuming the above structures and functions are now within an HLSL file,
GBuffer.hlsl , we can define a pixel shader to be used with the screen-aligned
quad to output the view-space normals as follows:
#include "Common.hlsl"
#include "GBuffer.hlsl"
// G-Buffer resources
 
Search WWH ::




Custom Search