forked from xml3d/xml3d.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.js
More file actions
470 lines (404 loc) · 19.1 KB
/
program.js
File metadata and controls
470 lines (404 loc) · 19.1 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
(function(){
//----------------------------------------------------------------------------------------------------------------------
// Xflow.OperatorList
//----------------------------------------------------------------------------------------------------------------------
Xflow.OperatorEntry = function(operator){
this.index = 0;
this.operator = operator;
this.inputInfo = [];
this.outputInfo = [];
}
Xflow.OperatorEntry.prototype.isTransferInput = function(mappingIndex){
return this.inputInfo[mappingIndex].operatorIndex !== undefined;
}
Xflow.OperatorEntry.prototype.getTransferInputOperatorIndex = function(mappingIndex){
return this.inputInfo[mappingIndex].operatorIndex;
}
Xflow.OperatorEntry.prototype.getTransferInputOutputIndex = function(mappingIndex){
return this.inputInfo[mappingIndex].outputIndex;
}
Xflow.OperatorEntry.prototype.getTransferInputId = function(mappingIdx){
var info = this.inputInfo[mappingIdx];
return info.operatorIndex + "_" + info.outputIndex;
}
Xflow.OperatorEntry.prototype.getTransferOutputId = function(outputIndex){
return this.index + "_" + outputIndex;
}
Xflow.OperatorEntry.prototype.getInputMappingName = function(mappingIdx){
return this.inputInfo[mappingIdx].mappedName;
}
Xflow.OperatorEntry.prototype.getDirectInputIndex = function(mappingIdx){
return this.inputInfo[mappingIdx].inputIndex;
}
Xflow.OperatorEntry.prototype.getOutputIndex = function(operatorOutputIdx){
return this.outputInfo[operatorOutputIdx].finalOut || this.outputInfo[operatorOutputIdx].lost || 0;
}
Xflow.OperatorEntry.prototype.isFinalOutput = function(outputIndex){
return this.outputInfo[outputIndex] && this.outputInfo[outputIndex].finalOut !== undefined;
}
Xflow.OperatorEntry.prototype.isTransferOutput = function(outputIndex){
return this.outputInfo[outputIndex] && this.outputInfo[outputIndex].transfer;
}
Xflow.OperatorEntry.prototype.isLostOutput = function(outputIndex){
return this.outputInfo[outputIndex] && this.outputInfo[outputIndex].lost !== undefined;
}
Xflow.OperatorEntry.prototype.setTransferInput = function(mappingIndex, operatorIndex, outputIndex){
this.inputInfo[mappingIndex] = { operatorIndex: operatorIndex, outputIndex: outputIndex};
}
Xflow.OperatorEntry.prototype.setDirectInput = function(mappingIndex, inputIndex, mappedName){
this.inputInfo[mappingIndex] = { inputIndex: inputIndex, mappedName: mappedName };
}
Xflow.OperatorEntry.prototype.setFinalOutput = function(operatorOutputIndex, globalOutputIndex){
this.outputInfo[operatorOutputIndex] = { finalOut : globalOutputIndex };
}
Xflow.OperatorEntry.prototype.setTransferOutput = function(operatorOutputIndex){
this.outputInfo[operatorOutputIndex] = { transfer: true };
}
Xflow.OperatorEntry.prototype.setLostOutput = function(operatorOutputIndex, globalOutputIndex){
this.outputInfo[operatorOutputIndex] = { lost: globalOutputIndex};
}
Xflow.OperatorEntry.prototype.getKey = function(){
var key = this.operator.name + "*O";
for(var i = 0; i <this.outputInfo.length; ++i){
var info = this.outputInfo[i];
key += "*" + ( info.transfer ? "_" : info.finalOut || (info.lost + "?"));
}
key += + "*I";
for(var i = 0; i <this.inputInfo.length; ++i){
var info = this.inputInfo[i];
key += "*" + (info.inputIndex ? info.inputInfo : info.operatorIndex + ">" + info.outputIndex);
}
return key;
}
Xflow.OperatorList = function(platform){
this.platform = platform
this.entries = [];
this.inputInfo = {};
}
Xflow.OperatorList.prototype.addEntry = function(entry){
entry.index = this.entries.length;
this.entries.push(entry);
}
Xflow.OperatorList.prototype.getKey = function(){
var keys = [];
for(var i = 0; i < this.entries.length; ++i){
keys.push(this.entries[i].getKey());
}
var result = this.platform + ">" + keys.join("!") + "|";
for(var i in this.inputInfo){
result += i + ">" + (this.inputInfo[i].iterate || 0) + "x" + (this.inputInfo[i].size || 0);
}
return result;
}
Xflow.OperatorList.prototype.setInputIterateType = function(inputIndex, type){
if(!this.inputInfo[inputIndex]) this.inputInfo[inputIndex] = {};
this.inputInfo[inputIndex].iterate = type;
}
Xflow.OperatorList.prototype.setInputSize = function(inputIndex, size){
if(!this.inputInfo[inputIndex]) this.inputInfo[inputIndex] = {};
this.inputInfo[inputIndex].size = size;
}
Xflow.OperatorList.prototype.isInputIterate = function(inputIndex){
return this.inputInfo[inputIndex] && this.inputInfo[inputIndex].iterate == Xflow.ITERATION_TYPE.MANY;
}
Xflow.OperatorList.prototype.isInputUniform = function(inputIndex){
return this.inputInfo[inputIndex] && this.inputInfo[inputIndex].iterate == Xflow.ITERATION_TYPE.ONE;
}
Xflow.OperatorList.prototype.isInputNull = function(inputIndex){
return this.inputInfo[inputIndex] && this.inputInfo[inputIndex].iterate == Xflow.ITERATION_TYPE.NULL;
}
Xflow.OperatorList.prototype.getInputIterateType = function(inputIndex){
return this.inputInfo[inputIndex] && this.inputInfo[inputIndex].iterate;
}
Xflow.OperatorList.prototype.getInputSize = function(inputIndex){
return this.inputInfo[inputIndex] && this.inputInfo[inputIndex].size || 0;
}
Xflow.OperatorList.prototype.getIterateCount = function(programData){
var count = -1;
for(var i = 0; i < programData.inputs.length; ++i){
if(this.isInputIterate(i)){
var dataEntry = programData.getDataEntry(i);
if(dataEntry && dataEntry.getIterateCount){
var size = dataEntry.getIterateCount();
count = count < 0 ? size : Math.min(size, count);
}
}
}
return count < 0 ? 1 : count;
}
var c_sizes = {};
Xflow.OperatorList.prototype.allocateOutput = function(programData){
for(var i = 0; i < this.entries.length; ++i){
var entry = this.entries[i];
var operator = entry.operator;
var operatorData = programData.operatorData[i];
var iterateCount = this.getIterateCount(programData);
if(operator.alloc){
var args = [c_sizes];
addInputToArgs(args, entry, programData);
args.push(iterateCount);
operator.alloc.apply(operatorData, args);
}
for(var j = 0; j < operator.outputs.length; ++j){
var d = operator.outputs[j];
var dataEntry = programData.outputs[entry.getOutputIndex(j)].dataEntry;
if (dataEntry.type == Xflow.DATA_TYPE.TEXTURE) {
// texture entry
if (d.customAlloc)
{
var texParams = c_sizes[d.name];
var newWidth = texParams.imageFormat.width;
var newHeight = texParams.imageFormat.height;
var newFormatType = texParams.imageFormat.type;
var newSamplerConfig = texParams.samplerConfig;
dataEntry._createImage(newWidth, newHeight, newFormatType, newSamplerConfig);
} else if (d.sizeof) {
var srcEntry = null;
for(var k = 0; k < operator.mapping.length; ++k){
if (operator.mapping[k].source == d.sizeof) {
srcEntry = programData.getDataEntry(entry.getDirectInputIndex(k));
break;
}
}
if (srcEntry) {
var newWidth = Math.max(srcEntry.getWidth(), 1);
var newHeight = Math.max(srcEntry.getHeight(), 1);
var newFormatType = d.formatType || srcEntry.getFormatType();
var newSamplerConfig = d.samplerConfig || srcEntry.getSamplerConfig();
dataEntry._createImage(newWidth, newHeight, newFormatType, newSamplerConfig);
}
else
throw new Error("Unknown texture input parameter '" + d.sizeof+"' in operator '"+operator.name+"'");
} else
throw new Error("Cannot create texture. Use customAlloc or sizeof parameter attribute");
} else {
var size = (d.customAlloc ? c_sizes[d.name] : iterateCount) * dataEntry.getTupleSize();
if( !dataEntry._value || dataEntry._value.length != size){
switch(dataEntry.type){
case Xflow.DATA_TYPE.FLOAT:
case Xflow.DATA_TYPE.FLOAT2:
case Xflow.DATA_TYPE.FLOAT3:
case Xflow.DATA_TYPE.FLOAT4:
case Xflow.DATA_TYPE.FLOAT4X4: dataEntry._setValue(new Float32Array(size)); break;
case Xflow.DATA_TYPE.INT:
case Xflow.DATA_TYPE.INT4:
case Xflow.DATA_TYPE.BOOL: dataEntry._setValue(new Int32Array(size)); break;
default: XML3D.debug.logWarning("Could not allocate output buffer of TYPE: " + dataEntry.type);
}
}
else{
dataEntry._notifyChanged();
}
}
}
}
}
/*
Xflow.OperatorList.prototype.checkInput = function(programData){
for(var i = 0; i < this.entries.length; ++i){
var entry = this.entries[i];
var mapping = entry.operator.mapping;
for(var j = 0; j < mapping.length; ++j){
if(entry.isTransferInput(j)){
var outputType = this.entries[entry.getTransferInputOperatorIndex(j)].operator.outputs[
entry.getTransferInputOutputIndex(j)].type;
if(outputType != entry.type){
XML3D.debug.logError("Xflow: operator " + entry.operator.name + ": Input for " + entry.source +
" has wrong type. Expected: " + Xflow.getTypeName(entry.type)
+ ", but got: " + Xflow.getTypeName(outputType) );
return false;
}
}
else{
var mappingName = entry.getInputMappingName(j);
if(!entry.optional && !mappingName){
XML3D.debug.logError("Xflow: operator " + entry.operator.name + ": Missing input argument for "
+ entry.source);
return false;
}
if(mappingName){
var channel = programData.getChannel(entry.getDirectInputIndex(j));
if(!channel){
XML3D.debug.logError("Xflow: operator " + entry.operator.name + ": Input of name '" + mappingName +
"' not found. Used for parameter " + entry.source);
return false;
}
var dataEntry = channel.getDataEntry();
if(!entry.optional && (!dataEntry || dataEntry.getLength() == 0)){
XML3D.debug.logError("Xflow: operator " + entry.operator.name + ": Input for " + entry.source +
' contains no data.');
return false;
}
if(dataEntry && dataEntry.type != entry.type){
XML3D.debug.logError("Xflow: operator " + entry.operator.name + ": Input for " + entry.source +
" has wrong type. Expected: " + Xflow.getTypeName(entry.type)
+ ", but got: " + Xflow.getTypeName(dataEntry.type) );
return false;
}
}
}
}
}
}
*/
Xflow.ProgramData = function(){
this.inputs = [];
this.outputs = [];
this.operatorData = [];
}
Xflow.ProgramData.prototype.getChannel = function(index){
return this.inputs[index].channel;
}
Xflow.ProgramData.prototype.getDataEntry = function(index){
var entry = this.inputs[index];
var channel = entry.channel;
if(!channel) return null;
var key = 0;
if(entry.sequenceKeySourceChannel){
var keyDataEntry = entry.sequenceKeySourceChannel.getDataEntry();
key = keyDataEntry && keyDataEntry._value ? keyDataEntry._value[0] : 0;
}
return channel.getDataEntry(entry.sequenceAccessType, key);
}
Xflow.ProgramInputConnection = function(){
this.channel = null;
this.arrayAccess = false;
this.sequenceAccessType = Xflow.SEQUENCE.NO_ACCESS;
this.sequenceKeySourceChannel = null;
}
Xflow.ProgramInputConnection.prototype.getKey = function(){
return (this.channel ? this.channel.id : "NULL") + ";" + this.arrayAccess + ";" + this.sequenceAccessType + ";" +
( this.sequenceKeySourceChannel ? this.sequenceKeySourceChannel.id : "");
}
var c_program_cache = {};
Xflow.createProgram = function(operatorList){
if(operatorList.entries.length == 0)
return null;
var key = operatorList.getKey();
if(!c_program_cache[key]){
if(operatorList.platform == Xflow.PLATFORM.GLSL){
c_program_cache[key] = new Xflow.VSProgram(operatorList);
}
else if(operatorList.entries.length == 1)
c_program_cache[key] = new Xflow.SingleProgram(operatorList);
else
Xflow.notifyError("Could not create program from operatorList");
}
return c_program_cache[key];
}
Xflow.SingleProgram = function(operatorList){
this.list = operatorList;
this.entry = operatorList.entries[0];
this.operator = this.entry.operator;
this._inlineLoop = null;
}
Xflow.SingleProgram.prototype.run = function(programData){
var operatorData = prepareOperatorData(this.list, 0, programData);
if(this.operator.evaluate_core){
applyCoreOperation(this, programData, operatorData);
}
else{
applyDefaultOperation(this.entry, programData, operatorData);
}
}
function applyDefaultOperation(entry, programData, operatorData){
var args = assembleFunctionArgs(entry, programData);
args.push(operatorData);
entry.operator.evaluate.apply(operatorData, args);
}
function applyCoreOperation(program, programData, operatorData){
var args = assembleFunctionArgs(program.entry, programData);
args.push(operatorData.iterateCount);
if(!program._inlineLoop){
program._inlineLoop = createOperatorInlineLoop(program.operator, operatorData);
}
program._inlineLoop.apply(operatorData, args);
}
var c_VarPattern = /var\s+(.)+[;\n]/;
var c_InnerVarPattern = /[^=,\s]+\s*(=[^,]+)?(,)?/;
function createOperatorInlineLoop(operator, operatorData){
var code = "function (";
var funcData = parseFunction(operator.evaluate_core);
code += funcData.args.join(",") + ",__xflowMax) {\n";
code += " var __xflowI = __xflowMax\n" +
" while(__xflowI--){\n";
var body = funcData.body;
body = replaceArrayAccess(body, funcData.args, operator, operatorData);
code += body + "\n }\n}";
var inlineFunc = eval("(" + code + ")");
return inlineFunc;
}
var c_FunctionPattern = /function\s+([^(]*)\(([^)]*)\)\s*\{([\s\S]*)\}/;
function parseFunction(func){
var result = {};
var matches = func.toString().match(c_FunctionPattern);
if(!matches){
Xflow.notifyError("Xflow Internal: Could not parse function: " + func);
return null;
}
result.args = matches[2].split(",");
for(var i in result.args) result.args[i] = result.args[i].trim();
result.body = matches[3];
return result;
}
var c_bracketPattern = /([a-zA-Z_$][\w$]*)(\[)/;
function replaceArrayAccess(code, args, operator, operatorData){
var result = "";
var index = 0, bracketIndex = code.indexOf("[", index);
while(bracketIndex != -1){
var key = code.substr(index).match(c_bracketPattern)[1];
var argIdx = args.indexOf(key);
var addIndex = false, tupleCnt = 0;
if(argIdx != -1){
if(argIdx < operator.outputs.length){
addIndex = true;
tupleCnt = Xflow.DATA_TYPE_TUPLE_SIZE[[operator.outputs[argIdx].type]];
}
else{
var i = argIdx - operator.outputs.length;
addIndex = operatorData.iterFlag[i];
tupleCnt = Xflow.DATA_TYPE_TUPLE_SIZE[operator.mapping[i].internalType];
}
}
result += code.substring(index, bracketIndex) + "["
if(addIndex){
result += tupleCnt + "*__xflowI + ";
}
index = bracketIndex + 1;
bracketIndex = code.indexOf("[", index);
}
result += code.substring(index);
return result;
}
function prepareOperatorData(list, idx, programData){
var data = programData.operatorData[0];
var entry = list.entries[idx];
var mapping = entry.operator.mapping;
data.iterFlag = {};
for(var i = 0; i < mapping.length; ++i){
var doIterate = (entry.isTransferInput(i) || list.isInputIterate(entry.getDirectInputIndex(i)));
data.iterFlag[i] = doIterate;
}
data.iterateCount = list.getIterateCount(programData);
return data;
}
function assembleFunctionArgs(entry, programData){
var args = [];
var outputs = entry.operator.outputs;
for(var i = 0; i < outputs.length; ++i){
var d = outputs[i];
var dataEntry = programData.outputs[entry.getOutputIndex(i)].dataEntry;
args.push(dataEntry ? dataEntry.getValue() : null);
}
addInputToArgs(args, entry, programData);
return args;
}
function addInputToArgs(args, entry, programData){
var mapping = entry.operator.mapping;
for(var i = 0; i < mapping.length; ++i){
var mapEntry = mapping[i];
var dataEntry = programData.getDataEntry(entry.getDirectInputIndex(i));
args.push(dataEntry ? dataEntry.getValue() : null);
}
}
}());