Direct State AccessGL 4.5

glCopyNamedBufferSubData

Copies bytes from one buffer to another on the GPU, by ID, without passing through the CPU.

Signature

void glCopyNamedBufferSubData(GLuint readBuffer, GLuint writeBuffer, GLintptr readOffset,
                              GLintptr writeOffset, GLsizeiptr size);

How it works

The DSA form of glCopyBufferSubData. The copy happens in GPU memory, in order with the other commands. Two common uses: growing a buffer (create a bigger one, copy the old contents, delete the old one), and moving data from a CPU-visible staging buffer into a fast GPU-only one.

Parameters

readBufferGLuint

Source buffer.

writeBufferGLuint

Destination buffer (may be the same, if the ranges do not overlap).

readOffsetGLintptr

First source byte.

writeOffsetGLintptr

First destination byte.

sizeGLsizeiptr

Bytes to copy.

Tips & pitfalls

  • Direct State Access (GL 4.5): the object is passed as a parameter, so nothing needs to be bound and no global state is disturbed.

Errors

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

GL_INVALID_VALUE
A range goes outside its buffer, or the ranges overlap in the same buffer.
GL_INVALID_OPERATION
Either buffer is mapped (without the persistent flag).

Example

example.cpp
1// grow a buffer: old contents into a bigger one
2GLuint bigger;
3glCreateBuffers(1, &bigger);
4glNamedBufferStorage(bigger, newSize, nullptr, GL_DYNAMIC_STORAGE_BIT);
5glCopyNamedBufferSubData(old, bigger, 0, 0, oldSize);
6glDeleteBuffers(1, &old);
Official Khronos documentation