Blender V5.3
MOD_compositor.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLT_translation.hh"
10
11#include "COM_domain.hh"
12#include "COM_result.hh"
13#include "COM_utilities.hh"
14
15#include "DNA_node_types.h"
16#include "DNA_sequence_types.h"
17
18#include "BKE_anim_data.hh"
19#include "BKE_animsys.hh"
20#include "BKE_compositor.hh"
23#include "BKE_context.hh"
24#include "BKE_idprop.hh"
25#include "BKE_node.hh"
26#include "BKE_node_runtime.hh"
27
29
31
32#include "NOD_composite.hh"
35
36#include "PRF_profile.hh"
37
38#include "SEQ_modifier.hh"
39#include "SEQ_select.hh"
40#include "SEQ_sequencer.hh"
41#include "SEQ_transform.hh"
42
43#include "UI_interface.hh"
44
45#include "RNA_access.hh"
46#include "RNA_prototypes.hh"
47
49#include "compositor.hh"
50#include "modifier.hh"
51#include "render.hh"
52
53namespace blender::seq {
54
56 Scene &sequencer_scene,
58{
59 if (!cmd.modifier.system_properties) {
61 bke::idprop::create_group("SequencerCompositorModifierProperties").release();
62 }
63
64 /* In case the node group is missing, do not update the properties to avoid the values reverting
65 * to their default value if the node group later becomes available. */
66 if (!cmd.node_group || ID_MISSING(cmd.node_group)) {
67 return;
68 }
69
71 &sequencer_scene.id, RNA_SequencerCompositorModifierProperties, &cmd);
73
75}
76
78 private:
79 const ModifierApplyContext &mod_context_;
80 SequencerCompositorModifierData *modifier_data_;
81
82 ImBuf *image_buffer_;
84 ImBuf *mask_buffer_ = nullptr;
85 int timeline_frame_;
86
87 /* The compute context of the active viewer if one exists. */
88 const ComputeContext *viewer_compute_context_;
89
90 bool owns_mask_ = false;
91 PointerRNA properties_ptr_;
92
93 public:
99 cache_manager, compute_context_cache, mod_context.render_data, mod_context.strip),
100 mod_context_(mod_context),
101 modifier_data_(modifier_data),
102 image_buffer_(mod_context.result.image),
103 mask_(*this, compositor::ResultType::Color, compositor::ResultPrecision::Full),
104 timeline_frame_(mod_context.timeline_frame),
105 viewer_compute_context_(bke::compositor::compute_viewer_compute_context(
106 *render_data_.scene, *modifier_data_->node_group, compute_context_cache))
107 {
109 &mod_context.render_data.scene->id, RNA_SequencerCompositorModifierData, modifier_data);
110 properties_ptr_ = RNA_pointer_get(&ptr, "properties");
111 }
112
114 {
115 IMB_freeImBuf(this->mask_buffer_);
116 this->mask_buffer_ = nullptr;
117
118 if (this->owns_mask_) {
119 this->mask_.release();
120 this->owns_mask_ = false;
121 }
122 }
123
125 {
126 return viewer_compute_context_;
127 }
128
130 {
131 return compositor::Domain(int2(image_buffer_->x, image_buffer_->y));
132 }
133
134 void write_viewer(compositor::Result &viewer_result) override
135 {
136 write_viewer_impl(viewer_result, *image_buffer_);
137 }
138
139 void evaluate()
140 {
141 using namespace compositor;
142 const StripModifierData &smd = this->modifier_data_->modifier;
143 const bool is_mask_used = smd.mask_input_type == STRIP_MASK_INPUT_STRIP ?
144 smd.mask_strip != nullptr :
145 smd.mask_id != nullptr;
146
148 modifier_data_->node_group);
149 const bke::DataBlockComputeContext &compute_context =
150 this->compute_context_cache().for_data_block(nullptr, this->get_scene().id);
151 NodeGroupOperation node_group_operation(*this, node_group, compute_context);
152 set_output_refcount(node_group, node_group_operation);
153
154 node_group.ensure_topology_cache();
155 PointerRNA inputs_ptr = RNA_pointer_get(&properties_ptr_, "inputs");
156 BLI_assert(inputs_ptr);
157
158 /* Map the inputs to the operation. */
160 const Span<const bNodeTreeInterfaceSocket *> interface_inputs = node_group.interface_inputs();
161 for (const bNodeTreeInterfaceSocket *input_socket : interface_inputs) {
162 bke::bNodeSocketType *typeinfo = input_socket->socket_typeinfo();
163 const eNodeSocketDatatype socket_type = typeinfo ? typeinfo->type : SOCK_CUSTOM;
164 const bool valid_socket_type = typeinfo && node_group.typeinfo->valid_socket_type(
165 node_group.typeinfo, typeinfo);
166 /* Fallback to ResultType::Float for invalid inputs. */
167 const ResultType result_type = valid_socket_type ?
169 *input_socket) :
170 ResultType::Float;
171 Result *input_result = new Result(this->create_result(result_type, ResultPrecision::Full));
172 if (input_socket == interface_inputs[0]) {
173 if (socket_type == SOCK_RGBA) {
174 /* First socket is the image input. */
175 create_result_from_input(*input_result, *image_buffer_);
176 }
177 else {
178 input_result->allocate_invalid();
179 }
180 }
181 else if (is_mask_used && input_socket == interface_inputs[1]) {
182 if (socket_type == SOCK_RGBA) {
183 /* Second socket is the mask input. */
184 render_mask_input(this->mod_context_, this->timeline_frame_);
185 if (this->mask_.is_allocated()) {
186 input_result->set_type(this->mask_.type());
187 input_result->set_precision(this->mask_.precision());
188 input_result->share_data(this->mask_);
189 input_result->set_transformation(this->mod_context_.transform_comp_result);
190 }
191 else {
192 input_result->allocate_invalid();
193 }
194 }
195 else {
196 input_result->allocate_invalid();
197 }
198 }
199 else if (valid_socket_type) {
200 /* Remaining inputs read their value from the exposed RNA property. */
201 set_input_result_from_rna(inputs_ptr, *input_socket, socket_type, *input_result);
202 }
203 else {
204 /* Unsupported sockets. */
205 input_result->allocate_invalid();
206 }
207
208 node_group_operation.map_input_to_result(input_socket->identifier, input_result);
209 inputs.append(std::unique_ptr<Result>(input_result));
210 }
211
212 {
213 PRF_scope_with_name("SeqCompositorEvaluate", ProfileCategory::Draw);
214 node_group_operation.evaluate();
215 }
216 this->write_outputs(node_group, node_group_operation, *this->image_buffer_);
217 }
218
219 /* Render mask - similar to #modifier_render_mask_input except for the Mask ID
220 * path we do a more efficient approach than rendering into a full ImBuf. */
221 void render_mask_input(const ModifierApplyContext &context, int timeline_frame)
222 {
223 PRF_scope_with_name("SeqRenderMaskInput", ProfileCategory::Draw);
224 const StripModifierData &smd = this->modifier_data_->modifier;
226 this->mask_buffer_ = seq_render_strip(&context.render_data,
227 &context.render_state,
228 smd.mask_strip,
229 timeline_frame)
230 .image;
231 if (this->mask_buffer_ != nullptr) {
232 this->create_result_from_input(this->mask_, *this->mask_buffer_);
233 this->owns_mask_ = true;
234 }
235 }
236 else if (smd.mask_input_type == STRIP_MASK_INPUT_ID && smd.mask_id) {
237 int frame_index = 0;
239 frame_index = smd.mask_id->sfra + timeline_frame - context.strip.start;
240 }
241 else if (smd.mask_time == STRIP_MASK_TIME_ABSOLUTE) {
242 frame_index = timeline_frame;
243 }
244
245 /* Mask is a grayscale value, similar to alpha, so conceptually it is already a
246 * "linear" quantity. However, masks used to be turned into grayscale images and
247 * interpreted as being in "sequencer working space" (default: sRGB), so keep at least
248 * that behavior working as before -- if sequencer space is sRGB, convert value to
249 * linear for the compositor. */
250 const bool seq_space_is_srgb = IMB_colormanagement_space_name_is_srgb(
251 context.render_data.scene->sequencer_colorspace_settings.name);
252
253 const int width = context.render_data.rectx;
254 const int height = context.render_data.recty;
256 this->mask_.share_data(
257 this->cache_manager().cached_masks.get(*this,
258 smd.mask_id,
259 compositor::Domain(int2(width, height)),
260 1.0f,
261 true,
262 frame_index,
263 1,
264 0.0f,
265 seq_space_is_srgb));
266 this->owns_mask_ = false;
267 }
268 }
269};
270
271static void compositor_modifier_init_data(StripModifierData *strip_modifier_data)
272{
273 SequencerCompositorModifierData *modifier_data =
274 reinterpret_cast<SequencerCompositorModifierData *>(strip_modifier_data);
275 modifier_data->node_group = nullptr;
276}
277
279 StripModifierData *strip_modifier_data)
280{
281 PRF_scope_with_name("SeqModCompositor", ProfileCategory::Draw);
282 SequencerCompositorModifierData *modifier_data =
283 reinterpret_cast<SequencerCompositorModifierData *>(strip_modifier_data);
284 if (!modifier_data->node_group) {
285 return;
286 }
287
288 CompositorCache &com_cache = context.render_data.scene->ed->runtime->ensure_compositor_cache();
289 bke::ComputeContextCache compute_context_cache;
290 CompositorModifierContext com_mod_context(
291 context, com_cache.get_cache_manager(), compute_context_cache, modifier_data);
292
294 if (com_mod_context.use_gpu()) {
295 gpu_state = render_begin_gpu(context.render_data);
296 com_mod_context.set_gpu_supported(gpu_state != GpuContextState::Unsupported);
297 }
298
299 com_cache.recreate_if_needed(
300 com_mod_context.use_gpu(), com_mod_context.get_precision(), context.render_data.gpu_context);
301 com_mod_context.evaluate();
302 com_mod_context.cache_manager().reset();
303 com_mod_context.free_resources();
304 render_end_gpu(context.render_data, gpu_state);
305
306 context.result.translation += com_mod_context.get_result_translation();
307}
308
310{
312 BLI_assert(*ptr);
313 BLI_assert(RNA_struct_is_a(ptr->type, RNA_StripModifier));
314 ui::panel_context_pointer_set(panel, "modifier", ptr);
315 return ptr;
316}
317
319{
320 ui::Layout &layout = *panel->layout;
322 nodes::draw_compositor_nodes_modifier_ui(*C, modifier_ptr, layout);
323}
324
330
332 /*idname*/ "Compositor",
333 /*name*/ CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "Compositor"),
334 /*struct_name*/ "SequencerCompositorModifierData",
335 /*struct_size*/ sizeof(SequencerCompositorModifierData),
336 /*init_data*/ compositor_modifier_init_data,
337 /*free_data*/ nullptr,
338 /*copy_data*/ nullptr,
340 /*panel_register*/ compositor_modifier_register,
341 /*blend_write*/ nullptr,
342 /*blend_read*/ nullptr,
343};
344
345}; // namespace blender::seq
#define CTX_N_(context, msgid)
#define BLT_I18NCONTEXT_ID_SEQUENCE
#define ID_MISSING(_id)
Definition DNA_ID.h:695
#define PRF_scope_with_name(ui_name, category)
#define C
Definition RandGen.cpp:29
void append(const T &value)
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 share_data(const Result &source)
Definition result.cc:996
static ResultType type(gpu::TextureFormat format)
Definition result.cc:337
static ResultPrecision precision(gpu::TextureFormat format)
Definition result.cc:307
void set_type(ResultType type)
Definition result.cc:1147
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
compositor::Domain get_compositing_domain() const override
void render_mask_input(const ModifierApplyContext &context, int timeline_frame)
const ComputeContext * viewer_compute_context() const override
CompositorModifierContext(const ModifierApplyContext &mod_context, compositor::StaticCacheManager &cache_manager, bke::ComputeContextCache &compute_context_cache, SequencerCompositorModifierData *modifier_data)
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 draw_compositor_nodes_modifier_ui(const bContext &C, PointerRNA *modifier_ptr, ui::Layout &layout)
StripModifierTypeInfo seqModifierType_Compositor
static void compositor_modifier_panel_draw(const bContext *C, Panel *panel)
SeqResult seq_render_strip(const RenderData *context, SeqRenderState *state, Strip *strip, float timeline_frame)
Definition render.cc:1903
void compositor_modifier_nodes_update_interface(Main &bmain, Scene &sequencer_scene, SequencerCompositorModifierData &cmd)
static void compositor_modifier_register(ARegionType *region_type)
void set_input_result_from_rna(PointerRNA &inputs_ptr, const bNodeTreeInterfaceSocket &socket, const eNodeSocketDatatype socket_type, compositor::Result &result)
static void compositor_modifier_apply(ModifierApplyContext &context, StripModifierData *strip_modifier_data)
void render_end_gpu(const RenderData &rd, GpuContextState state)
Definition render.cc:2294
static void compositor_modifier_init_data(StripModifierData *strip_modifier_data)
GpuContextState render_begin_gpu(const RenderData &rd)
Definition render.cc:2260
static PointerRNA * modifier_panel_get_property_pointers(Panel *panel)
PanelType * modifier_panel_register(ARegionType *region_type, const eStripModifierType type, PanelDrawFn draw)
void panel_context_pointer_set(Panel *panel, const char *name, PointerRNA *ptr)
PointerRNA * panel_custom_data_get(const Panel *panel)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool RNA_struct_is_a(const StructRNA *type, const StructRNA *srna)
void IMB_freeImBuf(ImBuf *ibuf)
VecBase< int32_t, 2 > int2
void DEG_id_tag_update(ID *id, unsigned int flags)
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)
void RNA_ensure_and_sync_system_properties(Main &bmain, PointerRNA &ptr, IDProperty &idprops)
@ ID_RECALC_SEQUENCER_STRIPS
Definition DNA_ID.h:1122
@ eSeqModifierType_Compositor
bool IMB_colormanagement_space_name_is_srgb(const char *name)
ui::Layout * layout
StructRNA * type
Definition RNA_types.hh:63
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
const RenderData & render_data
Definition modifier.hh:58