Skip to main content

How rendering works

A Gaussian Splatting image is millions of soft, translucent blobs, painted from nearest to farthest. That idea is simple. Doing it in a millisecond is what shapes all of the code.

A real render, built from the nearest Gaussians outwards
Render using the nearest 2% of the on-screen Gaussians
1 / 8
The nearest 2% of on-screen Gaussians (31 thousand), on a trained Mip-NeRF 360 garden model. Each image is a real MojoSplat render.

This guide follows one image through MojoSplat's renderer, based on Faster-GS. It is written for someone who knows the usual 3DGS CUDA rasterizer, but you can follow it without that. The animations play on a small example scene and run the real steps on it; press Pause or drag the slider to go at your own pace.

1. Project​

A Gaussian is described in 3D by a mean, a rotation, three scales and an opacity. To draw it, we need its 2D shape on the image. The mean projects like any point. For the covariance, we use the camera's Jacobian at the mean: the local linear approximation of the projection, J Σ Jᵀ.

Projecting one Gaussian
image planecamera3D Gaussian
1 / 90
Seen from above. The Gaussian's center goes through the pinhole, and its covariance goes through the Jacobian of the projection: J Σ Jᵀ. Closer means larger; rotating changes the width. Footprint here: ±2σ = 21.3 units.

In the code, QuatScale turns the raw parameters into a 3D covariance, and PinholeEWA does the projection.

2. Keep only what is visible​

One GPU thread handles each Gaussian. Most Gaussians aren't visible in a given view, so this stage's most important job is to throw them away early and pack the rest together.

Preprocess: one thread per Gaussian
All Gaussians (N)01234567891011Slots: only the visible ones, packed together0
1 / 12
Gaussian 0 is visible and takes slot 0, touching 9 tiles.

Three things happen for each survivor:

  • Its 2D covariance is inverted to a conic, so a pixel's distance is cheap to compute.
  • It gets a screen rectangle that holds exactly the pixels where alpha can reach 1/255, and the tiles under that ellipse are counted with an exact test, not just the rectangle.
  • It takes a slot. The k-th visible Gaussian writes slot k of every output.

Each slot is one 32-byte record holding position, conic, opacity and the rectangle, which is a single memory access for the later stages.

Final render of the garden scene
Final render, garden test view.
Heat map of how many Gaussians touch each tile
How many Gaussians touch each tile (log scale). The median is 437 and the busiest tile has 4,100.

In that view, 1.59 million of 4.2 million Gaussians are visible, and together they produce 3.7 million tile entries across 5,740 tiles.

3. Put everything in order​

To blend, each tile needs a list of the Gaussians touching it, nearest first. A Gaussian that touches several tiles appears in several lists.

The usual approach makes one entry per Gaussian-tile pair and sorts them all by a wide (tile, depth) key. Here, the same result comes from two cheap sorts.

Getting each tile’s Gaussians in depth order
Three tiles and the Gaussians on themtile 1tile 2tile 3CEADBVisible GaussiansAdepth 2Adepth 2Bdepth 2.6Cdepth 3.1Ddepth 3.6Ddepth 3.6Edepth 4.2
1 / 4
The goal: a list for each tile, nearest Gaussian first. We start with the visible Gaussians in no particular order.

The trick is in the last step. A stable sort keeps the existing order of entries with equal keys, and the entries were already in depth order. So sorting by tile alone leaves each tile nearest first, with no depth in the key. With about 5,700 tiles, the key fits in 16 bits, which is two radix passes.

4. Blend, one tile at a time​

Now the image is painted. A usual rasterizer loops over each pixel's Gaussians. Here, a tile (16×12 pixels on NVIDIA, one block of 192 threads) is divided into six sub-tiles of 8×4 pixels, and each one belongs to one warp of 32 threads, one thread per pixel.

Blend: tile 5, 16×12 pixels, 6 warps
Color so far
Light still getting through (blue = plenty, dark = almost none)
This tile's list, nearest first
051234
1 / 7
Tile 5, about to blend 6 Gaussians front to back. Each outlined sub-tile is one warp of 32 threads, one thread per pixel.

A warp walks the tile's list on its own, 32 Gaussians at a time. Its lanes fetch one record each and vote on which ones overlap its sub-tile. The overlapping ones go into a small shared-memory slab, and every lane composites them at its own pixel. This has two benefits: a Gaussian that misses a sub-tile costs that warp nothing per pixel, and a warp stops as soon as its pixels are opaque, without waiting for the others.

Compositing a fragment is one multiply and one exp2:

alpha = opacity * exp2(min(power, 0))

As in 3DGS, fragments with alpha below 1/255 are skipped, and a pixel stops after the fragment that brings its transmittance below 1e-4.

