Gorilla.Controller Unit Reference

Overview

The Gorilla.Controller unit provides the foundation for render pass management in Gorilla3D. It defines the TGorillaRenderPassController class and supporting infrastructure for implementing custom multi-pass rendering techniques, including reflections, refractions, shadows, and post-processing effects.

Architecture

The controller system uses a hierarchical structure:

TGorillaController (Base)
├─ TGorillaComponentController
└─ TGorillaRenderPassController
   ├─ Own Renderer
   ├─ Frame Buffer Object (FBO)
   ├─ Shader Material (optional)
   └─ Control Collections (allowed/ignored)

Core Classes

TGorillaController

Base controller class for all Gorilla3D controllers.

Inheritance: TFmxObject → TGorillaController

Platforms: All Gorilla3D supported platforms

Published Properties

Property Type Default Description
Enabled Boolean True Enable/disable controller.

Public Methods

constructor Create(AOwner: TComponent); override;
destructor Destroy(); override;

Standard component lifecycle management.


TGorillaComponentController

Controller with target component link.

Inheritance: TGorillaController → TGorillaComponentController

Platforms: All Gorilla3D supported platforms

Published Properties

Property Type Description
Target TControl3D Target 3D control for controller operations.

TGorillaRenderPassMaterialSource

Base class for render pass shader materials.

Inheritance: TMaterialSource → TGorillaRenderPassMaterialSource

Platforms: All Gorilla3D supported platforms

Public Properties

Property Type Description
OwnerPass TGorillaRenderPassController Render pass that owns this material source.
InputPass TGorillaRenderPassController Input render pass (for chaining).
HasMaterial Boolean Whether material instance exists.

Public Methods

constructor Create(AOwnerPass: TGorillaRenderPassController); reintroduce; virtual;

Creates material source owned by a render pass controller.

Parameters:

  • AOwnerPass - The render pass controller that owns this material

procedure PassesChanged(Sender: TObject);

Notification that render pass configuration changed.


TGorillaRenderPassMaterial

Internal material class for render pass shaders.

Inheritance: TCustomMaterial → TGorillaRenderPassMaterial

Public Properties

Property Type Description
Source TGorillaRenderPassMaterialSource Parent material source.
ShaderUpdateCounter NativeUInt Shader rebuild counter.

Public Methods

constructor Create(ASource: TGorillaRenderPassMaterialSource); reintroduce; virtual;

Creates material instance from material source.


function GetPreviousRenderTexture(AContext: TContext3D): TTexture; virtual;

Returns the output texture of the previous renderer in the render pipeline.

Parameters:

  • AContext - Rendering context

Returns: Previous pass output texture, or current context texture if none


TGorillaManagedControl

Collection item representing a managed (allowed/ignored) control.

Inheritance: TCollectionItem → TGorillaManagedControl

Published Properties

Property Type Description
DisplayName String Display name in collection editor.
Control TControl3D Reference to managed 3D control.

Public Methods

procedure Notification(AComponent: TComponent; Operation: TOperation);

Handles component notifications for lifetime management.


TGorillaManagedControls

Collection of managed controls for allow/ignore filtering.

Inheritance: TOwnedCollection → TGorillaManagedControls

Public Methods

function GetManagedControl(ACtrl: TControl3D): TGorillaManagedControl;

Finds collection item for a specific control.

Parameters:

  • ACtrl - Control to find

Returns: Collection item if found, nil otherwise


function IndexOf(ACtrl: TControl3D): Integer;

Returns index of control in collection, or -1 if not found.


function Contains(ACtrl: TControl3D): Boolean;

Checks if control exists in collection.


procedure Remove(ACtrl: TControl3D);

Removes control from collection.


procedure Notification(AComponent: TComponent; Operation: TOperation);

Handles component notifications for all collection items.


TGorillaRenderPassController

Primary render pass controller class. Abstract base - must be inherited for specific pass types.

Inheritance: TGorillaController → TGorillaRenderPassController

Platforms: All Gorilla3D supported platforms

Abstract: Cannot instantiate directly - use specialized subclasses like TGorillaRenderPassReflection

