On this page
The Render Pipeline
A shader is not a program you run. It is a small function the GPU calls, once per vertex or once per pixel, in the middle of a larger machine. To understand what your code can and cannot do, you first have to see the machine it lives inside, and how data moves through it.
// The two stages you write. Everything between and after them is fixed.
Varyings Vert(Attributes IN); // runs once per vertex
half4 Frag(Varyings IN) : SV_Target; // runs once per fragment
What a pipeline is¶
A pipeline is a fixed sequence of stages, each one doing one job, that together turn a triangle into pixels on your screen. The exact names and details differ from engine to engine, but the basic shape is the same everywhere, because it follows from how GPUs are built. Knowing that shape is what separates someone who can read any rendering API from someone who is memorizing one engine’s functions.
The stages, in order¶
-
Application, on the CPU. Your game is running here: input, physics, animation, AI, and the culling that decides which objects are even visible. The CPU assembles the vertex data and the render state and sends a draw call to the GPU. Nothing is colored yet; this is where work is defined.
-
Vertex stage, on the GPU. This is yours. Your vertex shader runs once per vertex. It moves each vertex through its spaces, from object to world to view to clip, and it computes any extra values you want to carry forward. It is the only place in the whole pipeline where you can move geometry, which is why everything in the vertex animation section of this library happens here.
-
Rasterizer, on the GPU. Fixed. The GPU turns the transformed triangles into pixels, called fragments, and it interpolates every value you passed from the vertex stage, smoothly across the inside of each triangle. You cannot write a shader for this stage, and you cannot change how it blends. But you choose what data flows through it, and that choice shapes the whole result.
-
Fragment stage, on the GPU. This is yours. Your fragment shader runs once per fragment and computes the final color: lighting, textures, procedural patterns, effects. All of the “shading” in shader programming lives in this one stage.
-
Depth test and blending, on the GPU. Fixed. Each finished fragment asks the depth buffer, “am I in front of whatever is already there?” and, if the material is transparent, it blends into the color buffer. This is how the GPU avoids drawing the back of a box over its front.
-
Screen. What survives is written to the framebuffer and composited into the frame you see.
The useful mental model¶
Read the list above and a pattern appears. You write exactly two stages, the vertex stage and the fragment stage. The CPU stage belongs to your game code, and the rasterizer, depth, and blend stages are fixed hardware you cannot edit. Your entire job as a shader author is therefore two things: move the data you need from the vertex stage into the fragment stage, and compute a color in the fragment stage. The rasterizer in between is a partner you work with, not against, because its interpolation is what turns per-vertex data into per-pixel detail.
Everything else in this library is a variation on that. A lighting model is a different fragment stage. A normal map is data you move and then apply in the fragment stage. A shadow is a value the pipeline computes for you that you read in the fragment stage. Once the shape is clear, the individual techniques stop feeling like magic and start feeling like choices.
What the shader does¶
The shader makes the quiet middle stage, the rasterizer, visible. In Interpolated mode the vertex stage writes the object position into a varying, so the fragment stage shows a smooth gradient blending across each triangle. In Lit mode the same values feed the lighting pass, which is where the two stages you control, vertex and fragment, hand off through the fixed stage between them.
Illustrative schematic: The two stages you control, with the fixed rasterizer between them.
The same mesh, two views of the pipeline. In interpolated mode the fragment colors by the blended object position, which is the rasterizer doing its work. In lit mode the fragment applies a normal, and you see the vertex stage and the fragment stage cooperating.
Shader "DecompiledArt/Foundations/RenderPipeline/RenderPipeline"
{
Properties
{
[Enum(Interpolated,0,Lit,1)] _Mode("View", Float) = 1
[MainColor] _BaseColor("Base Color", Color) = (0.5, 0.7, 1.0, 1.0)
_Range("Position Range", Range(0.1, 4.0)) = 1.0
}
SubShader
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "Queue" = "Geometry" }
Pass
{
Name "ForwardOnly"
Tags { "LightMode" = "UniversalForward" }
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment Frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float3 normalOS : NORMAL;
};
struct Varyings
{
float4 positionCS : SV_POSITION; // clip space, for the rasterizer
float3 positionOS : TEXCOORD0; // a payload the rasterizer will interpolate
half3 normalWS : TEXCOORD1;
};
CBUFFER_START(UnityPerMaterial)
half _Mode;
half _Range;
half4 _BaseColor;
CBUFFER_END
Varyings Vert(Attributes IN)
{
Varyings OUT;
// Vertex stage: move the vertex, and pick what to carry forward.
VertexPositionInputs p = GetVertexPositionInputs(IN.positionOS);
OUT.positionCS = p.positionCS;
// The rasterizer will blend these across the triangle.
OUT.positionOS = IN.positionOS.xyz;
OUT.normalWS = TransformObjectToWorldNormal(IN.normalOS);
return OUT;
}
half4 Frag(Varyings IN) : SV_Target
{
// Fragment stage: compute the color from what arrived.
half3 normalWS = normalize(IN.normalWS);
half ndl = saturate(dot(normalWS, normalize(half3(0.3, 1.0, 0.2))));
// Interpolated object position, remapped so it reads on screen.
half3 grad = saturate(IN.positionOS / _Range * 0.5 + 0.5);
half3 col = _Mode < 0.5h
? grad
: _BaseColor.rgb * (0.35h + 0.65h * ndl) * grad;
return half4(col, _BaseColor.a);
}
ENDHLSL
}
}
FallBack Off
}
🔗 Related Chapters¶
Mesh Data • Forward vs Deferred • Coordinate Spaces
Related functions: Lerp • Normalize
❤️ Support Shader Story¶
If this article helped you, consider supporting the project on Patreon: you’ll get access to the related source files and reference cheat-sheets.
Your support helps keep this library open and free for everyone.