shader.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /**************************************************************************/
  2. /* shader.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.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. // Preprocess and compile the code again because a dependency has changed. It also calls emit_changed() for us.
  42. _recompile();
  43. }
  44. void Shader::_recompile() {
  45. set_code(get_code());
  46. }
  47. void Shader::set_path(const String &p_path, bool p_take_over) {
  48. Resource::set_path(p_path, p_take_over);
  49. RS::get_singleton()->shader_set_path_hint(shader, p_path);
  50. }
  51. void Shader::set_include_path(const String &p_path) {
  52. // Used only if the shader does not have a resource path set,
  53. // for example during loading stage or when created by code.
  54. include_path = p_path;
  55. }
  56. void Shader::set_code(const String &p_code) {
  57. for (const Ref<ShaderInclude> &E : include_dependencies) {
  58. E->disconnect_changed(callable_mp(this, &Shader::_dependency_changed));
  59. }
  60. code = p_code;
  61. String pp_code = p_code;
  62. {
  63. String path = get_path();
  64. if (path.is_empty()) {
  65. path = include_path;
  66. }
  67. // Preprocessor must run here and not in the server because:
  68. // 1) Need to keep track of include dependencies at resource level
  69. // 2) Server does not do interaction with Resource filetypes, this is a scene level feature.
  70. HashSet<Ref<ShaderInclude>> new_include_dependencies;
  71. ShaderPreprocessor preprocessor;
  72. Error result = preprocessor.preprocess(p_code, path, pp_code, nullptr, nullptr, nullptr, &new_include_dependencies);
  73. if (result == OK) {
  74. // This ensures previous include resources are not freed and then re-loaded during parse (which would make compiling slower)
  75. include_dependencies = new_include_dependencies;
  76. }
  77. }
  78. // Try to get the shader type from the final, fully preprocessed shader code.
  79. String type = ShaderLanguage::get_shader_type(pp_code);
  80. if (type == "canvas_item") {
  81. mode = MODE_CANVAS_ITEM;
  82. } else if (type == "particles") {
  83. mode = MODE_PARTICLES;
  84. } else if (type == "sky") {
  85. mode = MODE_SKY;
  86. } else if (type == "fog") {
  87. mode = MODE_FOG;
  88. } else {
  89. mode = MODE_SPATIAL;
  90. }
  91. for (const Ref<ShaderInclude> &E : include_dependencies) {
  92. E->connect_changed(callable_mp(this, &Shader::_dependency_changed));
  93. }
  94. RenderingServer::get_singleton()->shader_set_code(shader, pp_code);
  95. emit_changed();
  96. }
  97. String Shader::get_code() const {
  98. _update_shader();
  99. return code;
  100. }
  101. void Shader::get_shader_uniform_list(List<PropertyInfo> *p_params, bool p_get_groups) const {
  102. _update_shader();
  103. List<PropertyInfo> local;
  104. RenderingServer::get_singleton()->get_shader_parameter_list(shader, &local);
  105. for (PropertyInfo &pi : local) {
  106. bool is_group = pi.usage == PROPERTY_USAGE_GROUP || pi.usage == PROPERTY_USAGE_SUBGROUP;
  107. if (!p_get_groups && is_group) {
  108. continue;
  109. }
  110. if (!is_group) {
  111. if (default_textures.has(pi.name)) { //do not show default textures
  112. continue;
  113. }
  114. }
  115. if (p_params) {
  116. //small little hack
  117. if (pi.type == Variant::RID) {
  118. pi.type = Variant::OBJECT;
  119. }
  120. p_params->push_back(pi);
  121. }
  122. }
  123. }
  124. RID Shader::get_rid() const {
  125. _update_shader();
  126. return shader;
  127. }
  128. void Shader::set_default_texture_parameter(const StringName &p_name, const Ref<Texture2D> &p_texture, int p_index) {
  129. if (p_texture.is_valid()) {
  130. if (!default_textures.has(p_name)) {
  131. default_textures[p_name] = HashMap<int, Ref<Texture2D>>();
  132. }
  133. default_textures[p_name][p_index] = p_texture;
  134. RS::get_singleton()->shader_set_default_texture_parameter(shader, p_name, p_texture->get_rid(), p_index);
  135. } else {
  136. if (default_textures.has(p_name) && default_textures[p_name].has(p_index)) {
  137. default_textures[p_name].erase(p_index);
  138. if (default_textures[p_name].is_empty()) {
  139. default_textures.erase(p_name);
  140. }
  141. }
  142. RS::get_singleton()->shader_set_default_texture_parameter(shader, p_name, RID(), p_index);
  143. }
  144. emit_changed();
  145. }
  146. Ref<Texture2D> Shader::get_default_texture_parameter(const StringName &p_name, int p_index) const {
  147. if (default_textures.has(p_name) && default_textures[p_name].has(p_index)) {
  148. return default_textures[p_name][p_index];
  149. }
  150. return Ref<Texture2D>();
  151. }
  152. void Shader::get_default_texture_parameter_list(List<StringName> *r_textures) const {
  153. for (const KeyValue<StringName, HashMap<int, Ref<Texture2D>>> &E : default_textures) {
  154. r_textures->push_back(E.key);
  155. }
  156. }
  157. bool Shader::is_text_shader() const {
  158. return true;
  159. }
  160. void Shader::_update_shader() const {
  161. }
  162. Array Shader::_get_shader_uniform_list(bool p_get_groups) {
  163. List<PropertyInfo> uniform_list;
  164. get_shader_uniform_list(&uniform_list, p_get_groups);
  165. Array ret;
  166. for (const PropertyInfo &pi : uniform_list) {
  167. ret.push_back(pi.operator Dictionary());
  168. }
  169. return ret;
  170. }
  171. void Shader::_bind_methods() {
  172. ClassDB::bind_method(D_METHOD("get_mode"), &Shader::get_mode);
  173. ClassDB::bind_method(D_METHOD("set_code", "code"), &Shader::set_code);
  174. ClassDB::bind_method(D_METHOD("get_code"), &Shader::get_code);
  175. ClassDB::bind_method(D_METHOD("set_default_texture_parameter", "name", "texture", "index"), &Shader::set_default_texture_parameter, DEFVAL(0));
  176. ClassDB::bind_method(D_METHOD("get_default_texture_parameter", "name", "index"), &Shader::get_default_texture_parameter, DEFVAL(0));
  177. ClassDB::bind_method(D_METHOD("get_shader_uniform_list", "get_groups"), &Shader::_get_shader_uniform_list, DEFVAL(false));
  178. ADD_PROPERTY(PropertyInfo(Variant::STRING, "code", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_code", "get_code");
  179. BIND_ENUM_CONSTANT(MODE_SPATIAL);
  180. BIND_ENUM_CONSTANT(MODE_CANVAS_ITEM);
  181. BIND_ENUM_CONSTANT(MODE_PARTICLES);
  182. BIND_ENUM_CONSTANT(MODE_SKY);
  183. BIND_ENUM_CONSTANT(MODE_FOG);
  184. }
  185. Shader::Shader() {
  186. shader = RenderingServer::get_singleton()->shader_create();
  187. }
  188. Shader::~Shader() {
  189. ERR_FAIL_NULL(RenderingServer::get_singleton());
  190. RenderingServer::get_singleton()->free(shader);
  191. }
  192. ////////////
  193. 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) {
  194. if (r_error) {
  195. *r_error = ERR_FILE_CANT_OPEN;
  196. }
  197. Error error = OK;
  198. Vector<uint8_t> buffer = FileAccess::get_file_as_bytes(p_path, &error);
  199. ERR_FAIL_COND_V_MSG(error, nullptr, "Cannot load shader: " + p_path);
  200. String str;
  201. if (buffer.size() > 0) {
  202. error = str.parse_utf8((const char *)buffer.ptr(), buffer.size());
  203. ERR_FAIL_COND_V_MSG(error, nullptr, "Cannot parse shader: " + p_path);
  204. }
  205. Ref<Shader> shader;
  206. shader.instantiate();
  207. shader->set_include_path(p_path);
  208. shader->set_code(str);
  209. if (r_error) {
  210. *r_error = OK;
  211. }
  212. return shader;
  213. }
  214. void ResourceFormatLoaderShader::get_recognized_extensions(List<String> *p_extensions) const {
  215. p_extensions->push_back("gdshader");
  216. }
  217. bool ResourceFormatLoaderShader::handles_type(const String &p_type) const {
  218. return (p_type == "Shader");
  219. }
  220. String ResourceFormatLoaderShader::get_resource_type(const String &p_path) const {
  221. String el = p_path.get_extension().to_lower();
  222. if (el == "gdshader") {
  223. return "Shader";
  224. }
  225. return "";
  226. }
  227. Error ResourceFormatSaverShader::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {
  228. Ref<Shader> shader = p_resource;
  229. ERR_FAIL_COND_V(shader.is_null(), ERR_INVALID_PARAMETER);
  230. String source = shader->get_code();
  231. Error err;
  232. Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);
  233. ERR_FAIL_COND_V_MSG(err, err, "Cannot save shader '" + p_path + "'.");
  234. file->store_string(source);
  235. if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) {
  236. return ERR_CANT_CREATE;
  237. }
  238. return OK;
  239. }
  240. void ResourceFormatSaverShader::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const {
  241. if (const Shader *shader = Object::cast_to<Shader>(*p_resource)) {
  242. if (shader->is_text_shader()) {
  243. p_extensions->push_back("gdshader");
  244. }
  245. }
  246. }
  247. bool ResourceFormatSaverShader::recognize(const Ref<Resource> &p_resource) const {
  248. return p_resource->get_class_name() == "Shader"; //only shader, not inherited
  249. }