Compute shader

On this page 7

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.

ConceptTypical value or formWhat it controls
DispatchDispatch(x, y, z) groupsTotal amount of work launched
Thread group64 to 1,024 threadsUnit that shares memory and barriers
Group-shared memory16 to 64 KBFast scratch space for cooperation
Warp or wavefront32 or 64 lanesHardware batch executed in lockstep
Thread IDThree-component indexWhich data element a thread handles
Core execution concepts in compute shaders

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 stageInvoked forOutputTypical use
Vertex shaderEach vertexClip-space position and attributesTransforming geometry
Fragment shaderEach rasterized sampleColor and depthLighting and texturing
Compute shaderEach thread in a dispatchAny buffer or textureSimulation, culling, image processing
Comparison of shader stages

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.

WorkloadData patternBenefit of compute
Particle simulationMillions of independent elementsUpdates stay on the GPU with no readback
Light cullingScreen tiles against light listsShared memory caches per-tile lists
Object cullingBounding volumes against the frustumFeeds indirect draws directly
Post-processingNeighborhood image filtersShared memory reduces texture reads
Mesh skinningVertices weighted by bonesResults reused by several render passes
Typical workloads and why compute suits them

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.

APIShading languageCompute entry point
Direct3D 11 and 12HLSLDispatch and DispatchIndirect
VulkanGLSL or HLSL compiled to SPIR-VvkCmdDispatch
MetalMetal Shading LanguagedispatchThreadgroups
OpenGL 4.3 and laterGLSLglDispatchCompute
WebGPUWGSLdispatchWorkgroups
Compute shader support across graphics APIs

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.