Skip to content

RustLayout Architecture

RustLayout is a clean-room, 100% pure-Rust (edition = "2024", rust-version = "1.100", unsafe_code = "deny" at workspace root) IC layout viewing and verification engine designed for gigabyte-scale GDSII, .gds.gz, and SEMI P39 OASIS databases.

1. Crate Dependency Graph

rustlayout-cli
 ├── rustlayout-app       (winit 0.30 + wgpu 30 + egui 0.37 + egui_dock 0.22 desktop workbench)
 │    └── rustlayout-render
 ├── rustlayout-render    (GUI-agnostic wgpu engine: headless PNG, embeddable surface, XOR diff)
 │    ├── rustlayout-style (KLayout .lyp XML codec, 32x32 GPU stipples, PDK presets)
 │    └── rustlayout-db
 ├── rustlayout-db        (SoA arenas, Hilbert sort, 128B BvhNode4 HLOD, picker, .rlay mmap cache)
 │    └── rustlayout-gds
 ├── rustlayout-gds       (Two-phase parallel GDSII, .gds.gz, SEMI P39 OASIS + CBLOCK inflate)
 │    └── rustlayout-core
 └── rustlayout-core      (DBU i32/i64 geometry, D4/affine transforms, CameraState, ResourceLimits)

rustlayout-benchgen       (Deterministic canonical, hostile-fuzz, and multi-GB stress GDS writer)

rustlayout-render has zero dependency on winit or egui and accepts externally owned wgpu handles via GpuContext::from_external(instance, adapter, device, queue) so RustEDA Studio or headless CI tools can embed it directly.

2. Two-Phase Parallel Ingestion (rustlayout-gds)

  • GDSII:
  • Phase 0 (Streaming Scan & Intra-Cell Chunking): Files < 32 MiB use zero-copy read-only mmap; files >= 32 MiB on Unix stream sequentially through a 4 MiB buffer to locate cell boundaries and split giant flat cells (> 16 MiB, e.g., 2 GB dummy metal fill) into ~8 MiB ENDEL-aligned chunks so all CPU cores stay saturated.
  • Phase 1 (Rayon Parallel Decode): Worker threads read chunks via Unix pread (FileExt::read_exact_at, preventing the raw multi-GB GDS file from inflating process RSS), run a 64-byte canonical rectangle fast-path directly into compact RectI32 (16 B/rect), preserve AREFs strictly unexpanded, and merge one layer at a time to keep peak RAM at 0.49×–0.61× file size on multi-GB layouts.
  • Hierarchy & Safety: Iterative 3-color DFS rejects cyclic SREF/AREF graphs cleanly, enforces ResourceLimits (max_file_bytes, max_cells, max_hierarchy_depth, max_vertices_per_shape), and propagates exact RectI64 bounding boxes bottom-up in O(1) per AREF.
  • .gds.gz: Detected automatically by gzip magic 0x1F 0x8B and decompressed with pure-Rust flate2 (rust_backend / miniz_oxide) bounded by ResourceLimits::max_file_bytes.
  • SEMI P39 OASIS (.oas): Detected automatically by %SEMI-OASIS\r\n. Top-level CBLOCK (record 34) compressed spans are extracted and inflated in parallel with rayon + pure-Rust RFC1951 raw deflate. Regular placement repetitions (types 1, 2, 3, 8, 9) map directly into native unexpanded ArefRecords.

3. Spatial Database, Hilbert 4-Wide BVH & .rlay Cache (rustlayout-db)

  • Hilbert Spatial Locality: Every rectangle bucket is sorted along a 32-bit Hilbert curve using a low-scratch parallel 12-bit MSB radix sort, grouping spatially adjacent metal tracks into contiguous 1 MiB (65,536 rect) GPU chunks.
  • 128-Byte Cache-Aligned BvhNode4:
  • Cache line 0 (0..64 B): SoA child bounding boxes (min_x[4], min_y[4], max_x[4], max_y[4]).
  • Cache line 1 (64..128 B): child_start[4], child_len[4] (MSB leaf tag), max_extent[4] (largest individual shape dimension in subtree so sub-pixel dummy fill collapses to HLOD while long power rails stay sharp), and non-inflating density[4] (u8 computed from an 8×8 u64 coverage bitmask + exact area summation).
  • O(1) AREF Clamping: clamp_aref_indices inverts the 2×2 world-space lattice matrix across all four frustum corners with conservative ±1 padding, achieving 100% recall on arbitrary rotated/mirrored/skewed 32767×32767 arrays without ever expanding them.
  • Zero-Copy .rlay Snapshot (RLAY0001): Serializes metadata plus 64-byte-aligned flat arenas (rects, bvh_nodes, points) protected by xxHash3-64 checksums. Reopening via read-only mmap maps arenas directly as &[RectI32] / &[BvhNode4] / &[PointI32] in < 0.5 ms.

