-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathvs-program.js
More file actions
273 lines (246 loc) · 11 KB
/
vs-program.js
File metadata and controls
273 lines (246 loc) · 11 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
var Base = require("../base.js");
var C = require("../interface/constants.js");
var Utils = require("../utils/utils.js");
var VertexShader = require("../processing/vs-connect.js").VertexShader;
//----------------------------------------------------------------------------------------------------------------------
// OperatorList
//----------------------------------------------------------------------------------------------------------------------
var c_SHADER_CONSTANT_TYPES = {};
c_SHADER_CONSTANT_TYPES[C.SHADER_CONSTANT_KEY.OBJECT_ID] = 'int';
c_SHADER_CONSTANT_TYPES[C.SHADER_CONSTANT_KEY.SCREEN_TRANSFORM] = 'mat4';
c_SHADER_CONSTANT_TYPES[C.SHADER_CONSTANT_KEY.SCREEN_TRANSFORM_NORMAL] = 'mat3';
c_SHADER_CONSTANT_TYPES[C.SHADER_CONSTANT_KEY.VIEW_TRANSFORM] = 'mat4';
c_SHADER_CONSTANT_TYPES[C.SHADER_CONSTANT_KEY.VIEW_TRANSFORM_NORMAL] = 'mat3';
c_SHADER_CONSTANT_TYPES[C.SHADER_CONSTANT_KEY.WORLD_TRANSFORM] = 'mat4';
c_SHADER_CONSTANT_TYPES[C.SHADER_CONSTANT_KEY.WORLD_TRANSFORM_NORMAL] = 'mat3';
var VSProgram = function(operatorList){
this.list = operatorList;
this._outputInfo = {};
setOutputIterate(this);
};
VSProgram.prototype.getOutputNames = function(){
return Object.keys(this._outputInfo);
};
VSProgram.prototype.getOutputType = function(name){
return this._outputInfo[name].type;
};
VSProgram.prototype.isOutputUniform = function(name){
return this._outputInfo[name].iteration == C.ITERATION_TYPE.ONE;
};
VSProgram.prototype.isOutputNull = function(name){
return this._outputInfo[name].iteration == C.ITERATION_TYPE.NULL;
};
VSProgram.prototype.createVertexShader = function(programData, vsConfig){
var result = new VertexShader(programData);
constructVS(result, this, vsConfig);
return result;
};
function setOutputIterate(program){
var operatorList = program.list, entries = operatorList.entries;
var baseEntry = entries[entries.length - 1], baseOperator = baseEntry.operator;
for( var i = 0; i < baseOperator.params.length; ++i){
var entry = baseOperator.params[i],
name = entry.source,
inputIndex = i,
directInputIndex = baseEntry.getDirectInputIndex(inputIndex);
program._outputInfo[name] = {type: entry.type};
if( baseEntry.isTransferInput(inputIndex) ||
operatorList.isInputIterate(directInputIndex))
{
program._outputInfo[name].iteration = C.ITERATION_TYPE.MANY;
}
else if(operatorList.isInputUniform(directInputIndex)){
program._outputInfo[name].iteration = C.ITERATION_TYPE.ONE;
}
else{
program._outputInfo[name].iteration = C.ITERATION_TYPE.NULL;
}
}
}
function constructVS(vs, program, vsConfig){
var operatorList = program.list, entries = operatorList.entries;
var usedNames = [],
directInputNames = {},
transferNames = {};
var baseEntry = entries[entries.length - 1], acceptedBaseShaderInput = [], baseOperator = baseEntry.operator;
if(!vsConfig)
throw new Error("Could not find vsConfig! Attempt to create vertex shader programm without VS operator?");
Utils.nameset.add(usedNames, vsConfig.getBlockedNames());
var code = "";
code += "// OUTPUT\n";
// First: collect output names
for(var name in vsConfig._addOutput){
var entry = vsConfig._addOutput[name];
code += "varying " + getGLSLType(entry.type) + " " + name + ";\n";
Utils.nameset.add(usedNames, name);
}
var inputIndex = 0;
for( var name in vsConfig._attributes){
var configAttr = vsConfig._attributes[name],
directInputIndex = baseEntry.getDirectInputIndex(inputIndex);
for(var i = 0; i < configAttr.channeling.length; ++i){
var channeling = configAttr.channeling[i];
var outputInfo = {type: configAttr.type, iteration: 0, index: 0, sourceName: name},
outputName = channeling.outputName;
if( channeling.code ||
baseEntry.isTransferInput(inputIndex) ||
operatorList.isInputIterate(directInputIndex))
{
acceptedBaseShaderInput[inputIndex] = true;
outputInfo.iteration = C.ITERATION_TYPE.MANY;
var type = baseOperator.outputs[inputIndex].type;
code += "varying " + getGLSLType(type) + " " + outputName + ";\n";
Utils.nameset.add(usedNames, outputName);
transferNames[baseEntry.getTransferOutputId(i)] = outputName;
}
else if(operatorList.isInputUniform(directInputIndex)){
outputInfo.iteration = C.ITERATION_TYPE.ONE;
outputInfo.index = directInputIndex;
}
else{
outputInfo.iteration = C.ITERATION_TYPE.NULL;
}
Utils.nameset.add(vs._outputNames, outputName);
vs._outputInfo[outputName] = outputInfo;
}
inputIndex++;
}
code += "\n";
code += "// INPUT\n";
// Add additional input
for(var name in vsConfig._addInput){
var entry = vsConfig._addInput[name];
code += (entry.uniform ? "uniform " : "attribute " ) + getGLSLType(entry.type) + " " + name + ";\n";
Utils.nameset.add(usedNames, name);
}
// Second: collect input names
for(var i = 0; i < entries.length; ++i){
var entry = entries[i], operator = entry.operator;
for(var j = 0; j < operator.mapping.length; ++j){
if( (i < entries.length - 1 || acceptedBaseShaderInput[j]) &&
!entry.isTransferInput(j) && !directInputNames[entry.getDirectInputIndex(j)])
{
var mapEntry = operator.mapping[j];
var name = getFreeName(mapEntry.name, usedNames), inputIndex = entry.getDirectInputIndex(j),
uniform = !operatorList.isInputIterate(inputIndex);
vs._inputInfo[name] = { index: inputIndex, uniform: uniform };
Utils.nameset.add(vs._inputNames, name);
directInputNames[inputIndex] = name;
code += (uniform ? "uniform " : "attribute ") + getGLSLType(mapEntry.internalType) + " " + name;
if(mapEntry.array)
code += "[" + operatorList.getInputSize(inputIndex) + "]";
code += ";\n";
}
}
}
// Start main
code += "\n// CODE\n";
code += "void main(void){\n";
// Create Code
for(var i = 0; i < entries.length; ++i){
var entry = entries[i], operator = entry.operator;
// Declare transfer output names
for(var j = 0; j < operator.outputs.length; ++j){
if(!entry.isFinalOutput(j)){
var name = getFreeName(operator.outputs[j].name, usedNames);
transferNames[entry.getTransferOutputId(j)] = name;
code += "\t" + getGLSLType(operator.outputs[j].type) + " " + name + ";\n";
}
}
// Take Code Fragment
var codeFragment = convertCodeFragment(operator.evaluate_glsl, entry,
transferNames, directInputNames, usedNames);
code += codeFragment + "\n";
}
// Add attribute channeling code
var mappingIndex = 0, conversionCode = "";
for( var name in vsConfig._attributes){
var entry = vsConfig._attributes[name];
for(var i = 0; i < entry.channeling.length; ++i){
var channeling = entry.channeling[i], outputName = channeling.outputName;
if(vs._outputInfo[outputName].iteration == C.ITERATION_TYPE.MANY){
if(channeling.code)
conversionCode += "\t" + channeling.code + "\n";
else
conversionCode += "\t" + outputName + " = #I{" + name + "};\n";
}
}
mappingIndex++;
}
for( var i = 0; i < vsConfig._codeFragments.length; ++i){
conversionCode += "\t" + vsConfig._codeFragments[i] + "\n";
}
code += convertCodeFragment(conversionCode, baseEntry, transferNames, directInputNames, usedNames) + "\n";
code += "}\n";
vs._glslCode = code;
}
function convertCodeFragment(codeFragment, entry, transferNames, directInputNames, usedNames){
var index, operator = entry.operator;
while((index = codeFragment.indexOf("#I{")) != -1){
var end = codeFragment.indexOf("}",index);
var mappingIndex = getMappingIndex(operator, codeFragment.substring(index+3,end));
var replaceName = entry.isTransferInput(mappingIndex) ?
transferNames[entry.getTransferInputId(mappingIndex)] :
directInputNames[entry.getDirectInputIndex(mappingIndex)];
codeFragment = codeFragment.substring(0, index) + replaceName + codeFragment.substring(end+1);
}
while((index = codeFragment.indexOf("#O{")) != -1){
var end = codeFragment.indexOf("}",index);
var outputIndex = getOutputIndex(operator, codeFragment.substring(index+3,end));
var replaceName = transferNames[entry.getTransferOutputId(outputIndex)];
codeFragment = codeFragment.substring(0, index) + replaceName + codeFragment.substring(end+1);
}
var localNames = [];
while((index = codeFragment.indexOf("#L{")) != -1){
var end = codeFragment.indexOf("}",index);
var key = codeFragment.substring(index+3,end);
if(!localNames[key]){
localNames[key] = getFreeName(key, usedNames);
}
var replaceName = localNames[key];
codeFragment = codeFragment.substring(0, index) + replaceName + codeFragment.substring(end+1);
}
while((index = codeFragment.indexOf("#G{")) != -1){
var end = codeFragment.indexOf("}",index);
var replaceName = codeFragment.substring(index+3,end);
codeFragment = codeFragment.substring(0, index) + replaceName + codeFragment.substring(end+1);
}
return codeFragment;
}
function getFreeName(name, usedNames){
var result = name, i = 1;
while(usedNames.indexOf(result) != -1){
result = name + "_" + (++i);
}
Utils.nameset.add(usedNames, result);
return result;
}
function getMappingIndex(operator, name){
for(var i = 0; i < operator.mapping.length; ++i){
if(operator.mapping[i].name == name)
return i;
}
throw new Error("Invalid input name '" + name + "' inside of code fragment" );
}
function getOutputIndex(operator, name){
for(var i = 0; i < operator.outputs.length; ++i){
if(operator.outputs[i].name == name)
return i;
}
}
function getGLSLType(xflowType){
switch(xflowType){
case C.DATA_TYPE.BOOL : return 'bool';
case C.DATA_TYPE.BYTE : return 'uint';
case C.DATA_TYPE.FLOAT : return 'float';
case C.DATA_TYPE.FLOAT2 : return 'vec2';
case C.DATA_TYPE.FLOAT3 : return 'vec3';
case C.DATA_TYPE.FLOAT4 : return 'vec4';
case C.DATA_TYPE.FLOAT3X3 : return 'mat3';
case C.DATA_TYPE.FLOAT4X4 : return 'mat4';
case C.DATA_TYPE.INT : return 'int';
case C.DATA_TYPE.INT4 : return 'ivec4';
}
throw new Error("Type not supported for GLSL " + C.getTypeName(xflowType) );
}
module.exports = VSProgram;