Skip to content

buffer

FreeBodyEngine.graphics.webgl.buffer #

WebGL2TextureBuffer: the WebGL2 implementation of the buffer-texture "readable storage buffer" - see graphics/gl33/buffer.py's own TextureBuffer for the desktop equivalent this mirrors. WebGL2 (GLSL ES 3.00) has no GL_TEXTURE_BUFFER/samplerBuffer at all (that's ES 3.2+/desktop-only, no WebGL2 extension exposes it either) - the same "read-only buffer, sampled via texelFetch" contract is emulated here as an ordinary 2D data texture instead. A 1D index i maps to a 2D texel at (i % width, i / width), where width is however wide this texture was actually created (queried back in GLSL via textureSize() - see WebGL2Generator's _ENGINE_buf_idx() helper) - no separate "width" uniform needs uploading or staying in sync.

WebGL2TextureBuffer(data, gl_internal_format=None) #

Bases: Buffer

Creates the backing 2D texture and uploads data via set_data(). gl_internal_format overrides the format set_data() would otherwise infer from data's shape/dtype (see _infer_format) - matching TextureBuffer.init()'s own contract exactly.

data = None instance-attribute #

gl = get_service('renderer').gl instance-attribute #

height = 1 instance-attribute #

internal_format = gl_internal_format instance-attribute #

tex = gl.createTexture() instance-attribute #

width = 1 instance-attribute #

bind(texture_unit) #

Activates texture_unit and binds this buffer's data texture to it, ready for texelFetch() in GLSL.

destroy() #

Deletes this object's underlying GL texture.

get_data() #

Returns the data last passed to set_data(), already coerced to this buffer's actual dtype/format (unpadded - the real element count, not width*height).

get_max_size() staticmethod #

Returns this GPU's max element count for one buffer (a single MAX_TEXTURE_SIZE-wide row of texels) - the WebGL2 analogue of TextureBuffer.get_max_size()'s GL_MAX_TEXTURE_BUFFER_SIZE, though unlike a real buffer texture this backend can still grow taller instead of failing outright past this many elements (see set_data()'s height growth) - this is a "still fits in one row" figure, not a hard ceiling.

set_data(data) #

Uploads data into a 2D texture sized to hold it one texel per element, wrapping to further rows as needed (width capped at this GPU's MAX_TEXTURE_SIZE, height grown instead once a single row can't hold every element) - the WebGL2 texture equivalent of TextureBuffer.set_data()'s GL_TEXTURE_BUFFER upload. The tail of the last row is zero-padded when data's length isn't an exact multiple of width; nothing in this backend's generated GLSL (see WebGL2Generator's buffer-block/raytrace codegen) ever computes an index reaching into that padding, so its contents are never actually read.

unbind() #

Unbinds GL_TEXTURE_2D from whichever unit is currently active.