Published Properties

Identity and Configuration
Property Type Description
Id String Unique render pass identifier. Duplicate IDs overwrite existing passes.
_Type TRenderPassType Execution timing: Pre, Post, or Intermediate.
Method TRenderPassMethod Render method: RenderListToFBO or RectToFBO.
RenderProc TRenderPassRenderingProc Rendering procedure: DefaultRendering or ListRendering.
Viewport TGorillaViewport Parent viewport for rendering.
Iterations Integer Number of times to execute pass per frame (for ping-pong effects). Default: 1.

Viewport Configuration
Property Type Range Description
ViewportWidth Integer > 0 Custom viewport width (if OverwriteViewportSize).
ViewportHeight Integer > 0 Custom viewport height (if OverwriteViewportSize).
OverwriteViewportSize Boolean - Use custom viewport size instead of main viewport.
ScaleViewportSize Boolean - Apply ViewportScale factor to viewport dimensions.
ViewportScale Single > 0 Scaling factor for viewport dimensions.

Background Color
Property Type Description
OverwriteColor Boolean Use custom background color instead of viewport color.
BackgroundColor TAlphaColor Custom background color (if OverwriteColor is True).

Control Filtering
Property Type Description
AllowedControls TGorillaManagedControls Collection of explicitly allowed controls. Empty = allow all.
IgnoredControls TGorillaManagedControls Collection of controls to exclude from rendering.

Events
Property Type Description
OnBeginSetup TRenderPassEvent Called once per frame before pass setup.
OnEndSetup TRenderPassEvent Called once per frame after pass setup.
OnBeginRender TRenderPassEvent Called before each iteration renders.
OnEndRender TRenderPassEvent Called after each iteration completes.

Public Properties

Property Type Description
Shader TGorillaRenderPassMaterialSource Shader material source for RectToFBO rendering.
RendererType TRendererType Renderer type: ForwardRenderer or LegacyRenderer.
Renderer TRenderer Internal renderer instance used by this pass.
PreviousRenderer TRenderer Renderer that was active before this pass.
RendererFlags TRendererFlags Renderer creation flags.
FBO TFrameBufferObject Frame buffer object for render-to-texture.
FBOTexture TGorillaTextureBitmap Output texture bitmap.
OutputTexture TTexture Final output texture (read-only).
ViewportSize TPoint Actual rendering dimensions.
ProjMatrix TMatrix3D Computed projection matrix (read-only).
ViewMatrix TMatrix3D Computed view matrix (read-only).
PrevViewMatrix TMatrix3D View matrix before pass execution (read-only).
PrevProjMatrix TMatrix3D Projection matrix before pass execution (read-only).
RenderingSkipped Boolean Whether rendering was skipped this frame (read-only).

Public Methods

Construction and Lifecycle
constructor Create(AOwner: TComponent; const AId: String); reintroduce; virtual;

Creates render pass controller with unique identifier.

Parameters:

  • AOwner - Component owner
  • AId - Unique pass identifier

Example:

var Pass := TMyRenderPass.Create(Self, 'CustomPass');

destructor Destroy(); override;

Destroys render pass and releases GPU resources.


procedure AfterConstruction(); override;

Called after construction completes. Override for initialization.


Renderer Management
procedure Refresh();

Recreates renderer (useful after major configuration changes).


procedure BindRenderer(); virtual;

Activates this pass's renderer and FBO.


procedure UnbindRenderer(); virtual;

Deactivates this pass's renderer and FBO.


procedure BeginRenderPassRendering(AContext: TContext3D; AFill: TAlphaColor;
  ADepth: Single = 1; AStencil: Cardinal = 0); virtual;

Begins render pass rendering cycle.

Parameters:

  • AContext - Rendering context
  • AFill - Clear color
  • ADepth - Depth clear value (default: 1.0)
  • AStencil - Stencil clear value (default: 0)

procedure EndRenderPassRendering(AContext: TContext3D); virtual;

Ends render pass rendering cycle.


Control Management
procedure AllowControl(ACtrl: TControl3D); virtual;

Adds control to allowed list. Automatically includes sub-meshes and child components.

