Shader Composition
PNGine composes shaders at compile time without adding a module syntax to WGSL.
Every .wgsl file remains usable by ordinary WGSL tools. SJON names the fragments
and import graph; the compiler assembles, validates, specializes, reflects, and
minifies each resulting module as one unit.
Import one fragment into a pass
Section titled “Import one fragment into a pass”This complete program declares a helper in (wgsl …) and imports it by name:
(wgsl :name palette :code """fn warmPalette(t: f32) -> vec3f { return vec3f( 0.8 + 0.2 * cos(t * 6.2831853), 0.6 + 0.4 * sin(t * 6.2831853), 0.9 - 0.3 * cos(t * 6.2831853) );}""")
(pass-graph (pass :name main :imports [palette] :code """ @fragment fn fs(@builtin(position) pos: vec4f) -> @location(0) vec4f { let uv = pos.xy / vec2f(pngine.width, pngine.height); return vec4f(warmPalette(uv.x + pngine.time * 0.1), 1.0); } """))The fragment is not a GPU object and has no data blob of its own. The payload holds one assembled module containing the helper and the pass’s code. Dead-code elimination removes declarations the entry point never reaches, after validation has checked the whole assembled module.
Fragments declare no @vertex, @fragment, or @compute entry points. A fragment
imported by a pass also declares no bindings because the pass prelude owns binding
numbering. A (shader-module …) may import resource-bearing fragments and bind them
through the document’s ordinary bind groups.
Use plain WGSL files
Section titled “Use plain WGSL files”Move either a fragment or a complete module to a file without changing the language:
(wgsl :name mesh :file "./lib/mesh.wgsl" :imports [camera])(shader-module :name lit :file "./lit.wgsl" :imports [mesh])Paths are relative and may not climb out of the compile root. In the browser compiler, which has no filesystem, the host supplies the file table and the compiler reports missing inputs through its loader protocol. The same assembly rules apply in both environments.
Imports are transitive and acyclic. Each fragment appears once per assembled module, and whole-module minification renames consistently across those boundaries.
Holes are deliberate parameters
Section titled “Holes are deliberate parameters”A fragment may call something it does not declare. :requires documents and checks
those open names:
(wgsl :name fbm :requires [noise2] :code """fn fbm(p: vec2f) -> f32 { var sum = 0.0; var amplitude = 0.5; var q = p; for (var i = 0; i < 5; i++) { sum += amplitude * noise2(q); q *= 2.0; amplitude *= 0.5; } return sum;}""")Each root supplies its own noise2. This is useful for noise families, scalar
fields, material functions, and stage variants: the library owns the algorithm and
the root owns the varying function. Validation runs before dead-code elimination,
so every declared hole must resolve even in an unreached function. Split a library
more finely if that rule pulls in unwanted requirements.
The validator compares the declared holes with the fragment’s actual unresolved
identifiers and warns in both directions. --strict turns those advisories into a
nonzero exit for CI.
Specialize constants per module
Section titled “Specialize constants per module”A (constant …) resolves a WGSL override at compile time by turning it into a
const before validation and minification:
(wgsl :name fbm :code """override OCTAVES: u32 = 5u;// ...""")
(pass-graph (pass :name main :imports [fbm] (constant :name OCTAVES :value 4) :code """ // fragment entry point """))Constants can appear on pipeline stages and directly on passes. Their values are checked against the override’s WGSL type. This is compile-time specialization, not runtime pipeline recompilation. It fixes values and makes names safe to minify, but it does not fold an untaken shader branch; use different fragments when the code itself must differ.
Extend a whole document
Section titled “Extend a whole document”(import …) compiles another .sjon document ahead of the current one, with
rebindings for its consumed constants and fragments:
(import :file "./shadow_scene.sjon" (define :name SHADOW_SIZE :value 1024) (wgsl :name shadowPcf :code """ // replacement filtering implementation """))Only a consumed form can be rebound: (define …) and (wgsl …). Emitting resources
such as textures, pipelines, and frames cannot be replaced by redeclaring their
names. The imported document remains a valid program on its own, and composition is
acyclic and deduplicated.
This gives a base artifact and a variant without copying its pipelines, resources, or shader library. It is source-level reuse; the result is still a new complete PNG.
Compose rendering graphs
Section titled “Compose rendering graphs”For geometry that needs multiple stage variants, (draw-graph …) expands a stage
by geometry table into ordinary pipelines and passes. A named pass graph or draw
graph becomes a step group that a hand-written frame can perform beside another
graph. The order is written in the frame, not inferred from lowering order.
See the worked scene-plus-post-processing example in
examples/graph_compose.sjon.
Related
Section titled “Related”(shader-module …)- Root modules, files, imports, and reflection(pass-graph …)- Fragment imports and per-pass constants(define …)- Constants and bounded expressions- Frame programs - Compose the resulting work into a schedule