GraphicsBatcher

clay.spec.GraphicsBatcher (Interface) → clay.opengl.GLGraphicsBatcher

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:

  1. initBuffers() - Initialize at startup
  2. beginRender() - Start frame
  3. Configure state (shader, textures, blend mode)
  4. Submit vertices via putVertex(), putUVs(), putColor(), putIndex()
  5. flush() when buffers full or state changes
  6. endRender() - End frame

Instance Members

initBuffers(): Void

Initializes vertex and index buffers. Called once during startup to allocate GPU resources.


beginRender(): Void

Begins a new rendering frame. Enables vertex attributes and prepares for draw operations.


endRender(): Void

Ends the current rendering frame. Performs any cleanup or finalization needed after all draw operations.


setVertexLayout(hasTextureSlot: Bool, customFloatAttributesSize: Int): Void

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

setCustomAttributes(attributes: Array<Dynamic>): Void

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

putVertex(x: Float, y: Float, z: Float): Void

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

putVertexWithTextureSlot(x: Float, y: Float, z: Float, textureSlot: Float): Void

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

putUVs(u: Float, v: Float): Void

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)

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

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)

putIndex(i: Int): Void

Adds an index to the index buffer. Indices reference vertices in the vertex buffer to form primitives.

Name Type Description
i Int Vertex index

putFloatAttribute(index: Int, value: Float): Void

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(): Void

Signals the end of custom float attributes for the current vertex. Must be called after all putFloatAttribute() calls for a vertex.


getNumVertices(): Int

Gets the number of vertices currently in the buffer.

Returns Description
Int Current vertex count

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

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(): Int

Gets the remaining vertex capacity in the buffer.

Returns Description
Int Number of vertices that can still be added

remainingIndices(): Int

Gets the remaining index capacity in the buffer.

Returns Description
Int Number of indices that can still be added

hasAnythingToFlush(): Bool

Checks if there is any geometry in the buffer to flush.

Returns Description
Bool True if there are vertices/indices waiting to be submitted

flush(): Void

Flushes 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

clear(r: Float, g: Float, b: Float, a: Float, clearDepth: Bool): Void

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

setViewport(x: Int, y: Int, width: Int, height: Int): Void

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

setPrimitiveType(primitiveType: Int): Void

Sets the primitive type for rendering.

Name Type Description
primitiveType Int 0 for triangles, 1 for lines

enableBlending(): Void

Enables alpha blending for subsequent draw operations.


disableBlending(): Void

Disables alpha blending for subsequent draw operations.


setBlendFuncSeparate(srcRgb: Int, dstRgb: Int, srcAlpha: Int, dstAlpha: Int): Void

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

setActiveTexture(slot: Int): Void

Sets the active texture slot for multi-texturing.

Name Type Description
slot Int Texture slot index (0-based)

bindTexture(textureId: clay.TextureId): Void

Binds a texture to the current texture slot.

Name Type Description
textureId clay.TextureId Texture identifier to bind

bindNoTexture(): Void

Unbinds any texture from the current texture slot. On some platforms, binds a default white texture instead.


shouldFlipRenderTargetY(): Bool

Returns true if render target MVP matrix should be flipped vertically.

Returns
Bool

setRenderTarget(renderTarget: clay.RenderTarget): Void

Sets 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): Void

Resolves 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): Void

Activates a shader program for subsequent draw operations.

Name Type Description
shader clay.GpuShader Shader program to use

setProjectionMatrix(matrix: clay.buffers.Float32Array): Void

Sets the projection matrix uniform.

Name Type Description
matrix clay.buffers.Float32Array 4x4 projection matrix as Float32Array (16 elements)

setModelViewMatrix(matrix: clay.buffers.Float32Array): Void

Sets the model-view matrix uniform.

Name Type Description
matrix clay.buffers.Float32Array 4x4 model-view matrix as Float32Array (16 elements)

beginStencilWrite(): Void

Begins writing to the stencil buffer. Subsequent draw calls will write to stencil instead of color buffer.


endStencilWrite(): Void

Ends writing to the stencil buffer. Returns to normal color buffer rendering.


enableStencilTest(): Void

Enables stencil testing for subsequent draw operations. Only pixels passing the stencil test will be rendered.


disableStencilTest(): Void

Disables stencil testing for subsequent draw operations.


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

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(): Void

Disables scissor testing.