Skip to content

Commit 882da8f

Browse files
committed
Feature: Even more cases where math results can be precalculated.
1 parent 9b95a10 commit 882da8f

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

ShaderShrinker/Shrinker.Parser/Optimizations/PerformArithmeticExtension.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,12 @@ private static bool IsSafeToPerformMath(SyntaxNode lhsNumNode, SymbolOperatorTok
354354
case TokenExtensions.MathSymbolType.AddSubtract:
355355
var prevSymbol = (lhsNumNode.Previous as GenericSyntaxNode)?.Token as SymbolOperatorToken;
356356
var nextSymbol = (rhsNumNode.Next is RoundBracketSyntaxNode ? rhsNumNode.Next.Next : rhsNumNode.Next)?.Token as SymbolOperatorToken;
357+
358+
if (prevSymbol?.GetMathSymbolType() == TokenExtensions.MathSymbolType.Unknown)
359+
prevSymbol = null;
360+
if (nextSymbol?.GetMathSymbolType() == TokenExtensions.MathSymbolType.Unknown)
361+
nextSymbol = null;
362+
357363
return new[] { prevSymbol, nextSymbol }.Where(o => o != null).All(o => o.GetMathSymbolType() == symbolType);
358364

359365
default:

ShaderShrinker/UnitTests/TestFiles/SimplifiedReference/Subway.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Processed by 'GLSL Shader Shrinker' (Shrunk by 1,318 characters)
1+
// Processed by 'GLSL Shader Shrinker' (Shrunk by 1,322 characters)
22
// (https://github.com/deanthecoder/GLSLShaderShrinker)
33

44
#define MY_GPU_CAN_TAKE_IT
@@ -79,7 +79,7 @@ vec2 sdStep(vec3 p) {
7979
float d2,
8080
d1 = sdBox(p, vec3(1.4, .02, .02));
8181
if (d1 > 1.) return vec2(d1, 3.5);
82-
p.y += .16 + .02;
82+
p.y += .18;
8383
d2 = sdBox(p, vec3(1.385, .145, .02));
8484
d1 = min(d1, sdBox(p - vec3(0, 0, .015), vec3(1.4, .16, .02)));
8585
p.yz += vec2(.18, .32);

ShaderShrinker/UnitTests/TestFiles/SimplifiedReference/Temple.glsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Processed by 'GLSL Shader Shrinker' (Shrunk by 408 characters)
1+
// Processed by 'GLSL Shader Shrinker' (Shrunk by 412 characters)
22
// (https://github.com/deanthecoder/GLSLShaderShrinker)
33

44
#define SKYCOL vec3(.6, .8, .9)
@@ -48,12 +48,12 @@ float sdTempleColumn(vec3 p) {
4848
d = sdColumn(p, r);
4949
{
5050
vec3 q = p;
51-
q.y -= 4. + .16;
51+
q.y -= 4.16;
5252
d = min(d, sdCube(q, vec3(.7, .16, .7)) - .05);
5353
}
5454
{
5555
vec3 q = p;
56-
q.y += 4. - .4;
56+
q.y += 3.6;
5757
d = min(d, sdCube(q, vec3(.7, .16, .7)) - .05);
5858
}
5959
return d;

ShaderShrinker/UnitTests/VectorArithmeticTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ public void CheckArithmeticWithVectorAndScalar(
238238
"vec2 main() { return 1. + 3. * vec2(1, 2); }",
239239
"vec2 main() { return 1. + vec2(1, 2) * 3.; }",
240240
"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,
241+
"vec3 main() { vec3 v = vec3(.4); float k = 1.; return (v + vec3(.6, .8, 1) * .5 * k) / (1. + k); }",
242+
"p.xy -= 5. * vec2(0.64, 0.7) - 2.5;")] string code,
242243
[Values("vec2 f = vec2(4.4, 5.5);",
243244
"vec3 f = vec3(-3.3, -2.2, -1.1);",
244245
"vec4 f = vec4(2.42);",
@@ -258,7 +259,8 @@ public void CheckArithmeticWithVectorAndScalar(
258259
"vec2 main() { return vec2(4, 7); }",
259260
"vec2 main() { return vec2(4, 7); }",
260261
"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)
262+
"vec3 main() { vec3 v = vec3(.4); float k = 1.; return (v + vec3(.3, .4, .5) * k) / (1. + k); }",
263+
"p.xy -= vec2(.7, 1);")] string expected)
262264
{
263265
var lexer = new Lexer();
264266
lexer.Load(code);

0 commit comments

Comments
 (0)