Blender V4.5
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
8
9#include "BLI_math_vector.hh"
11
12#include "GPU_shader.hh"
13
14#include "COM_node_operation.hh"
15#include "COM_utilities.hh"
16
17#include "UI_interface.hh"
18#include "UI_resources.hh"
19
21
22/* **************** Pixelate ******************** */
23
25
27{
28 b.add_input<decl::Color>("Color").compositor_domain_priority(0);
29 b.add_input<decl::Int>("Size")
30 .default_value(1)
31 .min(1)
32 .description("The number of pixels that correspond to the same output pixel")
33 .compositor_expects_single_value();
34
35 b.add_output<decl::Color>("Color");
36}
37
38using namespace blender::compositor;
39
41 public:
43
44 void execute() override
45 {
46 const Result &input_image = this->get_input("Color");
47 const int pixel_size = this->get_pixel_size();
48 if (input_image.is_single_value() || pixel_size == 1) {
49 Result &output_image = this->get_result("Color");
50 output_image.share_data(input_image);
51 return;
52 }
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 = context().get_shader("compositor_pixelate");
65 GPU_shader_bind(shader);
66
67 const int pixel_size = get_pixel_size();
68 GPU_shader_uniform_1i(shader, "pixel_size", pixel_size);
69
70 Result &input_image = get_input("Color");
71 input_image.bind_as_texture(shader, "input_tx");
72
73 Result &output_image = get_result("Color");
74 const Domain domain = compute_domain();
75 output_image.allocate_texture(domain);
76 output_image.bind_as_image(shader, "output_img");
77
79
81 output_image.unbind_as_image();
82 input_image.unbind_as_texture();
83 }
84
86 {
87 Result &input = get_input("Color");
88
89 Result &output = get_result("Color");
90 const Domain domain = compute_domain();
91 output.allocate_texture(domain);
92
93 const int2 size = domain.size;
94 const int pixel_size = get_pixel_size();
95 parallel_for(size, [&](const int2 texel) {
96 int2 start = (texel / int2(pixel_size)) * int2(pixel_size);
97 int2 end = math::min(start + int2(pixel_size), size);
98
99 float4 accumulated_color = float4(0.0f);
100 for (int y = start.y; y < end.y; y++) {
101 for (int x = start.x; x < end.x; x++) {
102 accumulated_color += input.load_pixel<float4>(int2(x, y));
103 }
104 }
105
106 int2 size = end - start;
107 int count = size.x * size.y;
108 output.store_pixel(texel, accumulated_color / count);
109 });
110 }
111
113 {
114 return math::max(1, this->get_input("Size").get_single_value_default(1));
115 }
116};
117
119{
120 return new PixelateOperation(context, node);
121}
122
123} // namespace blender::nodes::node_composite_pixelate_cc
124
126{
128
129 static blender::bke::bNodeType ntype;
130
131 cmp_node_type_base(&ntype, "CompositorNodePixelate", CMP_NODE_PIXELATE);
132 ntype.ui_name = "Pixelate";
133 ntype.ui_description =
134 "Reduce detail in an image by making individual pixels more prominent, for a blocky or "
135 "mosaic-like appearance";
136 ntype.enum_name_legacy = "PIXELATE";
138 ntype.declare = file_ns::cmp_node_pixelate_declare;
139 ntype.get_compositor_operation = file_ns::get_compositor_operation;
140
142}
#define NODE_CLASS_OP_FILTER
Definition BKE_node.hh:437
#define CMP_NODE_PIXELATE
void GPU_shader_uniform_1i(GPUShader *sh, const char *name, int 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)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
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 share_data(const Result &source)
Definition result.cc:401
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
bool is_single_value() const
Definition result.cc:625
#define input
#define output
int count
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 min(const T &a, const T &b)
T max(const T &a, const T &b)
static void cmp_node_pixelate_declare(NodeDeclarationBuilder &b)
static NodeOperation * get_compositor_operation(Context &context, DNode node)
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
static void register_node_type_cmp_pixelate()
void cmp_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
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
NodeDeclareFunction declare
Definition BKE_node.hh:355