inject_swizzle_assignments

Function inject_swizzle_assignments 

Source
pub fn inject_swizzle_assignments(src: &str, table: &SymbolTable) -> String
Expand description

Rewrite target.<swizzle> <op>= <rhs>; into a full-vector reconstruction. WGSL refuses multi-component swizzles on the LHS of an assignment (invalid left-hand side of assignment); HLSL allows them freely. The dominant cases in test-presets-200/:

  • ret.xy *= diff;
  • ret.xyz = tex2D(...).xyz;
  • ret.zy /= diff2;

Rewrite shape (for target: vec3<f32>):

ret.xy = expr;ret = vec3<f32>((expr).x, (expr).y, ret.z); ret.xy *= expr;ret = vec3<f32>(ret.x * (expr).x, ret.y * (expr).y, ret.z);

Constraints:

  • target must resolve to a known vec3/vec4 in table. Skip otherwise.
  • Swizzle length must be 2..=target_size, all distinct, all in {x,y,z,w}.
  • Reordering swizzles (yx, zy) are handled — each target component picks the matching swizzle slot.
  • Scalar RHS is broadcast (each lane uses the same (expr)).
  • RHS with unknown type is treated as a vec matching the swizzle width (the dominant case in real shaders); the resulting WGSL still validates because positional swizzle access on a vec is well-typed.