@shaderfrog/core 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/dist/ast/manipulate.js +328 -0
  2. package/dist/ast/shader-sections.js +256 -0
  3. package/dist/context.js +230 -0
  4. package/dist/engine.js +209 -0
  5. package/dist/evaluate.js +27 -0
  6. package/dist/graph-types.js +7 -0
  7. package/dist/graph.js +381 -0
  8. package/dist/graph.test.js +168 -0
  9. package/dist/nodes/code-nodes.js +18 -0
  10. package/dist/nodes/core-node.js +9 -0
  11. package/dist/nodes/data-nodes.js +123 -0
  12. package/dist/nodes/edge.js +1 -0
  13. package/dist/nodes/engine-node.js +189 -0
  14. package/dist/parsers.js +213 -0
  15. package/dist/plugins/babylon/bablyengine.js +582 -0
  16. package/dist/plugins/babylon/importers.js +64 -0
  17. package/dist/plugins/babylon/index.js +2 -0
  18. package/dist/plugins/playcanvas/importers.js +28 -0
  19. package/dist/plugins/playcanvas/index.js +2 -0
  20. package/dist/plugins/playcanvas/playengine.js +510 -0
  21. package/dist/plugins/three/importers.js +15 -0
  22. package/dist/plugins/three/index.js +2 -0
  23. package/dist/plugins/three/threngine.js +495 -0
  24. package/dist/strategy/assignemntTo.js +26 -0
  25. package/dist/strategy/declarationOf.js +23 -0
  26. package/dist/strategy/hardCode.js +23 -0
  27. package/dist/strategy/index.js +38 -0
  28. package/dist/strategy/inject.js +122 -0
  29. package/dist/strategy/namedAttribute.js +48 -0
  30. package/dist/strategy/texture2D.js +83 -0
  31. package/dist/strategy/uniform.js +190 -0
  32. package/dist/strategy/variable.js +80 -0
  33. package/dist/stratgies.test.js +164 -0
  34. package/dist/util/ast.js +9 -0
  35. package/dist/util/ensure.js +7 -0
  36. package/dist/util/id.js +2 -0
  37. package/package.json +1 -1
