Blender V5.0
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#include "BLI_math_numbers.hh"
11
13#include "UI_resources.hh"
14
15#include "GPU_shader.hh"
16
17#include "COM_node_operation.hh"
18#include "COM_utilities.hh"
19
21
22/* **************** SPLIT NODE ******************** */
23
25
27{
28 b.add_input<decl::Vector>("Position")
29 .dimensions(2)
31 .default_value({0.5f, 0.5f})
32 .min(0.0f)
33 .max(1.0f)
34 .description("Line position where the image should be split");
35 b.add_input<decl::Float>("Rotation")
36 .default_value(math::numbers::pi_v<float> / 4.0f)
38 .description("Line angle where the image should be split");
39
40 b.add_input<decl::Color>("Image").structure_type(StructureType::Dynamic);
41 b.add_input<decl::Color>("Image", "Image_001").structure_type(StructureType::Dynamic);
42
43 b.add_output<decl::Color>("Image").structure_type(StructureType::Dynamic);
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 gpu::Shader *shader = this->context().get_shader("compositor_split");
65 GPU_shader_bind(shader);
66
67 const Domain domain = this->compute_domain();
68
69 GPU_shader_uniform_2fv(shader, "position", this->get_position(domain));
70
71 const float2 normal = {-math::sin(this->get_rotation()), math::cos(this->get_rotation())};
72 GPU_shader_uniform_2fv(shader, "normal", normal);
73
74 const Result &first_image = this->get_input("Image");
75 first_image.bind_as_texture(shader, "first_image_tx");
76 const Result &second_image = this->get_input("Image_001");
77 second_image.bind_as_texture(shader, "second_image_tx");
78
79 Result &output_image = this->get_result("Image");
80 output_image.allocate_texture(domain);
81 output_image.bind_as_image(shader, "output_img");
82
84
85 first_image.unbind_as_texture();
86 second_image.unbind_as_texture();
87 output_image.unbind_as_image();
89 }
90
92 {
93 const Result &first_image = this->get_input("Image");
94 const Result &second_image = this->get_input("Image_001");
95
96 const Domain domain = this->compute_domain();
97 Result &output_image = this->get_result("Image");
98 output_image.allocate_texture(domain);
99
100 const math::AngleRadian rotation = this->get_rotation();
101 const float2 normal = {-math::sin(rotation), math::cos(rotation)};
102 const float2 line_point = this->get_position(domain);
103
104 parallel_for(domain.size, [&](const int2 texel) {
105 const float2 direction_to_line_point = line_point - float2(texel);
106 const float projection = math::dot(normal, direction_to_line_point);
107 const bool is_below_line = projection <= 0;
108 output_image.store_pixel(texel,
109 is_below_line ? first_image.load_pixel<float4, true>(texel) :
110 second_image.load_pixel<float4, true>(texel));
111 });
112 }
113
115 {
116 const float2 relative_position =
117 this->get_input("Position").get_single_value_default(float2(0.5f, 0.5f));
118 return float2(domain.size) * relative_position;
119 }
120
122 {
123 return this->get_input("Rotation").get_single_value_default(0.0f);
124 }
125};
126
128{
129 return new SplitOperation(context, node);
130}
131
132} // namespace blender::nodes::node_composite_split_cc
133
135{
136 namespace file_ns = blender::nodes::node_composite_split_cc;
137
138 static blender::bke::bNodeType ntype;
139
140 cmp_node_type_base(&ntype, "CompositorNodeSplit", CMP_NODE_SPLIT);
141 ntype.ui_name = "Split";
142 ntype.ui_description =
143 "Combine two images for side-by-side display. Typically used in combination with a Viewer "
144 "node";
145 ntype.enum_name_legacy = "SPLIT";
147 ntype.declare = file_ns::cmp_node_split_declare;
148 ntype.flag |= NODE_PREVIEW;
149 ntype.get_compositor_operation = file_ns::get_compositor_operation;
150
152}
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:453
#define CMP_NODE_SPLIT
@ NODE_PREVIEW
void GPU_shader_bind(blender::gpu::Shader *shader, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_2fv(blender::gpu::Shader *sh, const char *name, const float data[2])
void GPU_shader_unbind()
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_ANGLE
Definition RNA_types.hh:252
@ PROP_FACTOR
Definition RNA_types.hh:251
gpu::Shader * 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
T get_single_value_default(const T &default_value) const
void allocate_texture(const Domain domain, const bool from_pool=true, const std::optional< ResultStorageType > storage_type=std::nullopt)
Definition result.cc:389
void unbind_as_texture() const
Definition result.cc:511
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:487
void unbind_as_image() const
Definition result.cc:517
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
Definition result.cc:498
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:196
void parallel_for(const int2 range, const Function &function)
AngleRadianBase< float > AngleRadian
T cos(const AngleRadianBase< T > &a)
T sin(const AngleRadianBase< T > &a)
static void cmp_node_split_declare(NodeDeclarationBuilder &b)
static NodeOperation * get_compositor_operation(Context &context, DNode node)
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
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)
#define min(a, b)
Definition sort.cc:36
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:348
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362