Drawing
Point clouds
A laser scanner (LiDAR) or a photo survey gives millions of points: a position each, often a color or a value such as height or intensity. There are no triangles to draw. This page explains how to draw such a cloud fast: small splats on the screen, compact storage, a cheap level of detail, and colormaps that turn values into colors. The last part shows how RASM does it.
In short
- Each point is a splat: a small square, disc or sphere on the screen, a few pixels or a few centimetres wide.
- Points come in chunks of up to 65 536 near each other. A chunk stores each position as three 16-bit steps of its box: 8 bytes a point with a value, 12 with a color.
- A point is one triangle around its square; the pixel stage cuts it to its shape.
- Small points skip the triangle (up to 6 pixels in a perspective view, 4 in an orthographic one, 2 under MSAA): a compute pass writes each pixel with a 64-bit max of depth and point, two (6 pixels) to twelve (1 pixel) times faster per point, and a resolve puts them into the scene. Both paths take the same pixels, in every shape.
- The builder puts a chunk's points in an order where every prefix is an even sample. A chunk small on screen draws only its first points: level of detail with no extra data.
- A colormap turns a value into a color through a range (linear or logarithmic) and a 256-entry texture.
1Splats: what a point looks like
A point has no size. To see it, the renderer draws a small shape at its place, a splat. Three shapes cover most uses.
Square
The cheapest. At 1 or 2 pixels every shape looks like this anyway.
Round
A disc. Its edge is smooth with several samples per pixel.
Sphere
A disc that writes a sphere's depth: overlapping points meet along a curve, as solid balls would.
The size is in pixels (the same on screen at every distance) or in metres (smaller when far away). A splat is never less than one pixel wide, so a far point neither vanishes nor flickers.
2One triangle a point
The usual splat is a quad: two triangles, four corners. But for points of one or two pixels the cost is per point (the corners and the triangle setup), not per pixel. One triangle that holds the square costs one setup instead of two; the pixel stage cuts it back to the square or the disc.
3Small points by compute
A triangle costs the same setup whether it covers 4 pixels or 400. For points of 1 or 2 pixels almost all the time goes there, not into the pixels. A compute shader can skip the triangle: one thread takes a point, finds the pixels its shape covers, and writes them itself (Schütz, Kerbl and Wimmer, 2021). For bigger points the pixels take over, and at about 7 pixels the triangles win (below).
The hard part is the depth test, which the triangle path gets for free. Many threads write the same pixel at once, in any order. The trick: put the depth and the point in one 64-bit word and keep the largest with a 64-bit atomic max (InterlockedMax in Slang). With reversed-Z a nearer point has a larger depth, so the max is the nearest point, whatever order the threads ran in.
Then a resolve draws a quad over the pixels the points can reach. Each pixel reads its word, finds the point, and writes its color, its depth and its motion into the scene's targets, with the normal depth test. So meshes in front of a point still hide it, and everything drawn later (labels, the grid, see-through draws, TAA) sees the points like any other surface.
Which chunk takes which path
The cull pass decides per chunk, after it chose the chunk's prefix. A chunk goes to compute when both hold:
perspective view: sc ≤ 6 and sn ≤ 8 pixels
orthographic view: s ≤ 4 pixels
view with MSAA: sn ≤ 2 pixels
and k × max(sc² / 4, 1) ≥ 0.125 × (its rect on screen, in pixels)
- sc, sn
- the points' size at the chunk's centre, and at its point nearest the camera: the largest they get (in an orthographic view the two are the same)
- k
- the points the chunk draws (its prefix)
- rect
- its box's corners on screen, and sn pixels more (at least 2): where its points can land
The first rule is measured. A thread writes its point's pixels one after another, so the compute path pays for every pixel of every point; the triangle path pays a setup per point, and its early depth test throws away a hidden pixel cheaply. The cost of one more point, every point drawn (no LOD, so many points cover each pixel), one perspective view of 1920 × 1080 with one sample a pixel (28_point_cloud --all-points, 10 M against 20 M points, RTX 3060):
| point size | 1 px | 2 px | 3 px | 4 px | 6 px | 8 px |
|---|---|---|---|---|---|---|
| triangles, round | 1.03 ns | 1.07 ns | 1.39 ns | 1.99 ns | 3.58 ns | 5.53 ns |
| compute, round | 0.088 ns | 0.26 ns | 0.45 ns | 0.79 ns | 1.73 ns | 3.02 ns |
| triangles, square | 1.44 ns | 2.09 ns | 3.75 ns | 5.84 ns | ||
| compute, square | 0.58 ns | 1.02 ns | 2.20 ns | 3.88 ns |
Per point the compute path's lead shrinks from 12 times at 1 pixel to under 2 times at 8. Under 3 pixels every shape is a square; a disc has fewer pixels than its square, and a SPHERE point costs what a disc does (its depth at each pixel is a square root and a division more). But at the LOD's density each pixel holds few points, the triangles' depth test has less to throw away, and the compute path also clears and resolves its rect. A whole frame of points of one size decides:
Under MSAA the triangles' edges cover samples by their distance, and the compute path has one sample a pixel: there it takes only the points that cover whole pixels on both paths, of 2 pixels or less, so an MSAA view draws bigger points as before.
The second rule is the cost per pixel: once the compute path runs it also clears and resolves every pixel of the rect its chunks cover, so a sparse chunk spread over the screen is cheaper as triangles. A point of sc pixels counts as (sc / 2)² points of 2 pixels: it covers as many pixels. A chunk the camera is inside of, or that is partly behind it, stays triangles too.
Why the two paths draw the same pixels
A chunk can switch paths from one frame to the next as the camera moves, so the two must agree to the pixel, or the switch would show. Both cut a point to the same shape (getPointShape: a square under 3 pixels, from 3 pixels its style's square, disc or sphere) and take a pixel when its centre lies in that shape, edges included, and both place the shape at the same centre: the compute path snaps it to 1/256 of a pixel, as the rasterizer snaps a triangle's corners. A SPHERE point's centre is its front's, as its triangle's, and its depth at each pixel is its surface's, from the same function on both paths (getSphereDepth). A pixel whose centre falls exactly on the shape's border goes as the rasterizer sends it: kept on the left and right sides and on the top, left out on the bottom, which is the triangle's own bottom side (the top-left rule). With the hole fill off, the compute path's perspective view in 28_point_cloud then differs from the triangles' in 0.012 % of its pixels for squares of 2 pixels, 0.016 % for squares of 3, 2 pixels for discs of 4 and for spheres of 5: centres a rounding away from a border. Under MSAA the compute path has one sample a pixel and the resolve writes it into all of the pixel's samples; in a view that also has the compute path, the triangle path covers whole pixels the same way, its triangle reaching half a pixel past the square so no sample of a covered pixel falls outside it. (A view drawn by triangles only keeps the smaller triangle, which is a fifth cheaper under MSAA; see Not yet.)
Filling the LOD's holes
The LOD keeps about as many points as the pixels a chunk covers, so a few pixels of a surface stay empty, and with whole pixels each shows as a single black dot (0.04 % of the top view of 28_point_cloud). The resolve fills them: an empty pixel takes the nearest of its 8 neighbours' points when at least 6 of them hold one, their depths are within 5 % of each other, and that point's chunk was thinned by the LOD.
28_point_cloud; the top view ends with no hole (main: 0.001 %, its alpha-to-coverage dimmed them), the perspective view with 0.025 % (main: 0.027 %).4Storage: 16 bits in a chunk's box
Points near each other share a box. Inside it, a position needs far less than three 32-bit floats: 16 bits per axis is a step of 1/65 535 of the box, 0.5 mm for a 32 m box. A value (height, intensity, temperature) gets 16 bits too, in steps of the chunk's range of values.
The box also gives the chunk's bounding sphere, so a chunk is culled like any object: out of the view, or behind something in the depth of the frame before. The chunk is placed in the world by a transform in doubles, so a cloud far from the origin keeps its precision (large worlds).
5Level of detail by prefix
A chunk far away covers few pixels, and drawing all its points would put many on the same pixel. The cheap fix: draw only its first k points. That works only if the first k points are spread over the whole chunk, for every k. A scanner writes points line by line, and a random shuffle clumps. The builder sorts the points along a Morton curve (a path that visits every small square before the next one), then reads that list in bit-reversed steps.
- Quantize the positions to 16 bits in the box.
- Interleave the bits of x, y and z: the Morton code. Sort by it.
- For i = 0, 1, 2, … take sorted[reverse(i)], the bits of i reversed (0, P/2, P/4, 3P/4, …).
- The first 2j points are every P/2j-th point along the curve: one in each region.
The level of detail costs no memory: no second copy of the points, no tree. Its limit is that a chunk is one unit: a very large chunk near the camera draws all of its points even where part of it is far. Small chunks (tens of metres) keep that error small; for whole datasets the chunks become the nodes of an octree (Streaming point clouds).
6How many points a chunk keeps
The chunk keeps enough points to cover its square on screen a few times. With D the diameter of its sphere on screen and a a point's area in pixels:
k = min(N, ⌈density × D2 / a⌉)
a = s2 (square), or π/4 × s2 (round, sphere)
- D
- pixels per unit at the sphere's nearest point × its diameter
- s
- the point's size in pixels, at least 1; a size in metres is converted at the same depth, so it cancels the distance
- density
- how many times the points should cover the chunk's square; 0 keeps them all
Measured on a synthetic scan (4 M points, chunks of 32 m, round points of 2 pixels, a top view at 4× MSAA, RTX 3060), all by triangles:
| density | points drawn a frame | scene pass | empty pixels on the ground, top view |
|---|---|---|---|
| 0.5 | 1.49 M | 1.63 ms | 6.5 % |
| 1 | 2.44 M | 2.60 ms | 0.3 % |
| 2 (default) | 3.76 M | 3.99 ms | 0 % |
| 4 | 5.44 M | 5.81 ms | 0 % |
| 0 (all) | 7.02 M | 7.65 ms | 0 % |
The π/4 matters: a disc covers less than its square. Without it, a density of 1 keeps as many points as 0.785 does with it, and leaves 1.3 % of the ground empty.
7Colormaps: values to colors
A value becomes a color in two steps: the range turns it into t from 0 to 1, and a 256 × 1 texture turns t into a color, read with linear filtering between its entries.
Perceptually even
viridis, magma, inferno, plasma and cividis change lightness evenly, so equal steps of the value look like equal steps. cividis also reads for color-blind viewers.
Turbo
A smooth rainbow: more distinct hues, good for heights, but its lightness goes up and down.
Gray
Lightness only, for intensity scans.
8Anti-aliasing and motion
MSAA: coverage from the distance
The shader turns the distance to the disc's edge into a coverage (alpha-to-coverage): half the samples of an edge pixel, a smooth rim. Points of 2 pixels or less have no rim to smooth: they cover whole pixels, all samples, on both paths, so a dense cloud shows no background through partly covered pixels.
TAA: the centre's motion
The projection's jitter moves the splat; every pixel of it writes the motion of the point's centre, so the history follows the point. The compute path projects with the same jittered matrix, and its resolve writes the same motion.
9Pitfalls
- A far chunk is a stripe or a corner of itself
- Its points are in scan order, so a prefix is a part. Build the chunk in a Morton order read in bit-reversed steps.
- Holes in a surface at a distance
- Too few points for the chunk's size on screen. Raise the density, or the point size.
- Holes close to the camera
- The data is that sparse: every point is drawn. Use a size in metres about the points' spacing.
- A chunk changes look when it changes path
- The compute path and the triangles take different pixels, or round a centre differently. Use one rule for both: a pixel whose centre is in the square, the centre snapped as the rasterizer snaps corners.
- A small cloud costs more by compute
- The clear and the resolve cost per pixel. Send a chunk to compute only when it draws enough points for the pixels it covers, and clear and resolve only that rect.
- Round points vanish at 1 or 2 pixels
- A disc that small misses the pixel centres it covers. Draw points under 3 pixels square.
- Steps in the positions far from the origin
- The points were floats in world space. Keep them as small offsets in their chunk, and the chunk's place in doubles.
- Colors look washed out or too dark
- The colormap's entries are sRGB; store them in an sRGB texture so the read gives linear color.
10In RASM
A chunk is a mesh made by buildPointCloudChunk, on any thread, and uploaded by Renderer::createMesh. An entity draws it with a MaterialType::POINTS material, whose PointMaterial gives the size, the shape, the colors and the density. The entity's transform places the chunk; culling, render space far from the origin and picking work as for any entity. Each view sends its small, dense chunks to the compute path where the GPU has 64-bit buffer atomics (shaderBufferInt64Atomics); RendererConfig::computePoints or Renderer::setComputePoints(false) draws every point by triangles.
RASM_EXPORT_VIEW) sizes given in pixels are multiplied by SceneData::pixelScale (getPixelScale), its ExportDesc::pixelScale, which also divides the LOD's pixels per unit; in every other view it is 1 and the shaders do no work for it. Sizes in metres do not change.| This page | RASM |
|---|---|
| Splats | PointShape SQUARE, ROUND (default), SPHERE; PointMaterial::size in VectorUnit::PIXELS or METRES, at least a pixel; round points under 3 pixels are drawn square (getPointShape, the same on both paths). Unlit. PointSizeMode::ADAPTIVE grows them where the chunk's spacing on screen leaves gaps, up to PointMaterial::maxSize (Adaptive point size). |
| One triangle a point | gfx::POINT_INDICES (3). The chunks share one index range 0, 1, 2, …, taken from the index pool with the first chunk. One pipeline for every shape writes its depth with SV_DepthLessEqual, so the early depth test still works. |
| 16 bits in the box | gfx::PointChunkHeader (48 B, with the chunk's spacing, given or measured) in the point pool (RendererConfig::geometryPoolPoints, 8 B units, 8 MiB by default); the chunk's GeometryData::vertexOffset is its first unit. |
| Prefix LOD | selectPointCount in shaders/lib/points.slang, called by the cull pass for the point run (SceneData::pointRun), the one place that decides what a chunk draws. PointMaterial::density is 2 by default. lodErrorPixels 0 draws every point; the forced LOD k (F2) draws N / 2k. |
| Colormaps | Renderer::createColormap from 1 to COLORMAP_SIZE linear colors or a ColormapPreset; PointColorMode::SCALAR reads it through PointMaterial::range (ScalarRange); COLOR takes the points' own colors. |
| Small points by compute | The cull pass sends a chunk to shaders/passes/pointRaster.slang when isRasterChunk (shaders/lib/points.slang) holds: in a perspective view points of POINT_RASTER_MAX_SIZE (6) pixels or less at the chunk's centre and POINT_RASTER_MAX_NEAR (8) at its nearest point, in an orthographic view POINT_RASTER_MAX_ORTHO (4), under MSAA (SceneData::depthSamples) POINT_PIXELS_MAX_SIZE (2); at least POINT_RASTER_MIN_DENSITY (0.125) drawn points of 2 pixels per pixel of its rect, instance row under 65 536. rasterPoint cuts each point to getPointShape's shape, a SPHERE point's depth per pixel from getSphereDepth, as points.slang does. appendPointBatches adds batches of POINT_BATCH_POINTS (1024) and grows the rect. After the last cull: clearMain, rasterMain (indirect); the resolve quad in the last draw pass, before the sky. A view's buffer (SceneView::pointRaster, getPointRasterLayout): a 48 B header, 8 B a batch, 8 B a pixel. |
| MSAA and TAA | Alpha-to-coverage with more than one sample, but points of 2 pixels or less (POINT_SHAPE_PIXELS) cover whole pixels on both paths (the compute path's resolve writes every sample); the centre's motion with TAA, on both paths. |
| Picking | Always by triangles, which take the same pixels as the compute path. A pick pipeline of the point run: PICK_HIT_POINT, and PickHit::feature is the point's index in the arrays given to the builder (Mesh::pointOrder maps it back). PICK_SKIP_POINTS looks through them. |
| The point run's other chunks | Glyph chunks (a GLYPHS material) sit in the same run, so no draw run was added for them. The cull pass sends them to the glyph cull (appendGlyphChunk), they take no slot, and the point pipeline never draws them (Instanced glyphs). The same colormaps and ScalarRange color glyphs and data lines. |
Code map
| What | Where |
|---|---|
| The API | PointCloudChunkDesc, buildPointCloudChunk, MAX_POINT_CHUNK_POINTS in include/rasm/pointCloud.h; PointMaterial in include/rasm/material.h; ScalarRange, ColormapPreset in include/rasm/colormap.h; rasm_build_point_cloud_chunk, rasm_create_colormap in C |
| The builder | src/scene/pointCloud.cpp |
| Colormaps | src/scene/colormap.cpp: the preset tables and createColormap |
| Storage | GeometryPool::allocate and GeometryPool::write for a chunk (src/renderer/geometryPool.cpp) |
| The point run | DrawRun::points, ShadedPipelines::pointPipeline, rebuildDrawList, drawSceneView in src/renderer/gpuScene.cpp |
| The compute path | preparePointRaster, recordPointReset, recordPointRaster, drawPointResolve, getPointRasterLayout in src/renderer/pointRaster.cpp; the buffers made in createSceneView where RenderContext::hasBufferInt64Atomics |
| Shaders | shaders/lib/points.slang (header, padding, selectPointCount, isRasterChunk, appendPointBatches), shaders/passes/points.slang (the draw and the pick), shaders/passes/pointRaster.slang (reset, clear, raster, resolve), shaders/passes/cull.slang; getPointDiameter sizes a point for the cull and both paths |
| Example | examples/28_point_cloud/main.cpp: a synthetic scan 6 000 km out, colored by height, a perspective and an orthographic view; --adaptive for adaptive points |
| Tests | tests/pointCloudTests.cpp (round trip, prefixes); image tests points, points_msaa4, points_taa, points_ortho, far_points, and points_triangles, points_msaa4_triangles, points_taa_triangles (the same scene by triangles only, against the compute path's image), points_adaptive, points_large and points_large_triangles (discs of 5 pixels, spheres of 4, squares of 3 on both paths) |
A chunk's order is part of its data: every prefix must stay an even sample. The cull pass decides each chunk's count in selectPointCount only, and its path in isRasterChunk only. The two paths must take the same pixels: a change to one's coverage, size or depth changes the other. PointData, PointChunkHeader and PointRasterHeader are kept in step on both sides (struct layout).
Not yet
- Without the compute path (no 64-bit atomics, or
computePointsoff) a small point's triangle under MSAA leaves out the samples of a pixel past the square's left, bottom and diagonal sides (0.7 % of the pixels of thepoints_msaa4test differ from the compute path's image), and nothing fills the LOD's holes: 0.04 % of the pixels of28_point_cloud's top view stay black. - The hole fill is the compute path's only: a thinned chunk that switches to the triangles shows its few holes again (about 1 pixel in 2 500 at 2 pixels). With points of 3 to 8 pixels on the compute path that includes the single-pixel gaps between a thinned chunk's bigger points, squares or discs (0.5 to 0.65 % of the perspective view's pixels in
28_point_cloud --size 3and--size 4). - Under MSAA the compute path takes points of 2 pixels or less only: bigger ones keep their triangles, whose edges cover samples by their distance. The compute path would be cheaper there too, but with whole pixels, so their rims would change.
- A thread writes its point's pixels one after another. A group that shares out the pixels of its points among its threads was 0.4 ms faster where a chunk's points have mixed sizes (an adaptive chunk with finer chunks in some octants), but 11 % slower for points of one size, so it is not used.
- Points cast no shadows, and debug views draw them as they are (
D50).
Further reading
- Markus Schütz, Bernhard Kerbl, Michael Wimmer, "Rendering Point Clouds with Compute Shaders and Vertex Order Optimization", 2021.
- Markus Schütz, "Potree: Rendering Large Point Clouds in Web Browsers", 2016.
- LOD selection, frustum culling, large worlds, picking.