shader_globals_override.cpp 9.3 KB

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