Blender V5.3
effect_compositor.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "BKE_compositor.hh"
14#include "BKE_idprop.hh"
15#include "BKE_node_runtime.hh"
16
17#include "COM_domain.hh"
18#include "COM_utilities.hh"
19
21
22#include "DNA_sequence_types.h"
23
25#include "IMB_imbuf.hh"
26
28
29#include "PRF_profile.hh"
30
31#include "RNA_access.hh"
32#include "RNA_prototypes.hh"
33
34#include "SEQ_effects.hh"
35#include "SEQ_sequencer.hh"
36
38#include "compositor.hh"
39#include "effects.hh"
40
41namespace blender::seq {
42
44 bNodeTree *node_group_;
45
46 ImBuf *input_1_;
47 ImBuf *input_2_;
48 ImBuf *output_;
49 float factor_;
50
51 /* The compute context of the active viewer if one exists. */
52 const ComputeContext *viewer_compute_context_;
53
54 public:
57 const RenderData &render_data,
58 bNodeTree *node_tree,
59 ImBuf *input_1,
60 ImBuf *input_2,
62 float factor,
63 const Strip &strip)
65 node_group_(node_tree),
66 input_1_(input_1),
67 input_2_(input_2),
68 output_(output),
69 factor_(factor),
70 viewer_compute_context_(bke::compositor::compute_viewer_compute_context(
71 *render_data_.scene, *node_group_, compute_context_cache))
72 {
73 }
74
75 const ComputeContext *viewer_compute_context() const override
76 {
77 return viewer_compute_context_;
78 }
79
81 {
82 return compositor::Domain(int2(this->output_->x, this->output_->y));
83 }
84
85 void write_viewer(compositor::Result &viewer_result) override
86 {
87 write_viewer_impl(viewer_result, *this->output_);
88 }
89
90 void evaluate()
91 {
92 using namespace compositor;
94 node_group_);
95 const bke::DataBlockComputeContext &compute_context =
96 this->compute_context_cache().for_data_block(nullptr, this->get_scene().id);
97 NodeGroupOperation node_group_operation(*this, node_group, compute_context);
98 set_output_refcount(node_group, node_group_operation);
99
100 node_group.ensure_topology_cache();
102 &render_data_.scene->id, RNA_CompositorStrip, const_cast<Strip *>(strip_));
103 PointerRNA properties_ptr = RNA_pointer_get(&strip_ptr, "properties");
104 PointerRNA inputs_ptr = RNA_pointer_get(&properties_ptr, "inputs");
105
106 /* Map the inputs to the operation. */
108 int float_counter = 0;
109 int color_counter = 0;
110 for (const bNodeTreeInterfaceSocket *input_socket : node_group.interface_inputs()) {
111 bke::bNodeSocketType *typeinfo = input_socket->socket_typeinfo();
112 const eNodeSocketDatatype socket_type = typeinfo ? typeinfo->type : SOCK_CUSTOM;
113 const bool valid_socket_type = typeinfo && node_group.typeinfo->valid_socket_type(
114 node_group.typeinfo, typeinfo);
115 /* Fallback to ResultType::Float for invalid inputs. */
116 const ResultType result_type = valid_socket_type ?
118 *input_socket) :
119 ResultType::Float;
120 Result *input_result = new Result(this->create_result(result_type, ResultPrecision::Full));
121 if (socket_type == SOCK_FLOAT && float_counter == 0) {
122 /* First float input is the effect fader factor. */
123 input_result->allocate_single_value();
124 input_result->set_single_value(this->factor_);
125 float_counter++;
126 }
127 else if (socket_type == SOCK_RGBA && color_counter == 0 && this->input_1_) {
128 /* First color input is the first image. */
129 create_result_from_input(*input_result, *this->input_1_);
130 color_counter++;
131 }
132 else if (socket_type == SOCK_RGBA && color_counter == 1 && this->input_2_) {
133 /* Second color input is the second image. */
134 create_result_from_input(*input_result, *this->input_2_);
135 color_counter++;
136 }
137 else if (valid_socket_type && inputs_ptr) {
138 /* Remaining inputs read their value from the exposed RNA property. */
139 set_input_result_from_rna(inputs_ptr, *input_socket, socket_type, *input_result);
140 }
141 else {
142 /* Unsupported sockets. */
143 input_result->allocate_invalid();
144 }
145
146 node_group_operation.map_input_to_result(input_socket->identifier, input_result);
147 inputs.append(std::unique_ptr<Result>(input_result));
148 }
149
150 node_group_operation.evaluate();
151 this->write_outputs(node_group, node_group_operation, *this->output_);
152 }
153};
154
156 SeqRenderState * /*state*/,
157 Strip *strip,
158 float /*timeline_frame*/,
159 float fac,
160 const SeqResult &src1,
161 const SeqResult &src2)
162{
163 PRF_scope_with_name("SeqFxCompositor", ProfileCategory::Draw);
164 const int x = context->rectx;
165 const int y = context->recty;
170
171 CompositorEffectVars *data = static_cast<CompositorEffectVars *>(strip->effectdata);
172 if (!data || !data->node_group) {
173 IMB_rectfill(out.image, float4(0, 0, 0, 1));
174 out.image->color_mode = ImColorMode::RGB;
175 out.is_opaque_before_transform = true;
176 }
177 else {
178 CompositorCache &com_cache = context->scene->ed->runtime->ensure_compositor_cache();
179 bke::ComputeContextCache compute_context_cache;
180 CompositorEffectContext com_context(com_cache.get_cache_manager(),
181 compute_context_cache,
182 *context,
183 data->node_group,
184 src1.image,
185 src2.image,
186 out.image,
187 fac,
188 *strip);
189
191 if (com_context.use_gpu()) {
192 gpu_state = render_begin_gpu(*context);
193 com_context.set_gpu_supported(gpu_state != GpuContextState::Unsupported);
194 }
195 com_cache.recreate_if_needed(
196 com_context.use_gpu(), com_context.get_precision(), context->gpu_context);
197 com_context.evaluate();
198 com_context.cache_manager().reset();
199 render_end_gpu(*context, gpu_state);
200 out.translation += com_context.get_result_translation();
201 out.is_opaque_before_transform = !out.image->can_contain_alpha();
202 }
203 return out;
204}
205
206static void init_compositor_effect(Strip *strip)
207{
208 CompositorEffectVars *data = MEM_new<CompositorEffectVars>(__func__);
209 strip->effectdata = data;
210}
211
212static void free_compositor_effect(Strip *strip, const bool /*do_id_user*/)
213{
214 if (strip->effectdata) {
215 CompositorEffectVars *data = static_cast<CompositorEffectVars *>(strip->effectdata);
216 if (data->system_properties != nullptr) {
217 IDP_FreeProperty_ex(data->system_properties, false);
218 }
219 MEM_delete(data);
220 strip->effectdata = nullptr;
221 }
222}
223
224static void copy_compositor_effect(Strip *dst, const Strip *src, const int flag)
225{
226 CompositorEffectVars *dst_data = MEM_new<CompositorEffectVars>(__func__);
227 const auto *src_data = static_cast<const CompositorEffectVars *>(src->effectdata);
228 *dst_data = *src_data;
229 dst_data->system_properties = nullptr;
230 if (src_data->system_properties != nullptr) {
231 dst_data->system_properties = IDP_CopyProperty_ex(src_data->system_properties, flag);
232 }
233 dst->effectdata = dst_data;
234}
235
236static StripEarlyOut early_out_compositor(const Strip *strip, float /*fac*/)
237{
238 /* No inputs: compositor generates the result. */
239 if (strip->input1 == nullptr) {
241 }
242
243 /* One or two inputs: do the effect. */
245}
246
247void compositor_effect_nodes_update_interface(Main &bmain, Scene &sequencer_scene, Strip &strip)
248{
249 if (strip.type != STRIP_TYPE_COMPOSITOR || strip.effectdata == nullptr) {
250 return;
251 }
252 CompositorEffectVars *comp = static_cast<CompositorEffectVars *>(strip.effectdata);
253 if (!comp->system_properties) {
254 comp->system_properties =
255 bke::idprop::create_group("SequencerCompositorEffectProperties").release();
256 }
257
258 /* In case the node group is missing, do not update the properties to avoid the values reverting
259 * to their default value if the node group later becomes available. */
260 if (!comp->node_group || ID_MISSING(comp->node_group)) {
261 return;
262 }
263
265 &sequencer_scene.id, RNA_SequencerCompositorEffectProperties, comp);
266 RNA_ensure_and_sync_system_properties(bmain, properties_ptr, *comp->system_properties);
267
269}
270
271void compositor_effect_nodes_input_usages(const Scene &sequencer_scene,
272 Strip &strip,
273 Vector<bool> &r_used,
274 Vector<bool> &r_visible)
275{
276 r_used.clear();
277 r_visible.clear();
278 if (strip.type != STRIP_TYPE_COMPOSITOR || strip.effectdata == nullptr) {
279 return;
280 }
281 CompositorEffectVars *comp = static_cast<CompositorEffectVars *>(strip.effectdata);
282 if (comp->node_group == nullptr || ID_MISSING(comp->node_group)) {
283 return;
284 }
285 bNodeTree &tree = *comp->node_group;
286 tree.ensure_interface_cache();
287 const int inputs_num = tree.interface_inputs().size();
288 if (inputs_num == 0) {
289 return;
290 }
291
292 /* Get to the generated properties interface struct. */
294 &const_cast<Scene &>(sequencer_scene).id, RNA_CompositorStrip, &strip);
295 PointerRNA properties_ptr = RNA_pointer_get(&strip_ptr, "properties");
296 if (!properties_ptr) {
297 return;
298 }
299
300 ResourceScope scope;
302 tree, properties_ptr, scope);
303
304 /* Effect fader and first N color inputs are supplied by the strips during evaluation, so
305 * treat them as unknown. Otherwise an input with usage conditional on the fader (e.g. mixed
306 * with the fader as a factor) might wrongly inferred as unused. */
307 const int images_num = strip.effect_num_inputs_get();
308 int float_counter = 0;
309 int color_counter = 0;
310 for (const int i : IndexRange(inputs_num)) {
311 const bNodeTreeInterfaceSocket &socket = *tree.interface_inputs()[i];
312 const bke::bNodeSocketType *typeinfo = socket.socket_typeinfo();
313 const eNodeSocketDatatype socket_type = typeinfo ? typeinfo->type : SOCK_CUSTOM;
314 if (socket_type == SOCK_FLOAT && float_counter == 0) {
315 input_values[i] = nodes::InferenceValue::Unknown();
316 float_counter++;
317 }
318 else if (socket_type == SOCK_RGBA && color_counter < images_num) {
319 input_values[i] = nodes::InferenceValue::Unknown();
320 color_counter++;
321 }
322 }
323
326 r_used.resize(inputs_num);
327 r_visible.resize(inputs_num);
328 for (const int i : IndexRange(inputs_num)) {
329 r_used[i] = usages[i].is_used;
330 r_visible[i] = usages[i].is_visible;
331 }
332}
333
342
343} // namespace blender::seq
#define ID_MISSING(_id)
Definition DNA_ID.h:695
#define PRF_scope_with_name(ui_name, category)
#define output
constexpr IndexRange()=default
void append(const T &value)
void resize(const int64_t new_size)
const DataBlockComputeContext & for_data_block(const ComputeContext *parent, const uint32_t orig_session_uid, const ID *id=nullptr)
Result create_result(ResultType type, ResultPrecision precision)
bke::ComputeContextCache & compute_context_cache() const
void recreate_if_needed(bool gpu, compositor::ResultPrecision precision, const gpu::GPUSecondaryContextData &gpu_context)
compositor::StaticCacheManager & get_cache_manager()
const RenderData & render_data_
Definition compositor.hh:33
bool use_gpu() const override
Definition compositor.hh:68
void write_outputs(const bNodeTree &node_group, compositor::NodeGroupOperation &node_group_operation, ImBuf &output_image)
const Scene & get_scene() const override
Definition compositor.hh:54
void set_gpu_supported(const bool supported)
Definition compositor.hh:63
float2 get_result_translation() const
Definition compositor.hh:84
void write_viewer_impl(const compositor::Result &result, ImBuf &image)
CompositorContext(compositor::StaticCacheManager &cache_manager, bke::ComputeContextCache &compute_context_cache, const RenderData &render_data, const Strip &strip)
Definition compositor.hh:41
void create_result_from_input(compositor::Result &result, ImBuf &input)
void set_output_refcount(const bNodeTree &node_group, compositor::NodeGroupOperation &node_group_operation)
compositor::ResultPrecision get_precision() const override
CompositorEffectContext(compositor::StaticCacheManager &cache_manager, bke::ComputeContextCache &compute_context_cache, const RenderData &render_data, bNodeTree *node_tree, ImBuf *input_1, ImBuf *input_2, ImBuf *output, float factor, const Strip &strip)
compositor::Domain get_compositing_domain() const override
const ComputeContext * viewer_compute_context() const override
void write_viewer(compositor::Result &viewer_result) override
std::unique_ptr< IDProperty, IDPropertyDeleter > create_group(StringRef prop_name, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_GROUP.
ResultType get_node_interface_socket_result_type(const bNodeTreeInterfaceSocket &socket)
Definition utilities.cc:123
void infer_group_interface_usage(const bNodeTree &group, const Span< InferenceValue > group_input_values, const MutableSpan< SocketUsage > r_input_usages, const std::optional< MutableSpan< SocketUsage > > r_output_usages)
Vector< InferenceValue > get_geometry_nodes_input_inference_values(const bNodeTree &btree, const PointerRNA &properties_ptr, ResourceScope &scope)
void compositor_effect_nodes_input_usages(const Scene &sequencer_scene, Strip &strip, Vector< bool > &r_used, Vector< bool > &r_visible)
static StripEarlyOut early_out_compositor(const Strip *strip, float)
static void free_compositor_effect(Strip *strip, const bool)
void set_input_result_from_rna(PointerRNA &inputs_ptr, const bNodeTreeInterfaceSocket &socket, const eNodeSocketDatatype socket_type, compositor::Result &result)
void compositor_effect_get_handle(EffectHandle &rval)
static void init_compositor_effect(Strip *strip)
static SeqResult do_compositor_effect(const RenderData *context, SeqRenderState *, Strip *strip, float, float fac, const SeqResult &src1, const SeqResult &src2)
void render_end_gpu(const RenderData &rd, GpuContextState state)
Definition render.cc:2294
static void copy_compositor_effect(Strip *dst, const Strip *src, const int flag)
GpuContextState render_begin_gpu(const RenderData &rd)
Definition render.cc:2260
void compositor_effect_nodes_update_interface(Main &bmain, Scene &sequencer_scene, Strip &strip)
void IMB_colormanagement_assign_float_colorspace(ImBuf *ibuf, const char *name)
void IDP_FreeProperty_ex(IDProperty *prop, bool do_id_user)
Definition idprop.cc:1398
void IMB_rectfill(ImBuf *drect, const float col[4])
Definition rectop.cc:1165
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
void DEG_id_tag_update(ID *id, unsigned int flags)
const char * IMB_colormanagement_role_colorspace_name_get(int role)
BMesh const char void * data
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
void RNA_ensure_and_sync_system_properties(Main &bmain, PointerRNA &ptr, IDProperty &idprops)
@ ID_RECALC_SEQUENCER_STRIPS
Definition DNA_ID.h:1122
IDProperty * IDP_CopyProperty_ex(const IDProperty *prop, int flag) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:968
struct IDProperty * system_properties
bke::bNodeTreeType * typeinfo
Defines a socket type.
Definition BKE_node.hh:163
eNodeSocketDatatype type
Definition BKE_node.hh:198
bool(*) valid_socket_type(bNodeTreeType *ntreetype, bNodeSocketType *socket_type)
Definition BKE_node.hh:569
void(*) copy(Strip *dst, const Strip *src, int flag)
Definition effects.hh:44
void(*) free(Strip *strip, bool do_id_user)
Definition effects.hh:47
void(*) init(Strip *strip)
Definition effects.hh:41
StripEarlyOut(*) early_out(const Strip *strip, float fac)
Definition effects.hh:49
SeqResult(*) execute(const RenderData *context, SeqRenderState *state, Strip *strip, float timeline_frame, float fac, const SeqResult &input1, const SeqResult &input2)
Definition effects.hh:52