1/**************************************************************************/
2/* shader_language.h */
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#ifndef SHADER_LANGUAGE_H
32#define SHADER_LANGUAGE_H
33
34#include "core/object/script_language.h"
35#include "core/string/string_name.h"
36#include "core/string/ustring.h"
37#include "core/templates/list.h"
38#include "core/templates/rb_map.h"
39#include "core/typedefs.h"
40#include "core/variant/variant.h"
41#include "scene/resources/shader_include.h"
42
43#ifdef DEBUG_ENABLED
44#include "shader_warnings.h"
45#endif // DEBUG_ENABLED
46
47class ShaderLanguage {
48public:
49 struct TkPos {
50 int char_idx;
51 int tk_line;
52 };
53
54 enum TokenType {
55 TK_EMPTY,
56 TK_IDENTIFIER,
57 TK_TRUE,
58 TK_FALSE,
59 TK_FLOAT_CONSTANT,
60 TK_INT_CONSTANT,
61 TK_UINT_CONSTANT,
62 TK_TYPE_VOID,
63 TK_TYPE_BOOL,
64 TK_TYPE_BVEC2,
65 TK_TYPE_BVEC3,
66 TK_TYPE_BVEC4,
67 TK_TYPE_INT,
68 TK_TYPE_IVEC2,
69 TK_TYPE_IVEC3,
70 TK_TYPE_IVEC4,
71 TK_TYPE_UINT,
72 TK_TYPE_UVEC2,
73 TK_TYPE_UVEC3,
74 TK_TYPE_UVEC4,
75 TK_TYPE_FLOAT,
76 TK_TYPE_VEC2,
77 TK_TYPE_VEC3,
78 TK_TYPE_VEC4,
79 TK_TYPE_MAT2,
80 TK_TYPE_MAT3,
81 TK_TYPE_MAT4,
82 TK_TYPE_SAMPLER2D,
83 TK_TYPE_ISAMPLER2D,
84 TK_TYPE_USAMPLER2D,
85 TK_TYPE_SAMPLER2DARRAY,
86 TK_TYPE_ISAMPLER2DARRAY,
87 TK_TYPE_USAMPLER2DARRAY,
88 TK_TYPE_SAMPLER3D,
89 TK_TYPE_ISAMPLER3D,
90 TK_TYPE_USAMPLER3D,
91 TK_TYPE_SAMPLERCUBE,
92 TK_TYPE_SAMPLERCUBEARRAY,
93 TK_INTERPOLATION_FLAT,
94 TK_INTERPOLATION_SMOOTH,
95 TK_CONST,
96 TK_STRUCT,
97 TK_PRECISION_LOW,
98 TK_PRECISION_MID,
99 TK_PRECISION_HIGH,
100 TK_OP_EQUAL,
101 TK_OP_NOT_EQUAL,
102 TK_OP_LESS,
103 TK_OP_LESS_EQUAL,
104 TK_OP_GREATER,
105 TK_OP_GREATER_EQUAL,
106 TK_OP_AND,
107 TK_OP_OR,
108 TK_OP_NOT,
109 TK_OP_ADD,
110 TK_OP_SUB,
111 TK_OP_MUL,
112 TK_OP_DIV,
113 TK_OP_MOD,
114 TK_OP_SHIFT_LEFT,
115 TK_OP_SHIFT_RIGHT,
116 TK_OP_ASSIGN,
117 TK_OP_ASSIGN_ADD,
118 TK_OP_ASSIGN_SUB,
119 TK_OP_ASSIGN_MUL,
120 TK_OP_ASSIGN_DIV,
121 TK_OP_ASSIGN_MOD,
122 TK_OP_ASSIGN_SHIFT_LEFT,
123 TK_OP_ASSIGN_SHIFT_RIGHT,
124 TK_OP_ASSIGN_BIT_AND,
125 TK_OP_ASSIGN_BIT_OR,
126 TK_OP_ASSIGN_BIT_XOR,
127 TK_OP_BIT_AND,
128 TK_OP_BIT_OR,
129 TK_OP_BIT_XOR,
130 TK_OP_BIT_INVERT,
131 TK_OP_INCREMENT,
132 TK_OP_DECREMENT,
133 TK_CF_IF,
134 TK_CF_ELSE,
135 TK_CF_FOR,
136 TK_CF_WHILE,
137 TK_CF_DO,
138 TK_CF_SWITCH,
139 TK_CF_CASE,
140 TK_CF_DEFAULT,
141 TK_CF_BREAK,
142 TK_CF_CONTINUE,
143 TK_CF_RETURN,
144 TK_CF_DISCARD,
145 TK_BRACKET_OPEN,
146 TK_BRACKET_CLOSE,
147 TK_CURLY_BRACKET_OPEN,
148 TK_CURLY_BRACKET_CLOSE,
149 TK_PARENTHESIS_OPEN,
150 TK_PARENTHESIS_CLOSE,
151 TK_QUESTION,
152 TK_COMMA,
153 TK_COLON,
154 TK_SEMICOLON,
155 TK_PERIOD,
156 TK_UNIFORM,
157 TK_UNIFORM_GROUP,
158 TK_INSTANCE,
159 TK_GLOBAL,
160 TK_VARYING,
161 TK_ARG_IN,
162 TK_ARG_OUT,
163 TK_ARG_INOUT,
164 TK_RENDER_MODE,
165 TK_HINT_DEFAULT_WHITE_TEXTURE,
166 TK_HINT_DEFAULT_BLACK_TEXTURE,
167 TK_HINT_DEFAULT_TRANSPARENT_TEXTURE,
168 TK_HINT_NORMAL_TEXTURE,
169 TK_HINT_ROUGHNESS_NORMAL_TEXTURE,
170 TK_HINT_ROUGHNESS_R,
171 TK_HINT_ROUGHNESS_G,
172 TK_HINT_ROUGHNESS_B,
173 TK_HINT_ROUGHNESS_A,
174 TK_HINT_ROUGHNESS_GRAY,
175 TK_HINT_ANISOTROPY_TEXTURE,
176 TK_HINT_SOURCE_COLOR,
177 TK_HINT_RANGE,
178 TK_HINT_INSTANCE_INDEX,
179 TK_HINT_SCREEN_TEXTURE,
180 TK_HINT_NORMAL_ROUGHNESS_TEXTURE,
181 TK_HINT_DEPTH_TEXTURE,
182 TK_FILTER_NEAREST,
183 TK_FILTER_LINEAR,
184 TK_FILTER_NEAREST_MIPMAP,
185 TK_FILTER_LINEAR_MIPMAP,
186 TK_FILTER_NEAREST_MIPMAP_ANISOTROPIC,
187 TK_FILTER_LINEAR_MIPMAP_ANISOTROPIC,
188 TK_REPEAT_ENABLE,
189 TK_REPEAT_DISABLE,
190 TK_SHADER_TYPE,
191 TK_CURSOR,
192 TK_ERROR,
193 TK_EOF,
194 TK_MAX
195 };
196
197/* COMPILER */
198
199// lame work around to Apple defining this as a macro in 10.12 SDK
200#ifdef TYPE_BOOL
201#undef TYPE_BOOL
202#endif
203
204 enum DataType {
205 TYPE_VOID,
206 TYPE_BOOL,
207 TYPE_BVEC2,
208 TYPE_BVEC3,
209 TYPE_BVEC4,
210 TYPE_INT,
211 TYPE_IVEC2,
212 TYPE_IVEC3,
213 TYPE_IVEC4,
214 TYPE_UINT,
215 TYPE_UVEC2,
216 TYPE_UVEC3,
217 TYPE_UVEC4,
218 TYPE_FLOAT,
219 TYPE_VEC2,
220 TYPE_VEC3,
221 TYPE_VEC4,
222 TYPE_MAT2,
223 TYPE_MAT3,
224 TYPE_MAT4,
225 TYPE_SAMPLER2D,
226 TYPE_ISAMPLER2D,
227 TYPE_USAMPLER2D,
228 TYPE_SAMPLER2DARRAY,
229 TYPE_ISAMPLER2DARRAY,
230 TYPE_USAMPLER2DARRAY,
231 TYPE_SAMPLER3D,
232 TYPE_ISAMPLER3D,
233 TYPE_USAMPLER3D,
234 TYPE_SAMPLERCUBE,
235 TYPE_SAMPLERCUBEARRAY,
236 TYPE_STRUCT,
237 TYPE_MAX
238 };
239
240 enum DataPrecision {
241 PRECISION_LOWP,
242 PRECISION_MEDIUMP,
243 PRECISION_HIGHP,
244 PRECISION_DEFAULT,
245 };
246
247 enum DataInterpolation {
248 INTERPOLATION_FLAT,
249 INTERPOLATION_SMOOTH,
250 INTERPOLATION_DEFAULT,
251 };
252
253 enum Operator {
254 OP_EQUAL,
255 OP_NOT_EQUAL,
256 OP_LESS,
257 OP_LESS_EQUAL,
258 OP_GREATER,
259 OP_GREATER_EQUAL,
260 OP_AND,
261 OP_OR,
262 OP_NOT,
263 OP_NEGATE,
264 OP_ADD,
265 OP_SUB,
266 OP_MUL,
267 OP_DIV,
268 OP_MOD,
269 OP_SHIFT_LEFT,
270 OP_SHIFT_RIGHT,
271 OP_ASSIGN,
272 OP_ASSIGN_ADD,
273 OP_ASSIGN_SUB,
274 OP_ASSIGN_MUL,
275 OP_ASSIGN_DIV,
276 OP_ASSIGN_MOD,
277 OP_ASSIGN_SHIFT_LEFT,
278 OP_ASSIGN_SHIFT_RIGHT,
279 OP_ASSIGN_BIT_AND,
280 OP_ASSIGN_BIT_OR,
281 OP_ASSIGN_BIT_XOR,
282 OP_BIT_AND,
283 OP_BIT_OR,
284 OP_BIT_XOR,
285 OP_BIT_INVERT,
286 OP_INCREMENT,
287 OP_DECREMENT,
288 OP_SELECT_IF,
289 OP_SELECT_ELSE, //used only internally, then only IF appears with 3 arguments
290 OP_POST_INCREMENT,
291 OP_POST_DECREMENT,
292 OP_CALL,
293 OP_CONSTRUCT,
294 OP_STRUCT,
295 OP_INDEX,
296 OP_EMPTY,
297 OP_MAX
298 };
299
300 enum FlowOperation {
301 FLOW_OP_IF,
302 FLOW_OP_RETURN,
303 FLOW_OP_FOR,
304 FLOW_OP_WHILE,
305 FLOW_OP_DO,
306 FLOW_OP_BREAK,
307 FLOW_OP_SWITCH,
308 FLOW_OP_CASE,
309 FLOW_OP_DEFAULT,
310 FLOW_OP_CONTINUE,
311 FLOW_OP_DISCARD
312 };
313
314 enum ArgumentQualifier {
315 ARGUMENT_QUALIFIER_IN,
316 ARGUMENT_QUALIFIER_OUT,
317 ARGUMENT_QUALIFIER_INOUT,
318 };
319
320 enum SubClassTag {
321 TAG_GLOBAL,
322 TAG_ARRAY,
323 };
324
325 enum TextureFilter {
326 FILTER_NEAREST,
327 FILTER_LINEAR,
328 FILTER_NEAREST_MIPMAP,
329 FILTER_LINEAR_MIPMAP,
330 FILTER_NEAREST_MIPMAP_ANISOTROPIC,
331 FILTER_LINEAR_MIPMAP_ANISOTROPIC,
332 FILTER_DEFAULT,
333 };
334
335 enum TextureRepeat {
336 REPEAT_DISABLE,
337 REPEAT_ENABLE,
338 REPEAT_DEFAULT,
339 };
340
341 enum {
342 MAX_INSTANCE_UNIFORM_INDICES = 16
343 };
344
345 struct VaryingFunctionNames {
346 StringName fragment;
347 StringName vertex;
348 StringName light;
349 VaryingFunctionNames() {
350 fragment = "fragment";
351 vertex = "vertex";
352 light = "light";
353 }
354 };
355
356 struct Node {
357 Node *next = nullptr;
358
359 enum Type {
360 NODE_TYPE_SHADER,
361 NODE_TYPE_FUNCTION,
362 NODE_TYPE_BLOCK,
363 NODE_TYPE_VARIABLE,
364 NODE_TYPE_VARIABLE_DECLARATION,
365 NODE_TYPE_CONSTANT,
366 NODE_TYPE_OPERATOR,
367 NODE_TYPE_CONTROL_FLOW,
368 NODE_TYPE_MEMBER,
369 NODE_TYPE_ARRAY,
370 NODE_TYPE_ARRAY_CONSTRUCT,
371 NODE_TYPE_STRUCT,
372 };
373
374 Type type;
375
376 virtual DataType get_datatype() const { return TYPE_VOID; }
377 virtual String get_datatype_name() const { return ""; }
378 virtual int get_array_size() const { return 0; }
379 virtual bool is_indexed() const { return false; }
380
381 Node(Type t) :
382 type(t) {}
383 virtual ~Node() {}
384 };
385
386 template <class T>
387 T *alloc_node() {
388 T *node = memnew(T);
389 node->next = nodes;
390 nodes = node;
391 return node;
392 }
393
394 Node *nodes = nullptr;
395
396 struct OperatorNode : public Node {
397 DataType return_cache = TYPE_VOID;
398 DataPrecision return_precision_cache = PRECISION_DEFAULT;
399 int return_array_size = 0;
400 Operator op = OP_EQUAL;
401 StringName struct_name;
402 Vector<Node *> arguments;
403
404 virtual DataType get_datatype() const override { return return_cache; }
405 virtual String get_datatype_name() const override { return String(struct_name); }
406 virtual int get_array_size() const override { return return_array_size; }
407 virtual bool is_indexed() const override { return op == OP_INDEX; }
408
409 OperatorNode() :
410 Node(NODE_TYPE_OPERATOR) {}
411 };
412
413 struct VariableNode : public Node {
414 DataType datatype_cache = TYPE_VOID;
415 StringName name;
416 StringName struct_name;
417 bool is_const = false;
418 bool is_local = false;
419
420 virtual DataType get_datatype() const override { return datatype_cache; }
421 virtual String get_datatype_name() const override { return String(struct_name); }
422
423 VariableNode() :
424 Node(NODE_TYPE_VARIABLE) {}
425 };
426
427 struct VariableDeclarationNode : public Node {
428 DataPrecision precision = PRECISION_DEFAULT;
429 DataType datatype = TYPE_VOID;
430 String struct_name;
431 bool is_const = false;
432
433 struct Declaration {
434 StringName name;
435 uint32_t size = 0U;
436 Node *size_expression = nullptr;
437 Vector<Node *> initializer;
438 bool single_expression = false;
439 };
440 Vector<Declaration> declarations;
441
442 virtual DataType get_datatype() const override { return datatype; }
443
444 VariableDeclarationNode() :
445 Node(NODE_TYPE_VARIABLE_DECLARATION) {}
446 };
447
448 struct ArrayNode : public Node {
449 DataType datatype_cache = TYPE_VOID;
450 StringName struct_name;
451 StringName name;
452 Node *index_expression = nullptr;
453 Node *call_expression = nullptr;
454 Node *assign_expression = nullptr;
455 bool is_const = false;
456 int array_size = 0;
457 bool is_local = false;
458
459 virtual DataType get_datatype() const override { return call_expression ? call_expression->get_datatype() : datatype_cache; }
460 virtual String get_datatype_name() const override { return call_expression ? call_expression->get_datatype_name() : String(struct_name); }
461 virtual int get_array_size() const override { return (index_expression || call_expression) ? 0 : array_size; }
462 virtual bool is_indexed() const override { return index_expression != nullptr; }
463
464 ArrayNode() :
465 Node(NODE_TYPE_ARRAY) {}
466 };
467
468 struct ArrayConstructNode : public Node {
469 DataType datatype = TYPE_VOID;
470 String struct_name;
471 Vector<Node *> initializer;
472
473 virtual DataType get_datatype() const override { return datatype; }
474 virtual String get_datatype_name() const override { return struct_name; }
475 virtual int get_array_size() const override { return initializer.size(); }
476
477 ArrayConstructNode() :
478 Node(NODE_TYPE_ARRAY_CONSTRUCT) {}
479 };
480
481 struct ConstantNode : public Node {
482 DataType datatype = TYPE_VOID;
483 String struct_name = "";
484 int array_size = 0;
485
486 union Value {
487 bool boolean = false;
488 float real;
489 int32_t sint;
490 uint32_t uint;
491 };
492
493 Vector<Value> values;
494 Vector<VariableDeclarationNode::Declaration> array_declarations;
495
496 virtual DataType get_datatype() const override { return datatype; }
497 virtual String get_datatype_name() const override { return struct_name; }
498 virtual int get_array_size() const override { return array_size; }
499
500 ConstantNode() :
501 Node(NODE_TYPE_CONSTANT) {}
502 };
503
504 struct FunctionNode;
505
506 struct BlockNode : public Node {
507 FunctionNode *parent_function = nullptr;
508 BlockNode *parent_block = nullptr;
509
510 enum BlockType {
511 BLOCK_TYPE_STANDARD,
512 BLOCK_TYPE_FOR_INIT,
513 BLOCK_TYPE_FOR_CONDITION,
514 BLOCK_TYPE_FOR_EXPRESSION,
515 BLOCK_TYPE_SWITCH,
516 BLOCK_TYPE_CASE,
517 BLOCK_TYPE_DEFAULT,
518 };
519
520 int block_type = BLOCK_TYPE_STANDARD;
521 SubClassTag block_tag = SubClassTag::TAG_GLOBAL;
522
523 struct Variable {
524 DataType type;
525 StringName struct_name;
526 DataPrecision precision;
527 int line; //for completion
528 int array_size;
529 bool is_const;
530 ConstantNode::Value value;
531 };
532
533 HashMap<StringName, Variable> variables;
534 List<Node *> statements;
535 bool single_statement = false;
536 bool use_comma_between_statements = false;
537
538 BlockNode() :
539 Node(NODE_TYPE_BLOCK) {}
540 };
541
542 struct ControlFlowNode : public Node {
543 FlowOperation flow_op = FLOW_OP_IF;
544 Vector<Node *> expressions;
545 Vector<BlockNode *> blocks;
546
547 ControlFlowNode() :
548 Node(NODE_TYPE_CONTROL_FLOW) {}
549 };
550
551 struct MemberNode : public Node {
552 DataType basetype = TYPE_VOID;
553 bool basetype_const = false;
554 StringName base_struct_name;
555 DataPrecision precision = PRECISION_DEFAULT;
556 DataType datatype = TYPE_VOID;
557 int array_size = 0;
558 StringName struct_name;
559 StringName name;
560 Node *owner = nullptr;
561 Node *index_expression = nullptr;
562 Node *assign_expression = nullptr;
563 Node *call_expression = nullptr;
564 bool has_swizzling_duplicates = false;
565
566 virtual DataType get_datatype() const override { return call_expression ? call_expression->get_datatype() : datatype; }
567 virtual String get_datatype_name() const override { return call_expression ? call_expression->get_datatype_name() : String(struct_name); }
568 virtual int get_array_size() const override { return (index_expression || call_expression) ? 0 : array_size; }
569 virtual bool is_indexed() const override { return index_expression != nullptr || call_expression != nullptr; }
570
571 MemberNode() :
572 Node(NODE_TYPE_MEMBER) {}
573 };
574
575 struct StructNode : public Node {
576 List<MemberNode *> members;
577 StructNode() :
578 Node(NODE_TYPE_STRUCT) {}
579 };
580
581 struct FunctionNode : public Node {
582 struct Argument {
583 ArgumentQualifier qualifier;
584 StringName name;
585 DataType type;
586 StringName type_str;
587 DataPrecision precision;
588 //for passing textures as arguments
589 bool tex_argument_check;
590 TextureFilter tex_argument_filter;
591 TextureRepeat tex_argument_repeat;
592 bool tex_builtin_check;
593 StringName tex_builtin;
594 bool is_const;
595 int array_size;
596
597 HashMap<StringName, HashSet<int>> tex_argument_connect;
598 };
599
600 StringName name;
601 DataType return_type = TYPE_VOID;
602 StringName return_struct_name;
603 DataPrecision return_precision = PRECISION_DEFAULT;
604 int return_array_size = 0;
605 Vector<Argument> arguments;
606 BlockNode *body = nullptr;
607 bool can_discard = false;
608
609 virtual DataType get_datatype() const override { return return_type; }
610 virtual String get_datatype_name() const override { return String(return_struct_name); }
611 virtual int get_array_size() const override { return return_array_size; }
612
613 FunctionNode() :
614 Node(NODE_TYPE_FUNCTION) {}
615 };
616
617 struct ShaderNode : public Node {
618 struct Constant {
619 StringName name;
620 DataType type;
621 StringName type_str;
622 DataPrecision precision;
623 ConstantNode *initializer = nullptr;
624 int array_size;
625 };
626
627 struct Function {
628 StringName name;
629 FunctionNode *function = nullptr;
630 HashSet<StringName> uses_function;
631 bool callable;
632 };
633
634 struct Struct {
635 StringName name;
636 StructNode *shader_struct = nullptr;
637 };
638
639 struct Varying {
640 enum Stage {
641 STAGE_UNKNOWN,
642 STAGE_VERTEX, // transition stage to STAGE_VERTEX_TO_FRAGMENT_LIGHT, emits warning if it's not used
643 STAGE_FRAGMENT, // transition stage to STAGE_FRAGMENT_TO_LIGHT, emits warning if it's not used
644 STAGE_VERTEX_TO_FRAGMENT_LIGHT,
645 STAGE_FRAGMENT_TO_LIGHT,
646 };
647
648 Stage stage = STAGE_UNKNOWN;
649 DataType type = TYPE_VOID;
650 DataInterpolation interpolation = INTERPOLATION_FLAT;
651 DataPrecision precision = PRECISION_DEFAULT;
652 int array_size = 0;
653 TkPos tkpos;
654
655 Varying() {}
656 };
657
658 struct Uniform {
659 enum Hint {
660 HINT_NONE,
661 HINT_RANGE,
662 HINT_SOURCE_COLOR,
663 HINT_NORMAL,
664 HINT_ROUGHNESS_NORMAL,
665 HINT_ROUGHNESS_R,
666 HINT_ROUGHNESS_G,
667 HINT_ROUGHNESS_B,
668 HINT_ROUGHNESS_A,
669 HINT_ROUGHNESS_GRAY,
670 HINT_DEFAULT_BLACK,
671 HINT_DEFAULT_WHITE,
672 HINT_DEFAULT_TRANSPARENT,
673 HINT_ANISOTROPY,
674 HINT_SCREEN_TEXTURE,
675 HINT_NORMAL_ROUGHNESS_TEXTURE,
676 HINT_DEPTH_TEXTURE,
677 HINT_MAX
678 };
679
680 enum Scope {
681 SCOPE_LOCAL,
682 SCOPE_INSTANCE,
683 SCOPE_GLOBAL,
684 };
685
686 int order = 0;
687 int texture_order = 0;
688 int texture_binding = 0;
689 DataType type = TYPE_VOID;
690 DataPrecision precision = PRECISION_DEFAULT;
691 int array_size = 0;
692 Vector<ConstantNode::Value> default_value;
693 Scope scope = SCOPE_LOCAL;
694 Hint hint = HINT_NONE;
695 bool use_color = false;
696 TextureFilter filter = FILTER_DEFAULT;
697 TextureRepeat repeat = REPEAT_DEFAULT;
698 float hint_range[3];
699 int instance_index = 0;
700 String group;
701 String subgroup;
702
703 Uniform() {
704 hint_range[0] = 0.0f;
705 hint_range[1] = 1.0f;
706 hint_range[2] = 0.001f;
707 }
708 };
709
710 HashMap<StringName, Constant> constants;
711 HashMap<StringName, Varying> varyings;
712 HashMap<StringName, Uniform> uniforms;
713 HashMap<StringName, Struct> structs;
714 Vector<StringName> render_modes;
715
716 Vector<Function> functions;
717 Vector<Constant> vconstants;
718 Vector<Struct> vstructs;
719
720 ShaderNode() :
721 Node(NODE_TYPE_SHADER) {}
722 };
723
724 struct UniformOrderComparator {
725 _FORCE_INLINE_ bool operator()(const Pair<StringName, int> &A, const Pair<StringName, int> &B) const {
726 return A.second < B.second;
727 }
728 };
729
730 struct Expression {
731 bool is_op;
732 union {
733 Operator op;
734 Node *node = nullptr;
735 };
736 };
737
738 struct VarInfo {
739 StringName name;
740 DataType type;
741 };
742
743 enum CompletionType {
744 COMPLETION_NONE,
745 COMPLETION_SHADER_TYPE,
746 COMPLETION_RENDER_MODE,
747 COMPLETION_MAIN_FUNCTION,
748 COMPLETION_IDENTIFIER,
749 COMPLETION_FUNCTION_CALL,
750 COMPLETION_CALL_ARGUMENTS,
751 COMPLETION_INDEX,
752 COMPLETION_STRUCT,
753 COMPLETION_HINT,
754 };
755
756 struct Token {
757 TokenType type;
758 StringName text;
759 double constant;
760 uint16_t line;
761 bool is_integer_constant() const {
762 return type == TK_INT_CONSTANT || type == TK_UINT_CONSTANT;
763 }
764 };
765
766 static String get_operator_text(Operator p_op);
767 static String get_token_text(Token p_token);
768
769 static bool is_token_datatype(TokenType p_type);
770 static bool is_token_variable_datatype(TokenType p_type);
771 static DataType get_token_datatype(TokenType p_type);
772 static bool is_token_interpolation(TokenType p_type);
773 static DataInterpolation get_token_interpolation(TokenType p_type);
774 static bool is_token_precision(TokenType p_type);
775 static bool is_token_arg_qual(TokenType p_type);
776 static DataPrecision get_token_precision(TokenType p_type);
777 static String get_precision_name(DataPrecision p_type);
778 static String get_interpolation_name(DataInterpolation p_interpolation);
779 static String get_datatype_name(DataType p_type);
780 static String get_uniform_hint_name(ShaderNode::Uniform::Hint p_hint);
781 static String get_texture_filter_name(TextureFilter p_filter);
782 static String get_texture_repeat_name(TextureRepeat p_repeat);
783 static bool is_token_nonvoid_datatype(TokenType p_type);
784 static bool is_token_operator(TokenType p_type);
785 static bool is_token_operator_assign(TokenType p_type);
786 static bool is_token_hint(TokenType p_type);
787
788 static bool convert_constant(ConstantNode *p_constant, DataType p_to_type, ConstantNode::Value *p_value = nullptr);
789 static DataType get_scalar_type(DataType p_type);
790 static int get_cardinality(DataType p_type);
791 static bool is_scalar_type(DataType p_type);
792 static bool is_float_type(DataType p_type);
793 static bool is_sampler_type(DataType p_type);
794 static Variant constant_value_to_variant(const Vector<ShaderLanguage::ConstantNode::Value> &p_value, DataType p_type, int p_array_size, ShaderLanguage::ShaderNode::Uniform::Hint p_hint = ShaderLanguage::ShaderNode::Uniform::HINT_NONE);
795 static PropertyInfo uniform_to_property_info(const ShaderNode::Uniform &p_uniform);
796 static uint32_t get_datatype_size(DataType p_type);
797
798 static void get_keyword_list(List<String> *r_keywords);
799 static bool is_control_flow_keyword(String p_keyword);
800 static void get_builtin_funcs(List<String> *r_keywords);
801
802 struct BuiltInInfo {
803 DataType type = TYPE_VOID;
804 bool constant = false;
805
806 BuiltInInfo() {}
807
808 BuiltInInfo(DataType p_type, bool p_constant = false) :
809 type(p_type),
810 constant(p_constant) {}
811 };
812
813 struct StageFunctionInfo {
814 struct Argument {
815 StringName name;
816 DataType type;
817
818 Argument(const StringName &p_name = StringName(), DataType p_type = TYPE_VOID) {
819 name = p_name;
820 type = p_type;
821 }
822 };
823
824 Vector<Argument> arguments;
825 DataType return_type = TYPE_VOID;
826 };
827
828 struct ModeInfo {
829 StringName name;
830 Vector<StringName> options;
831
832 ModeInfo() {}
833
834 ModeInfo(const StringName &p_name) :
835 name(p_name) {
836 }
837
838 ModeInfo(const StringName &p_name, const StringName &p_arg1, const StringName &p_arg2) :
839 name(p_name) {
840 options.push_back(p_arg1);
841 options.push_back(p_arg2);
842 }
843
844 ModeInfo(const StringName &p_name, const StringName &p_arg1, const StringName &p_arg2, const StringName &p_arg3) :
845 name(p_name) {
846 options.push_back(p_arg1);
847 options.push_back(p_arg2);
848 options.push_back(p_arg3);
849 }
850
851 ModeInfo(const StringName &p_name, const StringName &p_arg1, const StringName &p_arg2, const StringName &p_arg3, const StringName &p_arg4) :
852 name(p_name) {
853 options.push_back(p_arg1);
854 options.push_back(p_arg2);
855 options.push_back(p_arg3);
856 options.push_back(p_arg4);
857 }
858
859 ModeInfo(const StringName &p_name, const StringName &p_arg1, const StringName &p_arg2, const StringName &p_arg3, const StringName &p_arg4, const StringName &p_arg5) :
860 name(p_name) {
861 options.push_back(p_arg1);
862 options.push_back(p_arg2);
863 options.push_back(p_arg3);
864 options.push_back(p_arg4);
865 options.push_back(p_arg5);
866 }
867
868 ModeInfo(const StringName &p_name, const StringName &p_arg1, const StringName &p_arg2, const StringName &p_arg3, const StringName &p_arg4, const StringName &p_arg5, const StringName &p_arg6) :
869 name(p_name) {
870 options.push_back(p_arg1);
871 options.push_back(p_arg2);
872 options.push_back(p_arg3);
873 options.push_back(p_arg4);
874 options.push_back(p_arg5);
875 options.push_back(p_arg6);
876 }
877 };
878
879 struct FunctionInfo {
880 HashMap<StringName, BuiltInInfo> built_ins;
881 HashMap<StringName, StageFunctionInfo> stage_functions;
882
883 bool can_discard = false;
884 bool main_function = false;
885 };
886 static bool has_builtin(const HashMap<StringName, ShaderLanguage::FunctionInfo> &p_functions, const StringName &p_name);
887
888 typedef DataType (*GlobalShaderUniformGetTypeFunc)(const StringName &p_name);
889
890 struct FilePosition {
891 String file;
892 int line = 0;
893 };
894
895private:
896 struct KeyWord {
897 TokenType token;
898 const char *text;
899 uint32_t flags;
900 const Vector<String> excluded_shader_types;
901 const Vector<String> functions;
902 };
903
904 static const KeyWord keyword_list[];
905
906 GlobalShaderUniformGetTypeFunc global_shader_uniform_get_type_func = nullptr;
907
908 bool error_set = false;
909 String error_str;
910 int error_line = 0;
911
912 Vector<FilePosition> include_positions;
913
914#ifdef DEBUG_ENABLED
915 struct Usage {
916 int decl_line;
917 bool used = false;
918 Usage(int p_decl_line = -1) {
919 decl_line = p_decl_line;
920 }
921 };
922
923 HashMap<StringName, Usage> used_constants;
924 HashMap<StringName, Usage> used_varyings;
925 HashMap<StringName, Usage> used_uniforms;
926 HashMap<StringName, Usage> used_functions;
927 HashMap<StringName, Usage> used_structs;
928 HashMap<ShaderWarning::Code, HashMap<StringName, Usage> *> warnings_check_map;
929
930 HashMap<StringName, HashMap<StringName, Usage>> used_local_vars;
931 HashMap<ShaderWarning::Code, HashMap<StringName, HashMap<StringName, Usage>> *> warnings_check_map2;
932
933 List<ShaderWarning> warnings;
934
935 bool check_warnings = false;
936 uint32_t warning_flags = 0;
937
938 void _add_line_warning(ShaderWarning::Code p_code, const StringName &p_subject = "", const Vector<Variant> &p_extra_args = Vector<Variant>()) {
939 warnings.push_back(ShaderWarning(p_code, tk_line, p_subject, p_extra_args));
940 }
941 void _add_global_warning(ShaderWarning::Code p_code, const StringName &p_subject = "", const Vector<Variant> &p_extra_args = Vector<Variant>()) {
942 warnings.push_back(ShaderWarning(p_code, -1, p_subject, p_extra_args));
943 }
944 void _add_warning(ShaderWarning::Code p_code, int p_line, const StringName &p_subject = "", const Vector<Variant> &p_extra_args = Vector<Variant>()) {
945 warnings.push_back(ShaderWarning(p_code, p_line, p_subject, p_extra_args));
946 }
947 void _check_warning_accums();
948#endif // DEBUG_ENABLED
949
950 String code;
951 int char_idx = 0;
952 int tk_line = 0;
953
954 StringName shader_type_identifier;
955 StringName current_function;
956 bool is_const_decl = false;
957 StringName last_name;
958 bool is_shader_inc = false;
959
960 String current_uniform_group_name;
961 String current_uniform_subgroup_name;
962
963 VaryingFunctionNames varying_function_names;
964
965 TkPos _get_tkpos() {
966 TkPos tkp;
967 tkp.char_idx = char_idx;
968 tkp.tk_line = tk_line;
969 return tkp;
970 }
971
972 void _set_tkpos(TkPos p_pos) {
973 char_idx = p_pos.char_idx;
974 tk_line = p_pos.tk_line;
975 }
976
977 void _set_error(const String &p_str) {
978 if (error_set) {
979 return;
980 }
981
982 error_line = tk_line;
983 error_set = true;
984 error_str = p_str;
985 include_positions.write[include_positions.size() - 1].line = tk_line;
986 }
987
988 void _set_expected_error(const String &p_what) {
989 _set_error(vformat(RTR("Expected a '%s'."), p_what));
990 }
991
992 void _set_expected_error(const String &p_first, const String p_second) {
993 _set_error(vformat(RTR("Expected a '%s' or '%s'."), p_first, p_second));
994 }
995
996 void _set_expected_after_error(const String &p_what, const String &p_after) {
997 _set_error(vformat(RTR("Expected a '%s' after '%s'."), p_what, p_after));
998 }
999
1000 void _set_redefinition_error(const String &p_what) {
1001 _set_error(vformat(RTR("Redefinition of '%s'."), p_what));
1002 }
1003
1004 void _set_parsing_error() {
1005 _set_error("Parser bug.");
1006 }
1007
1008 static const char *token_names[TK_MAX];
1009
1010 Token _make_token(TokenType p_type, const StringName &p_text = StringName());
1011 Token _get_token();
1012 bool _lookup_next(Token &r_tk);
1013
1014 ShaderNode *shader = nullptr;
1015
1016 enum IdentifierType {
1017 IDENTIFIER_FUNCTION,
1018 IDENTIFIER_UNIFORM,
1019 IDENTIFIER_VARYING,
1020 IDENTIFIER_FUNCTION_ARGUMENT,
1021 IDENTIFIER_LOCAL_VAR,
1022 IDENTIFIER_BUILTIN_VAR,
1023 IDENTIFIER_CONSTANT,
1024 IDENTIFIER_MAX,
1025 };
1026
1027 IdentifierType last_type = IDENTIFIER_MAX;
1028
1029 bool _find_identifier(const BlockNode *p_block, bool p_allow_reassign, const FunctionInfo &p_function_info, const StringName &p_identifier, DataType *r_data_type = nullptr, IdentifierType *r_type = nullptr, bool *r_is_const = nullptr, int *r_array_size = nullptr, StringName *r_struct_name = nullptr, ConstantNode::Value *r_constant_value = nullptr);
1030#ifdef DEBUG_ENABLED
1031 void _parse_used_identifier(const StringName &p_identifier, IdentifierType p_type, const StringName &p_function);
1032#endif // DEBUG_ENABLED
1033 bool _is_operator_assign(Operator p_op) const;
1034 bool _validate_assign(Node *p_node, const FunctionInfo &p_function_info, String *r_message = nullptr);
1035 bool _validate_operator(OperatorNode *p_op, DataType *r_ret_type = nullptr, int *r_ret_size = nullptr);
1036
1037 struct BuiltinFuncDef {
1038 enum { MAX_ARGS = 5 };
1039 const char *name;
1040 DataType rettype;
1041 const DataType args[MAX_ARGS];
1042 const char *args_names[MAX_ARGS];
1043 SubClassTag tag;
1044 bool high_end;
1045 };
1046
1047 struct BuiltinFuncOutArgs { //arguments used as out in built in functions
1048 enum { MAX_ARGS = 2 };
1049 const char *name;
1050 const int arguments[MAX_ARGS];
1051 };
1052
1053 struct BuiltinFuncConstArgs {
1054 const char *name;
1055 int arg;
1056 int min;
1057 int max;
1058 };
1059
1060 CompletionType completion_type;
1061 ShaderNode::Uniform::Hint current_uniform_hint = ShaderNode::Uniform::HINT_NONE;
1062 TextureFilter current_uniform_filter = FILTER_DEFAULT;
1063 TextureRepeat current_uniform_repeat = REPEAT_DEFAULT;
1064 bool current_uniform_instance_index_defined = false;
1065 int completion_line = 0;
1066 BlockNode *completion_block = nullptr;
1067 DataType completion_base;
1068 bool completion_base_array = false;
1069 SubClassTag completion_class;
1070 StringName completion_function;
1071 StringName completion_struct;
1072 int completion_argument = 0;
1073
1074#ifdef DEBUG_ENABLED
1075 uint32_t keyword_completion_context;
1076#endif // DEBUG_ENABLED
1077
1078 const HashMap<StringName, FunctionInfo> *stages = nullptr;
1079
1080 bool _get_completable_identifier(BlockNode *p_block, CompletionType p_type, StringName &identifier);
1081 static const BuiltinFuncDef builtin_func_defs[];
1082 static const BuiltinFuncOutArgs builtin_func_out_args[];
1083 static const BuiltinFuncConstArgs builtin_func_const_args[];
1084
1085 static bool is_const_suffix_lut_initialized;
1086
1087 Error _validate_precision(DataType p_type, DataPrecision p_precision);
1088 bool _compare_datatypes(DataType p_datatype_a, String p_datatype_name_a, int p_array_size_a, DataType p_datatype_b, String p_datatype_name_b, int p_array_size_b);
1089 bool _compare_datatypes_in_nodes(Node *a, Node *b);
1090
1091 bool _validate_function_call(BlockNode *p_block, const FunctionInfo &p_function_info, OperatorNode *p_func, DataType *r_ret_type, StringName *r_ret_type_str, bool *r_is_custom_function = nullptr);
1092 bool _parse_function_arguments(BlockNode *p_block, const FunctionInfo &p_function_info, OperatorNode *p_func, int *r_complete_arg = nullptr);
1093 bool _propagate_function_call_sampler_uniform_settings(StringName p_name, int p_argument, TextureFilter p_filter, TextureRepeat p_repeat);
1094 bool _propagate_function_call_sampler_builtin_reference(StringName p_name, int p_argument, const StringName &p_builtin);
1095 bool _validate_varying_assign(ShaderNode::Varying &p_varying, String *r_message);
1096 bool _check_node_constness(const Node *p_node) const;
1097
1098 Node *_parse_expression(BlockNode *p_block, const FunctionInfo &p_function_info);
1099 Error _parse_array_size(BlockNode *p_block, const FunctionInfo &p_function_info, bool p_forbid_unknown_size, Node **r_size_expression, int *r_array_size, bool *r_unknown_size);
1100 Node *_parse_array_constructor(BlockNode *p_block, const FunctionInfo &p_function_info);
1101 Node *_parse_array_constructor(BlockNode *p_block, const FunctionInfo &p_function_info, DataType p_type, const StringName &p_struct_name, int p_array_size);
1102 ShaderLanguage::Node *_reduce_expression(BlockNode *p_block, ShaderLanguage::Node *p_node);
1103
1104 Node *_parse_and_reduce_expression(BlockNode *p_block, const FunctionInfo &p_function_info);
1105 Error _parse_block(BlockNode *p_block, const FunctionInfo &p_function_info, bool p_just_one = false, bool p_can_break = false, bool p_can_continue = false);
1106 String _get_shader_type_list(const HashSet<String> &p_shader_types) const;
1107 String _get_qualifier_str(ArgumentQualifier p_qualifier) const;
1108
1109 Error _parse_shader(const HashMap<StringName, FunctionInfo> &p_functions, const Vector<ModeInfo> &p_render_modes, const HashSet<String> &p_shader_types);
1110
1111 Error _find_last_flow_op_in_block(BlockNode *p_block, FlowOperation p_op);
1112 Error _find_last_flow_op_in_op(ControlFlowNode *p_flow, FlowOperation p_op);
1113
1114public:
1115#ifdef DEBUG_ENABLED
1116 List<ShaderWarning>::Element *get_warnings_ptr();
1117
1118 void enable_warning_checking(bool p_enabled);
1119 bool is_warning_checking_enabled() const;
1120
1121 void set_warning_flags(uint32_t p_flags);
1122 uint32_t get_warning_flags() const;
1123#endif // DEBUG_ENABLED
1124
1125 //static void get_keyword_list(ShaderType p_type,List<String> *p_keywords);
1126
1127 void clear();
1128
1129 static String get_shader_type(const String &p_code);
1130 static bool is_builtin_func_out_parameter(const String &p_name, int p_param);
1131
1132 struct ShaderCompileInfo {
1133 HashMap<StringName, FunctionInfo> functions;
1134 Vector<ModeInfo> render_modes;
1135 VaryingFunctionNames varying_function_names = VaryingFunctionNames();
1136 HashSet<String> shader_types;
1137 GlobalShaderUniformGetTypeFunc global_shader_uniform_type_func = nullptr;
1138 bool is_include = false;
1139 };
1140
1141 Error compile(const String &p_code, const ShaderCompileInfo &p_info);
1142 Error complete(const String &p_code, const ShaderCompileInfo &p_info, List<ScriptLanguage::CodeCompletionOption> *r_options, String &r_call_hint);
1143
1144 String get_error_text();
1145 Vector<FilePosition> get_include_positions();
1146 int get_error_line();
1147
1148 ShaderNode *get_shader();
1149
1150 String token_debug(const String &p_code);
1151
1152 ShaderLanguage();
1153 ~ShaderLanguage();
1154};
1155
1156#endif // SHADER_LANGUAGE_H
1157