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