9.7 KiB
Buffer
A Buffer is a WebGL object that stores an chunk of memory allocated by the GPU. This memory can be accessed directly by the GPU and is used to store things like vertex data, pixel data retrieved from images or the framebuffer, etc. The Buffer class provides mechanism for allocating such memory, together with facilities for copying data to and from the GPU (usually via JavaScript typed arrays).
For additional information, see OpenGL Wiki.
Buffer Methods
| Method | Description |
|---|---|
constructor |
Creates a Buffer |
initialize |
Allocates and optionally initializes the buffer object's data store on GPU. |
delete |
Destroys buffer |
subData |
Updates a subset of a buffer object's data store. |
copySubData (WebGL2) |
Copies part of the data of another buffer into this buffer |
getSubData (WebGL2) |
Reads data from buffer (GPU) into an ArrayBuffer or SharedArrayBuffer. |
Usage
import {Buffer} from 'luma.gl';
Creating a generic buffer
const buffer = new Buffer(gl);
Creating an elements buffer
const buffer = new Buffer(gl, {target: GL.ELEMENT_ARRAY_BUFFER});
Allocating memory in a buffer
const buffer = new Buffer(gl, {bytes: 200});
const buffer = new Buffer(gl).initialize({bytes: 200});
Allocating and initializating a buffer
const buffer = new Buffer(gl, {
target: GL.ELEMENTS_ARRAY_BUFFER,
data: new Uint32Array([1, 2, 3])
});
const buffer = new Buffer(gl, {size: 3, data: new Float32Array([1, 2, 3])});
const buffer = new Buffer(gl).initialize({bytes: 200});
Updating a buffer
const buffer = new Buffer(gl, {bytes: 200})
buffer.subData({})
Copying data between buffers (WebGL2)
const buffer = new Buffer(gl, {bytes: 200})
buffer.subData({offset: 20, data: new Float32Array([1, 2, 3])});
Getting data from a buffer (WebGL2)
const buffer = ...;
const data = buffer.getSubData({offset: 20, size: 10});
Binding and unbinding a buffer
const buffer = ...;
buffer.bind({target: GL.ARRAY_BUFFER});
...
buffer.unbind({target: GL.ARRAY_BUFFER});
WebGL2 examples
buffer.bind({target: GL.PIXEL_PACK_BUFFER});
buffer.bind({target: GL.PIXEL_UNPACK_BUFFER});
buffer.bind({target: GL.TRANSFORM_FEEDBACK_BUFFER, index: 0});
buffer.bind({target: GL.UNIFORM_BUFFER, index: 0, offset: ..., size: ...});
buffer.unbind({target: GL.UNIFORM_BUFFER, index: 0});
Note: buffer binding and unbinding is handled internal by luma.gl methods so the application will typically not need to bind buffers unless integrating with external libraries or raw webgl code).
Members
handle- holds the underlyingWebGLBuffer
Methods
constructor
Creates a new Buffer, which will either be a an "element" buffer used for indices, or a generic buffer. To create an element buffer, simply specify target: GL.ELEMENT_ARRAY_BUFFER. If not, it will be a generic buffer that can be used in a variety of situations.
const buffer = new Buffer(gl, {target, ...initOptions, ...layoutOptions});
gl(WebGLRenderingContext) - gl contexttarget=GL.ARRAY_BUFFER|GL.COPY_READ_BUFFER(GLenum, optional) - the type of the buffer, see notes....initOptions(Object) - options passed on toinitialize....layoutOptions- options passed on tosetLayout
Note:
- In WebGL1, the default is
GL.ARRAY_BUFFERwhich will work as a generic buffer. - In WebGL2, the default is
GL.COPY_READ_BUFFERwhich means the buffer can work either as a generic buffer and an element buffer. This will be determined when it is first used (bound). From that point on, WebGL will consider it either as an element buffer or a generic buffer.
initialize
Allocates and optionally initializes buffer memory/data store (releasing any previously allocated memory).
Also extracts characteristics of stored data, hints for vertex attribute.
Buffer.initialize({data, bytes, usage=, dataType=, size=, ...layoutOptions})
data(ArrayBufferView) - contentsbytes(GLsizeiptr) - the size of the buffer object's data store.usage=GL.STATIC_DRAW(GLenum) - Allocation hint for GPU driver.type=Inferred (GLenum) - type of data stored in buffer. Inferred fromdataif supplied, otherwiseGL.FLOAT.size=1(GLuint) - number of components per vertex, e.g. avec2has 2 components....layoutOptions- parameters passed tosetLayout
Returns itself for chaining.
setLayout
Allows you to optionally describe the layout of the data in the buffer. This does not affect the buffer itself, but enables you can to avoid having to supply this data again (You might use it as an attribute later, see VertexArray).
Buffer.setLayout({bytes, usage=, dataType=, size=, type=})
type= type of the data being stored in the buffer. Usually not needed, when inferred by the typed array supplied asdata.size=1(number, optional) - The number of components in each element the buffer (typically 1-4).normalized=false(boolean, optional) -integer=false(boolean, optional) -instanced=0(number, optional) - whether buffer contains instance dataoffset=0(number, optional) - theoffset, where the data starts in the buffer.stride=0(number, optional) - thestriderepresents an additional offset between each element in the buffer.
Notes:
offsetandstrideare typically used to interleave data in buffers.
subData
Updates part or all of a buffer's allocated memory.
Buffer.subData({data, offset=, srcOffset=, length=})
data(ArrayBufferView) - length is inferred unless providedoffset=0- Offset into buffersrcOffset=0- WebGL2: Offset into srcDatalength- WebGL2: Number of bytes to be copied
Returns itself for chaining.
copyData (WEBGL2)
Copies part of the data of another buffer into this buffer. The copy happens on the GPU and is expected to be efficient.
Buffer.copyData({sourceBuffer, readOffset=, writeOffset=, size})
sourceBuffer(Buffer) - the buffer to read data from.readOffset=0(GLint) - byte offset from which to start reading from the buffer.writeOffset=0(GLint) - byte offset from which to start writing to the buffer.size(GLsizei) - byte count, specifying the size of the data to be copied.
Returns itself for chaining.
Note:
readOffset,writeOffsetandsizemust all be greater than or equal to zero.readOffset + sizereadOffset + sizemust not exceeed the size of the source buffer objectwriteOffset + sizewriteOffset + sizemust not exceeed the size of the buffer bound to writeTarget.- If the source and destination are the same buffer object, then the source and destination ranges must not overlap.
getData (WEBGL2)
Reads data from buffer into an ArrayBuffer or SharedArrayBuffer.
Buffer.getData({dstData, srcByteOffset, srcOffset, length})
dstData=null(ArrayBufferView|ArrayBuffer|SharedArrayBuffer|null) - memory to which to write the buffer data.srcByteOffset=0(GLintptr) - byte offset from which to start reading from the buffer.srcOffset=0(GLuint) - element index offset where to start reading the buffer.length=0(GLuint) Optional, defaulting to 0.
Returns a typed array containing the data from the buffer (if dstData was supplied it will be returned, otherwise this will be a freshly allocated array).
Types
Usage
| Usage | WebGL2 | WebGL1 | Description |
|---|---|---|---|
GL.STATIC_DRAW |
Yes | Yes | Buffer will be used often and not change often. Contents are written to the buffer, but not read. |
GL.DYNAMIC_DRAW |
Yes | Yes | Buffer will be used often and change often. Contents are written to the buffer, but not read. |
GL.STREAM_DRAW |
Yes | Yes | Buffer will not be used often. Contents are written to the buffer, but not read. |
GL.STATIC_READ |
Yes | No | Buffer will be used often and not change often. Contents are read from the buffer, but not written. |
GL.DYNAMIC_READ |
Yes | No | Buffer will be used often and change often. Contents are read from the buffer, but not written. |
GL.STREAM_READ |
Yes | No | Buffer will not be used often. Contents are read from the buffer, but not written. |
GL.STATIC_COPY |
Yes | No | Buffer will be used often and not change often. Contents are neither written or read by the user. |
GL.DYNAMIC_COPY |
Yes | No | Buffer will be used often and change often. Contents are neither written or read by the user. |
GL.STREAM_COPY |
Yes | No | Buffer will be used often and not change often. Contents are neither written or read by the user. |
Parameters
| Parameter | Type | Value |
|---|---|---|
GL.BUFFER_SIZE |
GLint | The size of the buffer in bytes |
GL.BUFFER_USAGE |
GLenum | The usage pattern of the buffer |
Remarks
- All instance methods in a buffer (unless they return some documented value) are chainable.
- While transferring memory between CPU and GPU takes some time, once the memory is available as a buffer on the GPU it can be very efficiently used as inputs and outputs by the GPU.
Note that in WebGL, there are two types of buffers:
- "element" buffers. These can only store vertex attributes with indices (a.k.a "elements") and can only be used by binding them to the
GL.ELEMENT_ARRAY_BUFFERbefore draw calls. - "generic" buffers. These can be used interchangeably to store different types of data, including (non-index) vertex attributes.
For more on the
GL.ELEMENT_ARRAY_BUFFERrestrictions in WebGL, see WebGL1 and WebGL2.