shader_warnings.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*************************************************************************/
  2. /* shader_warnings.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "shader_warnings.h"
  31. #include "core/variant/variant.h"
  32. #ifdef DEBUG_ENABLED
  33. ShaderWarning::Code ShaderWarning::get_code() const {
  34. return code;
  35. }
  36. int ShaderWarning::get_line() const {
  37. return line;
  38. }
  39. const StringName &ShaderWarning::get_subject() const {
  40. return subject;
  41. }
  42. String ShaderWarning::get_message() const {
  43. switch (code) {
  44. case FLOAT_COMPARISON:
  45. return vformat("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.");
  46. case UNUSED_CONSTANT:
  47. return vformat("The const '%s' is declared but never used.", subject);
  48. case UNUSED_FUNCTION:
  49. return vformat("The function '%s' is declared but never used.", subject);
  50. case UNUSED_STRUCT:
  51. return vformat("The struct '%s' is declared but never used.", subject);
  52. case UNUSED_UNIFORM:
  53. return vformat("The uniform '%s' is declared but never used.", subject);
  54. case UNUSED_VARYING:
  55. return vformat("The varying '%s' is declared but never used.", subject);
  56. case UNUSED_LOCAL_VARIABLE:
  57. return vformat("The local variable '%s' is declared but never used.", subject);
  58. default:
  59. break;
  60. }
  61. return String();
  62. }
  63. String ShaderWarning::get_name() const {
  64. return get_name_from_code(code);
  65. }
  66. String ShaderWarning::get_name_from_code(Code p_code) {
  67. ERR_FAIL_INDEX_V(p_code, WARNING_MAX, String());
  68. static const char *names[] = {
  69. "FLOAT_COMPARISON",
  70. "UNUSED_CONSTANT",
  71. "UNUSED_FUNCTION",
  72. "UNUSED_STRUCT",
  73. "UNUSED_UNIFORM",
  74. "UNUSED_VARYING",
  75. "UNUSED_LOCAL_VARIABLE",
  76. };
  77. static_assert((sizeof(names) / sizeof(*names)) == WARNING_MAX, "Amount of warning types don't match the amount of warning names.");
  78. return names[(int)p_code];
  79. }
  80. ShaderWarning::Code ShaderWarning::get_code_from_name(const String &p_name) {
  81. for (int i = 0; i < WARNING_MAX; i++) {
  82. if (get_name_from_code((Code)i) == p_name) {
  83. return (Code)i;
  84. }
  85. }
  86. ERR_FAIL_V_MSG(WARNING_MAX, "Invalid shader warning name: " + p_name);
  87. }
  88. static Map<int, uint32_t> *code_to_flags_map = nullptr;
  89. static void init_code_to_flags_map() {
  90. code_to_flags_map = memnew((Map<int, uint32_t>));
  91. code_to_flags_map->insert(ShaderWarning::FLOAT_COMPARISON, ShaderWarning::FLOAT_COMPARISON_FLAG);
  92. code_to_flags_map->insert(ShaderWarning::UNUSED_CONSTANT, ShaderWarning::UNUSED_CONSTANT_FLAG);
  93. code_to_flags_map->insert(ShaderWarning::UNUSED_FUNCTION, ShaderWarning::UNUSED_FUNCTION_FLAG);
  94. code_to_flags_map->insert(ShaderWarning::UNUSED_STRUCT, ShaderWarning::UNUSED_STRUCT_FLAG);
  95. code_to_flags_map->insert(ShaderWarning::UNUSED_UNIFORM, ShaderWarning::UNUSED_UNIFORM_FLAG);
  96. code_to_flags_map->insert(ShaderWarning::UNUSED_VARYING, ShaderWarning::UNUSED_VARYING_FLAG);
  97. code_to_flags_map->insert(ShaderWarning::UNUSED_LOCAL_VARIABLE, ShaderWarning::UNUSED_LOCAL_VARIABLE_FLAG);
  98. }
  99. ShaderWarning::CodeFlags ShaderWarning::get_flags_from_codemap(const Map<Code, bool> &p_map) {
  100. uint32_t result = 0U;
  101. if (code_to_flags_map == nullptr) {
  102. init_code_to_flags_map();
  103. }
  104. for (const KeyValue<Code, bool> &E : p_map) {
  105. if (E.value) {
  106. ERR_FAIL_COND_V(!code_to_flags_map->has((int)E.key), ShaderWarning::NONE_FLAG);
  107. result |= (*code_to_flags_map)[(int)E.key];
  108. }
  109. }
  110. return (CodeFlags)result;
  111. }
  112. ShaderWarning::ShaderWarning(Code p_code, int p_line, const StringName &p_subject) :
  113. code(p_code), line(p_line), subject(p_subject) {
  114. }
  115. #endif // DEBUG_ENABLED