shader_include_db.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**************************************************************************/
  2. /* shader_include_db.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_include_db.h"
  31. HashMap<String, String> ShaderIncludeDB::built_in_includes;
  32. void ShaderIncludeDB::_bind_methods() {
  33. ClassDB::bind_static_method("ShaderIncludeDB", D_METHOD("list_built_in_include_files"), &ShaderIncludeDB::list_built_in_include_files);
  34. ClassDB::bind_static_method("ShaderIncludeDB", D_METHOD("has_built_in_include_file", "filename"), &ShaderIncludeDB::has_built_in_include_file);
  35. ClassDB::bind_static_method("ShaderIncludeDB", D_METHOD("get_built_in_include_file", "filename"), &ShaderIncludeDB::get_built_in_include_file);
  36. }
  37. void ShaderIncludeDB::register_built_in_include_file(const String &p_filename, const String &p_shader_code) {
  38. built_in_includes[p_filename] = p_shader_code;
  39. }
  40. PackedStringArray ShaderIncludeDB::list_built_in_include_files() {
  41. PackedStringArray ret;
  42. for (const KeyValue<String, String> &e : built_in_includes) {
  43. ret.push_back(e.key);
  44. }
  45. return ret;
  46. }
  47. bool ShaderIncludeDB::has_built_in_include_file(const String &p_filename) {
  48. return built_in_includes.has(p_filename);
  49. }
  50. String ShaderIncludeDB::get_built_in_include_file(const String &p_filename) {
  51. const String *ptr = built_in_includes.getptr(p_filename);
  52. return ptr ? *ptr : String();
  53. }
  54. String ShaderIncludeDB::parse_include_files(const String &p_code) {
  55. // Prevent needless processing if we don't have any includes.
  56. if (p_code.find("#include ") == -1) {
  57. return p_code;
  58. }
  59. const String include = "#include ";
  60. String parsed_code;
  61. Vector<String> lines = p_code.split("\n");
  62. int line_count = lines.size();
  63. for (int i = 0; i < line_count; i++) {
  64. const String &l = lines[i];
  65. if (l.begins_with(include)) {
  66. String include_file = l.replace(include, "").strip_edges();
  67. if (include_file[0] == '"') {
  68. int end_pos = include_file.find_char('"', 1);
  69. if (end_pos >= 0) {
  70. include_file = include_file.substr(1, end_pos - 1);
  71. String include_code = ShaderIncludeDB::get_built_in_include_file(include_file);
  72. if (!include_code.is_empty()) {
  73. // Add these lines into our parse list so we parse them as well.
  74. Vector<String> include_lines = include_code.split("\n");
  75. for (int j = include_lines.size() - 1; j >= 0; j--) {
  76. lines.insert(i + 1, include_lines[j]);
  77. }
  78. line_count = lines.size();
  79. } else {
  80. // Just add it back in, this will cause a compile error to alert the user.
  81. parsed_code += l + "\n";
  82. }
  83. } else {
  84. // Include as is.
  85. parsed_code += l + "\n";
  86. }
  87. } else {
  88. // Include as is.
  89. parsed_code += l + "\n";
  90. }
  91. } else {
  92. // Include as is.
  93. parsed_code += l + "\n";
  94. }
  95. }
  96. return parsed_code;
  97. }