BuffersGL 1.5

glGetBufferSubData

Copies bytes from a GPU buffer back into CPU memory.

Part of the family glGet*

Signature

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

How it works

The reverse of glBufferSubData: it reads size bytes starting at offset from the bound buffer into data. Mostly used to read results produced by compute shaders or transform feedback.

The call waits until the GPU has finished every command that writes to the buffer, so it can be slow — avoid it in the middle of a frame.

Parameters

targetGLenum

Slot whose bound buffer is read.

offsetGLintptr

First byte to read.

sizeGLsizeiptr

Number of bytes to read.

datavoid *

Destination in CPU memory, at least size bytes.

Tips & pitfalls

  • After a compute shader writes to an SSBO, call glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT) before reading it back.

Errors

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

GL_INVALID_VALUE
The range is outside the buffer.

Example

example.cpp
1std::vector<float> result(count);
2glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);
3glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
4glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, count * sizeof(float), result.data());
Official Khronos documentation