-
Notifications
You must be signed in to change notification settings - Fork 4
/
ShaderGraphResolver.ts
344 lines (304 loc) · 11.4 KB
/
ShaderGraphResolver.ts
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
import {ShaderNodeUID, AbstractShaderNode} from './AbstractShaderNode';
import {Index} from '../../../types/CommonTypes';
import { CGAPIResourceRepository } from '../../renderer/CGAPIResourceRepository';
import {VertexAttribute} from '../../definitions/VertexAttribute';
import {ShaderType, ShaderTypeEnum} from '../../definitions/ShaderType';
import {ShaderSocket} from './AbstractMaterialContent';
import {CompositionType} from '../../definitions/CompositionType';
import {ComponentType} from '../../definitions/ComponentType';
import { GLSLShader } from '../../../webgl/shaders/GLSLShader';
import mainPrerequisitesShaderityObject from '../../../webgl/shaderity_shaders/common/mainPrerequisites.glsl';
import prerequisitesShaderityObject from '../../../webgl/shaderity_shaders/common/prerequisites.glsl';
export class ShaderGraphResolver {
static createVertexShaderCode(vertexNodes: AbstractShaderNode[]) {
const shaderNodes = vertexNodes.concat();
// Find Start Node
const firstShaderNode: AbstractShaderNode =
this.__findBeginNode(shaderNodes);
// Topological Sorting
const sortedShaderNodes = this.__sortTopologically(
firstShaderNode,
shaderNodes
);
// Add additional functions by system
let vertexShaderPrerequisites = '';
const webglResourceRepository =
CGAPIResourceRepository.getWebGLResourceRepository();
let in_ = 'attribute';
if (webglResourceRepository.currentWebGLContextWrapper?.isWebGL2) {
in_ = 'in';
}
vertexShaderPrerequisites += `
#version 300 es
precision highp float;
precision highp int;
${prerequisitesShaderityObject.code}
${in_} float a_instanceInfo;\n`;
vertexShaderPrerequisites += `
uniform bool u_vertexAttributesExistenceArray[${VertexAttribute.AttributeTypeNumber}];
`;
vertexShaderPrerequisites += '/* shaderity: @{matricesGetters} */';
vertexShaderPrerequisites += '/* shaderity: @{getters} */';
let shaderBody = '';
// function definitions
shaderBody += ShaderGraphResolver.getFunctionDefinition(
sortedShaderNodes,
ShaderType.VertexShader
);
// main process
shaderBody +=
ShaderGraphResolver.__constructShaderWithNodes(sortedShaderNodes);
const shader = vertexShaderPrerequisites + shaderBody;
return {shader, shaderBody};
}
static createPixelShaderCode(pixelNodes: AbstractShaderNode[]) {
const shaderNodes = pixelNodes.concat();
// Find Start Node
const firstShaderNode: AbstractShaderNode =
this.__findBeginNode(shaderNodes);
// Topological Sorting
const sortedShaderNodes = this.__sortTopologically(
firstShaderNode,
shaderNodes
);
// Add additional functions by system
let pixelShaderPrerequisites = '';
pixelShaderPrerequisites += `
#version 300 es
precision highp float;
precision highp int;
${prerequisitesShaderityObject.code}
`;
pixelShaderPrerequisites += '/* shaderity: @{getters} */';
pixelShaderPrerequisites += 'layout(location = 0) out vec4 rt0;';
let shaderBody = '';
// function definitions
shaderBody += ShaderGraphResolver.getFunctionDefinition(
sortedShaderNodes,
ShaderType.PixelShader
);
// main process
shaderBody +=
ShaderGraphResolver.__constructShaderWithNodes(sortedShaderNodes);
const shader = pixelShaderPrerequisites + shaderBody;
return {shader, shaderBody};
}
private static __findBeginNode(shaderNodes: AbstractShaderNode[]) {
let firstShaderNode: AbstractShaderNode | undefined;
for (let i = 0; i < shaderNodes.length; i++) {
const shaderNode = shaderNodes[i];
if (shaderNode.inputConnections.length === 0) {
firstShaderNode = shaderNode;
}
}
return firstShaderNode!;
}
private static __sortTopologically(
firstShaderNode: AbstractShaderNode,
shaderNodes: AbstractShaderNode[]
) {
const ignoredInputUids: Index[] = [firstShaderNode!.shaderNodeUid];
const sortedNodeArray: AbstractShaderNode[] = [firstShaderNode!];
// remove node which don't have inputConnections (except first node)
shaderNodes.splice(shaderNodes.indexOf(firstShaderNode!), 1);
do {
let shaderNodeWhichHasNoInputs: AbstractShaderNode;
shaderNodes.forEach(shaderNode => {
let inputCount = 0;
for (const inputConnection of shaderNode.inputConnections) {
if (ignoredInputUids.indexOf(inputConnection.shaderNodeUid) === -1) {
inputCount++;
}
}
if (inputCount === 0) {
shaderNodeWhichHasNoInputs = shaderNode;
}
});
sortedNodeArray.push(shaderNodeWhichHasNoInputs!);
ignoredInputUids.push(shaderNodeWhichHasNoInputs!.shaderNodeUid);
shaderNodes.splice(shaderNodes.indexOf(shaderNodeWhichHasNoInputs!), 1);
} while (shaderNodes.length !== 0);
return sortedNodeArray;
}
static getFunctionDefinition(
shaderNodes: AbstractShaderNode[],
shaderType: ShaderTypeEnum
) {
let shaderText = '';
const existVertexFunctions: string[] = [];
for (let i = 0; i < shaderNodes.length; i++) {
const materialNode = shaderNodes[i];
if (
existVertexFunctions.indexOf(materialNode.shaderFunctionName) !== -1
) {
continue;
}
if (materialNode.shaderCode) {
shaderText += materialNode.shaderCode;
} else {
if (shaderType === ShaderType.VertexShader) {
shaderText += (materialNode.shader as any).vertexShaderDefinitions;
} else {
shaderText += (materialNode.shader as any).pixelShaderDefinitions;
}
}
existVertexFunctions.push(materialNode.shaderFunctionName);
}
return shaderText;
}
private static __constructShaderWithNodes(
materialNodes: AbstractShaderNode[]
) {
let shaderBody = '';
const isAnyTypeInput = function (input: ShaderSocket) {
return (
input.compositionType === CompositionType.Unknown ||
input.componentType === ComponentType.Unknown
);
};
shaderBody += GLSLShader.glslMainBegin;
shaderBody += mainPrerequisitesShaderityObject.code;
// Collects varInputNames and varOutputNames
const varInputNames: Array<Array<string>> = []; // input names of topological sorted Nodes
const varOutputNames: Array<Array<string>> = []; // output names of topological sorted Nodes
{
const existingInputs: ShaderNodeUID[] = [];
const existingOutputs: ShaderNodeUID[] = [];
const existingOutputsVarName: Map<ShaderNodeUID, string> = new Map();
for (let i = 1; i < materialNodes.length; i++) {
const materialNode = materialNodes[i];
if (varInputNames[i] == null) {
varInputNames[i] = [];
}
if (i - 1 >= 0) {
if (varOutputNames[i - 1] == null) {
varOutputNames[i - 1] = [];
}
}
const inputConnections = materialNode.inputConnections;
// Collects ExistingInputs
for (let j = 0; j < inputConnections.length; j++) {
const inputConnection = inputConnections[j];
const inputNode =
AbstractShaderNode.shaderNodes[inputConnection.shaderNodeUid];
if (isAnyTypeInput(materialNode.getInputs()[j])) {
continue;
}
const outputSocketOfPrev = inputNode.getOutput(
inputConnection.outputNameOfPrev
);
const inputSocketOfThis = materialNode.getInput(
inputConnection.inputNameOfThis
);
const varName = `${outputSocketOfPrev!.name}_${
inputConnection.shaderNodeUid
}_to_${materialNode.shaderNodeUid}`;
//
if (existingInputs.indexOf(inputNode.shaderNodeUid) === -1) {
const glslTypeStr = inputSocketOfThis!.compositionType.getGlslStr(
inputSocketOfThis!.componentType
);
const glslInitialValue =
inputSocketOfThis!.compositionType.getGlslInitialValue(
inputSocketOfThis!.componentType
);
const rowStr = `${glslTypeStr} ${varName} = ${glslInitialValue};\n`;
shaderBody += rowStr;
}
const existVarName = existingOutputsVarName.get(
inputNode.shaderNodeUid
);
varInputNames[i].push(existVarName ? existVarName : varName);
existingInputs.push(inputConnection.shaderNodeUid);
}
// Collects ExistingOutputs
for (let j = i; j < materialNodes.length; j++) {
const targetMaterialNode = materialNodes[j];
const prevMaterialNodeInner = materialNodes[i - 1];
const targetNodeInputConnections =
targetMaterialNode.inputConnections;
for (let k = 0; k < targetNodeInputConnections.length; k++) {
const inputConnection = targetNodeInputConnections[k];
if (
prevMaterialNodeInner?.shaderNodeUid !==
inputConnection.shaderNodeUid
) {
continue;
}
const inputNode =
AbstractShaderNode.shaderNodes[inputConnection.shaderNodeUid];
if (!isAnyTypeInput(targetMaterialNode.getInputs()[k])) {
if (existingOutputs.indexOf(inputNode.shaderNodeUid) === -1) {
const outputSocketOfPrev = inputNode.getOutput(
inputConnection.outputNameOfPrev
);
const varName = `${outputSocketOfPrev!.name}_${
inputConnection.shaderNodeUid
}_to_${targetMaterialNode.shaderNodeUid}`;
if (i - 1 >= 0) {
varOutputNames[i - 1].push(varName);
}
existingOutputsVarName.set(
inputConnection.shaderNodeUid,
varName
);
}
existingOutputs.push(inputConnection.shaderNodeUid);
}
}
}
}
}
// generate shader code by topological sorted nodes, varInputNames and varOutputNames
let ifCondition = '';
for (let i = 0; i < materialNodes.length; i++) {
const materialNode = materialNodes[i];
const functionName = materialNode.shaderFunctionName;
if (varInputNames[i] == null) {
varInputNames[i] = [];
}
if (varOutputNames[i] == null) {
varOutputNames[i] = [];
}
let rowStr = '';
if (functionName === 'ifStatement') {
ifCondition = varInputNames[i][0];
} else {
if (functionName.match(/^blockBegin_/)) {
rowStr += `if (${ifCondition}) {\n`;
ifCondition = '';
}
// Normal Node Process Begin
if (
materialNode.getInputs().length !== varInputNames[i].length ||
materialNode.getOutputs().length !== varOutputNames[i].length
) {
continue;
}
const varNames = varInputNames[i].concat(varOutputNames[i]);
if (varNames.length > 0) {
// Call node functions
rowStr += `${functionName}(`;
for (let k = 0; k < varNames.length; k++) {
const varName = varNames[k];
if (varName == null) {
continue;
}
if (k !== 0) {
rowStr += ', ';
}
rowStr += varNames[k];
}
rowStr += ');\n';
}
// Normal Node Process End
if (functionName.match(/^blockEnd_/)) {
rowStr += '}\n';
}
}
shaderBody += rowStr;
}
shaderBody += GLSLShader.glslMainEnd;
return shaderBody;
}
}