-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathexecutor.js
More file actions
423 lines (371 loc) · 14.9 KB
/
executor.js
File metadata and controls
423 lines (371 loc) · 14.9 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
var Base = require("../base.js");
var C = require("../interface/constants.js");
var OperatorList = require("../operator/operator-list.js");
var Utils = require("../utils/utils.js");
var Operator = require("../operator/operator.js");
var OperatorEntry = require("../operator/operator-entry.js");
var Program = require("../operator/program.js");
//----------------------------------------------------------------------------------------------------------------------
// Executor
//----------------------------------------------------------------------------------------------------------------------
/**
* Tries to combine multiple ProcessNodes into a Program. Currently only used for vertex shaders.
*
* @param {RequestNode|ProcessNode} ownerNode
* @param {C.PLATFORM} platform
* @constructor
*/
var Executor = function(ownerNode, platform){
this.platform = platform;
/**
* Nodes that are merged by this executor
* @type {Array.<ProcessNode|RequestNode>}
*/
this.mergedNodes = [];
/**
* Subset of this.mergedNodes that directly provide results of the executor
* @type {Array.<ProcessNode>}
*/
this.mergedOutputNodes = [];
/**
* ProcessNodes to be executed before this executor can be
* executed
* @type {Array.<ProcessNode>}
*/
this.subNodes = [];
/**
* TODO: Unused. Remove?
* @type {Array}
*/
this.unprocessedDataNames = [];
/**
* TODO: Maybe we should just store the cl-platform objects in global object so they are more easily available and
* to avoid long prototype chains. Or we could pass the graph context to each node of the graph.
* However, it would be good to allow each Graph object to have at least own context, cmdQueue and kernelManager.
* e.g. passing graph information here requires a long prototype chain
*/
this.operatorList = new OperatorList(platform);
this.programData = new Program.ProgramData();
/**
*
* @type {Program}
*/
this.program = null;
constructExecutor(this, ownerNode);
};
Executor.prototype.isProcessed = function(){
var i = this.mergedOutputNodes.length;
while(i--){
if(this.mergedOutputNodes[i].status != C.PROCESS_STATE.PROCESSED)
return false;
}
return true;
};
Executor.prototype.run = function(asyncCallback){
runSubNodes(this);
updateIterateState(this); // TODO check if iterate State has changes in any way and only refetch program in that case
this.program = Program.createProgram(this.operatorList);
if(this.program){
this.operatorList.allocateOutput(this.programData, !!asyncCallback);
this.program.run(this.programData, asyncCallback);
}
if(this.platform != C.PLATFORM.ASYNC){
var i = this.mergedOutputNodes.length;
while(i--){
this.mergedOutputNodes[i].status = C.PROCESS_STATE.PROCESSED;
}
}
};
Executor.prototype.getVertexShader = function(){
runSubNodes(this);
updateIterateState(this);
this.program = Program.createProgram(this.operatorList);
return this.program;
};
/**
* Construct Executor
* @param executer
* @param ownerNode
*/
function constructExecutor(executer, ownerNode){
var cData = {
blockedNodes: [], // Bad Nodes that cannot be merge. Filled during pre scan
doneNodes: [], // Nodes that have been signed up for merging. TODO: Redundant with constructionOrder and subNodes? - maybe yes!
constructionOrder: [], // Store nodes in order of construction of OperatorEntries.
inputSlots: {}, // Collected input channels of all merged nodes. Used to avoid assigning same input buffer twice
finalOutput: null, // finalOutput channes in case we have a RequestNode
firstOperator: null // Set to first operator that has been merged (will be executed last)
};
var requestNode = initRequestNode(cData, executer, ownerNode);
var noOperators = false; // TODO: Remove this?
constructPreScan(cData, ownerNode, executer.platform, noOperators);
setConstructionOrderAndSubNodes(cData, executer, ownerNode);
constructFromData(executer, cData);
}
/**
* Only relevant if ownerNodes is a RequestNode
* Sets finalOutput of construction data and unprocessedDataNames
* @param cData
* @param executer
* @param ownerNode
* @returns {boolean}
*/
function initRequestNode(cData, executer, ownerNode){
if(true) { // FIXME: ownerNode instanceof RequestNode){
cData.finalOutput = {};
var filter = ownerNode.filter || ownerNode.owner.outputChannels.getNames();
for(var i = 0; i < filter.length; ++i){
var name = filter[i];
var channel = ownerNode.owner.outputChannels.getChannel(name);
if(channel && channel.creatorProcessNode)
cData.finalOutput[name] = channel.getDataEntry();
}
Utils.nameset.add(executer.unprocessedDataNames, filter);
return true;
}
return false;
}
/**
* Goes to processing subtree at filled blockedNodes array in construction data.
* All nodes that cannot be merged or have parents that can't be merged will be blocked
* @param cData
* @param node
* @param platform
* @param noOperators
*/
function constructPreScan(cData, node, platform, noOperators){
if(cData.blockedNodes.indexOf(node) != -1)
return;
if(node.operator){
if(noOperators || !canOperatorMerge(cData, node.operator, platform)){
blockSubtree(cData, node);
return;
}
else{
if(!cData.firstOperator) cData.firstOperator = node.operator;
var mapping = node.operator.mapping;
for(var i = 0; i < mapping.length; ++i){
if(mapping[i].sequence){
blockInput(cData, node, mapping[i].source);
blockInput(cData, node, mapping[i].keySource);
}
else if(mapping[i].array){
// TODO: Rename .array to .randomAccess
blockInput(cData, node, mapping[i].source);
}
}
}
}
for(var i = 0; i < node.children.length; ++i){
constructPreScan(cData, node.children[i], platform, noOperators);
}
}
function canOperatorMerge(cData, operator, platform){
// TODO: Detect merge support
return (platform == C.PLATFORM.ASYNC || !Operator.isOperatorAsync(operator)) &&
(!cData.firstOperator ||
(platform == C.PLATFORM.GLSL && cData.firstOperator.evaluate_glsl && operator.evaluate_glsl));
}
function blockSubtree(cData, node){
if(cData.blockedNodes.indexOf(node) != -1)
return;
cData.blockedNodes.push(node);
for(var i = 0; i < node.children.length; ++i){
blockSubtree(cData, node.children[i]);
}
}
/**
* Block all processNodes assigned to an input channel
* @param cData
* @param node
* @param inputName
*/
function blockInput(cData, node, inputName){
var channel = node.inputChannels[inputName];
if(channel && channel.creatorProcessNode){
blockSubtree(cData, channel.creatorProcessNode);
}
}
/**
* Fill doneNodes and constructionOrder arrays of construction data.
* It also fills the subNodes array of the executer
* @param cData construction data
* @param executer
* @param node
*/
function setConstructionOrderAndSubNodes(cData, executer, node){
if(cData.doneNodes.indexOf(node) != -1)
return;
cData.doneNodes.push(node);
if(cData.blockedNodes.indexOf(node) != -1){
executer.subNodes.push(node);
}
else{
for(var i = 0; i < node.children.length; ++i){
setConstructionOrderAndSubNodes(cData, executer, node.children[i]);
}
if(node.operator){ // RequestNodes don't have an operator. Consider this case.
cData.constructionOrder.push(node);
}
}
}
/**
* Last step of construction: create OperatorList from constructionOrder array
* Also fill mergedNodes and programData
* @param executer
* @param cData
*/
function constructFromData(executer, cData){
for(var i = 0; i < cData.constructionOrder.length; ++i){
var node = cData.constructionOrder[i];
var entry = new OperatorEntry(node.operator);
constructInputConnection(executer, entry, cData, node);
var isOutputNode = constructOutputConnection(executer, entry, cData, node);
executer.programData.operatorData.push({});
executer.operatorList.addEntry(entry);
executer.mergedNodes.push(node);
if(isOutputNode || (i == cData.constructionOrder.length-1))
executer.mergedOutputNodes.push(node)
}
constructLostOutput(executer, cData);
}
/**
* Construct input info for OperatorEntry.
* Will implicitly create ProgramInputConnections for ProgramData
* @param {Executor} executer
* @param {OperatorEntry} entry
* @param {{}} cData
* @param {ProcessNode} node
*/
function constructInputConnection(executer, entry, cData, node){
var mapping = node.operator.mapping;
for(var j = 0; j < mapping.length; ++j){
var channel = node.inputChannels[mapping[j].source];
var operatorIndex;
if(channel && channel.creatorProcessNode && (operatorIndex =
executer.mergedNodes.indexOf(channel.creatorProcessNode) ) != -1 )
{
// it's transfer input
var outputIndex = getOperatorOutputIndex(channel.creatorProcessNode, channel);
entry.setTransferInput(j, operatorIndex, outputIndex);
var prevOperator = executer.operatorList.entries[operatorIndex];
if(!prevOperator.isFinalOutput(outputIndex)){
prevOperator.setTransferOutput(outputIndex);
}
continue;
}
// Handle direct input
var mappedInputName = mapping[j].source;
if(node.owner.owner._computeInputMapping)
mappedInputName = node.owner.owner._computeInputMapping.getScriptInputName(mapping[j].paramIdx, mapping[j].source);
var connection = new Program.ProgramInputConnection();
connection.channel = channel;
connection.arrayAccess = mapping[j].array || false; // TODO: rename to randomAccess
connection.sequenceAccessType = mapping[j].sequence || 0;
if(connection.sequenceAccessType)
connection.sequenceKeySourceChannel = node.inputChannels[mapping[j].keySource];
var connectionKey = connection.getKey();
var inputSlotIdx = cData.inputSlots[connectionKey];
if(channel && inputSlotIdx != undefined){
// Direct input already exists
entry.setDirectInput(j, inputSlotIdx, mappedInputName);
}
else{
// new direct input
inputSlotIdx = executer.programData.inputs.length;
cData.inputSlots[connectionKey] = inputSlotIdx;
executer.programData.inputs.push(connection);
entry.setDirectInput(j, inputSlotIdx, mappedInputName);
}
}
}
/**
* Construct output info of OperatorEntry
* @param {Executor} executer
* @param {OperatorEntry} entry
* @param {{}} cData
* @param {ProcessNode} node
*/
function constructOutputConnection(executer, entry, cData, node){
var outputs = node.operator.outputs;
var isOutputNode = true;
for(var i = 0; i < outputs.length; ++i){
var slot = node.outputDataSlots[outputs[i].name];
var finalOutputName = getFinalOutputName(slot, cData);
if(finalOutputName){
var index = executer.programData.outputs.length;
executer.programData.outputs.push(slot);
entry.setFinalOutput(i, index);
if(finalOutputName !== true){
Utils.nameset.remove(executer.unprocessedDataNames, finalOutputName);
}
}
else{
isOutputNode = false;
}
}
return isOutputNode; // TODO: Check if computation of isOutputNode is really correct?
}
function getOperatorOutputIndex(processNode, channel){
var outputs = processNode.operator.outputs;
for(var i = 0; i < outputs.length; ++i){
if(channel.getDataEntry() == processNode.outputDataSlots[outputs[i].name].dataEntry){
return i;
}
}
return null;
}
function getFinalOutputName(dataSlot, cData){
if(!cData.finalOutput) // If root of Executor is a ProcessNode we don't have finalOutput defined and all outputs are final.
return true;
for(var name in cData.finalOutput){
if(cData.finalOutput[name] == dataSlot.dataEntry){
return name;
}
}
return false;
}
function constructLostOutput(executer, cData){
for(var i = 0; i < cData.constructionOrder.length; ++i){
var node = cData.constructionOrder[i];
var entry = executer.operatorList.entries[i];
var outputs = node.operator.outputs;
for(var j = 0; j < outputs.length; ++j){
if(!entry.isFinalOutput(j) && ! entry.isTransferOutput(j)){
var index = executer.programData.outputs.length;
executer.programData.outputs.push(node.outputDataSlots[outputs[j].name]);
entry.setLostOutput(j, index);
}
}
}
}
function updateIterateState(executer){
var inputs = executer.programData.inputs;
for(var i = 0; i < executer.programData.inputs.length; ++i){
var entry = executer.programData.getDataEntry(i);
var iterateCount = entry ? entry.getIterateCount ? entry.getIterateCount() : 1 : 0;
if(!iterateCount)
executer.operatorList.setInputIterateType(i, C.ITERATION_TYPE.NULL);
else if(!inputs[i].arrayAccess && iterateCount > 1)
executer.operatorList.setInputIterateType(i, C.ITERATION_TYPE.MANY);
else
executer.operatorList.setInputIterateType(i, C.ITERATION_TYPE.ONE);
if(inputs[i].arrayAccess && platformRequiresArraySize(executer. platform)){
executer.operatorList.setInputSize(i, iterateCount);
}
}
}
/**
* Determine if the platform needs to declare uniform array sizes in the source code.
* @param platform
* @returns {boolean}
*/
function platformRequiresArraySize(platform){
return platform == C.PLATFORM.GLSL;
}
function runSubNodes(executer){
for(var i = 0; i < executer.subNodes.length; ++i){
executer.subNodes[i].process();
}
}
module.exports = Executor;