Blender V4.3
node_composite_composite.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
9#include "BLI_bounds_types.hh"
11
12#include "UI_interface.hh"
13#include "UI_resources.hh"
14
15#include "GPU_shader.hh"
16#include "GPU_texture.hh"
17
18#include "COM_node_operation.hh"
19#include "COM_utilities.hh"
20
22
23/* **************** COMPOSITE ******************** */
24
26
28{
29 b.add_input<decl::Color>("Image").default_value({0.0f, 0.0f, 0.0f, 1.0f});
30 b.add_input<decl::Float>("Alpha").default_value(1.0f).min(0.0f).max(1.0f);
31}
32
34{
35 uiItemR(layout, ptr, "use_alpha", UI_ITEM_R_SPLIT_EMPTY_NAME, nullptr, ICON_NONE);
36}
37
38using namespace blender::realtime_compositor;
39
41 public:
43
44 void execute() override
45 {
46 if (!context().is_valid_compositing_region()) {
47 return;
48 }
49
50 const Result &image = get_input("Image");
51 const Result &alpha = get_input("Alpha");
52 if (image.is_single_value() && alpha.is_single_value()) {
54 }
55 else if (ignore_alpha()) {
57 }
58 else if (!node().input_by_identifier("Alpha")->is_logically_linked()) {
60 }
61 else {
63 }
64 }
65
66 /* Executes when all inputs are single values, in which case, the output texture can just be
67 * cleared to the appropriate color. */
69 {
70 const Result &image = get_input("Image");
71 const Result &alpha = get_input("Alpha");
72
73 float4 color = image.get_color_value();
74 if (ignore_alpha()) {
75 color.w = 1.0f;
76 }
77 else if (node().input_by_identifier("Alpha")->is_logically_linked()) {
78 color.w = alpha.get_float_value();
79 }
80
81 const Domain domain = compute_domain();
82 Result output = context().get_output_result();
83 if (this->context().use_gpu()) {
84 GPU_texture_clear(output, GPU_DATA_FLOAT, color);
85 }
86 else {
87 parallel_for(domain.size, [&](const int2 texel) { output.store_pixel(texel, color); });
88 }
89 }
90
91 /* Executes when the alpha channel of the image is ignored. */
93 {
94 if (context().use_gpu()) {
96 }
97 else {
99 }
100 }
101
103 {
104 const Result &image = get_input("Image");
105 const Domain domain = compute_domain();
106 Result output = context().get_output_result();
107
108 GPUShader *shader = context().get_shader("compositor_write_output_opaque", output.precision());
109 GPU_shader_bind(shader);
110
111 const Bounds<int2> bounds = get_output_bounds();
112 GPU_shader_uniform_2iv(shader, "lower_bound", bounds.min);
113 GPU_shader_uniform_2iv(shader, "upper_bound", bounds.max);
114
115 image.bind_as_texture(shader, "input_tx");
116
117 output.bind_as_image(shader, "output_img");
118
119 compute_dispatch_threads_at_least(shader, domain.size);
120
121 image.unbind_as_texture();
122 output.unbind_as_image();
124 }
125
127 {
128 const Domain domain = compute_domain();
129 const Result &image = get_input("Image");
130 Result output = context().get_output_result();
131
132 const Bounds<int2> bounds = get_output_bounds();
133 parallel_for(domain.size, [&](const int2 texel) {
134 const int2 output_texel = texel + bounds.min;
135 if (output_texel.x > bounds.max.x || output_texel.y > bounds.max.y) {
136 return;
137 }
138 output.store_pixel(texel + bounds.min, float4(image.load_pixel(texel).xyz(), 1.0f));
139 });
140 }
141
142 /* Executes when the image texture is written with no adjustments and can thus be copied directly
143 * to the output. */
145 {
146 if (context().use_gpu()) {
147 this->execute_copy_gpu();
148 }
149 else {
150 this->execute_copy_cpu();
151 }
152 }
153
155 {
156 const Result &image = get_input("Image");
157 const Domain domain = compute_domain();
158 Result output = context().get_output_result();
159
160 GPUShader *shader = context().get_shader("compositor_write_output", output.precision());
161 GPU_shader_bind(shader);
162
163 const Bounds<int2> bounds = get_output_bounds();
164 GPU_shader_uniform_2iv(shader, "lower_bound", bounds.min);
165 GPU_shader_uniform_2iv(shader, "upper_bound", bounds.max);
166
167 image.bind_as_texture(shader, "input_tx");
168
169 output.bind_as_image(shader, "output_img");
170
171 compute_dispatch_threads_at_least(shader, domain.size);
172
173 image.unbind_as_texture();
174 output.unbind_as_image();
176 }
177
179 {
180 const Domain domain = compute_domain();
181 const Result &image = get_input("Image");
182 Result output = context().get_output_result();
183
184 const Bounds<int2> bounds = get_output_bounds();
185 parallel_for(domain.size, [&](const int2 texel) {
186 const int2 output_texel = texel + bounds.min;
187 if (output_texel.x > bounds.max.x || output_texel.y > bounds.max.y) {
188 return;
189 }
190 output.store_pixel(texel + bounds.min, image.load_pixel(texel));
191 });
192 }
193
194 /* Executes when the alpha channel of the image is set as the value of the input alpha. */
196 {
197 if (context().use_gpu()) {
198 execute_set_alpha_gpu();
199 }
200 else {
201 execute_set_alpha_cpu();
202 }
203 }
204
206 {
207 const Result &image = get_input("Image");
208 const Domain domain = compute_domain();
209 Result output = context().get_output_result();
210
211 GPUShader *shader = context().get_shader("compositor_write_output_alpha", output.precision());
212 GPU_shader_bind(shader);
213
214 const Bounds<int2> bounds = get_output_bounds();
215 GPU_shader_uniform_2iv(shader, "lower_bound", bounds.min);
216 GPU_shader_uniform_2iv(shader, "upper_bound", bounds.max);
217
218 image.bind_as_texture(shader, "input_tx");
219
220 const Result &alpha = get_input("Alpha");
221 alpha.bind_as_texture(shader, "alpha_tx");
222
223 output.bind_as_image(shader, "output_img");
224
225 compute_dispatch_threads_at_least(shader, domain.size);
226
227 image.unbind_as_texture();
228 alpha.unbind_as_texture();
229 output.unbind_as_image();
231 }
232
234 {
235 const Domain domain = compute_domain();
236 const Result &image = get_input("Image");
237 const Result &alpha = get_input("Alpha");
238 Result output = context().get_output_result();
239
240 const Bounds<int2> bounds = get_output_bounds();
241 parallel_for(domain.size, [&](const int2 texel) {
242 const int2 output_texel = texel + bounds.min;
243 if (output_texel.x > bounds.max.x || output_texel.y > bounds.max.y) {
244 return;
245 }
246 output.store_pixel(texel + bounds.min,
247 float4(image.load_pixel(texel).xyz(), alpha.load_pixel(texel).x));
248 });
249 }
250
251 /* Returns the bounds of the area of the compositing region. Only write into the compositing
252 * region, which might be limited to a smaller region of the output result. */
254 {
255 const rcti compositing_region = context().get_compositing_region();
256 return Bounds<int2>(int2(compositing_region.xmin, compositing_region.ymin),
257 int2(compositing_region.xmax, compositing_region.ymax));
258 }
259
260 /* If true, the alpha channel of the image is set to 1, that is, it becomes opaque. If false, the
261 * alpha channel of the image is retained, but only if the alpha input is not linked. If the
262 * alpha input is linked, it the value of that input will be used as the alpha of the image. */
264 {
265 return bnode().custom2 & CMP_NODE_OUTPUT_IGNORE_ALPHA;
266 }
267
268 /* The operation domain has the same size as the compositing region without any transformations
269 * applied. */
271 {
272 return Domain(context().get_compositing_region_size());
273 }
274};
275
277{
278 return new CompositeOperation(context, node);
279}
280
281} // namespace blender::nodes::node_composite_composite_cc
282
284{
286
287 static blender::bke::bNodeType ntype;
288
289 cmp_node_type_base(&ntype, CMP_NODE_COMPOSITE, "Composite", NODE_CLASS_OUTPUT);
290 ntype.declare = file_ns::cmp_node_composite_declare;
291 ntype.draw_buttons = file_ns::node_composit_buts_composite;
292 ntype.get_compositor_operation = file_ns::get_compositor_operation;
293 ntype.no_muting = true;
294
296}
#define NODE_CLASS_OUTPUT
Definition BKE_node.hh:405
@ CMP_NODE_OUTPUT_IGNORE_ALPHA
void GPU_shader_uniform_2iv(GPUShader *sh, const char *name, const int data[2])
void GPU_shader_bind(GPUShader *shader)
void GPU_shader_unbind()
void GPU_texture_clear(GPUTexture *texture, eGPUDataFormat data_format, const void *data)
@ GPU_DATA_FLOAT
void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, eUI_Item_Flag flag, const char *name, int icon)
@ UI_ITEM_R_SPLIT_EMPTY_NAME
struct GPUShader GPUShader
static btDbvtVolume bounds(btDbvtNode **leaves, int count)
Definition btDbvt.cpp:299
GPUShader * get_shader(const char *info_name, ResultPrecision precision)
NodeOperation(Context &context, DNode node)
Result & get_input(StringRef identifier) const
Definition operation.cc:144
local_group_size(16, 16) .push_constant(Type b
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
static NodeOperation * get_compositor_operation(Context &context, DNode node)
static void cmp_node_composite_declare(NodeDeclarationBuilder &b)
static void node_composit_buts_composite(uiLayout *layout, bContext *, PointerRNA *ptr)
void parallel_for(const int2 range, FunctionRef< void(int2)> function)
Definition utilities.cc:252
void compute_dispatch_threads_at_least(GPUShader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:131
void register_node_type_cmp_composite()
void cmp_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
Defines a node type.
Definition BKE_node.hh:218
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:324
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:238
NodeDeclareFunction declare
Definition BKE_node.hh:347
int ymin
int ymax
int xmin
int xmax
static pxr::UsdShadeInput get_input(const pxr::UsdShadeShader &usd_shader, const pxr::TfToken &input_name)
PointerRNA * ptr
Definition wm_files.cc:4126