Draw

backend.Draw (Class)

Instance Members

clay
draw(visuals: Array<ceramic.Visual>): Void

Renders an array of visual objects.

On iOS, this method checks if the app is in background to prevent GPU operations that could cause crashes when the app is not active.

Name Type Description
visuals Array<ceramic.Visual> Array of Visual objects to render

clay
swap(): Void

Swaps the front and back buffers (unused in Clay backend). Buffer swapping is handled automatically by Clay framework.


clay
initBuffers(): Void

Initializes the vertex and index buffers for rendering.

This method sets up the buffer management system and prepares the first set of buffers for use. Should be called before any rendering operations begin.


clay
beginRender(): Void

Begins a rendering pass by enabling vertex attributes.

Enables the core vertex attributes used by all shaders:

  • Position (x, y, z)
  • Texture coordinates (u, v)
  • Color (r, g, b, a)

Additional attributes are enabled dynamically based on the active shader.


clay
clear(): Void

Clears the current render target to transparent white.

This method clears the color buffer with a transparent white background. When rendering to a texture, this marks the render target as updated.


clay
clearAndApplyBackground(): Void

Clears the current render target and applies the application background color.

This method clears the color buffer with the application's configured background color (fully opaque). Used for the main screen clearing.


clay
enableBlending(): Void

Enables alpha blending for transparent rendering.

This activates the GPU's blending functionality, allowing pixels to be combined based on their alpha values. Must be enabled for transparency to work correctly.


clay
disableBlending(): Void

Disables alpha blending.

When disabled, pixels are rendered opaque regardless of alpha values. This can improve performance when rendering fully opaque content.


clay
setActiveTexture(slot: Int): Void

Sets the active texture unit slot for multi-texturing.

Modern GPUs support multiple texture units, allowing shaders to sample from multiple textures simultaneously. This method selects which texture unit subsequent texture operations will affect.

Name Type Description
slot Int The texture unit index (0-based)

clay
setPrimitiveType(primitiveType: ceramic.RenderPrimitiveType): Void

Sets the primitive type for rendering.

Determines how vertices are interpreted:

  • TRIANGLE: Every 3 vertices form a triangle (default)
  • LINE: Every 2 vertices form a line
Name Type Description
primitiveType ceramic.RenderPrimitiveType The primitive type to use for subsequent draw calls

clay
getActiveTexture(): Int

Gets the currently active texture unit slot.

Returns Description
Int The index of the active texture unit

clay
setRenderTarget(renderTarget: ceramic.RenderTexture, ?force: Bool = false): Void

Sets the active render target for subsequent drawing operations.

This method switches between rendering to a texture (off-screen) or to the main screen buffer. When switching targets, it handles:

  • MSAA buffer blitting for antialiased render targets
  • Projection and view matrix updates
  • Viewport configuration
  • Optional automatic clearing

When renderTarget is null, rendering switches back to the main screen.

Name Type Default Description
renderTarget ceramic.RenderTexture The render texture to render to, or null for main screen
force Bool false Force the render target switch even if it's the same target

clay
useShader(shader: Shader): Void

Activates a shader program for rendering.

This method:

  • Sets the shader as the active GPU program
  • Uploads projection and modelview matrices as uniforms
  • Configures vertex layout based on shader attributes
  • Determines if multi-texture batching is supported
  • Calculates maximum vertices per batch based on attribute size

The vertex size calculation includes:

  • 3 floats for position (x, y, z)
  • Custom float attributes defined by the shader
  • 1 float for texture slot (if multi-texturing is enabled)
Name Type Description
shader Shader The shader to activate

clay
setBlendFuncSeparate(srcRgb: BlendMode, dstRgb: BlendMode, srcAlpha: BlendMode, dstAlpha: BlendMode): Void

Sets separate blend functions for RGB and alpha channels.

This allows fine control over how colors are blended, with different blend modes for color (RGB) and transparency (alpha) channels.

Common blend modes:

  • ONE: Use source value as-is
  • ZERO: Ignore value (multiply by 0)
  • SRC_ALPHA: Multiply by source alpha
  • ONE_MINUS_SRC_ALPHA: Multiply by (1 - source alpha)
Name Type Description
srcRgb BlendMode Blend factor for source RGB
dstRgb BlendMode Blend factor for destination RGB
srcAlpha BlendMode Blend factor for source alpha
dstAlpha BlendMode Blend factor for destination alpha

clay
beginDrawQuad(quad: ceramic.Quad): Void

Called before drawing a quad (currently unused).

Reserved for future optimizations or quad-specific setup.

Name Type Description
quad ceramic.Quad The quad about to be drawn

clay
endDrawQuad(): Void

Called after drawing a quad (currently unused).

Reserved for future optimizations or quad-specific cleanup.