Parameters:

  • ACtrl - Control to allow

Example:

ReflectionPass.AllowControl(Skybox);
ReflectionPass.AllowControl(Mountains);

procedure IgnoreControl(ACtrl: TControl3D); virtual;

Adds control to ignored list. Automatically includes sub-meshes and child components.

Parameters:

  • ACtrl - Control to ignore

Example:

ReflectionPass.IgnoreControl(WaterPlane);

function IsControlIgnored(ACtrl: TControl3D): Boolean; virtual;

Checks if control is ignored by this pass.

Parameters:

  • ACtrl - Control to check

Returns: True if control will not be rendered

Logic:

  • If AllowedControls is not empty and control is not in list: Ignored
  • If control is in IgnoredControls: Ignored
  • Otherwise: Not ignored

Example:

if not ReflectionPass.IsControlIgnored(TreeModel) then
  ShowMessage('Tree will be rendered in reflection');

Pass Management
procedure UpdateOnResize(); virtual;

Called when viewport resizes. Updates internal viewport size and recreates textures.


procedure UpdateRenderPass(); virtual;

Updates render pass registration in context. Called automatically when configuration changes.


function CreateRenderPassInstance(const AContext: TContext3D): TRenderPass; virtual;

Creates temporary render pass instance (for internal rendering).

Parameters:

  • AContext - Rendering context

Returns: New TRenderPass instance


class function IsInternalRenderPass(): Boolean; virtual;

Returns True if pass is internal (not registered with context).

Returns: False by default. Override to return True for internal passes.


function IsPrimitivesKindSupported(AKind: TPrimitivesKind): Boolean; virtual;

Checks if primitive type is supported by this pass.

Parameters:

  • AKind - Primitive type

Returns: True by default. Override to restrict primitive types.


Pass Chaining
procedure SetInputPass(const APass: TGorillaRenderPassController); virtual;

Sets input pass for chaining passes together.

Parameters:

  • APass - Pass whose output becomes this pass's input

Example:

BlurVertical.SetInputPass(BlurHorizontal);

function GetInputPass(): TGorillaRenderPassController; virtual;

Returns currently set input pass.

Returns: Input pass controller, or nil if none


Output and Textures
function GetOutputTexture(): TTexture; virtual;

Returns final output texture of this render pass.

Returns: FBO output texture

Note: Override for custom texture handling.


function GetTextureMatrix(AContext: TContext3D;
  const AUseBias: Boolean = false): TMatrix3D; virtual;

Returns texture matrix for this pass.

Parameters:

  • AContext - Rendering context
  • AUseBias - Apply bias matrix (for shadow mapping)

Returns: Texture coordinate transformation matrix

Note: Override for custom matrix computation (e.g., shadow map projection).


Matrix Management
function GetCurrentModelViewProjectionMatrix(): TMatrix3D; virtual;

Returns combined model-view-projection matrix.

Returns: MVP matrix for current pass


function GetViewMatrix(): TMatrix3D; overload; virtual;
function GetViewMatrix(const AContext: TContext3D): TMatrix3D; overload; virtual;

Returns view (camera) matrix for this pass.

Returns: View matrix (from camera or identity)

Note: Override for custom view matrices (e.g., mirrored for reflections).


function GetProjectionMatrix(): TMatrix3D; overload; virtual;
function GetProjectionMatrix(const AContext: TContext3D): TMatrix3D; overload; virtual;

Returns projection matrix for this pass.

Returns: Projection matrix (computed from viewport size and camera FOV)

Note: Override for custom projections (e.g., orthographic for shadow maps).


Rendering Control
procedure RenderToContext(const AContext: TContext3D; const ATexture: TTexture;
  const ARect: TRect; const ADstPos: TPoint);

Renders pass output to context using a fullscreen quad.

Parameters:

  • AContext - Rendering context
  • ATexture - Input texture
  • ARect - Destination rectangle
  • ADstPos - Destination position

Use Case: Compositing passes, post-processing


function ComputeRenderingSize(const AContext: TContext3D;
  const AWidth: Integer = 0; const AHeight: Integer = 0): TPoint;

