2
0

compositor_storage.cpp 7.4 KB

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