A ground-up mental model: how a 3D engine on the CPU turns a scene into commands, and how the GPU turns those commands into the colored pixels on your screen. Built general → specific. No filler.
A 3D program does not draw pixels. The CPU/engine prepares scene data and writes a to-do list of commands. The GPU reads that list and executes it — turning geometry + data into pixels. CPU = author of the list. GPU = executor of the list.
Your .exe never talks to the GPU directly. It talks downward through a stack of layers, each one more concrete and less portable than the one above it.
Before a single pixel is touched, the CPU does a lot of organizing. Think of one frame as three jobs in sequence.
Then a render prep step: find visible renderers → sort them (by render queue, material, shader pass, depth) → group by material/pass/target → batch where possible → build the actual render commands.
For each render pass, then for each batch, the engine issues a tight sequence of binds followed by a draw:
DrawIndexed / DrawInstancedBind = "make this already-existing GPU resource the active one for the next command." It's setting a pointer, not creating, copying, or moving data. Bind vertex buffer = "use this mesh's vertices next." Bind render target = "send output pixels here." The data was uploaded to GPU memory earlier; binding just points at it.
Correction to a common belief The CPU never colors a pixel. It assembles a command list. That's the whole job of this side.
These get blurred constantly. Lock them down and 80% of the confusion disappears.
| Term | What it actually is |
|---|---|
| Resource | Data living in GPU memory — a buffer or a texture. |
| State | Configuration / rules for rendering (blend mode, depth test, which shader). |
| Command | An instruction to do work now. |
| Shader | Programmable GPU code you wrote (HLSL/GLSL). |
| Pipeline stage | A worker/station along the rendering route. |
| Draw call | The command that runs the graphics pipeline once. |
| Dispatch | The command that runs the compute pipeline once. |
The split that matters most: data (resource) vs rules (state) vs action (command). Binding sets up data and rules; a draw/dispatch is the action that fires.
The CPU does not wait after every draw call. It throws commands into a queue and moves on. The GPU drains that queue on its own clock. They overlap by a frame.
ReadPixels, ComputeBuffer.GetData, or consuming an AsyncGPUReadback too early.Rule of thumb Anything that asks the GPU "are you done? give me the answer now" is a stall risk. Fire-and-forget is fast; round-trips are slow.
Not "a run of the render thread." A pass = "do this rendering, for this purpose, into this target." One pass holds one or many draws/dispatches/blits.
A better model:
| Drives count UP | Drives count DOWN |
|---|---|
| shadows, depth prepass | static/dynamic batching |
| multiple materials / submeshes | GPU instancing |
| transparent sorting | SRP Batcher (cuts CPU state-setup cost) |
| multiple cameras, XR eyes | |
| post-processing, many forward lights |
It doesn't always reduce the number of draw calls — it reduces the CPU cost of setting up state between them by keeping material data persistent on the GPU. Instancing reduces the count; the batcher reduces the per-call overhead. Different levers.
One draw call sends geometry through this fixed route. Two stages are programmable (you write the shader); the rest are fixed-function hardware you only configure.
SV_POSITION, plus interpolatable attributesfloat4(x,y,z,w)xyz by w to get NDC → maps NDC to screen pixelsClip space ≠ NDC. Clip space is still homogeneous (the w is meaningful, used for clipping). NDC is what you get after dividing by w — a tidy cube where x,y,z ∈ [-1,1] (or [0,1] for z in D3D). The perspective effect (far things shrink) happens in that divide.
Typical opaque state: ZTest LEqual, ZWrite On. Typical post-process: ZTest Always, ZWrite Off (draw unconditionally).
Opaque: Blend Off (overwrite). Alpha transparency: Blend SrcAlpha OneMinusSrcAlpha (mix by alpha).
Render target = where finished pixels live: screen backbuffer, a CameraColor texture, a RenderTexture, a G-buffer, a mask texture.
Two buffers, two jobs. The vertex buffer holds the data; the index buffer holds the recipe for connecting it into triangles.
| Count | What |
|---|---|
| 8 | geometric corner positions |
| 24 | render vertices for a hard-edged cube |
| 36 | indices (12 triangles × 3) |
A render vertex is position + normal + UV + …, not just position. At a cube corner three faces meet, and each face needs a different normal (it points a different way) and usually a different UV. Same position + different normal = a different render vertex. Three faces per corner × 8 corners = 24. The index buffer keeps the connectivity cheap (36 small ints) even though the vertices can't be shared across faces.
A cube has 6 square faces conceptually. Each square is triangulated into 2 triangles → the GPU renders 12 triangles. The GPU doesn't inherently know two triangles were once one quad unless tooling preserves that grouping.
One-liner Quads are artist-friendly; triangles are rasterizer-friendly. The GPU is built around the triangle.
Position decides where a corner lands on screen. But two more attributes ride along and are interpolated across every triangle.
| Attribute | Is | Used for |
|---|---|---|
| UV | a 2D texture coordinate | which part of a texture maps onto the surface |
| Normal | a surface direction (not a position) | lighting — how bright the surface faces a light |
Flow: each triangle has 3 vertices carrying position/UV/normal → the rasterizer interpolates UV and normal across the face → the fragment shader gets a per-fragment UV and normal → it samples the texture by UV and computes lighting with the normal.
0–1 maps to the base texture area.0–1; behavior is set by the sampler's wrap mode: Repeat, Clamp, or Mirror.brightness = saturate(dot(normal, lightDir)) — facing the light = bright, perpendicular = dark.A matrix is a machine that moves a position from one coordinate space into another. Rendering is just a relay of these conversions until a vertex lands in clip space.
| Matrix | Converts | Comes from |
|---|---|---|
| Model | Object → World | the object's transform (pos/rot/scale) |
| View | World → Camera | the camera's transform (its inverse) |
| Projection | Camera → Clip | FOV, aspect, near/far, ortho settings |
The CPU/engine builds and binds these matrices before the draw. The vertex shader applies them per vertex and outputs SV_POSITION; the GPU handles the rest of the relay.
w componentRotation and scale are linear and fit in a 3×3 matrix, but translation (moving) does not — a pure 3×3 can't add an offset. Bumping up to 4×4 with a w coordinate lets one matrix express translation and rotation/scale and perspective together. That's the whole trick.
w = 1 → translation affects them (a point can move).w = 0 → translation is ignored (a direction has no location, only orientation).Positions go through the model matrix directly. Normals are transformed as directions — and under non-uniform scale, naively applying the model matrix skews them off the surface. The fix is the inverse-transpose of the model matrix. Unity hands you UnityObjectToWorldNormal so you rarely write it by hand, but know why it exists.
The vertex shader handed three clip-space corners to the fixed-function stages, which divided by w and mapped them to screen pixels. The rasterizer now answers two questions for every pixel: is it inside the triangle? and what are its interpolated attributes? Remarkably, one piece of math answers both.
For a triangle A, B, C, every point P in the plane can be written uniquely as a weighted blend of the corners, with the weights summing to one:
(α, β, γ) are P's barycentric coordinates — how much each vertex contributes. At A they are (1,0,0), at B (0,1,0), at C (0,0,1). P is inside the triangle exactly when all three are ≥ 0. A negative weight means P is outside — that is the coverage test.
The 2D cross product cross(u,v) = u.x·v.y − u.y·v.x gives twice a triangle's signed area. With Total = cross(B−A, C−A):
This is the edge function, and it is exactly what hardware rasterizers evaluate per pixel: the sign gives the inside/outside test, the magnitude gives the interpolation weight. Coverage and weights fall out of one cheap, incremental computation.
Once you have the weights, every per-vertex attribute — uv, normal, color, anything the vertex carried — is blended with the same weights. The fragment shader then receives these interpolated values.
Affine (screen-linear) blends directly: uv = α·uvA + β·uvB + γ·uvC. Cheap, but wrong under perspective — after projection, equal screen steps are not equal surface steps, so the texture visibly slides (the PlayStation-1 wobble). It is exact only when all w are equal.
Perspective-correct blends attr/w and 1/w linearly, then divides — recovering the value that is linear along the real 3D surface:
The GPU does this automatically for all interpolators (unless you mark one noperspective).
Meaning: how far the fragment is along the view direction — written per-pixel to the depth (z) buffer and used by the depth test to resolve occlusion. Value: the post-divide NDC z (mapped to [0,1] in the buffer). Computed: NDC z is itself screen-linear over a planar triangle, so it interpolates with plain barycentric weights — no 1/w correction needed:
NDC depth is not linear in real-world distance — it is hyperbolic (1/z-like), packing most precision near the camera. That is why distant surfaces z-fight and why the near/far choice matters so much.
The rasterizer delivers, per fragment, an interpolated uv and normal. The fragment shader's job is to turn those into a final color. Two operations dominate: sample a texture with the uv, and apply lighting with the normal.
A texel is one stored color in the texture grid. Sampling maps a continuous uv ∈ [0,1] into texel space (uv × textureSize) and reads a color there. Two settings govern it.
Wrap (address) mode — what happens when uv leaves [0,1]:
| Mode | Behavior |
|---|---|
| repeat | frac(uv) — the texture tiles seamlessly. |
| clamp | uv is clamped to [0,1] — the edge texels stretch outward. |
| mirror | each successive tile is flipped — mirrored repeat. |
Filtering — what happens between texel centers:
| Mode | Behavior |
|---|---|
| nearest | pick the closest texel — blocky, pixelated. |
| bilinear | blend the 4 nearest texels by fractional position — smooth. |
For minification, GPUs add mipmaps (pre-shrunk copies) with trilinear blending, and anisotropic filtering for grazing angles.
The normal N is the direction the surface faces; L points toward the light. Their dot product is the cosine of the angle between them:
Facing the light → 1 (bright); perpendicular → 0 (dark); facing away → negative, clamped to 0. This is Lambert's cosine law. ambient is a small floor so unlit areas are not pure black; the final color is texColor × (ambient + (1−ambient)·diff).
A weighted average of two unit vectors is generally shorter than 1, so the interpolated normal is not unit-length. The fragment shader must normalize() it before use, or lighting comes out subtly wrong.
Effects like tone-mapping or bloom still ride the graphics pipeline — they just draw a single fullscreen triangle/quad and run all their logic in the fragment shader.
Cull Off · ZWrite Off · ZTest Always · Blend Off.So "blit" = draw a fullscreen quad that reads texture A and writes target B. Same pipeline, trivial geometry, all the work in the pixel shader.
Not every GPU job is about triangles. The compute pipeline runs general-purpose GPU programs over raw data.
| Graphics pipeline | Compute pipeline | |
|---|---|---|
| fired by | Draw call | Dispatch |
| has | vertex shader, rasterizer, fragment shader, depth/blend | one compute shader, run over thread groups |
| produces | pixels in a render target | data in buffers/textures |
Used for image processing, simulations, particles, GPU culling, and buffer/texture transforms — no rasterization anywhere. This is the road CUDA / TensorRT-style workloads conceptually live on.
A 3D engine prepares visible work on the CPU — culls, sorts, batches — then binds GPU resources + pipeline state and submits draw commands. The GPU runs the raster pipeline: input assembly builds triangles from vertex/index buffers, the vertex shader transforms them with MVP matrices, the rasterizer turns triangles into fragments and interpolates attributes, the fragment shader computes color, depth/blend decide visibility and composition, and the result is written to a render target. Compute dispatch is a separate road that runs compute shaders over buffers — no rasterization.
| Wrong | Right |
|---|---|
| CPU draws pixels | CPU writes commands; GPU draws pixels |
| bind = copy data | bind = point at existing data |
| 1 GameObject = 1 draw call | draws ≈ batches × materials × passes × cameras |
| clip space = NDC | NDC is clip space after ÷w |
| a cube has 8 vertices | 8 positions, 24 render vertices, 36 indices |
| render pass = a thread run | render pass = a logical stage with a target |
| SRP Batcher cuts draw count | it cuts per-call CPU state cost |