Blender V5.3
node_composite_pixelate.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_math_vector.hh"
7
8#include "GPU_shader.hh"
9
10#include "COM_node_operation.hh"
11#include "COM_utilities.hh"
12
14
16
18{
19 b.use_custom_socket_order();
20 b.allow_any_socket_order();
21 b.add_input<decl::Color>("Color"_ustr).structure_type(StructureType::Dynamic).hide_value();
22 b.add_output<decl::Color>("Color"_ustr)
23 .structure_type(StructureType::Dynamic)
24 .align_with_previous();
25
26 b.add_input<decl::Int>("Size"_ustr)
27 .default_value(1)
29 .min(1)
30 .description("The number of pixels that correspond to the same output pixel");
31}
32
33using namespace blender::compositor;
34
36 public:
38
39 void execute() override
40 {
41 const Result &input_image = this->get_input("Color");
42 const int pixel_size = this->get_pixel_size();
43 if (input_image.is_single_value() || pixel_size == 1) {
44 Result &output_image = this->get_result("Color");
45 output_image.share_data(input_image);
46 return;
47 }
48
49 if (this->context().use_gpu()) {
50 this->execute_gpu();
51 }
52 else {
53 this->execute_cpu();
54 }
55 }
56
58 {
59 gpu::Shader *shader = context().get_shader("compositor_pixelate");
61
62 const int pixel_size = get_pixel_size();
63 GPU_shader_uniform_1i(shader, "pixel_size", pixel_size);
64
65 Result &input_image = get_input("Color");
66 input_image.bind_as_texture(shader, "input_tx");
67
68 Result &output_image = get_result("Color");
69 const Domain domain = compute_domain();
70 output_image.allocate_texture(domain);
71 output_image.bind_as_image(shader, "output_img");
72
74
76 output_image.unbind_as_image();
77 input_image.unbind_as_texture();
78 }
79
81 {
82 Result &input = get_input("Color");
83
84 Result &output = get_result("Color");
85 const Domain domain = compute_domain();
86 output.allocate_texture(domain);
87
88 const int2 size = domain.data_size;
89 const int pixel_size = get_pixel_size();
90 parallel_for(size, [&](const int2 texel) {
91 int2 start = (texel / int2(pixel_size)) * int2(pixel_size);
92 int2 end = math::min(start + int2(pixel_size), size);
93
94 float4 accumulated_color = float4(0.0f);
95 for (int y = start.y; y < end.y; y++) {
96 for (int x = start.x; x < end.x; x++) {
97 accumulated_color += float4(input.load_pixel<Color>(int2(x, y)));
98 }
99 }
100
101 int2 size = end - start;
102 int count = size.x * size.y;
103 output.store_pixel(texel, Color(accumulated_color / count));
104 });
105 }
106
108 {
109 return math::max(1, this->get_input("Size").get_single_value_default<int>());
110 }
111};
112
114{
115 return new PixelateOperation(context, node);
116}
117
118static void node_register()
119{
120 static bke::bNodeType ntype;
121
122 cmp_node_type_base(&ntype, "CompositorNodePixelate"_ustr, CMP_NODE_PIXELATE);
123 ntype.ui_name = "Pixelate";
124 ntype.ui_description =
125 "Reduce detail in an image by making individual pixels more prominent, for a blocky or "
126 "mosaic-like appearance";
127 ntype.enum_name_legacy = "PIXELATE";
129 ntype.declare = node_declare;
131
133}
135
136} // namespace blender::nodes::node_composite_pixelate_cc
#define NODE_CLASS_OP_FILTER
Definition BKE_node.hh:482
#define CMP_NODE_PIXELATE
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define output
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 share_data(const Result &source)
Definition result.cc:996
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_texture() const
Definition result.cc:964
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:854
T load_pixel(const int2 &texel) const
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
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
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
void parallel_for(const int2 range, const Function &function)
T min(const T &a, const T &b)
T max(const T &a, const T &b)
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_1i(gpu::Shader *sh, const char *name, int value)
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
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