forked from xml3d/xml3d.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecuter.js
More file actions
295 lines (242 loc) · 10.3 KB
/
executer.js
File metadata and controls
295 lines (242 loc) · 10.3 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
(function(){
//----------------------------------------------------------------------------------------------------------------------
// Xflow.Executer
//----------------------------------------------------------------------------------------------------------------------
Xflow.Executer = function(ownerNode, platform){
this.platform = platform;
this.mergedNodes = [];
this.subNodes = [];
this.unprocessedDataNames = [];
this.operatorList = new Xflow.OperatorList(platform);
this.programData = new Xflow.ProgramData();
this.program = null;
constructExecuter(this, ownerNode);
}
Xflow.Executer.prototype.run = function(){
runSubNodes(this);
updateIterateState(this);
this.program = Xflow.createProgram(this.operatorList);
if(this.program){
this.operatorList.allocateOutput(this.programData);
this.program.run(this.programData);
}
}
Xflow.Executer.prototype.getVertexShader = function(){
runSubNodes(this);
updateIterateState(this);
this.program = Xflow.createProgram(this.operatorList);
return this.program;
}
function constructExecuter(executer, ownerNode){
var cData = {
blockedNodes: [],
doneNodes: [],
constructionOrder: [],
inputSlots: {},
finalOutput: null,
firstOperator: null
}
initRequestNode(cData, executer, ownerNode);
constructPreScan(cData, ownerNode, executer.platform);
setConstructionOrderAndSubNodes(cData, executer, ownerNode);
constructFromData(executer, cData);
}
function initRequestNode(cData, executer, ownerNode){
if(ownerNode instanceof Xflow.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();
}
Xflow.nameset.add(executer.unprocessedDataNames, filter);
}
}
function constructPreScan(cData, node, platform){
if(cData.blockedNodes.indexOf(node) != -1)
return;
if(node.operator){
if(!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: Check for other things that cancel merging
blockInput(cData, node, mapping[i].source);
}
}
}
}
for(var i = 0; i < node.children.length; ++i){
constructPreScan(cData, node.children[i], platform);
}
}
function canOperatorMerge(cData, operator, platform){
// TODO: Detect merge support
return !cData.firstOperator ||
(platform == Xflow.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]);
}
}
function blockInput(cData, node, inputName){
var channel = node.inputChannels[inputName];
if(channel && channel.creatorProcessNode){
blockSubtree(cData, channel.creatorProcessNode);
}
}
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){
cData.constructionOrder.push(node);
}
}
}
function constructFromData(executer, cData){
for(var i = 0; i < cData.constructionOrder.length; ++i){
var node = cData.constructionOrder[i];
var currentIdx = i;
var entry = new Xflow.OperatorEntry(node.operator);
constructInputConnection(executer, entry, cData, node);
constructOutputConnection(executer, entry, cData, node);
executer.programData.operatorData.push({});
executer.operatorList.addEntry(entry);
executer.mergedNodes.push(node);
}
constructLostOutput(executer, cData);
}
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);
if(!executer.operatorList.entries[operatorIndex].isFinalOutput(outputIndex))
executer.operatorList.entries[operatorIndex].setTransferOutput(outputIndex);
continue;
}
var mappedInputName = node.owner.owner._computeInputMapping.getScriptInputName(mapping[j].paramIdx,
mapping[j].source);
var connection = new Xflow.ProgramInputConnection();
connection.channel = channel;
connection.arrayAccess = mapping[j].array || false;
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);
}
}
}
function constructOutputConnection(executer, entry, cData, node){
var outputs = node.operator.outputs;
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){
Xflow.nameset.remove(executer.unprocessedDataNames, finalOutputName);
}
}
}
}
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)
return true;
for(var name in cData.finalOutput){
if(cData.finalOutput[name] == dataSlot.dataEntry){
return name;
}
}
return false;
}
function constructLostOutput(executor, cData){
for(var i = 0; i < cData.constructionOrder.length; ++i){
var node = cData.constructionOrder[i];
var entry = executor.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 = executor.programData.outputs.length;
executor.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, Xflow.ITERATION_TYPE.NULL);
else if(!inputs[i].arrayAccess && iterateCount > 1)
executer.operatorList.setInputIterateType(i, Xflow.ITERATION_TYPE.MANY);
else
executer.operatorList.setInputIterateType(i, Xflow.ITERATION_TYPE.ONE);
if(inputs[i].arrayAccess && platformRequiresArraySize(executer. platform)){
executer.operatorList.setInputSize(i, iterateCount);
}
}
}
function platformRequiresArraySize(platform){
return platform == Xflow.PLATFORM.GLSL;
}
function runSubNodes(executer){
for(var i = 0; i < executer.subNodes.length; ++i){
executer.subNodes[i].process();
}
}
})();