Conventions and patterns for developing polyscope-rs.
cargo build # Build all crates
cargo build --release # Release mode
cargo test # Run tests
cargo check # Check without building
cargo fmt # Format code
cargo clippy # Lint (must give zero warnings)Rust edition 2024, minimum Rust version 1.96 (rust-version in the workspace Cargo.toml).
The engine requests only the Vulkan, Metal, DirectX 12 and WebGPU backends of wgpu (SUPPORTED_BACKENDS in polyscope-render/src/engine/mod.rs). The OpenGL backend is not a target: some shaders sample depth textures in a way that it cannot compile. Without a supported backend, engine creation returns RenderError::AdapterCreationFailed. The GPU tests skip when no adapter is available.
crates/
├── polyscope-core/ # Core traits, registry, state management, options, view state
├── polyscope-render/ # wgpu rendering backend, shaders, colormaps, materials
├── polyscope-ui/ # egui UI integration
├── polyscope-structures/ # Structure implementations
└── polyscope/ # Main crate (package polyscope-rs); re-exports the public types of the sub-crates
The main crate re-exports Vec2, Vec3, Vec4 and Mat4 from glam, but not the glam crate. Users who need other glam types add glam = "0.34" to their own Cargo.toml.
- Create module in
polyscope-structures/src/ - Implement
Structuretrait (withas_any,as_any_mut,bounding_box,length_scale,transform, etc.) - Implement
HasQuantitiesif it supports quantities - Add registration function and handle in the main
polyscopecrate (polyscope/src/<structure>.rs) - Add GPU code in
polyscope/src/app/:- GPU initialization and buffer updates:
render_init.rs(shared by the window and the headless path) - Draw calls:
render_scene.rs - Pick resources and the pick pass:
render.rs - Screenshot and headless frame:
render_capture.rs
- GPU initialization and buffer updates:
- Add UI in
polyscope-ui/src/structure_ui.rs - Add tests
- Implement the
Quantitytrait:as_any,as_any_mut,name,structure_name,kind,is_enabled,set_enabled,build_ui,refresh,data_size. Overrideclear_gpu_resourcesif the quantity holds GPU data. - Add marker trait if needed (
VertexQuantity,FaceQuantity,EdgeQuantity,CellQuantity) - Add GPU rendering fields:
render_data,init_gpu_resources(),update_uniforms(),render_data() - Add active accessor on parent structure (e.g.,
active_vertex_vector_quantity()) - Call
auto_scale()for vector-like quantities in the method of the structure that adds them - Add init/update/draw code in
polyscope/src/app/(see the file list above) - Add the add method on the parent structure (e.g.,
mesh.add_vertex_vector_quantity(...)) and, if useful, on its handle - Add UI controls in
polyscope-ui
A new quantity starts disabled, as in C++ Polyscope.
GPU data from structure geometry must be transformed by the model matrix. When you add a rendered quantity:
- Include
model: mat4x4<f32>in shader uniform struct - Apply in vertex shader: positions (w=1), directions (w=0)
- Pass
structure.transform()every frame viaupdate_uniforms() - Never assume GPU-baked positions are in world space — they are local/object space
If a quantity ignores the model matrix, it stays in place when the user moves or rotates the structure with the gizmo. The pick shaders need the model matrix too.
All shaders are WGSL (WebGPU Shading Language). No geometry shader support in wgpu — use compute shaders and instancing instead.
- Group 0: Per-object uniforms (model matrix, colors, parameters)
- Group 1: Slice planes / reflection uniforms
- Group 2: Matcap textures (4-channel blend or single-texture)
The lit scene shaders define and call light_surface_matcap(normal, color), with the matcap textures of Group 2, for consistent lighting. WGSL has no include mechanism, so each shader has its own copy of this function.
Always set explicit min_binding_size (via NonZeroU64) rather than None. This works around wgpu Issue #7359 where late validation can cross-contaminate between pipelines.
WGSL alignment caveat: vec3<T> aligns to 16 bytes, not 12. Padding fields must use scalar types (e.g., _pad0: u32) rather than vec3<u32> to match Rust #[repr(C)] struct sizes.
The built-in colormaps come from the C++ Polyscope tables. The data flow:
scripts/extract_colormaps.pyreads theCM_*tables insrc/render/color_maps.cppof the C++ source (~/repo/polyscope).- The script writes one file per colormap to
crates/polyscope-render/data/colormaps/<name>.bin. Each file holds RGB triples of little-endianf32values. crates/polyscope-render/src/color_maps.rsembeds the files withinclude_bytes!inBUILTIN_COLOR_MAP_TABLES, in the C++ order.BUILTIN_COLOR_MAP_NAMESholds the names.ColorMapRegistry::new()decodes the tables. TheRenderEngineowns one registry (engine.color_maps).- Each frame, the structures receive the registry in
update_gpu_buffers. A scalar quantity computes one color per element on the CPU withcompute_colors. Volume grid gridcubes upload the colormap as a 1D texture instead.
The quantity panels offer BUILTIN_COLOR_MAP_NAMES. The volume grid panel offers the names of the registry.
To add a built-in colormap:
- Add the C++ table name and the user name to
COLORMAPSinscripts/extract_colormaps.py. - Run
python3 scripts/extract_colormaps.py. - Add the entry to
BUILTIN_COLOR_MAP_TABLESand change the array length. - Run
cargo test -p polyscope-render.
For label+widget rows (sliders, drag values, color pickers), always use egui::Grid with 2 columns instead of ui.horizontal + ui.add_sized. Grid auto-sizes the label column and left-aligns labels. For buttons, ui.add_sized with a fixed width is fine.
Global state is managed via OnceLock<RwLock<Context>>:
with_context(|ctx| ...)— Read accesswith_context_mut(|ctx| ...)— Write access
Context::options (Options) is the source of truth for the render look: background, ground plane, transparency, SSAA, SSAO and the frame rate limit. The UI panels read these values each frame and write their edits back. A saved view (ViewState) copies the render-look fields, but not max_fps.
The windowed frame in app/render.rs follows this order:
- Compute pass for curve network tubes
- Shadow map pass (surface meshes)
- Slice plane visualization
- Ground plane (stencil, ground, reflected structures)
- Depth prepass of surface meshes (Pretty transparency only)
- Main geometry pass (points, curves, vectors, camera views)
- Surface mesh and volume mesh pass (HDR color + normal G-buffer), with volume grid isosurfaces and gridcubes
- Depth peel transparency (multi-pass, Pretty transparency only)
- SSAO (only with an SSAA factor of 1)
- Tone mapping (with the SSAA downsample)
- egui overlay
- Pick pass (GPU picking; a click reads one pixel of it)
The pick pass comes last, so it uses the uniforms and the tube geometry of the image on the screen. The frame preparation writes the pick uniforms together with the render uniforms; the pick pass writes only the first pick index of each structure.
// C++ Polyscope
polyscope::PointCloud* pc = polyscope::registerPointCloud("my points", points);
pc->addScalarQuantity("height", heights);
pc->setPointRadius(0.01);// polyscope-rs
register_point_cloud("my points", points);
with_point_cloud("my points", |pc| {
pc.add_scalar_quantity("height", heights);
pc.set_point_radius(0.01);
});- Pointers to handles: C++ uses raw pointers; Rust uses handle structs or closure-based
with_*access - Error handling: C++ exceptions → Rust
Result<T, E> - Vector types:
glm::vec3→glam::Vec3 - Face format: polyscope-rs accepts
UVec3(triangles),[u32; 3], orVec<Vec<u32>>(polygons) viaIntoFaceList - Initialization:
init()returnsResult, use?orunwrap() - Thread safety: Global state uses
RwLock - Camera: no global camera functions; use
apply_view_stateor the UI - Front direction: the direction that the camera looks along (C++: the side where the camera sits)
Feature Status & Roadmap lists the C++ API that has no Rust equivalent yet.
- Local C++ Polyscope source:
~/repo/polyscope— check for implementation details - C++ Polyscope repo: https://github.com/nmwsharp/polyscope
- Polyscope docs: https://polyscope.run
- Design document:
docs/plans/2026-01-21-polyscope-rs-design.md