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