5. What training saves​

A plain render ends here. Training needs gradients, and for that the backward needs the state of each pixel partway through the tile. So a training forward also saves:

  • each pixel's accumulated color and transmittance before every group of 32 Gaussians, called a snapshot,
  • each pixel's final transmittance, and the last Gaussian that affected it.

The snapshots let the backward start at any group of 32 without replaying the tile. All of this is described by ForwardState, which holds only sizes and pointers.

6. Blend backward​

This is the stage least like a per-pixel backward. One warp takes one group of 32 consecutive Gaussians (a bucket), and each lane owns one Gaussian. The tile's pixels stream through the lanes.

Blend backward: one lane per Gaussian (6 of 32 lanes shown)
Lanes, each owning one Gaussianlane 0G0lane 1G5lane 2G1lane 3G2lane 4G3lane 5G4p0Gradient sum, in registers
1 / 18
Pixels stream through the warp, one lane per step. A lane evaluates its Gaussian at the pixel it holds, adds to that Gaussian’s gradient sum, and shuffles the pixel’s state to the next lane.

Each lane keeps its Gaussian's gradient in registers across all pixels, then does one set of atomic additions at the end. That's the point of the design: atomics are expensive, and here there are 32 sets per bucket instead of one per pixel per Gaussian.

For one pixel, with T the transmittance before this Gaussian, c its color, after the color contributed by Gaussians behind it, and g the loss gradient at that pixel:

dL/dalpha = sum over channels of g * (T * c - after / (1 - alpha))
- T_final * dot(g, background) / (1 - alpha)

The gradients of position, conic and opacity all follow from that value.

7. Back to the parameters​

The last stage turns each slot's gradients into gradients of the raw parameters, one thread per Gaussian. It writes in Gaussian order, so memory writes are contiguous, and it recomputes the forward from the parameters instead of storing it. Culled Gaussians simply get zero gradients.

One Gaussian, forward then backward
Raw parameters
mean, scales, quat
QuatScale
→ 3D covariance
PinholeEWA
→ position, 2D cov.
Gaussian2D
→ conic
Slot gradients
from blend backward
The fields of each block are the tape.
1 / 9
Forward: start from the raw parameters of one visible Gaussian.

The blocks in the code work exactly like this figure: a block's constructor runs its forward and stores what its backward needs, then backward walks it in reverse.

var shape = QuatScale[dt](log_scales, rotations) # forward, top to bottom
var place = P(mean, shape, cam)
var resp = R(place.mean2d(), place.cov2d(), GaussianGeometry.of(mean, shape), cam)

var grad = resp.backward(slot_gradients, place, shape, cam) # backward, bottom to top
# Gaussian2D.backward: its own pullback to screen gradients, then
# place.backward(shape, screen.mean2d, screen.cov2d)

Fields the backward doesn't use are removed by the compiler, so this costs what the same expressions would cost written out flat.

For training, the higher-order SH gradients aren't even written. Adam recomputes them per element, which avoids writing and reading the largest gradient buffer.

Where each stage lives​

StageFile
Stage order, scratch buffersmojosplat/render/rasterizer.mojo
Preprocessmojosplat/render/preprocess.mojo
Sorting and tile listsmojosplat/render/instances.mojo, mojosplat/gpu/sort.mojo
Blendmojosplat/render/blend.mojo
Backwardmojosplat/render/blend_backward.mojo, preprocess_backward.mojo
3D shape mathmojosplat/render/blocks/shape.mojo
Projection and Jacobianmojosplat/render/blocks/placement/pinhole_ewa.mojo
Footprint: coverage, pixel alpha, gradientsmojosplat/render/blocks/response/gaussian2d.mojo
Spherical harmonicsmojosplat/render/blocks/appearance/sh.mojo

rasterizer.mojo is mostly plumbing: buffers and pointer arguments. To read the algorithm, start at _raster there, then read the kernels in the order of this guide.

Reading Mojo if you know CUDA​

CUDAMojo
__global__ void kernel(...)a plain def kernel(...)
kernel<<<grid, block>>>(args)stream.enqueue_function(ctx.compile_function[kernel](), args, grid_dim=, block_dim=)
blockIdx.x, threadIdx.xblock_idx.x, thread_idx.x
ptr[i]ptr[unsafe_offset=i]
__shared__ float s[N]stack_allocation[N, Float32, address_space=AddressSpace.SHARED]()
__ballot_sync(mask, pred)warp.vote[...](pred)
__shfl_sync(mask, v, lane)warp.shuffle_idx(v, lane)
__syncwarp()syncwarp()
atomicAdd(&p, v)Atomic[...].fetch_add[...](p, v)
template<...>parameters in [...], and comptime if for compile-time branches