shader.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*************************************************************************/
  2. /* shader.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "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_signal(SceneStringNames::get_singleton()->changed);
  51. }
  52. String Shader::get_code() const {
  53. return VisualServer::get_singleton()->shader_get_code(shader);
  54. }
  55. void Shader::get_param_list(List<PropertyInfo> *p_params) const {
  56. List<PropertyInfo> local;
  57. VisualServer::get_singleton()->shader_get_param_list(shader, &local);
  58. params_cache.clear();
  59. params_cache_dirty = false;
  60. for (List<PropertyInfo>::Element *E = local.front(); E; E = E->next()) {
  61. PropertyInfo pi = E->get();
  62. pi.name = "shader_param/" + pi.name;
  63. params_cache[pi.name] = E->get().name;
  64. if (p_params) {
  65. //small little hack
  66. if (pi.type == Variant::_RID)
  67. pi.type = Variant::OBJECT;
  68. p_params->push_back(pi);
  69. }
  70. }
  71. }
  72. RID Shader::get_rid() const {
  73. return shader;
  74. }
  75. void Shader::set_default_texture_param(const StringName &p_param, const Ref<Texture> &p_texture) {
  76. if (p_texture.is_valid()) {
  77. default_textures[p_param] = p_texture;
  78. VS::get_singleton()->shader_set_default_texture_param(shader, p_param, p_texture->get_rid());
  79. } else {
  80. default_textures.erase(p_param);
  81. VS::get_singleton()->shader_set_default_texture_param(shader, p_param, RID());
  82. }
  83. }
  84. Ref<Texture> Shader::get_default_texture_param(const StringName &p_param) const {
  85. if (default_textures.has(p_param))
  86. return default_textures[p_param];
  87. else
  88. return Ref<Texture>();
  89. }
  90. void Shader::get_default_texture_param_list(List<StringName> *r_textures) const {
  91. for (const Map<StringName, Ref<Texture> >::Element *E = default_textures.front(); E; E = E->next()) {
  92. r_textures->push_back(E->key());
  93. }
  94. }
  95. bool Shader::has_param(const StringName &p_param) const {
  96. return params_cache.has(p_param);
  97. }
  98. void Shader::_bind_methods() {
  99. ClassDB::bind_method(D_METHOD("get_mode"), &Shader::get_mode);
  100. ClassDB::bind_method(D_METHOD("set_code", "code"), &Shader::set_code);
  101. ClassDB::bind_method(D_METHOD("get_code"), &Shader::get_code);
  102. ClassDB::bind_method(D_METHOD("set_default_texture_param", "param", "texture"), &Shader::set_default_texture_param);
  103. ClassDB::bind_method(D_METHOD("get_default_texture_param", "param"), &Shader::get_default_texture_param);
  104. ClassDB::bind_method(D_METHOD("has_param", "name"), &Shader::has_param);
  105. //ClassDB::bind_method(D_METHOD("get_param_list"),&Shader::get_fragment_code);
  106. ADD_PROPERTY(PropertyInfo(Variant::STRING, "code", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_code", "get_code");
  107. BIND_ENUM_CONSTANT(MODE_SPATIAL);
  108. BIND_ENUM_CONSTANT(MODE_CANVAS_ITEM);
  109. BIND_ENUM_CONSTANT(MODE_PARTICLES);
  110. }
  111. Shader::Shader() {
  112. mode = MODE_SPATIAL;
  113. shader = VisualServer::get_singleton()->shader_create();
  114. params_cache_dirty = true;
  115. }
  116. Shader::~Shader() {
  117. VisualServer::get_singleton()->free(shader);
  118. }