Renderer
The core 2D rendering engine for Ceramic, responsible for efficiently drawing all visuals to the screen.
This implementation-independent renderer works with backend draw implementations to:
- Batch draw calls for optimal GPU performance
- Manage render state (textures, shaders, blend modes)
- Handle render-to-texture operations
- Implement stencil-based clipping
- Support multi-texture batching when available
The renderer uses several optimization strategies:
- State batching: Groups visuals with the same rendering state
- Texture atlasing: Batches multiple textures in a single draw call
- Vertex buffering: Minimizes GPU state changes
- Z-ordering: Maintains proper visual layering
Rendering pipeline:
- Sort visuals by depth and rendering state
- Group visuals into batches with matching states
- Submit batches to GPU with minimal state changes
- Handle special cases (stencil clipping, render targets)
// The renderer is typically managed by the App
var renderer = app.renderer;
renderer.render(true, app.visuals);
Instance Members
Renders a list of visuals to the screen or render target.
This is the main entry point for the rendering pipeline. It:
- Initializes rendering state
- Processes each visual in order
- Batches visuals with matching states
- Handles special rendering modes (clipping, render targets)
- Submits draw calls to the GPU
| Name | Type | Description |
|---|---|---|
isMainRender |
Bool | Whether this is the main render pass (vs render-to-texture) |
ceramicVisuals |
Array<Visual> | Array of visuals to render, pre-sorted by depth |
new(): VoidPrivate Members
drawCalls: IntNumber of draw calls made in the current frame. Lower values indicate better batching performance.
For the meshes sent with their own indices (see _drawMesh()): the list 0, 1, 2...
used as the vertices of a mesh sent whole, and for a mesh sent in parts, the
vertices and indices of the current part, the number each mesh vertex has in it
(indexedSlot) and the part it was last numbered for (indexedStamp).
indexedPart: IntactiveShader: backend.ShaderCurrently active GPU shader program.
customFloatAttributesSize: IntNumber of custom float attributes per vertex for the active shader.
stencilClip: BoolWhether we're currently rendering to the stencil buffer for clipping.
lastTexture: TextureLast used texture to detect state changes.
lastTextureId: backend.TextureIdBackend ID of the last used texture.
lastShader: ShaderLast used shader to detect state changes.
lastRenderTarget: RenderTextureLast used render target to detect state changes.
lastComputedBlending: BlendingLast computed blending mode to detect state changes.
lastClip: VisualLast clipping visual to detect state changes.
lastClipIsRegular: BoolWhether the last clip was a regular quad (can use scissor test).
activeTextureSlot: IntCurrently active texture slot for multi-texturing.
backendTextures: backend.TexturesBackend texture management interface.
backendShaders: backend.ShadersBackend shader management interface.
texWidth: IntLogical width of the current texture.
texHeight: IntLogical height of the current texture.
texWidthActual: IntActual GPU width of the current texture (may be power of 2).
texHeightActual: IntActual GPU height of the current texture (may be power of 2).
defaultTexturedShader: backend.ShaderDefault shader for textured rendering.
defaultWhiteTexture: TextureDefault white texture used when no texture is specified.
quad: QuadCurrent quad being processed (for type casting optimization).
mesh: MeshCurrent mesh being processed (for type casting optimization).
stateDirty: BoolWhether the rendering state needs to be updated.
z: FloatCurrent Z depth value for layering visuals. Incremented slightly for each visual to maintain order.
Indexes of textures used in the current batch.
usedTextures: IntNumber of textures currently bound for multi-texturing.
maxUsableTexturesInBatch: IntMaximum number of textures that can be used in a single batch. Determined by GPU capabilities and shader limitations.
activeShaderCanBatchMultipleTextures: BoolWhether the active shader supports multi-texture batching.
usedRenderTarget: RenderTextureCurrently active render target.
zPerRenderTarget: IntFloatMapZ per render target
drawQuad(draw: backend.Draw, quad: Quad): VoidDraws a single quad to the current render target.
Optimized for the most common rendering case. Handles:
- Texture binding and UV mapping
- Color and alpha blending
- Matrix transformations
- Custom shader attributes
- Batching with previous quads when possible
| Name | Type | Description |
|---|---|---|
draw |
backend.Draw | Backend draw interface |
quad |
Quad | The quad visual to render |
drawRenderable(draw: backend.Draw, renderable: Renderable): VoidDraws a Renderable visual: it emits its own draw commands via render().
Handles render target binding (same pattern as Quad/Mesh), re-applies the active scissor clip if any, then brackets the custom rendering with a flush + texture unbind so batched 2D state stays consistent afterwards.
| Name | Type | Description |
|---|---|---|
draw |
backend.Draw | Backend draw interface |
renderable |
Renderable | The renderable visual to render |
drawMesh(draw: backend.Draw, mesh: Mesh): VoidDraws a mesh with arbitrary vertices and triangles.
More flexible than drawQuad but with similar optimizations:
- Vertex buffer management
- Color mapping (per-mesh, per-triangle, or per-vertex)
- Custom vertex attributes
- Large mesh splitting across multiple draw calls
| Name | Type | Description |
|---|---|---|
draw |
backend.Draw | Backend draw interface |
mesh |
Mesh | The mesh visual to render |
drawMeshWithVertices32(draw: backend.Draw, mesh: Mesh): Void| Name | Type |
|---|---|
draw |
backend.Draw |
mesh |
Mesh |
drawMeshWithVertices32AndFloatColors(draw: backend.Draw, mesh: Mesh): Void| Name | Type |
|---|---|
draw |
backend.Draw |
mesh |
Mesh |
drawMeshWithVertices(draw: backend.Draw, mesh: Mesh): Void| Name | Type |
|---|---|
draw |
backend.Draw |
mesh |
Mesh |
drawMeshWithVerticesAndFloatColors(draw: backend.Draw, mesh: Mesh): Void| Name | Type |
|---|---|
draw |
backend.Draw |
mesh |
Mesh |
The list 0, 1, 2... up to count (at least).
| Name | Type |
|---|---|
count |
Int |
| Returns |
|---|
| Array<Int> |
Makes room in the tables of the parts for a mesh of count vertices.
| Name | Type |
|---|---|
count |
Int |
nextIndexedPart(): IntA new part number: no vertex is numbered for it yet.
| Returns |
|---|
| Int |
Increments the Z (depth) value with a non-linear progression to maintain proper rendering order while avoiding Z-fighting issues. The formula applies a quadratic scaling factor to ensure adequate separation between consecutive depth values, particularly important for overlapping transparent objects.
| Name | Type | Description |
|---|---|---|
z |
Float | The current Z depth value |
| Returns | Description |
|---|---|
| Float | The incremented Z value with non-linear spacing |
flush(draw: backend.Draw): BoolFlushes pending draw commands to the GPU.
Called when:
- Render state changes (texture, shader, blend mode)
- Buffer capacity is reached
- Rendering is complete
| Name | Type | Description |
|---|---|---|
draw |
backend.Draw | Backend draw interface |
| Returns | Description |
|---|---|
| Bool | True if anything was flushed |
Computes the actual blending mode for a quad.
Resolves AUTO blending based on render target:
- Regular rendering: PREMULTIPLIED_ALPHA
- Render-to-texture: RENDER_TO_TEXTURE
| Name | Type | Description |
|---|---|---|
quad |
Quad | The quad to compute blending for |
| Returns | Description |
|---|---|
| Blending | The resolved blending mode |
Computes the actual blending mode for a mesh.
Similar to computeQuadBlending but for mesh visuals.
| Name | Type | Description |
|---|---|---|
mesh |
Mesh | The mesh to compute blending for |
| Returns | Description |
|---|---|
| Blending | The resolved blending mode |
| Name | Type |
|---|---|
shaderA |
Shader |
shaderB |
Shader |
| Returns |
|---|
| Bool |
useShader(draw: backend.Draw, shader: backend.Shader): VoidActivates a shader program for subsequent draw calls.
Updates:
- Active shader state
- Multi-texture capability flags
- Custom attribute configuration
| Name | Type | Description |
|---|---|---|
draw |
backend.Draw | Backend draw interface |
shader |
backend.Shader | Backend shader to activate (null for default) |
useBlending(draw: backend.Draw, blending: Blending): VoidConfigures GPU blending mode for transparency and compositing.
Supports various blend modes:
- PREMULTIPLIED_ALPHA: Standard alpha blending
- ADD: Additive blending for light effects
- ALPHA: Non-premultiplied alpha
- RENDER_TO_TEXTURE: Special mode for render targets
| Name | Type | Description |
|---|---|---|
draw |
backend.Draw | Backend draw interface |
blending |
Blending | The blending mode to apply |
scissorWithQuad(draw: backend.Draw, quad: Quad): Void| Name | Type |
|---|---|
draw |
backend.Draw |
quad |
Quad |
| Name | Type |
|---|---|
texture |
Texture |
| Returns |
|---|
| Bool |
useRenderTarget(draw: backend.Draw, renderTarget: RenderTexture): VoidSets the render target for subsequent draw calls.
| Name | Type | Description |
|---|---|---|
draw |
backend.Draw | Backend draw interface |
renderTarget |
RenderTexture | Texture to render to (null for screen) |
useFirstTextureInBatch(draw: backend.Draw, texture: Texture): Void| Name | Type |
|---|---|
draw |
backend.Draw |
texture |
Texture |
useTexture(draw: backend.Draw, texture: Texture, reusing: Bool): Void| Name | Type |
|---|---|
draw |
backend.Draw |
texture |
Texture |
reusing |
Bool |
canUseTextureInSameBatch(draw: backend.Draw, texture: Texture): BoolChecks if a texture can be added to the current batch.
For multi-texture batching, checks if:
- Texture is already bound in a slot
- Free texture slots are available
| Name | Type | Description |
|---|---|---|
draw |
backend.Draw | Backend draw interface |
texture |
Texture | Texture to check |
| Returns | Description |
|---|---|
| Bool | True if batching can continue |
useTextureInSameBatch(draw: backend.Draw, texture: Texture): Void| Name | Type |
|---|---|
draw |
backend.Draw |
texture |
Texture |
unbindUsedTextures(draw: backend.Draw): Void| Name | Type |
|---|---|
draw |
backend.Draw |
Metadata
| Name | Parameters |
|---|---|
:build |
ceramic.macros.EntityMacro.buildForCompletion() |
:autoBuild |
ceramic.macros.EntityMacro.buildForCompletion() |
:build |
tracker.macros.EventsMacro.build() |
:autoBuild |
tracker.macros.EventsMacro.build() |