@shaderfrog/core 1.2.0 → 1.3.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 (155) hide show
  1. package/cjs/package.json +1 -0
  2. package/esm/engine.d.ts +69 -0
  3. package/esm/engine.js +209 -0
  4. package/esm/graph/base-node.d.ts +36 -0
  5. package/esm/graph/base-node.js +9 -0
  6. package/esm/graph/code-nodes.d.ts +44 -0
  7. package/esm/graph/code-nodes.js +18 -0
  8. package/esm/graph/context.d.ts +37 -0
  9. package/esm/graph/context.js +243 -0
  10. package/esm/graph/data-nodes.d.ts +83 -0
  11. package/esm/graph/data-nodes.js +131 -0
  12. package/esm/graph/edge.d.ts +13 -0
  13. package/esm/graph/edge.js +7 -0
  14. package/esm/graph/evaluate.d.ts +9 -0
  15. package/esm/graph/evaluate.js +27 -0
  16. package/esm/graph/graph-node.d.ts +8 -0
  17. package/esm/graph/graph-node.js +135 -0
  18. package/esm/graph/graph-types.d.ts +38 -0
  19. package/esm/graph/graph-types.js +22 -0
  20. package/esm/graph/graph.d.ts +89 -0
  21. package/esm/graph/graph.js +506 -0
  22. package/esm/graph/graph.test.d.ts +1 -0
  23. package/esm/graph/graph.test.js +168 -0
  24. package/esm/graph/index.d.ts +11 -0
  25. package/esm/graph/index.js +11 -0
  26. package/esm/graph/parsers.d.ts +39 -0
  27. package/esm/graph/parsers.js +213 -0
  28. package/esm/graph/shader-sections.d.ts +47 -0
  29. package/esm/graph/shader-sections.js +256 -0
  30. package/esm/index.d.ts +3 -0
  31. package/esm/index.js +3 -0
  32. package/esm/package.json +1 -0
  33. package/esm/plugins/babylon/bablyengine.d.ts +28 -0
  34. package/esm/plugins/babylon/bablyengine.js +589 -0
  35. package/esm/plugins/babylon/importers.d.ts +3 -0
  36. package/esm/plugins/babylon/importers.js +64 -0
  37. package/esm/plugins/babylon/index.d.ts +2 -0
  38. package/esm/plugins/babylon/index.js +2 -0
  39. package/esm/plugins/playcanvas/importers.d.ts +3 -0
  40. package/esm/plugins/playcanvas/importers.js +28 -0
  41. package/esm/plugins/playcanvas/index.d.ts +2 -0
  42. package/esm/plugins/playcanvas/index.js +2 -0
  43. package/esm/plugins/playcanvas/playengine.d.ts +39 -0
  44. package/esm/plugins/playcanvas/playengine.js +517 -0
  45. package/esm/plugins/three/importers.d.ts +3 -0
  46. package/esm/plugins/three/importers.js +15 -0
  47. package/esm/plugins/three/index.d.ts +2 -0
  48. package/esm/plugins/three/index.js +2 -0
  49. package/esm/plugins/three/threngine.d.ts +34 -0
  50. package/esm/plugins/three/threngine.js +608 -0
  51. package/esm/strategy/assignemntTo.d.ts +9 -0
  52. package/esm/strategy/assignemntTo.js +26 -0
  53. package/esm/strategy/declarationOf.d.ts +9 -0
  54. package/esm/strategy/declarationOf.js +23 -0
  55. package/esm/strategy/hardCode.d.ts +15 -0
  56. package/esm/strategy/hardCode.js +23 -0
  57. package/esm/strategy/index.d.ts +9 -0
  58. package/esm/strategy/index.js +9 -0
  59. package/esm/strategy/inject.d.ts +15 -0
  60. package/esm/strategy/inject.js +122 -0
  61. package/esm/strategy/namedAttribute.d.ts +9 -0
  62. package/esm/strategy/namedAttribute.js +48 -0
  63. package/esm/strategy/strategy.d.ts +28 -0
  64. package/esm/strategy/strategy.js +31 -0
  65. package/esm/strategy/stratgies.test.d.ts +1 -0
  66. package/esm/strategy/stratgies.test.js +164 -0
  67. package/esm/strategy/texture2D.d.ts +6 -0
  68. package/esm/strategy/texture2D.js +83 -0
  69. package/esm/strategy/uniform.d.ts +6 -0
  70. package/esm/strategy/uniform.js +190 -0
  71. package/esm/strategy/variable.d.ts +6 -0
  72. package/esm/strategy/variable.js +80 -0
  73. package/esm/util/ast.d.ts +30 -0
  74. package/esm/util/ast.js +332 -0
  75. package/esm/util/ensure.d.ts +1 -0
  76. package/esm/util/ensure.js +7 -0
  77. package/esm/util/id.d.ts +1 -0
  78. package/esm/util/id.js +2 -0
  79. package/package.json +9 -11
  80. /package/{engine.d.ts → cjs/engine.d.ts} +0 -0
  81. /package/{engine.js → cjs/engine.js} +0 -0
  82. /package/{graph → cjs/graph}/base-node.d.ts +0 -0
  83. /package/{graph → cjs/graph}/base-node.js +0 -0
  84. /package/{graph → cjs/graph}/code-nodes.d.ts +0 -0
  85. /package/{graph → cjs/graph}/code-nodes.js +0 -0
  86. /package/{graph → cjs/graph}/context.d.ts +0 -0
  87. /package/{graph → cjs/graph}/context.js +0 -0
  88. /package/{graph → cjs/graph}/data-nodes.d.ts +0 -0
  89. /package/{graph → cjs/graph}/data-nodes.js +0 -0
  90. /package/{graph → cjs/graph}/edge.d.ts +0 -0
  91. /package/{graph → cjs/graph}/edge.js +0 -0
  92. /package/{graph → cjs/graph}/evaluate.d.ts +0 -0
  93. /package/{graph → cjs/graph}/evaluate.js +0 -0
  94. /package/{graph → cjs/graph}/graph-node.d.ts +0 -0
  95. /package/{graph → cjs/graph}/graph-node.js +0 -0
  96. /package/{graph → cjs/graph}/graph-types.d.ts +0 -0
  97. /package/{graph → cjs/graph}/graph-types.js +0 -0
  98. /package/{graph → cjs/graph}/graph.d.ts +0 -0
  99. /package/{graph → cjs/graph}/graph.js +0 -0
  100. /package/{graph → cjs/graph}/graph.test.d.ts +0 -0
  101. /package/{graph → cjs/graph}/graph.test.js +0 -0
  102. /package/{graph → cjs/graph}/index.d.ts +0 -0
  103. /package/{graph → cjs/graph}/index.js +0 -0
  104. /package/{graph → cjs/graph}/parsers.d.ts +0 -0
  105. /package/{graph → cjs/graph}/parsers.js +0 -0
  106. /package/{graph → cjs/graph}/shader-sections.d.ts +0 -0
  107. /package/{graph → cjs/graph}/shader-sections.js +0 -0
  108. /package/{index.d.ts → cjs/index.d.ts} +0 -0
  109. /package/{index.js → cjs/index.js} +0 -0
  110. /package/{plugins → cjs/plugins}/babylon/bablyengine.d.ts +0 -0
  111. /package/{plugins → cjs/plugins}/babylon/bablyengine.js +0 -0
  112. /package/{plugins → cjs/plugins}/babylon/importers.d.ts +0 -0
  113. /package/{plugins → cjs/plugins}/babylon/importers.js +0 -0
  114. /package/{plugins → cjs/plugins}/babylon/index.d.ts +0 -0
  115. /package/{plugins → cjs/plugins}/babylon/index.js +0 -0
  116. /package/{plugins → cjs/plugins}/playcanvas/importers.d.ts +0 -0
  117. /package/{plugins → cjs/plugins}/playcanvas/importers.js +0 -0
  118. /package/{plugins → cjs/plugins}/playcanvas/index.d.ts +0 -0
  119. /package/{plugins → cjs/plugins}/playcanvas/index.js +0 -0
  120. /package/{plugins → cjs/plugins}/playcanvas/playengine.d.ts +0 -0
  121. /package/{plugins → cjs/plugins}/playcanvas/playengine.js +0 -0
  122. /package/{plugins → cjs/plugins}/three/importers.d.ts +0 -0
  123. /package/{plugins → cjs/plugins}/three/importers.js +0 -0
  124. /package/{plugins → cjs/plugins}/three/index.d.ts +0 -0
  125. /package/{plugins → cjs/plugins}/three/index.js +0 -0
  126. /package/{plugins → cjs/plugins}/three/threngine.d.ts +0 -0
  127. /package/{plugins → cjs/plugins}/three/threngine.js +0 -0
  128. /package/{strategy → cjs/strategy}/assignemntTo.d.ts +0 -0
  129. /package/{strategy → cjs/strategy}/assignemntTo.js +0 -0
  130. /package/{strategy → cjs/strategy}/declarationOf.d.ts +0 -0
  131. /package/{strategy → cjs/strategy}/declarationOf.js +0 -0
  132. /package/{strategy → cjs/strategy}/hardCode.d.ts +0 -0
  133. /package/{strategy → cjs/strategy}/hardCode.js +0 -0
  134. /package/{strategy → cjs/strategy}/index.d.ts +0 -0
  135. /package/{strategy → cjs/strategy}/index.js +0 -0
  136. /package/{strategy → cjs/strategy}/inject.d.ts +0 -0
  137. /package/{strategy → cjs/strategy}/inject.js +0 -0
  138. /package/{strategy → cjs/strategy}/namedAttribute.d.ts +0 -0
  139. /package/{strategy → cjs/strategy}/namedAttribute.js +0 -0
  140. /package/{strategy → cjs/strategy}/strategy.d.ts +0 -0
  141. /package/{strategy → cjs/strategy}/strategy.js +0 -0
  142. /package/{strategy → cjs/strategy}/stratgies.test.d.ts +0 -0
  143. /package/{strategy → cjs/strategy}/stratgies.test.js +0 -0
  144. /package/{strategy → cjs/strategy}/texture2D.d.ts +0 -0
  145. /package/{strategy → cjs/strategy}/texture2D.js +0 -0
  146. /package/{strategy → cjs/strategy}/uniform.d.ts +0 -0
  147. /package/{strategy → cjs/strategy}/uniform.js +0 -0
  148. /package/{strategy → cjs/strategy}/variable.d.ts +0 -0
  149. /package/{strategy → cjs/strategy}/variable.js +0 -0
  150. /package/{util → cjs/util}/ast.d.ts +0 -0
  151. /package/{util → cjs/util}/ast.js +0 -0
  152. /package/{util → cjs/util}/ensure.d.ts +0 -0
  153. /package/{util → cjs/util}/ensure.js +0 -0
  154. /package/{util → cjs/util}/id.d.ts +0 -0
  155. /package/{util → cjs/util}/id.js +0 -0
