-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathprocess-node.js
More file actions
504 lines (437 loc) · 15.4 KB
/
process-node.js
File metadata and controls
504 lines (437 loc) · 15.4 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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
var Base = require("../base.js");
var C = require("../interface/constants.js");
var utils = require("../utils/utils.js");
var Executor = require("./executor.js");
var Result = require("./result.js");
var Operator = require("../operator/operator.js");
var Data = require("../interface/data.js");
var DataSlot = require("../processing/data-slot.js");
var BufferEntry = Data.BufferEntry;
var TextureEntry = Data.TextureEntry;
var ImageDataTextureEntry = Data.ImageDataTextureEntry;
//----------------------------------------------------------------------------------------------------------------------
// C.ProcessNode
//----------------------------------------------------------------------------------------------------------------------
var ASYNC_PROCESS_STATE = {
IDLE : 0,
RUNNING : 1,
RESCHEDULED : 2,
INIT: 3
};
/**
* Optimized representation for the processing graph. Only created for ChannelNodes with operators.
* Is connected directly to other ProcessNodes, ignoring channels that are not relevant for processing
* @param {ChannelNode} channelNode
* @constructor
* @extends {GraphNode}
*/
var ProcessNode = function(channelNode){
this.owner = channelNode;
this.operator = channelNode.operator;
/**
* Input channels for the operator
* @type {Object.<string, Channel>}
*/
this.inputChannels = {};
/**
* Outputs of operator
* @type {Object.<string, DataSlot>}
*/
this.outputDataSlots = {};
/**
* @type {exports.C.PROCESS_STATE}
*/
this.status = C.PROCESS_STATE.MODIFIED;
/**
* @type {ASYNC_PROCESS_STATE}
*/
this.asyncProcessState = ASYNC_PROCESS_STATE.INIT;
/**
* Direct Children without transitive children of children
* TODO: Use Set()?
* @type {Array.<ProcessNode>}
*/
this.children = [];
/**
* Children with transitive dependencies
* TODO: Use Set()?
* @type {Array.<ProcessNode>}
*/
this.descendants = [];
/**
* Callback, the executor needs to call when the computation is ready
* @type {Function}
* @private
*/
this._bindedAsyncCallback = null;
/**
* Index of array matches platform id (C.PLATFORM)
* @type {Array.<Executor>}
*/
this.executers = [];
constructProcessNode(this, channelNode);
if(Operator.isOperatorAsync(this.operator)){
this._bindedAsyncCallback = this.receiveAsyncProcessing.bind(this);
}
};
ProcessNode.prototype.onXflowChannelChange = function(channel, state){
if (Operator.isOperatorAsync(this.operator)) {
if (this.status == C.PROCESS_STATE.LOADING || this.asyncProcessState != ASYNC_PROCESS_STATE.INIT) {
this.status = C.PROCESS_STATE.MODIFIED;
this.updateState();
}
}
else {
if (state == C.DATA_ENTRY_STATE.CHANGED_VALUE && this.status > C.PROCESS_STATE.UNPROCESSED) {
this.status = C.PROCESS_STATE.UNPROCESSED;
} else {
this.status = C.PROCESS_STATE.MODIFIED;
}
this.notifyOutputChanged(state);
}
};
ProcessNode.prototype.startAsyncProcessing = function(){
if(this.asyncProcessState == ASYNC_PROCESS_STATE.IDLE || this.asyncProcessState == ASYNC_PROCESS_STATE.INIT){
this.asyncProcessState = ASYNC_PROCESS_STATE.RUNNING;
var executer = getOrCreateExecuter(this, C.PLATFORM.ASYNC);
executer.run(this._bindedAsyncCallback);
}
else{
this.asyncProcessState = ASYNC_PROCESS_STATE.RESCHEDULED;
}
};
ProcessNode.prototype.receiveAsyncProcessing = function(){
this.status = C.PROCESS_STATE.PROCESSED;
this.notifyOutputChanged(C.DATA_ENTRY_STATE.CHANGED_SIZE_TYPE);
if(this.asyncProcessState == ASYNC_PROCESS_STATE.RESCHEDULED){
this.asyncProcessState = ASYNC_PROCESS_STATE.IDLE;
this.status = C.PROCESS_STATE.MODIFIED;
this.updateState();
}
else{
this.asyncProcessState = ASYNC_PROCESS_STATE.IDLE;
}
Base._flushResultCallbacks();
};
ProcessNode.prototype.notifyOutputChanged = function(state){
for(var name in this.outputDataSlots){
this.outputDataSlots[name].notifyOnChange(state);
}
};
ProcessNode.prototype.clear = function(){
for(var name in this.inputChannels){
this.inputChannels[name] && this.inputChannels[name].removeListener(this);
}
for (var i = 0; i < this.children.length; i++) {
this.children[i].clear();
}
};
ProcessNode.prototype.updateState = function(){
if(this.status == C.PROCESS_STATE.MODIFIED){
this.status = C.PROCESS_STATE.UNPROCESSED;
XML3D.debug.assert(!this.owner.loading, "This should never happen");
if(this.owner.loading)
this.status = C.PROCESS_STATE.LOADING;
else{
for(var i = 0; i < this.children.length; ++i){
this.status = Math.min(this.status, this.children[i].updateState());
}
if(this.status > C.PROCESS_STATE.LOADING && isInputLoading(this.operator, this.inputChannels))
this.status = C.PROCESS_STATE.LOADING;
if(this.status > C.PROCESS_STATE.INVALID &&
!checkInput(this, this.operator, this.owner.owner._computeInputMapping, this.inputChannels))
this.status = C.PROCESS_STATE.INVALID;
if(this.status == C.PROCESS_STATE.UNPROCESSED && Operator.isOperatorAsync(this.operator)){
this.status = this.asyncProcessState == ASYNC_PROCESS_STATE.INIT ? C.PROCESS_STATE.LOADING
: C.PROCESS_STATE.PROCESSED;
this.startAsyncProcessing();
}
}
}
return this.status;
};
ProcessNode.prototype.process = function(){
if(this.status == C.PROCESS_STATE.UNPROCESSED){
var executer = getOrCreateExecuter(this, this.owner.platform);
executer.run();
this.status = C.PROCESS_STATE.PROCESSED;
}
};
/**
*
* @param {ProcessNode} processNode
* @param {ChannelNode} channelNode
*/
function constructProcessNode(processNode, channelNode){
var dataNode = channelNode.owner;
synchronizeInputChannels(processNode, channelNode, dataNode);
synchronizeChildrenAndDescendants(processNode.children, processNode.descendants, processNode.inputChannels);
synchronizeOutput(processNode.operator, processNode.outputDataSlots);
}
/**
*
* @param processNode
* @param channelNode
* @param dataNode
*/
function synchronizeInputChannels(processNode, channelNode, dataNode){
var operator = processNode.operator, inputMapping = dataNode._computeInputMapping;
for(var i = 0; i < operator.params.length; ++i){
var sourceName = operator.params[i].source;
var dataName = inputMapping ? inputMapping.getScriptInputName(i, sourceName) : sourceName;
if(dataName){
var channel = channelNode.inputChannels.getChannel(dataName);
if(channel) channel.addListener(processNode);
processNode.inputChannels[sourceName] = channel;
}
}
}
function isInputLoading(operator, inputChannels){
for(var i in operator.params){
var entry = operator.params[i];
var channel = inputChannels[entry.source];
if(!channel) continue;
var dataEntry = channel.getDataEntry();
if(!dataEntry) continue;
if(dataEntry.isLoading && dataEntry.isLoading()) return true;
}
return false;
}
function checkInput(processNode, operator, inputMapping, inputChannels){
var dataNode = processNode.owner.owner;
for(var i in operator.params){
var entry = operator.params[i];
var dataName = inputMapping ? inputMapping.getScriptInputName(i, entry.source) : entry.source;
if(!entry.optional && !dataName){
Base.notifyError("Xflow: operator " + operator.name + ": Missing input argument for "
+ entry.source, dataNode);
return false;
}
if(dataName){
var channel = inputChannels[entry.source];
if(!channel){
if(!inputMapping) continue;
Base.notifyError("Xflow: operator " + operator.name + ": Input of name '" + dataName +
"' not found. Used for parameter " + entry.source, dataNode);
return false;
}
var dataEntry = channel.getDataEntry();
if(!channel.creatorProcessNode){
if(!entry.optional && (!dataEntry || dataEntry.isEmpty())){
Base.notifyError("Xflow: operator " + operator.name + ": Input for " + entry.source +
' contains no data.', dataNode);
return false;
}
}
if(dataEntry && dataEntry.type != entry.type){
Base.notifyError("Xflow: operator " + operator.name + ": Input for " + entry.source +
" has wrong type. Expected: " + C.getTypeName(entry.type)
+ ", but got: " + C.getTypeName(dataEntry.type), dataNode);
return false;
}
}
}
return true;
}
function synchronizeChildrenAndDescendants(children, descendants, inputChannels){
var channel;
for(var name in inputChannels){
channel = inputChannels[name];
if(channel && channel.creatorProcessNode){
utils.set.add(children, channel.creatorProcessNode);
utils.set.add(descendants, channel.creatorProcessNode.descendants);
}
}
utils.set.remove(children, descendants);
utils.set.add(descendants, children);
}
function synchronizeOutput(operator, outputs){
var async = Operator.isOperatorAsync(operator);
for(var i in operator.outputs){
var dataEntry = operator.outputs[i];
var entry, asyncEntry;
var type = dataEntry.type;
if(type != C.DATA_TYPE.TEXTURE){
entry = new BufferEntry(type, null);
if(async) asyncEntry = new BufferEntry(type, null);
}
else{
entry = window.document ? new TextureEntry(null) : new ImageDataTextureEntry(null);
if(async) asyncEntry = window.document ? new TextureEntry(null) : new ImageDataTextureEntry(null);
}
outputs[dataEntry.name] = new DataSlot(entry, 0);
if(async) outputs[dataEntry.name].asyncDataEntry = asyncEntry;
}
}
function getOrCreateExecuter(node, platform){
if(!node.executers[platform]){
node.executers[platform] = new Executor(node, platform);
}
return node.executers[platform];
}
//----------------------------------------------------------------------------------------------------------------------
// RequestNode
//----------------------------------------------------------------------------------------------------------------------
/**
*
* FIXME: RequestNodes are never deleted.
* @param {ChannelNode} channelNode
* @param {Array.<string>} filter
* @constructor
*/
var RequestNode = function(channelNode, filter){
this.owner = channelNode;
this.filter = filter;
/**
*
* @type {Object<C.PLATFORM, exports.Result>}
*/
this.results = {};
/**
* @type {exports.C.PROCESS_STATE}
*/
this.status = C.PROCESS_STATE.MODIFIED;
/**
* @type {Object.<string, Channel>}
*/
this.channels = {};
/**
* @see ProcessNode.children
* @type {Array}
*/
this.children = [];
/**
* @see ProcessNode.executers
* @type {Array}
*/
this.executers = [];
/**
* @see ProcessNode.outOfSync
* @type {boolean}
*/
this.outOfSync = true;
};
RequestNode.prototype.synchronize = function(){
if(this.outOfSync){
this.outOfSync = false;
synchronizeRequestChannels(this, this.owner);
synchronizeChildrenAndDescendants(this.children, [], this.channels);
}
};
RequestNode.prototype.updateState = function(){
this.synchronize();
if(this.status == C.PROCESS_STATE.MODIFIED){
this.status = C.PROCESS_STATE.UNPROCESSED;
if(this.owner.loading) {
this.status = C.PROCESS_STATE.LOADING;
} else {
for(var i = 0; i < this.children.length; ++i){
this.status = Math.min(this.status, this.children[i].updateState());
}
}
}
return this.status;
};
RequestNode.prototype.getResult = function(resultType){
this.updateState();
// TODO: This could be in getRequestComputeResult
if(this.status == C.PROCESS_STATE.UNPROCESSED){
if(resultType == C.RESULT_TYPE.COMPUTE){
var executer = getOrCreateExecuter(this, this.owner.platform);
if(!executer.isProcessed())
executer.run();
}
this.status = C.PROCESS_STATE.PROCESSED;
}
var result = null;
if (resultType == C.RESULT_TYPE.COMPUTE) {
result = getRequestComputeResult(this);
} else if (resultType == C.RESULT_TYPE.VS) {
result = getRequestVSResult(this);
}
result.loading = (this.status == C.PROCESS_STATE.LOADING);
return result;
};
RequestNode.prototype.setStructureOutOfSync = function(){
this.outOfSync = true;
this.status = C.PROCESS_STATE.MODIFIED;
for(var type in this.results){
this.results[type]._notifyChanged(C.RESULT_STATE.CHANGED_STRUCTURE);
}
for(var name in this.channels){
this.channels[name].removeListener(this);
}
this.channels = [];
this.children = [];
this.executers = [];
};
RequestNode.prototype.onXflowChannelChange = function(channel, state){
if(channel.creatorProcessNode)
this.status = C.PROCESS_STATE.MODIFIED;
var notifyState = (state == C.DATA_ENTRY_STATE.CHANGED_VALUE ? C.RESULT_STATE.CHANGED_DATA_VALUE
: C.RESULT_STATE.CHANGED_DATA_SIZE);
for(var type in this.results){
this.results[type]._notifyChanged(notifyState);
}
};
RequestNode.prototype.clear = function() {
this.setStructureOutOfSync();
delete this.owner;
};
/**
*
* @param requestNode
* @param channelNode
*/
function synchronizeRequestChannels(requestNode, channelNode){
var names = requestNode.filter;
if(!names){
names = channelNode.outputChannels.getNames();
}
for(var i = 0; i < names.length; ++i){
var name = names[i];
var channel = channelNode.outputChannels.getChannel(name);
if(channel){
requestNode.channels[name] = channel;
channel.addListener(requestNode);
}
}
}
/**
*
* @param {RequestNode} requestNode
* @returns {Result}
*/
function getRequestComputeResult(requestNode)
{
if(!requestNode.results[C.RESULT_TYPE.COMPUTE])
requestNode.results[C.RESULT_TYPE.COMPUTE] = new Result.ComputeResult();
var result = requestNode.results[C.RESULT_TYPE.COMPUTE];
result._dataEntries = {}; result._outputNames = [];
for(var name in requestNode.channels){
var entry = requestNode.channels[name].getDataEntry();
result._dataEntries[name] = entry && !entry.isEmpty() ? entry : null;
result._outputNames.push(name);
}
return result;
}
/**
*
* @param requestNode
* @returns {exports.VSDataResult}
*/
function getRequestVSResult(requestNode)
{
var executer = getOrCreateExecuter(requestNode, C.PLATFORM.GLSL);
if(!requestNode.results[C.RESULT_TYPE.VS])
requestNode.results[C.RESULT_TYPE.VS] = new Result.VSDataResult();
var result = requestNode.results[C.RESULT_TYPE.VS];
var program = executer.getVertexShader();
result._program = program;
result._programData = executer.programData;
return result;
}
module.exports = {
RequestNode: RequestNode,
ProcessNode: ProcessNode
};