shader_warnings.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #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(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."));
  46. case UNUSED_CONSTANT:
  47. return vformat(RTR("The const '%s' is declared but never used."), subject);
  48. case UNUSED_FUNCTION:
  49. return vformat(RTR("The function '%s' is declared but never used."), subject);
  50. case UNUSED_STRUCT:
  51. return vformat(RTR("The struct '%s' is declared but never used."), subject);
  52. case UNUSED_UNIFORM:
  53. return vformat(RTR("The uniform '%s' is declared but never used."), subject);
  54. case UNUSED_VARYING:
  55. return vformat(RTR("The varying '%s' is declared but never used."), subject);
  56. case UNUSED_LOCAL_VARIABLE:
  57. return vformat(RTR("The local variable '%s' is declared but never used."), subject);
  58. case FORMATTING_ERROR:
  59. return subject;
  60. case DEVICE_LIMIT_EXCEEDED:
  61. 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]);
  62. default:
  63. break;
  64. }
  65. return String();
  66. }
  67. String ShaderWarning::get_name() const {
  68. return get_name_from_code(code);
  69. }
  70. Vector<Variant> ShaderWarning::get_extra_args() const {
  71. return extra_args;
  72. }
  73. String ShaderWarning::get_name_from_code(Code p_code) {
  74. ERR_FAIL_INDEX_V(p_code, WARNING_MAX, String());
  75. static const char *names[] = {
  76. "FLOAT_COMPARISON",
  77. "UNUSED_CONSTANT",
  78. "UNUSED_FUNCTION",
  79. "UNUSED_STRUCT",
  80. "UNUSED_UNIFORM",
  81. "UNUSED_VARYING",
  82. "UNUSED_LOCAL_VARIABLE",
  83. "FORMATTING_ERROR",
  84. "DEVICE_LIMIT_EXCEEDED",
  85. };
  86. static_assert((sizeof(names) / sizeof(*names)) == WARNING_MAX, "Amount of warning types don't match the amount of warning names.");
  87. return names[(int)p_code];
  88. }
  89. ShaderWarning::Code ShaderWarning::get_code_from_name(const String &p_name) {
  90. for (int i = 0; i < WARNING_MAX; i++) {
  91. if (get_name_from_code((Code)i) == p_name) {
  92. return (Code)i;
  93. }
  94. }
  95. ERR_FAIL_V_MSG(WARNING_MAX, "Invalid shader warning name: " + p_name);
  96. }
  97. static HashMap<int, uint32_t> *code_to_flags_map = nullptr;
  98. static void init_code_to_flags_map() {
  99. code_to_flags_map = memnew((HashMap<int, uint32_t>));
  100. code_to_flags_map->insert(ShaderWarning::FLOAT_COMPARISON, ShaderWarning::FLOAT_COMPARISON_FLAG);
  101. code_to_flags_map->insert(ShaderWarning::UNUSED_CONSTANT, ShaderWarning::UNUSED_CONSTANT_FLAG);
  102. code_to_flags_map->insert(ShaderWarning::UNUSED_FUNCTION, ShaderWarning::UNUSED_FUNCTION_FLAG);
  103. code_to_flags_map->insert(ShaderWarning::UNUSED_STRUCT, ShaderWarning::UNUSED_STRUCT_FLAG);
  104. code_to_flags_map->insert(ShaderWarning::UNUSED_UNIFORM, ShaderWarning::UNUSED_UNIFORM_FLAG);
  105. code_to_flags_map->insert(ShaderWarning::UNUSED_VARYING, ShaderWarning::UNUSED_VARYING_FLAG);
  106. code_to_flags_map->insert(ShaderWarning::UNUSED_LOCAL_VARIABLE, ShaderWarning::UNUSED_LOCAL_VARIABLE_FLAG);
  107. code_to_flags_map->insert(ShaderWarning::FORMATTING_ERROR, ShaderWarning::FORMATTING_ERROR_FLAG);
  108. code_to_flags_map->insert(ShaderWarning::DEVICE_LIMIT_EXCEEDED, ShaderWarning::DEVICE_LIMIT_EXCEEDED_FLAG);
  109. }
  110. ShaderWarning::CodeFlags ShaderWarning::get_flags_from_codemap(const HashMap<Code, bool> &p_map) {
  111. uint32_t result = 0U;
  112. if (code_to_flags_map == nullptr) {
  113. init_code_to_flags_map();
  114. }
  115. for (const KeyValue<Code, bool> &E : p_map) {
  116. if (E.value) {
  117. ERR_FAIL_COND_V(!code_to_flags_map->has((int)E.key), ShaderWarning::NONE_FLAG);
  118. result |= (*code_to_flags_map)[(int)E.key];
  119. }
  120. }
  121. return (CodeFlags)result;
  122. }
  123. ShaderWarning::ShaderWarning(Code p_code, int p_line, const StringName &p_subject, const Vector<Variant> &p_extra_args) :
  124. code(p_code), line(p_line), subject(p_subject), extra_args(p_extra_args) {
  125. }
  126. #endif // DEBUG_ENABLED