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

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 names
  • cargo run -p example -- source {name} — print the generated WGSL for the named example

Example catalog

NameDemonstratesPage
hello_triangleA "hello world" vertex+fragment shader with uniforms and builtinshello-triangle
structsUser-defined structs as fragment inputs/outputs with locations and builtinsstructs
compute_shaderA compute shader with storage buffers and the get!/get_mut! macroscompute-shader
matrix_exampleMatrix types and constant constructors (mat2x2f, mat3x3f, mat4x4f)matrix
impl_exampleStruct impl blocks: associated constants and methodsimpl
enum_exampleLimited enum support translated to u32 aliases and constantsenum
binary_ops_exampleAll supported binary operators (arithmetic, comparison, logical, bitwise)binary-ops
for_loop_exampleFor-loops with range expressions and #[wgsl_allow(non_literal_loop_bounds)]for-loop
while_loop_example / loop_examplewhile loops and infinite loop statementswhile-loop
if_example / break_example / return_example / switch_exampleControl flow: if, break, explicit return, and match/switchcontrol-flow
runtime_array_exampleRuntime-sized arrays (RuntimeArray<T>) in storage buffersruntime-array
ptr_examplePointer types (ptr!) in function parametersptr
atomic_exampleAtomic types and workgroup variables in compute shadersatomics
texture_exampleTextures, samplers, and texture builtin functionstexture
bitcast_examplebitcast builtin functions for type reinterpretationbitcast
packing_examplePacking/unpacking builtins (pack4x8snorm, etc.)packing
advanced_numeric_examplemodf, frexp, and ldexp builtinsadvanced-numeric
matrix_builtin_exampledeterminant and transpose matrix builtinsmatrix-builtin
synchronization_exampleworkgroupBarrier, storageBarrier, workgroupUniformLoadsynchronization
macro_rules_definitionsmacro_rules! and derive macros (stripped from WGSL output)macro-rules
slab_read_writeReading/writing structs from u32 "slabs" via slab macrosslab-read-write
derivative_exampleAll 9 WGSL derivative builtin functions in a fragment shaderderivatives
discard_exampleThe discard!() statement for discarding fragmentsdiscard
generic_functionsGeneric functions with monomorphizationgeneric-functions
trait_impl_exampleTrait definitions and impl blocks resolved via monomorphizationtrait-impls
renderer_specializationA full renderer specialized via traits and turbofishrenderer-specialization
renderer_specialization_simpleA second specialization of the shared renderer pipelinerenderer-specialization-simple
generic_structsGeneric structs with #[wgsl(skip_validation)] (known bug)generic-structs
shared_inter_stageA single struct shared between vertex and fragment stagesshared-inter-stage
phantom_dataPhantomData<T> marker fields (retained in IR, omitted from WGSL)phantom-data