@shaderfrog/core 0.1.1 → 1.0.0

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 (69) hide show
  1. package/dist/engine.d.ts +66 -0
  2. package/dist/engine.js +3 -3
  3. package/dist/graph/base-node.d.ts +34 -0
  4. package/dist/graph/code-nodes.d.ts +46 -0
  5. package/dist/graph/context.d.ts +35 -0
  6. package/dist/{context.js → graph/context.js} +1 -1
  7. package/dist/graph/data-nodes.d.ts +83 -0
  8. package/dist/graph/edge.d.ts +12 -0
  9. package/dist/graph/engine-node.d.ts +15 -0
  10. package/dist/{nodes → graph}/engine-node.js +2 -2
  11. package/dist/graph/evaluate.d.ts +9 -0
  12. package/dist/graph/graph-types.d.ts +25 -0
  13. package/dist/graph/graph.d.ts +70 -0
  14. package/dist/{graph.js → graph/graph.js} +6 -6
  15. package/dist/graph/graph.test.d.ts +1 -0
  16. package/dist/{graph.test.js → graph/graph.test.js} +4 -5
  17. package/dist/graph/index.d.ts +9 -0
  18. package/dist/graph/index.js +9 -0
  19. package/dist/graph/parsers.d.ts +39 -0
  20. package/dist/{parsers.js → graph/parsers.js} +5 -5
  21. package/dist/graph/shader-sections.d.ts +47 -0
  22. package/dist/{ast → graph}/shader-sections.js +1 -1
  23. package/dist/index.d.ts +3 -0
  24. package/dist/index.js +3 -0
  25. package/dist/plugins/babylon/bablyengine.d.ts +28 -0
  26. package/dist/plugins/babylon/bablyengine.js +16 -16
  27. package/dist/plugins/babylon/importers.d.ts +3 -0
  28. package/dist/plugins/babylon/index.d.ts +2 -0
  29. package/dist/plugins/playcanvas/importers.d.ts +3 -0
  30. package/dist/plugins/playcanvas/index.d.ts +2 -0
  31. package/dist/plugins/playcanvas/playengine.d.ts +39 -0
  32. package/dist/plugins/playcanvas/playengine.js +4 -4
  33. package/dist/plugins/three/importers.d.ts +3 -0
  34. package/dist/plugins/three/index.d.ts +2 -0
  35. package/dist/plugins/three/threngine.d.ts +31 -0
  36. package/dist/plugins/three/threngine.js +5 -5
  37. package/dist/strategy/assignemntTo.d.ts +9 -0
  38. package/dist/strategy/assignemntTo.js +2 -2
  39. package/dist/strategy/declarationOf.d.ts +9 -0
  40. package/dist/strategy/declarationOf.js +2 -2
  41. package/dist/strategy/hardCode.d.ts +15 -0
  42. package/dist/strategy/index.d.ts +8 -0
  43. package/dist/strategy/index.js +1 -31
  44. package/dist/strategy/inject.d.ts +15 -0
  45. package/dist/strategy/inject.js +1 -1
  46. package/dist/strategy/namedAttribute.d.ts +9 -0
  47. package/dist/strategy/namedAttribute.js +1 -1
  48. package/dist/strategy/strategy.d.ts +28 -0
  49. package/dist/strategy/strategy.js +31 -0
  50. package/dist/strategy/stratgies.test.d.ts +1 -0
  51. package/dist/{stratgies.test.js → strategy/stratgies.test.js} +3 -3
  52. package/dist/strategy/texture2D.d.ts +6 -0
  53. package/dist/strategy/texture2D.js +1 -1
  54. package/dist/strategy/uniform.d.ts +6 -0
  55. package/dist/strategy/uniform.js +2 -2
  56. package/dist/strategy/variable.d.ts +6 -0
  57. package/dist/strategy/variable.js +1 -1
  58. package/dist/util/ast.d.ts +30 -0
  59. package/dist/util/ast.js +325 -1
  60. package/dist/util/ensure.d.ts +1 -0
  61. package/dist/util/id.d.ts +1 -0
  62. package/package.json +3 -4
  63. package/dist/ast/manipulate.js +0 -328
  64. /package/dist/{nodes/core-node.js → graph/base-node.js} +0 -0
  65. /package/dist/{nodes → graph}/code-nodes.js +0 -0
  66. /package/dist/{nodes → graph}/data-nodes.js +0 -0
  67. /package/dist/{nodes → graph}/edge.js +0 -0
  68. /package/dist/{evaluate.js → graph/evaluate.js} +0 -0
  69. /package/dist/{graph-types.js → graph/graph-types.js} +0 -0
