Medium Coding adventures

Software rasterizer: one triangle

What a GPU does for every triangle: decide which pixels are inside it with three edge functions, and blend the corner colors with barycentric weights.

The target picture for the shader challenge "Software rasterizer: one triangle"
Recreate this picture. You pass at a 99% match, then try to write it in fewer characters than the par.

About the Software rasterizer: one triangle shader

What a GPU does for every triangle: decide which pixels are inside it with three edge functions, and blend the corner colors with barycentric weights. Experiments in the spirit of well-known open-source graphics projects: a software rasterizer, a ray tracer, procedural planets, fluid particles and more, rebuilt as pixel shaders.

How this challenge works

You write a fragment shader that draws the picture above, live in your browser. A match bar shows how close you are, and the score combines accuracy, code size (the par is 656 characters in Unity (Built-in)) and GPU speed. Work in HLSL, GLSL, WGSL, Metal or Godot and export the result when you are done.

What it is built from

Space
p = (uv - 0.5) * float2(aspect, 1.0)
Corners
A = (-?, -?) red, B = (?, -?) green, C = (0.0, ?) blue
Edge function
edge(a, b, p) = (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x): positive on one side of the line a-b, negative on the other (in the Toolbox)
Weights
area = edge(A, B, C); wA = edge(B, C, p) / area; wB = edge(C, A, p) / area; wC = edge(A, B, p) / area; they add up to 1
Inside
m = min(wA, min(wB, wC)): positive inside the triangle. Soft edge: smoothstep(-px, px, m) with px = fwidth(m)
Colors
background float3(0.1, 0.1, 0.12), A float3(0.95, 0.2, 0.2), B float3(0.2, 0.85, 0.3), C float3(0.25, 0.4, 0.95); color = A * wA + B * wB + C * wC

The exact numbers are left out (shown as ?), but every colour is given. Measure the rest with the pixel readout on both pictures and Diff.

How to make a software rasterizer: one triangle shader

  1. Each weight is the area of the sub-triangle opposite a corner, divided by the whole area. That is exactly what edge() computes.
  2. A point is inside the triangle if all three weights are positive, so look at the smallest one.
  3. The same weights blend the corner colors: that is how a GPU interpolates anything across a triangle.

Techniques used

  • smoothstep: turns a number into a soft 0-to-1 transition, used for soft edges and masks.
  • Anti-aliasing with fwidth: edges that are exactly one pixel soft at any size.
  • lerp (mix): blend between two colors or values.

Software rasterizer: one triangle shader code, explained step by step

Try to build it yourself first, because that is how it sticks. When you want to check your work or you are stuck, this is a complete working solution cut into parts, with the idea behind each part. The parts are in HLSL (Unity and Unreal); the full code below comes in every language.

Part 1: Helper: edge()

