Gorilla.Controller.Passes.PostFX
Overview
This unit implements a customizable post-processing render pass for the Gorilla3D rendering pipeline. It allows users to supply their own GLSL fragment shader code to create custom visual effects, with access to the deferred rendering G-buffer textures and time information.
Architecture
The PostFX pass is a post-pass using RectToFBO rendering. It compiles a user-provided GLSL surface shader function and executes it as a full-screen post-processing effect. A Uniform Buffer Object (UBO) provides time data for animated effects.
Core Classes
TGorillaRenderPassPostFX
Inherits from: TGorillaRenderPassController
Published Properties
| Property | Type | Default | Description |
|---|---|---|---|
SurfaceShader |
TStrings |
nil | The custom GLSL shader code. Must contain a function with the signature vec3 SurfaceShader(inout TLocals DATA). |
MaskTexture |
TBitmap |
nil | An optional mask texture accessible in the shader as _MaskTexture. |
Public Methods
| Method | Signature | Description |
|---|---|---|
Create |
constructor Create(AOwner: TComponent) |
Creates the PostFX pass. |
Destroy |
destructor Destroy() |
Cleans up resources. |
ResetTime |
Procedure | Resets the internal time counter to zero. Useful for restarting time-based animations. |
UpdateShader |
Procedure | Recompiles the shader program after modifying SurfaceShader. Must be called after changing the shader text at runtime. |
Shader Interface
Surface Shader Signature
Your custom GLSL code must implement the following function:
vec3 SurfaceShader(inout TLocals DATA)
The DATA structure provides access to the current fragment's color and other per-pixel information. The function returns the final RGB color for the fragment.
Available Texture Uniforms
The following sampler uniforms are available in the shader:
| Uniform | Type | Description |
|---|---|---|
_RenderedTexture |
sampler2D |
The main rendered scene texture (color buffer). |
_MaskTexture |
sampler2D |
The user-supplied mask texture (from the MaskTexture property). |
_AlphaTexture |
sampler2D |
Alpha channel from the G-buffer. |
_PositionTexture |
sampler2D |
World-space position from the G-buffer. |
_NormalsTexture |
sampler2D |
World-space normals from the G-buffer. |
_EmissiveTexture |
sampler2D |
Emissive channel from the G-buffer. |
UBO Time Data
A vec4 uniform _TimeInfo is provided via a Uniform Buffer Object:
| Component | Description |
|---|---|
_TimeInfo.x |
Start time (timestamp when the pass was created or last reset) |
_TimeInfo.y |
Current time |
_TimeInfo.z |
Previous frame time |
_TimeInfo.w |
Delta time (time elapsed since previous frame) |
Runtime Setup
Basic Grayscale Example
uses
Gorilla.Controller.Passes.PostFX;
var
LPostFX: TGorillaRenderPassPostFX;
begin
LPostFX := TGorillaRenderPassPostFX.Create(nil);
LPostFX.Viewport := GorillaViewport1;
LPostFX.SurfaceShader.Text :=
'vec3 SurfaceShader(inout TLocals DATA) {' + sLineBreak +
' vec3 col = DATA.Color.rgb;' + sLineBreak +
' float gray = dot(col, vec3(0.299, 0.587, 0.114));' + sLineBreak +
' return vec3(gray);' + sLineBreak +
'}';
LPostFX.UpdateShader;
LPostFX.Enabled := True;
end;
Animated Wave Distortion Example
LPostFX.SurfaceShader.Text :=
'vec3 SurfaceShader(inout TLocals DATA) {' + sLineBreak +
' float t = _TimeInfo.y - _TimeInfo.x;' + sLineBreak +
' vec2 uv = DATA.TexCoord;' + sLineBreak +
' uv.x += sin(uv.y * 10.0 + t) * 0.01;' + sLineBreak +
' return texture(_RenderedTexture, uv).rgb;' + sLineBreak +
'}';
LPostFX.UpdateShader;
Updating Shader at Runtime
When modifying the SurfaceShader text at runtime, always call UpdateShader afterward to recompile:
LPostFX.SurfaceShader.Clear;
LPostFX.SurfaceShader.Add('vec3 SurfaceShader(inout TLocals DATA) {');
LPostFX.SurfaceShader.Add(' return vec3(1.0, 0.0, 0.0);');
LPostFX.SurfaceShader.Add('}');
LPostFX.UpdateShader;
Technical Details
- Pass type: Post-pass (
TRenderPassType.Post) - Render method: RectToFBO (full-screen quad)
- The shader is compiled at runtime when
UpdateShaderis called. Shader compilation errors will be reported through the standard Gorilla3D error handling. - The
ResetTimemethod sets_TimeInfo.xto the current timestamp, effectively restarting time-based animations from zero. - G-buffer textures (
_PositionTexture,_NormalsTexture, etc.) are only available when the deferred rendering pipeline is active.