Blender V5.0
image_coordinates.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <cstdint>
6#include <memory>
7
8#include "BLI_assert.h"
9#include "BLI_hash.hh"
11
12#include "GPU_shader.hh"
13
14#include "COM_context.hh"
16#include "COM_result.hh"
17#include "COM_utilities.hh"
18
19namespace blender::compositor {
20
21/* --------------------------------------------------------------------
22 * Image Coordinates Key.
23 */
24
29
31{
32 return get_default_hash(this->size, this->type);
33}
34
36{
37 return a.size == b.size && a.type == b.type;
38}
39
40/* --------------------------------------------------------------------
41 * Image Coordinates.
42 */
43
45 : result(context.create_result(ResultType::Float2))
46{
47 this->result.allocate_texture(Domain(size), false);
48
49 if (context.use_gpu()) {
50 this->compute_gpu(context, type);
51 }
52 else {
53 this->compute_cpu(type);
54 }
55}
56
58{
59 this->result.release();
60}
61
62static const char *get_shader_name(const CoordinatesType type)
63{
64 switch (type) {
66 return "compositor_image_coordinates_uniform";
68 return "compositor_image_coordinates_normalized";
70 return "compositor_image_coordinates_pixel";
71 }
72
74 return "";
75}
76
77void ImageCoordinates::compute_gpu(Context &context, const CoordinatesType type)
78{
79 gpu::Shader *shader = context.get_shader(get_shader_name(type));
80 GPU_shader_bind(shader);
81
82 this->result.bind_as_image(shader, "output_img");
83
84 compute_dispatch_threads_at_least(shader, this->result.domain().size);
85
86 this->result.unbind_as_image();
88}
89
90void ImageCoordinates::compute_cpu(const CoordinatesType type)
91{
92 switch (type) {
94 const int2 size = this->result.domain().size;
95 const int max_size = math::max(size.x, size.y);
96 parallel_for(size, [&](const int2 texel) {
97 float2 centered_coordinates = (float2(texel) + 0.5f) - float2(size) / 2.0f;
98 float2 normalized_coordinates = (centered_coordinates / max_size) * 2.0f;
99 this->result.store_pixel(texel, normalized_coordinates);
100 });
101 break;
102 }
104 const int2 size = this->result.domain().size;
105 parallel_for(size, [&](const int2 texel) {
106 float2 normalized_coordinates = (float2(texel) + 0.5f) / float2(size);
107 this->result.store_pixel(texel, normalized_coordinates);
108 });
109 break;
110 }
112 parallel_for(this->result.domain().size,
113 [&](const int2 texel) { this->result.store_pixel(texel, float2(texel)); });
114 break;
115 }
116 }
117}
118
119/* --------------------------------------------------------------------
120 * Image Coordinates Container.
121 */
122
124{
125 /* First, delete all resources that are no longer needed. */
126 map_.remove_if([](auto item) { return !item.value->needed; });
127
128 /* Second, reset the needed status of the remaining resources to false to ready them to track
129 * their needed status for the next evaluation. */
130 for (auto &value : map_.values()) {
131 value->needed = false;
132 }
133}
134
136 const int2 &size,
137 const CoordinatesType type)
138{
139 const ImageCoordinatesKey key(size, type);
140
141 auto &pixel_coordinates = *map_.lookup_or_add_cb(
142 key, [&]() { return std::make_unique<ImageCoordinates>(context, size, type); });
143
144 pixel_coordinates.needed = true;
145 return pixel_coordinates.result;
146}
147
148} // namespace blender::compositor
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
void GPU_shader_bind(blender::gpu::Shader *shader, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_unbind()
unsigned long long int uint64_t
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
Result & get(Context &context, const int2 &size, const CoordinatesType type)
ImageCoordinatesKey(const int2 &size, const CoordinatesType type)
ImageCoordinates(Context &context, const int2 &size, const CoordinatesType type)
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:196
bool operator==(const BokehKernelKey &a, const BokehKernelKey &b)
void parallel_for(const int2 range, const Function &function)
static const char * get_shader_name(const int distance)
T max(const T &a, const T &b)
uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:233
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2