A shader is a small program that runs on the GPU. HLSL (High-Level Shading Language) is Microsoft's language for writing these programs.
In this lesson, you'll learn what HLSL shaders are and how the GPU runs them.
HLSL looks a lot like C, but it's designed for a very different kind of processor.
CPU vs GPU: Why HLSL Shaders Think in Parallel
A CPU has a fairly small number of powerful cores. Each core is good at complex, branching, step-by-step work. Think of a few expert workers who can each handle anything.
A GPU has thousands of simpler cores. Each one is weaker, but they all run the same program on different data. In practice, the GPU schedules them in big batches. Think of thousands of workers, all following the same instruction sheet, each on their own item.
The Key Idea: One Shader Program, Many Copies
Suppose your screen is 1920 × 1080. That's about 2 million pixels.
A pixel shader is not a loop over all the pixels. Instead:
- You write the program for one pixel.
- The GPU runs it for every pixel the mesh covers (called a fragment), with millions of copies in parallel.
- Each copy receives its own inputs, such as its screen position or its UV coordinate.
gradient.HLSL
float4 main(float2 uv : TEXCOORD0) : SV_Target
{
return float4(uv.x, uv.y, 0.0, 1.0);
}
// runs once for every covered pixel, in parallel
You don't need to understand the syntax yet. The key point is that this code runs once for every covered pixel. Each pixel gets a different uv, so each pixel produces a different color. The result is a gradient across the screen.
The Consequences of This Model for HLSL Shaders
This "one program, many copies" idea shapes how you write HLSL shaders. Here are three rules to keep in mind.
1. Each Shader Copy Is Isolated.
A shader copy can't directly read the results its neighbors calculate. It only gets its own inputs, plus resources like textures.
2. There's No Memory Between Frames.
When the next frame is drawn, each shader copy starts fresh. It keeps none of its own values from last time. Textures and buffers on the GPU can persist, but the shader itself remembers nothing. If you want something to change over time, you pass in a value like time from outside.
3. Everything Is About "What Is the Result for This One?".
Shader thinking means asking one question: "Given only this pixel's inputs, what color should it be?"
Where HLSL Shaders Run
HLSL isn't only for pixels. There are several shader types, each running once per something:
Shader type | Runs once per… | Job |
|---|
Vertex shader | Vertex of a mesh | Positions geometry on screen |
Pixel shader | Pixel covered by a mesh | Decides the pixel's color |
Compute shader | Work item you define | General-purpose parallel work, such as particles or simulations |
In Unreal, materials are mostly pixel and vertex shaders, and Niagara GPU particles run on compute shaders. That's why this model matters for both.
The same model applies in Unity. Shader Graph and hand-written Unity shaders run as vertex and pixel shaders, and VFX Graph runs on compute shaders. Unity's hand-written shaders are even written in HLSL.
Exercises
Take your time. Answers are just below.
Exercise 1: In your own words, what's the main difference between how a CPU and a GPU approach work?
Exercise 2: A pixel shader runs on a mesh that covers a whole 1000 × 500 image. How many times does it run for one frame?
Exercise 3: A classmate wants each pixel to copy the color of the pixel to its left. Why won't this work the way they expect?
Exercise 4: You want a pixel's color to pulse over time. The shader has no memory between frames. How could it still animate?
Exercise 5: Which shader type is commonly used to simulate GPU particles, like those in Niagara or Unity's VFX Graph?
Exercise Answers:
Exercise 1. A CPU is like a few expert workers; a GPU is like thousands of simple ones. A common mistake is saying a CPU does one thing at a time. A CPU runs things in parallel too, just on a handful of cores. The real difference is that GPU cores run the same program on different data. That's what makes GPUs fast for pixels. It's also what limits them.
Exercise 2. 1000 × 500 = 500,000 runs per frame. At 60 frames per second, that's 30 million runs every second. Not bad for a tiny program!
Exercise 3. Each shader copy only gets its own inputs. It can't read the results other copies calculate in the same pass. Even the order they run in isn't guaranteed. The usual workaround is to render to a texture first, then read it in a second pass. We'll cover that later in the series.
Exercise 4. Feed in time from outside. That's the standard technique. A common pattern is sin(time), which smoothly rises and falls between -1 and 1. For a color, remap it to 0–1 with sin(time) * 0.5 + 0.5. In Unreal, the Time node gives you this value. In Unity, use the Time node in Shader Graph or _Time in HLSL.
Exercise 5. A compute shader. Often each work item handles one particle, but that mapping is your choice, not a rule.
Wrapping Up
That's the core model behind HLSL shaders: one small program, run as millions of copies.
Three things to remember:
- A GPU runs one small program as many copies at once, each on its own pixel, vertex or work item.
- Each copy sees only its own inputs. It can't read its neighbors, and it keeps nothing between frames.
- Every shader answers one question: "What is the result for this one?"
Next in the HLSL series: coming soon.