Loading…
Hardware Scan-Line Triangle Rasterizer

Hardware Scan-Line Triangle Rasterizer

Hardware Scan-Line Triangle Rasterizer
Building a Hardware Scan-Line Triangle Rasterizer: From Single-Pixel Core to SIMD Warp Architecture
View Project

Synthesisable fixed-function triangle rasterizer in SystemVerilog using signed fixed-point arithmetic throughout. Two builds are included: a scalar baseline (1 pixel/cycle) and a SIMD variant (OUT_W=8, 8 pixels/cycle as a masked warp). Both share the same arithmetic core, command front end, and verification infrastructure.


What It Does

Takes a flat list of screen-space triangles with per-vertex RGB and renders them to a framebuffer through a fixed-function pipeline: edge setup, scan conversion, barycentric interpolation, Gouraud shading, and framebuffer writeback.

For 3D scenes, model, view, and perspective projection transforms run per frame in a host preprocessing step that emits projected 2D screen-space triangles to the hardware pipeline.


Architecture

Scalar datapath:

cmd_proc → edge_setup → rasterizer → frag_fifo → interp → pixel_shader → framebuffer

SIMD datapath:

cmd_proc → edge_setup → rasterizer(OUT_W=8) → warp_fifo → simd_interp → simd_pixel_shader → writeback_coalescer → framebuffer_mp

The SIMD rasterizer tests 8 horizontally adjacent pixels per cycle using a combinational generate loop over the row-start accumulators. Output is a masked warp: out_mask is 8 bits, one per lane, with lanes outside the triangle or past the row end already zeroed. framebuffer_mp has 8 independent write ports, all firing in the same clock edge.


Core Arithmetic

All arithmetic is signed fixed-point. Vertex coordinates are 16-bit signed. The SX macro sign-extends them to 32 bits before any multiply to prevent cross-product overflow on large triangles.

Edge equations. For edge $i \to j$:

$$E_{ij}(x,y) = A_{ij}\,x + B_{ij}\,y + C_{ij}$$

A pixel is inside the triangle when all three evaluations are non-negative simultaneously. $A$, $B$, $C$ are computed once at triangle load, so the scan walk advances across pixels using additions alone: stepping right adds $A$, and stepping down adds $B$.

Barycentric weights are the edge equation values themselves, forwarded directly from the coverage test without extra arithmetic stages.

Gouraud shading blends per-vertex colors weighted by the barycentric coordinates, accumulated in 48-bit intermediates, divided by area2, and clamped to [0, 255].

Winding is normalized at load time by checking area2[31] and negating all six coefficients if negative, allowing the SCAN state to apply a uniform >= 0 coverage check on every pixel.


Performance

SceneTrianglesPixelsScalar (cycles)SIMD (cycles)Speedup
Hello Triangle125,31350,6706,5807.7x
Quads863,000125,22016,3007.7x
Sunset2072,752146,73619,4267.6x

Scalar throughput is 0.50 px/cyc across all three scenes. SIMD throughput is 3.85 to 3.87 px/cyc on the larger axis-aligned triangles, approaching the 4.0 px/cyc theoretical ceiling at 50% fill efficiency for warp size 8. The sunset’s small triangles (157 to 160 pixels each) pull the average to 3.75 px/cyc due to partial-warp overhead at row ends. Rasterizer backpressure is zero across all three SIMD runs.


Scenes

Hello triangle, quads, and sunset were the primary regression targets across both builds.

Hello Triangle scalar 256x256

QuadsSunset
Quads 256x256Sunset 256x256

Photograph reconstruction using png2scene.py, which converts any image to a scene file by splitting it into an NxN cell grid and emitting two triangles per cell. At 256-cell resolution on a 256x256 framebuffer: 131,072 triangles.

6k triangles32k trianglesFinal
MJ 6kMJ 32kMJ 256x256

3D animation target: a layer-by-layer Rubik’s cube solve. 3x3 cube, 54 stickers, 6 independent face colors, 3 independently rotating layers. The solve sequence is F R' F' R U R U' R' F R U R' U' R' F' R U' R'.

Rubik’s cube solve animation


Verification

Constrained-random regression testbench (tb_gpu_top_cr.sv) with a Python golden model. 14 coverage buckets:

random_ccw   random_cw    degenerate_collin   degenerate_samept
fully_offscreen   partially_clipped   thin_sliver   full_screen
axis_aligned   near_horizontal   near_vertical   screen_corner
single_row   random_extra

Five independent checker blocks run cycle-by-cycle: cmd-proc protocol, rasterizer output bounds, FIFO push/pop integrity, pipeline drain (pixel conservation), and framebuffer write bounds.

Results against 98 triangles across all 14 buckets:

Triangles submitted    : 98
Degenerate (dropped)   : 14
Pixel count checks     : 98 pass / 0 fail
CMD-proto errors       : 0
Rast OOB pixel errors  : 0
FIFO overflow errors   : 0
FIFO underflow errors  : 0
Pipeline drain errors  : 0
FB OOB write errors    : 0
FB write-during-clear  : 0

Earlier Design Iterations

Iteration 1: per-lane FIFOs with round-robin arbitration. The first multi-lane prototype kept a single-pixel rasterizer feeding 4 per-lane FIFOs, with a free-running round-robin counter popping one lane per cycle. Under uneven triangle coverage, fuller lanes backpressured the rasterizer; when the scan stalled mid-row and resumed, queued pixel coordinates drifted out of alignment with the arbitration counter, causing out-of-order writes and diagonal gradient artifacts. This showed that lane ordering needed to travel in lockstep with pixel masks rather than through independent per-lane queues.

Hello Triangle (Iteration 1)Quads (Iteration 1)
Hello Triangle SIMD iteration 1Quads SIMD iteration 1

Iteration 2: warp batching scheduler FSM. The second iteration batched single-pixel rasterizer outputs into 8-lane warps before interpolation. This revealed two bottlenecks: a one-cycle DISPATCH-to-ACCUMULATE transition window could miss rast_done on partial tail warps, and serializing 8 lanes through a 1-pixel/cycle writeback coalescer capped downstream throughput to 1 pixel per 8 cycles whenever warps were full. That pointed directly to wide rasterization paired with multi-port framebuffer writeback.

Hello Triangle (Iteration 2)Quads (Iteration 2)
Hello Triangle SIMD iteration 2Quads SIMD iteration 2

Final SIMD architecture. Extending the rasterizer with OUT_W=8 evaluates 8 horizontally adjacent pixels in parallel per cycle, feeds a unified 16-entry warp FIFO into the SIMD interpolator and shader, and commits up to 8 valid pixels per clock through framebuffer_mp’s 8 parallel write ports.

Hello Triangle (final)Quads (final)Sunset (final)
Hello Triangle finalQuads finalSunset final