shader.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*************************************************************************/
  2. /* shader.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. bool Shader::is_text_shader() const {
  103. return true;
  104. }
  105. bool Shader::has_param(const StringName &p_param) const {
  106. return params_cache.has(p_param);
  107. }
  108. void Shader::_update_shader() const {
  109. }
  110. void Shader::_bind_methods() {
  111. ClassDB::bind_method(D_METHOD("get_mode"), &Shader::get_mode);
  112. ClassDB::bind_method(D_METHOD("set_code", "code"), &Shader::set_code);
  113. ClassDB::bind_method(D_METHOD("get_code"), &Shader::get_code);
  114. ClassDB::bind_method(D_METHOD("set_default_texture_param", "param", "texture"), &Shader::set_default_texture_param);
  115. ClassDB::bind_method(D_METHOD("get_default_texture_param", "param"), &Shader::get_default_texture_param);
  116. ClassDB::bind_method(D_METHOD("has_param", "name"), &Shader::has_param);
  117. //ClassDB::bind_method(D_METHOD("get_param_list"),&Shader::get_fragment_code);
  118. ADD_PROPERTY(PropertyInfo(Variant::STRING, "code", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_code", "get_code");
  119. BIND_ENUM_CONSTANT(MODE_SPATIAL);
  120. BIND_ENUM_CONSTANT(MODE_CANVAS_ITEM);
  121. BIND_ENUM_CONSTANT(MODE_PARTICLES);
  122. }
  123. Shader::Shader() {
  124. mode = MODE_SPATIAL;
  125. shader = VisualServer::get_singleton()->shader_create();
  126. params_cache_dirty = true;
  127. }
  128. Shader::~Shader() {
  129. VisualServer::get_singleton()->free(shader);
  130. }
  131. ////////////
  132. RES ResourceFormatLoaderShader::load(const String &p_path, const String &p_original_path, Error *r_error) {
  133. if (r_error)
  134. *r_error = ERR_FILE_CANT_OPEN;
  135. Ref<Shader> shader;
  136. shader.instance();
  137. Vector<uint8_t> buffer = FileAccess::get_file_as_array(p_path);
  138. String str;
  139. str.parse_utf8((const char *)buffer.ptr(), buffer.size());
  140. shader->set_code(str);
  141. if (r_error)
  142. *r_error = OK;
  143. return shader;
  144. }
  145. void ResourceFormatLoaderShader::get_recognized_extensions(List<String> *p_extensions) const {
  146. p_extensions->push_back("shader");
  147. }
  148. bool ResourceFormatLoaderShader::handles_type(const String &p_type) const {
  149. return (p_type == "Shader");
  150. }
  151. String ResourceFormatLoaderShader::get_resource_type(const String &p_path) const {
  152. String el = p_path.get_extension().to_lower();
  153. if (el == "shader")
  154. return "Shader";
  155. return "";
  156. }
  157. Error ResourceFormatSaverShader::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  158. Ref<Shader> shader = p_resource;
  159. ERR_FAIL_COND_V(shader.is_null(), ERR_INVALID_PARAMETER);
  160. String source = shader->get_code();
  161. Error err;
  162. FileAccess *file = FileAccess::open(p_path, FileAccess::WRITE, &err);
  163. if (err) {
  164. ERR_FAIL_COND_V(err, err);
  165. }
  166. file->store_string(source);
  167. if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) {
  168. memdelete(file);
  169. return ERR_CANT_CREATE;
  170. }
  171. file->close();
  172. memdelete(file);
  173. return OK;
  174. }
  175. void ResourceFormatSaverShader::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  176. if (const Shader *shader = Object::cast_to<Shader>(*p_resource)) {
  177. if (shader->is_text_shader()) {
  178. p_extensions->push_back("shader");
  179. }
  180. }
  181. }
  182. bool ResourceFormatSaverShader::recognize(const RES &p_resource) const {
  183. return p_resource->get_class_name() == "Shader"; //only shader, not inherited
  184. }