Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Design Decisions

This chapter curates the key architectural decisions behind wgsl-rs. The full narrative, including rejected alternatives and intermediate experiments, is in the DEVLOG.md.

Core Philosophy

DateDecision
2025-12-08User code is never translated. The macro is strictly additive; users write regular Rust and the macro only attaches WGSL metadata.
2025-12-27Rust type system catches all WGSL errors. Type mismatches surface as Rust compile errors, not runtime shader errors.
2025-12-27Macros for non-Rust WGSL constructs. uniform!, ptr!, and similar macros stand in for WGSL constructs that have no Rust analogue.

Types & Syntax

DateDecision
2025-12-08Swizzles are function calls, not field access, because the macro never alters user code and Rust field access cannot return a different type without translation.
2025-12-27Module imports are glob-only. use crate::module::*; keeps the transpiler's symbol resolution simple and avoids modeling Rust's visibility rules.
2026-01-29Pointer types via ptr! macro. WGSL pointers have no Rust equivalent; a dedicated macro expresses them without inventing reference semantics.
2026-01-31Atomic types and workgroup variables get first-class IR nodes and macros.
2026-02-11Variadic WGSL builtins are handled by multi-function name mapping rather than variadic generics.
2026-03-16discard!() is implemented via a thread-local flag rather than a control-flow primitive, preserving the "no code translation" rule.

Generics

DateDecision
2026-04-08Generic functions are monomorphized at macro time. Each concrete instantiation becomes a distinct WGSL function.
2026-04-17Generic structs are monomorphized. Same principle, applied to struct definitions.
2026-05-06Generic linkages use template modules that are instantiated per concrete type set.

IR & Extensions

DateDecision
2026-05-07IR crate for runtime type substitution, replacing earlier string-placeholder schemes.
2026-05-15WgslExtension trait and IR attributes provide a stable post-transpile hook for downstream code generation.
2026-05-18Bijective name mangling ensures Rust names map to unique WGSL names and back without collisions.
2026-07-18ir::Module is the AST; wgsl_rs::Source is the spec. The IR is the authoritative structure; Source is the user-facing handle.

Linkage

DateDecision
2026-05-29wgsl-rs-layout is a standalone extension crate, dogfooding the extension mechanism to compute WGSL memory layout.
2026-06-06Runtime wgpu linkage via IR traversal reflects bind groups and bindings by walking the IR rather than parsing generated WGSL text.

Generics & Monomorphization

DateDecision
2026-04-08Generic functions monomorphized at macro time. Each turbofish call-site produces a mangled, concrete WGSL function.
2026-04-17Generic structs monomorphized to concrete WGSL structs with mangled names.
2026-08-02Trait impls on complex types (e.g. impl Zeroable for [u32; 4]) transpile to mangled WGSL functions.
2026-08-04Generic trait impls on array types (impl<T: Trait> Trait for [T; N]) supported via monomorphizer widening (#133).
2026-08-04Const generics for u32/usize supported on functions, structs, impl blocks, and template entry points (#137). The substitution target is always a bare ident (stable Rust requires bare idents or literals), so no new IR variant is needed.
2026-08-05PhantomData<T> marker fields are retained in the IR (so extensions can see which type parameter each phantom slot binds) but omitted from the rendered WGSL (#138).
2026-08-06Storage texture support (texture_storage_*) added as Type::TextureStorage IR variant, with texel format markers and access mode traits (#140).
2026-08-07Extensible statement macros via Stmt::Macro passthrough — extensions claim macros via MACROS const and lower them in modify_ir, with compile-time E0080 safety check (#143).
2026-08-07Associated types in trait impls resolve to concrete WGSL types and emit alias declarations (#143).
2026-08-07Non-pub associated consts in trait impls — matches the existing exemption for trait-impl methods (#142).