4. GPU Rendering Pipeline (rustlayout-render)

  • Exact DBU Coordinate Precision (±1e9 DBU):
  • Each draw batch computes a camera-centered integer local_pivot: [i32; 2] and residual f32 world offset on the CPU in f64/i64.
  • In WGSL, local i32 coordinates are subtracted from local_pivot in integer arithmetic and split into (hi16, lo16) signed 16-bit pairs before multiplying by the 2×2 transform matrix — eliminating f32 mantissa loss even at 100 nm zoom on a 100 mm wafer.
  • Vertex-Pulled & Instanced Draw Paths:
  • RECT_MASK_WGSL: pulls RectI32 directly from 16 B/rect storage slabs, supports multi-SREF instance batches and native unexpanded AREF (col, row) stride evaluation directly on GPU, and renders crisp analytical 1px borders.
  • POLY_MASK_WGSL & EDGE_MASK_WGSL: renders lazily triangulated 1,024-polygon chunks (clean-room earcut with convex fast-path and keyhole-safe fallback) plus 1px outline edge quads.
  • HLOD_MASK_WGSL: draws coverage-calibrated density quads for sub-pixel BVH nodes, collapsed leaf cells, sub-pixel AREFs, and zero-fault deferred chunks while streaming.
  • CULL_COMPUTE_WGSL: runnable WGSL compute frustum + size culler producing indirect draw arguments.
  • Bounded VRAM & Mask Caching:
  • VramPool: frame-stamped LRU pool capped at --max-vram-mib with a per-frame staging budget (4 MiB/frame during interactive motion) and zero-fault HLOD fallback when streaming new chunks.
  • LayerMaskCache: per-layer RG8Unorm (R = fill, G = border) offscreen masks paired with a pre-allocated dynamic uniform ring for COMPOSITE_WGSL, yielding < 1.7 ms layer visibility/color/stipple/Z-order toggles at Retina 3024×1964 plus GPU copy_texture_to_texture pan tile-shift for smooth border-strip-only panning.
  • XOR_DIFF_WGSL: composites dual-layout masks in real time (A \ B Copper #F08A4B, B \ A Mint #4ADBC0, A ∩ B dim Slate).

5. Desktop Workbench & RustEDA Integration (rustlayout-app)

  • Zero Idle Power: Direct winit 0.30 ApplicationHandler switches to ControlFlow::Wait as soon as damped camera motion and background loaders settle (0% idle CPU/GPU).
  • Desktop Menu Bar & Native Dialogs: Complete 8-menu CAD bar (File, Edit, View, Layers, Tools, Diff, Window, Help) with native OS open/save dialogs (rfd), persistent Open Recent list, background .rlay cache export, GPU PNG viewport export, .lyp load/save, dynamic window titles, and dockable/toggleable side panels.
  • Zero-Copy Viewport & Minimap: LayoutRenderer renders directly into Rgba8Unorm wgpu::Textures registered with egui_wgpu::Renderer::register_native_texture — zero CPU pixel readback.
  • RustEDA Cross-Probing: Passing --watch-focus <path.json> watches a lightweight JSON payload (bbox_dbu, layers, highlights, message) so RustEDA, RustROAD DRC/antenna checks, or RustSTA critical paths can pan/highlight exact layout regions inside an open viewer window.

6. Audited unsafe Inventory

Workspace lint unsafe_code = "deny" is enforced across all 8 crates. Narrow #[expect(unsafe_code, reason = "...")] blocks exist solely in two audited categories: 1. Read-only memmap2::MmapOptions::map on opened files (rustlayout-gds/src/mmap.rs, rustlayout-db/src/cache.rs). 2. bytemuck::{Pod, Zeroable} implementations on compile-time-size-checked, plain integer/float #[repr(C)] structs (RectI32, RectI64, BvhNode4, and WGSL uniform/storage structs in rustlayout-render).