A compute shader is a GPU program that performs general-purpose parallel computation outside the fixed stages of the rasterization pipeline. In Real-time 3D graphics, compute shaders handle work such as particle simulation, culling, post-processing and skinning, reading and writing arbitrary buffers and textures instead of producing pixels for a render target.
Unlike a vertex shader or a fragment shader, a compute shader has no implied input or output: the programmer decides how many threads run, which data each thread reads and where results go. This freedom makes compute shaders the main tool for moving CPU-side workloads onto the GPU in modern engines built on Direct3D 12, Vulkan, Metal and WebGPU.
How a compute shader executes on the GPU
A compute shader runs as a grid of thread groups launched by a single dispatch call. The application specifies how many groups to launch in three dimensions, and the shader declares how many threads each group contains, for example 64 or 256 threads per group. Every thread receives built-in identifiers that tell it its position in the grid, which it uses to select the data element it processes.
Threads within one group run on the same streaming multiprocessor or compute unit and can share a small, fast group-shared memory, typically 16 to 64 KB per group. They can also synchronize with barriers, so one phase of an algorithm finishes before the next begins. Threads in different groups cannot synchronize directly within a dispatch; coordination between groups requires atomic operations or a second dispatch.
The hardware executes threads in lockstep batches called warps (32 threads on NVIDIA hardware) or wavefronts (32 or 64 threads on AMD hardware). Choosing a group size that is a multiple of the warp width avoids idle lanes and is one of the first performance decisions a developer makes.
| Concept | Typical value or form | What it controls |
|---|---|---|
| Dispatch | Dispatch(x, y, z) groups | Total amount of work launched |
| Thread group | 64 to 1,024 threads | Unit that shares memory and barriers |
| Group-shared memory | 16 to 64 KB | Fast scratch space for cooperation |
| Warp or wavefront | 32 or 64 lanes | Hardware batch executed in lockstep |
| Thread ID | Three-component index | Which data element a thread handles |
Compute shaders and the graphics pipeline stages
Compute shaders differ from graphics shaders mainly in what defines their inputs and outputs. A vertex shader receives one vertex and emits one transformed vertex; a fragment shader receives one rasterized sample and emits a color. A compute shader receives only an index and accesses unordered access views or storage buffers directly, which makes it suitable for any data-parallel algorithm.
The following table compares the three most common shader stages in real-time rendering.
| Shader stage | Invoked for | Output | Typical use |
|---|---|---|---|
| Vertex shader | Each vertex | Clip-space position and attributes | Transforming geometry |
| Fragment shader | Each rasterized sample | Color and depth | Lighting and texturing |
| Compute shader | Each thread in a dispatch | Any buffer or texture | Simulation, culling, image processing |
Because compute work bypasses the rasterizer, it does not benefit from fixed-function hardware such as depth testing, blending or interpolation. Developers therefore use compute shaders when the algorithm does not map naturally to triangles and pixels, and keep classic stages where the hardware already accelerates the task.
Common compute shader workloads in real-time engines
Modern engines run a large share of each frame in compute. GPU-driven rendering uses compute passes to perform frustum culling and occlusion culling on thousands of objects, then writes indirect draw arguments so the GPU issues its own draw calls without CPU involvement. This reduces draw call overhead dramatically in dense scenes.
Other frequent tasks include tiled and clustered light culling for deferred shading and forward rendering, generating mipmaps, screen space ambient occlusion, temporal anti-aliasing, bloom, histogram-based auto exposure, cloth and particle physics, GPU skinning of animated characters and building acceleration structures for ray tracing.
| Workload | Data pattern | Benefit of compute |
|---|---|---|
| Particle simulation | Millions of independent elements | Updates stay on the GPU with no readback |
| Light culling | Screen tiles against light lists | Shared memory caches per-tile lists |
| Object culling | Bounding volumes against the frustum | Feeds indirect draws directly |
| Post-processing | Neighborhood image filters | Shared memory reduces texture reads |
| Mesh skinning | Vertices weighted by bones | Results reused by several render passes |
APIs and languages for writing compute shaders
Every major graphics API exposes a compute stage, and the concepts transfer between them almost unchanged. The names of dispatch functions, memory qualifiers and synchronization primitives differ, but the model of groups, threads and shared memory is the same everywhere.
| API | Shading language | Compute entry point |
|---|---|---|
| Direct3D 11 and 12 | HLSL | Dispatch and DispatchIndirect |
| Vulkan | GLSL or HLSL compiled to SPIR-V | vkCmdDispatch |
| Metal | Metal Shading Language | dispatchThreadgroups |
| OpenGL 4.3 and later | GLSL | glDispatchCompute |
| WebGPU | WGSL | dispatchWorkgroups |
WebGL 2 has no compute stage, so browser-based engines such as Three.js and Babylon.js rely on WebGPU for compute work and fall back to fragment-shader tricks on older platforms. General-purpose frameworks such as CUDA and OpenCL use the same execution model but are not part of the graphics pipeline and require explicit interoperation with rendering resources.
Performance limits and common mistakes
Compute shaders are fast only when the workload has enough parallelism and memory access is coalesced, meaning neighboring threads read neighboring addresses. Scattered reads, heavy branching and excessive atomics are the most common reasons a compute pass runs slower than expected.
- Divergent branches inside a warp force both paths to execute, wasting lanes.
- Too much shared memory per group lowers occupancy, so fewer groups hide memory latency.
- Missing resource barriers between dispatches cause race conditions and flickering artifacts.
- CPU readback of results stalls the pipeline; keep data on the GPU whenever possible.
- Tiny dispatches cannot fill the GPU, so batching small jobs together usually helps.
Profiling tools such as RenderDoc, PIX and vendor profilers show occupancy, memory throughput and stalls. A sound practice is to measure before optimizing, because the bottleneck is often memory bandwidth rather than arithmetic.
The simple explanation
A graphics card has thousands of small workers. Normally they draw shapes and color pixels. A compute shader lets programmers give those workers any job they like. The job just has to split into many small, similar pieces.
Think of a large office that usually fills out one kind of form. A compute shader hands the same staff a new task, such as sorting mail or checking lists. Each worker takes one item. Small teams can share notes at one desk. The result is huge amounts of work done at once, which keeps games and 3D apps smooth.
How it is used in practice
A studio building an open-world game needs a forest with around 200,000 trees and bushes. Sending each one from the CPU as a separate draw call would overload the processor and drop the frame rate well below 60 frames per second.
The rendering team writes a culling compute shader that runs every frame. Each thread tests one object’s bounding sphere against the camera frustum and a low-resolution depth pyramid from the previous frame. Visible objects are appended to a buffer, and the shader writes indirect draw arguments for each mesh type. The CPU then issues a handful of indirect draw calls.
The pass takes under half a millisecond on a mid-range GPU and removes most hidden vegetation before any vertex is processed. The result is a stable 60 frames per second in dense forest scenes, with the CPU freed for gameplay logic and streaming.