2017-05-15 09:36:58 -07:00

7.2 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
delete Destroys buffer
initialize Allocates and optionally initializes the buffer object's data store on GPU.
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 underlying WebGLBuffer

Methods

Constructor

Creates a new WebGLBuffer. Also, for all properties set to a buffer, these properties are remembered so they're optional for later calls.

Note: Specify target: GL.ELEMENT_ARRAY_BUFFER To create an element buffer. Otherwise it will be a generic buffer that can be used in a variety of situations.

const buffer = new Buffer(gl, options);
  1. gl - WebGLContext
  2. options - (object) An object with options/data described below:
  • target - (enum, optional) The type of the buffer. Specify GL.ELEMENT_ARRAY_BUFFER To create an element buffer. Default is GL.ARRAY_BUFFER which indicates a generic buffer
  • size - (numer, optional) The size of the components in the buffer. Default is 1.
  • type - (enum, optional) The type of the data being stored in the buffer. Default's gl.FLOAT.
  • stride - (number, optional) The stride parameter when calling gl.vertexAttribPointer. Default 0.
  • offset - (number, optional) The offset parameter when calling gl.vertexAttribPointer. Default 0.
  • usage - (enum, optional) The access pattern used when setting the gl.bufferData. Default's gl.STATIC_DRAW.

setDataLayout

Stores the layout of data with the buffer which makes it easy to e.g. set it as an attribute later.

Params:

  • layout
  • type
  • size = 1
  • offset = 0
  • stride = 0
  • normalized = false
  • integer = false
  • instanced = 0

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.setData({data, bytes, usage=, dataType=, size=})

  • data (ArrayBufferView) - contents
  • bytes (GLsizeiptr) - the size of the buffer object's data store.
  • usage=GL.STATIC_DRAW (GLenum) - Allocation hint for GPU driver.
  • dataType=GL.FLOAT (GLenum) - type of data stored in buffer
  • size=1 (GLuint) - number of values per vertex

Returns {Buffer} Returns itself for chaining.

subData

Updates part of a buffer's allocated memory.

Buffer.subData({data, offset=, srcOffset=, length})

Params

  • data (ArrayBufferView) - length is inferred unless provided
  • offset=0 - Offset into buffer
  • srcOffset=0 - WebGL2: Offset into srcData
  • length - WebGL2: Number of bytes to be copied

Returns

  • Buffer - Returns itself for chaining.

copySubData (WEBGL2)

Copies part of the data of another buffer into this buffer

Buffer.copySubData({sourceBuffer, readOffset=, writeOffset=, size})

Params:

  • sourceBuffer
  • 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) - bytes specifying the size of the data to be copied

Remarks:

  • readOffset, writeOffset and size must all be greater than or equal to zero.
  • readOffset + sizereadOffset + size must not exceeed the size of the source buffer object
  • writeOffset + sizewriteOffset + size must 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.

getSubData (WEBGL2)

Reads data from buffer into an ArrayBuffer or SharedArrayBuffer.

Buffer.getSubData({dstData, srcByteOffset, srcOffset, length})

  • srcByteOffset=0 (GLintptr) - byte offset from which to start reading from the buffer.
  • dstData=null (ArrayBufferView | ArrayBuffer | SharedArrayBuffer) - memory to which to write the buffer data.
  • srcOffset=0 (GLuint) - element index offset where to start reading the buffer.
  • length=0 (GLuint) Optional, defaulting to 0.

Returns a buffer (provided or allocated)

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_BUFFER before 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_BUFFER restrictions in WebGL, see WebGL1 and WebGL2.