Computes actual rendering size based on pass configuration.

Parameters:

  • AContext - Rendering context
  • AWidth - Optional width override
  • AHeight - Optional height override

Returns: Final rendering dimensions (considering OverwriteViewportSize and ScaleViewportSize)


procedure SetViewportSizeAndScaleWithoutResize(AViewport: TComponent;
  ASize: TPoint; AScale: Single);

Sets viewport size and scale without triggering renderer resize.

Parameters:

  • AViewport - Viewport component
  • ASize - New size
  • AScale - New scale

Use Case: Batch configuration updates


Frame Control
procedure IncFrameCount();

Increments frame counter. Called automatically by render pass instance.


procedure SkipRendering(); virtual;

Skips rendering for current frame.

Example:

// Only update reflection every 3rd frame
if (FrameCount mod 3) <> 0 then
  ReflectionPass.SkipRendering();

procedure ResetSkippedRendering();

Resets skip flag (called automatically after skipped frame).


Enumerations

TRenderPassType

Defines when the render pass executes relative to the main rendering loop.

TRenderPassType = (
  Pre,          // Execute before main rendering loop
  Post,         // Execute after main rendering loop
  Intermediate  // Execute during main rendering loop
);

Usage:

ReflectionPass._Type := TRenderPassType.Pre;   // Before main scene
BlurPass._Type := TRenderPassType.Post;        // After main scene

TRenderPassMethod

Defines how the render pass produces output.

TRenderPassMethod = (
  RenderListToFBO,  // Renders all elements in the renderlist with the render pass material or the original material
  RectToFBO         // Renders with a render pass material a specific shader effect onto a rect
);

Usage:

ReflectionPass.Method := TRenderPassMethod.RenderListToFBO;  // Scene rendering
BlurPass.Method := TRenderPassMethod.RectToFBO;              // Post-process
Render Pass Method Description
RenderListToFBO Will render all objects in renderlist with the provided render pass material. The output will be written to the FBO of your renderpass. Use this method in case you need to re-render all elements with their original material or all elements with the same render pass material
RectToFBO Will render on basis of the latest context FBO or on a attached renderpass output. The output will be written to the FBO of your renderpass. Use this method if you want to produce a pre/post effect.

TRenderPassRenderingProc

Defines rendering procedure for scene objects.

TRenderPassRenderingProc = (
  DefaultRendering,  // WBOIT with separate opaque/translucent passes
  ListRendering      // Forward rendering without transparency separation
);

Usage:

ReflectionPass.RenderProc := TRenderPassRenderingProc.DefaultRendering;  // Accurate transparency
DepthPass.RenderProc := TRenderPassRenderingProc.ListRendering;          // Fast depth-only

Type Definitions

TRenderPassEvent

Event callback signature for render pass events.

TRenderPassEvent = procedure(ASender: TObject; const AContext: TContext3D;
  const ACount: Integer; const APass: TRenderPass) of object;

Parameters:

  • ASender - Render pass controller
  • AContext - Rendering context
  • ACount - Current iteration index (0-based)
  • APass - Render pass instance

Example:

procedure TForm1.OnPassBegin(ASender: TObject; const AContext: TContext3D;
  const ACount: Integer; const APass: TRenderPass);
begin
  // Custom setup logic
end;

ReflectionPass.OnBeginRender := OnPassBegin;

TGorillaRenderPassControllerClass

Class reference type for render pass controllers.

TGorillaRenderPassControllerClass = class of TGorillaRenderPassController;

Usage Examples

Basic Render Pass Setup

var
  DepthPass: TGorillaRenderPassCustomDepth;
begin
  DepthPass := TGorillaRenderPassCustomDepth.Create(Self, 'DepthPass');
  DepthPass.Viewport := GorillaViewport1;
  DepthPass._Type := TRenderPassType.Pre;
  DepthPass.Method := TRenderPassMethod.RenderListToFBO;
  DepthPass.RenderProc := TRenderPassRenderingProc.ListRendering;

  // Custom shadow map resolution
  DepthPass.OverwriteViewportSize := True;
  DepthPass.ViewportWidth := 2048;
  DepthPass.ViewportHeight := 2048;
