-
Notifications
You must be signed in to change notification settings - Fork 0
/
Generator.cpp
374 lines (326 loc) · 10.3 KB
/
Generator.cpp
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
#include "Generator.h"
#include "Utils.h"
#include <algorithm>
struct Variable
{
unsigned long id;
std::string name;
ValueType type;
};
std::map<unsigned long, std::vector<std::string>> variables;
std::map<unsigned long, std::vector<Variable>> vars;
std::map<std::string, UniformParameter> uniforms;
unsigned long lastVarName = 0;
void replaceAll(std::string& str, const std::string& from, const std::string& to)
{
if(from.empty())
return;
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos)
{
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
}
const Link* getLinkByNodeIn(const Graph& graph, unsigned long nodeInId, unsigned long slotIn)
{
for (int i = 0; i < graph.links.size(); i++)
{
if (graph.links[i].node_in == nodeInId && graph.links[i].slot_in == slotIn)
{
return &graph.links[i];
}
}
return nullptr;
}
std::string getTypeStringFromValueType(const ValueType& type)
{
switch (type)
{
case ValueType::FLOAT:
return "float";
break;
case ValueType::VEC2:
return "vec2";
break;
case ValueType::VEC3:
return "vec3";
break;
case ValueType::VEC4:
return "vec4";
break;
case ValueType::SAMPLER2D:
return "sampler2D";
break;
default:
return "";
break;
}
}
std::string wrapValue(std::string value, ValueType type)
{
switch (type)
{
case ValueType::VEC2:
return "vec2(" + value + ")";
case ValueType::VEC3:
return "vec3(" + value + ")";
case ValueType::VEC4:
return "vec4(" + value + ")";
case ValueType::FLOAT:
default:
return value;
}
}
struct InputVariable
{
int id;
std::string name;
bool hasLink;
ValueType inType;
std::string inName;
std::string value;
};
Signature findBestSignature(const std::vector<InputVariable>& inputVars, const std::vector<Signature>& signatures)
{
int currentBestSignature = 0;
std::vector<int> currentBestMatch;
currentBestMatch.resize(inputVars.size());
for (size_t i = 0; i < currentBestMatch.size(); i++)
{
currentBestMatch[i] = static_cast<int>(TypeMatch::NONE);
}
for (size_t i = 0; i < signatures.size(); i++)
{
std::vector<int> currentMatch;
currentMatch.resize(inputVars.size());
for (size_t j = 0; j < inputVars.size(); j++)
{
currentMatch[j] = static_cast<int>(getTypeMatch(static_cast<int>(inputVars[j].inType), static_cast<int>(signatures[i].inputs[j])));
}
bool isBetterMatch = true;
for (size_t j = 0; j < inputVars.size(); j++)
{
if (currentMatch[j] == static_cast<int>(TypeMatch::NONE) || currentMatch[j] < currentBestMatch[j])
{
isBetterMatch = false;
break;
}
}
if (isBetterMatch)
{
currentBestMatch = currentMatch;
currentBestSignature = i;
}
}
return signatures[currentBestSignature];
}
std::string getCastedValue(std::string value, ValueType castType)
{
unsigned long needComponents = 0;
switch (castType)
{
case ValueType::FLOAT:
return Utils::split(value, ",")[0];
case ValueType::VEC2:
needComponents = 2;
break;
case ValueType::VEC3:
needComponents = 3;
break;
case ValueType::VEC4:
needComponents = 4;
break;
}
std::vector<std::string> values = Utils::split(value, ",");
int leftComponents = needComponents - values.size();
int valuesCount = std::min(needComponents, values.size());
std::string ret = "";
for (int i = 0; i < valuesCount; i++)
{
ret += values[i] + ",";
}
for (int i = 0; i < leftComponents; i++)
{
ret += "1.0,";
}
return wrapValue(ret.substr(0, ret.size() - 1), castType);
}
std::string getCastedVariable(InputVariable inVariable, ValueType castType)
{
if (castType == inVariable.inType)
{
if (inVariable.hasLink)
{
return inVariable.inName;
}
else
{
return wrapValue(inVariable.value, castType);
}
}
if (!inVariable.hasLink)
{
return getCastedValue(inVariable.value, castType);
}
if (castType == ValueType::FLOAT)
{
switch (inVariable.inType)
{
case ValueType::VEC2:
return inVariable.inName + ".x";
case ValueType::VEC3:
return inVariable.inName + ".x";
case ValueType::VEC4:
return inVariable.inName + ".x";
}
}
if (castType == ValueType::VEC2)
{
switch (inVariable.inType)
{
case ValueType::FLOAT:
return "vec2(" + inVariable.inName + ")";
case ValueType::VEC3:
case ValueType::VEC4:
return "vec2(" + inVariable.inName + ".x, " + inVariable.inName + ".y)";
}
}
if (castType == ValueType::VEC3)
{
switch (inVariable.inType)
{
case ValueType::FLOAT:
return "vec3(" + inVariable.inName + ")";
case ValueType::VEC2:
return "vec3(" + inVariable.inName + ".x, " + inVariable.inName + ".y, 1.0)";
case ValueType::VEC4:
return "vec3(" + inVariable.inName + ".x, " + inVariable.inName + ".y, " + inVariable.inName + ".z)";
}
}
if (castType == ValueType::VEC4)
{
switch (inVariable.inType)
{
case ValueType::FLOAT:
return "vec4(" + inVariable.inName + ")";
case ValueType::VEC2:
return "vec4(" + inVariable.inName + ".x, " + inVariable.inName + ".y, 1.0, 1.0)";
case ValueType::VEC3:
return "vec4(" + inVariable.inName + ".x, " + inVariable.inName + ".y, " + inVariable.inName + ".z, 1.0)";
}
}
return "error";
}
std::string resolveNode(Graph& graph, const Node& node)
{
if (node.code == "none")
{
return "";
}
std::string selfCode = node.code;
std::string code = "";
std::vector<InputVariable> inVariables;
for (int i = 0; i < node.input_slots.size(); i++)
{
const Link* l = getLinkByNodeIn(graph, node.guid, i);
if (l == nullptr)
{
inVariables.push_back({i, "<in" + std::to_string(i) + ">", false, node.input_slots[i].type, "", node.input_slots[i].value});
continue;
}
const Link& link = *l;
auto nodeOut = vars.find(link.node_out);
std::string variable;
if (nodeOut == vars.end())
{
code += resolveNode(graph, graph.nodes[link.node_out]);
nodeOut = vars.find(link.node_out);
if (nodeOut != vars.end())
{
inVariables.push_back({i, "<in" + std::to_string(i) + ">", true, nodeOut->second[link.slot_out].type, nodeOut->second[link.slot_out].name, ""});
}
}
else
{
inVariables.push_back({i, "<in" + std::to_string(i) + ">", true, nodeOut->second[link.slot_out].type, nodeOut->second[link.slot_out].name, ""});
}
}
Signature sig = findBestSignature(inVariables, node.signatures);
for (size_t i = 0; i < inVariables.size(); i++)
{
replaceAll(selfCode, inVariables[i].name, getCastedVariable(inVariables[i], sig.inputs[i]));
}
for (int i = 0; i < node.output_slots.size(); i++)
{
std::string varName = "var" + std::to_string(lastVarName++);
replaceAll(selfCode, "<out" + std::to_string(i) + ">", getTypeStringFromValueType(sig.outputs[i]) + " " + varName);
vars[node.guid].push_back({node.guid, varName, sig.outputs[i]});
}
return code + selfCode;
}
std::string genHeader(Graph& graph)
{
std::string code = "#version 330 core\n\n";
code += "out vec4 fragColor;\n\n";
for (int i = 0; i < graph.parameters.size(); i++)
{
std::string prefix = "";
if (graph.parameters[i].isVertex)
{
prefix = "in ";
}
else
{
prefix = "uniform ";
}
if (!graph.parameters[i].isBuiltIn && !graph.parameters[i].isVertex)
{
UniformParameter param;
param.valueType = graph.parameters[i].type;
switch (param.valueType)
{
case ValueType::FLOAT:
param.floatValue = Utils::getValueFromString<float>(graph.nodes[graph.parameters[i].guid].output_slots[0].value);
break;
case ValueType::VEC2:
param.vec2Value = Utils::getValueFromString<Vec2>(graph.nodes[graph.parameters[i].guid].output_slots[0].value);
break;
case ValueType::VEC3:
param.vec3Value = Utils::getValueFromString<Vec3>(graph.nodes[graph.parameters[i].guid].output_slots[0].value);
break;
case ValueType::VEC4:
param.vec4Value = Utils::getValueFromString<Vec4>(graph.nodes[graph.parameters[i].guid].output_slots[0].value);
break;
case ValueType::SAMPLER2D:
new((void*)¶m.textureValue) std::string(graph.nodes[graph.parameters[i].guid].output_slots[0].value);
break;
}
uniforms.emplace(graph.parameters[i].name, param);
}
code += prefix + getTypeStringFromValueType(graph.parameters[i].type) + " " + graph.parameters[i].name + ";\n";
vars[graph.parameters[i].guid].push_back({graph.parameters[i].guid, graph.parameters[i].name, graph.parameters[i].type});
}
return code;
}
namespace Generator
{
GeneratorOutput generateGLSL(Graph& graph)
{
lastVarName = 0;
variables.clear();
uniforms.clear();
vars.clear();
std::string header = genHeader(graph);
auto node = graph.nodes.find(graph.masterNode);
if (node == graph.nodes.end())
{
return {"", {}};
}
const Node& masterNode = graph.nodes[graph.masterNode];
std::string alg = resolveNode(graph, masterNode);
replaceAll(alg, "\n", "\n\t");
alg = "\t" + alg;
return { header + "\nvoid main()\n{\n" + alg + "\n}\n", uniforms };
}
}