Blender V5.3
sequencer/intern/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
9#include <optional>
10
11#include "BLI_math_rotation.hh"
12
14#include "BKE_node_runtime.hh"
15
16#include "PRF_profile.hh"
17
21#include "COM_scheduler.hh"
22#include "COM_utilities.hh"
23
24#include "GPU_state.hh"
25#include "GPU_texture_pool.hh"
26
28#include "IMB_imbuf.hh"
29
31
32#include "RNA_access.hh"
33
34#include "compositor.hh"
35
36namespace blender::seq {
37
38template<typename T>
39static void set_float_array(PointerRNA *input_props_ptr, compositor::Result &result)
40{
41 T value;
42 RNA_float_get_array(input_props_ptr, "value", value);
43 result.set_single_value(value);
44}
45
46template<typename T>
47static void set_int_array(PointerRNA *input_props_ptr, compositor::Result &result)
48{
49 T value;
50 RNA_int_get_array(input_props_ptr, "value", value);
51 result.set_single_value(value);
52}
53
54static void set_single_input_from_rna_value(PointerRNA *input_props_ptr,
55 const eNodeSocketDatatype socket_type,
57 const std::optional<int> dimensions)
58{
59 using namespace nodes;
60
61 /* Only consider inputs explicitly set to Value type. */
62 if (CompositorNodesInputType(RNA_enum_get(input_props_ptr, "type")) !=
63 CompositorNodesInputType::Value)
64 {
65 return;
66 }
67
68 switch (socket_type) {
69 case SOCK_FLOAT: {
70 const float value = RNA_float_get(input_props_ptr, "value");
71 result.set_single_value(value);
72 break;
73 }
74 case SOCK_VECTOR: {
75 switch (dimensions.value_or(3)) {
76 case 2: {
77 set_float_array<float2>(input_props_ptr, result);
78 break;
79 }
80 case 3: {
81 set_float_array<float3>(input_props_ptr, result);
82 break;
83 }
84 case 4: {
85 set_float_array<float4>(input_props_ptr, result);
86 break;
87 }
88 default:
90 }
91 break;
92 }
93 case SOCK_RGBA: {
94 ColorGeometry4f value;
95 RNA_float_get_array(input_props_ptr, "value", value);
96 result.set_single_value(value);
97 break;
98 }
99 case SOCK_BOOLEAN: {
100 const bool value = RNA_boolean_get(input_props_ptr, "value");
101 result.set_single_value(value);
102 break;
103 }
104 case SOCK_INT: {
105 const int value = RNA_int_get(input_props_ptr, "value");
106 result.set_single_value(value);
107 break;
108 }
109 case SOCK_ROTATION: {
110 float3 value_euler;
111 RNA_float_get_array(input_props_ptr, "value", value_euler);
112 math::Quaternion value_rotation = math::to_quaternion(math::EulerXYZ(value_euler));
113 result.set_single_value(value_rotation);
114 break;
115 }
116 case SOCK_MENU: {
117 const MenuValue value = MenuValue(RNA_enum_get(input_props_ptr, "value"));
118 result.set_single_value(value);
119 break;
120 }
121 case SOCK_STRING: {
122 const std::string value = RNA_string_get(input_props_ptr, "value");
123 result.set_single_value(value);
124 break;
125 }
126 case SOCK_INT_VECTOR: {
127 switch (dimensions.value_or(2)) {
128 case 2: {
129 set_int_array<int2>(input_props_ptr, result);
130 break;
131 }
132 case 3: {
133 set_int_array<int3>(input_props_ptr, result);
134 break;
135 }
136 default:
138 }
139 break;
140 }
141 case SOCK_OBJECT: {
142 Object *value = RNA_pointer_get(input_props_ptr, "value").data_as<Object>();
143 result.set_single_value(value);
144 break;
145 }
146 case SOCK_FONT: {
147 VFont *value = RNA_pointer_get(input_props_ptr, "value").data_as<VFont>();
148 result.set_single_value(value);
149 break;
150 }
151 case SOCK_IMAGE:
152 case SOCK_COLLECTION:
153 case SOCK_TEXTURE:
154 case SOCK_MATERIAL:
155 case SOCK_SCENE:
156 case SOCK_TEXT_ID:
157 case SOCK_MASK:
158 case SOCK_SOUND:
159 case SOCK_GEOMETRY:
160 case SOCK_MATRIX:
161 case SOCK_BUNDLE:
162 case SOCK_CLOSURE:
163 case SOCK_SHADER:
164 case SOCK_CUSTOM:
165 break;
166 }
167}
168
169static std::optional<int> get_socket_dimension(const bNodeTreeInterfaceSocket *socket,
170 const eNodeSocketDatatype socket_type)
171{
172 if (socket_type == SOCK_VECTOR) {
173 return static_cast<bNodeSocketValueVector *>(socket->socket_data)->dimensions;
174 }
175 if (socket_type == SOCK_INT_VECTOR) {
176 return static_cast<bNodeSocketValueIntVector *>(socket->socket_data)->dimensions;
177 }
178 return {};
179}
180
182 const bNodeTreeInterfaceSocket &socket,
183 const eNodeSocketDatatype socket_type,
185{
186 PointerRNA input_props_ptr = RNA_pointer_get(&inputs_ptr, socket.identifier);
187 result.allocate_single_value();
189 &input_props_ptr, socket_type, result, get_socket_dimension(&socket, socket_type));
190}
191
193{
194 switch (this->render_data_.scene->r.compositor_precision) {
196 /* Auto uses full precision for final renders and half precision otherwise. */
201 }
204}
205
207{
208 PRF_scope_with_name("SeqCreateCompInput", ProfileCategory::Draw);
209 const bool gpu = this->use_gpu();
210 const int2 size = int2(input.x, input.y);
211 if (!gpu) {
212 /* CPU path: ensure input is linear float. */
213 ensure_ibuf_is_linear_space(&input, true);
214 BLI_assert(input.float_data());
215 result.share_data(input.float_data(), size);
216 return;
217 }
218
219 /* GPU path: do necessary color space conversions (if any) to linear space on the GPU. */
220 const bool input_is_byte = input.float_data() == nullptr;
221 const char *input_colorspace = input_is_byte ? IMB_colormanagement_get_byte_colorspace(&input) :
223 const char *linear_colorspace = IMB_colormanagement_role_colorspace_name_get(
225
226 bool use_fallback = true;
227
228 if (input_is_byte || !STREQ(input_colorspace, linear_colorspace)) {
229 /* Need to convert data format or colorspace: upload input into temporary texture,
230 * convert into compositor result. */
231
232 /* Get the conversion shader. */
235 *this, input_colorspace, linear_colorspace);
237 if (shader) {
238
239 /* Upload input image into a GPU texture. */
241 gpu::Texture *input_tex = pool.acquire_texture_2d(size,
242 1,
243 input_is_byte ?
244 gpu::TextureFormat::UNORM_8_8_8_8 :
245 gpu::TextureFormat::SFLOAT_32_32_32_32,
247 "seq_comp_input");
248 if (input_tex) {
249 if (input_is_byte) {
250 GPU_texture_update(input_tex, GPU_DATA_UBYTE, input.byte_data());
251 }
252 else {
253 GPU_texture_update(input_tex, GPU_DATA_FLOAT, input.float_data());
254 }
255
256 /* Allocate compositor result texture. We use global compositor precision even
257 * for byte inputs. In theory Half precision should be enough, but that leads to potential
258 * small differences between CPU & GPU paths. */
259 result.set_precision(get_precision());
260 result.allocate_texture(size);
261
262 /* Convert input texture into the compositor result texture. */
263 GPU_texture_bind(input_tex,
265 result.bind_as_image(shader, ocio_shader.output_image_name());
266
267 GPU_shader_uniform_1b(shader, "premultiply_output", input_is_byte);
268
270
271 GPU_texture_unbind(input_tex);
272 result.unbind_as_image();
273 pool.release_texture(input_tex);
274
275 use_fallback = false;
276 }
277 ocio_shader.unbind_shader_and_resources();
278 }
279 }
280
281 /* Colorspace conversion was not needed or failed: upload input float data into
282 * compositor result. */
283 if (use_fallback) {
284 /* This is a no-op if input is already linear float; otherwise this step might be needed
285 * if conversion above has failed. */
286 ensure_ibuf_is_linear_space(&input, true);
287 result.allocate_texture(size);
289 }
290}
291
293{
294 using namespace compositor;
295
296 /* Realize the transforms if needed. */
297 const InputDescriptor input_descriptor = {ResultType::Color,
298 InputRealizationMode::OperationDomain};
299 SimpleOperation *realization_operation = RealizeOnDomainOperation::construct_if_needed(
300 *this, result, input_descriptor, result.domain());
301
302 if (!realization_operation) {
303 this->write_output(result, image);
304 return;
305 }
306
307 Result realize_input = this->create_result(ResultType::Color, result.precision());
308 realize_input.share_data(result);
309 realization_operation->map_input_to_result(&realize_input);
310 realization_operation->evaluate();
311
312 Result &realized_result = realization_operation->get_result();
313 this->write_output(realized_result, image);
314 realized_result.release();
315 delete realization_operation;
316}
317
319{
320 PRF_scope_with_name("SeqCompWriteOutput", ProfileCategory::Draw);
321
322 if (result.is_single_value()) {
323 compositor::Color color = result.get_single_value<compositor::Color>();
325 image.color_mode = color.a < 1.0f ? ImColorMode::RGBA : ImColorMode::RGB;
326 return;
327 }
328
329 result_translation_ = result.domain().transformation.location();
330 const int output_size_x = result.domain().data_size.x;
331 const int output_size_y = result.domain().data_size.y;
332 if (output_size_x != image.x || output_size_y != image.y || !image.float_buffer.data) {
333 /* Output size is different (e.g. image is blurred with expanded bounds);
334 * need to allocate appropriately sized buffer. */
336 image.x = output_size_x;
337 image.y = output_size_y;
338 }
339
341 image.color_mode = min_color.a < 1.0f ? ImColorMode::RGBA : ImColorMode::RGB;
342
343 if (this->use_gpu()) {
344 PRF_scope_with_name("SeqCompositorGPUReadback", ProfileCategory::Draw);
346 IMB_alloc_float_pixels(&image, 4, false);
347 GPU_texture_read(result.gpu_texture(), GPU_DATA_FLOAT, 0, image.float_data_for_write());
348 }
349 else if (result.sharing_info()) {
350 image.channels = 4;
351 image.float_buffer = ImBufFloatBuffer{
352 .data = static_cast<const float *>(result.cpu_data().data()),
353 .sharing_info = result.sharing_info(),
354 .colorspace = nullptr};
355 }
356 else if (result.cpu_data().data() != image.float_data()) {
357 IMB_alloc_float_pixels(&image, 4, false);
358 std::memcpy(image.float_data_for_write(),
359 result.cpu_data().data(),
360 IMB_get_pixel_count(&image) * sizeof(float) * 4);
361 }
362 const char *to_colorspace = IMB_colormanagement_role_colorspace_name_get(
365}
366
368 compositor::NodeGroupOperation &node_group_operation,
369 ImBuf &output_image)
370{
371 using namespace compositor;
372 for (const bNodeTreeInterfaceSocket *output_socket : node_group.interface_outputs()) {
373 Result &output_result = node_group_operation.get_result(output_socket->identifier);
374 if (!output_result.should_compute()) {
375 continue;
376 }
377
378 this->write_viewer_impl(output_result, output_image);
379 output_result.release();
380 }
381}
382
384 compositor::NodeGroupOperation &node_group_operation)
385{
386 using namespace compositor;
387
388 /* If the node group has no viewer node in the active context, and the context requires a viewer
389 * output, we use the group output as a viewer. */
390 const bool has_viewer = this->viewer_compute_context() != nullptr;
391 const bool needs_viewer_output = flag_is_set(this->needed_side_effect_output_types(),
392 SideEffectOutputTypes::ViewerNode);
393 const bool use_group_output_as_viewer = (!has_viewer && needs_viewer_output);
394
395 const bool is_group_output_needed = render_data_.render || use_group_output_as_viewer;
396
397 /* Set the reference count for the outputs, only the first color output is actually needed,
398 * while the rest are ignored. */
399 node_group.ensure_interface_cache();
400 for (const bNodeTreeInterfaceSocket *output_socket : node_group.interface_outputs()) {
401 const bool is_first_output = output_socket == node_group.interface_outputs().first();
402 Result &output_result = node_group_operation.get_result(output_socket->identifier);
403 const bool is_color = output_result.type() == ResultType::Color;
404 const bool is_needed = is_group_output_needed && is_first_output && is_color;
405 output_result.set_reference_count(is_needed ? 1 : 0);
406 }
407}
408
409} // namespace blender::seq
#define PRF_scope_with_name(ui_name, category)
#define BLI_assert_unreachable()
#define STREQ(a, b)
ChannelStorageType a
Result create_result(ResultType type, ResultPrecision precision)
virtual const ComputeContext * viewer_compute_context() const =0
OCIOColorSpaceConversionShader & get(Context &context, std::string source, std::string target)
Result & get_result(StringRef identifier)
Definition operation.cc:40
static ResultType type(gpu::TextureFormat format)
Definition result.cc:337
OCIOColorSpaceConversionShaderContainer ocio_color_space_conversion_shaders
virtual void release_texture(Texture *tex)=0
virtual Texture * acquire_texture_2d(int2 extent, int mip_len, TextureFormat format, eGPUTextureUsage usage, const char *name=nullptr)=0
static TexturePool & get()
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)
void write_output(const compositor::Result &result, ImBuf &image)
compositor::SideEffectOutputTypes needed_side_effect_output_types() const override
Definition compositor.hh:74
void write_viewer_impl(const compositor::Result &result, ImBuf &image)
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
#define T
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:235
Color minimum_color(Context &context, const Result &input)
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
QuaternionBase< float > Quaternion
QuaternionBase< T > to_quaternion(const AxisAngleBase< T, AngleT > &axis_angle)
EulerXYZBase< float > EulerXYZ
static void set_single_input_from_rna_value(PointerRNA *input_props_ptr, const eNodeSocketDatatype socket_type, compositor::Result &result, const std::optional< int > dimensions)
static void set_int_array(PointerRNA *input_props_ptr, compositor::Result &result)
static void set_float_array(PointerRNA *input_props_ptr, compositor::Result &result)
void set_input_result_from_rna(PointerRNA &inputs_ptr, const bNodeTreeInterfaceSocket &socket, const eNodeSocketDatatype socket_type, compositor::Result &result)
static std::optional< int > get_socket_dimension(const bNodeTreeInterfaceSocket *socket, const eNodeSocketDatatype socket_type)
void ensure_ibuf_is_linear_space(ImBuf *ibuf, bool make_float)
Definition render.cc:176
void IMB_colormanagement_assign_float_colorspace(ImBuf *ibuf, const char *name)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int GPU_shader_get_sampler_binding(gpu::Shader *shader, const char *name)
int RNA_enum_get(PointerRNA *ptr, const char *name)
void GPU_texture_update(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
size_t IMB_get_pixel_count(const ImBuf *ibuf)
Get the length of the data of the given image buffer in pixels.
void IMB_rectfill(ImBuf *drect, const float col[4])
Definition rectop.cc:1165
int RNA_int_get(PointerRNA *ptr, const char *name)
const char * IMB_colormanagement_get_byte_colorspace(const ImBuf *ibuf)
void GPU_shader_uniform_1b(gpu::Shader *sh, const char *name, bool value)
VecBase< int32_t, 2 > int2
@ GPU_TEXTURE_USAGE_SHADER_READ
void GPU_memory_barrier(GPUBarrier barrier)
Definition gpu_state.cc:305
const char * IMB_colormanagement_role_colorspace_name_get(int role)
BMesh const char void * data
void RNA_int_get_array(PointerRNA *ptr, const char *name, int *values)
@ GPU_BARRIER_TEXTURE_UPDATE
Definition GPU_state.hh:42
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
void GPU_texture_bind(gpu::Texture *texture, int unit)
void IMB_free_all_data(ImBuf *ibuf)
void GPU_texture_unbind(gpu::Texture *texture)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
void * GPU_texture_read(gpu::Texture *texture, eGPUDataFormat data_format, int mip_level)
std::string RNA_string_get(PointerRNA *ptr, const char *name)
@ SCE_COMPOSITOR_PRECISION_AUTO
@ SCE_COMPOSITOR_PRECISION_FULL
bool IMB_alloc_float_pixels(ImBuf *ibuf, unsigned int channels, bool initialize_pixels=true)
VecBase< float, 3 > float3
const char * IMB_colormanagement_get_float_colorspace(const ImBuf *ibuf)
float RNA_float_get(PointerRNA *ptr, const char *name)
void RNA_float_get_array(PointerRNA *ptr, const char *name, float *values)
const uint8_t * byte_data() const
const float * float_data() const
T * data_as() const
Definition RNA_types.hh:139
eCompositorPrecision compositor_precision
struct RenderData r