Examples
This section contains a catalog of example wgsl-rs modules. Each example demonstrates a specific feature of the transpiler, showing the Rust source and the generated WGSL output.
Running examples
The example crate provides two subcommands for inspecting examples:
cargo run -p example -- show— list all available example namescargo run -p example -- source {name}— print the generated WGSL for the named example
Example catalog
| Name | Demonstrates | Page |
|---|---|---|
hello_triangle | A "hello world" vertex+fragment shader with uniforms and builtins | hello-triangle |
structs | User-defined structs as fragment inputs/outputs with locations and builtins | structs |
compute_shader | A compute shader with storage buffers and the get!/get_mut! macros | compute-shader |
matrix_example | Matrix types and constant constructors (mat2x2f, mat3x3f, mat4x4f) | matrix |
impl_example | Struct impl blocks: associated constants and methods | impl |
enum_example | Limited enum support translated to u32 aliases and constants | enum |
binary_ops_example | All supported binary operators (arithmetic, comparison, logical, bitwise) | binary-ops |
for_loop_example | For-loops with range expressions and #[wgsl_allow(non_literal_loop_bounds)] | for-loop |
while_loop_example / loop_example | while loops and infinite loop statements | while-loop |
if_example / break_example / return_example / switch_example | Control flow: if, break, explicit return, and match/switch | control-flow |
runtime_array_example | Runtime-sized arrays (RuntimeArray<T>) in storage buffers | runtime-array |
ptr_example | Pointer types (ptr!) in function parameters | ptr |
atomic_example | Atomic types and workgroup variables in compute shaders | atomics |
texture_example | Textures, samplers, and texture builtin functions | texture |
bitcast_example | bitcast builtin functions for type reinterpretation | bitcast |
packing_example | Packing/unpacking builtins (pack4x8snorm, etc.) | packing |
advanced_numeric_example | modf, frexp, and ldexp builtins | advanced-numeric |
matrix_builtin_example | determinant and transpose matrix builtins | matrix-builtin |
synchronization_example | workgroupBarrier, storageBarrier, workgroupUniformLoad | synchronization |
macro_rules_definitions | macro_rules! and derive macros (stripped from WGSL output) | macro-rules |
slab_read_write | Reading/writing structs from u32 "slabs" via slab macros | slab-read-write |
derivative_example | All 9 WGSL derivative builtin functions in a fragment shader | derivatives |
discard_example | The discard!() statement for discarding fragments | discard |
generic_functions | Generic functions with monomorphization | generic-functions |
trait_impl_example | Trait definitions and impl blocks resolved via monomorphization | trait-impls |
renderer_specialization | A full renderer specialized via traits and turbofish | renderer-specialization |
renderer_specialization_simple | A second specialization of the shared renderer pipeline | renderer-specialization-simple |
generic_structs | Generic structs with #[wgsl(skip_validation)] (known bug) | generic-structs |
shared_inter_stage | A single struct shared between vertex and fragment stages | shared-inter-stage |
phantom_data | PhantomData<T> marker fields (retained in IR, omitted from WGSL) | phantom-data |