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.
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.
Round first, subtract later
Floats on the GPU, world positions: the error is half a step of the big number.
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.
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.
Rewrite every row
Exact, but the CPU cost grows with the entity count.
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.
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.
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.
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.
| Data | How it reaches render space |
|---|---|
| Objects | High/low rows, minus the view's origin on the GPU. |
| Lights with a position | The same: high/low in the light's row. |
| View, frustum planes, LOD plane | Made from the world-space view in doubles, moved by the origin, then floats. The view matrix becomes a rotation. |
| Shadow maps | The 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, readbacks | A 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:
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.
Where RASM follows the page, and where it differs
| Part | This page | RASM |
|---|---|---|
| CPU positions | doubles | glm::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 space | the camera at the origin | Each 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. |
| Rows | high/low | InstanceData: three rows, w = the high part, and translationLow (160 B). InstanceTransform and AnimatedEntity the same. gfx::Light adds positionLow (68 B). |
| Keeping the order | opaque steps | subtractSplit 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. |
| Motion | last frame in world space | SceneView::previousViewProjection is a world-space glm::dmat4; previousWorld rows hold last − now in w. |
| Shadows | world texel grid | fitShadowCascade 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 conversions | explicit | RASM's own targets build with GLM_FORCE_EXPLICIT_CTOR: a dvec3 or dmat4 becomes a float type only through a written cast. |
| World positions back | origin + render position | toWorldPosition and toRenderPosition (rasm/renderSpace.h); a view's origin is its camera's getCameraTransform(camera).position in the frame it drew. |
--comparesyncGpuScene 0.3 µs per frame with 12 000 entitiesWhere it lives in code
| What | Where |
|---|---|
| The split, the rows, render-space matrices and planes | splitPosition, writeWorldRows, writePreviousRows, toRenderMatrix, moveRenderPlane in src/renderer/renderSpace.cpp; tested by tests/renderSpaceTests.cpp (also where the split stops being exact) |
| The GPU side | subtractSplit in shaders/lib/renderSpace.slang; toRenderSpace, getLight, getWorldMatrix, getPreviousWorldPosition in shaders/lib/core.slang; the animated rows in shaders/passes/animate.slang |
| The rows | Renderer::writeInstanceRows (src/renderer/gpuScene.cpp), Renderer::writeLightRow (lightClusters.cpp), writeAnimationRow (animation.cpp) |
| Each view's origin and matrices | Renderer::prepareSceneView, writeViewMotion, getShadowMaps in gpuScene.cpp |
| The demo and the proof | examples/23_large_world (--compare, --origin); image tests far_shadows, far_skinning, far_taa compare against the same scenes at the origin |
- No shader reads a world position: every position goes through
toRenderSpace(rows throughgetWorldMatrix, lights throughgetLight). - 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 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).
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
- Patrick Cozzi and Kevin Ring, 3D Engine Design for Virtual Globes (2011): chapter 5, Vertex Transform Precision.
- David Goldberg, What Every Computer Scientist Should Know About Floating-Point Arithmetic, ACM Computing Surveys (1991).
- Emil Persson, Creating Vast Game Worlds, SIGGRAPH 2012 course (Just Cause 2).