SVG art drawn crisp at its on-screen pixel size, in the GUI as an Image widget and in the 3D world as a texture. One rasterizer, one cache, no PNG step for users.
01Measured: the rasterizer
One icon (linear gradient, strokes, ellipses, a translucent path), median of 10 runs on a laptop CPU, Python 3.13. The rasterizer runs on the CPU, so the software GPU (llvmpipe) does not change these numbers.
| library | 48 px | 512 px | size and system deps | notes |
|---|---|---|---|---|
| thorvg-python 1.1.3 | 0.15 ms | 1.5 ms | 3.1 MB; libc and libstdc++ only; py3-none wheels for Linux, macOS, Windows | Writes premultiplied RGBA into a buffer. Renders <text> after a font is loaded. Binding LGPL-2.1, thorvg MIT; used unmodified as a dependency. |
| skia-python 144.1 | 0.2 ms | 4.7 ms | 51 MB; links system libGL, libEGL, libX11 | No SVG text (its SVGDOM has no font manager in the binding). |
| resvg-py 0.5.0 | 1.6 ms | 50 ms | 2.9 MB; no system deps | Most complete SVG, but only returns PNG: 45 ms render and encode plus 3.5 ms decode at 512 px. Holds the GIL, so a worker thread does not help. |
| cairosvg 2.9.1 | 2.0 ms | 28 ms | needs system libcairo | Import takes 280 to 420 ms. |
Coverage against resvg as reference, 128 px, mean alpha difference out of 255: clip path 0.1, radial gradient 0.15, dashes 0, mask 1.0, Gaussian blur 3.4, drop shadow 8.3 (skia gives the same shadow as thorvg; resvg is the outlier). Text matches once the font is loaded.
02Decisions
| decision | why |
|---|---|
Rasterize with thorvg-python, as the optional extra manifold-gfx[svg] (also in [all]). | Ten to thirty times faster than the alternatives without system libraries; a 512 px icon fits in a tenth of a 60 fps frame. |
| One GPU texture per (SVG, pixel size), one draw per image, instead of an atlas. | Icons run from 16 px to a 1200 px wordmark, which a fixed-slice array like the text atlas cannot hold, and a packed atlas needs repacking on every resize. Checked before the PR: the cost of 200 icons per frame on llvmpipe; above 1 ms it becomes a packed atlas behind the same API. |
| Cache key is (SHA-1 of the SVG bytes, width px, height px); LRU with a 64 MB budget; evicted textures are destroyed. | A resize makes new sizes; the budget keeps old ones from piling up. Hashing content means a path and the same markup share an entry. |
| Pixel size = layout size × GUI scale × the render target's pixels per window pixel, rounded; quads snap to whole target pixels. | That is the size the image lands at, so it maps 1:1 to the screen: crisp on HiDPI and in supersampled stills. |
| Draw order in the GUI pass: rects, then images, then text (changed during the build, see 05). | Same rule text already has: an image sits over every panel. |
Images blend premultiplied like glyphs; tint is passed to the shader exactly as text's fg. | A white icon tinted with a text colour then matches the text beside it. |
Textured StandardMaterial now honours albedo alpha: texels under 0.5 are discarded, and translucent materials multiply opacity by texel alpha. | A sign or logo needs see-through parts. Behaviour change for other users: a texture with an alpha channel that rendered opaque before now shows its holes. Opaque textures are unchanged. Shadows still use the full mesh. |
03The contract
These names are what a game builds against. Units are UI pixels unless stated.
04Tasks
1. Rasterizer and cache src/manifoldx/svg.py, pyproject.toml, tests/test_svg.py
- Tests: source kinds give the same pixels; sizes match the request; a second call at the same size does not rasterize again; a new size does; LRU evicts past the budget; a missing extra raises an ImportError naming
manifold-gfx[svg]. - Implement over one module-level thorvg Engine; unpremultiply the output.
verify: uv run pytest tests/test_svg.py
2. Image widget, ImageOp, GUI pass gui/widgets.py, gui/painter.py, render/passes/gui.py, tests/gui/
- Tests: intrinsic and one-sided sizes keep aspect; paint emits an ImageOp centred in the box; the pixel size follows GUI scale and target density; changing scale asks for a new size.
- Textured-quad pipeline with the glyph pipeline's blend state; per-image texture cache on the pipeline, destroyed on eviction.
- Measure the first draw of a 48 px and a 512 px icon through the pass, and 200 icons per frame on llvmpipe.
verify: uv run pytest tests/gui
3. Textures from arrays and SVG; alpha in StandardMaterial textures.py, resources.py, tests/test_textures*.py
load_texturebecomes decode plustexture_from_array; no change for its callers.- Textured shader samples alpha, discards under 0.5, multiplies output alpha.
- Tests: handle size, aspect from
size_px, an offscreen render where a transparent texel shows the background.
verify: uv run pytest tests/test_textures*.py
4. Example, still, changelog examples/svg_icons.py, CHANGELOG.md, AGENTS.md
- Icons at 24, 48, 96 and 192 px, a resize mid-run, an SVG texture on a spinning cube;
--still out.pngheadless. - Full suite and lint serial; all examples still import and render.
verify: make test && make lint and the still
05Where the build departs from this plan
| change | why |
|---|---|
| The GUI draws in the widget tree's order (rects, images and text interleaved), not rects, then images, then text. | The game's director asked for it: a dialog over buttons must hide their icons. Each run of same-kind ops is one draw; text is no longer above every panel. |
An Image whose pixel size changes from frame to frame draws from a raster bucketed in steps of 2^(1/8), and gets an exact raster again the frame its size holds (settle_size, bucket_size in render/passes/gui.py). | The game animates icon sizes every frame (pulses, pop-ins); an exact raster per frame would fill the cache. |
| Rasters use thorvg's straight-alpha colorspace (ABGR8888S). | Dividing premultiplied colour in numpy took 30 ms at 512 px; thorvg's own path takes 1.9 ms in all. |
| One texture per image stays, though 200 icons cost more than the 1 ms threshold set above. | Measured on a laptop with the GPU and with llvmpipe: image draws for 200 icons take 1.6 to 2 ms, of which 0.5 to 0.7 ms is draw calls, all an atlas would save. Laying out the same 200 widgets already costs about 2 ms in the existing GUI code. |
06Non-goals
Animated SVG and Lottie, SVG filters beyond what thorvg draws, rotating an Image, normal maps from SVG.