package/dist/util/ast.js CHANGED
@@ -1,4 +1,328 @@
1
- import { generate } from '@shaderfrog/glsl-parser';
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
+ import { parser, generate } from '@shaderfrog/glsl-parser';
27
+ import { visit, } from '@shaderfrog/glsl-parser/ast';
28
+ var log = function () {
29
+ var _a;
30
+ var args = [];
31
+ for (var _i = 0; _i < arguments.length; _i++) {
32
+ args[_i] = arguments[_i];
33
+ }
34
+ return (_a = console.log).call.apply(_a, __spreadArray([console, '\x1b[31m(core.manipulate)\x1b[0m'], __read(args), false));
35
+ };
36
+ export var findVec4Constructor = function (ast) {
37
+ var parent;
38
+ var visitors = {
39
+ function_call: {
40
+ enter: function (path) {
41
+ var _a, _b;
42
+ if (((_a = path.node.identifier) === null || _a === void 0 ? void 0 : _a.specifier).token === 'vec4') {
43
+ parent = (_b = path.findParent(function (p) { return 'right' in p.node; })) === null || _b === void 0 ? void 0 : _b.node;
44
+ path.skip();
45
+ }
46
+ },
47
+ },
48
+ };
49
+ visit(ast, visitors);
50
+ return parent;
51
+ };
52
+ export var findAssignmentTo = function (ast, assignTo) {
53
+ var assign;
54
+ var visitors = {
55
+ expression_statement: {
56
+ enter: function (path) {
57
+ var _a, _b;
58
+ if (((_b = (_a = path.node.expression) === null || _a === void 0 ? void 0 : _a.left) === null || _b === void 0 ? void 0 : _b.identifier) === assignTo) {
59
+ assign = path.node;
60
+ }
61
+ path.skip();
62
+ },
63
+ },
64
+ };
65
+ visit(ast, visitors);
66
+ return assign;
67
+ };
68
+ export var findDeclarationOf = function (ast, declarationOf) {
69
+ var declaration;
70
+ var visitors = {
71
+ declaration_statement: {
72
+ enter: function (path) {
73
+ var _a, _b;
74
+ 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; });
75
+ if (foundDecl) {
76
+ declaration = foundDecl;
77
+ }
78
+ path.skip();
79
+ },
80
+ },
81
+ };
82
+ visit(ast, visitors);
83
+ return declaration;
84
+ };
85
+ export var from2To3 = function (ast, stage) {
86
+ var glOut = 'fragmentColor';
87
+ // TODO: add this back in when there's only one after the merge
88
+ // ast.program.unshift({
89
+ // type: 'preprocessor',
90
+ // line: '#version 300 es',
91
+ // _: '\n',
92
+ // });
93
+ if (stage === 'fragment') {
94
+ ast.program.unshift(makeStatement("out vec4 ".concat(glOut)));
95
+ }
96
+ visit(ast, {
97
+ function_call: {
98
+ enter: function (path) {
99
+ var identifier = path.node.identifier;
100
+ if (identifier.type === 'identifier' &&
101
+ identifier.identifier === 'texture2D') {
102
+ identifier.identifier = 'texture';
103
+ }
104
+ },
105
+ },
106
+ identifier: {
107
+ enter: function (path) {
108
+ if (path.node.identifier === 'gl_FragColor') {
109
+ path.node.identifier = glOut;
110
+ }
111
+ },
112
+ },
113
+ keyword: {
114
+ enter: function (path) {
115
+ if ((path.node.token === 'attribute' || path.node.token === 'varying') &&
116
+ path.findParent(function (path) { return path.node.type === 'declaration_statement'; })) {
117
+ path.node.token =
118
+ stage === 'vertex' && path.node.token === 'varying' ? 'out' : 'in';
119
+ }
120
+ },
121
+ },
122
+ });
123
+ };
124
+ export var outDeclaration = function (name) { return ({
125
+ type: 'declaration_statement',
126
+ declaration: {
127
+ type: 'declarator_list',
128
+ specified_type: {
129
+ type: 'fully_specified_type',
130
+ qualifiers: [{ type: 'keyword', token: 'out', whitespace: ' ' }],
131
+ specifier: {
132
+ type: 'type_specifier',
133
+ specifier: { type: 'keyword', token: 'vec4', whitespace: ' ' },
134
+ quantifier: null,
135
+ },
136
+ },
137
+ declarations: [
138
+ {
139
+ type: 'declaration',
140
+ identifier: {
141
+ type: 'identifier',
142
+ identifier: name,
143
+ whitespace: undefined,
144
+ },
145
+ quantifier: null,
146
+ operator: undefined,
147
+ initializer: undefined,
148
+ },
149
+ ],
150
+ commas: [],
151
+ },
152
+ semi: { type: 'literal', literal: ';', whitespace: '\n ' },
153
+ }); };
154
+ export var makeStatement = function (stmt) {
155
+ // log(`Parsing "${stmt}"`);
156
+ var ast;
157
+ try {
158
+ ast = parser.parse("".concat(stmt, ";\n"), { quiet: true });
159
+ }
160
+ catch (error) {
161
+ console.error({ stmt: stmt, error: error });
162
+ throw new Error("Error parsing stmt \"".concat(stmt, "\": ").concat(error === null || error === void 0 ? void 0 : error.message));
163
+ }
164
+ // log(util.inspect(ast, false, null, true));
165
+ return ast.program[0];
166
+ };
167
+ export var makeFnStatement = function (fnStmt) {
168
+ var ast;
169
+ try {
170
+ ast = parser.parse("\n void main() {\n ".concat(fnStmt, ";\n }"), { quiet: true });
171
+ }
172
+ catch (error) {
173
+ console.error({ fnStmt: fnStmt, error: error });
174
+ throw new Error("Error parsing fnStmt \"".concat(fnStmt, "\": ").concat(error === null || error === void 0 ? void 0 : error.message));
175
+ }
176
+ // log(util.inspect(ast, false, null, true));
177
+ return ast.program[0].body.statements[0];
178
+ };
179
+ export var makeExpression = function (expr) {
180
+ var ast;
181
+ try {
182
+ ast = parser.parse("void main() {\n a = ".concat(expr, ";\n }"), { quiet: true });
183
+ }
184
+ catch (error) {
185
+ console.error({ expr: expr, error: error });
186
+ throw new Error("Error parsing expr \"".concat(expr, "\": ").concat(error === null || error === void 0 ? void 0 : error.message));
187
+ }
188
+ return ast.program[0].body
189
+ .statements[0].expression.right;
190
+ };
191
+ export var makeExpressionWithScopes = function (expr) {
192
+ var ast;
193
+ try {
194
+ ast = parser.parse("void main() {\n ".concat(expr, ";\n }"), { quiet: true });
195
+ }
196
+ catch (error) {
197
+ console.error({ expr: expr, error: error });
198
+ throw new Error("Error parsing expr \"".concat(expr, "\": ").concat(error === null || error === void 0 ? void 0 : error.message));
199
+ }
200
+ // log(util.inspect(ast, false, null, true));
201
+ return {
202
+ scope: ast.scopes[1],
203
+ expression: ast.program[0].body
204
+ .statements[0].expression,
205
+ };
206
+ };
207
+ export var makeFnBodyStatementWithScopes = function (body) {
208
+ var ast;
209
+ try {
210
+ ast = parser.parse("void main() {\n".concat(body, "\n }"), { quiet: true });
211
+ }
212
+ catch (error) {
213
+ console.error({ body: body, error: error });
214
+ throw new Error("Error parsing body \"".concat(body, "\": ").concat(error === null || error === void 0 ? void 0 : error.message));
215
+ }
216
+ // log(util.inspect(ast, false, null, true));
217
+ return {
218
+ scope: ast.scopes[1],
219
+ statements: ast.program[0].body.statements,
220
+ };
221
+ };
222
+ export var findFn = function (ast, name) {
223
+ return ast.program.find(function (stmt) {
224
+ return stmt.type === 'function' && stmt.prototype.header.name.identifier === name;
225
+ });
226
+ };
227
+ export var returnGlPosition = function (fnName, ast) {
228
+ return convertVertexMain(fnName, ast, 'vec4', function (assign) { return assign.expression.right; });
229
+ };
230
+ export var returnGlPositionHardCoded = function (fnName, ast, returnType, hardCodedReturn) {
231
+ return convertVertexMain(fnName, ast, returnType, function () {
232
+ return makeExpression(hardCodedReturn);
233
+ });
234
+ };
235
+ export var returnGlPositionVec3Right = function (fnName, ast) {
236
+ return convertVertexMain(fnName, ast, 'vec3', function (assign) {
237
+ var found;
238
+ visit(assign, {
239
+ function_call: {
240
+ enter: function (path) {
241
+ var _a, _b, _c, _d, _e;
242
+ log('returnGlPositionVec3Right', path.node);
243
+ var node = path.node;
244
+ 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' &&
245
+ ((_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.'))) {
246
+ found = node.args[0];
247
+ }
248
+ },
249
+ },
250
+ });
251
+ if (!found) {
252
+ console.error(generate(ast));
253
+ throw new Error('Could not find position assignment to convert to return!');
254
+ }
255
+ return found;
256
+ });
257
+ };
258
+ var convertVertexMain = function (fnName, ast, returnType, generateRight) {
259
+ var mainReturnVar = "frogOut";
260
+ var main = findFn(ast, fnName);
261
+ if (!main) {
262
+ throw new Error("No ".concat(fnName, " fn found!"));
263
+ }
264
+ // Convert the main function to one that returns
265
+ main.prototype.header.returnType.specifier.specifier.token =
266
+ returnType;
267
+ // Find the gl_position assignment line
268
+ var assign = main.body.statements.find(function (stmt) {
269
+ var _a;
270
+ return stmt.type === 'expression_statement' &&
271
+ ((_a = stmt.expression.left) === null || _a === void 0 ? void 0 : _a.identifier) === 'gl_Position';
272
+ });
273
+ if (!assign) {
274
+ throw new Error("No gl position assign found in main fn!");
275
+ }
276
+ var rtnStmt = makeFnStatement("".concat(returnType, " ").concat(mainReturnVar, " = 1.0"));
277
+ rtnStmt.declaration.declarations[0].initializer =
278
+ generateRight(assign);
279
+ main.body.statements.splice(main.body.statements.indexOf(assign), 1, rtnStmt);
280
+ main.body.statements.push(makeFnStatement("return ".concat(mainReturnVar)));
281
+ };
282
+ export var convert300MainToReturn = function (suffix, ast) {
283
+ var mainReturnVar = "frogOut_".concat(suffix);
284
+ // Find the output variable, as in "pc_fragColor" from "out highp vec4 pc_fragColor;"
285
+ var outName;
286
+ ast.program.find(function (line, index) {
287
+ var _a, _b;
288
+ var declaration = line === null || line === void 0 ? void 0 : line.declaration;
289
+ if (
290
+ // line.type === 'declaration_statement' &&
291
+ ((_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'; })) &&
292
+ declaration.specified_type.specifier.specifier.token ===
293
+ 'vec4') {
294
+ // Remove the out declaration
295
+ ast.program.splice(index, 1);
296
+ outName = declaration.declarations[0].identifier.identifier;
297
+ return true;
298
+ }
299
+ });
300
+ if (!outName) {
301
+ console.error(generate(ast));
302
+ throw new Error('No "out vec4" line found in the fragment shader');
303
+ }
304
+ ast.program.unshift(makeStatement("vec4 ".concat(mainReturnVar)));
305
+ visit(ast, {
306
+ identifier: {
307
+ enter: function (path) {
308
+ if (path.node.identifier === outName) {
309
+ path.node.identifier = mainReturnVar;
310
+ // @ts-ignore
311
+ path.node.doNotDescope = true; // hack because this var is in the scope which gets renamed later
312
+ }
313
+ },
314
+ },
315
+ function: {
316
+ enter: function (path) {
317
+ if (path.node.prototype.header.name.identifier === 'main') {
318
+ path.node.prototype.header.returnType.specifier
319
+ .specifier.token = 'vec4';
320
+ path.node.body.statements.push(makeFnStatement("return ".concat(mainReturnVar)));
321
+ }
322
+ },
323
+ },
324
+ });
325
+ };
2
326
  export var generateFiller = function (filler) {
3
327
  if (!filler) {
4
328
  throw new Error('Cannot generate void filler!');
@@ -0,0 +1 @@
1
+ export declare const ensure: <T>(argument: T, message?: string) => T;
@@ -0,0 +1 @@
1
+ export declare const makeId: () => string;
package/package.json CHANGED
@@ -1,8 +1,7 @@
1
1
  {
2
2
  "name": "@shaderfrog/core",
3
- "version": "0.1.1",
3
+ "version": "1.0.0",
4
4
  "description": "Shaderfrog core",
5
- "main": "index.js",
6
5
  "scripts": {
7
6
  "prepare": "npm run build",
8
7
  "build": "./build.sh",
@@ -42,8 +41,8 @@
42
41
  "peerDependencies": {
43
42
  "@shaderfrog/glsl-parser": "^2.0.0-beta.5",
44
43
  "babylonjs": ">=4",
45
- "three": ">=0.50",
46
- "playcanvas": "^1.65.3"
44
+ "playcanvas": "^1.65.3",
45
+ "three": ">=0.50"
47
46
  },
48
47
  "peerDependenciesMeta": {
49
48
  "babylonjs": {
@@ -1,328 +0,0 @@
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
- };
File without changes
File without changes
File without changes
File without changes