Blender V4.5
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::Float3))
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 GPUShader *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, float3(normalized_coordinates, 0.0f));
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, float3(normalized_coordinates, 0.0f));
108 });
109 break;
110 }
112 parallel_for(this->result.domain().size, [&](const int2 texel) {
113 this->result.store_pixel(texel, float3(float2(texel), 0.0f));
114 });
115 break;
116 }
117 }
118}
119
120/* --------------------------------------------------------------------
121 * Image Coordinates Container.
122 */
123
125{
126 /* First, delete all resources that are no longer needed. */
127 map_.remove_if([](auto item) { return !item.value->needed; });
128
129 /* Second, reset the needed status of the remaining resources to false to ready them to track
130 * their needed status for the next evaluation. */
131 for (auto &value : map_.values()) {
132 value->needed = false;
133 }
134}
135
137 const int2 &size,
138 const CoordinatesType type)
139{
140 const ImageCoordinatesKey key(size, type);
141
142 auto &pixel_coordinates = *map_.lookup_or_add_cb(
143 key, [&]() { return std::make_unique<ImageCoordinates>(context, size, type); });
144
145 pixel_coordinates.needed = true;
146 return pixel_coordinates.result;
147}
148
149} // namespace blender::compositor
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
void GPU_shader_bind(GPUShader *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)
bool operator==(const BokehKernelKey &a, const BokehKernelKey &b)
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)
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
VecBase< float, 3 > float3