Blender V5.3
node_composite_image.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2006 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_assert.hh"
6#include "BLI_listbase.hh"
7#include "BLI_memory_utils.hh"
8#include "BLI_set.hh"
9#include "BLI_string.hh"
10#include "BLI_string_ref.hh"
11
12#include "BKE_image.hh"
13#include "BKE_node.hh"
14#include "BKE_node_runtime.hh"
15
16#include "DNA_image_types.h"
17#include "DNA_node_types.h"
18#include "DNA_scene_types.h"
19
21#include "COM_node_operation.hh"
22#include "COM_utilities.hh"
23
25
27
30{
31 b.add_output<decl::Color>("Image"_ustr).structure_type(StructureType::Dynamic);
32 b.add_output<decl::Float>("Alpha"_ustr).structure_type(StructureType::Dynamic);
33}
34
35/* Declaration for simple single layer images. */
37{
38 b.add_output<decl::Color>("Image"_ustr).structure_type(StructureType::Dynamic);
39 b.add_output<decl::Float>("Alpha"_ustr).structure_type(StructureType::Dynamic);
40}
41
42/* Declares an already existing output. */
44 const bNodeSocket *output)
45{
46 if (output->type == SOCK_VECTOR) {
47 const int dimensions = output->default_value_typed<bNodeSocketValueVector>()->dimensions;
48 return b.add_output<decl::Vector>(UString(output->name))
49 .dimensions(dimensions)
50 .structure_type(StructureType::Dynamic)
51 .available(output->is_available());
52 }
53 return b.add_output(output->type, UString(output->name))
54 .structure_type(StructureType::Dynamic)
55 .available(output->is_available());
56}
57
58/* Declares the already existing outputs. This is done in cases where the passes can not be read
59 * due to an invalid image to retain the links and give the user the opportunity to update the
60 * image such that becomes valid again. */
62{
63 const bNode *node = b.node_or_null();
64 for (const bNodeSocket &output : node->outputs) {
66 }
67}
68
69/* Declares an output that matches the type of the given pass. */
71{
72 const UString name(pass.name);
73 switch (pass.channels) {
74 case 1:
75 b.add_output<decl::Float>(name).structure_type(StructureType::Dynamic);
76 return;
77 case 2:
78 b.add_output<decl::Vector>(name).dimensions(2).structure_type(StructureType::Dynamic);
79 return;
80 case 3:
81 if (STR_ELEM(pass.chan_id, "RGB", "rgb")) {
82 b.add_output<decl::Color>(name).structure_type(StructureType::Dynamic);
83 return;
84 }
85 b.add_output<decl::Vector>(name).dimensions(3).structure_type(StructureType::Dynamic);
86 return;
87 case 4:
88 if (STR_ELEM(pass.chan_id, "RGBA", "rgba")) {
89 b.add_output<decl::Color>(name).structure_type(StructureType::Dynamic);
90 return;
91 }
92 b.add_output<decl::Vector>(name).dimensions(4).structure_type(StructureType::Dynamic);
93 return;
94 }
95
97}
98
100 Image *image,
101 const ImageUser *image_user)
102{
103 RenderResult *render_result = BKE_image_acquire_renderresult(nullptr, image);
104 BLI_SCOPED_DEFER([&]() { BKE_image_release_renderresult(nullptr, image, render_result); });
105
106 if (!render_result) {
108 return;
109 }
110
111 RenderLayer *render_layer = static_cast<RenderLayer *>(
112 BLI_findlink(&render_result->layers, image_user->layer));
113 if (!render_layer) {
115 return;
116 }
117
118 bool has_alpha_pass = false;
119 for (RenderPass &pass : render_layer->passes) {
120 if (StringRef(pass.name) == "Alpha") {
121 has_alpha_pass = true;
122 break;
123 }
124 }
125
126 /* The special 0 view in the image user denotes the view currently being composited, but since
127 * this is not known at declaration time, we add all passes regardless of their view. */
128 const bool should_add_all_views = image_user->view == 0;
129 /* If the special 0 value is not chosen, the selected view will be the image user view minus 1,
130 * to offset for the special value. */
131 const int selected_view = image_user->view - 1;
132
133 Set<StringRef> added_passes;
134 for (RenderPass &pass : render_layer->passes) {
135 if (should_add_all_views) {
136 /* Pass already added from another view. */
137 if (added_passes.contains(pass.name)) {
138 continue;
139 }
140 added_passes.add_new(pass.name);
141 }
142 else {
143 if (pass.view_id != selected_view) {
144 continue;
145 }
146 }
147
148 declare_pass(b, pass);
149
150 /* If the image does not have an alpha pass add an extra alpha pass that is generated based on
151 * the combined pass, if the combined pass is an RGBA pass. */
152 if (!has_alpha_pass && StringRef(pass.name) == RE_PASSNAME_COMBINED && pass.channels == 4 &&
153 StringRef(pass.chan_id) == "RGBA")
154 {
155 b.add_output<decl::Float>("Alpha"_ustr).structure_type(StructureType::Dynamic);
156 }
157 }
158}
159
160/* The image may not necessary have its type initialized correctly yet, so we can't identify if it
161 * is multi-layer or not. Further, the render result structure for multi-layer images may also not
162 * be initialized yet, so we can't retrieve the passes. So this function prepares the image by
163 * acquiring a dummy image buffer since it initializes the necessary data we need as a side effect.
164 * This image buffer can be immediately released. Since it carries no important information. */
165static void prepare_image(Image *image, const ImageUser *image_user)
166{
167 /* Create a copy of image user that represents the structure of the image at the first frame. We
168 * do not support a temporally changing image structure, since that changes the topology of the
169 * node tree. */
170 const int image_start_frame_offset = BKE_image_sequence_guess_offset(image);
171 ImageUser initial_frame_image_user = *image_user;
172 initial_frame_image_user.framenr = image_start_frame_offset;
173
174 ImBuf *initial_image_buffer = BKE_image_acquire_ibuf(image, &initial_frame_image_user, nullptr);
175 BKE_image_release_ibuf(image, initial_image_buffer, nullptr);
176}
177
178/* Declares outputs that are linked and existed in the previous state of the node but no longer
179 * exist in the new state. The outputs are set as unavailable, so they are not accessible to the
180 * user. This is useful to retain links if the user accidentally changed the image or the image was
181 * changed through some external factor without an explicit action from the user. */
183{
184 Set<UString> added_outputs_identifiers;
185 for (const SocketDeclaration *output_declaration : b.declaration().sockets(SOCK_OUT)) {
186 added_outputs_identifiers.add_new(output_declaration->identifier);
187 }
188
189 const bNodeTree *node_tree = b.tree_or_null();
190 const bNode *node = b.node_or_null();
191 node_tree->ensure_topology_cache();
192 for (const bNodeSocket *output : node->output_sockets()) {
193 if (added_outputs_identifiers.contains(output->identifier_ustr())) {
194 continue;
195 }
196 if (!output->is_directly_linked()) {
197 continue;
198 }
200 }
201}
202
204{
205 const bNode *node = b.node_or_null();
206 if (!node) {
208 return;
209 }
210
211 const bNodeTree *node_tree = b.tree_or_null();
212 if (!node_tree) {
214 return;
215 }
216
218
219 Image *image = reinterpret_cast<Image *>(node->id);
220 const ImageUser *image_user = static_cast<ImageUser *>(node->storage);
221 if (!image || !image_user) {
223 return;
224 }
225
226 /* Avoid unnecessary updates, only changes to the Image/Image User data are of interest. */
227 if (!(node->runtime->update & NODE_UPDATE_ID)) {
229 return;
230 }
231
232 prepare_image(image, image_user);
233
236 return;
237 }
238
239 node_declare_multi_layer(b, image, image_user);
240}
241
242static void node_init(bNodeTree * /*node_tree*/, bNode *node)
243{
244 node->flag |= NODE_PREVIEW;
245
246 ImageUser *iuser = MEM_new<ImageUser>(__func__);
247 node->storage = iuser;
248 iuser->frames = 1;
249 iuser->sfra = 1;
250 iuser->flag |= IMA_ANIM_ALWAYS;
251}
252
253using namespace blender::compositor;
254
256 public:
258
259 void execute() override
260 {
261 if (!this->get_image() || !this->get_image_user()) {
263 return;
264 }
265
266 for (const bNodeSocket *output : this->node().output_sockets()) {
268 continue;
269 }
270
271 this->compute_output(output->identifier);
272 }
273 }
274
275 void compute_output(StringRef identifier)
276 {
277 Result &result = this->get_result(identifier);
278 if (!result.should_compute()) {
279 return;
280 }
281
282 if (identifier == "Alpha") {
283 this->compute_alpha();
284 return;
285 }
286
287 const Result &cached_image = this->context().cache_manager().cached_images.get(
288 this->context(), *this->get_image(), *this->get_image_user(), identifier.data());
289 if (!cached_image.is_allocated()) {
290 result.allocate_invalid();
291 return;
292 }
293
294 result.set_type(cached_image.type());
295 result.set_precision(cached_image.precision());
296 result.share_data(cached_image);
297 }
298
300 {
301 Result &result = this->get_result("Alpha");
302 const Result &cached_alpha = this->context().cache_manager().cached_images.get(
303 this->context(), *this->get_image(), *this->get_image_user(), "Alpha");
304
305 /* For single layer images, the returned cached alpha is actually just the image, and we just
306 * extract the alpha from it. */
307 if (!BKE_image_is_multilayer(this->get_image())) {
308 if (!cached_alpha.is_allocated()) {
309 result.allocate_invalid();
310 return;
311 }
312
313 extract_alpha(this->context(), cached_alpha, result);
314 return;
315 }
316
317 /* For multi-layer images, if the returned cached alpha is allocated, that means that an actual
318 * pass called Alpha exists, and we just return it as is. */
319 if (cached_alpha.is_allocated()) {
320 result.set_type(cached_alpha.type());
321 result.set_precision(cached_alpha.precision());
322 result.share_data(cached_alpha);
323 return;
324 }
325
326 /* Otherwise, we try to extract the alpha from the combined pass if it exists. */
327 const Result &cached_combined_image = this->context().cache_manager().cached_images.get(
328 this->context(), *this->get_image(), *this->get_image_user(), RE_PASSNAME_COMBINED);
329 if (!cached_combined_image.is_allocated()) {
330 result.allocate_invalid();
331 return;
332 }
333 extract_alpha(this->context(), cached_combined_image, result);
334 }
335
337 {
338 return reinterpret_cast<Image *>(node().id);
339 }
340
342 {
343 return static_cast<ImageUser *>(node().storage);
344 }
345};
346
348{
349 return new ImageOperation(context, node);
350}
351
352static void node_register()
353{
354 static bke::bNodeType ntype;
355
356 cmp_node_type_base(&ntype, "CompositorNodeImage"_ustr, CMP_NODE_IMAGE);
357 ntype.ui_name = "Image";
358 ntype.ui_description = "Input image or movie file";
359 ntype.enum_name_legacy = "IMAGE";
360 ntype.nclass = NODE_CLASS_INPUT;
361 ntype.declare = node_declare;
362 ntype.initfunc = node_init;
367 ntype.flag |= NODE_PREVIEW;
368
370}
372
373} // namespace blender::nodes::node_composite_image_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define CMP_NODE_IMAGE
#define BLI_SCOPED_DEFER(function_to_defer)
#define STR_ELEM(...)
#define RE_PASSNAME_COMBINED
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define BLI_assert_unreachable()
#define output
bool contains(const Key &key) const
Definition BLI_set.hh:310
void add_new(const Key &key)
Definition BLI_set.hh:233
constexpr StringRef()
UString()=default
constexpr const char * data() const
Result & get(Context &context, Image &image, const ImageUser &image_user, const char *pass_name)
NodeOperation(Context &context, const bNode &node)
Result & get_result(StringRef identifier)
Definition operation.cc:40
Context & context() const
Definition operation.cc:57
static ResultType type(gpu::TextureFormat format)
Definition result.cc:337
static ResultPrecision precision(gpu::TextureFormat format)
Definition result.cc:307
BaseSocketDeclarationBuilder & available(bool value=true)
NodeOperation(Context &context, const bNode &node)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
void extract_alpha(Context &context, const Result &input, Result &output)
bool is_socket_available(const bNodeSocket *socket)
Definition utilities.cc:26
static void prepare_image(Image *image, const ImageUser *image_user)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_init(bNodeTree *, bNode *node)
static void declare_default(NodeDeclarationBuilder &b)
static void declare_single_layer(NodeDeclarationBuilder &b)
static void declare_old_linked_outputs(NodeDeclarationBuilder &b)
static void declare_existing(NodeDeclarationBuilder &b)
static void node_declare(NodeDeclarationBuilder &b)
static void node_declare_multi_layer(NodeDeclarationBuilder &b, Image *image, const ImageUser *image_user)
static BaseSocketDeclarationBuilder & declare_existing_output(NodeDeclarationBuilder &b, const bNodeSocket *output)
static void declare_pass(NodeDeclarationBuilder &b, const RenderPass &pass)
RenderResult * BKE_image_acquire_renderresult(Scene *scene, Image *ima)
void BKE_image_release_renderresult(Scene *scene, Image *ima, RenderResult *render_result)
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
int BKE_image_sequence_guess_offset(Image *image)
void node_image_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
Definition node_util.cc:146
bool BKE_image_is_multilayer(const Image *ima)
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
eImageUser_Flag flag
ListBaseT< RenderPass > passes
Definition RE_pipeline.h:99
ListBaseT< RenderLayer > layers
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
void(*) labelfunc(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:301
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:379
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393