shader.cpp 9.9 KB

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