@shaderfrog/core 1.2.0 → 1.3.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/cjs/package.json +1 -0
- package/esm/engine.d.ts +69 -0
- package/esm/engine.js +209 -0
- package/esm/graph/base-node.d.ts +36 -0
- package/esm/graph/base-node.js +9 -0
- package/esm/graph/code-nodes.d.ts +44 -0
- package/esm/graph/code-nodes.js +18 -0
- package/esm/graph/context.d.ts +37 -0
- package/esm/graph/context.js +243 -0
- package/esm/graph/data-nodes.d.ts +83 -0
- package/esm/graph/data-nodes.js +131 -0
- package/esm/graph/edge.d.ts +13 -0
- package/esm/graph/edge.js +7 -0
- package/esm/graph/evaluate.d.ts +9 -0
- package/esm/graph/evaluate.js +27 -0
- package/esm/graph/graph-node.d.ts +8 -0
- package/esm/graph/graph-node.js +135 -0
- package/esm/graph/graph-types.d.ts +38 -0
- package/esm/graph/graph-types.js +22 -0
- package/esm/graph/graph.d.ts +89 -0
- package/esm/graph/graph.js +506 -0
- package/esm/graph/graph.test.d.ts +1 -0
- package/esm/graph/graph.test.js +168 -0
- package/esm/graph/index.d.ts +11 -0
- package/esm/graph/index.js +11 -0
- package/esm/graph/parsers.d.ts +39 -0
- package/esm/graph/parsers.js +213 -0
- package/esm/graph/shader-sections.d.ts +47 -0
- package/esm/graph/shader-sections.js +256 -0
- package/esm/index.d.ts +3 -0
- package/esm/index.js +3 -0
- package/esm/package.json +1 -0
- package/esm/plugins/babylon/bablyengine.d.ts +28 -0
- package/esm/plugins/babylon/bablyengine.js +589 -0
- package/esm/plugins/babylon/importers.d.ts +3 -0
- package/esm/plugins/babylon/importers.js +64 -0
- package/esm/plugins/babylon/index.d.ts +2 -0
- package/esm/plugins/babylon/index.js +2 -0
- package/esm/plugins/playcanvas/importers.d.ts +3 -0
- package/esm/plugins/playcanvas/importers.js +28 -0
- package/esm/plugins/playcanvas/index.d.ts +2 -0
- package/esm/plugins/playcanvas/index.js +2 -0
- package/esm/plugins/playcanvas/playengine.d.ts +39 -0
- package/esm/plugins/playcanvas/playengine.js +517 -0
- package/esm/plugins/three/importers.d.ts +3 -0
- package/esm/plugins/three/importers.js +15 -0
- package/esm/plugins/three/index.d.ts +2 -0
- package/esm/plugins/three/index.js +2 -0
- package/esm/plugins/three/threngine.d.ts +34 -0
- package/esm/plugins/three/threngine.js +608 -0
- package/esm/strategy/assignemntTo.d.ts +9 -0
- package/esm/strategy/assignemntTo.js +26 -0
- package/esm/strategy/declarationOf.d.ts +9 -0
- package/esm/strategy/declarationOf.js +23 -0
- package/esm/strategy/hardCode.d.ts +15 -0
- package/esm/strategy/hardCode.js +23 -0
- package/esm/strategy/index.d.ts +9 -0
- package/esm/strategy/index.js +9 -0
- package/esm/strategy/inject.d.ts +15 -0
- package/esm/strategy/inject.js +122 -0
- package/esm/strategy/namedAttribute.d.ts +9 -0
- package/esm/strategy/namedAttribute.js +48 -0
- package/esm/strategy/strategy.d.ts +28 -0
- package/esm/strategy/strategy.js +31 -0
- package/esm/strategy/stratgies.test.d.ts +1 -0
- package/esm/strategy/stratgies.test.js +164 -0
- package/esm/strategy/texture2D.d.ts +6 -0
- package/esm/strategy/texture2D.js +83 -0
- package/esm/strategy/uniform.d.ts +6 -0
- package/esm/strategy/uniform.js +190 -0
- package/esm/strategy/variable.d.ts +6 -0
- package/esm/strategy/variable.js +80 -0
- package/esm/util/ast.d.ts +30 -0
- package/esm/util/ast.js +332 -0
- package/esm/util/ensure.d.ts +1 -0
- package/esm/util/ensure.js +7 -0
- package/esm/util/id.d.ts +1 -0
- package/esm/util/id.js +2 -0
- package/package.json +10 -11
- /package/{engine.d.ts → cjs/engine.d.ts} +0 -0
- /package/{engine.js → cjs/engine.js} +0 -0
- /package/{graph → cjs/graph}/base-node.d.ts +0 -0
- /package/{graph → cjs/graph}/base-node.js +0 -0
- /package/{graph → cjs/graph}/code-nodes.d.ts +0 -0
- /package/{graph → cjs/graph}/code-nodes.js +0 -0
- /package/{graph → cjs/graph}/context.d.ts +0 -0
- /package/{graph → cjs/graph}/context.js +0 -0
- /package/{graph → cjs/graph}/data-nodes.d.ts +0 -0
- /package/{graph → cjs/graph}/data-nodes.js +0 -0
- /package/{graph → cjs/graph}/edge.d.ts +0 -0
- /package/{graph → cjs/graph}/edge.js +0 -0
- /package/{graph → cjs/graph}/evaluate.d.ts +0 -0
- /package/{graph → cjs/graph}/evaluate.js +0 -0
- /package/{graph → cjs/graph}/graph-node.d.ts +0 -0
- /package/{graph → cjs/graph}/graph-node.js +0 -0
- /package/{graph → cjs/graph}/graph-types.d.ts +0 -0
- /package/{graph → cjs/graph}/graph-types.js +0 -0
- /package/{graph → cjs/graph}/graph.d.ts +0 -0
- /package/{graph → cjs/graph}/graph.js +0 -0
- /package/{graph → cjs/graph}/graph.test.d.ts +0 -0
- /package/{graph → cjs/graph}/graph.test.js +0 -0
- /package/{graph → cjs/graph}/index.d.ts +0 -0
- /package/{graph → cjs/graph}/index.js +0 -0
- /package/{graph → cjs/graph}/parsers.d.ts +0 -0
- /package/{graph → cjs/graph}/parsers.js +0 -0
- /package/{graph → cjs/graph}/shader-sections.d.ts +0 -0
- /package/{graph → cjs/graph}/shader-sections.js +0 -0
- /package/{index.d.ts → cjs/index.d.ts} +0 -0
- /package/{index.js → cjs/index.js} +0 -0
- /package/{plugins → cjs/plugins}/babylon/bablyengine.d.ts +0 -0
- /package/{plugins → cjs/plugins}/babylon/bablyengine.js +0 -0
- /package/{plugins → cjs/plugins}/babylon/importers.d.ts +0 -0
- /package/{plugins → cjs/plugins}/babylon/importers.js +0 -0
- /package/{plugins → cjs/plugins}/babylon/index.d.ts +0 -0
- /package/{plugins → cjs/plugins}/babylon/index.js +0 -0
- /package/{plugins → cjs/plugins}/playcanvas/importers.d.ts +0 -0
- /package/{plugins → cjs/plugins}/playcanvas/importers.js +0 -0
- /package/{plugins → cjs/plugins}/playcanvas/index.d.ts +0 -0
- /package/{plugins → cjs/plugins}/playcanvas/index.js +0 -0
- /package/{plugins → cjs/plugins}/playcanvas/playengine.d.ts +0 -0
- /package/{plugins → cjs/plugins}/playcanvas/playengine.js +0 -0
- /package/{plugins → cjs/plugins}/three/importers.d.ts +0 -0
- /package/{plugins → cjs/plugins}/three/importers.js +0 -0
- /package/{plugins → cjs/plugins}/three/index.d.ts +0 -0
- /package/{plugins → cjs/plugins}/three/index.js +0 -0
- /package/{plugins → cjs/plugins}/three/threngine.d.ts +0 -0
- /package/{plugins → cjs/plugins}/three/threngine.js +0 -0
- /package/{strategy → cjs/strategy}/assignemntTo.d.ts +0 -0
- /package/{strategy → cjs/strategy}/assignemntTo.js +0 -0
- /package/{strategy → cjs/strategy}/declarationOf.d.ts +0 -0
- /package/{strategy → cjs/strategy}/declarationOf.js +0 -0
- /package/{strategy → cjs/strategy}/hardCode.d.ts +0 -0
- /package/{strategy → cjs/strategy}/hardCode.js +0 -0
- /package/{strategy → cjs/strategy}/index.d.ts +0 -0
- /package/{strategy → cjs/strategy}/index.js +0 -0
- /package/{strategy → cjs/strategy}/inject.d.ts +0 -0
- /package/{strategy → cjs/strategy}/inject.js +0 -0
- /package/{strategy → cjs/strategy}/namedAttribute.d.ts +0 -0
- /package/{strategy → cjs/strategy}/namedAttribute.js +0 -0
- /package/{strategy → cjs/strategy}/strategy.d.ts +0 -0
- /package/{strategy → cjs/strategy}/strategy.js +0 -0
- /package/{strategy → cjs/strategy}/stratgies.test.d.ts +0 -0
- /package/{strategy → cjs/strategy}/stratgies.test.js +0 -0
- /package/{strategy → cjs/strategy}/texture2D.d.ts +0 -0
- /package/{strategy → cjs/strategy}/texture2D.js +0 -0
- /package/{strategy → cjs/strategy}/uniform.d.ts +0 -0
- /package/{strategy → cjs/strategy}/uniform.js +0 -0
- /package/{strategy → cjs/strategy}/variable.d.ts +0 -0
- /package/{strategy → cjs/strategy}/variable.js +0 -0
- /package/{util → cjs/util}/ast.d.ts +0 -0
- /package/{util → cjs/util}/ast.js +0 -0
- /package/{util → cjs/util}/ensure.d.ts +0 -0
- /package/{util → cjs/util}/ensure.js +0 -0
- /package/{util → cjs/util}/id.d.ts +0 -0
- /package/{util → cjs/util}/id.js +0 -0
|
@@ -0,0 +1,190 @@
|
|
|
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
|
+
import { mangleName } from '../graph/graph';
|
|
18
|
+
import { nodeInput } from '../graph/base-node';
|
|
19
|
+
import { StrategyType } from '.';
|
|
20
|
+
import { generateFiller } from '../util/ast';
|
|
21
|
+
export var uniformStrategy = function () { return ({
|
|
22
|
+
type: StrategyType.UNIFORM,
|
|
23
|
+
config: {},
|
|
24
|
+
}); };
|
|
25
|
+
var DATA_TYPE_MAP = [
|
|
26
|
+
['vector2', new Set(['bvec2', 'dvec2', 'ivec2', 'uvec2', 'vec2'])],
|
|
27
|
+
['number', new Set(['float', 'double', 'int', 'uint', 'atomic_uint'])],
|
|
28
|
+
['vector3', new Set(['bvec3', 'dvec3', 'ivec3', 'uvec3', 'vec3'])],
|
|
29
|
+
['vector4', new Set(['bvec4', 'dvec4', 'ivec4', 'uvec4', 'vec4'])],
|
|
30
|
+
['texture', new Set(['sampler2D'])],
|
|
31
|
+
['mat2', new Set(['mat2', 'dmat2'])],
|
|
32
|
+
['mat3', new Set(['mat3', 'dmat3'])],
|
|
33
|
+
['mat4', new Set(['mat4', 'dmat4'])],
|
|
34
|
+
['mat2x2', new Set(['mat2x2', 'dmat2x2'])],
|
|
35
|
+
['mat2x3', new Set(['mat2x3', 'dmat2x3'])],
|
|
36
|
+
['mat2x4', new Set(['mat2x4', 'dmat2x4'])],
|
|
37
|
+
['mat3x2', new Set(['mat3x2', 'dmat3x2'])],
|
|
38
|
+
['mat3x3', new Set(['mat3x3', 'dmat3x3'])],
|
|
39
|
+
['mat3x4', new Set(['mat3x4', 'dmat3x4'])],
|
|
40
|
+
['mat4x2', new Set(['mat4x2', 'dmat4x2'])],
|
|
41
|
+
['mat4x3', new Set(['mat4x3', 'dmat4x3'])],
|
|
42
|
+
['mat4x4', new Set(['mat4x4', 'dmat4x4'])],
|
|
43
|
+
];
|
|
44
|
+
/**
|
|
45
|
+
* Uncategorized:
|
|
46
|
+
*
|
|
47
|
+
"sampler1D"
|
|
48
|
+
"sampler3D"
|
|
49
|
+
"samplerCube"
|
|
50
|
+
"sampler1DShadow"
|
|
51
|
+
"sampler2DShadow"
|
|
52
|
+
"samplerCubeShadow"
|
|
53
|
+
"sampler1DArray"
|
|
54
|
+
"sampler2DArray"
|
|
55
|
+
"sampler1DArrayShadow"
|
|
56
|
+
"sampler2DArrayshadow"
|
|
57
|
+
"isampler1D"
|
|
58
|
+
"isampler2D"
|
|
59
|
+
"isampler3D"
|
|
60
|
+
"isamplerCube"
|
|
61
|
+
"isampler1Darray"
|
|
62
|
+
"isampler2DArray"
|
|
63
|
+
"usampler1D"
|
|
64
|
+
"usampler2D"
|
|
65
|
+
"usampler3D"
|
|
66
|
+
"usamplerCube"
|
|
67
|
+
"usampler1DArray"
|
|
68
|
+
"usampler2DArray"
|
|
69
|
+
"sampler2DRect"
|
|
70
|
+
"sampler2DRectshadow"
|
|
71
|
+
"isampler2DRect"
|
|
72
|
+
"usampler2DRect"
|
|
73
|
+
"samplerBuffer"
|
|
74
|
+
"isamplerBuffer"
|
|
75
|
+
"usamplerBuffer"
|
|
76
|
+
"samplerCubeArray"
|
|
77
|
+
"samplerCubeArrayShadow"
|
|
78
|
+
"isamplerCubeArray"
|
|
79
|
+
"usamplerCubeArray"
|
|
80
|
+
"sampler2DMS"
|
|
81
|
+
"isampler2DMS"
|
|
82
|
+
"usampler2DMS"
|
|
83
|
+
"sampler2DMSArray"
|
|
84
|
+
"isampler2DMSArray"
|
|
85
|
+
"usampler2DMSArray"
|
|
86
|
+
"image1D"
|
|
87
|
+
"iimage1D"
|
|
88
|
+
"uimage1D"
|
|
89
|
+
"image2D"
|
|
90
|
+
"iimage2D"
|
|
91
|
+
"uimage2D"
|
|
92
|
+
"image3D"
|
|
93
|
+
"iimage3D"
|
|
94
|
+
"uimage3D"
|
|
95
|
+
"image2DRect"
|
|
96
|
+
"iimage2DRect"
|
|
97
|
+
"uimage2DRect"
|
|
98
|
+
"imageCube"
|
|
99
|
+
"iimageCube"
|
|
100
|
+
"uimageCube"
|
|
101
|
+
"imageBuffer"
|
|
102
|
+
"iimageBuffer"
|
|
103
|
+
"uimageBuffer"
|
|
104
|
+
"image1DArray"
|
|
105
|
+
"iimage1DArray"
|
|
106
|
+
"uimage1DArray"
|
|
107
|
+
"image2DArray"
|
|
108
|
+
"iimage2DArray"
|
|
109
|
+
"uimage2DArray"
|
|
110
|
+
"imageCubeArray"
|
|
111
|
+
"iimageCubeArray"
|
|
112
|
+
"uimageCubeArray"
|
|
113
|
+
"image2DMS"
|
|
114
|
+
"iimage2DMS"
|
|
115
|
+
"uimage2DMS"
|
|
116
|
+
"image2DMArray"
|
|
117
|
+
"iimage2DMSArray"
|
|
118
|
+
"uimage2DMSArray"
|
|
119
|
+
"struct"
|
|
120
|
+
*/
|
|
121
|
+
var mapUniformType = function (type) {
|
|
122
|
+
var found = DATA_TYPE_MAP.find(function (_a) {
|
|
123
|
+
var _b = __read(_a, 2), _ = _b[0], set = _b[1];
|
|
124
|
+
return set.has(type);
|
|
125
|
+
});
|
|
126
|
+
if (found) {
|
|
127
|
+
return found[0];
|
|
128
|
+
}
|
|
129
|
+
// console.log(`Unknown uniform type, can't map to graph: ${type}`);
|
|
130
|
+
};
|
|
131
|
+
export var applyUniformStrategy = function (strategy, ast, graphNode, siblingNode) {
|
|
132
|
+
var program = ast;
|
|
133
|
+
return (program.program || []).flatMap(function (node) {
|
|
134
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
135
|
+
// The uniform declaration type, like vec4
|
|
136
|
+
var uniformType = (_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.token;
|
|
137
|
+
var graphDataType = mapUniformType(uniformType);
|
|
138
|
+
// If this is a uniform declaration line
|
|
139
|
+
if (node.type === 'declaration_statement' &&
|
|
140
|
+
node.declaration.type === 'declarator_list' &&
|
|
141
|
+
((_g = (_f = (_e = node.declaration) === null || _e === void 0 ? void 0 : _e.specified_type) === null || _f === void 0 ? void 0 : _f.qualifiers) === null || _g === void 0 ? void 0 : _g.find(function (n) { return n.token === 'uniform'; }))
|
|
142
|
+
// commented this out to allow for sampler2D uniforms to appear as inputs
|
|
143
|
+
// && uniformType !== 'sampler2D'
|
|
144
|
+
) {
|
|
145
|
+
// Capture all the declared names, removing mangling suffix
|
|
146
|
+
var declarations_1 = node.declaration.declarations;
|
|
147
|
+
var names = declarations_1.map(function (d) { return d.identifier.identifier; });
|
|
148
|
+
// Tricky code warning: The flow of preparing a node for the graph is:
|
|
149
|
+
// 1. Produce/mangle the AST (with unmangled names)
|
|
150
|
+
// 2. findInputs() (with unmangled names)
|
|
151
|
+
// 3. The AST is *then* mangled in graph.ts
|
|
152
|
+
// 4. Later, the inputs are filled in, and now, we have an input with
|
|
153
|
+
// the name "x" but the ast now has the mangled name "x_1". So
|
|
154
|
+
// here, we look for the *mangled* name in the strategy runner
|
|
155
|
+
return names.map(function (name) { return [
|
|
156
|
+
nodeInput(name, "uniform_".concat(name), 'uniform', graphDataType, ['code', 'data'], true),
|
|
157
|
+
function (filler) {
|
|
158
|
+
var mangledName = mangleName(name, graphNode, siblingNode);
|
|
159
|
+
// Remove the declaration line, or the declared uniform
|
|
160
|
+
if (declarations_1.length === 1) {
|
|
161
|
+
program.program.splice(program.program.indexOf(node), 1);
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
var decl = node.declaration;
|
|
165
|
+
decl.declarations = decl.declarations.filter(function (d) { return d.identifier.identifier !== mangledName; });
|
|
166
|
+
}
|
|
167
|
+
// And rename all the references to said uniform
|
|
168
|
+
program.scopes[0].bindings[name].references.forEach(function (ref) {
|
|
169
|
+
if (ref.type === 'identifier' && ref.identifier === mangledName) {
|
|
170
|
+
ref.identifier = generateFiller(filler);
|
|
171
|
+
}
|
|
172
|
+
else if (ref.type === 'parameter_declaration' &&
|
|
173
|
+
'identifier' in ref &&
|
|
174
|
+
ref.identifier.identifier === mangledName) {
|
|
175
|
+
ref.identifier.identifier = generateFiller(filler);
|
|
176
|
+
}
|
|
177
|
+
else if ('identifier' in ref) {
|
|
178
|
+
ref.identifier = generateFiller(filler);
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
console.warn('Unknown uniform reference for', graphNode.name, 'ref');
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
return ast;
|
|
185
|
+
},
|
|
186
|
+
]; });
|
|
187
|
+
}
|
|
188
|
+
return [];
|
|
189
|
+
});
|
|
190
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { BaseStrategy, ApplyStrategy, StrategyType } from '.';
|
|
2
|
+
export interface VariableStrategy extends BaseStrategy {
|
|
3
|
+
type: StrategyType.VARIABLE;
|
|
4
|
+
}
|
|
5
|
+
export declare const variableStrategy: () => VariableStrategy;
|
|
6
|
+
export declare const applyVariableStrategy: ApplyStrategy<VariableStrategy>;
|
|
@@ -0,0 +1,80 @@
|
|
|
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 { nodeInput } from '../graph/base-node';
|
|
38
|
+
import { StrategyType } from '.';
|
|
39
|
+
import { generateFiller } from '../util/ast';
|
|
40
|
+
export var variableStrategy = function () { return ({
|
|
41
|
+
type: StrategyType.VARIABLE,
|
|
42
|
+
config: {},
|
|
43
|
+
}); };
|
|
44
|
+
export var applyVariableStrategy = function (strategy, ast, graphNode, siblingNode) {
|
|
45
|
+
var program = ast;
|
|
46
|
+
return Object.values(program.scopes.reduce(function (acc, scope) { return (__assign(__assign({}, acc), scope.bindings)); }, {})).flatMap(function (binding) {
|
|
47
|
+
return binding.references.reduce(function (acc, ref) {
|
|
48
|
+
var identifier, replacer;
|
|
49
|
+
if (ref.type === 'declaration') {
|
|
50
|
+
identifier = ref.identifier.identifier;
|
|
51
|
+
replacer = function (fillerAst) {
|
|
52
|
+
ref.identifier.identifier = generateFiller(fillerAst);
|
|
53
|
+
return ast;
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
else if (ref.type === 'identifier') {
|
|
57
|
+
identifier = ref.identifier;
|
|
58
|
+
replacer = function (fillerAst) {
|
|
59
|
+
ref.identifier = generateFiller(fillerAst);
|
|
60
|
+
return ast;
|
|
61
|
+
};
|
|
62
|
+
// } else if (ref.type === 'parameter_declaration') {
|
|
63
|
+
// identifier = ref.declaration.identifier.identifier;
|
|
64
|
+
// replacer = (fillerAst: AstNode) => {
|
|
65
|
+
// ref.declaration.identifier.identifier = generate(fillerAst);
|
|
66
|
+
// };
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
return acc;
|
|
70
|
+
}
|
|
71
|
+
return __spreadArray(__spreadArray([], __read(acc), false), [
|
|
72
|
+
[
|
|
73
|
+
nodeInput(identifier, "filler_".concat(identifier), 'filler', undefined, // Data type for what plugs into this filler
|
|
74
|
+
['code', 'data'], false),
|
|
75
|
+
replacer,
|
|
76
|
+
],
|
|
77
|
+
], false);
|
|
78
|
+
}, []);
|
|
79
|
+
});
|
|
80
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions to work with ASTs
|
|
3
|
+
*/
|
|
4
|
+
import { Filler } from '../graph/parsers';
|
|
5
|
+
import { AstNode, ExpressionStatementNode, FunctionNode, DeclarationNode } from '@shaderfrog/glsl-parser/ast';
|
|
6
|
+
import { Program } from '@shaderfrog/glsl-parser/ast';
|
|
7
|
+
import { ShaderStage } from '../graph/graph-types';
|
|
8
|
+
import { Scope } from '@shaderfrog/glsl-parser/parser/scope';
|
|
9
|
+
export declare const findVec4Constructor: (ast: AstNode) => AstNode | undefined;
|
|
10
|
+
export declare const findAssignmentTo: (ast: AstNode | Program, assignTo: string) => ExpressionStatementNode | undefined;
|
|
11
|
+
export declare const findDeclarationOf: (ast: AstNode | Program, declarationOf: string) => DeclarationNode | undefined;
|
|
12
|
+
export declare const from2To3: (ast: Program, stage: ShaderStage) => void;
|
|
13
|
+
export declare const outDeclaration: (name: string) => Object;
|
|
14
|
+
export declare const makeStatement: (stmt: string) => AstNode;
|
|
15
|
+
export declare const makeFnStatement: (fnStmt: string) => AstNode;
|
|
16
|
+
export declare const makeExpression: (expr: string) => AstNode;
|
|
17
|
+
export declare const makeExpressionWithScopes: (expr: string) => {
|
|
18
|
+
scope: Scope;
|
|
19
|
+
expression: AstNode;
|
|
20
|
+
};
|
|
21
|
+
export declare const makeFnBodyStatementWithScopes: (body: string) => {
|
|
22
|
+
scope: Scope;
|
|
23
|
+
statements: AstNode[];
|
|
24
|
+
};
|
|
25
|
+
export declare const findFn: (ast: Program, name: string) => FunctionNode | undefined;
|
|
26
|
+
export declare const returnGlPosition: (fnName: string, ast: Program) => void;
|
|
27
|
+
export declare const returnGlPositionHardCoded: (fnName: string, ast: Program, returnType: string, hardCodedReturn: string) => void;
|
|
28
|
+
export declare const returnGlPositionVec3Right: (fnName: string, ast: Program) => void;
|
|
29
|
+
export declare const convert300MainToReturn: (suffix: string, ast: Program) => void;
|
|
30
|
+
export declare const generateFiller: (filler: Filler) => string;
|
package/esm/util/ast.js
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
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
|
+
var node = path.node;
|
|
243
|
+
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' &&
|
|
244
|
+
((_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.'))) {
|
|
245
|
+
found = node.args[0];
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
},
|
|
249
|
+
});
|
|
250
|
+
if (!found) {
|
|
251
|
+
console.error(generate(ast));
|
|
252
|
+
throw new Error('Could not find position assignment to convert to return!');
|
|
253
|
+
}
|
|
254
|
+
return found;
|
|
255
|
+
});
|
|
256
|
+
};
|
|
257
|
+
var convertVertexMain = function (fnName, ast, returnType, generateRight) {
|
|
258
|
+
var mainReturnVar = "frogOut";
|
|
259
|
+
var main = findFn(ast, fnName);
|
|
260
|
+
if (!main) {
|
|
261
|
+
throw new Error("No ".concat(fnName, " fn found!"));
|
|
262
|
+
}
|
|
263
|
+
// Convert the main function to one that returns
|
|
264
|
+
main.prototype.header.returnType.specifier.specifier.token =
|
|
265
|
+
returnType;
|
|
266
|
+
// Find the gl_position assignment line
|
|
267
|
+
var assign = main.body.statements.find(function (stmt) {
|
|
268
|
+
var _a;
|
|
269
|
+
return stmt.type === 'expression_statement' &&
|
|
270
|
+
((_a = stmt.expression.left) === null || _a === void 0 ? void 0 : _a.identifier) === 'gl_Position';
|
|
271
|
+
});
|
|
272
|
+
if (!assign) {
|
|
273
|
+
throw new Error("No gl position assign found in main fn!");
|
|
274
|
+
}
|
|
275
|
+
var rtnStmt = makeFnStatement("".concat(returnType, " ").concat(mainReturnVar, " = 1.0"));
|
|
276
|
+
rtnStmt.declaration.declarations[0].initializer =
|
|
277
|
+
generateRight(assign);
|
|
278
|
+
main.body.statements.splice(main.body.statements.indexOf(assign), 1, rtnStmt);
|
|
279
|
+
main.body.statements.push(makeFnStatement("return ".concat(mainReturnVar)));
|
|
280
|
+
};
|
|
281
|
+
export var convert300MainToReturn = function (suffix, ast) {
|
|
282
|
+
var mainReturnVar = "frogOut_".concat(suffix);
|
|
283
|
+
// Find the output variable, as in "pc_fragColor" from "out highp vec4 pc_fragColor;"
|
|
284
|
+
var outName;
|
|
285
|
+
ast.program.find(function (line, index) {
|
|
286
|
+
var _a, _b;
|
|
287
|
+
var declaration = line === null || line === void 0 ? void 0 : line.declaration;
|
|
288
|
+
if (
|
|
289
|
+
// line.type === 'declaration_statement' &&
|
|
290
|
+
((_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'; })) &&
|
|
291
|
+
declaration.specified_type.specifier.specifier.token ===
|
|
292
|
+
'vec4') {
|
|
293
|
+
// Remove the out declaration
|
|
294
|
+
ast.program.splice(index, 1);
|
|
295
|
+
outName = declaration.declarations[0].identifier.identifier;
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
});
|
|
299
|
+
if (!outName) {
|
|
300
|
+
console.error(generate(ast));
|
|
301
|
+
throw new Error('No "out vec4" line found in the fragment shader');
|
|
302
|
+
}
|
|
303
|
+
ast.program.unshift(makeStatement("vec4 ".concat(mainReturnVar)));
|
|
304
|
+
visit(ast, {
|
|
305
|
+
identifier: {
|
|
306
|
+
enter: function (path) {
|
|
307
|
+
if (path.node.identifier === outName) {
|
|
308
|
+
path.node.identifier = mainReturnVar;
|
|
309
|
+
// @ts-ignore
|
|
310
|
+
path.node.doNotDescope = true; // hack because this var is in the scope which gets renamed later
|
|
311
|
+
}
|
|
312
|
+
},
|
|
313
|
+
},
|
|
314
|
+
function: {
|
|
315
|
+
enter: function (path) {
|
|
316
|
+
if (path.node.prototype.header.name.identifier === 'main') {
|
|
317
|
+
path.node.prototype.header.returnType.specifier
|
|
318
|
+
.specifier.token = 'vec4';
|
|
319
|
+
path.node.body.statements.push(makeFnStatement("return ".concat(mainReturnVar)));
|
|
320
|
+
}
|
|
321
|
+
},
|
|
322
|
+
},
|
|
323
|
+
});
|
|
324
|
+
};
|
|
325
|
+
export var generateFiller = function (filler) {
|
|
326
|
+
if (!filler) {
|
|
327
|
+
throw new Error('Cannot generate void filler!');
|
|
328
|
+
}
|
|
329
|
+
return Array.isArray(filler)
|
|
330
|
+
? filler.map(generate).join('')
|
|
331
|
+
: generate(filler);
|
|
332
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const ensure: <T>(argument: T, message?: string) => T;
|
package/esm/util/id.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const makeId: () => string;
|
package/esm/util/id.js
ADDED