Blender V4.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.h"
11
12#include "GPU_shader.hh"
13
14#include "COM_bokeh_kernel.hh"
15#include "COM_context.hh"
16#include "COM_result.hh"
17#include "COM_utilities.hh"
18
20
21/* --------------------------------------------------------------------
22 * Bokeh Kernel Key.
23 */
24
26 int2 size, int sides, float rotation, float roundness, float catadioptric, float lens_shift)
27 : size(size),
28 sides(sides),
29 rotation(rotation),
30 roundness(roundness),
31 catadioptric(catadioptric),
32 lens_shift(lens_shift)
33{
34}
35
41
43{
44 return a.size == b.size && a.sides == b.sides && a.rotation == b.rotation &&
45 a.roundness == b.roundness && a.catadioptric == b.catadioptric &&
46 a.lens_shift == b.lens_shift;
47}
48
49/* --------------------------------------------------------------------
50 * Bokeh Kernel.
51 */
52
53/* The exterior angle is the angle between each two consecutive vertices of the regular polygon
54 * from its center. */
55static float compute_exterior_angle(int sides)
56{
57 return (M_PI * 2.0f) / sides;
58}
59
60static float compute_rotation(float angle, int sides)
61{
62 /* Offset the rotation such that the second vertex of the regular polygon lies on the positive
63 * y axis, which is 90 degrees minus the angle that it makes with the positive x axis assuming
64 * the first vertex lies on the positive x axis. */
65 const float offset = M_PI_2 - compute_exterior_angle(sides);
66 return angle - offset;
67}
68
70 int2 size,
71 int sides,
72 float rotation,
73 float roundness,
74 float catadioptric,
75 float lens_shift)
76 : result(context.create_result(ResultType::Color))
77{
78 GPUShader *shader = context.get_shader("compositor_bokeh_image");
79 GPU_shader_bind(shader);
80
81 GPU_shader_uniform_1f(shader, "exterior_angle", compute_exterior_angle(sides));
82 GPU_shader_uniform_1f(shader, "rotation", compute_rotation(rotation, sides));
83 GPU_shader_uniform_1f(shader, "roundness", roundness);
84 GPU_shader_uniform_1f(shader, "catadioptric", catadioptric);
85 GPU_shader_uniform_1f(shader, "lens_shift", lens_shift);
86
87 this->result.allocate_texture(Domain(size), false);
88 this->result.bind_as_image(shader, "output_img");
89
91
92 this->result.unbind_as_image();
94}
95
97{
98 this->result.release();
99}
100
101/* --------------------------------------------------------------------
102 * Bokeh Kernel Container.
103 */
104
106{
107 /* First, delete all resources that are no longer needed. */
108 map_.remove_if([](auto item) { return !item.value->needed; });
109
110 /* Second, reset the needed status of the remaining resources to false to ready them to track
111 * their needed status for the next evaluation. */
112 for (auto &value : map_.values()) {
113 value->needed = false;
114 }
115}
116
118 int2 size,
119 int sides,
120 float rotation,
121 float roundness,
122 float catadioptric,
123 float lens_shift)
124{
125 const BokehKernelKey key(size, sides, rotation, roundness, catadioptric, lens_shift);
126
127 auto &bokeh_kernel = *map_.lookup_or_add_cb(key, [&]() {
128 return std::make_unique<BokehKernel>(
129 context, size, sides, rotation, roundness, catadioptric, lens_shift);
130 });
131
132 bokeh_kernel.needed = true;
133 return bokeh_kernel.result;
134}
135
136} // namespace blender::realtime_compositor
#define M_PI_2
#define M_PI
void GPU_shader_uniform_1f(GPUShader *sh, const char *name, float value)
void GPU_shader_bind(GPUShader *shader)
void GPU_shader_unbind()
struct GPUShader GPUShader
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)
local_group_size(16, 16) .push_constant(Type b
static float compute_exterior_angle(int sides)
bool operator==(const BokehKernelKey &a, const BokehKernelKey &b)
static float compute_rotation(float angle, int sides)
void compute_dispatch_threads_at_least(GPUShader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:131
VecBase< float, 4 > float4
uint64_t get_default_hash(const T &v)
Definition BLI_hash.hh:219
unsigned __int64 uint64_t
Definition stdint.h:90