Skip to content

Commit 737b2dc

Browse files
committed
Feature: Pre-calculate the results of a float and vector.
Extended support. Now includes 'vec * a * b'.
1 parent 43af02a commit 737b2dc

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

ShaderShrinker/Shrinker.Parser/Optimizations/PerformArithmeticExtension.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,10 @@ public static bool PerformArithmetic(this SyntaxNode rootNode)
191191
var numberNode = symbolNode.Next;
192192
if (numberNode?.Token is FloatToken)
193193
{
194-
// Number must not be used in a following math operation.
195-
if (numberNode.Next?.Token is not SymbolOperatorToken nextMath || nextMath.GetMathSymbolType() != TokenExtensions.MathSymbolType.MultiplyDivide)
194+
// Number must not be used in a following math operation of a different category.
195+
if (numberNode.Next?.Token is not SymbolOperatorToken nextMath ||
196+
nextMath.GetMathSymbolType() == symbol.GetMathSymbolType() ||
197+
nextMath.GetMathSymbolType() == TokenExtensions.MathSymbolType.AddSubtract)
196198
{
197199
// Perform math on each bracketed value.
198200
var newCsv =

ShaderShrinker/UnitTests/VectorArithmeticTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ public void CheckArithmeticWithVectorAndScalar(
237237
"vec2 main() { return 2. / vec2(1, 2); }",
238238
"vec2 main() { return 1. + 3. * vec2(1, 2); }",
239239
"vec2 main() { return 1. + vec2(1, 2) * 3.; }",
240-
"vec2 main() { return vec2(21, 22) / 7.; }")] string code,
240+
"vec2 main() { return vec2(21, 22) / 7.; }",
241+
"vec3 main() { vec3 v = vec3(.4); float k = 1.; return (v + vec3(.6, .8, 1) * .5 * k) / (1. + k); }")] string code,
241242
[Values("vec2 f = vec2(4.4, 5.5);",
242243
"vec3 f = vec3(-3.3, -2.2, -1.1);",
243244
"vec4 f = vec4(2.42);",
@@ -256,7 +257,8 @@ public void CheckArithmeticWithVectorAndScalar(
256257
"vec2 main() { return vec2(2, 1); }",
257258
"vec2 main() { return vec2(4, 7); }",
258259
"vec2 main() { return vec2(4, 7); }",
259-
"vec2 main() { return vec2(21, 22) / 7.; }")] string expected)
260+
"vec2 main() { return vec2(21, 22) / 7.; }",
261+
"vec3 main() { vec3 v = vec3(.4); float k = 1.; return (v + vec3(.3, .4, .5) * k) / (1. + k); }")] string expected)
260262
{
261263
var lexer = new Lexer();
262264
lexer.Load(code);

0 commit comments

Comments
 (0)