Blender V5.3
node_composite_map_uv.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 "MEM_guardedalloc.h"
6
7#include "BLI_math_vector.hh"
9
10#include "GPU_shader.hh"
11#include "GPU_texture.hh"
12
13#include "DNA_node_types.h"
14
15#include "RNA_enum_types.hh"
16
17#include "BKE_node.hh"
18
20#include "COM_domain.hh"
21#include "COM_node_operation.hh"
22#include "COM_utilities.hh"
23
25
27
29{
30 b.use_custom_socket_order();
31 b.allow_any_socket_order();
32
33 b.add_input<decl::Color>("Image"_ustr)
34 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
35 .hide_value()
37 .structure_type(StructureType::Dynamic);
38 b.add_output<decl::Color>("Image"_ustr)
39 .structure_type(StructureType::Dynamic)
40 .align_with_previous();
41
42 b.add_input<decl::Vector>("UV"_ustr)
43 .default_value({1.0f, 0.0f, 0.0f})
44 .min(0.0f)
45 .max(1.0f)
46 .description(
47 "The UV coordinates at which to sample the texture. The Z component is assumed to "
48 "contain an alpha channel")
49 .structure_type(StructureType::Dynamic);
50
51 PanelDeclarationBuilder &sampling_panel = b.add_panel("Sampling"_ustr).default_closed(true);
52 sampling_panel.add_input<decl::Menu>("Interpolation"_ustr)
56 .description("Interpolation method");
57 sampling_panel.add_input<decl::Menu>("Extension X"_ustr)
58 .default_value(CMP_NODE_EXTENSION_MODE_CLIP)
61 .description("The extension mode applied to the X axis");
62 sampling_panel.add_input<decl::Menu>("Extension Y"_ustr)
63 .default_value(CMP_NODE_EXTENSION_MODE_CLIP)
66 .description("The extension mode applied to the Y axis");
67}
68
69static void node_init(bNodeTree * /*ntree*/, bNode *node)
70{
71 NodeMapUVData *data = MEM_new<NodeMapUVData>(__func__);
72 node->storage = data;
73}
74
75using namespace blender::compositor;
76
78 public:
80
81 void execute() override
82 {
83 const Result &input = this->get_input("Image");
84 if (input.is_single_value()) {
85 Result &output = this->get_result("Image");
86 output.share_data(input);
87 return;
88 }
89
90 const Result &input_uv = this->get_input("UV");
91 if (input_uv.is_single_value()) {
92 this->execute_single();
93 return;
94 }
95
96 if (this->context().use_gpu()) {
97 this->execute_gpu();
98 }
99 else {
100 this->execute_cpu();
101 }
102 }
103
105 {
106 const Interpolation interpolation = this->get_interpolation();
107 gpu::Shader *shader = context().get_shader(this->get_shader_name(interpolation));
109
110 const Result &input_image = get_input("Image");
111 if (interpolation == Interpolation::Anisotropic) {
112 GPU_texture_anisotropic_filter(input_image, true);
113 GPU_texture_mipmap_mode(input_image, true, true);
114 }
115 else {
116 const bool use_bilinear = ELEM(
118 GPU_texture_filter_mode(input_image, use_bilinear);
119 }
120
121 GPU_texture_extend_mode_x(input_image,
123 GPU_texture_extend_mode_y(input_image,
125
126 input_image.bind_as_texture(shader, "input_tx");
127
128 const Result &input_uv = get_input("UV");
129 input_uv.bind_as_texture(shader, "uv_tx");
130
131 const Domain domain = compute_domain();
132 Result &output_image = get_result("Image");
133 output_image.allocate_texture(domain);
134 output_image.bind_as_image(shader, "output_img");
135
137
138 input_image.unbind_as_texture();
139 input_uv.unbind_as_texture();
140 output_image.unbind_as_image();
142 }
143
144 char const *get_shader_name(const Interpolation &interpolation)
145 {
146 switch (interpolation) {
148 return "compositor_map_uv_anisotropic";
150 return "compositor_map_uv_bicubic";
153 return "compositor_map_uv";
154 }
155
156 return "compositor_map_uv";
157 }
158
160 {
161 const Interpolation interpolation = this->get_interpolation();
162 if (interpolation == Interpolation::Anisotropic) {
164 }
165 else {
166 this->execute_cpu_interpolation(interpolation);
167 }
168 }
169
171 {
172 const Interpolation interpolation = this->get_interpolation();
173 const Extension extension_mode_x = this->get_extension_mode_x();
174 const Extension extension_mode_y = this->get_extension_mode_y();
175 const Result &input_uv = get_input("UV");
176 const Result &input_image = get_input("Image");
177
178 float2 uv_coordinates = input_uv.get_single_value<float3>().xy();
179 float4 sampled_color = float4(sample_pixel(this->context(),
180 input_image,
181 interpolation,
182 extension_mode_x,
183 extension_mode_y,
184 uv_coordinates));
185
186 /* The UV input is assumed to contain an alpha channel as its third channel, since the
187 * UV coordinates might be defined in only a subset area of the UV texture as mentioned.
188 * In that case, the alpha is typically opaque at the subset area and transparent
189 * everywhere else, and alpha pre-multiplication is then performed. This format of having
190 * an alpha channel in the UV coordinates is the format used by UV passes in render
191 * engines, hence the mentioned logic. */
192 float alpha = input_uv.get_single_value<float3>().z;
193
194 float4 result = sampled_color * alpha;
195
196 Result &output = get_result("Image");
197 output.allocate_single_value();
198 output.set_single_value(Color(result));
199 }
200
201 void execute_cpu_interpolation(const Interpolation &interpolation)
202 {
203 const Extension extension_mode_x = this->get_extension_mode_x();
204 const Extension extension_mode_y = this->get_extension_mode_y();
205 const Result &input_image = get_input("Image");
206 const Result &input_uv = get_input("UV");
207
208 const Domain domain = compute_domain();
209 Result &output_image = get_result("Image");
210 output_image.allocate_texture(domain);
211
212 parallel_for(domain.data_size, [&](const int2 texel) {
213 float2 uv_coordinates = input_uv.load_pixel<float3>(texel).xy();
214 float4 sampled_color = float4(input_image.sample<Color>(
215 uv_coordinates, interpolation, extension_mode_x, extension_mode_y));
216 /* The UV input is assumed to contain an alpha channel as its third channel, since the
217 * UV coordinates might be defined in only a subset area of the UV texture as mentioned.
218 * In that case, the alpha is typically opaque at the subset area and transparent
219 * everywhere else, and alpha pre-multiplication is then performed. This format of having
220 * an alpha channel in the UV coordinates is the format used by UV passes in render
221 * engines, hence the mentioned logic. */
222 float alpha = input_uv.load_pixel<float3>(texel).z;
223
224 float4 result = sampled_color * alpha;
225
226 output_image.store_pixel(texel, Color(result));
227 });
228 }
229
231 {
232 const Result &input_image = get_input("Image");
233 const Result &input_uv = get_input("UV");
234
235 const Domain domain = compute_domain();
236 Result &output_image = get_result("Image");
237 output_image.allocate_texture(domain);
238
239 /* In order to perform EWA sampling, we need to compute the partial derivative of the UV
240 * coordinates along the x and y directions using a finite difference approximation. But in
241 * order to avoid loading multiple neighboring UV coordinates for each pixel, we operate on
242 * the image in 2x2 blocks of pixels, where the derivatives are computed horizontally and
243 * vertically across the 2x2 block such that odd texels use a forward finite difference
244 * equation while even invocations use a backward finite difference equation. */
245 const int2 size = domain.data_size;
246 parallel_for(math::divide_ceil(size, int2(2)), [&](const int2 base_texel) {
247 const int x = base_texel.x * 2;
248 const int y = base_texel.y * 2;
249
250 const int2 lower_left_texel = int2(x, y);
251 const int2 lower_right_texel = int2(x + 1, y);
252 const int2 upper_left_texel = int2(x, y + 1);
253 const int2 upper_right_texel = int2(x + 1, y + 1);
254
255 const float2 lower_left_uv = input_uv.load_pixel<float3>(lower_left_texel).xy();
256 const float2 lower_right_uv = input_uv.load_pixel_extended<float3>(lower_right_texel).xy();
257 const float2 upper_left_uv = input_uv.load_pixel_extended<float3>(upper_left_texel).xy();
258 const float2 upper_right_uv = input_uv.load_pixel_extended<float3>(upper_right_texel).xy();
259
260 /* Compute the partial derivatives using finite difference. */
261 const float2 lower_x_gradient = lower_right_uv - lower_left_uv;
262 const float2 left_y_gradient = upper_left_uv - lower_left_uv;
263 const float2 right_y_gradient = upper_right_uv - lower_right_uv;
264 const float2 upper_x_gradient = upper_right_uv - upper_left_uv;
265
266 /* Computes one of the 2x2 pixels given its texel location, coordinates, and gradients. */
267 auto compute_pixel = [&](const int2 &texel,
268 const float2 &coordinates,
269 const float2 &x_gradient,
270 const float2 &y_gradient) {
271 /* Sample the input using the UV coordinates passing in the computed gradients in order
272 * to utilize the anisotropic filtering capabilities of the sampler. */
273 const float2x2 jacobian = float2x2(x_gradient, y_gradient);
274 float4 sampled_color = float4(input_image.sample<Color>(
276
277 /* The UV input is assumed to contain an alpha channel as its third channel, since the
278 * UV coordinates might be defined in only a subset area of the UV texture as mentioned.
279 * In that case, the alpha is typically opaque at the subset area and transparent
280 * everywhere else, and alpha pre-multiplication is then performed. This format of having
281 * an alpha channel in the UV coordinates is the format used by UV passes in render
282 * engines, hence the mentioned logic. */
283 float alpha = input_uv.load_pixel<float3>(texel).z;
284
285 float4 result = sampled_color * alpha;
286
287 output_image.store_pixel(texel, Color(result));
288 };
289
290 /* Compute each of the pixels in the 2x2 block, making sure to exempt out of bounds right
291 * and upper pixels. */
292 compute_pixel(lower_left_texel, lower_left_uv, lower_x_gradient, left_y_gradient);
293 if (lower_right_texel.x != size.x) {
294 compute_pixel(lower_right_texel, lower_right_uv, lower_x_gradient, right_y_gradient);
295 }
296 if (upper_left_texel.y != size.y) {
297 compute_pixel(upper_left_texel, upper_left_uv, upper_x_gradient, left_y_gradient);
298 }
299 if (upper_right_texel.x != size.x && upper_right_texel.y != size.y) {
300 compute_pixel(upper_right_texel, upper_right_uv, upper_x_gradient, right_y_gradient);
301 }
302 });
303 }
304
306 {
307 const CMPNodeInterpolation interpolation = CMPNodeInterpolation(
308 this->get_input("Interpolation").get_single_value_default<MenuValue>().value);
309 switch (interpolation) {
318 }
319
321 }
322
324 {
325 const CMPExtensionMode extension_x = CMPExtensionMode(
326 this->get_input("Extension X").get_single_value_default<MenuValue>().value);
327 switch (extension_x) {
329 return Extension::Clip;
331 return Extension::Repeat;
333 return Extension::Extend;
334 }
335
336 return Extension::Clip;
337 }
338
340 {
341 const CMPExtensionMode extension_y = CMPExtensionMode(
342 this->get_input("Extension Y").get_single_value_default<MenuValue>().value);
343 switch (extension_y) {
345 return Extension::Clip;
347 return Extension::Repeat;
349 return Extension::Extend;
350 }
351
352 return Extension::Clip;
353 }
354};
355
357{
358 return new MapUVOperation(context, node);
359}
360
361static void node_register()
362{
363 static bke::bNodeType ntype;
364
365 cmp_node_type_base(&ntype, "CompositorNodeMapUV"_ustr, CMP_NODE_MAP_UV);
366 ntype.ui_name = "Map UV";
367 ntype.ui_description =
368 "Map a texture using UV coordinates, to apply a texture to objects in compositing";
369 ntype.enum_name_legacy = "MAP_UV";
371 ntype.declare = node_declare;
373 ntype.initfunc = node_init;
376
378}
380
381} // namespace blender::nodes::node_composite_map_uv_cc
#define NODE_CLASS_DISTORT
Definition BKE_node.hh:486
#define CMP_NODE_MAP_UV
Read Blender memory allocation and freeing.
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define ELEM(...)
#define output
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
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
T sample(const float2 &coordinates, const Interpolation &interpolation, const Extension &extension_mode_x, const Extension &extension_mode_y, std::optional< float2x2 > jacobian=std::nullopt) const
void store_pixel(const int2 &texel, const T &pixel_value)
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_extended(const int2 &texel) const
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
const T & get_single_value() const
DeclType::Builder & add_input(UString name, UString identifier=""_ustr)
const CompositorInputRealizationMode & compositor_realization_mode() const
NodeOperation(Context &context, const bNode &node)
void execute_cpu_interpolation(const Interpolation &interpolation)
char const * get_shader_name(const Interpolation &interpolation)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
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
Color sample_pixel(Context &context, const Result &input, const Interpolation &interpolation, const Extension &extension_mode_x, const Extension &extension_mode_y, const float2 coordinates)
GPUSamplerExtendMode map_extension_mode_to_extend_mode(const Extension &mode)
Definition domain.cc:144
void parallel_for(const int2 range, const Function &function)
VecBase< T, Size > divide_ceil(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
static void node_init(bNodeTree *, bNode *node)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_declare(NodeDeclarationBuilder &b)
const EnumPropertyItem rna_enum_node_compositor_extension_items[]
MatBase< float, 2, 2 > float2x2
void GPU_texture_anisotropic_filter(gpu::Texture *texture, bool use_aniso)
void GPU_texture_mipmap_mode(gpu::Texture *texture, bool use_mipmap, bool use_filter)
BMesh const char void * data
void GPU_shader_unbind()
void GPU_texture_filter_mode(gpu::Texture *texture, bool use_filter)
@ CMP_NODE_EXTENSION_MODE_REPEAT
@ CMP_NODE_EXTENSION_MODE_EXTEND
@ CMP_NODE_EXTENSION_MODE_CLIP
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
@ CMP_NODE_INTERPOLATION_NEAREST
@ CMP_NODE_INTERPOLATION_BICUBIC
@ CMP_NODE_INTERPOLATION_ANISOTROPIC
@ CMP_NODE_INTERPOLATION_BILINEAR
void GPU_texture_extend_mode_x(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
void GPU_texture_extend_mode_y(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
const EnumPropertyItem rna_enum_node_compositor_interpolation_items[]
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
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393