Skip to content

Commit

Permalink
Fix bug 'not a swizzle' with custom struct (#284)
Browse files Browse the repository at this point in the history
Fixes #282
  • Loading branch information
laurentlb committed Apr 30, 2023
1 parent 050dfe7 commit 9266327
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
8 changes: 3 additions & 5 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ let private swizzleIndex = function
| 'a' | 'w' | 'q' -> 3
| c -> failwithf "not a swizzle (%c) " c

let private renameSwizzle field =
field |> String.map (fun c -> options.canonicalFieldNames.[swizzleIndex c])

let renameField field =
if isFieldSwizzle field then renameSwizzle field
if isFieldSwizzle field then
field |> String.map (fun c -> options.canonicalFieldNames.[swizzleIndex c])
else field

// Remove useless spaces in macros
Expand Down Expand Up @@ -250,7 +248,7 @@ let private simplifyVec (constr: Ident) args =

// vec3(a.x, b.xy) => vec3(a.x, b)
let rec dropLastSwizzle = function
| [Dot (expr, field) as last] ->
| [Dot (expr, field) as last] when isFieldSwizzle field ->
match [for c in field -> swizzleIndex c] with
| [0] | [0; 1] | [0; 1; 2] | [0; 1; 2; 3] -> [expr]
| _ -> [last]
Expand Down
2 changes: 1 addition & 1 deletion tests/commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
--no-remove-unused --format indented --no-renaming -o tests/unit/forward_declaration.frag.expected tests/unit/forward_declaration.frag
--no-remove-unused --format indented --no-renaming --move-declarations -o tests/unit/conditionals.frag.expected tests/unit/conditionals.frag
--no-renaming --no-inlining --format indented -o tests/unit/pi.frag.expected tests/unit/pi.frag
--no-renaming --no-inlining --format indented -o tests/unit/vectors.frag.expected tests/unit/vectors.frag
--no-remove-unused --no-renaming --no-inlining --format indented -o tests/unit/vectors.frag.expected tests/unit/vectors.frag
--no-renaming --format indented -o tests/unit/deadcode.frag.expected tests/unit/deadcode.frag
--no-renaming --no-inlining --format indented -o tests/unit/augmented.frag.expected tests/unit/augmented.frag
--no-remove-unused --no-renaming -o tests/unit/loop.frag.expected tests/unit/loop.frag
Expand Down
13 changes: 12 additions & 1 deletion tests/unit/vectors.frag
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,15 @@ float withExtraComponents() {
return v2.x + v3.x + v4.x;
}

void main() { swizzles(); constructor(); constructors(); withExtraComponents(); }
struct S{
vec2 p1;
vec2 cp1;
vec2 cp2;
vec2 p2;
};

vec2 calc(S points, float t) {
// #282 - not a swizzle
vec4 m1m2 = mix(vec4(points.p1, points.cp1), vec4(points.cp1, points.cp2), t);
return m1m2.xy;
}
10 changes: 4 additions & 6 deletions tests/unit/vectors.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ float withExtraComponents()
vec3 v3=vec3(1,2,v2);
vec4 v4=vec4(v2,v3);
return v2.x+v3.x+v4.x;
}
void main()
}struct S{vec2 p1;vec2 cp1;vec2 cp2;vec2 p2;};
vec2 calc(S points,float t)
{
swizzles();
constructor();
constructors();
withExtraComponents();
vec4 m1m2=mix(vec4(points.p1,points.cp1),vec4(points.cp1,points.cp2),t);
return m1m2.xy;
}

0 comments on commit 9266327

Please sign in to comment.