clay
beginDrawMesh(mesh: ceramic.Mesh): Void
Name Type
mesh ceramic.Mesh

clay
endDrawMesh(): Void

clay
enableScissor(x: Float, y: Float, width: Float, height: Float): Void

Enables scissor testing to clip rendering to a rectangular area.

Scissor testing restricts rendering to pixels within the specified rectangle. Any pixels outside this area are discarded by the GPU.

The coordinates are transformed by the current modelview matrix and adjusted for screen density. When rendering to a texture, the Y coordinate is flipped to account for texture coordinate differences.

Name Type Description
x Float Left edge of the scissor rectangle in logical coordinates
y Float Top edge of the scissor rectangle in logical coordinates
width Float Width of the scissor rectangle in logical pixels
height Float Height of the scissor rectangle in logical pixels

clay
disableScissor(): Void

Disables scissor testing.

After calling this, rendering is no longer clipped to a rectangular area.


clay
drawWithStencilTest(): Void

Enables stencil testing for masked rendering.

This configures the stencil test to only render pixels where the stencil buffer equals 1. Used after drawing to the stencil buffer to render content only within the stenciled area.

Stencil configuration:

  • Test: Passes when stencil buffer equals 1
  • Mask: Stencil buffer is read-only (0x00)
  • Color: All color channels are written

clay
drawWithoutStencilTest(): Void

Disables stencil testing for normal rendering.

Resets stencil configuration to default values where all pixels pass the stencil test and the stencil buffer can be written to.

Stencil configuration:

  • Test: Always passes
  • Mask: Stencil buffer is writable (0xFF)
  • Color: All color channels are written

clay
beginDrawingInStencilBuffer(): Void

Begins drawing to the stencil buffer for masking operations.

This sets up the GPU to write to the stencil buffer instead of the color buffer. Pixels drawn will mark areas in the stencil buffer with a value of 1, creating a mask for subsequent rendering.

Stencil configuration:

  • Clears stencil buffer to 0xFF
  • Writes 1 to stencil buffer where pixels are drawn
  • Disables color output (only stencil is affected)
  • Always passes stencil test during mask creation

clay
endDrawingInStencilBuffer(): Void

clay
bindTexture(backendItem: Texture): Void

Binds a texture to the current texture unit.

Makes the texture available for sampling in shaders. The texture will be bound to the currently active texture unit.

Name Type Description
backendItem Texture The texture to bind

clay
bindNoTexture(): Void

Binds no texture or a default white texture.

On web targets, binds a 1x1 white texture because WebGL requires a texture to be bound. On native targets, unbinds any texture.

Used when rendering untextured geometry or solid colors.


clay
textureBackendItemMatchesId(backendItem: Texture, textureId: TextureId): Bool
Name Type
backendItem Texture
textureId TextureId
Returns
Bool

clay
getTextureId(backendItem: Texture): TextureId
Name Type
backendItem Texture
Returns
TextureId

clay
getTextureWidth(backendItem: Texture): Int
Name Type
backendItem Texture
Returns
Int

clay
getTextureHeight(backendItem: Texture): Int
Name Type
backendItem Texture
Returns
Int

clay
getTextureWidthActual(backendItem: Texture): Int
Name Type
backendItem Texture
Returns
Int

clay
getTextureHeightActual(backendItem: Texture): Int
Name Type
backendItem Texture
Returns
Int

clay
getNumPos(): Int
Returns
Int

clay
putPos(x: Float, y: Float, z: Float): Void

Adds a vertex position to the current batch.

On C++ targets, uses direct memory access for performance. On other targets, uses array access.

Name Type Description
x Float X coordinate in screen space
y Float Y coordinate in screen space
z Float Z coordinate for depth ordering (0-1 range typically)

clay
putPosAndTextureSlot(x: Float, y: Float, z: Float, textureSlot: Float): Void
Name Type
x Float
y Float
z Float
textureSlot Float

clay
beginFloatAttributes(): Void

clay
putFloatAttribute(index: Int, value: Float): Void
Name Type
index Int
value Float

clay
endFloatAttributes(): Void

clay
putIndice(i: Int): Void
Name Type
i Int

clay
putUVs(uvX: Float, uvY: Float): Void
Name Type
uvX Float
uvY Float

clay
putColor(r: Float, g: Float, b: Float, a: Float): Void

Adds a vertex color to the current batch.

Colors are stored as floating-point values from 0.0 to 1.0. The color will be interpolated across the triangle/line.

Name Type Description
r Float Red component (0.0 to 1.0)
g Float Green component (0.0 to 1.0)
b Float Blue component (0.0 to 1.0)
a Float Alpha component (0.0 to 1.0)

clay
hasAnythingToFlush(): Bool
Returns
Bool

