Tile-based analytic-coverage vector path renderer (flatten, cell walk, backdrop, ordered compositing)
/app/cpu/main.cpp is the source of a multicore CPU program (C++17, OpenMP).
The program takes one input-file path (PROGRAM INPUT_FILE) and writes its result to stdout.
What the program computes
A tile-based 2D vector-graphics renderer (the kind of pipeline used by GPU font / SVG / UI
renderers) draws an animated scene of P filled paths on a W x H canvas for F frames.
Every frame goes through the full pipeline below; all arithmetic is integer and the CPU source is
the reference for every detail (rounding, clipping, tie rules).
- Scene (generated once from
seed, seegen_scene): each path is a glyph-like shape (75%, radius 3-24 px), a blob (23%, 24-160 px) or a giant (2%, 160-700 px) with 3-16 cubic Bezier segments per contour, optionally a hole contour (reversed or same orientation), a fill rule (nonzero or even-odd), an RGB colour and an opacity. Coordinates are 24.8 fixed point. Centres may lie up to W/8 (H/8) outside the canvas. - Transform: in frame
fpathpis rotated by one of 16 tabulated angles ((rot0 + rspd*f) & 15,costableCOS16,>> 8arithmetic shifts) about its centre and translated byf * (vx, vy). - Flatten: every cubic gets
n= the smallest integer in[1, 48]withn^2 >= ceil(3*dd/128)(or 48), whereddis the largest absolute component of the two second differences of the control polygon; then+1points att = i/nare evaluated exactly in Bernstein form and rounded (floor((sum + n^3/2) / n^3)). Consecutive points form lines. - Cell walk: each line is split at pixel-row boundaries and then at pixel-column boundaries
(crossing coordinates use floor division, see
walk_line/row_piece), producing accumulation-rasterizer cell records(row, col, cover, area)withcover = signed dy(24.8) andarea = cover * (fx_entry + fx_exit). Rows outside[0, H)and cells right of the canvas are dropped; everything left of column 0 is lumped into a per-row gutter cell (cover only). Records of the same path and cell are merged (summed). - Tiling + backdrop: cells are grouped into 16x16 tiles. Along each tile row of a path the
per-pixel-row backdrop (sum of
coverof all cells to the left, including the gutter) is carried across tiles. Tiles touched by cells are edge instances; untouched tiles in a gap whose incoming backdrop gives non-zero coverage in at least one row are solid instances. - Coverage: for a pixel,
A2 = (backdrop + covers left of it in the row + own cover)*512 - own area. Nonzero:a = min(|A2|, 131072); even-odd:a = |A2| mod 262144, folded to262144 - aabove 131072.alpha8 = (a*255 + 65536) >> 17. - Compositing: tiles start from a fixed background gradient; the instances of a tile are
applied in increasing path index (z order) with
a = (alpha8*opacity + 127)/255anddst = (src*a + dst*(255-a) + 127)/255per channel. Only pixels inside the canvas count.
Input format
One line: W H F P seed (canvas width and height in pixels, number of frames, number of paths,
64-bit seed).
Output format
One line per frame:
frame f lines L cells C edge E solid S maxdepth D covered N invisible Z maxarea A@p areahash X img Y rgb R G B
L = flattened line segments, C = merged cell records (including gutter records), E/S =
edge/solid tile instances, D = largest number of instances on one tile, N = canvas pixels with
alpha8 > 0 for at least one path, Z = paths with zero total coverage, A@p = the largest
per-path coverage sum (sum of alpha8 over the path's canvas pixels) and the lowest path index
attaining it (0@-1 if all are zero), X = sum over paths of mix64(area*0x9E3779B97F4A7C15 + p),
Y = sum over canvas pixels of mix64((y*W+x)*0xD1B54A32D192ED03 ^ rgb) (all sums mod 2^64),
R G B = channel sums. A final total ... line aggregates all frames (see source).
Hidden inputs
Correctness inputs (15 files): canvases from 1x1 up to 512x400 (including sizes that are not
multiples of 16), P from 0 to 1500, F from 1 to 4. The timing input is 4096 4096 F P seed
with P in [36000, 44000] and F = 28 at scale 1.0. Valid ranges: 1 <= W, H <= 8000,
0 <= P < 2^20, F <= 64.
Your task
Port this program to CUDA in /app/gpu/main.cu: same command line (PROGRAM INPUT_FILE),
same output on stdout. Then make it as fast as you can on the GPU while keeping the output correct.
The main computation must execute on the GPU. Renaming CPU code to .cu, running a
CPU fallback, or launching a dummy kernel while computing the answer on the CPU does not satisfy this task.
The CPU source is the specification: when this text and the code disagree, the code wins.
This sandbox is for writing the source only: it has g++ and the CPU program, but no CUDA
compiler and no GPU. Compilation, execution and verification happen later on an H100 in a
separate verifier, and a port that does not compile is rejected, so check your logic against
the CPU program before you finish (for example by emulating your kernels on the host).
How your port is verified
- Only
/app/gpu/main.cuis collected. It is compiled withnvcc -O3 -arch=sm_90 -std=c++17 -o port /app/gpu/main.cu; no extra libraries are linked. The CUDA runtime and the header-only libraries that ship with the CUDA toolkit (CUB, Thrust, libcu++) are allowed. - Your source must not contain any of:
system(,popen(,fork(,exec*,dlopen(,getenv(,#include <filesystem>,/tests/,/proc/. The program may read only the input file it is given. - Verification runs on one H100 in a fresh container without network. Hidden inputs are generated with a fresh random seed at verification time.
- Correctness: every hidden input (small edge cases plus a large timing input) must pass. Your output must be identical to the CPU program's output (trailing whitespace ignored). Any mismatch, crash or timeout is rejected.
- GPU execution is checked: a port that computes its answer on the CPU is rejected even when its output is correct.
- Speed: The timing input renders a 4096x4096 canvas with 36k-44k paths (2% of them giant) for 28 frames; one frame has about 3M lines, 9M merged cells and 1.8M tile instances.
/app/inputs/timing_example.inhas the shape and size of the hidden timing input. Your port is timed against a reference GPU port on it; the faster your port, the higher its score. /app/inputs/holds public input examples for understanding the input format.