UBOCache

View source

Provides a world resource that manages named WebGL uniform buffers for shader uniform blocks.

Use it with WebglRenderPipeline when a pipeline contains named uniform blocks so shader reflection and buffer binding use the same names.

Examples

import { UBOCache } from '@wimaengine/render-webgl'

function registerMaterialBlock(world, gl) {
  const ubos = world.getResource(UBOCache)
  ubos.create(gl, 'Material', 16)

  const material = ubos.get('Material')
  return material?.buffer
}

Constructor


constructor()

Methods


create(gl: WebGL2RenderingContext, name: string, size: number): void

View source

Creates and registers a dynamic WebGL uniform buffer.

The buffer is allocated with createBuffer as a gl.UNIFORM_BUFFER, bound to the next available binding point, and stored under name. Binding points start at zero and increase by one for every call. Creating another buffer with an existing name replaces that entry and still consumes a new binding point.

Parameters

NameDescriptionDefault
gl

Context that owns the buffer.

—
name

Uniform-block name used by shader lookup.

—
size

Buffer size in bytes.

—

get(name: string): UBO

View source

Returns the registered uniform-buffer metadata for a name.

The result contains the WebGL buffer, its allocated binding point, and the requested size. No buffer is created when the name is absent.

Parameters

NameDescriptionDefault
name

Uniform-block name to look up.

—

Returns

The metadata, or undefined when unregistered.