@shaderfrog/core 2.0.0-beta.3 → 2.0.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.
- package/README.md +2 -2
- package/engine.d.ts +1 -0
- package/graph/code-nodes.d.ts +8 -2
- package/graph/code-nodes.js +5 -1
- package/graph/context.d.ts +3 -2
- package/graph/context.js +25 -12
- package/graph/evaluate.js +7 -0
- package/graph/graph.d.ts +8 -2
- package/graph/graph.js +105 -125
- package/graph/graph.test.js +226 -50
- package/graph/parsers.d.ts +2 -11
- package/graph/parsers.js +24 -14
- package/graph/shader-sections.d.ts +1 -1
- package/graph/shader-sections.js +1 -1
- package/package.json +4 -4
- package/plugins/babylon/bablyengine.js +4 -1
- package/plugins/playcanvas/playengine.js +4 -1
- package/plugins/three/importers.d.ts +1 -0
- package/plugins/three/importers.js +49 -1
- package/plugins/three/threngine.js +5 -2
- package/plugins/three/threngine.test.js +52 -4
- package/strategy/assignemntTo.js +4 -3
- package/strategy/declarationOf.js +2 -2
- package/strategy/hardCode.js +1 -4
- package/strategy/inject.js +3 -4
- package/strategy/namedAttribute.js +2 -2
- package/strategy/strategy.d.ts +28 -3
- package/strategy/stratgies.test.js +59 -31
- package/strategy/texture2D.js +20 -22
- package/strategy/uniform.js +54 -56
- package/strategy/variable.js +5 -5
- package/util/ast.d.ts +7 -3
- package/util/ast.js +29 -8
- package/util/ast.test.d.ts +1 -0
- package/util/ast.test.js +14 -0
- package/util/whitespace.d.ts +2 -0
- package/util/whitespace.js +27 -1
package/util/ast.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions to work with ASTs
|
|
3
|
+
*/
|
|
1
4
|
var __assign = (this && this.__assign) || function () {
|
|
2
5
|
__assign = Object.assign || function(t) {
|
|
3
6
|
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
@@ -37,7 +40,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
|
37
40
|
import { parser, generate } from '@shaderfrog/glsl-parser';
|
|
38
41
|
import { visit, } from '@shaderfrog/glsl-parser/ast';
|
|
39
42
|
import { addFnStmtWithIndent } from './whitespace';
|
|
40
|
-
import { renameBinding
|
|
43
|
+
import { renameBinding } from '@shaderfrog/glsl-parser/parser/utils';
|
|
41
44
|
var log = function () {
|
|
42
45
|
var _a;
|
|
43
46
|
var args = [];
|
|
@@ -159,11 +162,12 @@ export var from2To3 = function (ast, stage) {
|
|
|
159
162
|
},
|
|
160
163
|
});
|
|
161
164
|
};
|
|
162
|
-
export var makeStatement = function (stmt) {
|
|
165
|
+
export var makeStatement = function (stmt, ws) {
|
|
166
|
+
if (ws === void 0) { ws = ''; }
|
|
163
167
|
// log(`Parsing "${stmt}"`);
|
|
164
168
|
var ast;
|
|
165
169
|
try {
|
|
166
|
-
ast = parser.parse("".concat(stmt, "
|
|
170
|
+
ast = parser.parse("".concat(stmt, ";").concat(ws, "\n"), { quiet: true });
|
|
167
171
|
}
|
|
168
172
|
catch (error) {
|
|
169
173
|
console.error({ stmt: stmt, error: error });
|
|
@@ -366,11 +370,28 @@ export var convert300MainToReturn = function (ast) {
|
|
|
366
370
|
main.body.statements = addFnStmtWithIndent(main, rtn);
|
|
367
371
|
ast.scopes[0].bindings[replacedReturn].references.push(rtn.expression);
|
|
368
372
|
};
|
|
369
|
-
export var generateFiller = function (
|
|
370
|
-
if (!
|
|
373
|
+
export var generateFiller = function (ast) {
|
|
374
|
+
if (!ast) {
|
|
371
375
|
throw new Error('Cannot generate void filler!');
|
|
372
376
|
}
|
|
373
|
-
return Array.isArray(
|
|
374
|
-
|
|
375
|
-
|
|
377
|
+
return Array.isArray(ast) ? ast.map(generate).join('') : generate(ast);
|
|
378
|
+
};
|
|
379
|
+
export var isDeclarationStatement = function (node) {
|
|
380
|
+
return node.type === 'declaration_statement' &&
|
|
381
|
+
node.declaration.type === 'declarator_list';
|
|
382
|
+
};
|
|
383
|
+
export var backfillAst = function (ast, fromType, targetVariable, mainFn) {
|
|
384
|
+
if (!ast.scopes[0].bindings[targetVariable]) {
|
|
385
|
+
console.warn("Variable \"".concat(targetVariable, "\" not found in global program scope to backfill! Variables: ").concat(Object.keys(ast.scopes[0].bindings)));
|
|
386
|
+
}
|
|
387
|
+
// Inject the backfill param as the arg
|
|
388
|
+
if (mainFn) {
|
|
389
|
+
mainFn.prototype.parameters = (mainFn.prototype.parameters || [])
|
|
390
|
+
.filter(
|
|
391
|
+
// Watch out for the main(void){} case!
|
|
392
|
+
function (arg) { return arg.specifier.specifier.token !== 'void'; })
|
|
393
|
+
.concat(parser.parse("void x(".concat(fromType, " ").concat(targetVariable, ") {}"))
|
|
394
|
+
.program[0].prototype.parameters);
|
|
395
|
+
}
|
|
396
|
+
return ast;
|
|
376
397
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/util/ast.test.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { expect, it } from 'vitest';
|
|
2
|
+
import { parser } from '@shaderfrog/glsl-parser';
|
|
3
|
+
import { generate } from '@shaderfrog/glsl-parser';
|
|
4
|
+
import { backfillAst, findMain } from '../util/ast';
|
|
5
|
+
it('backfillAst', function () {
|
|
6
|
+
var source = parser.parse("\nattribute vec2 vUv, xx;\nvoid main() {\n gl_FragColor = vec4(vUv, xx);\n}");
|
|
7
|
+
var result = backfillAst(source, 'vec2', 'vUv', findMain(source));
|
|
8
|
+
expect(generate(result)).toBe("\nattribute vec2 vUv, xx;\nvoid main(vec2 vUv) {\n gl_FragColor = vec4(vUv, xx);\n}");
|
|
9
|
+
});
|
|
10
|
+
it('backfillAst with void main fn', function () {
|
|
11
|
+
var source = parser.parse("\nattribute vec2 vUv;\nvoid main(void) {\n gl_FragColor = vec4(vUv, 1.0, 1.0);\n}");
|
|
12
|
+
var result = backfillAst(source, 'vec2', 'vUv', findMain(source));
|
|
13
|
+
expect(generate(result)).toBe("\nattribute vec2 vUv;\nvoid main(vec2 vUv) {\n gl_FragColor = vec4(vUv, 1.0, 1.0);\n}");
|
|
14
|
+
});
|
package/util/whitespace.d.ts
CHANGED
|
@@ -16,3 +16,5 @@ export declare const getLiteralIndent: (node: {
|
|
|
16
16
|
export declare const tryAddTrailingWhitespace: <T extends AstNode>(node: T, ws: string) => T;
|
|
17
17
|
export declare const guessFnIndent: (fnBody: FunctionNode) => string;
|
|
18
18
|
export declare const addFnStmtWithIndent: (fnBody: FunctionNode, newNode: string | AstNode) => AstNode[];
|
|
19
|
+
export declare const unshiftFnStmtWithIndent: (fnBody: FunctionNode, newNode: string | AstNode) => AstNode[];
|
|
20
|
+
export declare const spliceFnStmtWithIndent: (fnBody: FunctionNode, index: number, ...newNodes: (string | AstNode)[]) => AstNode[];
|
package/util/whitespace.js
CHANGED
|
@@ -79,6 +79,9 @@ export var guessFnIndent = function (fnBody) {
|
|
|
79
79
|
return ws || getLiteralIndent(n.semi) || '';
|
|
80
80
|
}, '');
|
|
81
81
|
};
|
|
82
|
+
var addWs = function (node, ws) {
|
|
83
|
+
return tryAddTrailingWhitespace(typeof node === 'string' ? makeFnStatement(node)[0] : node, ws);
|
|
84
|
+
};
|
|
82
85
|
export var addFnStmtWithIndent = function (fnBody, newNode) {
|
|
83
86
|
var statements = fnBody.body.statements;
|
|
84
87
|
var indent = guessFnIndent(fnBody);
|
|
@@ -86,6 +89,29 @@ export var addFnStmtWithIndent = function (fnBody, newNode) {
|
|
|
86
89
|
// This simple hack is way easier than trying to modify the function body
|
|
87
90
|
// opening brace and/or the previous statement
|
|
88
91
|
{ type: 'literal', literal: '', whitespace: indent },
|
|
89
|
-
|
|
92
|
+
addWs(newNode, "\n"),
|
|
90
93
|
], false);
|
|
91
94
|
};
|
|
95
|
+
export var unshiftFnStmtWithIndent = function (fnBody, newNode) {
|
|
96
|
+
var statements = fnBody.body.statements;
|
|
97
|
+
var indent = guessFnIndent(fnBody);
|
|
98
|
+
return __spreadArray([
|
|
99
|
+
addWs(newNode, "\n"),
|
|
100
|
+
{ type: 'literal', literal: '', whitespace: indent }
|
|
101
|
+
], __read(statements), false);
|
|
102
|
+
};
|
|
103
|
+
export var spliceFnStmtWithIndent = function (fnBody, index) {
|
|
104
|
+
var newNodes = [];
|
|
105
|
+
for (var _i = 2; _i < arguments.length; _i++) {
|
|
106
|
+
newNodes[_i - 2] = arguments[_i];
|
|
107
|
+
}
|
|
108
|
+
var statements = fnBody.body.statements;
|
|
109
|
+
var indent = guessFnIndent(fnBody);
|
|
110
|
+
return __spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(statements.slice(0, index)), false), __read(newNodes.map(function (n) { return addWs(n, "\n"); })), false), [
|
|
111
|
+
{
|
|
112
|
+
type: 'literal',
|
|
113
|
+
literal: '',
|
|
114
|
+
whitespace: indent,
|
|
115
|
+
}
|
|
116
|
+
], false), __read(statements.slice(index)), false);
|
|
117
|
+
};
|