Explain the roles of vertex and fragment shaders in WebGL

GPU pipeline separation between geometry transform and pixel color.
vertex shaders write gl_Position per vertex; fragment shaders write gl_FragColor per pixel.
swapping their outputs or claiming CPU/DOM access.
What's really being asked
This question probes whether you understand the fixed pipeline stages of WebGL and how the GPU parallelizes work. Interviewers want to see that you know vertex shaders handle geometry while fragment shaders handle rasterization output, and that you are aware of the built-in variables gl_Position and gl_FragColor.
The full answer
First, the vertex shader runs once per vertex and its primary responsibility is transforming vertex coordinates into clip space by writing to the built-in GLSL variable gl_Position. It typically processes attributes like vertex positions, normals, and texture coordinates, and can accept uniforms like transformation matrices. Second, the fragment shader runs once per pixel fragment and its primary responsibility is determining the final color by writing to the built-in GLSL variable gl_FragColor. It typically processes interpolated varyings from the vertex stage, plus uniforms for textures or lighting parameters. Third, mention that shaders execute on the GPU not the CPU, which offloads computation and enables massive parallelization. Fourth, note that GLSL is strongly typed and uses vectors and matrices heavily.
The mistakes people make
A major red flag is swapping the two stages, such as saying the vertex shader sets pixel colors or the fragment shader moves geometry. Another red flag is claiming shaders can directly access the DOM, JavaScript variables, or CPU memory without using the WebGL API to pass attributes, uniforms, or textures. Saying that shaders are written in JavaScript rather than GLSL is also a serious error. A subtler mistake is forgetting that fragment shaders operate on interpolated fragments, not raw vertices.
What usually comes next
The interviewer may ask how data flows from JavaScript into the shader via buffers and attributes, or how varyings interpolate across a triangle. They might ask about the difference between attributes and uniforms, or what happens after the vertex shader before the fragment shader, namely primitive assembly, rasterization, and interpolation. They may also ask how to pass a texture coordinate from vertex to fragment stage using a varying.
A concrete example
Imagine rendering a textured cube. The vertex shader takes a vec3 position attribute and a vec2 texCoord attribute, multiplies the position by a model-view-projection matrix uniform, writes the result to gl_Position, and passes texCoord to the fragment shader through a varying. The fragment shader receives the interpolated texCoord, samples a sampler2D uniform texture, and writes the sampled vec4 color to gl_FragColor. The vertex shader runs 8 times for the 8 cube corners, while the fragment shader runs once for every pixel covering the cube faces, which could be thousands of times.
Interview question
When rendering a textured mesh in WebGL, which task is performed by the fragment shader rather than the vertex shader?
- a.Sampling a texture uniform using interpolated varyings and writing the color to gl_FragColorCorrect
- b.Reading raw vertex attributes directly without interpolation to determine per-triangle colors
- c.Executing once per pixel fragment to compute clip-space coordinates for the rasterizer
- d.Transforming vertex positions into clip space with a matrix uniform and writing to gl_Position
Why? this is the answer
The fragment shader receives interpolated varyings and writes the final pixel color to gl_FragColor after sampling textures. Option C is tempting because it correctly notes the per-fragment execution frequency, but clip-space coordinates are actually computed by the vertex shader and consumed by the rasterizer before the fragment stage ever runs.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
- #webgl
- #glsl
- #shaders
- #gpu
- #rendering pipeline
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.
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