Skip to content
systemcoding.dev
GPU

Upload vertex data once with GL_STATIC_DRAW

Upload vertex data once with glBufferData and the GL_STATIC_DRAW usage hint, and know when GL_DYNAMIC_DRAW fits better in OpenGL 3.3 core.

1 min read
Summarize in AI

Mesh data that never changes should be uploaded one time with the GL_STATIC_DRAW hint. The hint is a promise about use, not a restriction, so the driver can place the buffer where the GPU reads fastest.

upload.cc
const float quad[] = {
    -1.f, -1.f,   1.f, -1.f,   1.f, 1.f,   -1.f, 1.f,
};

glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof quad, quad, GL_STATIC_DRAW);

GLint size = 0;
glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
printf("buffer holds %d bytes\n", size);
Text
buffer holds 32 bytes

The usage values are listed on the glBufferData page: STATIC is written once and drawn many times, DYNAMIC is rewritten often and drawn often, STREAM is rewritten almost every draw. The spec calls them hints, so a wrong one still renders correctly; it only risks a slower memory placement.

Calling glBufferData again reallocates the store. For small in-place edits use glBufferSubData instead, and for per-frame data prefer one of the dynamic hints.

Taggedcopengl
Share

More snippets

GPU

List platforms with clGetPlatformIDs

Call clGetPlatformIDs twice, once for the count and once for the handles, then print each platform name with clGetPlatformInfo in OpenCL 3.0 C.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.