Drawing

Large worlds: float precision and camera-relative rendering

A map of the Earth puts its objects millions of metres from the origin. A 32-bit float cannot hold such a number to the centimetre, so the picture shakes and breaks apart as the camera moves. This page shows where the precision goes, and the three tools that bring it back: double positions on the CPU, camera-relative rendering, and a high/low split that lets the GPU subtract the camera without losing anything. The last part shows how RASM does it.

In short

  • A float has 24 bits of precision. Its step (the gap to the next float) grows with the number: 0.5 m at the Earth's radius.
  • The vertex shader computes object − camera. Both are big, the result is small: the big numbers' rounding errors are all that is left (cancellation).
  • Keep positions in doubles on the CPU (a step of 1 nanometre on the Earth), and draw in render space: the world moved so that the camera is at the origin.
  • Moving every object every frame costs CPU time. Instead, store each position as two floats, high + low, and let the GPU subtract the camera's high and low parts separately.
  • The compiler must not regroup those sums. Make each step opaque to it.
  • Reversed-Z with a float depth buffer already keeps depth precise at any distance from the camera; the origin does not matter to it.

1Floats have steps

A float stores a number as 24 bits of digits and a power of two. So between two powers of two there are always 223 floats, evenly spaced. The farther from 0, the wider the gap.

near 1 m step 0.000 000 12 m: too small to see near 100 km 100 000.0000+7.8 mm near 6378 km 6 378 137.06 378 137.56 378 138.0 6 378 137.37
The same 24 bits at three distances from the origin. On the Earth's surface, in Earth-centred coordinates, a float can only say "here or half a metre away".
0.12 µmfloat step at 1 m20 − 23
7.8 mmat 100 km216 − 23
0.5 mat 6378 km, the Earth's radius222 − 23
0.93 nma double's step at 6378 km222 − 52

2Big minus big: why it shakes

The GPU never needs the big numbers. It needs where a vertex is seen from the camera: object minus camera, a few metres. But if both are floats first, each is already rounded, and the subtraction keeps only the difference of the two roundings.

camera 6 378 135.12 → 135.0 vertex 6 378 137.37 → 137.5 vertex − camera = 2.5 (true: 2.25) as the camera moves, the result jumps in steps of 0.5 m: the scene shakes

Round first, subtract later

Floats on the GPU, world positions: the error is half a step of the big number.

camera 6 378 135.12 (double) vertex 6 378 137.37 (double) vertex − camera = 2.25, then a float a float near 2.25 has a step of 0.24 µm: nothing to see

Subtract first, round later

The small result is what becomes a float, so its error is tiny.

This is cancellation: subtracting two close numbers cancels their leading digits, and the error that was small next to the big numbers becomes big next to the result. It is why a scene far from the origin shakes, and also why lighting can break into blocks: every value computed from a world position carries the same error.

3Render space: the camera at the origin

So subtract the camera first, in doubles, on the CPU. The GPU then works in render space: the world moved so that the camera sits at the origin. Near the camera, where precision matters, every number is small.

world space (doubles) origin: the Earth's centre 6 378 137 m camera house render space (floats) camera at (0, 0, 0) house at (12, 4, −8) render = world − camera, in doubles, then floats
The view matrix of render space has no translation: the camera is already at the origin, so the matrix only turns the world. Lights, frustum planes and shadow matrices are all made in the same space.

The catch is cost. When the camera moves, every object's render-space position changes. Rewriting every object's GPU row every frame makes the CPU's work grow with the scene: 100 000 rows of 160 B, twice for two frames in flight, is 32 MB of uploads per frame.

all rows, every frame

Rewrite every row

Exact, but the CPU cost grows with the entity count.

rebase when the camera leaves its cell

Floating origin

Move the origin only now and then. Cheaper, but each move rewrites everything at once, and the error grows with the cell's size.

origin one origin per view per frame

High/low rows

Rows keep the absolute position in two floats; the GPU subtracts the camera (next section). Nothing per entity.

4The high/low split

A double has about 53 bits of digits, a float 24. Two floats hold about 48 of them: the high part is the double rounded to a float, the low part is what rounding left over, also as a float.

high = float(d)

low = float(d − high)

render = (high − cameraHigh) + (low − cameraLow)

high
The nearest float: 6 378 137.5 for 6 378 137.37.
low
The rest, small: −0.13, as precise as any float near 0.1.
render
The high parts of two nearby points are equal or a few steps apart, so their difference is exact. The low parts add what is left.

