shader_include.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**************************************************************************/
  2. /* shader_include.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.h"
  31. #include "servers/rendering/shader_language.h"
  32. #include "servers/rendering/shader_preprocessor.h"
  33. void ShaderInclude::_dependency_changed() {
  34. emit_changed();
  35. }
  36. void ShaderInclude::set_code(const String &p_code) {
  37. code = p_code;
  38. for (const Ref<ShaderInclude> &E : dependencies) {
  39. E->disconnect_changed(callable_mp(this, &ShaderInclude::_dependency_changed));
  40. }
  41. {
  42. String path = get_path();
  43. if (path.is_empty()) {
  44. path = include_path;
  45. }
  46. String pp_code;
  47. HashSet<Ref<ShaderInclude>> new_dependencies;
  48. ShaderPreprocessor preprocessor;
  49. Error result = preprocessor.preprocess(p_code, path, pp_code, nullptr, nullptr, nullptr, &new_dependencies);
  50. if (result == OK) {
  51. // This ensures previous include resources are not freed and then re-loaded during parse (which would make compiling slower)
  52. dependencies = new_dependencies;
  53. }
  54. }
  55. for (const Ref<ShaderInclude> &E : dependencies) {
  56. E->connect_changed(callable_mp(this, &ShaderInclude::_dependency_changed));
  57. }
  58. emit_changed();
  59. }
  60. String ShaderInclude::get_code() const {
  61. return code;
  62. }
  63. void ShaderInclude::set_include_path(const String &p_path) {
  64. include_path = p_path;
  65. }
  66. void ShaderInclude::_bind_methods() {
  67. ClassDB::bind_method(D_METHOD("set_code", "code"), &ShaderInclude::set_code);
  68. ClassDB::bind_method(D_METHOD("get_code"), &ShaderInclude::get_code);
  69. ADD_PROPERTY(PropertyInfo(Variant::STRING, "code", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_code", "get_code");
  70. }
  71. // ResourceFormatLoaderShaderInclude
  72. Ref<Resource> ResourceFormatLoaderShaderInclude::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
  73. if (r_error) {
  74. *r_error = ERR_FILE_CANT_OPEN;
  75. }
  76. Error error = OK;
  77. Vector<uint8_t> buffer = FileAccess::get_file_as_bytes(p_path, &error);
  78. ERR_FAIL_COND_V_MSG(error, nullptr, "Cannot load shader include: " + p_path);
  79. String str;
  80. if (buffer.size() > 0) {
  81. error = str.parse_utf8((const char *)buffer.ptr(), buffer.size());
  82. ERR_FAIL_COND_V_MSG(error, nullptr, "Cannot parse shader include: " + p_path);
  83. }
  84. Ref<ShaderInclude> shader_inc;
  85. shader_inc.instantiate();
  86. shader_inc->set_include_path(p_path);
  87. shader_inc->set_code(str);
  88. if (r_error) {
  89. *r_error = OK;
  90. }
  91. return shader_inc;
  92. }
  93. void ResourceFormatLoaderShaderInclude::get_recognized_extensions(List<String> *p_extensions) const {
  94. p_extensions->push_back("gdshaderinc");
  95. }
  96. bool ResourceFormatLoaderShaderInclude::handles_type(const String &p_type) const {
  97. return (p_type == "ShaderInclude");
  98. }
  99. String ResourceFormatLoaderShaderInclude::get_resource_type(const String &p_path) const {
  100. String extension = p_path.get_extension().to_lower();
  101. if (extension == "gdshaderinc") {
  102. return "ShaderInclude";
  103. }
  104. return "";
  105. }
  106. // ResourceFormatSaverShaderInclude
  107. Error ResourceFormatSaverShaderInclude::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {
  108. Ref<ShaderInclude> shader_inc = p_resource;
  109. ERR_FAIL_COND_V(shader_inc.is_null(), ERR_INVALID_PARAMETER);
  110. String source = shader_inc->get_code();
  111. Error error;
  112. Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &error);
  113. ERR_FAIL_COND_V_MSG(error, error, "Cannot save shader include '" + p_path + "'.");
  114. file->store_string(source);
  115. if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) {
  116. return ERR_CANT_CREATE;
  117. }
  118. return OK;
  119. }
  120. void ResourceFormatSaverShaderInclude::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const {
  121. const ShaderInclude *shader_inc = Object::cast_to<ShaderInclude>(*p_resource);
  122. if (shader_inc != nullptr) {
  123. p_extensions->push_back("gdshaderinc");
  124. }
  125. }
  126. bool ResourceFormatSaverShaderInclude::recognize(const Ref<Resource> &p_resource) const {
  127. return p_resource->get_class_name() == "ShaderInclude"; //only shader, not inherited
  128. }