From be2c93a4d56633f9b17f22c94997ff6d42b54536 Mon Sep 17 00:00:00 2001 From: Laurent Le Brun Date: Sat, 18 Mar 2023 14:36:05 +0100 Subject: [PATCH] Apply vector simplification only when the number of arguments matches vec3(x,x,x) => vec3(x) vec4(v2,v2) => vec4(v2,v2) --- src/rewriter.fs | 6 ++++-- tests/unit/vectors.frag | 9 ++++++--- tests/unit/vectors.frag.expected | 9 ++++++--- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/rewriter.fs b/src/rewriter.fs index 62c09d1c..97214b67 100644 --- a/src/rewriter.fs +++ b/src/rewriter.fs @@ -189,7 +189,9 @@ let private simplifyOperator env = function // Simplify calls to the vec constructor. -let private simplifyVec constr args = +let private simplifyVec (constr: Ident) args = + let vecSize = try int(constr.Name.[constr.Name.Length - 1]) - int '0' with _ -> 0 + // Combine swizzles, e.g. // vec4(v1.x, v1.z, v2.r, v2.t) => vec4(v1.xz, v2.xy) let rec combineSwizzles = function @@ -229,7 +231,7 @@ let private simplifyVec constr args = | _ -> allArgs let args = combineSwizzles args |> List.map useInts - let args = mergeAllEquals args args + let args = if args.Length = vecSize then mergeAllEquals args args else args FunCall (Var constr, args) let private simplifyExpr (didInline: bool ref) env = function diff --git a/tests/unit/vectors.frag b/tests/unit/vectors.frag index db421b69..9dcdcaca 100644 --- a/tests/unit/vectors.frag +++ b/tests/unit/vectors.frag @@ -12,8 +12,11 @@ vec4 constructor() { return vec4(1., 1e2, 2e3, 3e4); } -vec2 constructor2() { - return vec2(1e10, 1e10); +float constructors() { + vec2 v2 = vec2(1e10, 1e10); + vec3 v3 = vec3(v2, v2); + vec4 v4 = vec4(v3, v2); + return v2.x + v3.x + v4.x; } -void main() { swizzles(); constructor(); constructor2(); } +void main() { swizzles(); constructor(); constructors(); } diff --git a/tests/unit/vectors.frag.expected b/tests/unit/vectors.frag.expected index 3e7216b7..a1a27eb0 100644 --- a/tests/unit/vectors.frag.expected +++ b/tests/unit/vectors.frag.expected @@ -9,13 +9,16 @@ vec4 constructor() { return vec4(1,100,2e3,3e4); } -vec2 constructor2() +float constructors() { - return vec2(1e10); + vec2 v2=vec2(1e10); + vec3 v3=vec3(v2,v2); + vec4 v4=vec4(v3,v2); + return v2.x+v3.x+v4.x; } void main() { swizzles(); constructor(); - constructor2(); + constructors(); }