Blender V5.3
bokeh_kernel.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <cstdint>
6#include <memory>
7
8#include "BLI_hash.hh"
9#include "BLI_math_base.hh"
10#include "BLI_math_vector.hh"
12
13#include "GPU_shader.hh"
14
15#include "COM_bokeh_kernel.hh"
16#include "COM_context.hh"
17#include "COM_result.hh"
18#include "COM_utilities.hh"
19
20namespace blender::compositor {
21
22/* --------------------------------------------------------------------
23 * Bokeh Kernel Key.
24 */
25
36
42
44{
45 return a.size == b.size && a.sides == b.sides && a.rotation == b.rotation &&
46 a.roundness == b.roundness && a.catadioptric == b.catadioptric &&
47 a.lens_shift == b.lens_shift;
48}
49
50/* --------------------------------------------------------------------
51 * Bokeh Kernel.
52 */
53
55 int2 size,
56 int sides,
57 float rotation,
58 float roundness,
59 float catadioptric,
60 float lens_shift)
61 : result(context.create_result(ResultType::Color))
62{
63 this->result.allocate_texture(Domain(size), false);
64
65 if (context.use_gpu()) {
66 this->compute_gpu(context, sides, rotation, roundness, catadioptric, lens_shift);
67 }
68 else {
69 this->compute_cpu(sides, rotation, roundness, catadioptric, lens_shift);
70 }
71}
72
77
78/* The exterior angle is the angle between each two consecutive vertices of the regular polygon
79 * from its center. */
80static float compute_exterior_angle(int sides)
81{
82 return (std::numbers::pi * 2.0f) / sides;
83}
84
85static float compute_rotation(float angle, int sides)
86{
87 /* Offset the rotation such that the second vertex of the regular polygon lies on the positive
88 * y axis, which is 90 degrees minus the angle that it makes with the positive x axis assuming
89 * the first vertex lies on the positive x axis. */
90 const float offset = (std::numbers::pi / 2.0f) - compute_exterior_angle(sides);
91 return angle - offset;
92}
93
94void BokehKernel::compute_gpu(Context &context,
95 const int sides,
96 const float rotation,
97 const float roundness,
98 const float catadioptric,
99 const float lens_shift)
100{
101 gpu::Shader *shader = context.get_shader("compositor_bokeh_image");
103
104 GPU_shader_uniform_1f(shader, "exterior_angle", compute_exterior_angle(sides));
105 GPU_shader_uniform_1f(shader, "rotation", compute_rotation(rotation, sides));
106 GPU_shader_uniform_1f(shader, "roundness", roundness);
107 GPU_shader_uniform_1f(shader, "catadioptric", catadioptric);
108 GPU_shader_uniform_1f(shader, "lens_shift", lens_shift);
109
110 this->result.bind_as_image(shader, "output_img");
111
113
114 this->result.unbind_as_image();
116}
117
118/* Get the 2D vertex position of the vertex with the given index in the regular polygon
119 * representing this bokeh. The polygon is rotated by the rotation amount and have a unit
120 * circumradius. The regular polygon is one whose vertices' exterior angles are given by
121 * exterior_angle. See the bokeh function for more information. */
122static float2 get_regular_polygon_vertex_position(const int vertex_index,
123 const float exterior_angle,
124 const float rotation)
125{
126 float angle = exterior_angle * vertex_index - rotation;
128}
129
130/* Find the closest point to the given point on the given line. This assumes the length of the
131 * given line is not zero. */
133 const float2 line_start,
134 const float2 line_end)
135{
136 float2 line_vector = line_end - line_start;
137 float2 point_vector = point - line_start;
138 float line_length_squared = math::dot(line_vector, line_vector);
139 float parameter = math::dot(point_vector, line_vector) / line_length_squared;
140 return line_start + line_vector * parameter;
141}
142
143/* Compute the value of the bokeh at the given point. The computed bokeh is essentially a regular
144 * polygon centered in space having the given circumradius. The regular polygon is one whose
145 * vertices' exterior angles are given by "exterior_angle", which relates to the number of vertices
146 * n through the equation "exterior angle = 2 pi / n". The regular polygon may additionally morph
147 * into a shape with the given properties:
148 *
149 * - The regular polygon may have a circular hole in its center whose radius is controlled by the
150 * "catadioptric" value.
151 * - The regular polygon is rotated by the "rotation" value.
152 * - The regular polygon can morph into a circle controlled by the "roundness" value, such that it
153 * becomes a full circle at unit roundness.
154 *
155 * The function returns 0 when the point lies inside the regular polygon and 1 otherwise. However,
156 * at the edges, it returns a narrow band gradient as a form of anti-aliasing. */
157static float bokeh(const float2 point,
158 const float circumradius,
159 const float exterior_angle,
160 const float rotation,
161 const float roundness,
162 const float catadioptric)
163{
164 if (circumradius == 0.0f) {
165 return 0.0f;
166 }
167
168 /* Get the index of the vertex of the regular polygon whose polar angle is maximum but less than
169 * the polar angle of the given point, taking rotation into account. This essentially finds the
170 * vertex closest to the given point in the clock-wise direction. */
171 float angle = math::mod_periodic(math::atan2(point.y, point.x) + rotation,
172 2.0f * std::numbers::pi_v<float>);
173 int vertex_index = int(angle / exterior_angle);
174
175 /* Compute the shortest distance between the origin and the polygon edge composed from the
176 * previously selected vertex and the one following it. */
178 vertex_index, exterior_angle, rotation) *
179 circumradius;
181 vertex_index + 1, exterior_angle, rotation) *
182 circumradius;
183 float2 closest_point = closest_point_on_line(point, first_vertex, second_vertex);
184 float distance_to_edge = math::length(closest_point);
185
186 /* Mix the distance to the edge with the circumradius, making it tend to the distance to a
187 * circle when roundness tends to 1. */
188 float distance_to_edge_round = math::interpolate(distance_to_edge, circumradius, roundness);
189
190 /* The point is outside of the bokeh, so we return 0. */
191 float distance = math::length(point);
192 if (distance > distance_to_edge_round) {
193 return 0.0f;
194 }
195
196 /* The point is inside the catadioptric hole and is not part of the bokeh, so we return 0. */
197 float catadioptric_distance = distance_to_edge_round * catadioptric;
198 if (distance < catadioptric_distance) {
199 return 0.0f;
200 }
201
202 /* The point is very close to the edge of the bokeh, so we return the difference between the
203 * distance to the edge and the distance as a form of anti-aliasing. */
204 if (distance_to_edge_round - distance < 1.0f) {
205 return distance_to_edge_round - distance;
206 }
207
208 /* The point is very close to the edge of the catadioptric hole, so we return the difference
209 * between the distance to the hole and the distance as a form of anti-aliasing. */
210 if (catadioptric != 0.0f && distance - catadioptric_distance < 1.0f) {
211 return distance - catadioptric_distance;
212 }
213
214 /* Otherwise, the point is part of the bokeh and we return 1. */
215 return 1.0f;
216}
217
218static float4 spectral_bokeh(const int2 texel,
219 const int2 size,
220 const float exterior_angle,
221 const float rotation,
222 const float roundness,
223 const float catadioptric,
224 const float lens_shift)
225{
226 /* Since we need the regular polygon to occupy the entirety of the output image, the circumradius
227 * of the regular polygon is half the width of the output image. */
228 float circumradius = float(size.x) / 2.0f;
229
230 /* Move the texel coordinates such that the regular polygon is centered. */
231 float2 point = float2(texel) + float2(0.5f) - circumradius;
232
233 /* Each of the color channels of the output image contains a bokeh with a different circumradius.
234 * The largest one occupies the whole image as stated above, while the other two have circumradii
235 * that are shifted by an amount that is proportional to the "lens_shift" value. The alpha
236 * channel of the output is the average of all three values. */
237 float min_shift = math::abs(lens_shift * circumradius);
238 float min = bokeh(
239 point, circumradius - min_shift, exterior_angle, rotation, roundness, catadioptric);
240
241 float median_shift = min_shift / 2.0f;
242 float median = bokeh(
243 point, circumradius - median_shift, exterior_angle, rotation, roundness, catadioptric);
244
245 float max = bokeh(point, circumradius, exterior_angle, rotation, roundness, catadioptric);
246 float4 bokeh = float4(min, median, max, (max + median + min) / 3.0f);
247
248 /* If the lens shift is negative, swap the min and max bokeh values, which are stored in the red
249 * and blue channels respectively. Note that we take the absolute value of the lens shift above,
250 * so the sign of the lens shift only controls this swap. */
251 if (lens_shift < 0.0f) {
252 bokeh = float4(bokeh.z, bokeh.y, bokeh.x, bokeh.w);
253 }
254
255 return bokeh;
256}
257
258void BokehKernel::compute_cpu(const int sides,
259 const float rotation,
260 const float roundness,
261 const float catadioptric,
262 const float lens_shift)
263{
264 const int2 size = this->result.domain().data_size;
265 const float exterior_angle = compute_exterior_angle(sides);
266 const float corrected_rotation = compute_rotation(rotation, sides);
267
268 parallel_for(size, [&](const int2 texel) {
269 const float4 bokeh_value = spectral_bokeh(
270 texel, size, exterior_angle, corrected_rotation, roundness, catadioptric, lens_shift);
271 this->result.store_pixel(texel, Color(bokeh_value));
272 });
273}
274
275/* --------------------------------------------------------------------
276 * Bokeh Kernel Container.
277 */
278
280{
281 /* First, delete all resources that are no longer needed. */
282 map_.remove_if([](auto item) { return !item.value->needed; });
283
284 /* Second, reset the needed status of the remaining resources to false to ready them to track
285 * their needed status for the next evaluation. */
286 for (auto &value : map_.values()) {
287 value->needed = false;
288 }
289}
290
292 int2 size,
293 int sides,
294 float rotation,
295 float roundness,
296 float catadioptric,
297 float lens_shift)
298{
299 const BokehKernelKey key(size, sides, rotation, roundness, catadioptric, lens_shift);
300
301 auto &bokeh_kernel = *map_.lookup_or_add_cb(key, [&]() {
302 return std::make_unique<BokehKernel>(
303 context, size, sides, rotation, roundness, catadioptric, lens_shift);
304 });
305
306 bokeh_kernel.needed = true;
307 return bokeh_kernel.result;
308}
309
310} // namespace blender::compositor
btScalar max
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
Result & get(Context &context, int2 size, int sides, float rotation, float roundness, float catadioptric, float lens_shift)
BokehKernelKey(int2 size, int sides, float rotation, float roundness, float catadioptric, float lens_shift)
BokehKernel(Context &context, int2 size, int sides, float rotation, float roundness, float catadioptric, float lens_shift)
Domain(const int2 &size)
Definition domain.cc:20
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_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
float distance(float, float) RET
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
static float4 spectral_bokeh(const int2 texel, const int2 size, const float exterior_angle, const float rotation, const float roundness, const float catadioptric, const float lens_shift)
bool operator==(const BokehKernelKey &a, const BokehKernelKey &b)
static float compute_exterior_angle(int sides)
static float2 get_regular_polygon_vertex_position(const int vertex_index, const float exterior_angle, const float rotation)
static float compute_rotation(float angle, int sides)
static float2 closest_point_on_line(const float2 point, const float2 line_start, const float2 line_end)
static float bokeh(const float2 point, const float circumradius, const float exterior_angle, const float rotation, const float roundness, const float catadioptric)
void parallel_for(const int2 range, const Function &function)
T cos(const AngleRadianBase< T > &a)
T length(const VecBase< T, Size > &a)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
T interpolate(const T &a, const T &b, const FactorT &t)
T atan2(const T &y, const T &x)
T mod_periodic(const T &a, const T &b)
T sin(const AngleRadianBase< T > &a)
T abs(const T &a)
unsigned __int64 uint64_t
Definition BKE_text.h:91
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
void GPU_shader_unbind()
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1f(gpu::Shader *sh, const char *name, float value)
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38