Skip to content

Commit e09473a

Browse files
committed
Feature: Golf common built-in types using #define.
1 parent 93fc623 commit e09473a

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

ShaderShrinker/Shrinker.Parser/Optimizations/GolfExtensions.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111

1212
using System;
1313
using System.Collections.Generic;
14-
using System.IO;
1514
using System.Linq;
16-
using System.Net;
1715
using Shrinker.Parser.SyntaxNodes;
1816

1917
namespace Shrinker.Parser.Optimizations
@@ -42,6 +40,7 @@ public static void GolfDefineCommonTerms(this SyntaxNode rootNode)
4240
var userDefinedNames = rootNode.FindUserDefinedNames();
4341
var defineMap = new Dictionary<string, string[]>
4442
{
43+
{ "float", new[] { "F", "f", "_f" } },
4544
{ "abs", new[] { "A" } },
4645
{ "acos", new[] { "AC" } },
4746
{ "acosh", new[] { "ACH" } },
@@ -63,7 +62,7 @@ public static void GolfDefineCommonTerms(this SyntaxNode rootNode)
6362
{ "distance", new[] { "DST" } },
6463
{ "dot", new[] { "D" } },
6564
{ "faceforward", new[] { "FF" } },
66-
{ "floor", new[] { "F" } },
65+
{ "floor", new[] { "FL" } },
6766
{ "fract", new[] { "FC" } },
6867
{ "fwidth", new[] { "FW" } },
6968
{ "greaterThan", new[] { "GT" } },
@@ -122,13 +121,12 @@ public static void GolfDefineCommonTerms(this SyntaxNode rootNode)
122121

123122
var keywordNodes =
124123
rootNode.TheTree
125-
.OfType<GlslFunctionCallSyntaxNode>()
126-
.Where(o => defineMap.ContainsKey(o.Name))
124+
.Where(o => o is IRenamable or VariableDeclarationSyntaxNode && defineMap.ContainsKey(GetSupportedBuiltinName(o)))
127125
.ToList();
128126
foreach (var keyword in defineMap.Keys)
129127
{
130128
// How many occurrences are in the code?
131-
var nodes = keywordNodes.Where(o => o.Name == keyword).ToList();
129+
var nodes = keywordNodes.Where(o => GetSupportedBuiltinName(o) == keyword).ToList();
132130
if (nodes.Count <= 1)
133131
continue; // Not worth it.
134132

@@ -144,7 +142,13 @@ public static void GolfDefineCommonTerms(this SyntaxNode rootNode)
144142
continue; // Could replace with #define, but not worth it.
145143

146144
// We'll get a space saving - Replace the nodes...
147-
nodes.ForEach(o => o.Rename(replacement));
145+
foreach (var node in nodes)
146+
{
147+
if (node is IRenamable r)
148+
r.Rename(replacement);
149+
else
150+
((VariableDeclarationSyntaxNode)node).RenameType(replacement);
151+
}
148152

149153
// ...and add a #define.
150154
var existingDefine = rootNode.Children.OfType<PragmaDefineSyntaxNode>().FirstOrDefault();
@@ -153,6 +157,21 @@ public static void GolfDefineCommonTerms(this SyntaxNode rootNode)
153157
}
154158
}
155159

160+
private static string GetSupportedBuiltinName(SyntaxNode node)
161+
{
162+
if (node is IRenamable n)
163+
return n.Name;
164+
if (node is VariableDeclarationSyntaxNode decl)
165+
{
166+
var name = decl.VariableType.Content;
167+
if (name.StartsWith("const"))
168+
name = name.Substring(5).TrimStart();
169+
return name;
170+
}
171+
172+
return null;
173+
}
174+
156175
private static string NameWithoutDotSuffix(string name) => name.Split('.').First();
157176

158177
private static Dictionary<string, string> BuildGolfRenameMap(SyntaxNode rootNode)

ShaderShrinker/Shrinker.Parser/SyntaxNodes/VariableDeclarationSyntaxNode.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace Shrinker.Parser.SyntaxNodes
1919
{
2020
public class VariableDeclarationSyntaxNode : SyntaxNode
2121
{
22-
public TypeToken VariableType { get; }
22+
public TypeToken VariableType { get; private set; }
2323

2424
public IEnumerable<VariableAssignmentSyntaxNode> Definitions => Children.OfType<VariableAssignmentSyntaxNode>().ToList();
2525

@@ -56,5 +56,8 @@ public override string UiName
5656
public bool IsSameType(VariableDeclarationSyntaxNode other) =>
5757
VariableType.Content == other.VariableType.Content &&
5858
VariableType.IsConst == other.VariableType.IsConst;
59+
60+
public void RenameType(string newTypeName) =>
61+
VariableType = new TypeToken(VariableType.IsConst ? $"const {newTypeName}" : newTypeName);
5962
}
6063
}

ShaderShrinker/UnitTests/GolfTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public void CheckGolfingCodeNames(
9595
public void CheckGolfingCommonTerms(
9696
[Values(
9797
"void f() { vec3 a; vec3 b; }|void f() { vec3 a; vec3 b; }",
98+
"void f(vec3 q) { const vec3 a = vec3(1); vec3 b; vec3 c; vec3 d; vec3 e; vec3 f; vec3 g; vec3 h; vec3 i; }|#define v3 vec3 void f(v3 q) { const v3 a = v3(1); v3 b; v3 c; v3 d; v3 e; v3 f; v3 g; v3 h; v3 i; }",
9899
"float f(float a, float b) { return smoothstep(0.0, 1.0, a) + smoothstep(0.5, 1.5, b) + smoothstep(0.0, 1.0, a + b); }|#define S smoothstep float f(float a, float b) { return S(0.0, 1.0, a) + S(0.5, 1.5, b) + S(0.0, 1.0, a + b); }"
99100
)]
100101
string codeAndGolfed)

0 commit comments

Comments
 (0)