-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathprogram.js
More file actions
296 lines (251 loc) · 8.93 KB
/
program.js
File metadata and controls
296 lines (251 loc) · 8.93 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
var Base = require("../base.js");
var C = require("../interface/constants.js");
var VSProgram = require("./vs-program.js");
var CLProgram = require("./cl-program.js");
/**
* Data for a OperatorList, generated by an Executor
* @constructor
*/
var ProgramData = function(){
/**
* @type {Array.<ProgramInputConnection>}
*/
this.inputs = [];
/**
*
* @type {Array.<DataSlot>}
*/
this.outputs = [];
/**
* iterateCount: How often we iterate with the default execution model
* iterFlag: Per input: true if the input can be iterated, otherwise false
* customData: Per instance data that users can persist between operator invocations
* @type {Array.<{iterateCount: number, iterFlag: Array, customData: {}}>}
*/
this.operatorData = [];
};
ProgramData.prototype.getChannel = function(index){
return this.inputs[index].channel;
};
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);
};
/**
* @constructor
*/
var ProgramInputConnection = function(){
/**
* @type {Channel}
*/
this.channel = null;
/**
* Is this input a uniform array
* @type {boolean}
*/
this.arrayAccess = false;
/**
* @type {C.SEQUENCE}
*/
this.sequenceAccessType = C.SEQUENCE.NO_ACCESS;
/**
*
* @type {Channel|null}
*/
this.sequenceKeySourceChannel = null;
};
/**
* Hash-able key to identify equal inputs within executor
* @returns {string}
*/
ProgramInputConnection.prototype.getKey = function(){
return (this.channel ? this.channel.id : "NULL") + ";" + this.arrayAccess + ";" + this.sequenceAccessType + ";" +
( this.sequenceKeySourceChannel ? this.sequenceKeySourceChannel.id : "");
};
var c_program_cache = {};
var createProgram = function(operatorList){
var firstOperator;
if(operatorList.entries.length === 0) {
return null;
}
firstOperator = operatorList.entries[0].operator;
var key = operatorList.getKey();
if(!c_program_cache[key]){
// GLSL operators are implemented in a different way, so platform information is fetched from the operatorList
// as a fallback mode to not break the old implementations
if(operatorList.platform === C.PLATFORM.GLSL){
c_program_cache[key] = new VSProgram(operatorList);
} else if (firstOperator.platform === C.PLATFORM.CL) {
c_program_cache[key] = new CLProgram(operatorList);
}else if(firstOperator.platform === C.PLATFORM.JAVASCRIPT && operatorList.entries.length === 1 ) {
c_program_cache[key] = new SingleProgram(operatorList);
}else {
Base.notifyError("Could not create program from operatorList");
}
}
return c_program_cache[key];
};
var SingleProgram = function(operatorList){
this.list = operatorList;
this.entry = operatorList.entries[0];
this.operator = this.entry.operator;
this._inlineLoop = null;
};
SingleProgram.prototype.run = function(programData, asyncCallback){
var operatorData = prepareOperatorData(this.list, 0, programData);
if(asyncCallback)
applyAsyncOperator(this.entry, programData, operatorData, asyncCallback);
else 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);
handlePostProcessOutput(entry, programData, args, false);
}
function applyAsyncOperator(entry, programData, operatorData, asyncCallback){
var args = assembleFunctionArgs(entry, programData, true);
args.push(operatorData);
args.push(function(){
handlePostProcessOutput(entry, programData, args, true);
asyncCallback();
});
entry.operator.evaluate_async.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){
Base.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 = C.DATA_TYPE_TUPLE_SIZE[[operator.outputs[argIdx].type]];
}
else{
var i = argIdx - operator.outputs.length;
addIndex = operatorData.iterFlag[i];
tupleCnt = C.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);
if(!data.customData)
data.customData = {};
return data;
}
function assembleFunctionArgs(entry, programData, async){
var args = [];
var outputs = entry.operator.outputs;
for(var i = 0; i < outputs.length; ++i){
if(outputs[i].noAlloc){
args.push({assign: null});
}
else{
var dataSlot = programData.outputs[entry.getOutputIndex(i)];
var dataEntry = async ? dataSlot.asyncDataEntry : dataSlot.dataEntry;
args.push(dataEntry ? dataEntry.getValue() : null);
}
}
addInputToArgs(args, entry, programData);
return args;
}
function handlePostProcessOutput(entry, programData, parameters, async){
var outputs = entry.operator.outputs;
for(var i = 0; i < outputs.length; ++i){
var dataSlot = programData.outputs[entry.getOutputIndex(i)];
if(outputs[i].noAlloc){
var dataEntry = async ? dataSlot.asyncDataEntry : dataSlot.dataEntry;
if(dataEntry.type == C.DATA_TYPE.TEXTURE ){
dataEntry._setImage(parameters[i].assign);
}
else{
dataEntry._setValue(parameters[i].assign);
}
}
if(async){
dataSlot.swapAsync();
}
}
}
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);
}
}
module.exports = {
createProgram: createProgram,
ProgramData: ProgramData,
ProgramInputConnection: ProgramInputConnection
};