shader_globals_override.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /**************************************************************************/
  2. /* shader_globals_override.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_globals_override.h"
  31. #include "scene/3d/node_3d.h"
  32. #include "scene/scene_string_names.h"
  33. StringName *ShaderGlobalsOverride::_remap(const StringName &p_name) const {
  34. StringName *r = param_remaps.getptr(p_name);
  35. if (!r) {
  36. //not cached, do caching
  37. String p = p_name;
  38. if (p.begins_with("params/")) {
  39. String q = p.replace_first("params/", "");
  40. param_remaps[p] = q;
  41. r = param_remaps.getptr(p);
  42. }
  43. }
  44. return r;
  45. }
  46. bool ShaderGlobalsOverride::_set(const StringName &p_name, const Variant &p_value) {
  47. StringName *r = _remap(p_name);
  48. if (r) {
  49. Override *o = overrides.getptr(*r);
  50. if (!o) {
  51. Override ov;
  52. ov.in_use = false;
  53. overrides[*r] = ov;
  54. o = overrides.getptr(*r);
  55. }
  56. if (o) {
  57. o->override = p_value;
  58. if (active) {
  59. if (o->override.get_type() == Variant::OBJECT) {
  60. RID tex_rid = p_value;
  61. RS::get_singleton()->global_shader_parameter_set_override(*r, tex_rid);
  62. } else {
  63. RS::get_singleton()->global_shader_parameter_set_override(*r, p_value);
  64. }
  65. }
  66. o->in_use = p_value.get_type() != Variant::NIL;
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. bool ShaderGlobalsOverride::_get(const StringName &p_name, Variant &r_ret) const {
  73. StringName *r = _remap(p_name);
  74. if (r) {
  75. const Override *o = overrides.getptr(*r);
  76. if (o) {
  77. r_ret = o->override;
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83. void ShaderGlobalsOverride::_get_property_list(List<PropertyInfo> *p_list) const {
  84. Vector<StringName> variables;
  85. variables = RS::get_singleton()->global_shader_parameter_get_list();
  86. for (int i = 0; i < variables.size(); i++) {
  87. PropertyInfo pinfo;
  88. pinfo.name = "params/" + variables[i];
  89. pinfo.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
  90. switch (RS::get_singleton()->global_shader_parameter_get_type(variables[i])) {
  91. case RS::GLOBAL_VAR_TYPE_BOOL: {
  92. pinfo.type = Variant::BOOL;
  93. } break;
  94. case RS::GLOBAL_VAR_TYPE_BVEC2: {
  95. pinfo.type = Variant::INT;
  96. pinfo.hint = PROPERTY_HINT_FLAGS;
  97. pinfo.hint_string = "x,y";
  98. } break;
  99. case RS::GLOBAL_VAR_TYPE_BVEC3: {
  100. pinfo.type = Variant::INT;
  101. pinfo.hint = PROPERTY_HINT_FLAGS;
  102. pinfo.hint_string = "x,y,z";
  103. } break;
  104. case RS::GLOBAL_VAR_TYPE_BVEC4: {
  105. pinfo.type = Variant::INT;
  106. pinfo.hint = PROPERTY_HINT_FLAGS;
  107. pinfo.hint_string = "x,y,z,w";
  108. } break;
  109. case RS::GLOBAL_VAR_TYPE_INT: {
  110. pinfo.type = Variant::INT;
  111. } break;
  112. case RS::GLOBAL_VAR_TYPE_IVEC2: {
  113. pinfo.type = Variant::VECTOR2I;
  114. } break;
  115. case RS::GLOBAL_VAR_TYPE_IVEC3: {
  116. pinfo.type = Variant::VECTOR3I;
  117. } break;
  118. case RS::GLOBAL_VAR_TYPE_IVEC4: {
  119. pinfo.type = Variant::VECTOR4I;
  120. } break;
  121. case RS::GLOBAL_VAR_TYPE_RECT2I: {
  122. pinfo.type = Variant::RECT2I;
  123. } break;
  124. case RS::GLOBAL_VAR_TYPE_UINT: {
  125. pinfo.type = Variant::INT;
  126. } break;
  127. case RS::GLOBAL_VAR_TYPE_UVEC2: {
  128. pinfo.type = Variant::VECTOR2I;
  129. } break;
  130. case RS::GLOBAL_VAR_TYPE_UVEC3: {
  131. pinfo.type = Variant::VECTOR3I;
  132. } break;
  133. case RS::GLOBAL_VAR_TYPE_UVEC4: {
  134. pinfo.type = Variant::VECTOR4I;
  135. } break;
  136. case RS::GLOBAL_VAR_TYPE_FLOAT: {
  137. pinfo.type = Variant::FLOAT;
  138. } break;
  139. case RS::GLOBAL_VAR_TYPE_VEC2: {
  140. pinfo.type = Variant::VECTOR2;
  141. } break;
  142. case RS::GLOBAL_VAR_TYPE_VEC3: {
  143. pinfo.type = Variant::VECTOR3;
  144. } break;
  145. case RS::GLOBAL_VAR_TYPE_VEC4: {
  146. pinfo.type = Variant::VECTOR4;
  147. } break;
  148. case RS::GLOBAL_VAR_TYPE_RECT2: {
  149. pinfo.type = Variant::RECT2;
  150. } break;
  151. case RS::GLOBAL_VAR_TYPE_COLOR: {
  152. pinfo.type = Variant::COLOR;
  153. } break;
  154. case RS::GLOBAL_VAR_TYPE_MAT2: {
  155. pinfo.type = Variant::PACKED_FLOAT32_ARRAY;
  156. } break;
  157. case RS::GLOBAL_VAR_TYPE_MAT3: {
  158. pinfo.type = Variant::BASIS;
  159. } break;
  160. case RS::GLOBAL_VAR_TYPE_MAT4: {
  161. pinfo.type = Variant::PROJECTION;
  162. } break;
  163. case RS::GLOBAL_VAR_TYPE_TRANSFORM_2D: {
  164. pinfo.type = Variant::TRANSFORM2D;
  165. } break;
  166. case RS::GLOBAL_VAR_TYPE_TRANSFORM: {
  167. pinfo.type = Variant::TRANSFORM3D;
  168. } break;
  169. case RS::GLOBAL_VAR_TYPE_SAMPLER2D: {
  170. pinfo.type = Variant::OBJECT;
  171. pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
  172. pinfo.hint_string = "Texture2D";
  173. } break;
  174. case RS::GLOBAL_VAR_TYPE_SAMPLER2DARRAY: {
  175. pinfo.type = Variant::OBJECT;
  176. pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
  177. pinfo.hint_string = "Texture2DArray";
  178. } break;
  179. case RS::GLOBAL_VAR_TYPE_SAMPLER3D: {
  180. pinfo.type = Variant::OBJECT;
  181. pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
  182. pinfo.hint_string = "Texture3D";
  183. } break;
  184. case RS::GLOBAL_VAR_TYPE_SAMPLERCUBE: {
  185. pinfo.type = Variant::OBJECT;
  186. pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
  187. pinfo.hint_string = "Cubemap";
  188. } break;
  189. default: {
  190. } break;
  191. }
  192. if (!overrides.has(variables[i])) {
  193. Override o;
  194. o.in_use = false;
  195. Callable::CallError ce;
  196. Variant::construct(pinfo.type, o.override, nullptr, 0, ce);
  197. overrides[variables[i]] = o;
  198. }
  199. Override *o = overrides.getptr(variables[i]);
  200. if (o->in_use && o->override.get_type() != Variant::NIL) {
  201. pinfo.usage |= PROPERTY_USAGE_CHECKED;
  202. pinfo.usage |= PROPERTY_USAGE_STORAGE;
  203. }
  204. p_list->push_back(pinfo);
  205. }
  206. }
  207. void ShaderGlobalsOverride::_activate() {
  208. ERR_FAIL_NULL(get_tree());
  209. List<Node *> nodes;
  210. get_tree()->get_nodes_in_group(SceneStringNames::get_singleton()->shader_overrides_group_active, &nodes);
  211. if (nodes.size() == 0) {
  212. //good we are the only override, enable all
  213. active = true;
  214. add_to_group(SceneStringNames::get_singleton()->shader_overrides_group_active);
  215. for (const KeyValue<StringName, Override> &E : overrides) {
  216. const Override *o = &E.value;
  217. if (o->in_use && o->override.get_type() != Variant::NIL) {
  218. if (o->override.get_type() == Variant::OBJECT) {
  219. RID tex_rid = o->override;
  220. RS::get_singleton()->global_shader_parameter_set_override(E.key, tex_rid);
  221. } else {
  222. RS::get_singleton()->global_shader_parameter_set_override(E.key, o->override);
  223. }
  224. }
  225. update_configuration_warnings(); //may have activated
  226. }
  227. }
  228. }
  229. void ShaderGlobalsOverride::_notification(int p_what) {
  230. switch (p_what) {
  231. case Node3D::NOTIFICATION_ENTER_TREE: {
  232. add_to_group(SceneStringNames::get_singleton()->shader_overrides_group);
  233. _activate();
  234. } break;
  235. case Node3D::NOTIFICATION_EXIT_TREE: {
  236. if (active) {
  237. //remove overrides
  238. for (const KeyValue<StringName, Override> &E : overrides) {
  239. const Override *o = &E.value;
  240. if (o->in_use) {
  241. RS::get_singleton()->global_shader_parameter_set_override(E.key, Variant());
  242. }
  243. }
  244. }
  245. remove_from_group(SceneStringNames::get_singleton()->shader_overrides_group_active);
  246. remove_from_group(SceneStringNames::get_singleton()->shader_overrides_group);
  247. get_tree()->call_group_flags(SceneTree::GROUP_CALL_DEFERRED, SceneStringNames::get_singleton()->shader_overrides_group, "_activate"); //another may want to activate when this is removed
  248. active = false;
  249. } break;
  250. }
  251. }
  252. PackedStringArray ShaderGlobalsOverride::get_configuration_warnings() const {
  253. PackedStringArray warnings = Node::get_configuration_warnings();
  254. if (!active) {
  255. warnings.push_back(RTR("ShaderGlobalsOverride is not active because another node of the same type is in the scene."));
  256. }
  257. return warnings;
  258. }
  259. void ShaderGlobalsOverride::_bind_methods() {
  260. ClassDB::bind_method(D_METHOD("_activate"), &ShaderGlobalsOverride::_activate);
  261. }
  262. ShaderGlobalsOverride::ShaderGlobalsOverride() {}