GraphicsBatcher
Interface for graphics batcher implementations.
Provides a unified API for batched rendering across different graphics backends (OpenGL, D3D12, etc.). Each backend would implement this interface with platform-specific optimizations.
The graphics batcher manages vertex/index data accumulation and submission to the GPU. It handles buffer cycling to prevent GPU stalls and supports multi-texture batching.
Typical usage flow:
- initBuffers() - Initialize at startup
- beginRender() - Start frame
- Configure state (shader, textures, blend mode)
- Submit vertices via putVertex(), putUVs(), putColor(), putIndex()
- flush() when buffers full or state changes
- endRender() - End frame
Instance Members
initBuffers(): VoidInitializes vertex and index buffers. Called once during startup to allocate GPU resources.
beginRender(): VoidBegins a new rendering frame. Enables vertex attributes and prepares for draw operations.
endRender(): VoidEnds the current rendering frame. Performs any cleanup or finalization needed after all draw operations.
Configures the vertex layout for the current shader.
This determines how vertex data is structured in the buffer, including support for multi-texture batching and custom attributes.
| Name | Type | Description |
|---|---|---|
hasTextureSlot |
Bool | Whether vertices include a texture slot for multi-texture batching |
customFloatAttributesSize |
Int | Number of custom float attributes per vertex |
Sets the custom shader attributes for vertex layout.
These attributes define how custom per-vertex data is structured in the position buffer. Used by shaders that require additional vertex data beyond position, UV, and color.
| Name | Type | Description |
|---|---|---|
attributes |
Array<Dynamic> | Array of shader attributes (objects with 'size' field), or null for none |
Adds a vertex position to the buffer.
| Name | Type | Description |
|---|---|---|
x |
Float | X coordinate in screen space |
y |
Float | Y coordinate in screen space |
z |
Float | Z coordinate for depth ordering |
Adds a vertex position with texture slot for multi-texture batching.
| Name | Type | Description |
|---|---|---|
x |
Float | X coordinate in screen space |
y |
Float | Y coordinate in screen space |
z |
Float | Z coordinate for depth ordering |
textureSlot |
Float | Texture slot index for multi-texture batching |
Adds texture coordinates for the current vertex.
| Name | Type | Description |
|---|---|---|
u |
Float | Horizontal texture coordinate (0.0 to 1.0) |
v |
Float | Vertical texture coordinate (0.0 to 1.0) |
Adds color data for the current vertex.
| 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) |
Adds an index to the index buffer. Indices reference vertices in the vertex buffer to form primitives.
| Name | Type | Description |
|---|---|---|
i |
Int | Vertex index |
Adds a custom float attribute value for the current vertex. Used with custom shaders that require additional per-vertex data.
| Name | Type | Description |
|---|---|---|
index |
Int | Attribute index (as defined by the shader) |
value |
Float | Attribute value |
endFloatAttributes(): VoidSignals the end of custom float attributes for the current vertex. Must be called after all putFloatAttribute() calls for a vertex.
getNumVertices(): IntGets the number of vertices currently in the buffer.
| Returns | Description |
|---|---|
| Int | Current vertex count |
Checks if the buffer should be flushed before adding more geometry.
| Name | Type | Description |
|---|---|---|
numVerticesAfter |
Int | Number of vertices to be added |
numIndicesAfter |
Int | Number of indices to be added |
| Returns | Description |
|---|---|
| Bool | True if flush is needed before adding the geometry |
remainingVertices(): IntGets the remaining vertex capacity in the buffer.
| Returns | Description |
|---|---|
| Int | Number of vertices that can still be added |
remainingIndices(): IntGets the remaining index capacity in the buffer.
| Returns | Description |
|---|---|
| Int | Number of indices that can still be added |
hasAnythingToFlush(): BoolChecks if there is any geometry in the buffer to flush.
| Returns | Description |
|---|---|
| Bool | True if there are vertices/indices waiting to be submitted |
flush(): VoidFlushes all buffered geometry to the GPU.
Submits accumulated vertex and index data as a draw call. Should be called when:
- Buffers are full
- Render state changes (texture, shader, blend mode)
- Frame is complete
Clears the current render target.
| Name | Type | Description |
|---|---|---|
r |
Float | Red component of clear color (0.0 to 1.0) |
g |
Float | Green component of clear color (0.0 to 1.0) |
b |
Float | Blue component of clear color (0.0 to 1.0) |
a |
Float | Alpha component of clear color (0.0 to 1.0) |
clearDepth |
Bool | Whether to also clear the depth buffer |
Sets the viewport dimensions.
| Name | Type | Description |
|---|---|---|
x |
Int | Left edge of viewport |
y |
Int | Bottom edge of viewport |
width |
Int | Viewport width in pixels |
height |
Int | Viewport height in pixels |
Sets the primitive type for rendering.
| Name | Type | Description |
|---|---|---|
primitiveType |
Int | 0 for triangles, 1 for lines |
enableBlending(): VoidEnables alpha blending for subsequent draw operations.
disableBlending(): VoidDisables alpha blending for subsequent draw operations.
Sets separate blend functions for RGB and alpha channels.
| Name | Type | Description |
|---|---|---|
srcRgb |
Int | Source blend factor for RGB channels |
dstRgb |
Int | Destination blend factor for RGB channels |
srcAlpha |
Int | Source blend factor for alpha channel |
dstAlpha |
Int | Destination blend factor for alpha channel |
Sets the active texture slot for multi-texturing.
| Name | Type | Description |
|---|---|---|
slot |
Int | Texture slot index (0-based) |
bindTexture(textureId: clay.TextureId): VoidBinds a texture to the current texture slot.
| Name | Type | Description |
|---|---|---|
textureId |
clay.TextureId | Texture identifier to bind |
bindNoTexture(): VoidUnbinds any texture from the current texture slot. On some platforms, binds a default white texture instead.
shouldFlipRenderTargetY(): BoolReturns true if render target
MVP matrix should be flipped vertically.
| Returns |
|---|
| Bool |
setRenderTarget(renderTarget: clay.RenderTarget): VoidSets the render target for subsequent draw operations.
| Name | Type | Description |
|---|---|---|
renderTarget |
clay.RenderTarget | Render target to draw into, or null for main framebuffer |
blitRenderTargetBuffers(renderTarget: clay.RenderTarget, width: Int, height: Int): VoidResolves MSAA render target buffers. Called when switching away from an antialiased render target.
| Name | Type | Description |
|---|---|---|
renderTarget |
clay.RenderTarget | The render target to resolve |
width |
Int | Width of the render target |
height |
Int | Height of the render target |
useShader(shader: clay.GpuShader): VoidActivates a shader program for subsequent draw operations.
| Name | Type | Description |
|---|---|---|
shader |
clay.GpuShader | Shader program to use |
setProjectionMatrix(matrix: clay.buffers.Float32Array): VoidSets the projection matrix uniform.
| Name | Type | Description |
|---|---|---|
matrix |
clay.buffers.Float32Array | 4x4 projection matrix as Float32Array (16 elements) |
setModelViewMatrix(matrix: clay.buffers.Float32Array): VoidSets the model-view matrix uniform.
| Name | Type | Description |
|---|---|---|
matrix |
clay.buffers.Float32Array | 4x4 model-view matrix as Float32Array (16 elements) |
beginStencilWrite(): VoidBegins writing to the stencil buffer. Subsequent draw calls will write to stencil instead of color buffer.
endStencilWrite(): VoidEnds writing to the stencil buffer. Returns to normal color buffer rendering.
enableStencilTest(): VoidEnables stencil testing for subsequent draw operations. Only pixels passing the stencil test will be rendered.
disableStencilTest(): VoidDisables stencil testing for subsequent draw operations.
Enables scissor testing with the specified rectangle. Only pixels within this rectangle will be rendered.
| Name | Type | Description |
|---|---|---|
x |
Float | Left edge of scissor rectangle |
y |
Float | Top edge of scissor rectangle |
width |
Float | Width of scissor rectangle |
height |
Float | Height of scissor rectangle |
disableScissor(): VoidDisables scissor testing.