The CPU splits each position once, when it changes, and stores both halves in the object's row. Each frame it splits only the camera. The GPU does the four subtractions and one addition per vertex: a few instructions, and nothing to upload when the camera moves.

Drag the distance from 1 m to 10 million km. The red outline is the house with plain floats (world positions, rounded, then minus the camera); the green one uses the high/low split. Watch the red one break apart near the Earth's radius.

The split is not endless: its error is about 2−48 of the distance from the origin. On the Earth that is 0.02 µm. At 1010 m, 70 times the distance from the Earth to the Sun, points near the camera still land within 0.03 mm; at 1012 m it reaches millimetres, and a world that big needs an origin per region.

5Keep the order of the sums

The formula only works if it runs exactly as written. A compiler that sees (high − cameraHigh) + (low − cameraLow) − pixel may regroup it, for example as (high − pixel) − cameraHigh + …. Then a big number meets a small one first and the low parts are lost again.

Regrouped

The shading position snaps to 0.5 m blocks: a lamp's pool of light becomes squares.

In order

Each difference is computed on its own, then added.

Make each step opaque

SPIR-V's NoContraction decoration (HLSL and GLSL precise) is meant to forbid regrouping, but a shader compiler may drop it and a driver may ignore it. What every compiler respects is a value it cannot know: an OR of each step's bits with a 0 that is read from memory. The compiler cannot prove it is 0, so it cannot regroup through it. It costs one integer OR per component.

6Everything in one space

Every position the GPU compares with another must be in the same render space. Directions (a sun, the sky, a normal) do not change when the origin moves; positions do.

DataHow it reaches render space
ObjectsHigh/low rows, minus the view's origin on the GPU.
Lights with a positionThe same: high/low in the light's row.
View, frustum planes, LOD planeMade from the world-space view in doubles, moved by the origin, then floats. The view matrix becomes a rotation.
Shadow mapsThe light's matrices in doubles, moved by the origin. A cascade still snaps to the world's texel grid, in doubles: snapped in render space, the grid would move with the camera and the edges would crawl.
Last frame (motion vectors)The camera moved since the last frame, so the last frame's view-projection is kept in world space, in doubles, and moved into this frame's render space. An object's last translation is stored as last − now, a small number.
Picking, readbacksA render-space position read from the GPU plus the view's origin, in doubles, is the world position: takePick gives it so, to a millimetre 6 000 km from the origin (Picking).

7Depth far from the camera

Depth is a separate question. The depth buffer stores the distance from the camera, so the world's origin does not matter to it. What matters is how far the camera can see. With reversed-Z, a 32-bit float depth and an infinite far plane, depth = near / distance, and the float keeps the same relative precision at every distance:

0.12 mmdepth step at 1 kmdistance × 2−23
12 cmat 1000 kma mountain range from orbit
4.3 mat 36 000 kmthe Earth from a geostationary orbit
anynear planenear only scales the depth; it does not cost precision

Without reversed-Z, a standard 0-to-1 depth puts almost all its precision near the near plane, and far surfaces z-fight. With it, a camera in orbit and a camera on the street both need no extra trick: no logarithmic depth, no second depth range.

8Pitfalls

Far from the origin, vertices shake as the camera moves
Positions reach the GPU as world-space floats, or the view matrix has the camera's translation. Draw in render space.
Geometry is right, but lighting falls in blocks
One shader path still subtracts big numbers, or the compiler regrouped the split sum. Check every position the shading uses, and keep the sums in order.
Shadow edges crawl while the camera moves
The cascade snaps to texels in render space. Snap in world space, in doubles.
Moving objects smear with TAA far away only
The last frame's matrix was stored in render space and not moved to the new origin, or the last translation was stored as an absolute float.
A double silently becomes a float
Math libraries convert between precisions without a word. Make such conversions explicit, so each one is a decision.

9In RASM

Positions are doubles in the public API: Transform::position, Light::position and CameraController::TrackBall::center are glm::dvec3, and Transform::toMatrix and Renderer::getViewMatrix return a glm::dmat4. Each view draws in the render space of its camera: its origin is the camera's position.

