Blender V5.3
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
5#include <numbers>
6
7#include "GPU_shader.hh"
8
10#include "COM_utilities.hh"
11
13
15
17{
18 b.add_input<decl::Vector>("Position"_ustr)
19 .dimensions(2)
21 .default_value({0.5f, 0.5f})
22 .min(0.0f)
23 .max(1.0f)
24 .description("Line position where the image should be split");
25 b.add_input<decl::Float>("Rotation"_ustr)
26 .default_value(std::numbers::pi_v<float> / 4.0f)
28 .description("Line angle where the image should be split");
29
30 b.add_input<decl::Color>("Image"_ustr).structure_type(StructureType::Dynamic);
31 b.add_input<decl::Color>("Image"_ustr, "Image_001"_ustr).structure_type(StructureType::Dynamic);
32
33 b.add_output<decl::Color>("Image"_ustr).structure_type(StructureType::Dynamic);
34}
35
36using namespace blender::compositor;
37
39 public:
41
42 void execute() override
43 {
44 if (this->context().use_gpu()) {
45 this->execute_gpu();
46 }
47 else {
48 this->execute_cpu();
49 }
50 }
51
53 {
54 gpu::Shader *shader = this->context().get_shader("compositor_split");
56
57 const Domain domain = this->compute_domain();
58
59 GPU_shader_uniform_2fv(shader, "position", this->get_position(domain));
60
61 const float2 normal = {-math::sin(this->get_rotation()), math::cos(this->get_rotation())};
62 GPU_shader_uniform_2fv(shader, "normal", normal);
63
64 const Result &first_image = this->get_input("Image");
65 gpu::Texture *first_image_texture = first_image.bind_as_texture_or_single_value(
66 shader, "first_image_tx");
67 const Result &second_image = this->get_input("Image_001");
68 gpu::Texture *second_image_texture = second_image.bind_as_texture_or_single_value(
69 shader, "second_image_tx");
70
71 Result &output_image = this->get_result("Image");
72 output_image.allocate_texture(domain);
73 output_image.bind_as_image(shader, "output_img");
74
76
77 first_image.unbind_as_texture_or_single_value(first_image_texture);
78 second_image.unbind_as_texture_or_single_value(second_image_texture);
79 output_image.unbind_as_image();
81 }
82
84 {
85 const Result &first_image = this->get_input("Image");
86 const Result &second_image = this->get_input("Image_001");
87
88 const Domain domain = this->compute_domain();
89 Result &output_image = this->get_result("Image");
90 output_image.allocate_texture(domain);
91
92 const math::AngleRadian rotation = this->get_rotation();
93 const float2 normal = {-math::sin(rotation), math::cos(rotation)};
94 const float2 line_point = this->get_position(domain);
95
96 parallel_for(domain.data_size, [&](const int2 texel) {
97 const float2 direction_to_line_point = line_point - float2(texel);
98 const float projection = math::dot(normal, direction_to_line_point);
99 const bool is_below_line = projection <= 0;
100 output_image.store_pixel(texel,
101 is_below_line ? first_image.load_pixel<Color, true>(texel) :
102 second_image.load_pixel<Color, true>(texel));
103 });
104 }
105
107 {
108 const float2 relative_position =
109 this->get_input("Position").get_single_value_default<float2>();
110 return float2(domain.data_size) * relative_position;
111 }
112
114 {
115 return this->get_input("Rotation").get_single_value_default<float>();
116 }
117};
118
120{
121 return new SplitOperation(context, node);
122}
123
124static void node_register()
125{
126 static bke::bNodeType ntype;
127
128 cmp_node_type_base(&ntype, "CompositorNodeSplit"_ustr, CMP_NODE_SPLIT);
129 ntype.ui_name = "Split";
130 ntype.ui_description =
131 "Combine two images for side-by-side display. Typically used in combination with a Viewer "
132 "node";
133 ntype.enum_name_legacy = "SPLIT";
135 ntype.declare = node_declare;
136 ntype.flag |= NODE_PREVIEW;
138
140}
142
143} // namespace blender::nodes::node_composite_split_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define CMP_NODE_SPLIT
#define NOD_REGISTER_NODE(REGISTER_FUNC)
gpu::Shader * get_shader(const char *info_name, ResultPrecision precision)
NodeOperation(Context &context, const bNode &node)
Result & get_result(StringRef identifier)
Definition operation.cc:40
Context & context() const
Definition operation.cc:57
Result & get_input(StringRef identifier) const
Definition operation.cc:35
virtual Domain compute_domain()
Definition operation.cc:62
void unbind_as_texture_or_single_value(gpu::Texture *single_value_texture) const
Definition result.cc:970
void allocate_texture(const Domain domain, const bool from_pool=true, const std::optional< ResultStorageType > storage_type=std::nullopt)
Definition result.cc:554
void unbind_as_image() const
Definition result.cc:990
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
Definition result.cc:951
gpu::Texture * bind_as_texture_or_single_value(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:861
NodeOperation(Context &context, const bNode &node)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:235
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 node_declare(NodeDeclarationBuilder &b)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
void GPU_shader_unbind()
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_2fv(gpu::Shader *sh, const char *name, const float data[2])
@ PROP_FACTOR
Definition RNA_types.hh:308
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
#define min(a, b)
Definition sort.cc:38
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:379
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393