compositor_storage.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**************************************************************************/
  2. /* compositor_storage.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 "compositor_storage.h"
  31. // Storage
  32. RendererCompositorStorage *RendererCompositorStorage::singleton = nullptr;
  33. RendererCompositorStorage::RendererCompositorStorage() {
  34. singleton = this;
  35. }
  36. RendererCompositorStorage::~RendererCompositorStorage() {
  37. singleton = nullptr;
  38. }
  39. // Compositor effect
  40. RID RendererCompositorStorage::compositor_effect_allocate() {
  41. return compositor_effects_owner.allocate_rid();
  42. }
  43. void RendererCompositorStorage::compositor_effect_initialize(RID p_rid) {
  44. compositor_effects_owner.initialize_rid(p_rid, CompositorEffect());
  45. }
  46. void RendererCompositorStorage::compositor_effect_free(RID p_rid) {
  47. CompositorEffect *effect = compositor_effects_owner.get_or_null(p_rid);
  48. ERR_FAIL_NULL(effect);
  49. // Remove this RID from any compositor that uses it.
  50. List<RID> compositor_rids;
  51. compositor_owner.get_owned_list(&compositor_rids);
  52. for (const RID &compositor_rid : compositor_rids) {
  53. Compositor *compositor = compositor_owner.get_or_null(compositor_rid);
  54. if (compositor) {
  55. compositor->compositor_effects.erase(p_rid);
  56. }
  57. }
  58. // Update motion vector count if needed.
  59. if (effect->is_enabled && effect->flags.has_flag(RS::CompositorEffectFlags::COMPOSITOR_EFFECT_FLAG_NEEDS_MOTION_VECTORS)) {
  60. num_compositor_effects_with_motion_vectors--;
  61. }
  62. compositor_effects_owner.free(p_rid);
  63. }
  64. void RendererCompositorStorage::compositor_effect_set_callback(RID p_effect, RS::CompositorEffectCallbackType p_callback_type, const Callable &p_callback) {
  65. CompositorEffect *effect = compositor_effects_owner.get_or_null(p_effect);
  66. ERR_FAIL_NULL(effect);
  67. effect->callback_type = p_callback_type;
  68. effect->callback = p_callback;
  69. }
  70. void RendererCompositorStorage::compositor_effect_set_enabled(RID p_effect, bool p_enabled) {
  71. CompositorEffect *effect = compositor_effects_owner.get_or_null(p_effect);
  72. ERR_FAIL_NULL(effect);
  73. if (effect->is_enabled != p_enabled && effect->flags.has_flag(RS::CompositorEffectFlags::COMPOSITOR_EFFECT_FLAG_NEEDS_MOTION_VECTORS)) {
  74. if (p_enabled) {
  75. num_compositor_effects_with_motion_vectors++;
  76. } else {
  77. num_compositor_effects_with_motion_vectors--;
  78. }
  79. }
  80. effect->is_enabled = p_enabled;
  81. }
  82. bool RendererCompositorStorage::compositor_effect_get_enabled(RID p_effect) const {
  83. CompositorEffect *effect = compositor_effects_owner.get_or_null(p_effect);
  84. ERR_FAIL_NULL_V(effect, false);
  85. return effect->is_enabled;
  86. }
  87. RS::CompositorEffectCallbackType RendererCompositorStorage::compositor_effect_get_callback_type(RID p_effect) const {
  88. CompositorEffect *effect = compositor_effects_owner.get_or_null(p_effect);
  89. ERR_FAIL_NULL_V(effect, RS::COMPOSITOR_EFFECT_CALLBACK_TYPE_MAX);
  90. return effect->callback_type;
  91. }
  92. Callable RendererCompositorStorage::compositor_effect_get_callback(RID p_effect) const {
  93. CompositorEffect *effect = compositor_effects_owner.get_or_null(p_effect);
  94. ERR_FAIL_NULL_V(effect, Callable());
  95. return effect->callback;
  96. }
  97. void RendererCompositorStorage::compositor_effect_set_flag(RID p_effect, RS::CompositorEffectFlags p_flag, bool p_set) {
  98. CompositorEffect *effect = compositor_effects_owner.get_or_null(p_effect);
  99. ERR_FAIL_NULL(effect);
  100. if (effect->is_enabled && p_flag == RS::CompositorEffectFlags::COMPOSITOR_EFFECT_FLAG_NEEDS_MOTION_VECTORS) {
  101. bool was_set = effect->flags.has_flag(RS::CompositorEffectFlags::COMPOSITOR_EFFECT_FLAG_NEEDS_MOTION_VECTORS);
  102. if (was_set != p_set) {
  103. if (p_set) {
  104. num_compositor_effects_with_motion_vectors++;
  105. } else {
  106. num_compositor_effects_with_motion_vectors--;
  107. }
  108. }
  109. }
  110. if (p_set) {
  111. effect->flags.set_flag(p_flag);
  112. } else {
  113. effect->flags.clear_flag(p_flag);
  114. }
  115. }
  116. bool RendererCompositorStorage::compositor_effect_get_flag(RID p_effect, RS::CompositorEffectFlags p_flag) const {
  117. CompositorEffect *effect = compositor_effects_owner.get_or_null(p_effect);
  118. ERR_FAIL_NULL_V(effect, false);
  119. return effect->flags.has_flag(p_flag);
  120. }
  121. // Compositor
  122. RID RendererCompositorStorage::compositor_allocate() {
  123. return compositor_owner.allocate_rid();
  124. }
  125. void RendererCompositorStorage::compositor_initialize(RID p_rid) {
  126. compositor_owner.initialize_rid(p_rid, Compositor());
  127. }
  128. void RendererCompositorStorage::compositor_free(RID p_rid) {
  129. compositor_owner.free(p_rid);
  130. }
  131. // compositor effects
  132. void RendererCompositorStorage::compositor_set_compositor_effects(RID p_compositor, const Vector<RID> &p_effects) {
  133. Compositor *compositor = compositor_owner.get_or_null(p_compositor);
  134. ERR_FAIL_NULL(compositor);
  135. compositor->compositor_effects.clear();
  136. for (const RID &effect : p_effects) {
  137. if (is_compositor_effect(effect)) {
  138. compositor->compositor_effects.push_back(effect);
  139. }
  140. }
  141. }
  142. Vector<RID> RendererCompositorStorage::compositor_get_compositor_effects(RID p_compositor, RS::CompositorEffectCallbackType p_callback_type, bool p_enabled_only) const {
  143. Compositor *compositor = compositor_owner.get_or_null(p_compositor);
  144. ERR_FAIL_NULL_V(compositor, Vector<RID>());
  145. if (p_enabled_only || p_callback_type != RS::COMPOSITOR_EFFECT_CALLBACK_TYPE_ANY) {
  146. Vector<RID> effects;
  147. for (RID rid : compositor->compositor_effects) {
  148. if ((!p_enabled_only || compositor_effect_get_enabled(rid)) && (p_callback_type == RS::COMPOSITOR_EFFECT_CALLBACK_TYPE_ANY || compositor_effect_get_callback_type(rid) == p_callback_type)) {
  149. effects.push_back(rid);
  150. }
  151. }
  152. return effects;
  153. } else {
  154. return compositor->compositor_effects;
  155. }
  156. }