Direct State AccessGL 4.5
glNamedBufferStorage
Allocates IMMUTABLE storage for a buffer — fixed size, explicit usage flags.
Signature
void glNamedBufferStorage(GLuint buffer, GLsizeiptr size, const void *data, GLbitfield flags);
How it works
Unlike glBufferData, the size can never change afterwards. In exchange you state precisely what you will do with it (flags), instead of a vague usage hint. With flags = 0 the buffer can only be modified by the GPU — perfect for static meshes.
Parameters
bufferGLuint
Buffer to allocate (created with glCreateBuffers).
sizeGLsizeiptr
Size in bytes, fixed forever.
dataconst void *
Initial contents or nullptr.
flagsGLbitfield
What the CPU may do with the buffer, combined with |. 0 = nothing.
Common values
- GL_DYNAMIC_STORAGE_BIT
- Allows glNamedBufferSubData updates.
- GL_MAP_READ_BIT / GL_MAP_WRITE_BIT
- Allows mapping for reading / writing.
- GL_MAP_PERSISTENT_BIT
- The mapping may stay open while the GPU uses the buffer.
- GL_MAP_COHERENT_BIT
- Writes become visible without explicit flushes.
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_OPERATION
- The buffer already has immutable storage.
Example
example.cpp
1glCreateBuffers(1, &ubo);
2glNamedBufferStorage(ubo, sizeof(CameraData), nullptr, GL_DYNAMIC_STORAGE_BIT);