end;

Control Filtering

// Exclude water from reflection
ReflectionPass.IgnoreControl(WaterPlane);
ReflectionPass.IgnoreControl(UnderwaterRocks);

// Allow only sky and terrain
BackfacePass.AllowControl(SkyDome);
BackfacePass.AllowControl(Terrain);

Custom Viewport Scaling

// Render at half resolution for performance
BlurPass.ScaleViewportSize := True;
BlurPass.ViewportScale := 0.5;  // 50% of viewport dimensions

// Or fixed size
BlurPass.OverwriteViewportSize := True;
BlurPass.ViewportWidth := 512;
BlurPass.ViewportHeight := 512;

Pass Chaining

// Blur pipeline: Horizontal → Vertical → Output
BlurHorizontal.SetInputPass(SourcePass);
BlurVertical.SetInputPass(BlurHorizontal);
OutputPass.SetInputPass(BlurVertical);

Event Handling

Event Type Description
OnBeginSetup TRenderPassEvent Callback event when setting up render pass on each cycle was started.
OnEndSetup TRenderPassEvent Callback event when setting up render pass on each cycle was done.
OnBeginRender TRenderPassEvent Callback event when rendering is about to begin for each iteration.
OnEndRender TRenderPassEvent Callback event when rendering is finished for each iteration.

Creating Custom Render Passes

To create a custom render pass, inherit from TGorillaRenderPassController and override required methods:

type
  TMyCustomRenderPass = class(TGorillaRenderPassController)
  protected
    // Create material source for RectToFBO method
    function CreateMaterialSource(): TGorillaRenderPassMaterialSource; override;

    // Optionally override matrix computation
    function GetViewMatrix(const AContext: TContext3D): TMatrix3D; override;
    function GetProjectionMatrix(const AContext: TContext3D): TMatrix3D; override;

    // Custom configuration
    procedure DoConfigure(); override;

  public
    constructor Create(AOwner: TComponent; const AId: String); override;
  end;

implementation

constructor TMyCustomRenderPass.Create(AOwner: TComponent; const AId: String);
begin
  inherited;
  // Custom initialization
end;

procedure TMyCustomRenderPass.DoConfigure();
begin
  inherited;
  FType := TRenderPassType.Pre;
  FMethod := TRenderPassMethod.RenderListToFBO;
  FRenderProc := TRenderPassRenderingProc.DefaultRendering;
end;

function TMyCustomRenderPass.CreateMaterialSource(): TGorillaRenderPassMaterialSource;
begin
  Result := TMyCustomMaterialSource.Create(Self);
end;

function TMyCustomRenderPass.GetViewMatrix(const AContext: TContext3D): TMatrix3D;
begin
  // Custom view matrix computation
  Result := ComputeCustomViewMatrix();
end;

Important Notes

  1. Unique IDs Required: Each render pass must have a unique Id. Duplicate IDs overwrite existing passes.

  2. Viewport Assignment: Pass must have Viewport assigned before use. Renderer is created automatically on assignment.

  3. Control Filtering Order: If AllowedControls is not empty, it acts as a whitelist. Only controls in AllowedControls render, then IgnoredControls is checked.

  4. Automatic Children: Calling IgnoreControl() or AllowControl() automatically includes sub-meshes and child components.

  5. Matrix Caching: View and projection matrices are cached and recalculated only when needed. Set FRecalcViewMatrix/FRecalcProjMatrix when custom changes require recomputation.

  6. Abstract Class: TGorillaRenderPassController is abstract. Use specialized subclasses like TGorillaRenderPassReflection or create your own.

  7. Renderer Ownership: Pass owns its renderer (FOwnsRenderer). Don't manually free renderer.

  8. FBO Auto-Creation: FBO and textures are created automatically based on ViewportSize.

  9. Event Timing: OnBeginSetup/OnEndSetup called once per frame. OnBeginRender/OnEndRender called per iteration.

  10. Skip Persistence: SkipRendering() affects only the current frame. Call each frame to continuously skip.

See Also