One kernel source for many GPUs
- Slang
2026.19, sources ingpu/kernels, built bytools/build_gpu_modules.py.
Every GPU family speaks its own kernel language. NVIDIA drivers load PTX, Apple devices compile the Metal Shading Language, and WebGPU runs WGSL. A decoder written once per language would need every fix made several times, and the copies would slowly drift apart.
GeoZL's decoder has exactly one kernel source, in Slang, compiled ahead of time for each backend. CUDA and Metal are the first two backends, and more are planned, WebGPU among them. The compiled kernels ship inside the library, so neither building GeoZL nor running it needs Slang.
This last part follows that source to the GPU, from one line of Slang to a module the device runs.
What Slang is
Slang is an open-source shading language, developed at NVIDIA and now hosted by Khronos. Its syntax is close to HLSL, and a single compiler, slangc, emits code for many targets, including Metal source, CUDA through PTX, SPIR-V for Vulkan and WGSL for WebGPU.
What matters most for GeoZL is that Slang has portable wave operations, a prefix sum, a sum, an OR and a read from another thread. Here is the inner step of the PivCo merge from pivco.slang, with comments added.
uint ones = countbits(bits); // 1s in my byte
uint onesBefore = carry + WavePrefixSum(ones); // 1s before me
uint zerosBefore = p0 - onesBefore;
merge_byte(bits, p0, count, leftCount, onesBefore, zerosBefore,
outputBase, toLanes);
carry += WaveActiveSum(ones); // 1s in the step
The same line becomes different code on each target. Metal has a built-in call, simd_prefix_exclusive_sum. PTX has none, so Slang builds the wave prefix sum from shfl.sync.up shuffles. The kernel source does not change.
How the kernels are organised
gpu/kernels holds three kinds of file. common/payload.slang sets what every kernel shares, a 32-thread wave, four waves per group, eight values per thread, and the loads that read the compressed payload. stages/ holds the decoding steps, PFOR, the band decoder of planar_zigzag_pfor, the PivCo merge and the inverse planar scans. programs/ holds the three programs the library loads, which pull in the stages they need.
The macro ELT_BYTES picks the sample type, so a program is compiled once per width. pfor holds the PFOR unpack, size_chunks, plan_blocks and decode_strips for planar_zigzag_pfor, and a raw copy. pivco holds the PivCo merge, the byte-lane unpack, the lane join, the three planar scans and the same raw copy. place has one kernel, which copies decoded rectangles into the caller's target. The table of programs in the build script lists them all.
Compiling a module for each target
tools/build_gpu_modules.py builds every module of one target into a directory, normally core/src/gpu/modules. These two lines rebuild both targets we ship.
python3 tools/build_gpu_modules.py slangc metal core/src/gpu/modules
python3 tools/build_gpu_modules.py slangc ptx core/src/gpu/modules
It first checks that slangc reports version 2026.19, because another compiler would write different modules. Then, for each program and width, it runs slangc with every entry point of the program, the target, ELT_BYTES set to the width, and -line-directive-mode none so no file path ends up in the output.
Without a check. The host hands buffers to a kernel by index, and Slang numbers them in the order a program declares them. Swap two declarations and the kernel reads one buffer as another, with no error at all.
With the check. Each compile also writes Slang's reflection JSON, and check_slang_layout.py compares its order with the order the host binds. A swap fails the build instead of the decode.
Metal source builds anywhere slangc runs, a Linux machine included. PTX also needs NVRTC, the CUDA runtime compiler that slangc calls to turn its CUDA output into PTX.
PTX and the oldest driver
PTX is not machine code. The CUDA driver compiles it for the GPU it finds when the module loads, so one PTX file covers every NVIDIA GPU from its target up. Ours target sm_70.
The catch is that a driver only loads PTX up to its own PTX version, and every NVRTC release writes a newer one. So the compiler is chosen by the oldest driver GeoZL should run on, not by the newest toolkit we have.
The PTX we ship comes from NVRTC 12.2, so drivers from the CUDA 12.2 generation on can load it. NVRTC does not exist for macOS, so we regenerated the PTX on a workstation with two RTX A5500 cards and CUDA 12.2. When a driver refuses a module anyway, the error GeoZL returns carries the first line of the JIT log, which names the PTX feature it rejected.
Where we skip Slang's shuffles
A shuffle is how one thread of a wave reads a value from another, without going through memory. The wave prefix sum above is built from them on CUDA, and CUDA's shuffle takes a mask of the threads that join it. Slang cannot know that every thread is still running at that point, so its CUDA code asks which ones are before each shuffle.
That question costs little once, but the planar>zigzag>pfor strip decoder shuffles about fifty times per block, and it made the decoder more than twice as slow on an A100. In that kernel all 32 threads always take part, so it calls the shuffles directly with a full mask, __shfl_up_sync on CUDA and simd_shuffle_up on Metal.
Keeping the modules in step with the source
The library loads the compiled modules kept in the repository, not the .slang sources. If someone edits a kernel and forgets to rebuild, the old module ships and nothing notices.
So each build also writes sources.sha256, a hash of every module and a hash of everything the compiler read, meaning the .slang sources, the Slang version, the target and the flags. In CI, build_gpu_modules.py --check computes the hashes again. If a source changed without a rebuild, they no longer match, and the check fails and names the stale modules.
The check only computes hashes, so it needs no Slang and no GPU and runs on every change. That matters most for PTX, which cannot be rebuilt on macOS at all.
Loading the embedded modules
At build time CMake picks one kind of module, Metal source on Apple and PTX on Linux, and embed_modules.cmake turns each into a C byte array in a table of name, bytes and size. A build without GPU support compiles a stub instead, whose calls report that GPU support is not built in.
At run time the decoder looks a module up by name the first time a batch needs it, pfor2 for two-byte PFOR frames, and hands the bytes to the backend.
The Metal runtime compiles the source with newLibraryWithSource, builds a pipeline per entry point and rejects any pipeline whose SIMD-group width is not 32. The CUDA runtime opens libcuda.so.1 with dlopen, so the library still loads on machines without CUDA, and passes the PTX to cuModuleLoadDataEx, where the driver compiles it.
On CUDA, kernels read their buffer pointers from one parameter block per module, so one program runs one batch at a time. Callers that need work on several streams at once create one decoder per stream.
Creating a decoder loads or compiles the kernels for its device, so create one decoder and reuse it.
Adding a backend
Capture, planning, task lists and staging are shared by every backend. runtime.c checks each call and forwards it through GeozlGpuBackendOps, a small table of operations that opens a device, allocates memory and staging, queues a batch of kernels and waits for it.
A new GPU family, WebGPU, Vulkan or HIP, needs one runtime file that fills in that table and compiles in under its own define, as runtime_metal.m and runtime_cuda.c do, and a backend id that selects it. build_gpu_modules.py gains its Slang target, and the CMake build embeds that kind of module.
The kernels assume a 32-wide wave. A backend whose devices can run another width, as some WebGPU and Vulkan devices do, must ask for 32 or reject the device, as the Metal runtime does today. The formats, the kernels and every caller stay as they are.
Where this leaves us
GeoZL keeps one Slang source for Metal and CUDA, then verifies the generated modules in CI. Across the CPU and GPU decoders, the common idea is to let each lane or thread find its own input.
The one place we paid for it in ratio, skewed data in the lanes format, is still open. If it gets a GPU path, it will be a codec of its own rather than a change to lanes.
If you would rather use all this than read about it, the GPU API is where to start, and the benchmark compares the recipes on real Earth observation data.