shader.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*************************************************************************/
  2. /* shader.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/os/file_access.h"
  32. #include "scene/scene_string_names.h"
  33. #include "servers/visual/shader_language.h"
  34. #include "servers/visual_server.h"
  35. #include "texture.h"
  36. Shader::Mode Shader::get_mode() const {
  37. return mode;
  38. }
  39. void Shader::set_code(const String &p_code) {
  40. String type = ShaderLanguage::get_shader_type(p_code);
  41. if (type == "canvas_item") {
  42. mode = MODE_CANVAS_ITEM;
  43. } else if (type == "particles") {
  44. mode = MODE_PARTICLES;
  45. } else {
  46. mode = MODE_SPATIAL;
  47. }
  48. VisualServer::get_singleton()->shader_set_code(shader, p_code);
  49. params_cache_dirty = true;
  50. emit_changed();
  51. }
  52. String Shader::get_code() const {
  53. _update_shader();
  54. return VisualServer::get_singleton()->shader_get_code(shader);
  55. }
  56. void Shader::get_param_list(List<PropertyInfo> *p_params) const {
  57. _update_shader();
  58. List<PropertyInfo> local;
  59. VisualServer::get_singleton()->shader_get_param_list(shader, &local);
  60. params_cache.clear();
  61. params_cache_dirty = false;
  62. for (List<PropertyInfo>::Element *E = local.front(); E; E = E->next()) {
  63. PropertyInfo pi = E->get();
  64. if (default_textures.has(pi.name)) { //do not show default textures
  65. continue;
  66. }
  67. pi.name = "shader_param/" + pi.name;
  68. params_cache[pi.name] = E->get().name;
  69. if (p_params) {
  70. //small little hack
  71. if (pi.type == Variant::_RID)
  72. pi.type = Variant::OBJECT;
  73. p_params->push_back(pi);
  74. }
  75. }
  76. }
  77. RID Shader::get_rid() const {
  78. _update_shader();
  79. return shader;
  80. }
  81. void Shader::set_default_texture_param(const StringName &p_param, const Ref<Texture> &p_texture) {
  82. if (p_texture.is_valid()) {
  83. default_textures[p_param] = p_texture;
  84. VS::get_singleton()->shader_set_default_texture_param(shader, p_param, p_texture->get_rid());
  85. } else {
  86. default_textures.erase(p_param);
  87. VS::get_singleton()->shader_set_default_texture_param(shader, p_param, RID());
  88. }
  89. emit_changed();
  90. }
  91. Ref<Texture> Shader::get_default_texture_param(const StringName &p_param) const {
  92. if (default_textures.has(p_param))
  93. return default_textures[p_param];
  94. else
  95. return Ref<Texture>();
  96. }
  97. void Shader::get_default_texture_param_list(List<StringName> *r_textures) const {
  98. for (const Map<StringName, Ref<Texture> >::Element *E = default_textures.front(); E; E = E->next()) {
  99. r_textures->push_back(E->key());
  100. }
  101. }
  102. void Shader::set_custom_defines(const String &p_defines) {
  103. if (shader_custom_defines == p_defines) {
  104. return;
  105. }
  106. if (!shader_custom_defines.empty()) {
  107. VS::get_singleton()->shader_remove_custom_define(shader, shader_custom_defines);
  108. }
  109. shader_custom_defines = p_defines;
  110. VS::get_singleton()->shader_add_custom_define(shader, shader_custom_defines);
  111. }
  112. String Shader::get_custom_defines() const {
  113. return shader_custom_defines;
  114. }
  115. bool Shader::is_text_shader() const {
  116. return true;
  117. }
  118. bool Shader::has_param(const StringName &p_param) const {
  119. return params_cache.has("shader_param/" + p_param);
  120. }
  121. void Shader::_update_shader() const {
  122. }
  123. void Shader::_bind_methods() {
  124. ClassDB::bind_method(D_METHOD("get_mode"), &Shader::get_mode);
  125. ClassDB::bind_method(D_METHOD("set_code", "code"), &Shader::set_code);
  126. ClassDB::bind_method(D_METHOD("get_code"), &Shader::get_code);
  127. ClassDB::bind_method(D_METHOD("set_default_texture_param", "param", "texture"), &Shader::set_default_texture_param);
  128. ClassDB::bind_method(D_METHOD("get_default_texture_param", "param"), &Shader::get_default_texture_param);
  129. ClassDB::bind_method(D_METHOD("set_custom_defines", "custom_defines"), &Shader::set_custom_defines);
  130. ClassDB::bind_method(D_METHOD("get_custom_defines"), &Shader::get_custom_defines);
  131. ClassDB::bind_method(D_METHOD("has_param", "name"), &Shader::has_param);
  132. //ClassDB::bind_method(D_METHOD("get_param_list"),&Shader::get_fragment_code);
  133. ADD_PROPERTY(PropertyInfo(Variant::STRING, "code", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_code", "get_code");
  134. ADD_PROPERTY(PropertyInfo(Variant::STRING, "custom_defines", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_custom_defines", "get_custom_defines");
  135. BIND_ENUM_CONSTANT(MODE_SPATIAL);
  136. BIND_ENUM_CONSTANT(MODE_CANVAS_ITEM);
  137. BIND_ENUM_CONSTANT(MODE_PARTICLES);
  138. }
  139. Shader::Shader() {
  140. mode = MODE_SPATIAL;
  141. shader = VisualServer::get_singleton()->shader_create();
  142. params_cache_dirty = true;
  143. }
  144. Shader::~Shader() {
  145. VisualServer::get_singleton()->free(shader);
  146. }
  147. ////////////
  148. RES ResourceFormatLoaderShader::load(const String &p_path, const String &p_original_path, Error *r_error) {
  149. if (r_error)
  150. *r_error = ERR_FILE_CANT_OPEN;
  151. Ref<Shader> shader;
  152. shader.instance();
  153. Vector<uint8_t> buffer = FileAccess::get_file_as_array(p_path);
  154. String str;
  155. str.parse_utf8((const char *)buffer.ptr(), buffer.size());
  156. shader->set_code(str);
  157. if (r_error)
  158. *r_error = OK;
  159. return shader;
  160. }
  161. void ResourceFormatLoaderShader::get_recognized_extensions(List<String> *p_extensions) const {
  162. p_extensions->push_back("shader");
  163. }
  164. bool ResourceFormatLoaderShader::handles_type(const String &p_type) const {
  165. return (p_type == "Shader");
  166. }
  167. String ResourceFormatLoaderShader::get_resource_type(const String &p_path) const {
  168. String el = p_path.get_extension().to_lower();
  169. if (el == "shader")
  170. return "Shader";
  171. return "";
  172. }
  173. Error ResourceFormatSaverShader::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  174. Ref<Shader> shader = p_resource;
  175. ERR_FAIL_COND_V(shader.is_null(), ERR_INVALID_PARAMETER);
  176. String source = shader->get_code();
  177. Error err;
  178. FileAccess *file = FileAccess::open(p_path, FileAccess::WRITE, &err);
  179. ERR_FAIL_COND_V_MSG(err, err, "Cannot save shader '" + p_path + "'.");
  180. file->store_string(source);
  181. if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) {
  182. memdelete(file);
  183. return ERR_CANT_CREATE;
  184. }
  185. file->close();
  186. memdelete(file);
  187. return OK;
  188. }
  189. void ResourceFormatSaverShader::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  190. if (const Shader *shader = Object::cast_to<Shader>(*p_resource)) {
  191. if (shader->is_text_shader()) {
  192. p_extensions->push_back("shader");
  193. }
  194. }
  195. }
  196. bool ResourceFormatSaverShader::recognize(const RES &p_resource) const {
  197. return p_resource->get_class_name() == "Shader"; //only shader, not inherited
  198. }