Color legend method
Reading the G-buffer back in a shader: the position and the normal of the surface at a pixel, and the pixels a pass at a reduced resolution reads.
An HLSL header, copied to your build output at Content/Effects/Lib/: #include "Lib/GBuffer.fxh". It undoes the encoding of a GBuffer’s targets in one place, so every pass that reads them agrees on where a surface is. The G-buffer page shows it in context, with code.
Names are colored by what they are: a method; values and fields keep the plain color.
Functions
GBufferPixelOf() →
int2 GBufferPixelOf(uint2 pixel, int pixelScale) · The G-buffer pixel a pass at 1 / pixelScale of the resolution reads for a pixel of its own: the top-left of the block it covers, pixel * pixelScale. Never an average of the block: across an edge, that is a surface that isn’t there.
| Parameter | Type | Description |
|---|---|---|
pixel |
uint2 |
The pass’s pixel. |
pixelScale |
int |
How many G-buffer pixels a pass pixel covers each way: 2 for half the resolution. |
GBufferViewNormal() →
float3 GBufferViewNormal(float4 packed) · The view-space normal, unpacked from 0..1 and normalized again: eight bits a channel doesn’t keep its length.
| Parameter | Type | Description |
|---|---|---|
packed |
float4 |
The Normal target at the pixel. |
GBufferViewPosition() →
float3 GBufferViewPosition(int2 pixel, float4 gBufferSize, float depth, float4x4 inverseProjection) · The view-space point of the surface at a pixel: on the line from the eye through the pixel’s centre, where its distance in front of the camera is depth. Perspective projections only; an orthographic camera needs a function of your own, see perspective only.
| Parameter | Type | Description |
|---|---|---|
pixel |
int2 |
The G-buffer pixel. |
gBufferSize |
float4 |
The G-buffer’s (width, height, 1 / width, 1 / height). Only the last two are read. |
depth |
float |
The Depth target at the pixel. 0, nothing drawn, gives the eye. |
inverseProjection |
float4x4 |
The inverse of the projection the prepass drew with. |
GBufferWorldNormal() →
float3 GBufferWorldNormal(float4 packed, float4x4 inverseView) · The normal, taken through inverseView and normalized.
| Parameter | Type | Description |
|---|---|---|
packed |
float4 |
The Normal target at the pixel. |
inverseView |
float4x4 |
The inverse of the view the prepass drew with. |
GBufferWorldPosition() →
float3 GBufferWorldPosition(int2 pixel, float4 gBufferSize, float depth, float4x4 inverseProjection, float4x4 inverseView) · GBufferViewPosition’s point, taken through inverseView: in the space the view matrix was made from, which for a traced pass has to be the space the instances were declared in.
| Parameter | Type | Description |
|---|---|---|
pixel |
int2 |
The G-buffer pixel. |
gBufferSize |
float4 |
The G-buffer’s (width, height, 1 / width, 1 / height). |
depth |
float |
The Depth target at the pixel. |
inverseProjection |
float4x4 |
The inverse of the projection the prepass drew with. |
inverseView |
float4x4 |
The inverse of the view it drew with. |
ReducedGridPosition() →
float2 ReducedGridPosition(float2 fullResolution, int pixelScale) · Where a full-resolution position falls on the grid of a pass at 1 / pixelScale of the resolution, in the grid’s texels, with the whole numbers on the pixels the pass computed: (fullResolution - 0.5) / pixelScale. The nearest texel is floor(position + 0.5), and a bilinear weight is the fraction as it stands.
| Parameter | Type | Description |
|---|---|---|
fullResolution |
float2 |
A position in G-buffer pixels, pixel k’s centre at k + 0.5: SV_Position.xy, or a UV times the G-buffer’s size. |
pixelScale |
int |
The pass’s scale, as given to GBufferPixelOf. |