UUSL GBuffer Structure
G-buffer is a series of textures where all the visible information is written: albedo color, normals, lightmap, etc. G-buffer is used for deferred rending pipeline.
UUSL has a handy GBuffer structure to fill it in the shader's code.
GBuffer Textures
GBuffer has the following set of textures:
Texture name | Type | Description |
---|---|---|
Albedo | RGBA8 | RGB - albedo value.
A - occlusion value. |
Shading | RGBA8 | R - metalness value.
G - F0 (specular) value. B - translucent value. A - microfiber value. |
Normal | RGBA8 | RGB - normal value.
A - roughness value. |
Velocity | RG16F | RG - pixel displacement value (XY coordinates). |
Material Mask | R32U | R - material mask value. |
Lightmap | RG11B10F | RGB - lightmap value. |
Channels of these textures fill corresponding fields of the GBuffer structure.
struct GBuffer {
float3 albedo;
float occlusion;
float metalness;
float f0;
float translucent;
float microfiber;
float3 normal;
float roughness;
float2 velocity;
float material_mask;
float3 lightmap;
};
Specular G-buffer struct has different fields:
struct GBufferSpecular {
float3 diffuse;
float occlusion;
float3 specular;
float translucent;
float microfiber;
float3 normal;
float gloss;
float2 velocity;
float material_mask;
float3 lightmap;
};
To convert Specular G-buffer to Metalness G-buffer, use specularToMetalness()function.
GBuffer Functions
GBuffer GBufferDefault()
Default constructor for GBuffer structure for metalness workflow. It creates an instance with default (not null) values.Return value
GBuffer structure with default (not null) values.GBufferSpecular GBufferSpecularDefault()
Constructor for GBuffferSpecular structure for specular workflow. It creates an instance with default (not null) values.Return value
GBufferSpecular structure with default (not null) values.GBuffer specularToMetalness(GBufferSpecular gbuffer)
Converts given specular GBufferSpecular to metalness Gbuffer.Arguments
- GBufferSpecular gbuffer - Specular GBufferSpecular instance to be converted.
Return value
Metalness Gbuffer instance.void setGBuffer(GBuffer gbuffer)
Sets GBuffer values by using corresponding values of given GBuffer instance.Arguments
- GBuffer gbuffer - GBuffer instance with necessary values to be set.
void setGBuffer(GBufferSpecular gbuffer)
Sets GBuffer values by using corresponding values of given GBufferSpecular instance.Inside this method specularToMetalness()is called.
Arguments
- GBufferSpecular gbuffer - GBufferSpecular instance with necessary values to be set.
void loadGBufferAlbedo(inout GBuffer gbuffer, TEXTURE_IN TEX_ALBEDO, float2 uv)
Loads albedo texture values (albedo color and occlusion) to GBuffer.Arguments
- inout GBuffer gbuffer - Gbuffer structure. Values will be written in it.
- TEXTURE_IN TEX_ALBEDO - Albedo texture.
- float2 uv - UV coordinates.
void loadGBufferShading(inout GBuffer gbuffer, TEXTURE_IN TEX_SHADING, float2 uv)
Loads shading texture values (metalness, f0, translucence, microfiber) to GBuffer.Arguments
- inout GBuffer gbuffer - Gbuffer structure. Values will be written in it.
- TEXTURE_IN TEX_SHADING - Shading texture.
- float2 uv - UV coordinates.
void loadGBufferNormal(inout GBuffer gbuffer, TEXTURE_IN TEX_NORMAL, float2 uv)
Loads normal texture values (normal and roughness) to GBuffer.Arguments
- inout GBuffer gbuffer - Gbuffer structure. Values will be written in it.
- TEXTURE_IN TEX_NORMAL - Normal texture.
- float2 uv - UV coordinates.
void loadGBufferLightMap(inout GBuffer gbuffer, TEXTURE_IN TEX_LIGHTMAP, float2 uv)
Loads lightmap texture values to passed GBuffer instance.Arguments
- inout GBuffer gbuffer - Gbuffer structure. Values will be written in it.
- TEXTURE_IN TEX_LIGHTMAP - Lightmap texture.
- float2 uv - UV coordinates.
Usage Example
Defining the GBuffer Structure
To use GBuffer structure in your shader's code, you should define and initialize the GBuffer structure by using its constructors. For metalness worlkflow use GBufferDefault() constructor (for specular workflow you should use GBufferSpecular structure and GBufferSpecularDefault() constructor respectively).
The following example shows the way of creating GBuffer structure for both workflows by using #ifdef #else #endif preprocessor statements.
/* ... */
#ifdef METALNESS
GBuffer gbuffer = GBufferDefault();
#define GBUFFER gbuffer
#else
GBufferSpecular gbuffer_s = GBufferSpecularDefault();
#define GBUFFER gbuffer_s
#endif
/* ... */
Filling the GBuffer Structure
After defining and initializing the GBuffer structure (like in an example above), you can easily fill the GBuffer's fields. You can fill the specific channel of the GBuffer texture.
/* ... */
#ifdef METALNESS
GBUFFER.albedo = color.rgb;
GBUFFER.metalness = shading.r;
GBUFFER.roughness = shading.g;
#else
GBUFFER.diffuse = color.rgb;
GBUFFER.specular = shading.rgb;
GBUFFER.gloss = shading.a;
GBUFFER.microfiber = m_microfiber;
#endif
/* ... */
Loading Textures to GBuffer Structure
Another way of filling the GBuffer structure is using GBuffer functions for textures loading.
In case of changing GBuffer structure fields, these loading functions will perform the same function and allows to prevent possible errors during migration to another Unigine Engine version.
// initialize textures
/* ... */
INIT_TEXTURE(1,TEX_NORMAL)
INIT_TEXTURE(2,TEX_ALBEDO)
INIT_TEXTURE(3,TEX_SHADING)
/* ... */
/* ... */
GBuffer gbuffer = GBufferDefault();
loadGBufferAlbedo(gbuffer, TEXTURE_OUT(TEX_ALBEDO),uv);
loadGBufferShading(gbuffer, TEXTURE_OUT(TEX_SHADING),uv);
loadGBufferNormal(gbuffer, TEXTURE_OUT(TEX_NORMAL),uv);
/* ... */