Blender V4.5
node_composite_split.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
8
9#include "BLI_math_base.hh"
10
11#include "UI_interface.hh"
12#include "UI_resources.hh"
13
14#include "GPU_shader.hh"
15
16#include "COM_node_operation.hh"
17#include "COM_utilities.hh"
18
20
21/* **************** SPLIT NODE ******************** */
22
24
26{
27 b.add_input<decl::Float>("Factor")
28 .default_value(0.5f)
30 .min(0.0f)
31 .max(1.0f)
32 .description("Specifies the position of the split")
33 .compositor_expects_single_value();
34 b.add_input<decl::Color>("Image");
35 b.add_input<decl::Color>("Image", "Image_001");
36
37 b.add_output<decl::Color>("Image");
38}
39
41{
42 uiLayout *row = &layout->row(false);
43 row->prop(ptr, "axis", UI_ITEM_R_SPLIT_EMPTY_NAME | UI_ITEM_R_EXPAND, std::nullopt, ICON_NONE);
44}
45
46using namespace blender::compositor;
47
49 public:
51
52 void execute() override
53 {
54 if (this->context().use_gpu()) {
55 this->execute_gpu();
56 }
57 else {
58 this->execute_cpu();
59 }
60 }
61
63 {
64 GPUShader *shader = this->get_split_shader();
65 GPU_shader_bind(shader);
66
67 GPU_shader_uniform_1f(shader, "split_ratio", this->get_split_ratio());
68
69 const Result &first_image = this->get_input("Image");
70 first_image.bind_as_texture(shader, "first_image_tx");
71 const Result &second_image = this->get_input("Image_001");
72 second_image.bind_as_texture(shader, "second_image_tx");
73
74 const Domain domain = this->compute_domain();
75 Result &output_image = this->get_result("Image");
76 output_image.allocate_texture(domain);
77 output_image.bind_as_image(shader, "output_img");
78
80
81 first_image.unbind_as_texture();
82 second_image.unbind_as_texture();
83 output_image.unbind_as_image();
85 }
86
87 GPUShader *get_split_shader()
88 {
90 return this->context().get_shader("compositor_split_horizontal");
91 }
92
93 return this->context().get_shader("compositor_split_vertical");
94 }
95
97 {
98 const Result &first_image = this->get_input("Image");
99 const Result &second_image = this->get_input("Image_001");
100
101 const Domain domain = this->compute_domain();
102 Result &output_image = this->get_result("Image");
103 output_image.allocate_texture(domain);
104
105 const float split_ratio = this->get_split_ratio();
106 const bool is_horizontal = this->get_split_axis() == CMP_NODE_SPLIT_HORIZONTAL;
107 const float split_pixel = (is_horizontal ? domain.size.x : domain.size.y) * split_ratio;
108
109 if (is_horizontal) {
110 parallel_for(domain.size, [&](const int2 texel) {
111 output_image.store_pixel(texel,
112 split_pixel <= texel.x ?
113 first_image.load_pixel<float4, true>(texel) :
114 second_image.load_pixel<float4, true>(texel));
115 });
116 }
117 else {
118 parallel_for(domain.size, [&](const int2 texel) {
119 output_image.store_pixel(texel,
120 split_pixel <= texel.y ?
121 first_image.load_pixel<float4, true>(texel) :
122 second_image.load_pixel<float4, true>(texel));
123 });
124 }
125 }
126
128 {
129 return static_cast<CMPNodeSplitAxis>(bnode().custom2);
130 }
131
133 {
134 return math::clamp(this->get_input("Factor").get_single_value_default(0.5f), 0.0f, 1.0f);
135 }
136};
137
139{
140 return new SplitOperation(context, node);
141}
142
143} // namespace blender::nodes::node_composite_split_cc
144
146{
147 namespace file_ns = blender::nodes::node_composite_split_cc;
148
149 static blender::bke::bNodeType ntype;
150
151 cmp_node_type_base(&ntype, "CompositorNodeSplit", CMP_NODE_SPLIT);
152 ntype.ui_name = "Split";
153 ntype.ui_description =
154 "Combine two images for side-by-side display. Typically used in combination with a Viewer "
155 "node";
156 ntype.enum_name_legacy = "SPLIT";
158 ntype.declare = file_ns::cmp_node_split_declare;
159 ntype.draw_buttons = file_ns::node_composit_buts_split;
160 ntype.flag |= NODE_PREVIEW;
161 ntype.get_compositor_operation = file_ns::get_compositor_operation;
162
163 ntype.no_muting = true;
164
166}
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:439
#define CMP_NODE_SPLIT
CMPNodeSplitAxis
@ CMP_NODE_SPLIT_HORIZONTAL
@ NODE_PREVIEW
void GPU_shader_uniform_1f(GPUShader *sh, const char *name, float value)
void GPU_shader_bind(GPUShader *shader, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_unbind()
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_FACTOR
Definition RNA_types.hh:239
@ UI_ITEM_R_SPLIT_EMPTY_NAME
@ UI_ITEM_R_EXPAND
GPUShader * get_shader(const char *info_name, ResultPrecision precision)
NodeOperation(Context &context, DNode node)
Result & get_result(StringRef identifier)
Definition operation.cc:39
Result & get_input(StringRef identifier) const
Definition operation.cc:138
virtual Domain compute_domain()
Definition operation.cc:56
void allocate_texture(Domain domain, bool from_pool=true)
Definition result.cc:309
void unbind_as_texture() const
Definition result.cc:389
void bind_as_texture(GPUShader *shader, const char *texture_name) const
Definition result.cc:365
void bind_as_image(GPUShader *shader, const char *image_name, bool read=false) const
Definition result.cc:376
void unbind_as_image() const
Definition result.cc:395
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
void compute_dispatch_threads_at_least(GPUShader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:170
void parallel_for(const int2 range, const Function &function)
T clamp(const T &a, const T &min, const T &max)
static void node_composit_buts_split(uiLayout *layout, bContext *, PointerRNA *ptr)
static void cmp_node_split_declare(NodeDeclarationBuilder &b)
static NodeOperation * get_compositor_operation(Context &context, DNode node)
VecBase< int32_t, 2 > int2
static void register_node_type_cmp_split()
void cmp_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
int16_t custom2
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:336
const char * enum_name_legacy
Definition BKE_node.hh:235
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:355
uiLayout & row(bool align)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
PointerRNA * ptr
Definition wm_files.cc:4227