BuffersGL 1.5

glBufferSubData

Overwrites part of an already allocated buffer.

Signature

void glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const void *data);

How it works

Unlike glBufferData, this never reallocates. It copies size bytes from data into the bound buffer, starting offset bytes from its beginning. The buffer must already be big enough.

It is the usual way to update per-frame data such as a uniform buffer with the camera matrices.

Parameters

targetGLenum

Slot whose bound buffer is updated.

offsetGLintptr

Where to start writing, in bytes from the start of the buffer.

sizeGLsizeiptr

How many bytes to write.

dataconst void *

Source data on the CPU.

Tips & pitfalls

  • offset + size must not exceed the size given to glBufferData.

  • If the GPU is still reading the buffer from a previous draw, the driver may stall until it finishes.

Errors

When the call is invalid it does nothing and sets an error, which glGetError returns:

GL_INVALID_VALUE
offset or size is negative, or offset + size is larger than the buffer.
GL_INVALID_OPERATION
No buffer bound, or the buffer is immutable without GL_DYNAMIC_STORAGE_BIT.

Example

example.cpp
1// Update the view matrix (bytes 64..127) of a camera UBO
2glBindBuffer(GL_UNIFORM_BUFFER, uboCamera);
3glBufferSubData(GL_UNIFORM_BUFFER, 64, sizeof(glm::mat4), glm::value_ptr(view));
Official Khronos documentation