Blender V5.3
node_composite_render_layers.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"
8#include "BLI_memory_utils.hh"
9#include "BLI_set.hh"
10#include "BLI_string.hh"
11#include "BLI_string_ref.hh"
12
13#include "DNA_layer_types.h"
14#include "DNA_node_types.h"
15#include "DNA_scene_types.h"
16#include "DNA_space_types.h"
17
18#include "BKE_compositor.hh"
19#include "BKE_context.hh"
20#include "BKE_image.hh"
21#include "BKE_lib_id.hh"
22#include "BKE_node.hh"
23#include "BKE_node_runtime.hh"
24#include "BKE_scene.hh"
25
26#include "RE_engine.h"
27
28#include "RNA_access.hh"
29
30#include "UI_interface.hh"
32#include "UI_resources.hh"
33
35
37#include "COM_node_operation.hh"
38#include "COM_utilities.hh"
39
41
43
44static void node_init(bNodeTree * /*node_tree*/, bNode *node)
45{
46 node->flag |= NODE_PREVIEW;
47}
48
49static void node_init_api(const bContext *C, PointerRNA *node_ptr)
50{
51 Scene *scene = CTX_data_scene(C);
52 bNode *node = node_ptr->data_as<bNode>();
53 node->id = &scene->id;
54 id_us_plus(node->id);
55}
56
57/* Default declaration for contextless static declarations, when no scene is assigned, or when the
58 * engine has no extra passes. */
60{
61 b.add_output<decl::Color>("Image"_ustr).structure_type(StructureType::Dynamic);
62 b.add_output<decl::Float>("Alpha"_ustr).structure_type(StructureType::Dynamic);
63}
64
65/* Declares an already existing output. */
67 const bNodeSocket *output)
68{
69 if (output->type == SOCK_VECTOR) {
70 const int dimensions = output->default_value_typed<bNodeSocketValueVector>()->dimensions;
71 return b.add_output<decl::Vector>(output->identifier_ustr())
72 .dimensions(dimensions)
73 .structure_type(StructureType::Dynamic);
74 }
75 return b.add_output(output->type, output->identifier_ustr())
76 .structure_type(StructureType::Dynamic);
77}
78
79/* Declares the already existing outputs. This is done in cases where the scene references an
80 * engine that is not registered or a view layer that does not exist. Which gives the user the
81 * opportunity to register the engine or update the view layer while maintaining sockets and out
82 * going links. */
84{
85 const bNode *node = b.node_or_null();
86 for (const bNodeSocket &output : node->outputs) {
88 }
89}
90
91/* Declares an output that matches the type of the given pass. */
92static void declare_pass_callback(void *user_data,
93 Scene * /*scene*/,
94 ViewLayer * /*view_layer*/,
95 const char *pass_name,
96 int channels_count,
97 const char * /*channel_id*/,
98 eNodeSocketDatatype socket_type)
99{
100 NodeDeclarationBuilder &b = *static_cast<NodeDeclarationBuilder *>(user_data);
101
102 /* The combined pass is aliased as Image. */
103 const UString name = StringRef(pass_name) == RE_PASSNAME_COMBINED ? "Image"_ustr :
104 UString(pass_name);
105 if (socket_type == SOCK_VECTOR) {
106 b.add_output<decl::Vector>(UString(name))
107 .dimensions(channels_count)
108 .structure_type(StructureType::Dynamic);
109 }
110 else {
111 b.add_output(socket_type, name).structure_type(StructureType::Dynamic);
112 }
113
114 /* The Alpha pass is generated based on the combined pass. */
115 if (StringRef(pass_name) == RE_PASSNAME_COMBINED) {
116 b.add_output<decl::Float>("Alpha"_ustr).structure_type(StructureType::Dynamic);
117 }
118}
119
121 const Scene *scene,
122 const ViewLayer *view_layer)
123{
124 if ((scene->r.mode & R_EDGE_FRS) &&
126 {
127 b.add_output<decl::Color>(RE_PASSNAME_FREESTYLE ""_ustr)
128 .structure_type(StructureType::Dynamic);
129 }
130
132 b.add_output<decl::Color>(RE_PASSNAME_GREASE_PENCIL ""_ustr)
133 .structure_type(StructureType::Dynamic);
134 }
135}
136
137/* Declares outputs that are linked and existed in the previous state of the node but no longer
138 * exist in the new state. The outputs are set as unavailable, so they are not accessible to the
139 * user. This is useful to retain links if the user changed the render engine and thus the passes
140 * changed. */
142{
143 Set<UString> added_outputs_identifiers;
144 for (const SocketDeclaration *output_declaration : b.declaration().sockets(SOCK_OUT)) {
145 added_outputs_identifiers.add_new(output_declaration->identifier);
146 }
147 const bNodeTree *node_tree = b.tree_or_null();
148 const bNode *node = b.node_or_null();
149 node_tree->ensure_topology_cache();
150 for (const bNodeSocket *output : node->output_sockets()) {
151 if (added_outputs_identifiers.contains(output->identifier_ustr())) {
152 continue;
153 }
154 if (!output->is_directly_linked()) {
155 continue;
156 }
158 }
159}
160
162{
163 const bNode *node = b.node_or_null();
164 if (!node) {
166 return;
167 }
168
169 const bNodeTree *node_tree = b.tree_or_null();
170 if (!node_tree) {
172 return;
173 }
174
176
177 Scene *scene = reinterpret_cast<Scene *>(node->id);
178 if (!scene) {
180 return;
181 }
182
183 if (!RE_engines_is_registered(scene->r.engine)) {
185 return;
186 }
187
188 RenderEngineType *engine_type = RE_engines_find(scene->r.engine);
189 if (!engine_type->update_render_passes) {
191 return;
192 }
193
194 ViewLayer *view_layer = static_cast<ViewLayer *>(
195 BLI_findlink(&scene->view_layers, node->custom1));
196 if (!view_layer) {
198 return;
199 }
200
201 RenderEngine *engine = RE_engine_create(engine_type);
202 RE_engine_update_render_passes(engine, scene, view_layer, declare_pass_callback, &b);
203 RE_engine_free(engine);
204
205 declare_extra_passes(b, scene, view_layer);
206}
207
208static void node_draw_buttons(ui::Layout &layout, bContext *context, PointerRNA *node_pointer)
209{
210 template_id(&layout, context, node_pointer, "scene", nullptr, nullptr, nullptr);
211
212 const bNode *node = node_pointer->data_as<bNode>();
213 if (!node->id) {
214 return;
215 }
216
217 ui::Layout &column = layout.column(false);
218 ui::Layout &row = column.row(true);
219 row.prop(node_pointer, "layer", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
220
221 PropertyRNA *layer_property = RNA_struct_find_property(node_pointer, "layer");
222 const char *layer_name;
223 if (!RNA_property_enum_identifier(context,
224 node_pointer,
225 layer_property,
226 RNA_property_enum_get(node_pointer, layer_property),
227 &layer_name))
228 {
229 return;
230 }
231
232 char scene_name[MAX_ID_NAME - 2];
233 PointerRNA scene_pointer = RNA_pointer_get(node_pointer, "scene");
234 RNA_string_get(&scene_pointer, "name", scene_name);
235
236 PointerRNA render_operator = row.op(
237 "RENDER_OT_render", "", ICON_RENDER_STILL, wm::OpCallContext::InvokeDefault, UI_ITEM_NONE);
238 RNA_string_set(&render_operator, "layer", layer_name);
239 RNA_string_set(&render_operator, "scene", scene_name);
240}
241
242static void node_extra_info(NodeExtraInfoParams &parameters)
243{
244 SpaceNode *space_node = CTX_wm_space_node(&parameters.C);
245 if (space_node->node_tree_sub_type != SNODE_COMPOSITOR_SCENE) {
247 row.text = RPT_("Node Unsupported");
248 row.tooltip = TIP_("The Render Layers node is only supported for scene compositing");
249 row.icon = ICON_STATUS_ERROR;
250 parameters.rows.append(std::move(row));
251 }
252
253 /* EEVEE supports passes. */
254 const Scene *scene = CTX_data_scene(&parameters.C);
256 return;
257 }
258
260 return;
261 }
262
263 bool is_any_pass_used = false;
264 for (const bNodeSocket *output : parameters.node.output_sockets()) {
265 /* Combined pass is always available. */
266 if (STR_ELEM(output->name, "Image", "Alpha", "Grease Pencil")) {
267 continue;
268 }
269 if (output->is_logically_linked()) {
270 is_any_pass_used = true;
271 break;
272 }
273 }
274
275 if (!is_any_pass_used) {
276 return;
277 }
278
280 row.text = RPT_("Passes Not Supported");
281 row.tooltip = TIP_("Render passes in the Viewport compositor are only supported in EEVEE");
282 row.icon = ICON_STATUS_ERROR;
283 parameters.rows.append(std::move(row));
284}
285
286using namespace blender::compositor;
287
289 public:
291
292 void execute() override
293 {
294 const Scene *scene = this->get_scene();
295 const int view_layer = this->get_viewer_layer_index();
296
297 for (const bNodeSocket *output : this->node().output_sockets()) {
299 continue;
300 }
301
302 Result &result = this->get_result(output->identifier);
303 if (!result.should_compute()) {
304 continue;
305 }
306
307 if (StringRef(output->identifier) == "Alpha") {
308 Result combined_pass = this->context().get_pass(scene, view_layer, RE_PASSNAME_COMBINED);
309 extract_alpha(this->context(), combined_pass, result);
310 combined_pass.release();
311 continue;
312 }
313
314 Result pass = this->context().get_pass(scene, view_layer, output->identifier);
315 result.set_type(pass.type());
316 result.set_precision(pass.precision());
317 result.share_data(pass);
318 pass.release();
319 }
320 }
321
323 {
324 return this->node().custom1;
325 }
326
328 {
329 return reinterpret_cast<const Scene *>(this->node().id);
330 }
331};
332
334{
335 return new RenderLayerOperation(context, node);
336}
337
338static void node_register()
339{
340 static bke::bNodeType ntype;
341
342 cmp_node_type_base(&ntype, "CompositorNodeRLayers"_ustr, CMP_NODE_R_LAYERS);
343 ntype.ui_name = "Render Layers";
344 ntype.ui_description = "Input render passes from a scene render";
345 ntype.enum_name_legacy = "R_LAYERS";
346 ntype.nclass = NODE_CLASS_INPUT;
347 ntype.flag |= NODE_PREVIEW;
348 ntype.initfunc = node_init;
350 ntype.declare = node_declare;
355
357}
359
360} // namespace blender::nodes::node_composite_render_layer_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define CMP_NODE_R_LAYERS
#define BLI_SCOPED_DEFER(function_to_defer)
#define STR_ELEM(...)
#define RPT_(msgid)
#define TIP_(msgid)
#define MAX_ID_NAME
Definition DNA_ID.h:367
#define RE_PASSNAME_COMBINED
#define RE_PASSNAME_FREESTYLE
#define RE_PASSNAME_GREASE_PENCIL
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#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
virtual Result get_pass(const Scene *scene, int view_layer, const char *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)
bool is_viewport_compositor_used(const bContext &context)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void extract_alpha(Context &context, const Result &input, Result &output)
bool is_socket_available(const bNodeSocket *socket)
Definition utilities.cc:26
static void declare_extra_passes(NodeDeclarationBuilder &b, const Scene *scene, const ViewLayer *view_layer)
static void node_extra_info(NodeExtraInfoParams &parameters)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void declare_default(NodeDeclarationBuilder &b)
static void declare_old_linked_outputs(NodeDeclarationBuilder &b)
static BaseSocketDeclarationBuilder & declare_existing_output(NodeDeclarationBuilder &b, const bNodeSocket *output)
static void declare_existing(NodeDeclarationBuilder &b)
static void node_init_api(const bContext *C, PointerRNA *node_ptr)
static void node_draw_buttons(ui::Layout &layout, bContext *context, PointerRNA *node_pointer)
static void declare_pass_callback(void *user_data, Scene *, ViewLayer *, const char *pass_name, int channels_count, const char *, eNodeSocketDatatype socket_type)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
bool RE_engines_is_registered(const char *idname)
bool RNA_property_enum_identifier(bContext *C, PointerRNA *ptr, PropertyRNA *prop, const int value, const char **r_identifier)
void id_us_plus(ID *id)
Definition lib_id.cc:433
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
int RNA_property_enum_get(PointerRNA *ptr, PropertyRNA *prop)
void RE_engine_update_render_passes(RenderEngine *engine, Scene *scene, ViewLayer *view_layer, update_render_passes_cb_t callback, void *callback_data)
@ GREASE_PENCIL_AS_SEPARATE_PASS
const char * RE_engine_id_BLENDER_EEVEE
Definition scene.cc:1717
void RE_engine_free(RenderEngine *engine)
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
@ SNODE_COMPOSITOR_SCENE
SpaceNode * CTX_wm_space_node(const bContext *C)
RenderEngineType * RE_engines_find(const char *idname)
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
RenderEngine * RE_engine_create(RenderEngineType *type)
Scene * CTX_data_scene(const bContext *C)
std::string RNA_string_get(PointerRNA *ptr, const char *name)
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
eFreestyleConfig_Flags flags
T * data_as() const
Definition RNA_types.hh:139
void(*) update_render_passes(struct RenderEngine *engine, struct Scene *scene, struct ViewLayer *view_layer)
Definition RE_engine.h:123
struct RenderData r
struct FreestyleConfig freestyle_config
eViewLayerGreasePencilFlags grease_pencil_flags
static constexpr int _240
Definition BKE_node.hh:250
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
void(*) initfunc_api(const bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:333
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:379
NodeExtraInfoFunction get_extra_info
Definition BKE_node.hh:412
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
Vector< NodeExtraInfoRow > & rows
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
Layout & column(bool align)
Layout & row(bool align)