65 if (context.use_gpu()) {
66 this->compute_gpu(context, sides, rotation, roundness, catadioptric, lens_shift);
69 this->compute_cpu(sides, rotation, roundness, catadioptric, lens_shift);
82 return (std::numbers::pi * 2.0f) / sides;
91 return angle - offset;
94void BokehKernel::compute_gpu(
Context &context,
97 const float roundness,
98 const float catadioptric,
99 const float lens_shift)
123 const float exterior_angle,
124 const float rotation)
126 float angle = exterior_angle * vertex_index - rotation;
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;
158 const float circumradius,
159 const float exterior_angle,
160 const float rotation,
161 const float roundness,
162 const float catadioptric)
164 if (circumradius == 0.0f) {
172 2.0f * std::numbers::pi_v<float>);
173 int vertex_index = int(
angle / exterior_angle);
178 vertex_index, exterior_angle, rotation) *
181 vertex_index + 1, exterior_angle, rotation) *
188 float distance_to_edge_round =
math::interpolate(distance_to_edge, circumradius, roundness);
192 if (
distance > distance_to_edge_round) {
197 float catadioptric_distance = distance_to_edge_round * catadioptric;
198 if (
distance < catadioptric_distance) {
204 if (distance_to_edge_round -
distance < 1.0f) {
205 return distance_to_edge_round -
distance;
210 if (catadioptric != 0.0f &&
distance - catadioptric_distance < 1.0f) {
211 return distance - catadioptric_distance;
220 const float exterior_angle,
221 const float rotation,
222 const float roundness,
223 const float catadioptric,
224 const float lens_shift)
228 float circumradius =
float(size.x) / 2.0f;
237 float min_shift =
math::abs(lens_shift * circumradius);
239 point, circumradius - min_shift, exterior_angle, rotation, roundness, catadioptric);
241 float median_shift = min_shift / 2.0f;
242 float median =
bokeh(
243 point, circumradius - median_shift, exterior_angle, rotation, roundness, catadioptric);
245 float max =
bokeh(point, circumradius, exterior_angle, rotation, roundness, catadioptric);
251 if (lens_shift < 0.0f) {
258void BokehKernel::compute_cpu(
const int sides,
259 const float rotation,
260 const float roundness,
261 const float catadioptric,
262 const float lens_shift)
270 texel, size, exterior_angle, corrected_rotation, roundness, catadioptric, lens_shift);
282 map_.remove_if([](
auto item) {
return !item.value->needed; });
286 for (
auto &value : map_.values()) {
287 value->needed =
false;
299 const BokehKernelKey key(size, sides, rotation, roundness, catadioptric, lens_shift);
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);
306 bokeh_kernel.needed =
true;
307 return bokeh_kernel.result;
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
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)
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)
void unbind_as_image() const
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
float distance(float, float) RET
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
ColorSceneLinear4f< eAlpha::Premultiplied > Color
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)
unsigned __int64 uint64_t
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
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)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float