CPU Transformdvec3 position on change instance row (160 B)world[3].w + translationLow cameradvec3 position each frame SceneData of the vieworiginHigh, originLow, view GPU, every vertex, light, sphere toRenderSpace (high − originHigh) + (low − originLow) each step kept in order Lights: Light::position the same way, in the light table (positionLow). Animated rows: the Animate pass adds the node's offset to the low part. then the shaders work as before: culling, Hi-Z, LODs, clusters, shadows, motion, the sky
Moving the camera writes one origin per view and no instance row. Moving an entity writes its rows, as before.

Where RASM follows the page, and where it differs

PartThis pageRASM
CPU positionsdoublesglm::dvec3 positions; rotation and scale stay floats. Mesh vertices and glTF node transforms are floats, in their mesh's space. The C API from 1.2 (rasm_transform_d, the _d calls, position_d).
Render spacethe camera at the originEach scene view uses its camera's position, also its shadow maps' views (they belong to the camera of addShadowPasses). toRenderMatrix moves a world-space matrix there in doubles.
Rowshigh/lowInstanceData: three rows, w = the high part, and translationLow (160 B). InstanceTransform and AnimatedEntity the same. gfx::Light adds positionLow (68 B).
Keeping the orderopaque stepssubtractSplit in shaders/lib/renderSpace.slang ORs each step with SceneData::zero (AnimatePushConstants::zero in the Animate pass). Slang drops precise, and the NVIDIA driver regroups across NoContraction, which makes lighting fall in 0.5 m blocks.
Motionlast frame in world spaceSceneView::previousViewProjection is a world-space glm::dmat4; previousWorld rows hold last − now in w.
Shadowsworld texel gridfitShadowCascade snaps in doubles; getSpotShadowView and getCubeFaceView return world-space glm::dmat4s.
Frozen culling—The frozen planes and LOD plane stay in the render space of the frame that froze them and are moved to each new origin (moveRenderPlane).
Silent conversionsexplicitRASM's own targets build with GLM_FORCE_EXPLICIT_CTOR: a dvec3 or dmat4 becomes a float type only through a written cast.
World positions backorigin + render positiontoWorldPosition and toRenderPosition (rasm/renderSpace.h); a view's origin is its camera's getCameraTransform(camera).position in the frame it drew.
62.8%pixels off, beforefar vs near, more than 8 levels, frame 120 of --compare
0.04%pixels off, aftershadow-edge pixels only; 0 with the culling debug view
0rows written per camera movesyncGpuScene 0.3 µs per frame with 12 000 entities
1.34 msframe, before and after12 000 pillars, flying camera, RTX 3060, Release

Where it lives in code

WhatWhere
The split, the rows, render-space matrices and planessplitPosition, writeWorldRows, writePreviousRows, toRenderMatrix, moveRenderPlane in src/renderer/renderSpace.cpp; tested by tests/renderSpaceTests.cpp (also where the split stops being exact)
The GPU sidesubtractSplit in shaders/lib/renderSpace.slang; toRenderSpace, getLight, getWorldMatrix, getPreviousWorldPosition in shaders/lib/core.slang; the animated rows in shaders/passes/animate.slang
The rowsRenderer::writeInstanceRows (src/renderer/gpuScene.cpp), Renderer::writeLightRow (lightClusters.cpp), writeAnimationRow (animation.cpp)
Each view's origin and matricesRenderer::prepareSceneView, writeViewMotion, getShadowMaps in gpuScene.cpp
The demo and the proofexamples/23_large_world (--compare, --origin); image tests far_shadows, far_skinning, far_taa compare against the same scenes at the origin
Keep these true
  • No shader reads a world position: every position goes through toRenderSpace (rows through getWorldMatrix, lights through getLight).
  • A view, shadow or light matrix is made in world space in doubles and moved with toRenderMatrix, never built from float world positions.
  • A new GPU struct with a position stores it high/low, and its shader subtracts with subtractSplit.
  • Data kept across frames (motion, frozen culling) is kept in world space or with its origin, because the origin moves with the camera.
A local frame

A globe's local up is the ellipsoid's normal, not +Y. SceneEnvironment::orientation turns the sky and its light into the local frame, and CameraController::FreeFly::up makes the free-fly camera yaw around the local up (examples/23_large_world). The C API has the positions in doubles from 1.2: rasm_dvec3, rasm_transform_d and the _d calls (C API).

Not done

glTF node translations are floats, in their mesh's space: a file whose nodes sit millions of metres from its own origin (3D Tiles without a centre of its own) loses precision. Place such a mesh with the entity's double position instead.

Further reading