Module prelude

Module prelude 

Source
Expand description

Standard WGSL prelude injected ahead of every translated user shader.

The prelude exposes the MilkDrop 2 uniform set (audio, time, aspect, q1..q32, etc.) under a single uniforms binding so warp and comp HLSL shaders can be translated to WGSL with a known reference target.

Two artefacts must stay in lockstep:

  • ShaderUniforms — the Rust mirror, repr-C, Pod/Zeroable. The renderer fills it each frame from RenderState/MilkContext and uploads it via wgpu::Queue::write_buffer.
  • SHADER_UNIFORMS_WGSL — the WGSL struct declaration plus the @group(0) @binding(0) var<uniform> uniforms: ShaderUniforms; line. Concatenated to the front of every translated user shader before naga validates and the renderer compiles it.

Layout discipline (matches WGSL/std140-ish uniform alignment):

  • All scalar f32 fields packed at the top in groups of four (each group is one 16-byte slot).
  • All vec4<f32> fields after, contiguous (each is exactly 16 bytes, so no further padding is needed).
  • Trailing array<vec4<f32>, N> for q1..q32 — eight vec4 slots = 32 floats.

The size in bytes is asserted by the tests::layout_is_byte_for_byte_compatible test in this module against a naga-parsed WGSL module: any drift between the Rust struct and the WGSL declaration trips the test.

Structs§

ShaderUniforms
Rust mirror of the WGSL ShaderUniforms struct.

Constants§

SHADER_Q_CHANNELS
Number of q* channels exposed to user shaders. MilkDrop 2 ships q1..q32; the engine tracks 64 internally but only the first 32 cross into shader-land.
SHADER_UNIFORMS_WGSL
WGSL struct declaration mirroring ShaderUniforms plus the standard @group(0) @binding(0) uniform binding.

Functions§

with_prelude
Build a full prelude string by appending the user’s shader body to the standard ShaderUniforms declaration. body is expected to already be translated to WGSL.