Blender V5.3
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.hh"
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
32
34{
35 return get_default_hash(this->data_size, this->display_size, this->data_offset, this->type);
36}
37
39{
40 return a.data_size == b.data_size && a.display_size == b.display_size &&
41 a.data_offset == b.data_offset && a.type == b.type;
42}
43
44/* --------------------------------------------------------------------
45 * Image Coordinates.
46 */
47
49 const Domain &domain,
50 const CoordinatesType type)
51 : result(context.create_result(type == CoordinatesType::Pixel ? ResultType::Int2 :
53{
54 /* Ignore transformations to improve cache hits, the caller can restore transformations if
55 * needed. */
56 Domain untransformed_domain = domain;
57 untransformed_domain.transformation = float3x3::identity();
58 this->result.allocate_texture(untransformed_domain, false);
59
60 if (context.use_gpu()) {
61 this->compute_gpu(context, type);
62 }
63 else {
64 this->compute_cpu(type);
65 }
66}
67
72
73static const char *get_shader_name(const CoordinatesType type)
74{
75 switch (type) {
77 return "compositor_image_coordinates_uniform";
79 return "compositor_image_coordinates_normalized";
81 return "compositor_image_coordinates_pixel";
82 }
83
85 return "";
86}
87
88void ImageCoordinates::compute_gpu(Context &context, const CoordinatesType type)
89{
90 gpu::Shader *shader = context.get_shader(get_shader_name(type));
92
93 const Domain domain = this->result.domain();
94 GPU_shader_uniform_2iv(shader, "data_offset", domain.data_offset);
95 if (type != CoordinatesType::Pixel) {
96 GPU_shader_uniform_2iv(shader, "display_size", domain.display_size);
97 }
98
99 this->result.bind_as_image(shader, "output_img");
100
101 compute_dispatch_threads_at_least(shader, domain.data_size);
102
103 this->result.unbind_as_image();
105}
106
107void ImageCoordinates::compute_cpu(const CoordinatesType type)
108{
109 const Domain domain = this->result.domain();
110 switch (type) {
112 const int max_display_size = math::reduce_max(domain.display_size);
113 parallel_for(domain.data_size, [&](const int2 texel) {
114 const float2 coordinates = float2(domain.data_offset + texel) + 0.5f;
115 const float2 centered_coordinates = coordinates - float2(domain.display_size) / 2.0f;
116 const float2 normalized_coordinates = (centered_coordinates / max_display_size) * 2.0f;
117 this->result.store_pixel(texel, normalized_coordinates);
118 });
119 break;
120 }
122 parallel_for(domain.data_size, [&](const int2 texel) {
123 const float2 coordinates = float2(domain.data_offset + texel) + 0.5f;
124 const float2 normalized_coordinates = coordinates / float2(domain.display_size);
125 this->result.store_pixel(texel, normalized_coordinates);
126 });
127 break;
128 }
130 parallel_for(domain.data_size, [&](const int2 texel) {
131 this->result.store_pixel(texel, domain.data_offset + texel);
132 });
133 break;
134 }
135 }
136}
137
138/* --------------------------------------------------------------------
139 * Image Coordinates Container.
140 */
141
143{
144 /* First, delete all resources that are no longer needed. */
145 map_.remove_if([](auto item) { return !item.value->needed; });
146
147 /* Second, reset the needed status of the remaining resources to false to ready them to track
148 * their needed status for the next evaluation. */
149 for (auto &value : map_.values()) {
150 value->needed = false;
151 }
152}
153
155 const Domain &domain,
156 const CoordinatesType type)
157{
158 const ImageCoordinatesKey key(domain, type);
159
160 auto &pixel_coordinates = *map_.lookup_or_add_cb(
161 key, [&]() { return std::make_unique<ImageCoordinates>(context, domain, type); });
162
163 pixel_coordinates.needed = true;
164 return pixel_coordinates.result;
165}
166
167} // namespace blender::compositor
#define BLI_assert_unreachable()
Result & get(Context &context, const Domain &domain, const CoordinatesType type)
ImageCoordinatesKey(const Domain &domain, const CoordinatesType type)
ImageCoordinates(Context &context, const Domain &domain, const CoordinatesType type)
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_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
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:235
static const char * get_shader_name(const CoordinatesType type)
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 reduce_max(const VecBase< T, Size > &a)
void GPU_shader_uniform_2iv(gpu::Shader *sh, const char *name, const int data[2])
unsigned __int64 uint64_t
Definition BKE_text.h:91
void GPU_shader_unbind()
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252