Shaders

backend.Shaders (Class)
Implements: spec.Shaders

Clay backend implementation of shader program management. Handles loading GLSL shaders from files and multi-texture batching.

This class loads shader files generated by the shade compiler:

  • Standard shaders: basename.vert, basename.frag
  • Multi-texture variants: basename_mt8.vert, basename_mt8.frag

Instance Members

clay
load(path: String, baseAttributes: ceramic.ReadOnlyArray<ceramic.ShaderAttribute>, customAttributes: ceramic.ReadOnlyArray<ceramic.ShaderAttribute>, textureIdAttribute: ceramic.ShaderAttribute, _done: Function): Void

Loads a shader from .vert and .frag files. Automatically attempts to load multi-texture variant (_mt8 suffix) first for better batching performance.

Name Type Description
path String Path to the shader (without extension, e.g., "shaders/default")
baseAttributes ceramic.ReadOnlyArray<ceramic.ShaderAttribute> Base vertex attributes (position, texCoord, color)
customAttributes ceramic.ReadOnlyArray<ceramic.ShaderAttribute> Custom vertex attributes beyond base ones (can be null)
textureIdAttribute ceramic.ShaderAttribute Texture slot attribute for multi-texture batching (can be null)
_done Function Callback with loaded shader (null on failure)

clay
destroy(shader: Shader): Void

Destroys a shader program and releases GPU resources.

Name Type Description
shader Shader The shader to destroy

clay
clone(shader: Shader): Shader

Creates a deep copy of a shader program.

Name Type Description
shader Shader The shader to clone
Returns Description
Shader A new shader instance with the same properties

clay
setInt(shader: Shader, name: String, value: Int): Void

Sets an integer uniform value in the shader.

Name Type Description
shader Shader Target shader program
name String Uniform variable name
value Int Integer value to set

clay
setFloat(shader: Shader, name: String, value: Float): Void

Sets a float uniform value in the shader.

Name Type Description
shader Shader Target shader program
name String Uniform variable name
value Float Float value to set

clay
setVec2(shader: Shader, name: String, x: Float, y: Float): Void

Sets a 2D vector uniform value in the shader.

Name Type Description
shader Shader Target shader program
name String Uniform variable name
x Float X component
y Float Y component

clay
setVec3(shader: Shader, name: String, x: Float, y: Float, z: Float): Void

Sets a 3D vector uniform value in the shader.

Name Type Description
shader Shader Target shader program
name String Uniform variable name
x Float X component
y Float Y component
z Float Z component

clay
setVec4(shader: Shader, name: String, x: Float, y: Float, z: Float, w: Float): Void

Sets a 4D vector uniform value in the shader.

Name Type Description
shader Shader Target shader program
name String Uniform variable name
x Float X component
y Float Y component
z Float Z component
w Float W component

clay
setFloatArray(shader: Shader, name: String, array: Array<Float>): Void

Sets a float array uniform value in the shader.

Name Type Description
shader Shader Target shader program
name String Uniform variable name
array Array<Float> Array of float values

clay
setTexture(shader: Shader, name: String, slot: Int, texture: Texture): Void

Sets a texture uniform value in the shader.

Name Type Description
shader Shader Target shader program
name String Uniform variable name
slot Int Texture unit slot (0-based)
texture Texture Texture to bind

clay
setMat2(shader: Shader, name: String, m00: Float, m10: Float, m01: Float, m11: Float): Void

Sets a 2x2 matrix uniform value in the shader (column-major order).

Name Type Description
shader Shader Target shader program
name String Uniform variable name
m00 Float Column 0, row 0
m10 Float Column 0, row 1
m01 Float Column 1, row 0
m11 Float Column 1, row 1

clay
setMat3(shader: Shader, name: String, m00: Float, m10: Float, m20: Float, m01: Float, m11: Float, m21: Float, m02: Float, m12: Float, m22: Float): Void

Sets a 3x3 matrix uniform value in the shader (column-major order).

Name Type Description
shader Shader Target shader program
name String Uniform variable name
m00 Float Column 0, row 0
m10 Float Column 0, row 1
m20 Float Column 0, row 2
m01 Float Column 1, row 0
m11 Float Column 1, row 1
m21 Float Column 1, row 2
m02 Float Column 2, row 0
m12 Float Column 2, row 1
m22 Float Column 2, row 2

clay
setMat4(shader: Shader, name: String, m00: Float, m10: Float, m20: Float, m30: Float, m01: Float, m11: Float, m21: Float, m31: Float, m02: Float, m12: Float, m22: Float, m32: Float, m03: Float, m13: Float, m23: Float, m33: Float): Void

Sets a 4x4 matrix uniform value in the shader (column-major order).

Name Type Description
shader Shader Target shader program
name String Uniform variable name
m00 Float Column 0, row 0
m10 Float Column 0, row 1
m20 Float Column 0, row 2
m30 Float Column 0, row 3
m01 Float Column 1, row 0
m11 Float Column 1, row 1
m21 Float Column 1, row 2
m31 Float Column 1, row 3
m02 Float Column 2, row 0
m12 Float Column 2, row 1
m22 Float Column 2, row 2
m32 Float Column 2, row 3
m03 Float Column 3, row 0
m13 Float Column 3, row 1
m23 Float Column 3, row 2
m33 Float Column 3, row 3

clay
customFloatAttributesSize(shader: ShaderImpl): Int

Calculates the total size of custom float attributes for a shader. Used for vertex buffer layout calculations.

Name Type Description
shader ShaderImpl The shader to analyze
Returns Description
Int Total number of floats needed for custom attributes

clay
maxIfStatementsByFragmentShader(): Int

Returns the maximum number of if-statements supported by fragment shaders. For shaders with _mt8 variants, this is always 8.

Returns Description
Int Maximum if-statements supported (8 for multi-texture shaders)

clay
canBatchWithMultipleTextures(shader: Shader): Bool

Checks if a shader supports multi-texture batching. Multi-texture shaders can render multiple textures in a single draw call.

Name Type Description
shader Shader The shader to check
Returns Description
Bool True if the shader supports multi-texture batching

clay
supportsHotReloadPath(): Bool

Indicates whether hot-reloading of shader files is supported. Clay backend supports watching shader files for changes.

Returns Description
Bool Always returns true for Clay backend

clay
new(): Void

Private Members

clay
tryLoadShader(basePath: String, baseAttributes: ceramic.ReadOnlyArray<ceramic.ShaderAttribute>, customAttributes: ceramic.ReadOnlyArray<ceramic.ShaderAttribute>, textureIdAttribute: ceramic.ShaderAttribute, isMultiTexture: Bool, callback: Function): Void

Attempts to load a shader from the given base path. Reads .vert and .frag files asynchronously and compiles them.

Name Type Description
basePath String Path without extension
baseAttributes ceramic.ReadOnlyArray<ceramic.ShaderAttribute> Base vertex attributes
customAttributes ceramic.ReadOnlyArray<ceramic.ShaderAttribute> Custom vertex attributes
textureIdAttribute ceramic.ShaderAttribute Texture slot attribute for multi-texture
isMultiTexture Bool Whether this is a multi-texture variant
callback Function Called with shader or null on failure

clay
buildMultiTextures(): Array<String>

Builds texture uniform names array for multi-texture batching.

Returns Description
Array<String> Array of texture uniform names (mainTex, tex1, ..., tex8)