@@ -0,0 +1,11 @@
1
+ export * from './base-node';
2
+ export * from './data-nodes';
3
+ export * from './graph-node';
4
+ export * from './code-nodes';
5
+ export * from './edge';
6
+ export * from './parsers';
7
+ export * from './context';
8
+ export * from './evaluate';
9
+ export * from './graph-types';
10
+ export * from './graph';
11
+ export * from './shader-sections';
@@ -0,0 +1,11 @@
1
+ export * from './base-node';
2
+ export * from './data-nodes';
3
+ export * from './graph-node';
4
+ export * from './code-nodes';
5
+ export * from './edge';
6
+ export * from './parsers';
7
+ export * from './context';
8
+ export * from './evaluate';
9
+ export * from './graph-types';
10
+ export * from './graph';
11
+ export * from './shader-sections';
@@ -0,0 +1,39 @@
1
+ import { AstNode, Program } from '@shaderfrog/glsl-parser/ast';
2
+ import { Engine, EngineContext } from '../engine';
3
+ import { Edge } from './edge';
4
+ import { SourceNode } from './code-nodes';
5
+ import { NodeInput } from './base-node';
6
+ import { Graph } from './graph-types';
7
+ import { Evaluate } from './evaluate';
8
+ export declare const alphabet = "abcdefghijklmnopqrstuvwxyz";
9
+ export type Filler = AstNode | AstNode[] | void;
10
+ export type InputFiller = (filler: Filler) => AstNode | Program;
11
+ export type InputFillerGroup = {
12
+ filler: InputFiller;
13
+ backfillArgs?: AstNode[];
14
+ };
15
+ export type InputFillers = Record<string, InputFillerGroup>;
16
+ type FillerArguments = AstNode[];
17
+ export type ComputedInput = [NodeInput, InputFiller, FillerArguments?];
18
+ export type ProduceAst = (engineContext: EngineContext, engine: Engine, graph: Graph, node: SourceNode, inputEdges: Edge[]) => AstNode | Program;
19
+ export type OnBeforeCompile = (graph: Graph, engineContext: EngineContext, node: SourceNode, sibling?: SourceNode) => Promise<void>;
20
+ export type ManipulateAst = (engineContext: EngineContext, engine: Engine, graph: Graph, ast: AstNode | Program, inputEdges: Edge[], node: SourceNode, sibling: SourceNode) => AstNode | Program;
21
+ export type NodeParser = {
22
+ onBeforeCompile?: OnBeforeCompile;
23
+ manipulateAst?: ManipulateAst;
24
+ findInputs?: FindInputs;
25
+ produceFiller?: ProduceNodeFiller;
26
+ };
27
+ export type FindInputs = (engineContext: EngineContext, ast: Program | AstNode, inputEdges: Edge[], node: SourceNode, sibling: SourceNode) => ComputedInput[];
28
+ export type ProduceNodeFiller = (node: SourceNode, ast: Program | AstNode) => Filler;
29
+ type CoreNodeParser = {
30
+ produceAst: ProduceAst;
31
+ findInputs: FindInputs;
32
+ produceFiller: ProduceNodeFiller;
33
+ evaluate?: Evaluate;
34
+ };
35
+ type CoreParser = {
36
+ [key: string]: CoreNodeParser;
37
+ };
38
+ export declare const coreParsers: CoreParser;
39
+ export {};
@@ -0,0 +1,213 @@
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
+ var _a;
27
+ import { generate, parser } from '@shaderfrog/glsl-parser';
28
+ import { visit, } from '@shaderfrog/glsl-parser/ast';
29
+ import preprocess from '@shaderfrog/glsl-parser/preprocessor';
30
+ import { convert300MainToReturn, from2To3, makeExpression, makeExpressionWithScopes, makeFnBodyStatementWithScopes, makeFnStatement, } from '../util/ast';
31
+ import { applyStrategy } from '../strategy';
32
+ import { SourceType } from './code-nodes';
33
+ import { nodeInput } from './base-node';
34
+ import { MAGIC_OUTPUT_STMTS, NodeType } from './graph-types';
35
+ import { nodeName } from './graph';
36
+ import { generateFiller } from '../util/ast';
37
+ /*
38
+ * Core graph parsers, which is the plumbing/interface the graph and context
39
+ * calls into, to parse, find inputs, etc, and define this per-node type.
40
+ */
41
+ var log = function () {
42
+ var _a;
43
+ var args = [];
44
+ for (var _i = 0; _i < arguments.length; _i++) {
45
+ args[_i] = arguments[_i];
46
+ }
47
+ return (_a = console.log).call.apply(_a, __spreadArray([console, '\x1b[31m(core.parsers)\x1b[0m'], __read(args), false));
48
+ };
49
+ export var alphabet = 'abcdefghijklmnopqrstuvwxyz';
50
+ export var coreParsers = (_a = {},
51
+ _a[NodeType.SOURCE] = {
52
+ produceAst: function (engineContext, engine, graph, node, inputEdges) {
53
+ var ast;
54
+ // @ts-ignore
55
+ if (node.expressionOnly) {
56
+ node.sourceType = SourceType.EXPRESSION;
57
+ // @ts-ignore
58
+ delete node.expressionOnly;
59
+ }
60
+ if (node.sourceType === SourceType.FN_BODY_FRAGMENT) {
61
+ var _a = makeFnBodyStatementWithScopes(node.source), statements = _a.statements, scope = _a.scope;
62
+ ast = {
63
+ type: 'program',
64
+ scopes: [scope],
65
+ // @ts-ignore
66
+ program: statements,
67
+ };
68
+ }
69
+ else if (node.sourceType === SourceType.EXPRESSION) {
70
+ var _b = makeExpressionWithScopes(node.source), expression = _b.expression, scope = _b.scope;
71
+ ast = {
72
+ type: 'program',
73
+ scopes: [scope],
74
+ // @ts-ignore
75
+ program: [expression],
76
+ };
77
+ }
78
+ else {
79
+ var preprocessed = node.config.preprocess === false
80
+ ? node.source
81
+ : preprocess(node.source, {
82
+ preserve: {
83
+ version: function () { return true; },
84
+ },
85
+ });
86
+ ast = parser.parse(preprocessed);
87
+ if (node.config.version === 2 && node.stage) {
88
+ from2To3(ast, node.stage);
89
+ log('converted ', node, 'to version 3', {
90
+ code: generate(ast),
91
+ });
92
+ }
93
+ // This assumes that expressionOnly nodes don't have a stage and that all
94
+ // fragment source code shades have main function, which is probably wrong
95
+ if (node.stage === 'fragment') {
96
+ convert300MainToReturn(node.id, ast);
97
+ }
98
+ }
99
+ return ast;
100
+ },
101
+ findInputs: function (engineContext, ast, edges, node, sibling) {
102
+ var seen = new Set();
103
+ return node.config.strategies
104
+ .flatMap(function (strategy) { return applyStrategy(strategy, ast, node, sibling); })
105
+ .filter(function (_a) {
106
+ var _b = __read(_a, 2), input = _b[0], _ = _b[1];
107
+ if (!seen.has(input.id)) {
108
+ seen.add(input.id);
109
+ return true;
110
+ }
111
+ return false;
112
+ });
113
+ },
114
+ produceFiller: function (node, ast) {
115
+ return node.sourceType === SourceType.EXPRESSION
116
+ ? ast.program[0]
117
+ : node.sourceType === SourceType.FN_BODY_FRAGMENT
118
+ ? ast.program
119
+ : makeExpression("".concat(nodeName(node), "()"));
120
+ },
121
+ },
122
+ // TODO: Output node assumes strategies are still passed in on node creation,
123
+ // which might be a little awkward for graph creators?
124
+ _a[NodeType.OUTPUT] = {
125
+ produceAst: function (engineContext, engine, graph, node, inputEdges) {
126
+ return parser.parse(node.source);
127
+ },
128
+ findInputs: function (engineContext, ast, edges, node, sibling) {
129
+ return __spreadArray(__spreadArray([], __read(node.config.strategies.flatMap(function (strategy) {
130
+ return applyStrategy(strategy, ast, node, sibling);
131
+ })), false), [
132
+ [
133
+ nodeInput(MAGIC_OUTPUT_STMTS, "filler_".concat(MAGIC_OUTPUT_STMTS), 'filler', 'rgba', ['code'], false),
134
+ function (fillerAst) {
135
+ var fn = ast.program.find(function (stmt) { return stmt.type === 'function'; });
136
+ fn === null || fn === void 0 ? void 0 : fn.body.statements.unshift(makeFnStatement(generateFiller(fillerAst)));
137
+ return ast;
138
+ },
139
+ ],
140
+ ], false);
141
+ },
142
+ produceFiller: function (node, ast) {
143
+ return makeExpression('impossible_call()');
144
+ },
145
+ },
146
+ _a[NodeType.BINARY] = {
147
+ produceAst: function (engineContext, engine, graph, iNode, inputEdges) {
148
+ var node = iNode;
149
+ return makeExpression('(' +
150
+ (inputEdges.length
151
+ ? inputEdges
152
+ .map(function (_, index) { return alphabet.charAt(index); })
153
+ .join(" ".concat(node.operator, " "))
154
+ : "a ".concat(node.operator, " b")) +
155
+ ')');
156
+ },
157
+ findInputs: function (engineContext, ast, inputEdges, node, sibling) {
158
+ return new Array(Math.max(inputEdges.length + 1, 2))
159
+ .fill(0)
160
+ .map(function (_, index) {
161
+ var letter = alphabet.charAt(index);
162
+ return [
163
+ nodeInput(letter, letter, 'filler', undefined, ['data', 'code'], false),
164
+ function (fillerAst) {
165
+ var foundPath;
166
+ var visitors = {
167
+ identifier: {
168
+ enter: function (path) {
169
+ if (path.node.identifier === letter) {
170
+ foundPath = path;
171
+ }
172
+ },
173
+ },
174
+ };
175
+ visit(ast, visitors);
176
+ if (!foundPath) {
177
+ throw new Error("Im drunk and I think this case is impossible, no \"".concat(letter, "\" found in binary node?"));
178
+ }
179
+ if (foundPath.parent && foundPath.key) {
180
+ // @ts-ignore
181
+ foundPath.parent[foundPath.key] = fillerAst;
182
+ return ast;
183
+ }
184
+ else {
185
+ return fillerAst;
186
+ }
187
+ },
188
+ ];
189
+ });
190
+ },
191
+ produceFiller: function (node, ast) {
192
+ return ast;
193
+ },
194
+ evaluate: function (node, inputEdges, inputNodes, evaluateNode) {
195
+ var operator = node.operator;
196
+ return inputNodes.map(evaluateNode).reduce(function (num, next) {
197
+ if (operator === '+') {
198
+ return num + next;
199
+ }
200
+ else if (operator === '*') {
201
+ return num * next;
202
+ }
203
+ else if (operator === '-') {
204
+ return num - next;
205
+ }
206
+ else if (operator === '/') {
207
+ return num / next;
208
+ }
209
+ throw new Error("Don't know how to evaluate ".concat(operator, " for node ").concat(node.name, " (").concat(node.id, ")"));
210
+ });
211
+ },
212
+ },
213
+ _a);
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Categorizing / deduping parts of shaders to help merge them together
3
+ */
4
+ import { AstNode, DeclarationStatementNode, PreprocessorNode } from '@shaderfrog/glsl-parser/ast';
5
+ import { Program } from '@shaderfrog/glsl-parser/ast';
6
+ export interface ShaderSections {
7
+ precision: DeclarationStatementNode[];
8
+ version: AstNode[];
9
+ preprocessor: PreprocessorNode[];
10
+ structs: AstNode[];
11
+ inStatements: DeclarationStatementNode[];
12
+ outStatements: DeclarationStatementNode[];
13
+ uniforms: DeclarationStatementNode[];
14
+ program: AstNode[];
15
+ }
16
+ export declare const emptyShaderSections: () => ShaderSections;
17
+ declare enum Precision {
18
+ highp = 2,
19
+ mediump = 1,
20
+ lowp = 0
21
+ }
22
+ export declare const higherPrecision: (p1: Precision, p2: Precision) => Precision;
23
+ export declare const dedupeVersions: (nodes: AstNode[]) => AstNode;
24
+ export declare const highestPrecisions: (nodes: DeclarationStatementNode[]) => DeclarationStatementNode[];
25
+ export declare const dedupeQualifiedStatements: (statements: DeclarationStatementNode[], qualifier: string) => any;
26
+ /**
27
+ * Merge uniforms together into lists of identifiers under the same type.
28
+ * There's special case handling for mixing of uniforms with "interface blocks"
29
+ * and those without when merging to make sure the interface block definition is
30
+ * preserved. Check out the tests for more.
31
+ *
32
+ * This function consumes uniforms as found by findShaderSections, so the
33
+ * definitions must line up
34
+ */
35
+ export declare const dedupeUniforms: (statements: DeclarationStatementNode[]) => any;
36
+ export declare const mergeShaderSections: (s1: ShaderSections, s2: ShaderSections) => ShaderSections;
37
+ export type MergeOptions = {
38
+ includePrecisions: boolean;
39
+ includeVersion: boolean;
40
+ };
41
+ export declare const shaderSectionsToProgram: (sections: ShaderSections, mergeOptions: MergeOptions) => Program;
42
+ /**
43
+ * Group an AST into logical sections. The output of this funciton is consumed
44
+ * by the dedupe methods, namely dedupeUniforms, so the data shapes are coupled
45
+ */
46
+ export declare const findShaderSections: (ast: Program) => ShaderSections;
47
+ export {};
@@ -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 '../util/ast';
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
+ };
package/esm/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './graph';
2
+ export * from './strategy';
3
+ export * from './engine';
package/esm/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from './graph';
2
+ export * from './strategy';
3
+ export * from './engine';
@@ -0,0 +1 @@
1
+ {"type": "module"}
@@ -0,0 +1,28 @@
1
+ import { PBRMaterial, Camera, Scene } from 'babylonjs';
2
+ import { Engine } from '../../engine';
3
+ import { ShaderStage } from '../../graph/graph-types';
4
+ import { CodeNode } from '../../graph/code-nodes';
5
+ import { NodePosition } from '../../graph/base-node';
6
+ import { UniformDataType } from '../../graph/data-nodes';
7
+ export declare const physicalDefaultProperties: Partial<Record<keyof PBRMaterial, any>>;
8
+ export declare const physicalNode: (id: string, name: string, position: NodePosition, uniforms: UniformDataType[], stage: ShaderStage | undefined) => CodeNode;
9
+ export type RuntimeContext = {
10
+ scene: Scene;
11
+ camera: Camera;
12
+ BABYLON: any;
13
+ sceneData: any;
14
+ cache: {
15
+ data: {
16
+ [key: string]: any;
17
+ };
18
+ nodes: {
19
+ [id: string]: {
20
+ fragment: string;
21
+ vertex: string;
22
+ };
23
+ };
24
+ };
25
+ };
26
+ export declare const toonNode: (id: string, name: string, position: NodePosition, uniforms: UniformDataType[], stage: ShaderStage | undefined) => CodeNode;
27
+ export declare let mIdx: number;
28
+ export declare const babylengine: Engine;