GLGraphicsDriver
OpenGL implementation of the cross-platform graphics driver interface.
This class provides instance methods for GPU resource management including textures, shaders, render targets, and framebuffer operations. It serves as the OpenGL backend for Clay's graphics abstraction layer.
The API is designed to be cross-platform, allowing other graphics backends (D3D12, Metal, Vulkan) to implement equivalent functionality via spec.GraphicsDriver.
Key features:
- Texture creation, binding, and management
- Shader compilation and uniform management
- Render target creation for off-screen rendering
- Framebuffer and renderbuffer management
- Blending and viewport configuration
Access via Clay.app.graphics.
Instance Members
noTexture: clay.TextureIdSentinel value representing no texture bound.
noFramebuffer: clay.FramebufferSentinel value representing the default framebuffer.
noRenderbuffer: clay.RenderbufferSentinel value representing the default renderbuffer.
noShader: clay.ShaderHandleSentinel value representing no shader handle.
noProgram: clay.ProgramHandleSentinel value representing no program handle.
noLocation: clay.UniformLocationSentinel value representing an invalid uniform location.
noBuffer: clay.BufferHandleSentinel value representing no buffer bound.
setup(): VoidInitializes the graphics driver after the graphics context is ready.
This must be called once after the window and graphics context are created. Fetches default framebuffer and renderbuffer bindings from the platform.
getMaxTextureSize(): IntReturns the maximum texture size supported by the GPU.
| Returns | Description |
|---|---|
| Int | Maximum texture dimension in pixels |
getMaxTextureUnits(): IntReturns the maximum number of texture units available.
| Returns | Description |
|---|---|
| Int | Maximum number of texture units (capped at 32) |
Tests the maximum number of if-statements supported in fragment shaders.
This is used for multi-texture batching optimization, which requires conditional texture sampling based on texture ID per vertex.
| Name | Type | Default | Description |
|---|---|---|---|
maxIfs |
Int | 32 |
Starting maximum to test (halves on failure until working) |
| Returns | Description |
|---|---|
| Int | Maximum number of if-statements supported |
loadExtensions(): VoidLoads platform-specific graphics extensions.
On WebGL, this loads extensions like OES_standard_derivatives that provide additional shader functionality.
Reads pixels from the current framebuffer.
The pixels are read in RGBA format with unsigned bytes (0-255). The buffer must be large enough to hold width * height * 4 bytes.
| Name | Type | Description |
|---|---|---|
x |
Int | X position to start reading |
y |
Int | Y position to start reading |
width |
Int | Width of the rectangle to read |
height |
Int | Height of the rectangle to read |
pixels |
clay.buffers.Uint8Array | Buffer to store RGBA pixel data |
Clears the current render buffer.
| Name | Type | Default | 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 | true |
Whether to also clear the depth buffer (default: true) |
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 |
enableBlending(): VoidEnables alpha blending for subsequent draw operations.
disableBlending(): VoidDisables alpha blending for subsequent draw operations.
setBlendFuncSeparate(srcRgb: clay.BlendMode, dstRgb: clay.BlendMode, srcAlpha: clay.BlendMode, dstAlpha: clay.BlendMode): VoidSets separate blend functions for RGB and alpha channels.
| Name | Type | Description |
|---|---|---|
srcRgb |
clay.BlendMode | Source blend factor for RGB channels |
dstRgb |
clay.BlendMode | Destination blend factor for RGB channels |
srcAlpha |
clay.BlendMode | Source blend factor for alpha channel |
dstAlpha |
clay.BlendMode | Destination blend factor for alpha channel |
createTextureId(): clay.TextureIdCreates a new texture and returns its identifier.
| Returns | Description |
|---|---|
| clay.TextureId | New texture identifier |
deleteTexture(textureId: clay.TextureId): VoidDeletes a texture from the GPU.
| Name | Type | Description |
|---|---|---|
textureId |
clay.TextureId | Texture identifier to delete |
Sets the active texture slot for subsequent texture operations.
| Name | Type | Description |
|---|---|---|
slot |
Int | Texture slot index (0-based) |
bindTexture2d(textureId: clay.TextureId): VoidBinds a 2D texture to the current texture slot.
| Name | Type | Description |
|---|---|---|
textureId |
clay.TextureId | Texture identifier to bind, or noTexture to unbind |
maxTextureSize(): IntReturns the maximum texture size supported by the GPU.
| Returns | Description |
|---|---|
| Int | Maximum texture dimension in pixels |
needsPreprocessedPremultipliedAlpha(): BoolChecks if premultiplied textures require pixel preprocessing.
| Returns | Description |
|---|---|
| Bool | True if preprocessing is needed, false if handled by GL |
submitTexture2dPixels(level: Int, format: clay.TextureFormat, width: Int, height: Int, dataType: clay.TextureDataType, pixels: clay.buffers.Uint8Array, premultipliedAlpha: Bool): VoidSubmits 2D texture pixels to the GPU.
| Name | Type | Description |
|---|---|---|
level |
Int | Mipmap level (0 = base image) |
format |
clay.TextureFormat | Texture format (RGB or RGBA) |
width |
Int | Texture width in pixels |
height |
Int | Texture height in pixels |
dataType |
clay.TextureDataType | Data type of pixel data (UNSIGNED_BYTE) |
pixels |
clay.buffers.Uint8Array | Pixel data buffer |
premultipliedAlpha |
Bool | Whether pixels use premultiplied alpha |
submitCompressedTexture2dPixels(level: Int, format: clay.TextureFormat, width: Int, height: Int, pixels: clay.buffers.Uint8Array, premultipliedAlpha: Bool): VoidSubmits compressed 2D texture pixels to the GPU.
| Name | Type | Description |
|---|---|---|
level |
Int | Mipmap level (0 = base image) |
format |
clay.TextureFormat | Compressed texture format |
width |
Int | Texture width in pixels |
height |
Int | Texture height in pixels |
pixels |
clay.buffers.Uint8Array | Compressed pixel data buffer |
premultipliedAlpha |
Bool | Whether pixels use premultiplied alpha |
Fetches 2D texture pixels from the GPU.
| Name | Type | Description |
|---|---|---|
into |
clay.buffers.Uint8Array | Buffer to store pixel data (must be at least w * h * 4 bytes) |
x |
Int | X position of texture rectangle to fetch |
y |
Int | Y position of texture rectangle to fetch |
w |
Int | Width of texture rectangle to fetch |
h |
Int | Height of texture rectangle to fetch |
setTexture2dMinFilter(minFilter: clay.TextureFilter): VoidSets the minification filter for 2D textures.
| Name | Type | Description |
|---|---|---|
minFilter |
clay.TextureFilter | Filter mode (NEAREST, LINEAR, or mipmap variants) |
setTexture2dMagFilter(magFilter: clay.TextureFilter): VoidSets the magnification filter for 2D textures.
| Name | Type | Description |
|---|---|---|
magFilter |
clay.TextureFilter | Filter mode (NEAREST or LINEAR) |
setTexture2dWrapS(wrapS: clay.TextureWrap): VoidSets the horizontal (S) texture wrap mode.
| Name | Type | Description |
|---|---|---|
wrapS |
clay.TextureWrap | Wrap mode (CLAMP_TO_EDGE, REPEAT, or MIRRORED_REPEAT) |
setTexture2dWrapT(wrapT: clay.TextureWrap): VoidSets the vertical (T) texture wrap mode.
| Name | Type | Description |
|---|---|---|
wrapT |
clay.TextureWrap | Wrap mode (CLAMP_TO_EDGE, REPEAT, or MIRRORED_REPEAT) |
createFramebuffer(): clay.FramebufferCreates a new framebuffer object.
| Returns | Description |
|---|---|
| clay.Framebuffer | Created framebuffer |
bindFramebuffer(framebuffer: clay.Framebuffer): VoidBinds a framebuffer for rendering.
| Name | Type | Description |
|---|---|---|
framebuffer |
clay.Framebuffer | Framebuffer to bind, or noFramebuffer for default |
createRenderbuffer(): clay.RenderbufferCreates a new renderbuffer object.
| Returns | Description |
|---|---|
| clay.Renderbuffer | Created renderbuffer |
bindRenderbuffer(renderbuffer: clay.Renderbuffer): VoidBinds a renderbuffer.
| Name | Type | Description |
|---|---|---|
renderbuffer |
clay.Renderbuffer | Renderbuffer to bind, or noRenderbuffer for default |
createRenderTarget(textureId: clay.TextureId, width: Int, height: Int, depth: Bool, stencil: Bool, antialiasing: Int, level: Int, format: clay.TextureFormat, dataType: clay.TextureDataType): clay.RenderTargetCreates a render target for off-screen rendering.
| Name | Type | Description |
|---|---|---|
textureId |
clay.TextureId | Texture to render into |
width |
Int | Render target width in pixels |
height |
Int | Render target height in pixels |
depth |
Bool | Whether to include a depth buffer |
stencil |
Bool | Whether to include a stencil buffer |
antialiasing |
Int | MSAA sample count (0 or 1 for none) |
level |
Int | Mipmap level (0 = base) |
format |
clay.TextureFormat | Texture format |
dataType |
clay.TextureDataType | Data type of texture |
| Returns | Description |
|---|---|
| clay.RenderTarget | Created render target |
deleteRenderTarget(renderTarget: clay.RenderTarget): VoidDeletes a render target from the GPU.
| Name | Type | Description |
|---|---|---|
renderTarget |
clay.RenderTarget | Render target to delete |
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 |
configureRenderTargetBuffersStorage(renderTarget: clay.RenderTarget, textureId: clay.TextureId, width: Int, height: Int, depth: Bool, stencil: Bool, antialiasing: Int): VoidConfigures storage for render target buffers.
Used when resizing a render target's underlying buffers.
| Name | Type | Description |
|---|---|---|
renderTarget |
clay.RenderTarget | Render target to configure |
textureId |
clay.TextureId | Texture associated with the render target |
width |
Int | New width in pixels |
height |
Int | New height in pixels |
depth |
Bool | Whether to include depth buffer |
stencil |
Bool | Whether to include stencil buffer |
antialiasing |
Int | MSAA sample count |
blitRenderTargetBuffers(renderTarget: clay.RenderTarget, width: Int, height: Int): VoidResolves MSAA render target buffers to the texture.
Called when switching away from an antialiased render target to blit the multisampled content to the texture.
| Name | Type | Description |
|---|---|---|
renderTarget |
clay.RenderTarget | Render target to resolve |
width |
Int | Width of the render target |
height |
Int | Height of the render target |
createShader(vertSource: String, fragSource: String, ?attributes: Array<String>, ?textures: Array<String>): clay.GpuShaderCreates and compiles a shader program.
Automatically patches GLSL version for platform compatibility:
- Desktop GL (non-ANGLE): converts
#version 300 esto#version 330 - Mobile/WebGL (GLES/ANGLE): keeps
#version 300 es
| Name | Type | Default | Description |
|---|---|---|---|
vertSource |
String | Vertex shader source code | |
fragSource |
String | Fragment shader source code | |
attributes |
Array<String> | (optional) | Optional array of attribute names in binding order |
textures |
Array<String> | (optional) | Optional array of texture uniform names in slot order |
| Returns | Description |
|---|---|
| clay.GpuShader | Compiled shader, or null on failure |
deleteShader(shader: clay.GpuShader): VoidDeletes a shader program from the GPU.
| Name | Type | Description |
|---|---|---|
shader |
clay.GpuShader | Shader program to delete |
useShader(shader: clay.GpuShader): VoidActivates a shader program for subsequent draw operations.
| Name | Type | Description |
|---|---|---|
shader |
clay.GpuShader | Shader program to use |
synchronizeShaderMatrices(shader: clay.GpuShader): VoidSynchronizes projectionMatrix and modelViewMatrix with the given shader, as this is needed on some graphics backend.
| Name | Type | Description |
|---|---|---|
shader |
clay.GpuShader | Shader program to synchronize |
getUniformLocation(shader: clay.GpuShader, name: String): clay.UniformLocationGets the location of a uniform variable in a shader.
| Name | Type | Description |
|---|---|---|
shader |
clay.GpuShader | Shader program |
name |
String | Uniform variable name |
| Returns | Description |
|---|---|
| clay.UniformLocation | Uniform location, or noLocation if not found |
setIntUniform(shader: clay.GpuShader, location: clay.UniformLocation, value: Int): VoidSets an integer uniform value.
| Name | Type | Description |
|---|---|---|
shader |
clay.GpuShader | Shader program |
location |
clay.UniformLocation | Uniform location |
value |
Int | Integer value |
setIntArrayUniform(shader: clay.GpuShader, location: clay.UniformLocation, value: clay.buffers.Int32Array): VoidSets an integer array uniform value.
| Name | Type | Description |
|---|---|---|
shader |
clay.GpuShader | Shader program |
location |
clay.UniformLocation | Uniform location |
value |
clay.buffers.Int32Array | Integer array |
setFloatUniform(shader: clay.GpuShader, location: clay.UniformLocation, value: Float): VoidSets a float uniform value.
| Name | Type | Description |
|---|---|---|
shader |
clay.GpuShader | Shader program |
location |
clay.UniformLocation | Uniform location |
value |
Float | Float value |
setFloatArrayUniform(shader: clay.GpuShader, location: clay.UniformLocation, value: clay.buffers.Float32Array): VoidSets a float array uniform value.
| Name | Type | Description |
|---|---|---|
shader |
clay.GpuShader | Shader program |
location |
clay.UniformLocation | Uniform location |
value |
clay.buffers.Float32Array | Float array |
setVector2Uniform(shader: clay.GpuShader, location: clay.UniformLocation, x: Float, y: Float): VoidSets a 2D vector uniform value.
| Name | Type | Description |
|---|---|---|
shader |
clay.GpuShader | Shader program |
location |
clay.UniformLocation | Uniform location |
x |
Float | X component |
y |
Float | Y component |
setVector3Uniform(shader: clay.GpuShader, location: clay.UniformLocation, x: Float, y: Float, z: Float): VoidSets a 3D vector uniform value.
| Name | Type | Description |
|---|---|---|
shader |
clay.GpuShader | Shader program |
location |
clay.UniformLocation | Uniform location |
x |
Float | X component |
y |
Float | Y component |
z |
Float | Z component |
setVector4Uniform(shader: clay.GpuShader, location: clay.UniformLocation, x: Float, y: Float, z: Float, w: Float): VoidSets a 4D vector uniform value.
| Name | Type | Description |
|---|---|---|
shader |
clay.GpuShader | Shader program |
location |
clay.UniformLocation | Uniform location |
x |
Float | X component |
y |
Float | Y component |
z |
Float | Z component |
w |
Float | W component |
setMatrix2Uniform(shader: clay.GpuShader, location: clay.UniformLocation, value: clay.buffers.Float32Array): VoidSets a 2x2 matrix uniform value.
| Name | Type | Description |
|---|---|---|
shader |
clay.GpuShader | Shader program |
location |
clay.UniformLocation | Uniform location |
value |
clay.buffers.Float32Array | Matrix as Float32Array (4 elements, column-major) |
setMatrix3Uniform(shader: clay.GpuShader, location: clay.UniformLocation, value: clay.buffers.Float32Array): VoidSets a 3x3 matrix uniform value.
| Name | Type | Description |
|---|---|---|
shader |
clay.GpuShader | Shader program |
location |
clay.UniformLocation | Uniform location |
value |
clay.buffers.Float32Array | Matrix as Float32Array (9 elements, column-major) |
setMatrix4Uniform(shader: clay.GpuShader, location: clay.UniformLocation, value: clay.buffers.Float32Array): VoidSets a 4x4 matrix uniform value.
| Name | Type | Description |
|---|---|---|
shader |
clay.GpuShader | Shader program |
location |
clay.UniformLocation | Uniform location |
value |
clay.buffers.Float32Array | Matrix as Float32Array (16 elements, column-major) |
setTexture2dUniform(shader: clay.GpuShader, location: clay.UniformLocation, slot: Int, texture: clay.TextureId): VoidSets a texture sampler uniform value.
| Name | Type | Description |
|---|---|---|
shader |
clay.GpuShader | Shader program |
location |
clay.UniformLocation | Uniform location |
slot |
Int | Texture slot index |
texture |
clay.TextureId | Texture identifier to bind |
ensureNoError(): VoidChecks for and throws on any pending graphics errors.
Useful for debugging graphics operations.
new(): VoidCreates a new OpenGL graphics driver instance.
Private Members
DEPTH24_STENCIL8: IntDEPTH_COMPONENT24: IntTEXTURE_2D_MULTISAMPLE: IntREAD_FRAMEBUFFER: IntDRAW_FRAMEBUFFER: IntRGBA8: IntCOLOR: IntprojectionMatrix: clay.buffers.Float32ArraymodelViewMatrix: clay.buffers.Float32ArrayPatches GLSL version directive for platform compatibility.
Shade compiler outputs GLES 3.0 syntax (#version 300 es).
Desktop GL (non-ANGLE) requires #version 330 instead.
| Name | Type | Description |
|---|---|---|
source |
String | Shader source code |
| Returns | Description |
|---|---|
| String | Patched source code |
linkShader(shader: GLGraphicsDriver_GpuShader, ?attributes: Array<String>): BoolLinks vertex and fragment shaders into a program.
| Name | Type | Default | Description |
|---|---|---|---|
shader |
GLGraphicsDriver_GpuShader | Shader object with compiled vert/frag shaders | |
attributes |
Array<String> | (optional) | Optional array of attribute names in binding order |
| Returns | Description |
|---|---|
| Bool | True if linking succeeded, false otherwise |
configureShaderTextureSlots(shader: GLGraphicsDriver_GpuShader, textures: Array<String>): VoidConfigures texture slot uniform bindings for a shader.
| Name | Type | Description |
|---|---|---|
shader |
GLGraphicsDriver_GpuShader | Shader to configure |
textures |
Array<String> | Array of texture uniform names in slot order |
Compiles a single GL shader (vertex or fragment).
| Name | Type | Description |
|---|---|---|
type |
Int | GL.VERTEX_SHADER or GL.FRAGMENT_SHADER |
source |
String | GLSL source code |
| Returns | Description |
|---|---|
| GLShader | Compiled shader, or noShader on failure |