Skip to content
tezvyn:

WebGL Buffers: Telling the GPU How to Read Your Data

Source: developer.mozilla.orgMediumHow cards are made

WebGL Buffers: Telling the GPU How to Read Your Data

WebGL's vertexAttribPointer is the map you give the GPU to read vertex data from a buffer. It tells your shader how to parse a flat byte array into attributes like position. This is how you link CPU data to the GPU before drawing.

Why it exists

The GPU operates on highly optimized, contiguous blocks of memory called buffers. Your application code has structured data, like arrays of vectors. WebGL needs a way to bridge this gap, to tell the GPU's parallel processors how to interpret a raw byte stream as structured per-vertex data for your shaders.

The mental model

Think of a WebGL buffer as a long, unmarked tape of binary data. The vertexAttribPointer function is the set of instructions you give the GPU to read it. It says, "To find the 'position' attribute for each vertex, start at byte offset, read size numbers of type, and know that the data for the next vertex's 'position' is stride bytes away." You are defining the memory layout for the GPU.

How it works

Before you can draw, you upload your vertex data (positions, colors, etc.) to a WebGLBuffer. You then bind this buffer to the gl.ARRAY_BUFFER target. Next, for each attribute in your vertex shader (e.g., a_position), you enable it and call vertexAttribPointer. This call associates the currently bound buffer with that attribute and provides the layout information: the number of components per vertex (e.g., 3 for a vec3), the data type (gl.FLOAT), and the stride and offset in bytes to navigate the buffer. The GPU then uses this "map" during the draw call to fetch the correct data for each vertex.

When to use it

Use vertexAttribPointer during the initialization phase of your WebGL application, after creating and populating your buffers but before your main render loop. You are setting up the connection between your data and your shaders. You might also call it if you switch between different buffer layouts, though this is better handled with Vertex Array Objects.

When not to use it

Do not call this function in your main render loop for every frame if the data layout is static, as the setup has overhead. The modern best practice is to use WebGLVertexArrayObject (VAO) to encapsulate this state. You set up the attribute pointers once, bind them to a VAO, and then in your render loop, you only need to bind the VAO to restore the entire vertex attribute state.

One canonical example

Imagine a buffer with interleaved position (3 floats) and color (4 floats) data. To configure the 'position' attribute, you would call vertexAttribPointer(index, 3, gl.FLOAT, false, 28, 0). The stride is 28 bytes because a vertex's data is 7 floats total (3 position + 4 color), and a float is 4 bytes (7 4 = 28). The offset is 0 because position data is at the start of each vertex block. To configure the 'color' attribute, the call would be vertexAttribPointer(index, 4, gl.FLOAT, false, 28, 12). The stride is the same, but the offset is 12 bytes, skipping the 3 position floats (3 4 = 12).

Interview question

What is the fundamental purpose of "vertexAttribPointer" in WebGL's rendering pipeline?

  • a.To activate a specific vertex attribute for use in the shader program.
  • b.To instruct the GPU on how to parse raw buffer data into structured vertex attributes for shaders.Correct
  • c.To specify the primitive type (e.g., triangles, points) for the subsequent draw call.
  • d.To transfer vertex data from the CPU to the currently bound GPU buffer.
Why?

The correct answer is C because vertexAttribPointer acts as a map, telling the GPU how to interpret a flat buffer's bytes as structured vertex attributes (like position or color) for shaders. Option D describes gl.bufferData, which handles data transfer, not interpretation.

Just read this? Test yourself on what you have been reading.

Read the original → developer.mozilla.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles