Blender V5.3
eevee_ray_types_lib.bsl.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
5#pragma once
6
10#include "gpu_shader_ray.bsl.hh"
11
12#if 0
13/* Screen-space ray ([0..1] "uv" range) where direction is normalize to be as small as one
14 * full-resolution pixel. The ray is also clipped to all frustum sides.
15 * Z component is device normalized Z (aka. depth buffer value).
16 * W component is device normalized Z + Thickness.
17 */
18struct ScreenSpaceRay {
21 float max_time;
22
23};
24#endif
25
26/* Screen-space ray ([0..1] "uv" range) where direction is normalize to be as small as one
27 * full-resolution pixel. The ray is also clipped to all frustum sides.
28 * Z component is device normalized Z (aka. depth buffer value).
29 * W component is device normalized Z + Thickness.
30 */
34 float max_time;
35
36 static ScreenSpaceRay from_start_end(float4 hs_start, float4 hs_end, float2 pixel_size)
37 {
38 /* Constant bias (due to depth buffer precision). Helps with self intersection.
39 * Magic numbers for 24bits of precision from
40 * http://terathon.com/gdc07_lengyel.pdf (slide 26)
41 * Increased factor from 2 to 7 to work around
42 * https://projects.blender.org/blender/blender/issues/159634. */
43 constexpr float bias = -2.4e-7f * 7.0f;
44 hs_start.z += bias;
45 hs_end.z += bias;
46
47 hs_start.xyz /= hs_start.w;
48 hs_end.xyz /= hs_end.w;
49
51 ray.direction = hs_end - hs_start;
52 ray.origin = hs_start;
53 /* If the line is degenerate, make it cover at least one pixel
54 * to not have to handle zero-pixel extent as a special case later */
55 if (length_squared(ray.direction.xy) < 0.00001f) {
56 ray.direction.xy = float2(0.0f, 0.00001f);
57 }
58 float ray_len_sqr = length_squared(ray.direction.xyz);
59 /* Make direction cover one pixel. */
60 bool is_more_vertical = abs(ray.direction.x / pixel_size.x) <
61 abs(ray.direction.y / pixel_size.y);
62 ray.direction /= (is_more_vertical) ? abs(ray.direction.y) : abs(ray.direction.x);
63 ray.direction *= (is_more_vertical) ? pixel_size.y : pixel_size.x;
64 /* Clip to segment's end. */
65 ray.max_time = sqrt(ray_len_sqr * safe_rcp(length_squared(ray.direction.xyz)));
66 /* Clipping to frustum sides. */
67 float clip_dist = line_unit_box_intersect_dist_safe(ray.origin.xyz, ray.direction.xyz);
68 ray.max_time = min(ray.max_time, clip_dist);
69 /* Convert to texture coords [0..1] range. */
70 ray.origin.xyz = ray.origin.xyz * 0.5f + 0.5f;
71 ray.direction.xyz *= 0.5f;
72 return ray;
73 }
74
75 static ScreenSpaceRay create(ViewMatrices view, Ray ray, float2 pixel_size)
76 {
77 float4 start = view.point_view_to_homogenous(ray.origin);
78 float4 end = view.point_view_to_homogenous(ray.origin + ray.direction * ray.max_time);
79
80 return ScreenSpaceRay::from_start_end(start, end, pixel_size);
81 }
82
83 static ScreenSpaceRay create(Ray ray, float4x4 winmat, float2 pixel_size)
84 {
85 float4 start = winmat * float4(ray.origin, 1.0f);
86 float4 end = winmat * float4(ray.origin + ray.direction * ray.max_time, 1.0f);
87
88 return ScreenSpaceRay::from_start_end(start, end, pixel_size);
89 }
90
92 {
93 return origin.xyz + direction.xyz * t;
94 }
95};
96
97/* Estimates the thickness of an occluder pixel along a screen ray. */
99 /* Z slope of the N previous steps (n-1, n-2). */
101 /* Depth buffer value of the previous sample. */
103 /* Ray T of the previous sample. */
104 float prev_t;
105
106 static ScreenThicknessEstimator init(float start_z)
107 {
108 ScreenThicknessEstimator estimator;
109 estimator.prev_ss_z_slope = float2(0.0f);
110 estimator.prev_ss_z = start_z;
111 estimator.prev_t = 0.0f;
112 return estimator;
113 }
114
123 float thickness(float sample_ss_z, float sample_ss_t, float sample_min_thickness)
124 {
125 float delta_t = sample_ss_t - prev_t;
126 /* Slope is negative if getting closer to the camera and positive if going away. */
127 float delta_z = sample_ss_z - prev_ss_z;
128 float slope = delta_z / delta_t;
129 /* Treat this sample as a segment between the previous sample and this one.
130 * This avoids loosing the surface when ray is parallel to the view plane. */
131 float thickness = abs(delta_z);
132 /* Treat abrupt change of slope as different objects.
133 * Increasing this number reduces the porosity of surfaces almost parallel to the view but
134 * might introduce connection between very thin objects and their background. */
136 thickness = 0.0f; /* Disconnect from previous sample. */
137 }
138
139 /* Minimum thickness. */
140 thickness = max(thickness, sample_min_thickness);
141 /* Update state. */
143 prev_ss_z_slope.x = slope;
144 prev_t = sample_ss_t;
145 prev_ss_z = sample_ss_z;
146 return thickness;
147 }
148
159 bool intersect(float sample_ss_z,
160 float sample_ss_t,
161 float sample_min_thickness,
162 float ray_ss_z,
163 float ray_ss_z_prev)
164 {
165 float sample_thickness = thickness(sample_ss_z, sample_ss_t, sample_min_thickness);
166 /* We want to test the intersection between the surface estimated AABB and the ray step AABB.
167 * This is equivalent to adding the step delta to the surface thickness and doing an AABB vs
168 * point test. */
169 sample_thickness += abs(ray_ss_z - ray_ss_z_prev);
170
171 float sample_min = sample_ss_z;
172 float sample_max = sample_ss_z + sample_thickness;
173 return ray_ss_z >= sample_min && ray_ss_z <= sample_max;
174 }
175};
btScalar max
float line_unit_box_intersect_dist_safe(float3 line_origin, float3 line_direction)
bool any(VecOp< bool, D >) RET
VecBase< bool, D > greaterThan(VecOp< T, D >, VecOp< T, D >) RET
#define abs
#define sqrt
float safe_rcp(float a)
float length_squared(VecT a)
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
VecBase< float, 2 > float2
VecBase< float, 3 > float3
#define min(a, b)
Definition sort.cc:38
float max_time
packed_float3 direction
packed_float3 origin
static ScreenSpaceRay create(Ray ray, float4x4 winmat, float2 pixel_size)
static ScreenSpaceRay from_start_end(float4 hs_start, float4 hs_end, float2 pixel_size)
static ScreenSpaceRay create(ViewMatrices view, Ray ray, float2 pixel_size)
float3 screen_position_at(float t)
static ScreenThicknessEstimator init(float start_z)
float thickness(float sample_ss_z, float sample_ss_t, float sample_min_thickness)
bool intersect(float sample_ss_z, float sample_ss_t, float sample_min_thickness, float ray_ss_z, float ray_ss_z_prev)
float4 point_view_to_homogenous(float3 vP) const