-
-
Notifications
You must be signed in to change notification settings - Fork 36.4k
Expand file tree
/
Copy pathStackNode.js
More file actions
425 lines (277 loc) · 8.05 KB
/
StackNode.js
File metadata and controls
425 lines (277 loc) · 8.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import Node from './Node.js';
import { select } from '../math/ConditionalNode.js';
import { ShaderNode, nodeProxy, getCurrentStack, setCurrentStack, nodeObject } from '../tsl/TSLBase.js';
import { error } from '../../utils.js';
/**
* Stack is a helper for Nodes that need to produce stack-based code instead of continuous flow.
* They are usually needed in cases like `If`, `Else`.
*
* @augments Node
*/
class StackNode extends Node {
static get type() {
return 'StackNode';
}
/**
* Constructs a new stack node.
*
* @param {?StackNode} [parent=null] - The parent stack node.
*/
constructor( parent = null ) {
super();
/**
* List of nodes.
*
* @type {Array<Node>}
*/
this.nodes = [];
/**
* The output node.
*
* @type {?Node}
* @default null
*/
this.outputNode = null;
/**
* The parent stack node.
*
* @type {?StackNode}
* @default null
*/
this.parent = parent;
/**
* The current conditional node.
*
* @private
* @type {ConditionalNode}
* @default null
*/
this._currentCond = null;
/**
* The expression node. Only
* relevant for Switch/Case.
*
* @private
* @type {Node}
* @default null
*/
this._expressionNode = null;
/**
* The current node being processed.
*
* @private
* @type {Node}
* @default null
*/
this._currentNode = null;
/**
* This flag can be used for type testing.
*
* @type {boolean}
* @readonly
* @default true
*/
this.isStackNode = true;
}
getElementType( builder ) {
return this.hasOutput( builder ) ? this.outputNode.getElementType( builder ) : 'void';
}
getNodeType( builder ) {
return this.hasOutput( builder ) ? this.outputNode.getNodeType( builder ) : 'void';
}
getMemberType( builder, name ) {
return this.hasOutput( builder ) ? this.outputNode.getMemberType( builder, name ) : 'void';
}
/**
* Adds a node to this stack.
*
* @param {Node} node - The node to add.
* @param {number} [index=this.nodes.length] - The index where the node should be added.
* @return {StackNode} A reference to this stack node.
*/
addToStack( node, index = this.nodes.length ) {
if ( node.isNode !== true ) {
error( 'TSL: Invalid node added to stack.' );
return this;
}
this.nodes.splice( index, 0, node );
return this;
}
/**
* Adds a node to the stack before the current node.
*
* @param {Node} node - The node to add.
* @return {StackNode} A reference to this stack node.
*/
addToStackBefore( node ) {
const index = this._currentNode ? this.nodes.indexOf( this._currentNode ) : 0;
return this.addToStack( node, index );
}
/**
* Represent an `if` statement in TSL.
*
* @param {Node} boolNode - Represents the condition.
* @param {Function} method - TSL code which is executed if the condition evaluates to `true`.
* @return {StackNode} A reference to this stack node.
*/
If( boolNode, method ) {
const methodNode = new ShaderNode( method );
this._currentCond = select( boolNode, methodNode );
return this.addToStack( this._currentCond );
}
/**
* Represent an `elseif` statement in TSL.
*
* @param {Node} boolNode - Represents the condition.
* @param {Function} method - TSL code which is executed if the condition evaluates to `true`.
* @return {StackNode} A reference to this stack node.
*/
ElseIf( boolNode, method ) {
const methodNode = new ShaderNode( method );
const ifNode = select( boolNode, methodNode );
this._currentCond.elseNode = ifNode;
this._currentCond = ifNode;
return this;
}
/**
* Represent an `else` statement in TSL.
*
* @param {Function} method - TSL code which is executed in the `else` case.
* @return {StackNode} A reference to this stack node.
*/
Else( method ) {
this._currentCond.elseNode = new ShaderNode( method );
return this;
}
/**
* Represents a `switch` statement in TSL.
*
* @param {any} expression - Represents the expression.
* @param {Function} method - TSL code which is executed if the condition evaluates to `true`.
* @return {StackNode} A reference to this stack node.
*/
Switch( expression ) {
this._expressionNode = nodeObject( expression );
return this;
}
/**
* Represents a `case` statement in TSL. The TSL version accepts an arbitrary numbers of values.
* The last parameter must be the callback method that should be executed in the `true` case.
*
* @param {...any} params - The values of the `Case()` statement as well as the callback method.
* @return {StackNode} A reference to this stack node.
*/
Case( ...params ) {
const caseNodes = [];
// extract case nodes from the parameter list
if ( params.length >= 2 ) {
for ( let i = 0; i < params.length - 1; i ++ ) {
caseNodes.push( this._expressionNode.equal( nodeObject( params[ i ] ) ) );
}
} else {
error( 'TSL: Invalid parameter length. Case() requires at least two parameters.' );
}
// extract method
const method = params[ params.length - 1 ];
const methodNode = new ShaderNode( method );
// chain multiple cases when using Case( 1, 2, 3, () => {} )
let caseNode = caseNodes[ 0 ];
for ( let i = 1; i < caseNodes.length; i ++ ) {
caseNode = caseNode.or( caseNodes[ i ] );
}
// build condition
const condNode = select( caseNode, methodNode );
if ( this._currentCond === null ) {
this._currentCond = condNode;
return this.addToStack( this._currentCond );
} else {
this._currentCond.elseNode = condNode;
this._currentCond = condNode;
return this;
}
}
/**
* Represents the default code block of a Switch/Case statement.
*
* @param {Function} method - TSL code which is executed in the `else` case.
* @return {StackNode} A reference to this stack node.
*/
Default( method ) {
this.Else( method );
return this;
}
setup( builder ) {
const nodeProperties = builder.getNodeProperties( this );
let index = 0;
for ( const childNode of this.getChildren() ) {
if ( childNode.isVarNode && childNode.isIntent( builder ) ) {
if ( childNode.isAssign( builder ) !== true ) {
continue;
}
}
nodeProperties[ 'node' + index ++ ] = childNode;
}
// return a outputNode if exists or null
return nodeProperties.outputNode || null;
}
hasOutput( builder ) {
return this.outputNode && this.outputNode.isNode && this.outputNode.getNodeType( builder ) !== 'void';
}
build( builder, ...params ) {
const previousStack = getCurrentStack();
const buildStage = builder.buildStage;
setCurrentStack( this );
builder.setActiveStack( this );
//
const buildNode = ( node ) => {
this._currentNode = node;
if ( node.isVarNode && node.isIntent( builder ) ) {
if ( node.isAssign( builder ) !== true ) {
return;
}
}
if ( buildStage === 'setup' ) {
node.build( builder );
} else if ( buildStage === 'analyze' ) {
node.build( builder, this );
} else if ( buildStage === 'generate' ) {
const stages = builder.getDataFromNode( node, 'any' ).stages;
const parents = stages && stages[ builder.shaderStage ];
if ( node.isVarNode && parents && parents.length === 1 && parents[ 0 ] && parents[ 0 ].isStackNode ) {
return; // skip var nodes that are only used in .toVarying()
}
node.build( builder, 'void' );
}
};
//
const nodes = [ ...this.nodes ];
for ( const node of nodes ) {
buildNode( node );
}
this._currentNode = null;
const newNodes = this.nodes.filter( ( node ) => nodes.indexOf( node ) === - 1 );
for ( const node of newNodes ) {
buildNode( node );
}
//
let result;
if ( this.hasOutput( builder ) ) {
result = this.outputNode.build( builder, ...params );
} else {
result = super.build( builder, ...params );
}
setCurrentStack( previousStack );
builder.removeActiveStack( this );
return result;
}
}
export default StackNode;
/**
* TSL function for creating a stack node.
*
* @tsl
* @function
* @param {?StackNode} [parent=null] - The parent stack node.
* @returns {StackNode}
*/
export const stack = /*@__PURE__*/ nodeProxy( StackNode ).setParameterLength( 0, 1 );