1 | /**************************************************************************/ |
2 | /* shader_compiler.cpp */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #include "shader_compiler.h" |
32 | |
33 | #include "core/config/project_settings.h" |
34 | #include "core/os/os.h" |
35 | #include "servers/rendering/rendering_server_globals.h" |
36 | #include "servers/rendering/shader_types.h" |
37 | |
38 | #define SL ShaderLanguage |
39 | |
40 | static String _mktab(int p_level) { |
41 | return String("\t" ).repeat(p_level); |
42 | } |
43 | |
44 | static String _typestr(SL::DataType p_type) { |
45 | String type = ShaderLanguage::get_datatype_name(p_type); |
46 | if (!RS::get_singleton()->is_low_end() && ShaderLanguage::is_sampler_type(p_type)) { |
47 | type = type.replace("sampler" , "texture" ); //we use textures instead of samplers in Vulkan GLSL |
48 | } |
49 | return type; |
50 | } |
51 | |
52 | static int _get_datatype_alignment(SL::DataType p_type) { |
53 | switch (p_type) { |
54 | case SL::TYPE_VOID: |
55 | return 0; |
56 | case SL::TYPE_BOOL: |
57 | return 4; |
58 | case SL::TYPE_BVEC2: |
59 | return 8; |
60 | case SL::TYPE_BVEC3: |
61 | return 16; |
62 | case SL::TYPE_BVEC4: |
63 | return 16; |
64 | case SL::TYPE_INT: |
65 | return 4; |
66 | case SL::TYPE_IVEC2: |
67 | return 8; |
68 | case SL::TYPE_IVEC3: |
69 | return 16; |
70 | case SL::TYPE_IVEC4: |
71 | return 16; |
72 | case SL::TYPE_UINT: |
73 | return 4; |
74 | case SL::TYPE_UVEC2: |
75 | return 8; |
76 | case SL::TYPE_UVEC3: |
77 | return 16; |
78 | case SL::TYPE_UVEC4: |
79 | return 16; |
80 | case SL::TYPE_FLOAT: |
81 | return 4; |
82 | case SL::TYPE_VEC2: |
83 | return 8; |
84 | case SL::TYPE_VEC3: |
85 | return 16; |
86 | case SL::TYPE_VEC4: |
87 | return 16; |
88 | case SL::TYPE_MAT2: |
89 | return 16; |
90 | case SL::TYPE_MAT3: |
91 | return 16; |
92 | case SL::TYPE_MAT4: |
93 | return 16; |
94 | case SL::TYPE_SAMPLER2D: |
95 | return 16; |
96 | case SL::TYPE_ISAMPLER2D: |
97 | return 16; |
98 | case SL::TYPE_USAMPLER2D: |
99 | return 16; |
100 | case SL::TYPE_SAMPLER2DARRAY: |
101 | return 16; |
102 | case SL::TYPE_ISAMPLER2DARRAY: |
103 | return 16; |
104 | case SL::TYPE_USAMPLER2DARRAY: |
105 | return 16; |
106 | case SL::TYPE_SAMPLER3D: |
107 | return 16; |
108 | case SL::TYPE_ISAMPLER3D: |
109 | return 16; |
110 | case SL::TYPE_USAMPLER3D: |
111 | return 16; |
112 | case SL::TYPE_SAMPLERCUBE: |
113 | return 16; |
114 | case SL::TYPE_SAMPLERCUBEARRAY: |
115 | return 16; |
116 | case SL::TYPE_STRUCT: |
117 | return 0; |
118 | case SL::TYPE_MAX: { |
119 | ERR_FAIL_V(0); |
120 | } |
121 | } |
122 | |
123 | ERR_FAIL_V(0); |
124 | } |
125 | |
126 | static String _interpstr(SL::DataInterpolation p_interp) { |
127 | switch (p_interp) { |
128 | case SL::INTERPOLATION_FLAT: |
129 | return "flat " ; |
130 | case SL::INTERPOLATION_SMOOTH: |
131 | return "" ; |
132 | case SL::INTERPOLATION_DEFAULT: |
133 | return "" ; |
134 | } |
135 | return "" ; |
136 | } |
137 | |
138 | static String _prestr(SL::DataPrecision p_pres, bool p_force_highp = false) { |
139 | switch (p_pres) { |
140 | case SL::PRECISION_LOWP: |
141 | return "lowp " ; |
142 | case SL::PRECISION_MEDIUMP: |
143 | return "mediump " ; |
144 | case SL::PRECISION_HIGHP: |
145 | return "highp " ; |
146 | case SL::PRECISION_DEFAULT: |
147 | return p_force_highp ? "highp " : "" ; |
148 | } |
149 | return "" ; |
150 | } |
151 | |
152 | static String _constr(bool p_is_const) { |
153 | if (p_is_const) { |
154 | return "const " ; |
155 | } |
156 | return "" ; |
157 | } |
158 | |
159 | static String _qualstr(SL::ArgumentQualifier p_qual) { |
160 | switch (p_qual) { |
161 | case SL::ARGUMENT_QUALIFIER_IN: |
162 | return "" ; |
163 | case SL::ARGUMENT_QUALIFIER_OUT: |
164 | return "out " ; |
165 | case SL::ARGUMENT_QUALIFIER_INOUT: |
166 | return "inout " ; |
167 | } |
168 | return "" ; |
169 | } |
170 | |
171 | static String _opstr(SL::Operator p_op) { |
172 | return SL::get_operator_text(p_op); |
173 | } |
174 | |
175 | static String _mkid(const String &p_id) { |
176 | String id = "m_" + p_id.replace("__" , "_dus_" ); |
177 | return id.replace("__" , "_dus_" ); //doubleunderscore is reserved in glsl |
178 | } |
179 | |
180 | static String f2sp0(float p_float) { |
181 | String num = rtos(p_float); |
182 | if (!num.contains("." ) && !num.contains("e" )) { |
183 | num += ".0" ; |
184 | } |
185 | return num; |
186 | } |
187 | |
188 | static String get_constant_text(SL::DataType p_type, const Vector<SL::ConstantNode::Value> &p_values) { |
189 | switch (p_type) { |
190 | case SL::TYPE_BOOL: |
191 | return p_values[0].boolean ? "true" : "false" ; |
192 | case SL::TYPE_BVEC2: |
193 | case SL::TYPE_BVEC3: |
194 | case SL::TYPE_BVEC4: { |
195 | String text = "bvec" + itos(p_type - SL::TYPE_BOOL + 1) + "(" ; |
196 | for (int i = 0; i < p_values.size(); i++) { |
197 | if (i > 0) { |
198 | text += "," ; |
199 | } |
200 | |
201 | text += p_values[i].boolean ? "true" : "false" ; |
202 | } |
203 | text += ")" ; |
204 | return text; |
205 | } |
206 | |
207 | case SL::TYPE_INT: |
208 | return itos(p_values[0].sint); |
209 | case SL::TYPE_IVEC2: |
210 | case SL::TYPE_IVEC3: |
211 | case SL::TYPE_IVEC4: { |
212 | String text = "ivec" + itos(p_type - SL::TYPE_INT + 1) + "(" ; |
213 | for (int i = 0; i < p_values.size(); i++) { |
214 | if (i > 0) { |
215 | text += "," ; |
216 | } |
217 | |
218 | text += itos(p_values[i].sint); |
219 | } |
220 | text += ")" ; |
221 | return text; |
222 | |
223 | } break; |
224 | case SL::TYPE_UINT: |
225 | return itos(p_values[0].uint) + "u" ; |
226 | case SL::TYPE_UVEC2: |
227 | case SL::TYPE_UVEC3: |
228 | case SL::TYPE_UVEC4: { |
229 | String text = "uvec" + itos(p_type - SL::TYPE_UINT + 1) + "(" ; |
230 | for (int i = 0; i < p_values.size(); i++) { |
231 | if (i > 0) { |
232 | text += "," ; |
233 | } |
234 | |
235 | text += itos(p_values[i].uint) + "u" ; |
236 | } |
237 | text += ")" ; |
238 | return text; |
239 | } break; |
240 | case SL::TYPE_FLOAT: |
241 | return f2sp0(p_values[0].real); |
242 | case SL::TYPE_VEC2: |
243 | case SL::TYPE_VEC3: |
244 | case SL::TYPE_VEC4: { |
245 | String text = "vec" + itos(p_type - SL::TYPE_FLOAT + 1) + "(" ; |
246 | for (int i = 0; i < p_values.size(); i++) { |
247 | if (i > 0) { |
248 | text += "," ; |
249 | } |
250 | |
251 | text += f2sp0(p_values[i].real); |
252 | } |
253 | text += ")" ; |
254 | return text; |
255 | |
256 | } break; |
257 | case SL::TYPE_MAT2: |
258 | case SL::TYPE_MAT3: |
259 | case SL::TYPE_MAT4: { |
260 | String text = "mat" + itos(p_type - SL::TYPE_MAT2 + 2) + "(" ; |
261 | for (int i = 0; i < p_values.size(); i++) { |
262 | if (i > 0) { |
263 | text += "," ; |
264 | } |
265 | |
266 | text += f2sp0(p_values[i].real); |
267 | } |
268 | text += ")" ; |
269 | return text; |
270 | |
271 | } break; |
272 | default: |
273 | ERR_FAIL_V(String()); |
274 | } |
275 | } |
276 | |
277 | String ShaderCompiler::_get_sampler_name(ShaderLanguage::TextureFilter p_filter, ShaderLanguage::TextureRepeat p_repeat) { |
278 | if (p_filter == ShaderLanguage::FILTER_DEFAULT) { |
279 | ERR_FAIL_COND_V(actions.default_filter == ShaderLanguage::FILTER_DEFAULT, String()); |
280 | p_filter = actions.default_filter; |
281 | } |
282 | if (p_repeat == ShaderLanguage::REPEAT_DEFAULT) { |
283 | ERR_FAIL_COND_V(actions.default_repeat == ShaderLanguage::REPEAT_DEFAULT, String()); |
284 | p_repeat = actions.default_repeat; |
285 | } |
286 | constexpr const char *name_mapping[] = { |
287 | "SAMPLER_NEAREST_CLAMP" , |
288 | "SAMPLER_LINEAR_CLAMP" , |
289 | "SAMPLER_NEAREST_WITH_MIPMAPS_CLAMP" , |
290 | "SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP" , |
291 | "SAMPLER_NEAREST_WITH_MIPMAPS_ANISOTROPIC_CLAMP" , |
292 | "SAMPLER_LINEAR_WITH_MIPMAPS_ANISOTROPIC_CLAMP" , |
293 | "SAMPLER_NEAREST_REPEAT" , |
294 | "SAMPLER_LINEAR_REPEAT" , |
295 | "SAMPLER_NEAREST_WITH_MIPMAPS_REPEAT" , |
296 | "SAMPLER_LINEAR_WITH_MIPMAPS_REPEAT" , |
297 | "SAMPLER_NEAREST_WITH_MIPMAPS_ANISOTROPIC_REPEAT" , |
298 | "SAMPLER_LINEAR_WITH_MIPMAPS_ANISOTROPIC_REPEAT" |
299 | }; |
300 | return String(name_mapping[p_filter + (p_repeat == ShaderLanguage::REPEAT_ENABLE ? ShaderLanguage::FILTER_DEFAULT : 0)]); |
301 | } |
302 | |
303 | void ShaderCompiler::_dump_function_deps(const SL::ShaderNode *p_node, const StringName &p_for_func, const HashMap<StringName, String> &p_func_code, String &r_to_add, HashSet<StringName> &added) { |
304 | int fidx = -1; |
305 | |
306 | for (int i = 0; i < p_node->functions.size(); i++) { |
307 | if (p_node->functions[i].name == p_for_func) { |
308 | fidx = i; |
309 | break; |
310 | } |
311 | } |
312 | |
313 | ERR_FAIL_COND(fidx == -1); |
314 | |
315 | Vector<StringName> uses_functions; |
316 | |
317 | for (const StringName &E : p_node->functions[fidx].uses_function) { |
318 | uses_functions.push_back(E); |
319 | } |
320 | uses_functions.sort_custom<StringName::AlphCompare>(); //ensure order is deterministic so the same shader is always produced |
321 | |
322 | for (int k = 0; k < uses_functions.size(); k++) { |
323 | if (added.has(uses_functions[k])) { |
324 | continue; //was added already |
325 | } |
326 | |
327 | _dump_function_deps(p_node, uses_functions[k], p_func_code, r_to_add, added); |
328 | |
329 | SL::FunctionNode *fnode = nullptr; |
330 | |
331 | for (int i = 0; i < p_node->functions.size(); i++) { |
332 | if (p_node->functions[i].name == uses_functions[k]) { |
333 | fnode = p_node->functions[i].function; |
334 | break; |
335 | } |
336 | } |
337 | |
338 | ERR_FAIL_COND(!fnode); |
339 | |
340 | r_to_add += "\n" ; |
341 | |
342 | String ; |
343 | if (fnode->return_type == SL::TYPE_STRUCT) { |
344 | header = _mkid(fnode->return_struct_name); |
345 | } else { |
346 | header = _typestr(fnode->return_type); |
347 | } |
348 | |
349 | if (fnode->return_array_size > 0) { |
350 | header += "[" ; |
351 | header += itos(fnode->return_array_size); |
352 | header += "]" ; |
353 | } |
354 | |
355 | header += " " ; |
356 | header += _mkid(fnode->name); |
357 | header += "(" ; |
358 | |
359 | for (int i = 0; i < fnode->arguments.size(); i++) { |
360 | if (i > 0) { |
361 | header += ", " ; |
362 | } |
363 | header += _constr(fnode->arguments[i].is_const); |
364 | if (fnode->arguments[i].type == SL::TYPE_STRUCT) { |
365 | header += _qualstr(fnode->arguments[i].qualifier) + _mkid(fnode->arguments[i].type_str) + " " + _mkid(fnode->arguments[i].name); |
366 | } else { |
367 | header += _qualstr(fnode->arguments[i].qualifier) + _prestr(fnode->arguments[i].precision) + _typestr(fnode->arguments[i].type) + " " + _mkid(fnode->arguments[i].name); |
368 | } |
369 | if (fnode->arguments[i].array_size > 0) { |
370 | header += "[" ; |
371 | header += itos(fnode->arguments[i].array_size); |
372 | header += "]" ; |
373 | } |
374 | } |
375 | |
376 | header += ")\n" ; |
377 | r_to_add += header; |
378 | r_to_add += p_func_code[uses_functions[k]]; |
379 | |
380 | added.insert(uses_functions[k]); |
381 | } |
382 | } |
383 | |
384 | static String _get_global_shader_uniform_from_type_and_index(const String &p_buffer, const String &p_index, ShaderLanguage::DataType p_type) { |
385 | switch (p_type) { |
386 | case ShaderLanguage::TYPE_BOOL: { |
387 | return "bool(floatBitsToUint(" + p_buffer + "[" + p_index + "].x))" ; |
388 | } |
389 | case ShaderLanguage::TYPE_BVEC2: { |
390 | return "bvec2(floatBitsToUint(" + p_buffer + "[" + p_index + "].xy))" ; |
391 | } |
392 | case ShaderLanguage::TYPE_BVEC3: { |
393 | return "bvec3(floatBitsToUint(" + p_buffer + "[" + p_index + "].xyz))" ; |
394 | } |
395 | case ShaderLanguage::TYPE_BVEC4: { |
396 | return "bvec4(floatBitsToUint(" + p_buffer + "[" + p_index + "].xyzw))" ; |
397 | } |
398 | case ShaderLanguage::TYPE_INT: { |
399 | return "floatBitsToInt(" + p_buffer + "[" + p_index + "].x)" ; |
400 | } |
401 | case ShaderLanguage::TYPE_IVEC2: { |
402 | return "floatBitsToInt(" + p_buffer + "[" + p_index + "].xy)" ; |
403 | } |
404 | case ShaderLanguage::TYPE_IVEC3: { |
405 | return "floatBitsToInt(" + p_buffer + "[" + p_index + "].xyz)" ; |
406 | } |
407 | case ShaderLanguage::TYPE_IVEC4: { |
408 | return "floatBitsToInt(" + p_buffer + "[" + p_index + "].xyzw)" ; |
409 | } |
410 | case ShaderLanguage::TYPE_UINT: { |
411 | return "floatBitsToUint(" + p_buffer + "[" + p_index + "].x)" ; |
412 | } |
413 | case ShaderLanguage::TYPE_UVEC2: { |
414 | return "floatBitsToUint(" + p_buffer + "[" + p_index + "].xy)" ; |
415 | } |
416 | case ShaderLanguage::TYPE_UVEC3: { |
417 | return "floatBitsToUint(" + p_buffer + "[" + p_index + "].xyz)" ; |
418 | } |
419 | case ShaderLanguage::TYPE_UVEC4: { |
420 | return "floatBitsToUint(" + p_buffer + "[" + p_index + "].xyzw)" ; |
421 | } |
422 | case ShaderLanguage::TYPE_FLOAT: { |
423 | return "(" + p_buffer + "[" + p_index + "].x)" ; |
424 | } |
425 | case ShaderLanguage::TYPE_VEC2: { |
426 | return "(" + p_buffer + "[" + p_index + "].xy)" ; |
427 | } |
428 | case ShaderLanguage::TYPE_VEC3: { |
429 | return "(" + p_buffer + "[" + p_index + "].xyz)" ; |
430 | } |
431 | case ShaderLanguage::TYPE_VEC4: { |
432 | return "(" + p_buffer + "[" + p_index + "].xyzw)" ; |
433 | } |
434 | case ShaderLanguage::TYPE_MAT2: { |
435 | return "mat2(" + p_buffer + "[" + p_index + "].xy," + p_buffer + "[" + p_index + "+1].xy)" ; |
436 | } |
437 | case ShaderLanguage::TYPE_MAT3: { |
438 | return "mat3(" + p_buffer + "[" + p_index + "].xyz," + p_buffer + "[" + p_index + "+1].xyz," + p_buffer + "[" + p_index + "+2].xyz)" ; |
439 | } |
440 | case ShaderLanguage::TYPE_MAT4: { |
441 | return "mat4(" + p_buffer + "[" + p_index + "].xyzw," + p_buffer + "[" + p_index + "+1].xyzw," + p_buffer + "[" + p_index + "+2].xyzw," + p_buffer + "[" + p_index + "+3].xyzw)" ; |
442 | } |
443 | default: { |
444 | ERR_FAIL_V("void" ); |
445 | } |
446 | } |
447 | } |
448 | |
449 | String ShaderCompiler::_dump_node_code(const SL::Node *p_node, int p_level, GeneratedCode &r_gen_code, IdentifierActions &p_actions, const DefaultIdentifierActions &p_default_actions, bool p_assigning, bool p_use_scope) { |
450 | String code; |
451 | |
452 | switch (p_node->type) { |
453 | case SL::Node::NODE_TYPE_SHADER: { |
454 | SL::ShaderNode *pnode = (SL::ShaderNode *)p_node; |
455 | |
456 | for (int i = 0; i < pnode->render_modes.size(); i++) { |
457 | if (p_default_actions.render_mode_defines.has(pnode->render_modes[i]) && !used_rmode_defines.has(pnode->render_modes[i])) { |
458 | r_gen_code.defines.push_back(p_default_actions.render_mode_defines[pnode->render_modes[i]]); |
459 | used_rmode_defines.insert(pnode->render_modes[i]); |
460 | } |
461 | |
462 | if (p_actions.render_mode_flags.has(pnode->render_modes[i])) { |
463 | *p_actions.render_mode_flags[pnode->render_modes[i]] = true; |
464 | } |
465 | |
466 | if (p_actions.render_mode_values.has(pnode->render_modes[i])) { |
467 | Pair<int *, int> &p = p_actions.render_mode_values[pnode->render_modes[i]]; |
468 | *p.first = p.second; |
469 | } |
470 | } |
471 | |
472 | // structs |
473 | |
474 | for (int i = 0; i < pnode->vstructs.size(); i++) { |
475 | SL::StructNode *st = pnode->vstructs[i].shader_struct; |
476 | String struct_code; |
477 | |
478 | struct_code += "struct " ; |
479 | struct_code += _mkid(pnode->vstructs[i].name); |
480 | struct_code += " " ; |
481 | struct_code += "{\n" ; |
482 | for (int j = 0; j < st->members.size(); j++) { |
483 | SL::MemberNode *m = st->members[j]; |
484 | if (m->datatype == SL::TYPE_STRUCT) { |
485 | struct_code += _mkid(m->struct_name); |
486 | } else { |
487 | struct_code += _prestr(m->precision); |
488 | struct_code += _typestr(m->datatype); |
489 | } |
490 | struct_code += " " ; |
491 | struct_code += m->name; |
492 | if (m->array_size > 0) { |
493 | struct_code += "[" ; |
494 | struct_code += itos(m->array_size); |
495 | struct_code += "]" ; |
496 | } |
497 | struct_code += ";\n" ; |
498 | } |
499 | struct_code += "}" ; |
500 | struct_code += ";\n" ; |
501 | |
502 | for (int j = 0; j < STAGE_MAX; j++) { |
503 | r_gen_code.stage_globals[j] += struct_code; |
504 | } |
505 | } |
506 | |
507 | int max_texture_uniforms = 0; |
508 | int max_uniforms = 0; |
509 | |
510 | for (const KeyValue<StringName, SL::ShaderNode::Uniform> &E : pnode->uniforms) { |
511 | if (SL::is_sampler_type(E.value.type)) { |
512 | if (E.value.hint == SL::ShaderNode::Uniform::HINT_SCREEN_TEXTURE || |
513 | E.value.hint == SL::ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE || |
514 | E.value.hint == SL::ShaderNode::Uniform::HINT_DEPTH_TEXTURE) { |
515 | continue; // Don't create uniforms in the generated code for these. |
516 | } |
517 | max_texture_uniforms++; |
518 | } else { |
519 | if (E.value.scope == SL::ShaderNode::Uniform::SCOPE_INSTANCE) { |
520 | continue; // Instances are indexed directly, don't need index uniforms. |
521 | } |
522 | |
523 | max_uniforms++; |
524 | } |
525 | } |
526 | |
527 | r_gen_code.texture_uniforms.resize(max_texture_uniforms); |
528 | |
529 | Vector<int> uniform_sizes; |
530 | Vector<int> uniform_alignments; |
531 | Vector<StringName> uniform_defines; |
532 | uniform_sizes.resize(max_uniforms); |
533 | uniform_alignments.resize(max_uniforms); |
534 | uniform_defines.resize(max_uniforms); |
535 | bool uses_uniforms = false; |
536 | |
537 | Vector<StringName> uniform_names; |
538 | |
539 | for (const KeyValue<StringName, SL::ShaderNode::Uniform> &E : pnode->uniforms) { |
540 | uniform_names.push_back(E.key); |
541 | } |
542 | |
543 | uniform_names.sort_custom<StringName::AlphCompare>(); //ensure order is deterministic so the same shader is always produced |
544 | |
545 | for (int k = 0; k < uniform_names.size(); k++) { |
546 | StringName uniform_name = uniform_names[k]; |
547 | const SL::ShaderNode::Uniform &uniform = pnode->uniforms[uniform_name]; |
548 | |
549 | String ucode; |
550 | |
551 | if (uniform.scope == SL::ShaderNode::Uniform::SCOPE_INSTANCE) { |
552 | //insert, but don't generate any code. |
553 | p_actions.uniforms->insert(uniform_name, uniform); |
554 | continue; // Instances are indexed directly, don't need index uniforms. |
555 | } |
556 | |
557 | if (uniform.hint == SL::ShaderNode::Uniform::HINT_SCREEN_TEXTURE || |
558 | uniform.hint == SL::ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE || |
559 | uniform.hint == SL::ShaderNode::Uniform::HINT_DEPTH_TEXTURE) { |
560 | continue; // Don't create uniforms in the generated code for these. |
561 | } |
562 | |
563 | if (SL::is_sampler_type(uniform.type)) { |
564 | // Texture layouts are different for OpenGL GLSL and Vulkan GLSL |
565 | if (!RS::get_singleton()->is_low_end()) { |
566 | ucode = "layout(set = " + itos(actions.texture_layout_set) + ", binding = " + itos(actions.base_texture_binding_index + uniform.texture_binding) + ") " ; |
567 | } |
568 | ucode += "uniform " ; |
569 | } |
570 | |
571 | bool is_buffer_global = !SL::is_sampler_type(uniform.type) && uniform.scope == SL::ShaderNode::Uniform::SCOPE_GLOBAL; |
572 | |
573 | if (is_buffer_global) { |
574 | //this is an integer to index the global table |
575 | ucode += _typestr(ShaderLanguage::TYPE_UINT); |
576 | } else { |
577 | ucode += _prestr(uniform.precision, ShaderLanguage::is_float_type(uniform.type)); |
578 | ucode += _typestr(uniform.type); |
579 | } |
580 | |
581 | ucode += " " + _mkid(uniform_name); |
582 | if (uniform.array_size > 0) { |
583 | ucode += "[" ; |
584 | ucode += itos(uniform.array_size); |
585 | ucode += "]" ; |
586 | } |
587 | ucode += ";\n" ; |
588 | if (SL::is_sampler_type(uniform.type)) { |
589 | for (int j = 0; j < STAGE_MAX; j++) { |
590 | r_gen_code.stage_globals[j] += ucode; |
591 | } |
592 | |
593 | GeneratedCode::Texture texture; |
594 | texture.name = uniform_name; |
595 | texture.hint = uniform.hint; |
596 | texture.type = uniform.type; |
597 | texture.use_color = uniform.use_color; |
598 | texture.filter = uniform.filter; |
599 | texture.repeat = uniform.repeat; |
600 | texture.global = uniform.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL; |
601 | texture.array_size = uniform.array_size; |
602 | if (texture.global) { |
603 | r_gen_code.uses_global_textures = true; |
604 | } |
605 | |
606 | r_gen_code.texture_uniforms.write[uniform.texture_order] = texture; |
607 | } else { |
608 | if (!uses_uniforms) { |
609 | uses_uniforms = true; |
610 | } |
611 | uniform_defines.write[uniform.order] = ucode; |
612 | if (is_buffer_global) { |
613 | //globals are indices into the global table |
614 | uniform_sizes.write[uniform.order] = ShaderLanguage::get_datatype_size(ShaderLanguage::TYPE_UINT); |
615 | uniform_alignments.write[uniform.order] = _get_datatype_alignment(ShaderLanguage::TYPE_UINT); |
616 | } else { |
617 | // The following code enforces a 16-byte alignment of uniform arrays. |
618 | if (uniform.array_size > 0) { |
619 | int size = ShaderLanguage::get_datatype_size(uniform.type) * uniform.array_size; |
620 | int m = (16 * uniform.array_size); |
621 | if ((size % m) != 0) { |
622 | size += m - (size % m); |
623 | } |
624 | uniform_sizes.write[uniform.order] = size; |
625 | uniform_alignments.write[uniform.order] = 16; |
626 | } else { |
627 | uniform_sizes.write[uniform.order] = ShaderLanguage::get_datatype_size(uniform.type); |
628 | uniform_alignments.write[uniform.order] = _get_datatype_alignment(uniform.type); |
629 | } |
630 | } |
631 | } |
632 | |
633 | p_actions.uniforms->insert(uniform_name, uniform); |
634 | } |
635 | |
636 | for (int i = 0; i < max_uniforms; i++) { |
637 | r_gen_code.uniforms += uniform_defines[i]; |
638 | } |
639 | |
640 | // add up |
641 | int offset = 0; |
642 | for (int i = 0; i < uniform_sizes.size(); i++) { |
643 | int align = offset % uniform_alignments[i]; |
644 | |
645 | if (align != 0) { |
646 | offset += uniform_alignments[i] - align; |
647 | } |
648 | |
649 | r_gen_code.uniform_offsets.push_back(offset); |
650 | |
651 | offset += uniform_sizes[i]; |
652 | } |
653 | |
654 | r_gen_code.uniform_total_size = offset; |
655 | |
656 | if (r_gen_code.uniform_total_size % 16 != 0) { //UBO sizes must be multiples of 16 |
657 | r_gen_code.uniform_total_size += 16 - (r_gen_code.uniform_total_size % 16); |
658 | } |
659 | |
660 | uint32_t index = p_default_actions.base_varying_index; |
661 | |
662 | List<Pair<StringName, SL::ShaderNode::Varying>> var_frag_to_light; |
663 | |
664 | Vector<StringName> varying_names; |
665 | |
666 | for (const KeyValue<StringName, SL::ShaderNode::Varying> &E : pnode->varyings) { |
667 | varying_names.push_back(E.key); |
668 | } |
669 | |
670 | varying_names.sort_custom<StringName::AlphCompare>(); //ensure order is deterministic so the same shader is always produced |
671 | |
672 | for (int k = 0; k < varying_names.size(); k++) { |
673 | StringName varying_name = varying_names[k]; |
674 | const SL::ShaderNode::Varying &varying = pnode->varyings[varying_name]; |
675 | |
676 | if (varying.stage == SL::ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT || varying.stage == SL::ShaderNode::Varying::STAGE_FRAGMENT) { |
677 | var_frag_to_light.push_back(Pair<StringName, SL::ShaderNode::Varying>(varying_name, varying)); |
678 | fragment_varyings.insert(varying_name); |
679 | continue; |
680 | } |
681 | if (varying.type < SL::TYPE_INT) { |
682 | continue; // Ignore boolean types to prevent crashing (if varying is just declared). |
683 | } |
684 | |
685 | String vcode; |
686 | String interp_mode = _interpstr(varying.interpolation); |
687 | vcode += _prestr(varying.precision, ShaderLanguage::is_float_type(varying.type)); |
688 | vcode += _typestr(varying.type); |
689 | vcode += " " + _mkid(varying_name); |
690 | uint32_t inc = 1U; |
691 | |
692 | if (varying.array_size > 0) { |
693 | inc = (uint32_t)varying.array_size; |
694 | |
695 | vcode += "[" ; |
696 | vcode += itos(varying.array_size); |
697 | vcode += "]" ; |
698 | } |
699 | |
700 | switch (varying.type) { |
701 | case SL::TYPE_MAT2: |
702 | inc *= 2U; |
703 | break; |
704 | case SL::TYPE_MAT3: |
705 | inc *= 3U; |
706 | break; |
707 | case SL::TYPE_MAT4: |
708 | inc *= 4U; |
709 | break; |
710 | default: |
711 | break; |
712 | } |
713 | |
714 | vcode += ";\n" ; |
715 | // GLSL ES 3.0 does not allow layout qualifiers for varyings |
716 | if (!RS::get_singleton()->is_low_end()) { |
717 | r_gen_code.stage_globals[STAGE_VERTEX] += "layout(location=" + itos(index) + ") " ; |
718 | r_gen_code.stage_globals[STAGE_FRAGMENT] += "layout(location=" + itos(index) + ") " ; |
719 | } |
720 | r_gen_code.stage_globals[STAGE_VERTEX] += interp_mode + "out " + vcode; |
721 | r_gen_code.stage_globals[STAGE_FRAGMENT] += interp_mode + "in " + vcode; |
722 | |
723 | index += inc; |
724 | } |
725 | |
726 | if (var_frag_to_light.size() > 0) { |
727 | String gcode = "\n\nstruct {\n" ; |
728 | for (const Pair<StringName, SL::ShaderNode::Varying> &E : var_frag_to_light) { |
729 | gcode += "\t" + _prestr(E.second.precision) + _typestr(E.second.type) + " " + _mkid(E.first); |
730 | if (E.second.array_size > 0) { |
731 | gcode += "[" ; |
732 | gcode += itos(E.second.array_size); |
733 | gcode += "]" ; |
734 | } |
735 | gcode += ";\n" ; |
736 | } |
737 | gcode += "} frag_to_light;\n" ; |
738 | r_gen_code.stage_globals[STAGE_FRAGMENT] += gcode; |
739 | } |
740 | |
741 | for (int i = 0; i < pnode->vconstants.size(); i++) { |
742 | const SL::ShaderNode::Constant &cnode = pnode->vconstants[i]; |
743 | String gcode; |
744 | gcode += _constr(true); |
745 | gcode += _prestr(cnode.precision, ShaderLanguage::is_float_type(cnode.type)); |
746 | if (cnode.type == SL::TYPE_STRUCT) { |
747 | gcode += _mkid(cnode.type_str); |
748 | } else { |
749 | gcode += _typestr(cnode.type); |
750 | } |
751 | gcode += " " + _mkid(String(cnode.name)); |
752 | if (cnode.array_size > 0) { |
753 | gcode += "[" ; |
754 | gcode += itos(cnode.array_size); |
755 | gcode += "]" ; |
756 | } |
757 | gcode += "=" ; |
758 | gcode += _dump_node_code(cnode.initializer, p_level, r_gen_code, p_actions, p_default_actions, p_assigning); |
759 | gcode += ";\n" ; |
760 | for (int j = 0; j < STAGE_MAX; j++) { |
761 | r_gen_code.stage_globals[j] += gcode; |
762 | } |
763 | } |
764 | |
765 | HashMap<StringName, String> function_code; |
766 | |
767 | //code for functions |
768 | for (int i = 0; i < pnode->functions.size(); i++) { |
769 | SL::FunctionNode *fnode = pnode->functions[i].function; |
770 | function = fnode; |
771 | current_func_name = fnode->name; |
772 | function_code[fnode->name] = _dump_node_code(fnode->body, p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning); |
773 | function = nullptr; |
774 | } |
775 | |
776 | //place functions in actual code |
777 | |
778 | HashSet<StringName> added_funcs_per_stage[STAGE_MAX]; |
779 | |
780 | for (int i = 0; i < pnode->functions.size(); i++) { |
781 | SL::FunctionNode *fnode = pnode->functions[i].function; |
782 | |
783 | function = fnode; |
784 | |
785 | current_func_name = fnode->name; |
786 | |
787 | if (p_actions.entry_point_stages.has(fnode->name)) { |
788 | Stage stage = p_actions.entry_point_stages[fnode->name]; |
789 | _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.stage_globals[stage], added_funcs_per_stage[stage]); |
790 | r_gen_code.code[fnode->name] = function_code[fnode->name]; |
791 | } |
792 | |
793 | function = nullptr; |
794 | } |
795 | |
796 | //code+=dump_node_code(pnode->body,p_level); |
797 | } break; |
798 | case SL::Node::NODE_TYPE_STRUCT: { |
799 | } break; |
800 | case SL::Node::NODE_TYPE_FUNCTION: { |
801 | } break; |
802 | case SL::Node::NODE_TYPE_BLOCK: { |
803 | SL::BlockNode *bnode = (SL::BlockNode *)p_node; |
804 | |
805 | //variables |
806 | if (!bnode->single_statement) { |
807 | code += _mktab(p_level - 1) + "{\n" ; |
808 | } |
809 | |
810 | for (int i = 0; i < bnode->statements.size(); i++) { |
811 | String scode = _dump_node_code(bnode->statements[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); |
812 | |
813 | if (bnode->statements[i]->type == SL::Node::NODE_TYPE_CONTROL_FLOW || bnode->single_statement) { |
814 | code += scode; //use directly |
815 | if (bnode->use_comma_between_statements && i + 1 < bnode->statements.size()) { |
816 | code += "," ; |
817 | } |
818 | } else { |
819 | code += _mktab(p_level) + scode + ";\n" ; |
820 | } |
821 | } |
822 | if (!bnode->single_statement) { |
823 | code += _mktab(p_level - 1) + "}\n" ; |
824 | } |
825 | |
826 | } break; |
827 | case SL::Node::NODE_TYPE_VARIABLE_DECLARATION: { |
828 | SL::VariableDeclarationNode *vdnode = (SL::VariableDeclarationNode *)p_node; |
829 | |
830 | String declaration; |
831 | declaration += _constr(vdnode->is_const); |
832 | if (vdnode->datatype == SL::TYPE_STRUCT) { |
833 | declaration += _mkid(vdnode->struct_name); |
834 | } else { |
835 | declaration += _prestr(vdnode->precision) + _typestr(vdnode->datatype); |
836 | } |
837 | declaration += " " ; |
838 | for (int i = 0; i < vdnode->declarations.size(); i++) { |
839 | bool is_array = vdnode->declarations[i].size > 0; |
840 | if (i > 0) { |
841 | declaration += "," ; |
842 | } |
843 | declaration += _mkid(vdnode->declarations[i].name); |
844 | if (is_array) { |
845 | declaration += "[" ; |
846 | if (vdnode->declarations[i].size_expression != nullptr) { |
847 | declaration += _dump_node_code(vdnode->declarations[i].size_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning); |
848 | } else { |
849 | declaration += itos(vdnode->declarations[i].size); |
850 | } |
851 | declaration += "]" ; |
852 | } |
853 | |
854 | if (!is_array || vdnode->declarations[i].single_expression) { |
855 | if (!vdnode->declarations[i].initializer.is_empty()) { |
856 | declaration += "=" ; |
857 | declaration += _dump_node_code(vdnode->declarations[i].initializer[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); |
858 | } |
859 | } else { |
860 | int size = vdnode->declarations[i].initializer.size(); |
861 | if (size > 0) { |
862 | declaration += "=" ; |
863 | if (vdnode->datatype == SL::TYPE_STRUCT) { |
864 | declaration += _mkid(vdnode->struct_name); |
865 | } else { |
866 | declaration += _typestr(vdnode->datatype); |
867 | } |
868 | declaration += "[" ; |
869 | declaration += itos(size); |
870 | declaration += "]" ; |
871 | declaration += "(" ; |
872 | for (int j = 0; j < size; j++) { |
873 | if (j > 0) { |
874 | declaration += "," ; |
875 | } |
876 | declaration += _dump_node_code(vdnode->declarations[i].initializer[j], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); |
877 | } |
878 | declaration += ")" ; |
879 | } |
880 | } |
881 | } |
882 | |
883 | code += declaration; |
884 | } break; |
885 | case SL::Node::NODE_TYPE_VARIABLE: { |
886 | SL::VariableNode *vnode = (SL::VariableNode *)p_node; |
887 | bool use_fragment_varying = false; |
888 | |
889 | if (!vnode->is_local && !(p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX)) { |
890 | if (p_assigning) { |
891 | if (shader->varyings.has(vnode->name)) { |
892 | use_fragment_varying = true; |
893 | } |
894 | } else { |
895 | if (fragment_varyings.has(vnode->name)) { |
896 | use_fragment_varying = true; |
897 | } |
898 | } |
899 | } |
900 | |
901 | if (p_assigning && p_actions.write_flag_pointers.has(vnode->name)) { |
902 | *p_actions.write_flag_pointers[vnode->name] = true; |
903 | } |
904 | |
905 | if (p_default_actions.usage_defines.has(vnode->name) && !used_name_defines.has(vnode->name)) { |
906 | String define = p_default_actions.usage_defines[vnode->name]; |
907 | if (define.begins_with("@" )) { |
908 | define = p_default_actions.usage_defines[define.substr(1, define.length())]; |
909 | } |
910 | r_gen_code.defines.push_back(define); |
911 | used_name_defines.insert(vnode->name); |
912 | } |
913 | |
914 | if (p_actions.usage_flag_pointers.has(vnode->name) && !used_flag_pointers.has(vnode->name)) { |
915 | *p_actions.usage_flag_pointers[vnode->name] = true; |
916 | used_flag_pointers.insert(vnode->name); |
917 | } |
918 | |
919 | if (p_default_actions.renames.has(vnode->name)) { |
920 | code = p_default_actions.renames[vnode->name]; |
921 | } else { |
922 | if (shader->uniforms.has(vnode->name)) { |
923 | //its a uniform! |
924 | const ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[vnode->name]; |
925 | if (u.texture_order >= 0) { |
926 | StringName name; |
927 | if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SCREEN_TEXTURE) { |
928 | name = "color_buffer" ; |
929 | if (u.filter >= ShaderLanguage::FILTER_NEAREST_MIPMAP) { |
930 | r_gen_code.uses_screen_texture_mipmaps = true; |
931 | } |
932 | r_gen_code.uses_screen_texture = true; |
933 | } else if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE) { |
934 | name = "normal_roughness_buffer" ; |
935 | r_gen_code.uses_normal_roughness_texture = true; |
936 | } else if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_DEPTH_TEXTURE) { |
937 | name = "depth_buffer" ; |
938 | r_gen_code.uses_depth_texture = true; |
939 | } else { |
940 | name = _mkid(vnode->name); //texture, use as is |
941 | } |
942 | |
943 | code = name; |
944 | } else { |
945 | //a scalar or vector |
946 | if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL) { |
947 | code = actions.base_uniform_string + _mkid(vnode->name); //texture, use as is |
948 | //global variable, this means the code points to an index to the global table |
949 | code = _get_global_shader_uniform_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type); |
950 | } else if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) { |
951 | //instance variable, index it as such |
952 | code = "(" + p_default_actions.instance_uniform_index_variable + "+" + itos(u.instance_index) + ")" ; |
953 | code = _get_global_shader_uniform_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type); |
954 | } else { |
955 | //regular uniform, index from UBO |
956 | code = actions.base_uniform_string + _mkid(vnode->name); |
957 | } |
958 | } |
959 | |
960 | } else { |
961 | if (use_fragment_varying) { |
962 | code = "frag_to_light." ; |
963 | } |
964 | code += _mkid(vnode->name); //its something else (local var most likely) use as is |
965 | } |
966 | } |
967 | |
968 | if (vnode->name == time_name) { |
969 | if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX) { |
970 | r_gen_code.uses_vertex_time = true; |
971 | } |
972 | if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_FRAGMENT) { |
973 | r_gen_code.uses_fragment_time = true; |
974 | } |
975 | } |
976 | |
977 | } break; |
978 | case SL::Node::NODE_TYPE_ARRAY_CONSTRUCT: { |
979 | SL::ArrayConstructNode *acnode = (SL::ArrayConstructNode *)p_node; |
980 | int sz = acnode->initializer.size(); |
981 | if (acnode->datatype == SL::TYPE_STRUCT) { |
982 | code += _mkid(acnode->struct_name); |
983 | } else { |
984 | code += _typestr(acnode->datatype); |
985 | } |
986 | code += "[" ; |
987 | code += itos(acnode->initializer.size()); |
988 | code += "]" ; |
989 | code += "(" ; |
990 | for (int i = 0; i < sz; i++) { |
991 | code += _dump_node_code(acnode->initializer[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); |
992 | if (i != sz - 1) { |
993 | code += ", " ; |
994 | } |
995 | } |
996 | code += ")" ; |
997 | } break; |
998 | case SL::Node::NODE_TYPE_ARRAY: { |
999 | SL::ArrayNode *anode = (SL::ArrayNode *)p_node; |
1000 | bool use_fragment_varying = false; |
1001 | |
1002 | if (!anode->is_local && !(p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX)) { |
1003 | if (anode->assign_expression != nullptr && shader->varyings.has(anode->name)) { |
1004 | use_fragment_varying = true; |
1005 | } else { |
1006 | if (p_assigning) { |
1007 | if (shader->varyings.has(anode->name)) { |
1008 | use_fragment_varying = true; |
1009 | } |
1010 | } else { |
1011 | if (fragment_varyings.has(anode->name)) { |
1012 | use_fragment_varying = true; |
1013 | } |
1014 | } |
1015 | } |
1016 | } |
1017 | |
1018 | if (p_assigning && p_actions.write_flag_pointers.has(anode->name)) { |
1019 | *p_actions.write_flag_pointers[anode->name] = true; |
1020 | } |
1021 | |
1022 | if (p_default_actions.usage_defines.has(anode->name) && !used_name_defines.has(anode->name)) { |
1023 | String define = p_default_actions.usage_defines[anode->name]; |
1024 | if (define.begins_with("@" )) { |
1025 | define = p_default_actions.usage_defines[define.substr(1, define.length())]; |
1026 | } |
1027 | r_gen_code.defines.push_back(define); |
1028 | used_name_defines.insert(anode->name); |
1029 | } |
1030 | |
1031 | if (p_actions.usage_flag_pointers.has(anode->name) && !used_flag_pointers.has(anode->name)) { |
1032 | *p_actions.usage_flag_pointers[anode->name] = true; |
1033 | used_flag_pointers.insert(anode->name); |
1034 | } |
1035 | |
1036 | if (p_default_actions.renames.has(anode->name)) { |
1037 | code = p_default_actions.renames[anode->name]; |
1038 | } else { |
1039 | if (shader->uniforms.has(anode->name)) { |
1040 | //its a uniform! |
1041 | const ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[anode->name]; |
1042 | if (u.texture_order >= 0) { |
1043 | code = _mkid(anode->name); //texture, use as is |
1044 | } else { |
1045 | //a scalar or vector |
1046 | if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL) { |
1047 | code = actions.base_uniform_string + _mkid(anode->name); //texture, use as is |
1048 | //global variable, this means the code points to an index to the global table |
1049 | code = _get_global_shader_uniform_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type); |
1050 | } else if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) { |
1051 | //instance variable, index it as such |
1052 | code = "(" + p_default_actions.instance_uniform_index_variable + "+" + itos(u.instance_index) + ")" ; |
1053 | code = _get_global_shader_uniform_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type); |
1054 | } else { |
1055 | //regular uniform, index from UBO |
1056 | code = actions.base_uniform_string + _mkid(anode->name); |
1057 | } |
1058 | } |
1059 | } else { |
1060 | if (use_fragment_varying) { |
1061 | code = "frag_to_light." ; |
1062 | } |
1063 | code += _mkid(anode->name); |
1064 | } |
1065 | } |
1066 | |
1067 | if (anode->call_expression != nullptr) { |
1068 | code += "." ; |
1069 | code += _dump_node_code(anode->call_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning, false); |
1070 | } else if (anode->index_expression != nullptr) { |
1071 | code += "[" ; |
1072 | code += _dump_node_code(anode->index_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning); |
1073 | code += "]" ; |
1074 | } else if (anode->assign_expression != nullptr) { |
1075 | code += "=" ; |
1076 | code += _dump_node_code(anode->assign_expression, p_level, r_gen_code, p_actions, p_default_actions, true, false); |
1077 | } |
1078 | |
1079 | if (anode->name == time_name) { |
1080 | if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX) { |
1081 | r_gen_code.uses_vertex_time = true; |
1082 | } |
1083 | if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_FRAGMENT) { |
1084 | r_gen_code.uses_fragment_time = true; |
1085 | } |
1086 | } |
1087 | |
1088 | } break; |
1089 | case SL::Node::NODE_TYPE_CONSTANT: { |
1090 | SL::ConstantNode *cnode = (SL::ConstantNode *)p_node; |
1091 | |
1092 | if (cnode->array_size == 0) { |
1093 | return get_constant_text(cnode->datatype, cnode->values); |
1094 | } else { |
1095 | if (cnode->get_datatype() == SL::TYPE_STRUCT) { |
1096 | code += _mkid(cnode->struct_name); |
1097 | } else { |
1098 | code += _typestr(cnode->datatype); |
1099 | } |
1100 | code += "[" ; |
1101 | code += itos(cnode->array_size); |
1102 | code += "]" ; |
1103 | code += "(" ; |
1104 | for (int i = 0; i < cnode->array_size; i++) { |
1105 | if (i > 0) { |
1106 | code += "," ; |
1107 | } else { |
1108 | code += "" ; |
1109 | } |
1110 | code += _dump_node_code(cnode->array_declarations[0].initializer[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); |
1111 | } |
1112 | code += ")" ; |
1113 | } |
1114 | |
1115 | } break; |
1116 | case SL::Node::NODE_TYPE_OPERATOR: { |
1117 | SL::OperatorNode *onode = (SL::OperatorNode *)p_node; |
1118 | |
1119 | switch (onode->op) { |
1120 | case SL::OP_ASSIGN: |
1121 | case SL::OP_ASSIGN_ADD: |
1122 | case SL::OP_ASSIGN_SUB: |
1123 | case SL::OP_ASSIGN_MUL: |
1124 | case SL::OP_ASSIGN_DIV: |
1125 | case SL::OP_ASSIGN_SHIFT_LEFT: |
1126 | case SL::OP_ASSIGN_SHIFT_RIGHT: |
1127 | case SL::OP_ASSIGN_MOD: |
1128 | case SL::OP_ASSIGN_BIT_AND: |
1129 | case SL::OP_ASSIGN_BIT_OR: |
1130 | case SL::OP_ASSIGN_BIT_XOR: |
1131 | code = _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, true) + _opstr(onode->op) + _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); |
1132 | break; |
1133 | case SL::OP_BIT_INVERT: |
1134 | case SL::OP_NEGATE: |
1135 | case SL::OP_NOT: |
1136 | case SL::OP_DECREMENT: |
1137 | case SL::OP_INCREMENT: |
1138 | code = _opstr(onode->op) + _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); |
1139 | break; |
1140 | case SL::OP_POST_DECREMENT: |
1141 | case SL::OP_POST_INCREMENT: |
1142 | code = _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + _opstr(onode->op); |
1143 | break; |
1144 | case SL::OP_CALL: |
1145 | case SL::OP_STRUCT: |
1146 | case SL::OP_CONSTRUCT: { |
1147 | ERR_FAIL_COND_V(onode->arguments[0]->type != SL::Node::NODE_TYPE_VARIABLE, String()); |
1148 | const SL::VariableNode *vnode = static_cast<const SL::VariableNode *>(onode->arguments[0]); |
1149 | const SL::FunctionNode *func = nullptr; |
1150 | const bool is_internal_func = internal_functions.has(vnode->name); |
1151 | |
1152 | if (!is_internal_func) { |
1153 | for (int i = 0; i < shader->functions.size(); i++) { |
1154 | if (shader->functions[i].name == vnode->name) { |
1155 | func = shader->functions[i].function; |
1156 | break; |
1157 | } |
1158 | } |
1159 | } |
1160 | |
1161 | bool is_texture_func = false; |
1162 | bool is_screen_texture = false; |
1163 | bool texture_func_no_uv = false; |
1164 | bool texture_func_returns_data = false; |
1165 | |
1166 | if (onode->op == SL::OP_STRUCT) { |
1167 | code += _mkid(vnode->name); |
1168 | } else if (onode->op == SL::OP_CONSTRUCT) { |
1169 | code += String(vnode->name); |
1170 | } else { |
1171 | if (p_actions.usage_flag_pointers.has(vnode->name) && !used_flag_pointers.has(vnode->name)) { |
1172 | *p_actions.usage_flag_pointers[vnode->name] = true; |
1173 | used_flag_pointers.insert(vnode->name); |
1174 | } |
1175 | |
1176 | if (is_internal_func) { |
1177 | code += vnode->name; |
1178 | is_texture_func = texture_functions.has(vnode->name); |
1179 | texture_func_no_uv = (vnode->name == "textureSize" || vnode->name == "textureQueryLevels" ); |
1180 | texture_func_returns_data = texture_func_no_uv || vnode->name == "textureQueryLod" ; |
1181 | } else if (p_default_actions.renames.has(vnode->name)) { |
1182 | code += p_default_actions.renames[vnode->name]; |
1183 | } else { |
1184 | code += _mkid(vnode->name); |
1185 | } |
1186 | } |
1187 | |
1188 | code += "(" ; |
1189 | |
1190 | // if color backbuffer, depth backbuffer or normal roughness texture is used, |
1191 | // we will add logic to automatically switch between |
1192 | // sampler2D and sampler2D array and vec2 UV and vec3 UV. |
1193 | bool multiview_uv_needed = false; |
1194 | |
1195 | for (int i = 1; i < onode->arguments.size(); i++) { |
1196 | if (i > 1) { |
1197 | code += ", " ; |
1198 | } |
1199 | |
1200 | bool is_out_qualifier = false; |
1201 | if (is_internal_func) { |
1202 | is_out_qualifier = SL::is_builtin_func_out_parameter(vnode->name, i - 1); |
1203 | } else if (func != nullptr) { |
1204 | const SL::ArgumentQualifier qualifier = func->arguments[i - 1].qualifier; |
1205 | is_out_qualifier = qualifier == SL::ARGUMENT_QUALIFIER_OUT || qualifier == SL::ARGUMENT_QUALIFIER_INOUT; |
1206 | } |
1207 | |
1208 | if (is_out_qualifier) { |
1209 | StringName name; |
1210 | bool found = false; |
1211 | { |
1212 | const SL::Node *node = onode->arguments[i]; |
1213 | |
1214 | bool done = false; |
1215 | do { |
1216 | switch (node->type) { |
1217 | case SL::Node::NODE_TYPE_VARIABLE: { |
1218 | name = static_cast<const SL::VariableNode *>(node)->name; |
1219 | done = true; |
1220 | found = true; |
1221 | } break; |
1222 | case SL::Node::NODE_TYPE_MEMBER: { |
1223 | node = static_cast<const SL::MemberNode *>(node)->owner; |
1224 | } break; |
1225 | default: { |
1226 | done = true; |
1227 | } break; |
1228 | } |
1229 | } while (!done); |
1230 | } |
1231 | |
1232 | if (found && p_actions.write_flag_pointers.has(name)) { |
1233 | *p_actions.write_flag_pointers[name] = true; |
1234 | } |
1235 | } |
1236 | |
1237 | String node_code = _dump_node_code(onode->arguments[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); |
1238 | if (is_texture_func && i == 1) { |
1239 | // If we're doing a texture lookup we need to check our texture argument |
1240 | StringName texture_uniform; |
1241 | bool correct_texture_uniform = false; |
1242 | |
1243 | switch (onode->arguments[i]->type) { |
1244 | case SL::Node::NODE_TYPE_VARIABLE: { |
1245 | const SL::VariableNode *varnode = static_cast<const SL::VariableNode *>(onode->arguments[i]); |
1246 | texture_uniform = varnode->name; |
1247 | correct_texture_uniform = true; |
1248 | } break; |
1249 | case SL::Node::NODE_TYPE_ARRAY: { |
1250 | const SL::ArrayNode *anode = static_cast<const SL::ArrayNode *>(onode->arguments[i]); |
1251 | texture_uniform = anode->name; |
1252 | correct_texture_uniform = true; |
1253 | } break; |
1254 | default: |
1255 | break; |
1256 | } |
1257 | |
1258 | if (correct_texture_uniform && !RS::get_singleton()->is_low_end()) { |
1259 | // Need to map from texture to sampler in order to sample when using Vulkan GLSL. |
1260 | String sampler_name; |
1261 | bool is_depth_texture = false; |
1262 | bool is_normal_roughness_texture = false; |
1263 | |
1264 | if (actions.custom_samplers.has(texture_uniform)) { |
1265 | sampler_name = actions.custom_samplers[texture_uniform]; |
1266 | } else { |
1267 | if (shader->uniforms.has(texture_uniform)) { |
1268 | const ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[texture_uniform]; |
1269 | if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SCREEN_TEXTURE) { |
1270 | is_screen_texture = true; |
1271 | } else if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_DEPTH_TEXTURE) { |
1272 | is_depth_texture = true; |
1273 | } else if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE) { |
1274 | is_normal_roughness_texture = true; |
1275 | } |
1276 | sampler_name = _get_sampler_name(u.filter, u.repeat); |
1277 | } else { |
1278 | bool found = false; |
1279 | |
1280 | for (int j = 0; j < function->arguments.size(); j++) { |
1281 | if (function->arguments[j].name == texture_uniform) { |
1282 | if (function->arguments[j].tex_builtin_check) { |
1283 | ERR_CONTINUE(!actions.custom_samplers.has(function->arguments[j].tex_builtin)); |
1284 | sampler_name = actions.custom_samplers[function->arguments[j].tex_builtin]; |
1285 | found = true; |
1286 | break; |
1287 | } |
1288 | if (function->arguments[j].tex_argument_check) { |
1289 | sampler_name = _get_sampler_name(function->arguments[j].tex_argument_filter, function->arguments[j].tex_argument_repeat); |
1290 | found = true; |
1291 | break; |
1292 | } |
1293 | } |
1294 | } |
1295 | if (!found) { |
1296 | //function was most likely unused, so use anything (compiler will remove it anyway) |
1297 | sampler_name = _get_sampler_name(ShaderLanguage::FILTER_DEFAULT, ShaderLanguage::REPEAT_DEFAULT); |
1298 | } |
1299 | } |
1300 | } |
1301 | |
1302 | String data_type_name = "" ; |
1303 | if (actions.check_multiview_samplers && (is_screen_texture || is_depth_texture || is_normal_roughness_texture)) { |
1304 | data_type_name = "multiviewSampler" ; |
1305 | multiview_uv_needed = true; |
1306 | } else { |
1307 | data_type_name = ShaderLanguage::get_datatype_name(onode->arguments[i]->get_datatype()); |
1308 | } |
1309 | |
1310 | code += data_type_name + "(" + node_code + ", " + sampler_name + ")" ; |
1311 | } else if (actions.check_multiview_samplers && correct_texture_uniform && RS::get_singleton()->is_low_end()) { |
1312 | // Texture function on low end hardware (i.e. OpenGL). |
1313 | // We just need to know if the texture supports multiview. |
1314 | |
1315 | if (shader->uniforms.has(texture_uniform)) { |
1316 | const ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[texture_uniform]; |
1317 | if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SCREEN_TEXTURE) { |
1318 | multiview_uv_needed = true; |
1319 | } else if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_DEPTH_TEXTURE) { |
1320 | multiview_uv_needed = true; |
1321 | } else if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE) { |
1322 | multiview_uv_needed = true; |
1323 | } |
1324 | } |
1325 | |
1326 | code += node_code; |
1327 | } else { |
1328 | code += node_code; |
1329 | } |
1330 | } else if (multiview_uv_needed && !texture_func_no_uv && i == 2) { |
1331 | // UV coordinate after using color, depth or normal roughness texture. |
1332 | node_code = "multiview_uv(" + node_code + ".xy)" ; |
1333 | |
1334 | code += node_code; |
1335 | } else { |
1336 | code += node_code; |
1337 | } |
1338 | } |
1339 | code += ")" ; |
1340 | if (is_screen_texture && !texture_func_returns_data && actions.apply_luminance_multiplier) { |
1341 | code = "(" + code + " * vec4(vec3(sc_luminance_multiplier), 1.0))" ; |
1342 | } |
1343 | } break; |
1344 | case SL::OP_INDEX: { |
1345 | code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); |
1346 | code += "[" ; |
1347 | code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); |
1348 | code += "]" ; |
1349 | |
1350 | } break; |
1351 | case SL::OP_SELECT_IF: { |
1352 | code += "(" ; |
1353 | code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); |
1354 | code += "?" ; |
1355 | code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); |
1356 | code += ":" ; |
1357 | code += _dump_node_code(onode->arguments[2], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); |
1358 | code += ")" ; |
1359 | |
1360 | } break; |
1361 | case SL::OP_EMPTY: { |
1362 | // Semicolon (or empty statement) - ignored. |
1363 | } break; |
1364 | |
1365 | default: { |
1366 | if (p_use_scope) { |
1367 | code += "(" ; |
1368 | } |
1369 | code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + " " + _opstr(onode->op) + " " + _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); |
1370 | if (p_use_scope) { |
1371 | code += ")" ; |
1372 | } |
1373 | break; |
1374 | } |
1375 | } |
1376 | |
1377 | } break; |
1378 | case SL::Node::NODE_TYPE_CONTROL_FLOW: { |
1379 | SL::ControlFlowNode *cfnode = (SL::ControlFlowNode *)p_node; |
1380 | if (cfnode->flow_op == SL::FLOW_OP_IF) { |
1381 | code += _mktab(p_level) + "if (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n" ; |
1382 | code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning); |
1383 | if (cfnode->blocks.size() == 2) { |
1384 | code += _mktab(p_level) + "else\n" ; |
1385 | code += _dump_node_code(cfnode->blocks[1], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning); |
1386 | } |
1387 | } else if (cfnode->flow_op == SL::FLOW_OP_SWITCH) { |
1388 | code += _mktab(p_level) + "switch (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n" ; |
1389 | code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning); |
1390 | } else if (cfnode->flow_op == SL::FLOW_OP_CASE) { |
1391 | code += _mktab(p_level) + "case " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ":\n" ; |
1392 | code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning); |
1393 | } else if (cfnode->flow_op == SL::FLOW_OP_DEFAULT) { |
1394 | code += _mktab(p_level) + "default:\n" ; |
1395 | code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning); |
1396 | } else if (cfnode->flow_op == SL::FLOW_OP_DO) { |
1397 | code += _mktab(p_level) + "do" ; |
1398 | code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning); |
1399 | code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ");" ; |
1400 | } else if (cfnode->flow_op == SL::FLOW_OP_WHILE) { |
1401 | code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n" ; |
1402 | code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning); |
1403 | } else if (cfnode->flow_op == SL::FLOW_OP_FOR) { |
1404 | String left = _dump_node_code(cfnode->blocks[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); |
1405 | String middle = _dump_node_code(cfnode->blocks[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); |
1406 | String right = _dump_node_code(cfnode->blocks[2], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); |
1407 | code += _mktab(p_level) + "for (" + left + ";" + middle + ";" + right + ")\n" ; |
1408 | code += _dump_node_code(cfnode->blocks[3], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning); |
1409 | |
1410 | } else if (cfnode->flow_op == SL::FLOW_OP_RETURN) { |
1411 | if (cfnode->expressions.size()) { |
1412 | code = "return " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ";" ; |
1413 | } else { |
1414 | code = "return;" ; |
1415 | } |
1416 | } else if (cfnode->flow_op == SL::FLOW_OP_DISCARD) { |
1417 | if (p_actions.usage_flag_pointers.has("DISCARD" ) && !used_flag_pointers.has("DISCARD" )) { |
1418 | *p_actions.usage_flag_pointers["DISCARD" ] = true; |
1419 | used_flag_pointers.insert("DISCARD" ); |
1420 | } |
1421 | |
1422 | code = "discard;" ; |
1423 | } else if (cfnode->flow_op == SL::FLOW_OP_CONTINUE) { |
1424 | code = "continue;" ; |
1425 | } else if (cfnode->flow_op == SL::FLOW_OP_BREAK) { |
1426 | code = "break;" ; |
1427 | } |
1428 | |
1429 | } break; |
1430 | case SL::Node::NODE_TYPE_MEMBER: { |
1431 | SL::MemberNode *mnode = (SL::MemberNode *)p_node; |
1432 | code = _dump_node_code(mnode->owner, p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + "." + mnode->name; |
1433 | if (mnode->index_expression != nullptr) { |
1434 | code += "[" ; |
1435 | code += _dump_node_code(mnode->index_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning); |
1436 | code += "]" ; |
1437 | } else if (mnode->assign_expression != nullptr) { |
1438 | code += "=" ; |
1439 | code += _dump_node_code(mnode->assign_expression, p_level, r_gen_code, p_actions, p_default_actions, true, false); |
1440 | } else if (mnode->call_expression != nullptr) { |
1441 | code += "." ; |
1442 | code += _dump_node_code(mnode->call_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning, false); |
1443 | } |
1444 | } break; |
1445 | } |
1446 | |
1447 | return code; |
1448 | } |
1449 | |
1450 | ShaderLanguage::DataType ShaderCompiler::_get_global_shader_uniform_type(const StringName &p_name) { |
1451 | RS::GlobalShaderParameterType gvt = RSG::material_storage->global_shader_parameter_get_type(p_name); |
1452 | return (ShaderLanguage::DataType)RS::global_shader_uniform_type_get_shader_datatype(gvt); |
1453 | } |
1454 | |
1455 | Error ShaderCompiler::compile(RS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code) { |
1456 | SL::ShaderCompileInfo info; |
1457 | info.functions = ShaderTypes::get_singleton()->get_functions(p_mode); |
1458 | info.render_modes = ShaderTypes::get_singleton()->get_modes(p_mode); |
1459 | info.shader_types = ShaderTypes::get_singleton()->get_types(); |
1460 | info.global_shader_uniform_type_func = _get_global_shader_uniform_type; |
1461 | |
1462 | Error err = parser.compile(p_code, info); |
1463 | |
1464 | if (err != OK) { |
1465 | Vector<ShaderLanguage::FilePosition> include_positions = parser.get_include_positions(); |
1466 | |
1467 | String current; |
1468 | HashMap<String, Vector<String>> includes; |
1469 | includes["" ] = Vector<String>(); |
1470 | Vector<String> include_stack; |
1471 | Vector<String> shader_lines = p_code.split("\n" ); |
1472 | |
1473 | // Reconstruct the files. |
1474 | for (int i = 0; i < shader_lines.size(); i++) { |
1475 | String l = shader_lines[i]; |
1476 | if (l.begins_with("@@>" )) { |
1477 | String inc_path = l.replace_first("@@>" , "" ); |
1478 | |
1479 | l = "#include \"" + inc_path + "\"" ; |
1480 | includes[current].append("#include \"" + inc_path + "\"" ); // Restore the include directive |
1481 | include_stack.push_back(current); |
1482 | current = inc_path; |
1483 | includes[inc_path] = Vector<String>(); |
1484 | |
1485 | } else if (l.begins_with("@@<" )) { |
1486 | if (include_stack.size()) { |
1487 | current = include_stack[include_stack.size() - 1]; |
1488 | include_stack.resize(include_stack.size() - 1); |
1489 | } |
1490 | } else { |
1491 | includes[current].push_back(l); |
1492 | } |
1493 | } |
1494 | |
1495 | // Print the files. |
1496 | for (const KeyValue<String, Vector<String>> &E : includes) { |
1497 | if (E.key.is_empty()) { |
1498 | if (p_path == "" ) { |
1499 | print_line("--Main Shader--" ); |
1500 | } else { |
1501 | print_line("--" + p_path + "--" ); |
1502 | } |
1503 | } else { |
1504 | print_line("--" + E.key + "--" ); |
1505 | } |
1506 | int err_line = -1; |
1507 | for (int i = 0; i < include_positions.size(); i++) { |
1508 | if (include_positions[i].file == E.key) { |
1509 | err_line = include_positions[i].line; |
1510 | } |
1511 | } |
1512 | const Vector<String> &V = E.value; |
1513 | for (int i = 0; i < V.size(); i++) { |
1514 | if (i == err_line - 1) { |
1515 | // Mark the error line to be visible without having to look at |
1516 | // the trace at the end. |
1517 | print_line(vformat("E%4d-> %s" , i + 1, V[i])); |
1518 | } else { |
1519 | print_line(vformat("%5d | %s" , i + 1, V[i])); |
1520 | } |
1521 | } |
1522 | } |
1523 | |
1524 | String file; |
1525 | int line; |
1526 | if (include_positions.size() > 1) { |
1527 | file = include_positions[include_positions.size() - 1].file; |
1528 | line = include_positions[include_positions.size() - 1].line; |
1529 | } else { |
1530 | file = p_path; |
1531 | line = parser.get_error_line(); |
1532 | } |
1533 | |
1534 | _err_print_error(nullptr, file.utf8().get_data(), line, parser.get_error_text().utf8().get_data(), false, ERR_HANDLER_SHADER); |
1535 | return err; |
1536 | } |
1537 | |
1538 | r_gen_code.defines.clear(); |
1539 | r_gen_code.code.clear(); |
1540 | for (int i = 0; i < STAGE_MAX; i++) { |
1541 | r_gen_code.stage_globals[i] = String(); |
1542 | } |
1543 | r_gen_code.uses_fragment_time = false; |
1544 | r_gen_code.uses_vertex_time = false; |
1545 | r_gen_code.uses_global_textures = false; |
1546 | r_gen_code.uses_screen_texture_mipmaps = false; |
1547 | r_gen_code.uses_screen_texture = false; |
1548 | r_gen_code.uses_depth_texture = false; |
1549 | r_gen_code.uses_normal_roughness_texture = false; |
1550 | |
1551 | used_name_defines.clear(); |
1552 | used_rmode_defines.clear(); |
1553 | used_flag_pointers.clear(); |
1554 | fragment_varyings.clear(); |
1555 | |
1556 | shader = parser.get_shader(); |
1557 | function = nullptr; |
1558 | _dump_node_code(shader, 1, r_gen_code, *p_actions, actions, false); |
1559 | |
1560 | return OK; |
1561 | } |
1562 | |
1563 | void ShaderCompiler::initialize(DefaultIdentifierActions p_actions) { |
1564 | actions = p_actions; |
1565 | |
1566 | time_name = "TIME" ; |
1567 | |
1568 | List<String> func_list; |
1569 | |
1570 | ShaderLanguage::get_builtin_funcs(&func_list); |
1571 | |
1572 | for (const String &E : func_list) { |
1573 | internal_functions.insert(E); |
1574 | } |
1575 | texture_functions.insert("texture" ); |
1576 | texture_functions.insert("textureProj" ); |
1577 | texture_functions.insert("textureLod" ); |
1578 | texture_functions.insert("textureProjLod" ); |
1579 | texture_functions.insert("textureGrad" ); |
1580 | texture_functions.insert("textureProjGrad" ); |
1581 | texture_functions.insert("textureGather" ); |
1582 | texture_functions.insert("textureSize" ); |
1583 | texture_functions.insert("textureQueryLod" ); |
1584 | texture_functions.insert("textureQueryLevels" ); |
1585 | texture_functions.insert("texelFetch" ); |
1586 | } |
1587 | |
1588 | ShaderCompiler::ShaderCompiler() { |
1589 | } |
1590 | |