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 | default: |
69 | break; |
70 | } |
71 | return String(); |
72 | } |
73 | |
74 | String ShaderWarning::get_name() const { |
75 | return get_name_from_code(code); |
76 | } |
77 | |
78 | Vector<Variant> ShaderWarning::() const { |
79 | return extra_args; |
80 | } |
81 | |
82 | String ShaderWarning::get_name_from_code(Code p_code) { |
83 | ERR_FAIL_INDEX_V(p_code, WARNING_MAX, String()); |
84 | |
85 | static const char *names[] = { |
86 | "FLOAT_COMPARISON" , |
87 | "UNUSED_CONSTANT" , |
88 | "UNUSED_FUNCTION" , |
89 | "UNUSED_STRUCT" , |
90 | "UNUSED_UNIFORM" , |
91 | "UNUSED_VARYING" , |
92 | "UNUSED_LOCAL_VARIABLE" , |
93 | "FORMATTING_ERROR" , |
94 | "DEVICE_LIMIT_EXCEEDED" , |
95 | }; |
96 | |
97 | static_assert((sizeof(names) / sizeof(*names)) == WARNING_MAX, "Amount of warning types don't match the amount of warning names." ); |
98 | |
99 | return names[(int)p_code]; |
100 | } |
101 | |
102 | ShaderWarning::Code ShaderWarning::get_code_from_name(const String &p_name) { |
103 | for (int i = 0; i < WARNING_MAX; i++) { |
104 | if (get_name_from_code((Code)i) == p_name) { |
105 | return (Code)i; |
106 | } |
107 | } |
108 | |
109 | ERR_FAIL_V_MSG(WARNING_MAX, "Invalid shader warning name: " + p_name); |
110 | } |
111 | |
112 | static HashMap<int, uint32_t> *code_to_flags_map = nullptr; |
113 | |
114 | static void init_code_to_flags_map() { |
115 | code_to_flags_map = memnew((HashMap<int, uint32_t>)); |
116 | code_to_flags_map->insert(ShaderWarning::FLOAT_COMPARISON, ShaderWarning::FLOAT_COMPARISON_FLAG); |
117 | code_to_flags_map->insert(ShaderWarning::UNUSED_CONSTANT, ShaderWarning::UNUSED_CONSTANT_FLAG); |
118 | code_to_flags_map->insert(ShaderWarning::UNUSED_FUNCTION, ShaderWarning::UNUSED_FUNCTION_FLAG); |
119 | code_to_flags_map->insert(ShaderWarning::UNUSED_STRUCT, ShaderWarning::UNUSED_STRUCT_FLAG); |
120 | code_to_flags_map->insert(ShaderWarning::UNUSED_UNIFORM, ShaderWarning::UNUSED_UNIFORM_FLAG); |
121 | code_to_flags_map->insert(ShaderWarning::UNUSED_VARYING, ShaderWarning::UNUSED_VARYING_FLAG); |
122 | code_to_flags_map->insert(ShaderWarning::UNUSED_LOCAL_VARIABLE, ShaderWarning::UNUSED_LOCAL_VARIABLE_FLAG); |
123 | code_to_flags_map->insert(ShaderWarning::FORMATTING_ERROR, ShaderWarning::FORMATTING_ERROR_FLAG); |
124 | code_to_flags_map->insert(ShaderWarning::DEVICE_LIMIT_EXCEEDED, ShaderWarning::DEVICE_LIMIT_EXCEEDED_FLAG); |
125 | } |
126 | |
127 | ShaderWarning::CodeFlags ShaderWarning::get_flags_from_codemap(const HashMap<Code, bool> &p_map) { |
128 | uint32_t result = 0U; |
129 | |
130 | if (code_to_flags_map == nullptr) { |
131 | init_code_to_flags_map(); |
132 | } |
133 | |
134 | for (const KeyValue<Code, bool> &E : p_map) { |
135 | if (E.value) { |
136 | ERR_FAIL_COND_V(!code_to_flags_map->has((int)E.key), ShaderWarning::NONE_FLAG); |
137 | result |= (*code_to_flags_map)[(int)E.key]; |
138 | } |
139 | } |
140 | return (CodeFlags)result; |
141 | } |
142 | |
143 | ShaderWarning::ShaderWarning(Code p_code, int p_line, const StringName &p_subject, const Vector<Variant> &) : |
144 | code(p_code), line(p_line), subject(p_subject), extra_args(p_extra_args) { |
145 | } |
146 | |
147 | #endif // DEBUG_ENABLED |
148 | |