Debian Codegraph @

servers/rendering/shader_warnings.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* shader_warnings.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_warnings.h"
32 #include "core/variant/variant.h"
33
34 #ifdef DEBUG_ENABLED
35
36 ShaderWarning::Code ShaderWarning::get_code() const {
37 return code;
38 }
39
40 int ShaderWarning::get_line() const {
41 return line;
42 }
43
44 const StringName &ShaderWarning::get_subject() const {
45 return subject;
46 }
47
48 String ShaderWarning::get_message() const {
49 switch (code) {
50 case FLOAT_COMPARISON:
51 return vformat(RTR("Direct floating-point comparison (this may not evaluate to `true` as you expect). Instead, use `abs(a - b) < 0.0001` for an approximate but predictable comparison."));
52 case UNUSED_CONSTANT:
53 return vformat(RTR("The const '%s' is declared but never used."), subject);
54 case UNUSED_FUNCTION:
55 return vformat(RTR("The function '%s' is declared but never used."), subject);
56 case UNUSED_STRUCT:
57 return vformat(RTR("The struct '%s' is declared but never used."), subject);
58 case UNUSED_UNIFORM:
59 return vformat(RTR("The uniform '%s' is declared but never used."), subject);
60 case UNUSED_VARYING:
61 return vformat(RTR("The varying '%s' is declared but never used."), subject);
62 case UNUSED_LOCAL_VARIABLE:
63 return vformat(RTR("The local variable '%s' is declared but never used."), subject);
64 case FORMATTING_ERROR:
65 return subject;
66 case DEVICE_LIMIT_EXCEEDED:
67 return vformat(RTR("The total size of the %s for this shader on this device has been exceeded (%d/%d). The shader may not work correctly."), subject, (int)extra_args[0], (int)extra_args[1]);
68 case MAGIC_POSITION_WRITE:
69 return vformat(RTR("You are attempting to assign the VERTEX position in model space to the vertex POSITION in clip space. The definition of clip space changed in version 4.3, so if this code was written prior to 4.3, it will not continue to work. Consider specifying the clip space z-component directly i.e. use `vec4(VERTEX.xy, 1.0, 1.0)`."));
70 default:
71 break;
72 }
73 return String();
74 }
75
76 String ShaderWarning::get_name() const {
77 return get_name_from_code(code);
78 }
79
80 Vector<Variant> ShaderWarning::get_extra_args() const {
81 return extra_args;
82 }
83
84 String ShaderWarning::get_name_from_code(Code p_code) {
85 ERR_FAIL_INDEX_V(p_code, WARNING_MAX, String());
86
87 static const char *names[] = {
88 PNAME("FLOAT_COMPARISON"),
89 PNAME("UNUSED_CONSTANT"),
90 PNAME("UNUSED_FUNCTION"),
91 PNAME("UNUSED_STRUCT"),
92 PNAME("UNUSED_UNIFORM"),
93 PNAME("UNUSED_VARYING"),
94 PNAME("UNUSED_LOCAL_VARIABLE"),
95 PNAME("FORMATTING_ERROR"),
96 PNAME("DEVICE_LIMIT_EXCEEDED"),
97 PNAME("MAGIC_POSITION_WRITE"),
98 };
99
100 static_assert(std_size(names) == WARNING_MAX, "Amount of warning types don't match the amount of warning names.");
101
102 return names[(int)p_code];
103 }
104
105 ShaderWarning::Code ShaderWarning::get_code_from_name(const String &p_name) {
106 for (int i = 0; i < WARNING_MAX; i++) {
107 if (get_name_from_code((Code)i) == p_name) {
108 return (Code)i;
109 }
110 }
111
112 ERR_FAIL_V_MSG(WARNING_MAX, "Invalid shader warning name: " + p_name);
113 }
114
115 static HashMap<int, uint32_t> *code_to_flags_map = nullptr;
116
117 static void init_code_to_flags_map() {
118 code_to_flags_map = memnew((HashMap<int, uint32_t>));
119 code_to_flags_map->insert(ShaderWarning::FLOAT_COMPARISON, ShaderWarning::FLOAT_COMPARISON_FLAG);
120 code_to_flags_map->insert(ShaderWarning::UNUSED_CONSTANT, ShaderWarning::UNUSED_CONSTANT_FLAG);
121 code_to_flags_map->insert(ShaderWarning::UNUSED_FUNCTION, ShaderWarning::UNUSED_FUNCTION_FLAG);
122 code_to_flags_map->insert(ShaderWarning::UNUSED_STRUCT, ShaderWarning::UNUSED_STRUCT_FLAG);
123 code_to_flags_map->insert(ShaderWarning::UNUSED_UNIFORM, ShaderWarning::UNUSED_UNIFORM_FLAG);
124 code_to_flags_map->insert(ShaderWarning::UNUSED_VARYING, ShaderWarning::UNUSED_VARYING_FLAG);
125 code_to_flags_map->insert(ShaderWarning::UNUSED_LOCAL_VARIABLE, ShaderWarning::UNUSED_LOCAL_VARIABLE_FLAG);
126 code_to_flags_map->insert(ShaderWarning::FORMATTING_ERROR, ShaderWarning::FORMATTING_ERROR_FLAG);
127 code_to_flags_map->insert(ShaderWarning::DEVICE_LIMIT_EXCEEDED, ShaderWarning::DEVICE_LIMIT_EXCEEDED_FLAG);
128 code_to_flags_map->insert(ShaderWarning::MAGIC_POSITION_WRITE, ShaderWarning::MAGIC_POSITION_WRITE_FLAG);
129 }
130
131 ShaderWarning::CodeFlags ShaderWarning::get_flags_from_codemap(const HashMap<Code, bool> &p_map) {
132 uint32_t result = 0U;
133
134 if (code_to_flags_map == nullptr) {
135 init_code_to_flags_map();
136 }
137
138 for (const KeyValue<Code, bool> &E : p_map) {
139 if (E.value) {
140 ERR_FAIL_COND_V(!code_to_flags_map->has((int)E.key), ShaderWarning::NONE_FLAG);
141 result |= (*code_to_flags_map)[(int)E.key];
142 }
143 }
144 return (CodeFlags)result;
145 }
146
147 ShaderWarning::ShaderWarning(Code p_code, int p_line, const StringName &p_subject, const Vector<Variant> &p_extra_args) :
148 code(p_code), line(p_line), subject(p_subject), extra_args(p_extra_args) {
149 }
150
151 #endif // DEBUG_ENABLED