@@ -0,0 +1,328 @@
1
+ var __read = (this && this.__read) || function (o, n) {
2
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
3
+ if (!m) return o;
4
+ var i = m.call(o), r, ar = [], e;
5
+ try {
6
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
7
+ }
8
+ catch (error) { e = { error: error }; }
9
+ finally {
10
+ try {
11
+ if (r && !r.done && (m = i["return"])) m.call(i);
12
+ }
13
+ finally { if (e) throw e.error; }
14
+ }
15
+ return ar;
16
+ };
17
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
18
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
19
+ if (ar || !(i in from)) {
20
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
21
+ ar[i] = from[i];
22
+ }
23
+ }
24
+ return to.concat(ar || Array.prototype.slice.call(from));
25
+ };
26
+ /**
27
+ * Utility functions to work with ASTs
28
+ */
29
+ import { parser, generate } from '@shaderfrog/glsl-parser';
30
+ import { visit, } from '@shaderfrog/glsl-parser/ast';
31
+ var log = function () {
32
+ var _a;
33
+ var args = [];
34
+ for (var _i = 0; _i < arguments.length; _i++) {
35
+ args[_i] = arguments[_i];
36
+ }
37
+ return (_a = console.log).call.apply(_a, __spreadArray([console, '\x1b[31m(core.manipulate)\x1b[0m'], __read(args), false));
38
+ };
39
+ export var findVec4Constructor = function (ast) {
40
+ var parent;
41
+ var visitors = {
42
+ function_call: {
43
+ enter: function (path) {
44
+ var _a, _b;
45
+ if (((_a = path.node.identifier) === null || _a === void 0 ? void 0 : _a.specifier).token === 'vec4') {
46
+ parent = (_b = path.findParent(function (p) { return 'right' in p.node; })) === null || _b === void 0 ? void 0 : _b.node;
47
+ path.skip();
48
+ }
49
+ },
50
+ },
51
+ };
52
+ visit(ast, visitors);
53
+ return parent;
54
+ };
55
+ export var findAssignmentTo = function (ast, assignTo) {
56
+ var assign;
57
+ var visitors = {
58
+ expression_statement: {
59
+ enter: function (path) {
60
+ var _a, _b;
61
+ if (((_b = (_a = path.node.expression) === null || _a === void 0 ? void 0 : _a.left) === null || _b === void 0 ? void 0 : _b.identifier) === assignTo) {
62
+ assign = path.node;
63
+ }
64
+ path.skip();
65
+ },
66
+ },
67
+ };
68
+ visit(ast, visitors);
69
+ return assign;
70
+ };
71
+ export var findDeclarationOf = function (ast, declarationOf) {
72
+ var declaration;
73
+ var visitors = {
74
+ declaration_statement: {
75
+ enter: function (path) {
76
+ var _a, _b;
77
+ var foundDecl = (_b = (_a = path.node.declaration) === null || _a === void 0 ? void 0 : _a.declarations) === null || _b === void 0 ? void 0 : _b.find(function (decl) { var _a; return ((_a = decl === null || decl === void 0 ? void 0 : decl.identifier) === null || _a === void 0 ? void 0 : _a.identifier) === declarationOf; });
78
+ if (foundDecl) {
79
+ declaration = foundDecl;
80
+ }
81
+ path.skip();
82
+ },
83
+ },
84
+ };
85
+ visit(ast, visitors);
86
+ return declaration;
87
+ };
88
+ export var from2To3 = function (ast, stage) {
89
+ var glOut = 'fragmentColor';
90
+ // TODO: add this back in when there's only one after the merge
91
+ // ast.program.unshift({
92
+ // type: 'preprocessor',
93
+ // line: '#version 300 es',
94
+ // _: '\n',
95
+ // });
96
+ if (stage === 'fragment') {
97
+ ast.program.unshift(makeStatement("out vec4 ".concat(glOut)));
98
+ }
99
+ visit(ast, {
100
+ function_call: {
101
+ enter: function (path) {
102
+ var identifier = path.node.identifier;
103
+ if (identifier.type === 'identifier' &&
104
+ identifier.identifier === 'texture2D') {
105
+ identifier.identifier = 'texture';
106
+ }
107
+ },
108
+ },
109
+ identifier: {
110
+ enter: function (path) {
111
+ if (path.node.identifier === 'gl_FragColor') {
112
+ path.node.identifier = glOut;
113
+ }
114
+ },
115
+ },
116
+ keyword: {
117
+ enter: function (path) {
118
+ if ((path.node.token === 'attribute' || path.node.token === 'varying') &&
119
+ path.findParent(function (path) { return path.node.type === 'declaration_statement'; })) {
120
+ path.node.token =
121
+ stage === 'vertex' && path.node.token === 'varying' ? 'out' : 'in';
122
+ }
123
+ },
124
+ },
125
+ });
126
+ };
127
+ export var outDeclaration = function (name) { return ({
128
+ type: 'declaration_statement',
129
+ declaration: {
130
+ type: 'declarator_list',
131
+ specified_type: {
132
+ type: 'fully_specified_type',
133
+ qualifiers: [{ type: 'keyword', token: 'out', whitespace: ' ' }],
134
+ specifier: {
135
+ type: 'type_specifier',
136
+ specifier: { type: 'keyword', token: 'vec4', whitespace: ' ' },
137
+ quantifier: null,
138
+ },
139
+ },
140
+ declarations: [
141
+ {
142
+ type: 'declaration',
143
+ identifier: {
144
+ type: 'identifier',
145
+ identifier: name,
146
+ whitespace: undefined,
147
+ },
148
+ quantifier: null,
149
+ operator: undefined,
150
+ initializer: undefined,
151
+ },
152
+ ],
153
+ commas: [],
154
+ },
155
+ semi: { type: 'literal', literal: ';', whitespace: '\n ' },
156
+ }); };
157
+ export var makeStatement = function (stmt) {
158
+ // log(`Parsing "${stmt}"`);
159
+ var ast;
160
+ try {
161
+ ast = parser.parse("".concat(stmt, ";\n"), { quiet: true });
162
+ }
163
+ catch (error) {
164
+ console.error({ stmt: stmt, error: error });
165
+ throw new Error("Error parsing stmt \"".concat(stmt, "\": ").concat(error === null || error === void 0 ? void 0 : error.message));
166
+ }
167
+ // log(util.inspect(ast, false, null, true));
168
+ return ast.program[0];
169
+ };
170
+ export var makeFnStatement = function (fnStmt) {
171
+ var ast;
172
+ try {
173
+ ast = parser.parse("\n void main() {\n ".concat(fnStmt, ";\n }"), { quiet: true });
174
+ }
175
+ catch (error) {
176
+ console.error({ fnStmt: fnStmt, error: error });
177
+ throw new Error("Error parsing fnStmt \"".concat(fnStmt, "\": ").concat(error === null || error === void 0 ? void 0 : error.message));
178
+ }
179
+ // log(util.inspect(ast, false, null, true));
180
+ return ast.program[0].body.statements[0];
181
+ };
182
+ export var makeExpression = function (expr) {
183
+ var ast;
184
+ try {
185
+ ast = parser.parse("void main() {\n a = ".concat(expr, ";\n }"), { quiet: true });
186
+ }
187
+ catch (error) {
188
+ console.error({ expr: expr, error: error });
189
+ throw new Error("Error parsing expr \"".concat(expr, "\": ").concat(error === null || error === void 0 ? void 0 : error.message));
190
+ }
191
+ return ast.program[0].body
192
+ .statements[0].expression.right;
193
+ };
194
+ export var makeExpressionWithScopes = function (expr) {
195
+ var ast;
196
+ try {
197
+ ast = parser.parse("void main() {\n ".concat(expr, ";\n }"), { quiet: true });
198
+ }
199
+ catch (error) {
200
+ console.error({ expr: expr, error: error });
201
+ throw new Error("Error parsing expr \"".concat(expr, "\": ").concat(error === null || error === void 0 ? void 0 : error.message));
202
+ }
203
+ // log(util.inspect(ast, false, null, true));
204
+ return {
205
+ scope: ast.scopes[1],
206
+ expression: ast.program[0].body
207
+ .statements[0].expression,
208
+ };
209
+ };
210
+ export var makeFnBodyStatementWithScopes = function (body) {
211
+ var ast;
212
+ try {
213
+ ast = parser.parse("void main() {\n".concat(body, "\n }"), { quiet: true });
214
+ }
215
+ catch (error) {
216
+ console.error({ body: body, error: error });
217
+ throw new Error("Error parsing body \"".concat(body, "\": ").concat(error === null || error === void 0 ? void 0 : error.message));
218
+ }
219
+ // log(util.inspect(ast, false, null, true));
220
+ return {
221
+ scope: ast.scopes[1],
222
+ statements: ast.program[0].body.statements,
223
+ };
224
+ };
225
+ export var findFn = function (ast, name) {
226
+ return ast.program.find(function (stmt) {
227
+ return stmt.type === 'function' && stmt.prototype.header.name.identifier === name;
228
+ });
229
+ };
230
+ export var returnGlPosition = function (fnName, ast) {
231
+ return convertVertexMain(fnName, ast, 'vec4', function (assign) { return assign.expression.right; });
232
+ };
233
+ export var returnGlPositionHardCoded = function (fnName, ast, returnType, hardCodedReturn) {
234
+ return convertVertexMain(fnName, ast, returnType, function () {
235
+ return makeExpression(hardCodedReturn);
236
+ });
237
+ };
238
+ export var returnGlPositionVec3Right = function (fnName, ast) {
239
+ return convertVertexMain(fnName, ast, 'vec3', function (assign) {
240
+ var found;
241
+ visit(assign, {
242
+ function_call: {
243
+ enter: function (path) {
244
+ var _a, _b, _c, _d, _e;
245
+ log('returnGlPositionVec3Right', path.node);
246
+ var node = path.node;
247
+ if (((_b = (_a = node === null || node === void 0 ? void 0 : node.identifier) === null || _a === void 0 ? void 0 : _a.specifier) === null || _b === void 0 ? void 0 : _b.token) === 'vec4' &&
248
+ ((_e = (_d = (_c = node === null || node === void 0 ? void 0 : node.args) === null || _c === void 0 ? void 0 : _c[2]) === null || _d === void 0 ? void 0 : _d.token) === null || _e === void 0 ? void 0 : _e.includes('1.'))) {
249
+ found = node.args[0];
250
+ }
251
+ },
252
+ },
253
+ });
254
+ if (!found) {
255
+ console.error(generate(ast));
256
+ throw new Error('Could not find position assignment to convert to return!');
257
+ }
258
+ return found;
259
+ });
260
+ };
261
+ var convertVertexMain = function (fnName, ast, returnType, generateRight) {
262
+ var mainReturnVar = "frogOut";
263
+ var main = findFn(ast, fnName);
264
+ if (!main) {
265
+ throw new Error("No ".concat(fnName, " fn found!"));
266
+ }
267
+ // Convert the main function to one that returns
268
+ main.prototype.header.returnType.specifier.specifier.token =
269
+ returnType;
270
+ // Find the gl_position assignment line
271
+ var assign = main.body.statements.find(function (stmt) {
272
+ var _a;
273
+ return stmt.type === 'expression_statement' &&
274
+ ((_a = stmt.expression.left) === null || _a === void 0 ? void 0 : _a.identifier) === 'gl_Position';
275
+ });
276
+ if (!assign) {
277
+ throw new Error("No gl position assign found in main fn!");
278
+ }
279
+ var rtnStmt = makeFnStatement("".concat(returnType, " ").concat(mainReturnVar, " = 1.0"));
280
+ rtnStmt.declaration.declarations[0].initializer =
281
+ generateRight(assign);
282
+ main.body.statements.splice(main.body.statements.indexOf(assign), 1, rtnStmt);
283
+ main.body.statements.push(makeFnStatement("return ".concat(mainReturnVar)));
284
+ };
285
+ export var convert300MainToReturn = function (suffix, ast) {
286
+ var mainReturnVar = "frogOut_".concat(suffix);
287
+ // Find the output variable, as in "pc_fragColor" from "out highp vec4 pc_fragColor;"
288
+ var outName;
289
+ ast.program.find(function (line, index) {
290
+ var _a, _b;
291
+ var declaration = line === null || line === void 0 ? void 0 : line.declaration;
292
+ if (
293
+ // line.type === 'declaration_statement' &&
294
+ ((_b = (_a = declaration === null || declaration === void 0 ? void 0 : declaration.specified_type) === null || _a === void 0 ? void 0 : _a.qualifiers) === null || _b === void 0 ? void 0 : _b.find(function (n) { return n.token === 'out'; })) &&
295
+ declaration.specified_type.specifier.specifier.token ===
296
+ 'vec4') {
297
+ // Remove the out declaration
298
+ ast.program.splice(index, 1);
299
+ outName = declaration.declarations[0].identifier.identifier;
300
+ return true;
301
+ }
302
+ });
303
+ if (!outName) {
304
+ console.error(generate(ast));
305
+ throw new Error('No "out vec4" line found in the fragment shader');
306
+ }
307
+ ast.program.unshift(makeStatement("vec4 ".concat(mainReturnVar)));
308
+ visit(ast, {
309
+ identifier: {
310
+ enter: function (path) {
311
+ if (path.node.identifier === outName) {
312
+ path.node.identifier = mainReturnVar;
313
+ // @ts-ignore
314
+ path.node.doNotDescope = true; // hack because this var is in the scope which gets renamed later
315
+ }
316
+ },
317
+ },
318
+ function: {
319
+ enter: function (path) {
320
+ if (path.node.prototype.header.name.identifier === 'main') {
321
+ path.node.prototype.header.returnType.specifier
322
+ .specifier.token = 'vec4';
323
+ path.node.body.statements.push(makeFnStatement("return ".concat(mainReturnVar)));
324
+ }
325
+ },
326
+ },
327
+ });
328
+ };
@@ -0,0 +1,256 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var __read = (this && this.__read) || function (o, n) {
13
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
14
+ if (!m) return o;
15
+ var i = m.call(o), r, ar = [], e;
16
+ try {
17
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
18
+ }
19
+ catch (error) { e = { error: error }; }
20
+ finally {
21
+ try {
22
+ if (r && !r.done && (m = i["return"])) m.call(i);
23
+ }
24
+ finally { if (e) throw e.error; }
25
+ }
26
+ return ar;
27
+ };
28
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
29
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
30
+ if (ar || !(i in from)) {
31
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
32
+ ar[i] = from[i];
33
+ }
34
+ }
35
+ return to.concat(ar || Array.prototype.slice.call(from));
36
+ };
37
+ import { generate } from '@shaderfrog/glsl-parser';
38
+ import { makeStatement } from './manipulate';
39
+ export var emptyShaderSections = function () { return ({
40
+ precision: [],
41
+ preprocessor: [],
42
+ version: [],
43
+ structs: [],
44
+ program: [],
45
+ inStatements: [],
46
+ outStatements: [],
47
+ uniforms: [],
48
+ }); };
49
+ var Precision;
50
+ (function (Precision) {
51
+ Precision[Precision["highp"] = 2] = "highp";
52
+ Precision[Precision["mediump"] = 1] = "mediump";
53
+ Precision[Precision["lowp"] = 0] = "lowp";
54
+ })(Precision || (Precision = {}));
55
+ export var higherPrecision = function (p1, p2) {
56
+ return Precision[p1] > Precision[p2] ? p1 : p2;
57
+ };
58
+ export var dedupeVersions = function (nodes) { return nodes[0]; };
59
+ export var highestPrecisions = function (nodes) {
60
+ return Object.entries(nodes.reduce(function (precisions, stmt) {
61
+ var _a;
62
+ return (__assign(__assign({}, precisions), (_a = {}, _a[stmt.declaration.specifier.specifier.token] = higherPrecision(precisions[stmt.declaration.specifier.specifier.token], stmt.declaration.qualifier.token), _a)));
63
+ }, {})).map(function (_a) {
64
+ var _b = __read(_a, 2), typeName = _b[0], precision = _b[1];
65
+ return makeStatement("precision ".concat(precision, " ").concat(typeName));
66
+ });
67
+ };
68
+ export var dedupeQualifiedStatements = function (statements, qualifier) {
69
+ return Object.entries(statements.reduce(function (stmts, stmt) {
70
+ var _a;
71
+ return (__assign(__assign({}, stmts), (_a = {}, _a[stmt.declaration.specified_type.specifier.specifier.token] = __assign(__assign({}, (stmts[stmt.declaration.specified_type.specifier.specifier.token] || {})), stmt.declaration.declarations.reduce(function (types, decl) {
72
+ var _a;
73
+ return (__assign(__assign({}, types), (_a = {}, _a[decl.identifier.identifier] = true, _a)));
74
+ }, {})), _a)));
75
+ }, {})).map(function (_a) {
76
+ var _b = __read(_a, 2), type = _b[0], varNames = _b[1];
77
+ return makeStatement("".concat(qualifier, " ").concat(type, " ").concat(Object.keys(varNames).join(', ')));
78
+ });
79
+ };
80
+ /**
81
+ * Merge uniforms together into lists of identifiers under the same type.
82
+ * There's special case handling for mixing of uniforms with "interface blocks"
83
+ * and those without when merging to make sure the interface block definition is
84
+ * preserved. Check out the tests for more.
85
+ *
86
+ * This function consumes uniforms as found by findShaderSections, so the
87
+ * definitions must line up
88
+ */
89
+ export var dedupeUniforms = function (statements) {
90
+ var groupedByTypeName = Object.entries(statements.reduce(function (stmts, stmt) {
91
+ var _a, _b, _c;
92
+ var _d;
93
+ var decl = stmt.declaration;
94
+ // This is the standard case, a uniform like "uniform vec2 x"
95
+ if ('specified_type' in decl) {
96
+ var specified_type = decl.specified_type;
97
+ var specifier = specified_type.specifier.specifier;
98
+ // Token is for "vec2", "identifier" is for custom names like struct
99
+ var type_1 = 'token' in specifier
100
+ ? specifier.token
101
+ : 'identifier' in specifier
102
+ ? specifier.identifier
103
+ : undefined;
104
+ if (!type_1) {
105
+ console.error('Unknown statement: ', stmt);
106
+ throw new Error("Unknown specifier: ".concat(specifier.type));
107
+ }
108
+ // Groups uniforms into their return type, and for each type, collapses
109
+ // uniform names into an object where the keys determine uniqueness
110
+ // "vec2": { x: x[1] }
111
+ var grouped = decl.declarations.reduce(function (types, decl) {
112
+ var _a;
113
+ var _b, _c, _d;
114
+ var identifier = decl.identifier;
115
+ var quantifier = '';
116
+ if (decl.quantifier) {
117
+ if (!('token' in decl.quantifier[0].expression)) {
118
+ console.error('Unknown expression in quantifier: ', decl);
119
+ throw new Error("Unknown expression in quantifier: ".concat(generate(decl)));
120
+ }
121
+ quantifier = "[".concat(decl.quantifier[0].expression.token, "]");
122
+ }
123
+ return __assign(__assign({}, types), (_a = {}, _a[identifier.identifier] = ((_c = (_b = stmts[type_1]) === null || _b === void 0 ? void 0 : _b[identifier.identifier]) === null || _c === void 0 ? void 0 : _c.hasInterface)
124
+ ? (_d = stmts[type_1]) === null || _d === void 0 ? void 0 : _d[identifier.identifier]
125
+ : {
126
+ hasInterface: false,
127
+ generated: identifier.identifier + quantifier,
128
+ }, _a));
129
+ }, {});
130
+ return __assign(__assign({}, stmts), (_a = {}, _a[type_1] = __assign(__assign({}, (stmts[type_1] || {})), grouped), _a));
131
+ // This is the less common case, a uniform like "uniform Light { vec3 position; } name"
132
+ }
133
+ else if ('interface_type' in decl) {
134
+ var interface_type = decl.interface_type, identifier = decl.identifier;
135
+ // If this is an interface block only, like uniform Scene { mat4 view; };
136
+ // then group the interface block declaration under ''
137
+ var interfaceDeclaredUniform = ((_d = identifier === null || identifier === void 0 ? void 0 : identifier.identifier) === null || _d === void 0 ? void 0 : _d.identifier) || '';
138
+ var node = {
139
+ type: 'interface_declarator',
140
+ lp: decl.lp,
141
+ declarations: decl.declarations,
142
+ qualifiers: [],
143
+ // This is non-nullable, to produce "X" in "uniform X { ... } varName"
144
+ // But it appears "X" is in declarations above
145
+ interface_type: {
146
+ type: 'identifier',
147
+ identifier: '',
148
+ whitespace: '',
149
+ },
150
+ rp: decl.rp,
151
+ };
152
+ return __assign(__assign({}, stmts), (_b = {}, _b[interface_type.identifier] = (_c = {},
153
+ _c[interfaceDeclaredUniform] = {
154
+ generated: "".concat(generate(node)).concat(interfaceDeclaredUniform),
155
+ hasInterface: true,
156
+ },
157
+ _c), _b));
158
+ }
159
+ else {
160
+ console.error('Unknown uniform AST', { stmt: stmt, code: generate(stmt) });
161
+ throw new Error('Unknown uniform AST encountered when merging uniforms');
162
+ }
163
+ }, {}));
164
+ return groupedByTypeName.map(function (_a) {
165
+ var _b = __read(_a, 2), type = _b[0], variables = _b[1];
166
+ return makeStatement("uniform ".concat(type, " ").concat(Object.values(variables)
167
+ .map(function (v) { return v.generated; })
168
+ .join(', ')));
169
+ });
170
+ };
171
+ export var mergeShaderSections = function (s1, s2) {
172
+ return {
173
+ version: __spreadArray(__spreadArray([], __read(s1.version), false), __read(s2.version), false),
174
+ precision: __spreadArray(__spreadArray([], __read(s1.precision), false), __read(s2.precision), false),
175
+ preprocessor: __spreadArray(__spreadArray([], __read(s1.preprocessor), false), __read(s2.preprocessor), false),
176
+ inStatements: __spreadArray(__spreadArray([], __read(s1.inStatements), false), __read(s2.inStatements), false),
177
+ outStatements: __spreadArray(__spreadArray([], __read(s1.outStatements), false), __read(s2.outStatements), false),
178
+ structs: __spreadArray(__spreadArray([], __read(s1.structs), false), __read(s2.structs), false),
179
+ uniforms: __spreadArray(__spreadArray([], __read(s1.uniforms), false), __read(s2.uniforms), false),
180
+ program: __spreadArray(__spreadArray([], __read(s1.program), false), __read(s2.program), false),
181
+ };
182
+ };
183
+ export var shaderSectionsToProgram = function (sections, mergeOptions) { return ({
184
+ type: 'program',
185
+ scopes: [],
186
+ program: __spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read((mergeOptions.includeVersion ? [dedupeVersions(sections.version)] : [])), false), __read((mergeOptions.includePrecisions
187
+ ? highestPrecisions(sections.precision)
188
+ : [])), false), __read(sections.preprocessor), false), __read(sections.structs), false), __read(dedupeQualifiedStatements(sections.inStatements, 'in')), false), __read(dedupeQualifiedStatements(sections.outStatements, 'out')), false), __read(dedupeUniforms(sections.uniforms)), false), __read(sections.program), false),
189
+ }); };
190
+ /**
191
+ * Group an AST into logical sections. The output of this funciton is consumed
192
+ * by the dedupe methods, namely dedupeUniforms, so the data shapes are coupled
193
+ */
194
+ export var findShaderSections = function (ast) {
195
+ var initialValue = {
196
+ precision: [],
197
+ preprocessor: [],
198
+ version: [],
199
+ structs: [],
200
+ inStatements: [],
201
+ outStatements: [],
202
+ uniforms: [],
203
+ program: [],
204
+ };
205
+ return ast.program.reduce(function (sections, node) {
206
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
207
+ if (node.type === 'preprocessor' && node.line.startsWith('#version')) {
208
+ return __assign(__assign({}, sections), { version: sections.version.concat(node) });
209
+ }
210
+ else if (node.type === 'declaration_statement' &&
211
+ node.declaration.type === 'precision') {
212
+ return __assign(__assign({}, sections), { precision: sections.precision.concat(node) });
213
+ }
214
+ else if (node.type === 'preprocessor') {
215
+ return __assign(__assign({}, sections), { preprocessor: sections.preprocessor.concat(node) });
216
+ }
217
+ else if (node.type === 'declaration_statement' &&
218
+ node.declaration.type === 'declarator_list' &&
219
+ ((_d = (_c = (_b = (_a = node.declaration) === null || _a === void 0 ? void 0 : _a.specified_type) === null || _b === void 0 ? void 0 : _b.specifier) === null || _c === void 0 ? void 0 : _c.specifier) === null || _d === void 0 ? void 0 : _d.type) === 'struct') {
220
+ return __assign(__assign({}, sections), { structs: sections.structs.concat(node) });
221
+ // This definition of a uniform lines up with the processing we do in
222
+ // dedupeUniforms
223
+ }
224
+ else if (node.type === 'declaration_statement' &&
225
+ // Ignore lines like "layout(std140,column_major) uniform;"
226
+ !('qualifiers' in node.declaration &&
227
+ ((_f = (_e = node.declaration) === null || _e === void 0 ? void 0 : _e.qualifiers) === null || _f === void 0 ? void 0 : _f.find(function (q) { return 'layout' in q; }))) &&
228
+ // One of these checks is for a uniform with an interface block, and the
229
+ // other is for vanilla uniforms. I don't remember which is which
230
+ (('specified_type' in node.declaration &&
231
+ 'qualifiers' in node.declaration.specified_type &&
232
+ ((_g = node.declaration.specified_type.qualifiers) === null || _g === void 0 ? void 0 : _g.find(function (n) { return 'token' in n && n.token === 'uniform'; }))) ||
233
+ ('qualifiers' in node.declaration &&
234
+ ((_j = (_h = node.declaration) === null || _h === void 0 ? void 0 : _h.qualifiers) === null || _j === void 0 ? void 0 : _j.find(function (n) { return 'token' in n && n.token === 'uniform'; }))))) {
235
+ return __assign(__assign({}, sections), { uniforms: sections.uniforms.concat(node) });
236
+ }
237
+ else if (node.type === 'declaration_statement' &&
238
+ 'specified_type' in node.declaration &&
239
+ ((_m = (_l = (_k = node.declaration) === null || _k === void 0 ? void 0 : _k.specified_type) === null || _l === void 0 ? void 0 : _l.qualifiers) === null || _m === void 0 ? void 0 : _m.find(function (n) { return 'token' in n && n.token === 'in'; }))) {
240
+ if (generate(node).includes('main_Fireball')) {
241
+ console.log('findShaderSections\n', generate(ast));
242
+ console.log("Tracking inStatement \"".concat(generate(node), "\""), node);
243
+ debugger;
244
+ }
245
+ return __assign(__assign({}, sections), { inStatements: sections.inStatements.concat(node) });
246
+ }
247
+ else if (node.type === 'declaration_statement' &&
248
+ 'specified_type' in node.declaration &&
249
+ ((_q = (_p = (_o = node.declaration) === null || _o === void 0 ? void 0 : _o.specified_type) === null || _p === void 0 ? void 0 : _p.qualifiers) === null || _q === void 0 ? void 0 : _q.find(function (n) { return 'token' in n && n.token === 'out'; }))) {
250
+ return __assign(__assign({}, sections), { outStatements: sections.outStatements.concat(node) });
251
+ }
252
+ else {
253
+ return __assign(__assign({}, sections), { program: sections.program.concat(node) });
254
+ }
255
+ }, initialValue);
256
+ };