Blender V5.3
eevee_depth_of_field_shared.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#pragma once
12
14
16#include "eevee_defines.hh"
17
18#ifndef GPU_SHADER
19namespace blender::eevee {
20#endif
21
22/* 5% error threshold. */
23static constexpr float DOF_FAST_GATHER_COC_ERROR = 0.05f;
24static constexpr int DOF_GATHER_RING_COUNT = 5;
25static constexpr int DOF_DILATE_RING_COUNT = 3;
26
62
71
72static inline float coc_radius_from_camera_depth(DepthOfFieldData dof, float depth)
73{
74 depth = (dof.camera_type != CAMERA_ORTHO) ? 1.0f / depth : depth;
75 return dof.coc_mul * depth + dof.coc_bias;
76}
77
78static inline float regular_polygon_side_length(float sides_count)
79{
80 return 2.0f * sinf(EEVEE_PI / sides_count);
81}
82
83/* Returns intersection ratio between the radius edge at theta and the regular polygon edge.
84 * Start first corners at theta == 0. */
85static inline float circle_to_polygon_radius(float sides_count, float theta)
86{
87 /* From Graphics Gems from CryENGINE 3 (SIGGRAPH 2013) by Tiago Sousa (slide 36). */
88 float side_angle = (2.0f * EEVEE_PI) / sides_count;
89 return cosf(side_angle * 0.5f) /
90 cosf(theta - side_angle * floorf((sides_count * theta + EEVEE_PI) / (2.0f * EEVEE_PI)));
91}
92
93/* Remap input angle to have homogenous spacing of points along a polygon edge.
94 * Expects theta to be in [0..2pi] range. */
95static inline float circle_to_polygon_angle(float sides_count, float theta)
96{
97 float side_angle = (2.0f * EEVEE_PI) / sides_count;
98 float halfside_angle = side_angle * 0.5f;
99 float side = floorf(theta / side_angle);
100 /* Length of segment from center to the middle of polygon side. */
101 float adjacent = circle_to_polygon_radius(sides_count, 0.0f);
102
103 /* This is the relative position of the sample on the polygon half side. */
104 float local_theta = theta - side * side_angle;
105 float ratio = (local_theta - halfside_angle) / halfside_angle;
106
107 float halfside_len = regular_polygon_side_length(sides_count) * 0.5f;
108 float opposite = ratio * halfside_len;
109
110 /* NOTE: atan(y_over_x) has output range [-pi/2..pi/2]. */
111 float final_local_theta = atanf(opposite / adjacent);
112
113 return side * side_angle + final_local_theta;
114}
115
116#ifndef GPU_SHADER
117} // namespace blender::eevee
118#endif
#define host_shared
#define EEVEE_PI
static constexpr int DOF_GATHER_RING_COUNT
static float circle_to_polygon_angle(float sides_count, float theta)
static float regular_polygon_side_length(float sides_count)
static constexpr float DOF_FAST_GATHER_COC_ERROR
static float coc_radius_from_camera_depth(DepthOfFieldData dof, float depth)
static float circle_to_polygon_radius(float sides_count, float theta)
static constexpr int DOF_DILATE_RING_COUNT
unsigned int uint
Definition BKE_text.h:55
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
#define floorf
#define atanf
#define sinf
#define cosf