shader.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*************************************************************************/
  2. /* shader.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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.h"
  31. #include "core/io/file_access.h"
  32. #include "scene/scene_string_names.h"
  33. #include "servers/rendering/shader_language.h"
  34. #include "servers/rendering/shader_preprocessor.h"
  35. #include "servers/rendering_server.h"
  36. #include "texture.h"
  37. Shader::Mode Shader::get_mode() const {
  38. return mode;
  39. }
  40. void Shader::_dependency_changed() {
  41. RenderingServer::get_singleton()->shader_set_code(shader, RenderingServer::get_singleton()->shader_get_code(shader));
  42. params_cache_dirty = true;
  43. emit_changed();
  44. }
  45. void Shader::set_path(const String &p_path, bool p_take_over) {
  46. Resource::set_path(p_path, p_take_over);
  47. RS::get_singleton()->shader_set_path_hint(shader, p_path);
  48. }
  49. void Shader::set_code(const String &p_code) {
  50. for (Ref<ShaderInclude> E : include_dependencies) {
  51. E->disconnect(SNAME("changed"), callable_mp(this, &Shader::_dependency_changed));
  52. }
  53. String type = ShaderLanguage::get_shader_type(p_code);
  54. if (type == "canvas_item") {
  55. mode = MODE_CANVAS_ITEM;
  56. } else if (type == "particles") {
  57. mode = MODE_PARTICLES;
  58. } else if (type == "sky") {
  59. mode = MODE_SKY;
  60. } else if (type == "fog") {
  61. mode = MODE_FOG;
  62. } else {
  63. mode = MODE_SPATIAL;
  64. }
  65. code = p_code;
  66. String pp_code = p_code;
  67. HashSet<Ref<ShaderInclude>> new_include_dependencies;
  68. {
  69. // Preprocessor must run here and not in the server because:
  70. // 1) Need to keep track of include dependencies at resource level
  71. // 2) Server does not do interaction with Resource filetypes, this is a scene level feature.
  72. ShaderPreprocessor preprocessor;
  73. preprocessor.preprocess(p_code, pp_code, nullptr, nullptr, &new_include_dependencies);
  74. }
  75. // This ensures previous include resources are not freed and then re-loaded during parse (which would make compiling slower)
  76. include_dependencies = new_include_dependencies;
  77. for (Ref<ShaderInclude> E : include_dependencies) {
  78. E->connect(SNAME("changed"), callable_mp(this, &Shader::_dependency_changed));
  79. }
  80. RenderingServer::get_singleton()->shader_set_code(shader, pp_code);
  81. params_cache_dirty = true;
  82. emit_changed();
  83. }
  84. String Shader::get_code() const {
  85. _update_shader();
  86. return code;
  87. }
  88. void Shader::get_param_list(List<PropertyInfo> *p_params) const {
  89. _update_shader();
  90. List<PropertyInfo> local;
  91. RenderingServer::get_singleton()->shader_get_param_list(shader, &local);
  92. params_cache.clear();
  93. params_cache_dirty = false;
  94. for (PropertyInfo &pi : local) {
  95. if (default_textures.has(pi.name)) { //do not show default textures
  96. continue;
  97. }
  98. String original_name = pi.name;
  99. pi.name = "shader_param/" + pi.name;
  100. params_cache[pi.name] = original_name;
  101. if (p_params) {
  102. //small little hack
  103. if (pi.type == Variant::RID) {
  104. pi.type = Variant::OBJECT;
  105. }
  106. p_params->push_back(pi);
  107. }
  108. }
  109. }
  110. RID Shader::get_rid() const {
  111. _update_shader();
  112. return shader;
  113. }
  114. void Shader::set_default_texture_param(const StringName &p_param, const Ref<Texture2D> &p_texture, int p_index) {
  115. if (p_texture.is_valid()) {
  116. if (!default_textures.has(p_param)) {
  117. default_textures[p_param] = HashMap<int, Ref<Texture2D>>();
  118. }
  119. default_textures[p_param][p_index] = p_texture;
  120. RS::get_singleton()->shader_set_default_texture_param(shader, p_param, p_texture->get_rid(), p_index);
  121. } else {
  122. if (default_textures.has(p_param) && default_textures[p_param].has(p_index)) {
  123. default_textures[p_param].erase(p_index);
  124. if (default_textures[p_param].is_empty()) {
  125. default_textures.erase(p_param);
  126. }
  127. }
  128. RS::get_singleton()->shader_set_default_texture_param(shader, p_param, RID(), p_index);
  129. }
  130. emit_changed();
  131. }
  132. Ref<Texture2D> Shader::get_default_texture_param(const StringName &p_param, int p_index) const {
  133. if (default_textures.has(p_param) && default_textures[p_param].has(p_index)) {
  134. return default_textures[p_param][p_index];
  135. }
  136. return Ref<Texture2D>();
  137. }
  138. void Shader::get_default_texture_param_list(List<StringName> *r_textures) const {
  139. for (const KeyValue<StringName, HashMap<int, Ref<Texture2D>>> &E : default_textures) {
  140. r_textures->push_back(E.key);
  141. }
  142. }
  143. bool Shader::is_text_shader() const {
  144. return true;
  145. }
  146. bool Shader::has_param(const StringName &p_param) const {
  147. return params_cache.has("shader_param/" + p_param);
  148. }
  149. void Shader::_update_shader() const {
  150. }
  151. void Shader::_bind_methods() {
  152. ClassDB::bind_method(D_METHOD("get_mode"), &Shader::get_mode);
  153. ClassDB::bind_method(D_METHOD("set_code", "code"), &Shader::set_code);
  154. ClassDB::bind_method(D_METHOD("get_code"), &Shader::get_code);
  155. ClassDB::bind_method(D_METHOD("set_default_texture_param", "param", "texture", "index"), &Shader::set_default_texture_param, DEFVAL(0));
  156. ClassDB::bind_method(D_METHOD("get_default_texture_param", "param", "index"), &Shader::get_default_texture_param, DEFVAL(0));
  157. ClassDB::bind_method(D_METHOD("has_param", "name"), &Shader::has_param);
  158. ADD_PROPERTY(PropertyInfo(Variant::STRING, "code", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_code", "get_code");
  159. BIND_ENUM_CONSTANT(MODE_SPATIAL);
  160. BIND_ENUM_CONSTANT(MODE_CANVAS_ITEM);
  161. BIND_ENUM_CONSTANT(MODE_PARTICLES);
  162. BIND_ENUM_CONSTANT(MODE_SKY);
  163. BIND_ENUM_CONSTANT(MODE_FOG);
  164. }
  165. Shader::Shader() {
  166. shader = RenderingServer::get_singleton()->shader_create();
  167. }
  168. Shader::~Shader() {
  169. RenderingServer::get_singleton()->free(shader);
  170. }
  171. ////////////
  172. Ref<Resource> ResourceFormatLoaderShader::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) {
  173. if (r_error) {
  174. *r_error = ERR_FILE_CANT_OPEN;
  175. }
  176. Ref<Shader> shader;
  177. shader.instantiate();
  178. Vector<uint8_t> buffer = FileAccess::get_file_as_array(p_path);
  179. String str;
  180. str.parse_utf8((const char *)buffer.ptr(), buffer.size());
  181. shader->set_code(str);
  182. if (r_error) {
  183. *r_error = OK;
  184. }
  185. return shader;
  186. }
  187. void ResourceFormatLoaderShader::get_recognized_extensions(List<String> *p_extensions) const {
  188. p_extensions->push_back("gdshader");
  189. }
  190. bool ResourceFormatLoaderShader::handles_type(const String &p_type) const {
  191. return (p_type == "Shader");
  192. }
  193. String ResourceFormatLoaderShader::get_resource_type(const String &p_path) const {
  194. String el = p_path.get_extension().to_lower();
  195. if (el == "gdshader") {
  196. return "Shader";
  197. }
  198. return "";
  199. }
  200. Error ResourceFormatSaverShader::save(const String &p_path, const Ref<Resource> &p_resource, uint32_t p_flags) {
  201. Ref<Shader> shader = p_resource;
  202. ERR_FAIL_COND_V(shader.is_null(), ERR_INVALID_PARAMETER);
  203. String source = shader->get_code();
  204. Error err;
  205. Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);
  206. ERR_FAIL_COND_V_MSG(err, err, "Cannot save shader '" + p_path + "'.");
  207. file->store_string(source);
  208. if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) {
  209. return ERR_CANT_CREATE;
  210. }
  211. return OK;
  212. }
  213. void ResourceFormatSaverShader::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const {
  214. if (const Shader *shader = Object::cast_to<Shader>(*p_resource)) {
  215. if (shader->is_text_shader()) {
  216. p_extensions->push_back("gdshader");
  217. }
  218. }
  219. }
  220. bool ResourceFormatSaverShader::recognize(const Ref<Resource> &p_resource) const {
  221. return p_resource->get_class_name() == "Shader"; //only shader, not inherited
  222. }