clay
shouldFlush(numVerticesAfter: Int, numIndicesAfter: Int, customFloatAttributesSize: Int): Bool

Checks if the current batch should be flushed before adding more vertices.

Returns true if adding the specified number of vertices or indices would exceed buffer capacity, indicating that the current batch should be sent to the GPU before continuing.

Name Type Description
numVerticesAfter Int Number of vertices to be added
numIndicesAfter Int Number of indices to be added
customFloatAttributesSize Int Size of custom attributes (unused)
Returns Description
Bool True if flush is needed, false otherwise

clay
remainingVertices(): Int
Returns
Int

clay
remainingIndices(): Int
Returns
Int

clay
flush(): Void

Flushes the current batch of vertices to the GPU.

This is the core rendering method that:

  1. Creates GPU buffers from the accumulated vertex data
  2. Configures vertex attributes for the shader
  3. Handles multi-texture batching if supported
  4. Sets up custom shader attributes
  5. Issues the draw call to render all triangles/lines
  6. Cleans up temporary buffers
  7. Prepares for the next batch

The method uses temporary GPU buffers that are deleted after rendering to avoid memory leaks. Buffer data is uploaded as STREAM_DRAW for optimal performance with dynamic geometry.


clay
new(): Void

Creates a new Draw backend instance. Initializes the Ceramic renderer for handling visual rendering.

Private Members

clay
MAX_VERTS_SIZE: Int

Maximum number of vertices that can be stored in a single buffer.


clay
MAX_INDICES: Int

Maximum number of indices that can be stored in a single buffer.


clay
MAX_BUFFERS: Int

Maximum number of buffer sets to cycle through. Buffer cycling prevents GPU stalls by using multiple buffer sets.


clay
ATTRIBUTE_POS: Int

Vertex attribute location for position data (x, y, z).


clay
ATTRIBUTE_UV: Int

Vertex attribute location for texture coordinate data (u, v).


clay
ATTRIBUTE_COLOR: Int

Vertex attribute location for color data (r, g, b, a).


clay
debugShader: clay.graphics.Shader

clay
renderer: ceramic.Renderer

The Ceramic renderer instance used for high-level rendering operations.


clay
begin(): Void

Begins a rendering frame (currently unused). Reserved for future use or platform-specific initialization.


clay
end(): Void

Ends a rendering frame (currently unused). Reserved for future use or platform-specific cleanup.


clay
prepareNextBuffers(): Void

Prepares the next set of vertex buffers for use.

This implements a buffer cycling system to avoid GPU stalls. Instead of reusing the same buffer immediately (which could cause the GPU to wait), it cycles through multiple buffer sets.

Buffer allocation:

  • Position buffer: Full vertex capacity (MAX_VERTS_SIZE)
  • UV buffer: 2/3 of vertex capacity (optimized for quads)
  • Color buffer: Full vertex capacity (4 floats per vertex)
  • Index buffer: MAX_INDICES * 2 capacity

On C++ targets, additional ArrayBufferView objects are created for efficient memory access without copying.


clay
resetIndexes(): Void

Resets all vertex buffer indexes to zero.

This prepares the buffers for a new batch of vertices. Called when starting a new draw batch or after flushing.


clay
updateProjectionMatrix(width: Float, height: Float): Void

Updates the projection matrix for orthographic rendering.

Creates an orthographic projection matrix that maps logical coordinates to normalized device coordinates (-1 to 1).

The projection uses:

  • Origin at top-left (0,0)
  • X increases rightward
  • Y increases downward
  • Z range from -1000 to 1000 for layering
Name Type Description
width Float Viewport width in logical pixels
height Float Viewport height in logical pixels

clay
updateViewMatrix(density: Float, width: Float, height: Float, ?transform: ceramic.Transform, ?flipY: Float = 1): Void

Updates the view matrix for camera transformations.

The view matrix handles:

  • Camera position and rotation (via transform parameter)
  • Screen density scaling for high-DPI displays
  • Y-axis flipping for render-to-texture (textures have inverted Y)

The matrix is inverted at the end because the view matrix represents the inverse of the camera transform.

Name Type Default Description
density Float Screen density multiplier (e.g., 2 for retina)
width Float Viewport width in logical pixels
height Float Viewport height in logical pixels
transform ceramic.Transform (optional) Optional camera transform
flipY Float 1 -1 to flip Y axis (for render targets), 1 for normal

clay
matrixIdentity(m: ceramic.Float32Array): Void
Name Type
m ceramic.Float32Array

clay
setMatrixToTransform(m: ceramic.Float32Array, transform: ceramic.Transform): Void
Name Type
m ceramic.Float32Array
transform ceramic.Transform

Metadata

Name Parameters
:allow backend.Backend
:access backend.Backend