shader.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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, bool p_get_groups) 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. bool is_group = pi.usage == PROPERTY_USAGE_GROUP || pi.usage == PROPERTY_USAGE_SUBGROUP;
  96. if (!p_get_groups && is_group) {
  97. continue;
  98. }
  99. if (!is_group) {
  100. if (default_textures.has(pi.name)) { //do not show default textures
  101. continue;
  102. }
  103. params_cache[pi.name] = pi.name;
  104. }
  105. if (p_params) {
  106. //small little hack
  107. if (pi.type == Variant::RID) {
  108. pi.type = Variant::OBJECT;
  109. }
  110. p_params->push_back(pi);
  111. }
  112. }
  113. }
  114. RID Shader::get_rid() const {
  115. _update_shader();
  116. return shader;
  117. }
  118. void Shader::set_default_texture_param(const StringName &p_param, const Ref<Texture2D> &p_texture, int p_index) {
  119. if (p_texture.is_valid()) {
  120. if (!default_textures.has(p_param)) {
  121. default_textures[p_param] = HashMap<int, Ref<Texture2D>>();
  122. }
  123. default_textures[p_param][p_index] = p_texture;
  124. RS::get_singleton()->shader_set_default_texture_param(shader, p_param, p_texture->get_rid(), p_index);
  125. } else {
  126. if (default_textures.has(p_param) && default_textures[p_param].has(p_index)) {
  127. default_textures[p_param].erase(p_index);
  128. if (default_textures[p_param].is_empty()) {
  129. default_textures.erase(p_param);
  130. }
  131. }
  132. RS::get_singleton()->shader_set_default_texture_param(shader, p_param, RID(), p_index);
  133. }
  134. emit_changed();
  135. }
  136. Ref<Texture2D> Shader::get_default_texture_param(const StringName &p_param, int p_index) const {
  137. if (default_textures.has(p_param) && default_textures[p_param].has(p_index)) {
  138. return default_textures[p_param][p_index];
  139. }
  140. return Ref<Texture2D>();
  141. }
  142. void Shader::get_default_texture_param_list(List<StringName> *r_textures) const {
  143. for (const KeyValue<StringName, HashMap<int, Ref<Texture2D>>> &E : default_textures) {
  144. r_textures->push_back(E.key);
  145. }
  146. }
  147. bool Shader::is_text_shader() const {
  148. return true;
  149. }
  150. bool Shader::has_param(const StringName &p_param) const {
  151. return params_cache.has("shader_param/" + p_param);
  152. }
  153. void Shader::_update_shader() const {
  154. }
  155. void Shader::_bind_methods() {
  156. ClassDB::bind_method(D_METHOD("get_mode"), &Shader::get_mode);
  157. ClassDB::bind_method(D_METHOD("set_code", "code"), &Shader::set_code);
  158. ClassDB::bind_method(D_METHOD("get_code"), &Shader::get_code);
  159. ClassDB::bind_method(D_METHOD("set_default_texture_param", "param", "texture", "index"), &Shader::set_default_texture_param, DEFVAL(0));
  160. ClassDB::bind_method(D_METHOD("get_default_texture_param", "param", "index"), &Shader::get_default_texture_param, DEFVAL(0));
  161. ClassDB::bind_method(D_METHOD("has_param", "name"), &Shader::has_param);
  162. ADD_PROPERTY(PropertyInfo(Variant::STRING, "code", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_code", "get_code");
  163. BIND_ENUM_CONSTANT(MODE_SPATIAL);
  164. BIND_ENUM_CONSTANT(MODE_CANVAS_ITEM);
  165. BIND_ENUM_CONSTANT(MODE_PARTICLES);
  166. BIND_ENUM_CONSTANT(MODE_SKY);
  167. BIND_ENUM_CONSTANT(MODE_FOG);
  168. }
  169. Shader::Shader() {
  170. shader = RenderingServer::get_singleton()->shader_create();
  171. }
  172. Shader::~Shader() {
  173. RenderingServer::get_singleton()->free(shader);
  174. }
  175. ////////////
  176. 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) {
  177. if (r_error) {
  178. *r_error = ERR_FILE_CANT_OPEN;
  179. }
  180. Ref<Shader> shader;
  181. shader.instantiate();
  182. Vector<uint8_t> buffer = FileAccess::get_file_as_array(p_path);
  183. String str;
  184. str.parse_utf8((const char *)buffer.ptr(), buffer.size());
  185. shader->set_code(str);
  186. if (r_error) {
  187. *r_error = OK;
  188. }
  189. return shader;
  190. }
  191. void ResourceFormatLoaderShader::get_recognized_extensions(List<String> *p_extensions) const {
  192. p_extensions->push_back("gdshader");
  193. }
  194. bool ResourceFormatLoaderShader::handles_type(const String &p_type) const {
  195. return (p_type == "Shader");
  196. }
  197. String ResourceFormatLoaderShader::get_resource_type(const String &p_path) const {
  198. String el = p_path.get_extension().to_lower();
  199. if (el == "gdshader") {
  200. return "Shader";
  201. }
  202. return "";
  203. }
  204. Error ResourceFormatSaverShader::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {
  205. Ref<Shader> shader = p_resource;
  206. ERR_FAIL_COND_V(shader.is_null(), ERR_INVALID_PARAMETER);
  207. String source = shader->get_code();
  208. Error err;
  209. Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);
  210. ERR_FAIL_COND_V_MSG(err, err, "Cannot save shader '" + p_path + "'.");
  211. file->store_string(source);
  212. if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) {
  213. return ERR_CANT_CREATE;
  214. }
  215. return OK;
  216. }
  217. void ResourceFormatSaverShader::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const {
  218. if (const Shader *shader = Object::cast_to<Shader>(*p_resource)) {
  219. if (shader->is_text_shader()) {
  220. p_extensions->push_back("gdshader");
  221. }
  222. }
  223. }
  224. bool ResourceFormatSaverShader::recognize(const Ref<Resource> &p_resource) const {
  225. return p_resource->get_class_name() == "Shader"; //only shader, not inherited
  226. }