Debian Codegraph @

scene/resources/compositor.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* compositor.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.h"
32
33 #include "servers/rendering/rendering_server.h"
34
35 /* Compositor Effect */
36
37 void CompositorEffect::_bind_methods() {
38 ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &CompositorEffect::set_enabled);
39 ClassDB::bind_method(D_METHOD("get_enabled"), &CompositorEffect::get_enabled);
40 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "get_enabled");
41
42 ClassDB::bind_method(D_METHOD("set_effect_callback_type", "effect_callback_type"), &CompositorEffect::set_effect_callback_type);
43 ClassDB::bind_method(D_METHOD("get_effect_callback_type"), &CompositorEffect::get_effect_callback_type);
44 ADD_PROPERTY(PropertyInfo(Variant::INT, "effect_callback_type", PROPERTY_HINT_ENUM, "Pre Opaque,Post Opaque,Post Sky,Pre Transparent,Post Transparent"), "set_effect_callback_type", "get_effect_callback_type");
45
46 ClassDB::bind_method(D_METHOD("set_access_resolved_color", "enable"), &CompositorEffect::set_access_resolved_color);
47 ClassDB::bind_method(D_METHOD("get_access_resolved_color"), &CompositorEffect::get_access_resolved_color);
48 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "access_resolved_color"), "set_access_resolved_color", "get_access_resolved_color");
49
50 ClassDB::bind_method(D_METHOD("set_access_resolved_depth", "enable"), &CompositorEffect::set_access_resolved_depth);
51 ClassDB::bind_method(D_METHOD("get_access_resolved_depth"), &CompositorEffect::get_access_resolved_depth);
52 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "access_resolved_depth"), "set_access_resolved_depth", "get_access_resolved_depth");
53
54 ClassDB::bind_method(D_METHOD("set_needs_motion_vectors", "enable"), &CompositorEffect::set_needs_motion_vectors);
55 ClassDB::bind_method(D_METHOD("get_needs_motion_vectors"), &CompositorEffect::get_needs_motion_vectors);
56 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "needs_motion_vectors"), "set_needs_motion_vectors", "get_needs_motion_vectors");
57
58 ClassDB::bind_method(D_METHOD("set_needs_normal_roughness", "enable"), &CompositorEffect::set_needs_normal_roughness);
59 ClassDB::bind_method(D_METHOD("get_needs_normal_roughness"), &CompositorEffect::get_needs_normal_roughness);
60 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "needs_normal_roughness"), "set_needs_normal_roughness", "get_needs_normal_roughness");
61
62 ClassDB::bind_method(D_METHOD("set_needs_separate_specular", "enable"), &CompositorEffect::set_needs_separate_specular);
63 ClassDB::bind_method(D_METHOD("get_needs_separate_specular"), &CompositorEffect::get_needs_separate_specular);
64 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "needs_separate_specular"), "set_needs_separate_specular", "get_needs_separate_specular");
65
66 BIND_ENUM_CONSTANT(EFFECT_CALLBACK_TYPE_PRE_OPAQUE)
67 BIND_ENUM_CONSTANT(EFFECT_CALLBACK_TYPE_POST_OPAQUE)
68 BIND_ENUM_CONSTANT(EFFECT_CALLBACK_TYPE_POST_SKY)
69 BIND_ENUM_CONSTANT(EFFECT_CALLBACK_TYPE_PRE_TRANSPARENT)
70 BIND_ENUM_CONSTANT(EFFECT_CALLBACK_TYPE_POST_TRANSPARENT)
71 BIND_ENUM_CONSTANT(EFFECT_CALLBACK_TYPE_MAX)
72
73 GDVIRTUAL_BIND(_render_callback, "effect_callback_type", "render_data");
74 }
75
76 void CompositorEffect::_validate_property(PropertyInfo &p_property) const {
77 if (p_property.name == "access_resolved_color" && effect_callback_type == EFFECT_CALLBACK_TYPE_POST_TRANSPARENT) {
78 p_property.usage = PROPERTY_USAGE_NONE;
79 }
80 if (p_property.name == "access_resolved_depth" && effect_callback_type == EFFECT_CALLBACK_TYPE_POST_TRANSPARENT) {
81 p_property.usage = PROPERTY_USAGE_NONE;
82 }
83 if (p_property.name == "needs_separate_specular" && effect_callback_type != EFFECT_CALLBACK_TYPE_POST_SKY) {
84 p_property.usage = PROPERTY_USAGE_NONE;
85 }
86 }
87
88 void CompositorEffect::_call_render_callback(int p_effect_callback_type, const RenderData *p_render_data) {
89 GDVIRTUAL_CALL(_render_callback, p_effect_callback_type, p_render_data);
90 }
91
92 void CompositorEffect::set_enabled(bool p_enabled) {
93 enabled = p_enabled;
94 if (rid.is_valid()) {
95 RenderingServer *rs = RenderingServer::get_singleton();
96 ERR_FAIL_NULL(rs);
97 rs->compositor_effect_set_enabled(rid, enabled);
98 }
99 }
100
101 bool CompositorEffect::get_enabled() const {
102 return enabled;
103 }
104
105 void CompositorEffect::set_effect_callback_type(EffectCallbackType p_callback_type) {
106 effect_callback_type = p_callback_type;
107 notify_property_list_changed();
108
109 if (rid.is_valid()) {
110 RenderingServer *rs = RenderingServer::get_singleton();
111 ERR_FAIL_NULL(rs);
112 rs->compositor_effect_set_callback(rid, RenderingServer::CompositorEffectCallbackType(effect_callback_type), callable_mp(this, &CompositorEffect::_call_render_callback));
113 }
114 }
115
116 CompositorEffect::EffectCallbackType CompositorEffect::get_effect_callback_type() const {
117 return effect_callback_type;
118 }
119
120 void CompositorEffect::set_access_resolved_color(bool p_enabled) {
121 access_resolved_color = p_enabled;
122 if (rid.is_valid()) {
123 RenderingServer *rs = RenderingServer::get_singleton();
124 ERR_FAIL_NULL(rs);
125 rs->compositor_effect_set_flag(rid, RS::CompositorEffectFlags::COMPOSITOR_EFFECT_FLAG_ACCESS_RESOLVED_COLOR, access_resolved_color);
126 }
127 }
128
129 bool CompositorEffect::get_access_resolved_color() const {
130 return access_resolved_color;
131 }
132
133 void CompositorEffect::set_access_resolved_depth(bool p_enabled) {
134 access_resolved_depth = p_enabled;
135 if (rid.is_valid()) {
136 RenderingServer *rs = RenderingServer::get_singleton();
137 ERR_FAIL_NULL(rs);
138 rs->compositor_effect_set_flag(rid, RS::CompositorEffectFlags::COMPOSITOR_EFFECT_FLAG_ACCESS_RESOLVED_DEPTH, access_resolved_depth);
139 }
140 }
141
142 bool CompositorEffect::get_access_resolved_depth() const {
143 return access_resolved_depth;
144 }
145
146 void CompositorEffect::set_needs_motion_vectors(bool p_enabled) {
147 needs_motion_vectors = p_enabled;
148 if (rid.is_valid()) {
149 RenderingServer *rs = RenderingServer::get_singleton();
150 ERR_FAIL_NULL(rs);
151 rs->compositor_effect_set_flag(rid, RS::CompositorEffectFlags::COMPOSITOR_EFFECT_FLAG_NEEDS_MOTION_VECTORS, needs_motion_vectors);
152 }
153 }
154
155 bool CompositorEffect::get_needs_motion_vectors() const {
156 return needs_motion_vectors;
157 }
158
159 void CompositorEffect::set_needs_normal_roughness(bool p_enabled) {
160 needs_normal_roughness = p_enabled;
161 if (rid.is_valid()) {
162 RenderingServer *rs = RenderingServer::get_singleton();
163 ERR_FAIL_NULL(rs);
164 rs->compositor_effect_set_flag(rid, RS::CompositorEffectFlags::COMPOSITOR_EFFECT_FLAG_NEEDS_ROUGHNESS, needs_normal_roughness);
165 }
166 }
167
168 bool CompositorEffect::get_needs_normal_roughness() const {
169 return needs_normal_roughness;
170 }
171
172 void CompositorEffect::set_needs_separate_specular(bool p_enabled) {
173 needs_separate_specular = p_enabled;
174 if (rid.is_valid()) {
175 RenderingServer *rs = RenderingServer::get_singleton();
176 ERR_FAIL_NULL(rs);
177 rs->compositor_effect_set_flag(rid, RS::CompositorEffectFlags::COMPOSITOR_EFFECT_FLAG_NEEDS_SEPARATE_SPECULAR, needs_separate_specular);
178 }
179 }
180
181 bool CompositorEffect::get_needs_separate_specular() const {
182 return needs_separate_specular;
183 }
184
185 CompositorEffect::CompositorEffect() {
186 RenderingServer *rs = RenderingServer::get_singleton();
187 if (rs != nullptr) {
188 rid = rs->compositor_effect_create();
189 rs->compositor_effect_set_callback(rid, RenderingServer::CompositorEffectCallbackType(effect_callback_type), callable_mp(this, &CompositorEffect::_call_render_callback));
190 }
191 }
192
193 CompositorEffect::~CompositorEffect() {
194 RenderingServer *rs = RenderingServer::get_singleton();
195 if (rs != nullptr && rid.is_valid()) {
196 rs->free_rid(rid);
197 }
198 }
199
200 /* Compositor */
201
202 void Compositor::_bind_methods() {
203 // compositor effects
204 ClassDB::bind_method(D_METHOD("set_compositor_effects", "compositor_effects"), &Compositor::set_compositor_effects);
205 ClassDB::bind_method(D_METHOD("get_compositor_effects"), &Compositor::get_compositor_effects);
206
207 ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "compositor_effects", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("CompositorEffect")), "set_compositor_effects", "get_compositor_effects");
208 }
209
210 Compositor::Compositor() {
211 RenderingServer *rs = RenderingServer::get_singleton();
212 if (rs != nullptr) {
213 compositor = rs->compositor_create();
214 }
215 }
216
217 Compositor::~Compositor() {
218 RenderingServer *rs = RenderingServer::get_singleton();
219 if (rs != nullptr && compositor.is_valid()) {
220 rs->free_rid(compositor);
221 }
222 }
223
224 // Compositor effects
225 void Compositor::set_compositor_effects(const TypedArray<CompositorEffect> &p_compositor_effects) {
226 Array effect_rids;
227 effects.clear();
228
229 for (int i = 0; i < p_compositor_effects.size(); i++) {
230 // Cast to proper ref, if our object isn't a CompositorEffect resource this will be an empty Ref.
231 Ref<CompositorEffect> compositor_effect = p_compositor_effects[i];
232
233 // We add the effect even if this is an empty Ref, this allows the UI to add new entries.
234 effects.push_back(compositor_effect);
235
236 // But we only add a rid for valid Refs
237 if (compositor_effect.is_valid()) {
238 RID rid = compositor_effect->get_rid();
239 effect_rids.push_back(rid);
240 }
241 }
242
243 RenderingServer::get_singleton()->compositor_set_compositor_effects(compositor, effect_rids);
244 }
245
246 TypedArray<CompositorEffect> Compositor::get_compositor_effects() const {
247 TypedArray<CompositorEffect> arr;
248
249 for (uint32_t i = 0; i < effects.size(); i++) {
250 arr.push_back(effects[i]);
251 }
252
253 return arr;
254 }