hoist_global_vars

Function hoist_global_vars 

Source
pub(crate) fn hoist_global_vars(body: &mut String, hlsl_src: &str) -> String
Expand description

Hoist top-level user globals to module scope.

Scans body for var NAME: TYPE [= INIT]; lines and, when NAME matches a top-level Item::GlobalVar from hlsl_src and the declared type matches that global’s type, emits a module-scope var<private> NAME: TYPE; declaration. The body line is rewritten to a bare NAME = INIT; (assignment runs inside fs_main so it can still reference q1..q32 and other var<private> state seeded by the wrapper) or removed entirely when there’s no initializer.

The TYPE match is what protects a corpus shape like float2 rs2,rs0,rss,uv2; … shader_body { float3 uv2 = …; }: at top level uv2 is vec2<f32> (the hoist target), but the in-body var uv2: vec3<f32> = …; line is a shadowing re-declaration of a different type and must keep its local var form so the in-body uv2 ends up as a vec3<f32> local. Without this guard, the bare regex match would strip the var from the shadowing decl and the assignment would silently target the vec2 module-scope global, producing downstream InvalidStoreTypes errors.

Returns the joined module-scope declarations ("" if none).