Blender V5.0
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_resources.hh"
18
20
21/* **************** Pixelate ******************** */
22
24
26{
27 b.use_custom_socket_order();
28 b.allow_any_socket_order();
29 b.add_input<decl::Color>("Color").structure_type(StructureType::Dynamic).hide_value();
30 b.add_output<decl::Color>("Color").structure_type(StructureType::Dynamic).align_with_previous();
31
32 b.add_input<decl::Int>("Size").default_value(1).min(1).description(
33 "The number of pixels that correspond to the same output pixel");
34}
35
36using namespace blender::compositor;
37
39 public:
41
42 void execute() override
43 {
44 const Result &input_image = this->get_input("Color");
45 const int pixel_size = this->get_pixel_size();
46 if (input_image.is_single_value() || pixel_size == 1) {
47 Result &output_image = this->get_result("Color");
48 output_image.share_data(input_image);
49 return;
50 }
51
52 if (this->context().use_gpu()) {
53 this->execute_gpu();
54 }
55 else {
56 this->execute_cpu();
57 }
58 }
59
61 {
62 gpu::Shader *shader = context().get_shader("compositor_pixelate");
63 GPU_shader_bind(shader);
64
65 const int pixel_size = get_pixel_size();
66 GPU_shader_uniform_1i(shader, "pixel_size", pixel_size);
67
68 Result &input_image = get_input("Color");
69 input_image.bind_as_texture(shader, "input_tx");
70
71 Result &output_image = get_result("Color");
72 const Domain domain = compute_domain();
73 output_image.allocate_texture(domain);
74 output_image.bind_as_image(shader, "output_img");
75
77
79 output_image.unbind_as_image();
80 input_image.unbind_as_texture();
81 }
82
84 {
85 Result &input = get_input("Color");
86
87 Result &output = get_result("Color");
88 const Domain domain = compute_domain();
89 output.allocate_texture(domain);
90
91 const int2 size = domain.size;
92 const int pixel_size = get_pixel_size();
93 parallel_for(size, [&](const int2 texel) {
94 int2 start = (texel / int2(pixel_size)) * int2(pixel_size);
95 int2 end = math::min(start + int2(pixel_size), size);
96
97 float4 accumulated_color = float4(0.0f);
98 for (int y = start.y; y < end.y; y++) {
99 for (int x = start.x; x < end.x; x++) {
100 accumulated_color += input.load_pixel<float4>(int2(x, y));
101 }
102 }
103
104 int2 size = end - start;
105 int count = size.x * size.y;
106 output.store_pixel(texel, accumulated_color / count);
107 });
108 }
109
111 {
112 return math::max(1, this->get_input("Size").get_single_value_default(1));
113 }
114};
115
117{
118 return new PixelateOperation(context, node);
119}
120
121} // namespace blender::nodes::node_composite_pixelate_cc
122
124{
126
127 static blender::bke::bNodeType ntype;
128
129 cmp_node_type_base(&ntype, "CompositorNodePixelate", CMP_NODE_PIXELATE);
130 ntype.ui_name = "Pixelate";
131 ntype.ui_description =
132 "Reduce detail in an image by making individual pixels more prominent, for a blocky or "
133 "mosaic-like appearance";
134 ntype.enum_name_legacy = "PIXELATE";
136 ntype.declare = file_ns::cmp_node_pixelate_declare;
137 ntype.get_compositor_operation = file_ns::get_compositor_operation;
138
140}
#define NODE_CLASS_OP_FILTER
Definition BKE_node.hh:451
#define CMP_NODE_PIXELATE
void GPU_shader_bind(blender::gpu::Shader *shader, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1i(blender::gpu::Shader *sh, const char *name, int value)
void GPU_shader_unbind()
#define NOD_REGISTER_NODE(REGISTER_FUNC)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
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
void share_data(const Result &source)
Definition result.cc:523
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
bool is_single_value() const
Definition result.cc:758
#define input
#define output
int count
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)
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: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