float edge(float2 a, float2 b, float2 p)
{
    return (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
}
  • The edge function: it tells which side of the line a-b a point is on, and its size is the area of the triangle a, b, p. Divided by the whole triangle, it gives barycentric weights.

Part 2: Set up the coordinates

Inside float4 frag(float2 uv)

float2 p = (uv - 0.5) * float2(aspect, 1.0);
float2 A = float2(-0.35, -0.3);
float2 B = float2(0.35, -0.3);
  • Centre the coordinates on the middle of the picture and multiply x by the aspect ratio, so a circle stays round on a wide picture.
  • Subtracting 0.5 moves the origin from the bottom-left corner to the middle of the picture.

Part 3: Build the pattern

Still inside frag()

float2 C = float2(0.0, 0.4);
float area = edge(A, B, C);
float wA = edge(B, C, p) / area;
  • Each weight is an edge-function result divided by the whole triangle area: how much each corner counts at this pixel. The weights add up to 1 inside the triangle.

Part 4: Build the pattern (continued)

Still inside frag()

float wB = edge(C, A, p) / area;
float wC = edge(A, B, p) / area;
float m = min(wA, min(wB, wC));
  • Each weight is an edge-function result divided by the whole triangle area: how much each corner counts at this pixel. The weights add up to 1 inside the triangle.
  • max and min pick the larger or smaller of two values. They are how shapes are combined or clamped.

Part 5: Choose the colours

Still inside frag()

float px = fwidth(m);
float inside = smoothstep(-px, px, m);
float3 tri = float3(0.95, 0.2, 0.2) * wA + float3(0.2, 0.85, 0.3) * wB + float3(0.25, 0.4, 0.95) * wC;
  • fwidth(x) is how much x changes between neighbouring pixels, so a smoothstep over that width gives an edge exactly one pixel soft.
  • smoothstep(a, b, x) turns x into a smooth 0-to-1 value between a and b. It is the soft edge or mask.
  • float3(r, g, b) is a colour: each channel from 0 (none) to 1 (full).

Part 6: Colour the pixel and return it

Still inside frag()

return float4(lerp(float3(0.1, 0.1, 0.12), tri, inside), 1.0);
  • lerp(a, b, t) blends from a to b. With a mask as t it paints one colour over another.
  • float3(r, g, b) is a colour: each channel from 0 (none) to 1 (full).
  • The shader returns the final colour: red, green, blue, and an alpha of 1 (fully opaque).

The full code

HLSL Unity, Unreal
float edge(float2 a, float2 b, float2 p)
{
    return (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
}

float4 frag(float2 uv)
{
    float2 p = (uv - 0.5) * float2(aspect, 1.0);
    float2 A = float2(-0.35, -0.3);
    float2 B = float2(0.35, -0.3);
    float2 C = float2(0.0, 0.4);
    float area = edge(A, B, C);
    float wA = edge(B, C, p) / area;
    float wB = edge(C, A, p) / area;
    float wC = edge(A, B, p) / area;
    float m = min(wA, min(wB, wC));
    float px = fwidth(m);
    float inside = smoothstep(-px, px, m);
    float3 tri = float3(0.95, 0.2, 0.2) * wA + float3(0.2, 0.85, 0.3) * wB + float3(0.25, 0.4, 0.95) * wC;
    return float4(lerp(float3(0.1, 0.1, 0.12), tri, inside), 1.0);
}
GLSL Shadertoy, three.js, OpenGL
float edge(vec2 a, vec2 b, vec2 p)
{
    return (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
}

vec4 frag(vec2 uv)
{
    vec2 p = (uv - 0.5) * vec2(aspect, 1.0);
    vec2 A = vec2(-0.35, -0.3);
    vec2 B = vec2(0.35, -0.3);
    vec2 C = vec2(0.0, 0.4);
    float area = edge(A, B, C);
    float wA = edge(B, C, p) / area;
    float wB = edge(C, A, p) / area;
    float wC = edge(A, B, p) / area;
    float m = min(wA, min(wB, wC));
    float px = fwidth(m);
    float inside = smoothstep(-px, px, m);
    vec3 tri = vec3(0.95, 0.2, 0.2) * wA + vec3(0.2, 0.85, 0.3) * wB + vec3(0.25, 0.4, 0.95) * wC;
    return vec4(mix(vec3(0.1, 0.1, 0.12), tri, inside), 1.0);
}
WGSL WebGPU, Bevy
fn edge(a: vec2f, b: vec2f, p: vec2f) -> f32
{
    return (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
}

fn frag(uv: vec2f) -> vec4f
{
    let p = (uv - 0.5) * vec2f(aspect, 1.0);
    let A = vec2f(-0.35, -0.3);
    let B = vec2f(0.35, -0.3);
    let C = vec2f(0.0, 0.4);
    let area = edge(A, B, C);
    let wA = edge(B, C, p) / area;
    let wB = edge(C, A, p) / area;
    let wC = edge(A, B, p) / area;
    let m = min(wA, min(wB, wC));
    let px = fwidth(m);
    let inside = smoothstep(-px, px, m);
    let tri = vec3f(0.95, 0.2, 0.2) * wA + vec3f(0.2, 0.85, 0.3) * wB + vec3f(0.25, 0.4, 0.95) * wC;
    return vec4f(mix(vec3f(0.1, 0.1, 0.12), tri, inside), 1.0);
}
Godot Godot 4 shading language
shader_type canvas_item;

float edge(vec2 a, vec2 b, vec2 p)
{
    return (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
}

vec4 frag(vec2 uv)
{
    vec2 p = (uv - 0.5) * vec2(aspect, 1.0);
    vec2 A = vec2(-0.35, -0.3);
    vec2 B = vec2(0.35, -0.3);
    vec2 C = vec2(0.0, 0.4);
    float area = edge(A, B, C);
    float wA = edge(B, C, p) / area;
    float wB = edge(C, A, p) / area;
    float wC = edge(A, B, p) / area;
    float m = min(wA, min(wB, wC));
    float px = fwidth(m);
    float inside = smoothstep(-px, px, m);
    vec3 tri = vec3(0.95, 0.2, 0.2) * wA + vec3(0.2, 0.85, 0.3) * wB + vec3(0.25, 0.4, 0.95) * wC;
    return vec4(mix(vec3(0.1, 0.1, 0.12), tri, inside), 1.0);
}

void fragment()
{
    // Godot's UV starts at the top-left, the lessons use bottom-left
    COLOR = frag(vec2(UV.x, 1.0 - UV.y));
}
Metal iOS, macOS
float edge(float2 a, float2 b, float2 p)
{
    return (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
}

float4 frag(float2 uv)
{
    float2 p = (uv - 0.5) * float2(aspect, 1.0);
    float2 A = float2(-0.35, -0.3);
    float2 B = float2(0.35, -0.3);
    float2 C = float2(0.0, 0.4);
    float area = edge(A, B, C);
    float wA = edge(B, C, p) / area;
    float wB = edge(C, A, p) / area;
    float wC = edge(A, B, p) / area;
    float m = min(wA, min(wB, wC));
    float px = fwidth(m);
    float inside = smoothstep(-px, px, m);
    float3 tri = float3(0.95, 0.2, 0.2) * wA + float3(0.2, 0.85, 0.3) * wB + float3(0.25, 0.4, 0.95) * wC;
    return float4(mix(float3(0.1, 0.1, 0.12), tri, inside), 1.0);
}

Open it in the editor and change it

Helper functions provided

The toolbox has: edge(). Add them above your frag function with one click.

Solve it in your language

Unity (Built-in), Unity (URP), Unreal, Shadertoy / WebGL, OpenGL (LWJGL, raylib), three.js, GameMaker, LÖVE (Lua), Godot, Apple (iOS, macOS), WebGPU / Bevy.

Start coding

Questions about this shader

How do I make a software rasterizer: one triangle shader?

What a GPU does for every triangle: decide which pixels are inside it with three edge functions, and blend the corner colors with barycentric weights. A good first step: each weight is the area of the sub-triangle opposite a corner, divided by the whole area. That is exactly what edge() computes. In ShaderBattle you build it in the browser with a live match bar, so you see how close you are after every change.

What techniques does the software rasterizer: one triangle shader use?

It is built from smoothstep, anti-aliasing with fwidth, lerp (mix). Each one is a small idea you can reuse in other effects.

Does this software rasterizer: one triangle shader work in Unity, Unreal, Godot and Shadertoy?

Yes. The idea is the same in every shader language. ShaderBattle shows the code in HLSL (Unity, Unreal), GLSL (Shadertoy, three.js, OpenGL), WGSL (WebGPU), Metal and Godot, and exports a ready-to-paste version.