Gaussian Splats (native)

The acron.gsplat module is the native half of Gaussian-splat rendering: everything that touches every splat on the CPU. A scan is millions of splats; reading one, packing it into GPU planes, decoding it for tools and sorting it every time the view changes are array work with no business in an interpreter – and Android, web and C++-only programs have no numpy to lean on anyway. The renderer itself (the work-buffer pass that decodes the planes on the GPU, the instanced draw, the modifier hook that lets a program deform the splats) lives in the Python tier, acron.splats, over public engine API; this module is what it calls, and what a C++ program uses directly.

The bundle

GSplatBundle

A scan in the SOG v2 layout (“Spatially Ordered Gaussians”, PlayCanvas splat-transform’s format), whatever it was read from. Every attribute is an RGBA8 plane with splat i at texel (i % width, i / width): 16-bit log-space positions (means_l / means_u), smallest-three quaternions (quats), codebook indices for the log scales (scales) and the DC colour plus opacity (sh0), and optionally the higher-band spherical-harmonic palette (shN_labels / shN_centroids).

  • read_sog() reads a .sog (a ZIP, through ZipArchive, or a directory of the same files); read_ply() reads a 3DGS training .ply (binary of either endianness, or ASCII) or a compressed one (PlayCanvas’s: a chunk element of bounds, four packed integers per splat and optionally an sh element of bytes; recognised by its packed_position property) and read_splat() a .splat file (the web viewers’ flat format: 32 bytes per splat, no header, no higher bands), each packed into the same planes; read() picks by extension (anything that is not a .ply or a .splat is read as a .sog). with_sh keeps the higher spherical-harmonic bands and is on by default in every reader, as in the viewers of these formats: colour is then view-dependent as the scan was trained. Pass false to load without them (a .sog saves its label plane and a palette of at most 4 MB; a large .ply saves 15 texels per splat).

  • from_arrays() packs flat float arrays (positions, quaternions, linear scales, colours, SH coefficients); write_sog() and write_ply() are the inverse, for tools and tests.

  • get_plane() hands a plane out as a PTA_uchar (RGBA, row 0 first), make_plane_texture() and make_codebook_texture() as nearest-filtered textures already in the engine’s BGRA RAM order – uploaded undecoded; the GPU decodes.

  • get_count, get_sh_bands, get_bounds_min / get_bounds_max (the splats’ centres; get_means_min / max are the log-space range the positions are quantised over) and get_meta_json (the bundle’s meta.json) describe it.

  • decode_positions / decode_quaternions / decode_scales / decode_log_scales / decode_colors / decode_sh are the CPU mirror of the shader’s decode (PTA_float, one row per splat); select / decimate / head repack a subset. A quats texel whose mode byte names no component (under 252) decodes as no rotation, here and in the shader.

  • release_planes() drops the planes once their textures are made, for a program that keeps the bundle only for its count, bounds and codebook: the decode methods then return zeros and the bundle can no longer be written, selected from or turned into textures.

  • eval_sh() evaluates the real SH basis (bands 1-3) for per-splat view directions – the CPU twin of the shader’s evalSH.

Failures return a null bundle (or false) and leave a message in get_last_error().

The readers treat a file as untrusted, and refuse rather than repair: meta.json is at most 8 MiB and 100 000 JSON values, nested at most 64 deep; the means range and every codebook entry must be finite (entry 0 may be null, as older tools wrote it), the range not inverted and within ±88 (it is of log-transformed positions; from_arrays and the .ply / .splat readers refuse positions past that range too, so what is packed can be written and read back); a plane file is at most the raw size of the largest image the WebP decoder takes. Of a plane taller than the splat count needs, only the rows in use are decoded – straight into the plane’s own array – so get_height, the plane textures and the memory a load takes follow the count, not the size a file declares (rows below those are not read, so damage there goes unnoticed); the SH palette is bounded the same way, at the 65 536 entries a label can name. Members of a zipped .sog are read with ZipArchive’s bounded read: a member over its limit, with a damaged header, with a stream that is not the length its directory records, or failing its CRC-32 is a reader error like any other (a null bundle and a message), never an engine assertion. A .ply with more than 2^24 splats is read without its higher SH bands (with a warning): a direct palette holds no more. write_sog() writes the shN section only for a bundle that holds its palette (one read without with_sh is written as a bundle without higher bands).

WebP

Every SOG plane is a lossless webp, and neither the engine’s image readers nor its thirdparty libraries include libwebp. The module therefore carries its own codec (gsplatWebP.cxx, after RFC 9649): the decoder reads the whole lossless (VP8L) format – all four transforms, meta prefix codes, the colour cache and LZ77 references – and is checked against libwebp’s output; the encoder writes a simple valid lossless stream (no transforms, per-channel Huffman codes). Lossy webp is refused: a lossy plane would corrupt the quantised attributes. decode_webp() and encode_webp() expose it for any PNMImage, and encode_webp_rgba() for raw RGBA bytes.

The sorter

GSplatSorter

Owns three arrays sized to the splat count. depths holds one view-space depth per splat and visible one byte per splat (1 = draw); the caller fills both – from Python, where the three arrays are writable, fixed-size memoryview objects (they cannot be resized, so nothing can reallocate them under a sort running on another thread), or from a GPU depth plane with load_depths() (or a copy of its RAM image with load_depth_image()). A depth plane is a float texture with one texel per splat: R32F, holding the view depth of a splat worth drawing and 0 (or less) for the others – what the Python tier’s work buffer writes, 4 bytes a splat on the way back to the CPU – or RGBA32F, with the depth in red and a draw flag in green. sort() then fills order with the indices of the visible splats, farthest first, as float32 (the format an R32F order texture takes), and returns how many (a splat whose depth is NaN or infinite is left out, like an invisible one); write_order() copies them into such a texture’s RAM image in one step (it returns false for a texture that is not single-channel float), and make_order_texture() makes one holding the identity order. The sort quantises the depth range to 2^bits buckets (bits = clamp(log2(n / 4), 10, 20) unless set) and counting-sorts on the key, stable, so ties keep index order: O(n), and BLOCKING, so a worker thread can run it while the frame loop draws. The buckets span the bulk of the depths – the central 80% of a sample, widened by twice its width on each side – not their extremes: depths beyond that share the first and the last bucket, which are then ordered exactly, so a stray splat at 1e30 costs the rest of the scene no precision. Fewer than 64 splats are simply sorted. depth_along() gives the plain undeformed depth of a centre array along a direction. A sorter is not synchronised: fill, sort and read it from one thread at a time (the Python SplatSorter does this under its own lock).

from array import array
from acron.gsplat import GSplatBundle, GSplatSorter

bundle = GSplatBundle.read("scan.ply")
textures = {name: bundle.make_plane_texture(name)
            for name in ("means_l", "means_u", "quats", "scales", "sh0")}

sorter = GSplatSorter(bundle.count)
memoryview(sorter.depths)[:] = array("f", my_depths)
n = sorter.sort(False)               # farthest first
sorter.write_order(order_texture)    # an R32F texture the draw reads

A C++ program gets the decode and draw shaders from the Python package’s splats/shaders (see Gaussian Splats for what each pass does) or writes its own against the plane layout above.