Blender V5.3
eevee_shadow_tag_usage_transparent.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
10
11#pragma once
12
13#include "draw_model.bsl.hh"
14#include "draw_view.bsl.hh"
16
17namespace eevee::shadow::usage {
18
19struct VertIn {
21};
22
30
33
34 [[storage(4, read)]] const ObjectBounds (&bounds_buf)[];
35
37 [[push_constant]] const int fb_lod;
38
39 float ray_aabb(float3 ray_origin, float3 ray_direction, float3 aabb_min, float3 aabb_max)
40 {
41 /* https://gdbooks.gitbooks.io/3dcollisions/content/Chapter3/raycast_aabb.html */
42 float3 t_mins = (aabb_min - ray_origin) / ray_direction;
43 float3 t_maxs = (aabb_max - ray_origin) / ray_direction;
44
45 float t_min = reduce_max(min(t_mins, t_maxs));
46 float t_max = reduce_min(max(t_mins, t_maxs));
47
48 /* AABB is in the opposite direction. */
49 if (t_max < 0.0f) {
50 return -1.0f;
51 }
52 /* No intersection. */
53 if (t_min > t_max) {
54 return -1.0f;
55 }
56 /* The ray origin is inside the aabb. */
57 if (t_min < 0.0f) {
58 /* For regular ray casting we would return t_max here,
59 * but we want to ray cast against the box volume, not just the surface. */
60 return 0.0f;
61 }
62 return t_min;
63 }
64
65 float pixel_size_at(const ViewMatrices view, float linear_depth)
66 {
67 float pixel_size = uniforms.uniform_buf.shadow.film_pixel_radius;
68 bool is_persp = (view.winmat[3][3] == 0.0f);
69 if (is_persp) {
70 pixel_size *= max(0.01f, linear_depth);
71 }
72 return pixel_size * exp2(float(fb_lod));
73 }
74
76 float3 vs_near_plane,
77 float3 vs_view_direction,
78 float near_t,
79 float far_t,
80 float3 &sphere_center,
81 float &sphere_radius)
82 {
83 float near_pixel_size = pixel_size_at(view, near_t);
84 float3 near_center = vs_near_plane + vs_view_direction * near_t;
85
86 float far_pixel_size = pixel_size_at(view, far_t);
87 float3 far_center = vs_near_plane + vs_view_direction * far_t;
88
89 sphere_center = mix(near_center, far_center, 0.5f);
90 sphere_radius = 0;
91
92 for (int x = -1; x <= 1; x += 2) {
93 for (int y = -1; y <= 1; y += 2) {
94 float3 near_corner = near_center + (near_pixel_size * 0.5f * float3(x, y, 0));
95 sphere_radius = max(sphere_radius, length_squared(near_corner - sphere_center));
96
97 float3 far_corner = far_center + (far_pixel_size * 0.5f * float3(x, y, 0));
98 sphere_radius = max(sphere_radius, length_squared(far_corner - sphere_center));
99 }
100 }
101
102 sphere_center = view.point_view_to_world(sphere_center);
103 sphere_radius = sqrt(sphere_radius);
104 }
105
106 /* Inflate bounds by half a pixel as a conservative rasterization alternative,
107 * to ensure the tiles needed by all LOD0 pixels get tagged */
109 const ViewMatrices view, const ObjectMatrices obj, float3 ls_center, float3 &P, float3 &lP)
110 {
111 float3 vP = view.point_world_to_view(P);
112
113 float inflate_scale = uniforms.uniform_buf.shadow.film_pixel_radius * exp2(float(fb_lod));
114 if (view.is_perspective()) {
115 inflate_scale *= -vP.z;
116 }
117 /* Half-pixel. */
118 inflate_scale *= 0.5f;
119
120 float3 ws_inflate_vector = obj.normal_object_to_world(sign(lP - ls_center));
121 float3 vs_inflate_vector = view.normal_world_to_view(ws_inflate_vector);
122 vs_inflate_vector.z = 0;
123 /* Scale the vector so the largest axis length is 1 */
124 vs_inflate_vector /= reduce_max(abs(vs_inflate_vector.xy));
125 vs_inflate_vector *= inflate_scale;
126
127 vP += vs_inflate_vector;
128 P = view.point_view_to_world(vP);
129 lP = obj.point_world_to_object(P);
130 }
131};
132
133/* Warning: Only works for valid, finite, positive floats. */
134float nextafter(float value)
135{
136 return uintBitsToFloat(floatBitsToUint(value) + 1);
137}
138
139[[vertex]]
141 [[resource_table]] TagUsage & /*tag*/,
142 [[instance_index]] const int inst_index,
143 [[resource_table]] const draw::View &views,
144 [[resource_table]] const draw::Model &models,
145 [[resource_table]] draw::Resource &res_id,
146 [[vertex_id]] [[maybe_unused]] const int vert_id,
147 [[in]] const VertIn &v_in,
148 [[out]] VertOut &v_out,
149 [[position]] float4 &out_position)
150{
151 draw::ID id = res_id.get(inst_index);
152 uint resource_id = id.resource_id<1>();
153
154 const ObjectMatrices obj = models.get(resource_id);
155 const ViewMatrices view = views.get(0);
156
157 v_out.P = float3(0.0);
158 v_out.vP = float3(0.0);
159 v_out.ls_aabb_min = float3(0.0);
160 v_out.ls_aabb_max = float3(0.0);
161 v_out.resource_id = resource_id;
162
163 ObjectBounds bounds = srt.bounds_buf[resource_id];
164 if (!drw_bounds_are_valid(bounds)) {
165 /* Discard. */
166 out_position = float4(NAN_FLT);
167 return;
168 }
169
170 Box box = shape_box(bounds.bounding_corners[0].xyz,
171 bounds.bounding_corners[0].xyz + bounds.bounding_corners[1].xyz,
172 bounds.bounding_corners[0].xyz + bounds.bounding_corners[2].xyz,
173 bounds.bounding_corners[0].xyz + bounds.bounding_corners[3].xyz);
174
175 float3 ws_aabb_min = bounds.bounding_corners[0].xyz;
176 float3 ws_aabb_max = bounds.bounding_corners[0].xyz + bounds.bounding_corners[1].xyz +
177 bounds.bounding_corners[2].xyz + bounds.bounding_corners[3].xyz;
178
179 float3 ls_center = obj.point_world_to_object(midpoint(ws_aabb_min, ws_aabb_max));
180
181 float3 ls_conservative_min = float3(FLT_MAX);
182 float3 ls_conservative_max = float3(-FLT_MAX);
183
184 for (int i = 0; i < 8; i++) {
185 float3 P = box.corners[i];
187 srt.inflate_bounds(view, obj, ls_center, P, lP);
188
189 ls_conservative_min = min(ls_conservative_min, lP);
190 ls_conservative_max = max(ls_conservative_max, lP);
191 }
192
193 v_out.ls_aabb_min = ls_conservative_min;
194 v_out.ls_aabb_max = ls_conservative_max;
195
196 float3 lP = mix(ls_conservative_min, ls_conservative_max, max(float3(0), v_in.pos));
197
198 v_out.P = obj.point_object_to_world(lP);
199 v_out.vP = view.point_world_to_view(v_out.P);
200
201 out_position = view.point_world_to_homogenous(v_out.P);
202
203#if 0
204 if (vert_id == 0) {
205 Box debug_box = shape_box(
206 ls_conservative_min,
207 ls_conservative_min + (ls_conservative_max - ls_conservative_min) * float3(1, 0, 0),
208 ls_conservative_min + (ls_conservative_max - ls_conservative_min) * float3(0, 1, 0),
209 ls_conservative_min + (ls_conservative_max - ls_conservative_min) * float3(0, 0, 1));
210 for (int i = 0; i < 8; i++) {
211 debug_box.corners[i] = obj.point_object_to_world(debug_box.corners[i]);
212 }
213 drw_debug(debug_box);
214 }
215#endif
216}
217
218[[fragment]]
220 [[resource_table]] const draw::View &views,
221 [[resource_table]] const draw::Model &models,
222 [[resource_table]] TagUsage &tag,
223 [[resource_table]] const HiZ &hiz,
224 [[in]] const VertOut interp,
225 [[frag_coord]] const float4 frag_co)
226{
227 const ViewMatrices view = views.get(0);
228 const ObjectMatrices obj = models.get(interp.resource_id);
229
230 float2 screen_uv = frag_co.xy / float2(srt.fb_resolution);
231
232 float opaque_depth = texelFetch(hiz.hiz_tx, int2(frag_co.xy), srt.fb_lod).r;
233 float3 ws_opaque = view.point_screen_to_world(float3(screen_uv, opaque_depth));
234
235 float3 ws_near_plane = view.point_screen_to_world(float3(screen_uv, 0.0f));
236 float3 ws_view_direction = normalize(interp.P - ws_near_plane);
237 float3 vs_near_plane = view.point_screen_to_view(float3(screen_uv, 0.0f));
238 float3 vs_view_direction = normalize(interp.vP - vs_near_plane);
239 float3 ls_near_plane = obj.point_world_to_object(ws_near_plane);
240 float3 ls_view_direction = normalize(obj.point_world_to_object(interp.P) - ls_near_plane);
241
242 /* TODO (Miguel Pozo): We could try to ray-cast against the non-inflated bounds first,
243 * and fall back to the inflated ones if there is no hit.
244 * The inflated bounds can cause unnecessary extra steps. */
245 float ls_near_box_t = srt.ray_aabb(
246 ls_near_plane, ls_view_direction, interp.ls_aabb_min, interp.ls_aabb_max);
247
248 if (ls_near_box_t < 0.0f) {
249 /* The ray cast can fail in ortho mode due to numerical precision. (See #121629) */
250 return;
251 }
252
253 float3 ls_near_box = ls_near_plane + ls_view_direction * ls_near_box_t;
254 float3 ws_near_box = obj.point_object_to_world(ls_near_box);
255
256 float near_box_t = distance(ws_near_plane, ws_near_box);
257 float far_box_t = distance(ws_near_plane, interp.P);
258 /* Depth test. */
259 far_box_t = min(far_box_t, distance(ws_near_plane, ws_opaque));
260
261 /* Ray march from the front to the back of the bbox, and tag shadow usage along the way. */
262 float step_size;
263 /* In extreme cases, step_size can be smaller than the next representable float delta, so we use
264 * nextafter to prevent infinite loops. (See #137566) */
265 for (float t = near_box_t; t <= far_box_t; t = max(t + step_size, nextafter(t))) {
266 /* Ensure we don't get past far_box_t. */
267 t = min(t, far_box_t);
268 step_size = srt.pixel_size_at(view, t);
269
270 float3 P = ws_near_plane + (ws_view_direction * t);
271 float step_radius;
272 srt.step_bounding_sphere(
273 views.get(0), vs_near_plane, vs_view_direction, t, t + step_size, P, step_radius);
274 float3 vP = view.point_world_to_view(P);
275
276 float2 pixel = frag_co.xy * exp2(float(srt.fb_lod));
277
278 TagPixelCtx ctx = {
279 .P = P,
280 .V = view.world_incident_vector(P),
281 .radius = step_radius,
282 .lod_bias = 0,
283 };
284
285 light::foreach_visible(tag.light_data, pixel, vP.z, ctx, tag);
286 }
287}
288
289} // namespace eevee::shadow::usage
290
291namespace eevee::shadow {
292
294
295} // namespace eevee::shadow
#define NAN_FLT
btScalar max
void drw_debug(Box shape, float4 color)
bool drw_bounds_are_valid(ObjectBounds bounds)
Box shape_box(float3 v000, float3 v100, float3 v010, float3 v001)
#define FLT_MAX
#define instance_index
#define storage(slot, qualifiers)
#define position
#define flat
#define frag_coord
#define smooth
#define push_constant
#define resource_table
#define attribute(slot)
#define fragment
#define vertex_id
#define vertex
#define exp2
VecBase< float, D > normalize(VecOp< float, D >) RET
constexpr T sign(T) RET
#define abs
float distance(float, float) RET
VecBase< uint, D > floatBitsToUint(VecOp< float, D >) RET
#define sqrt
VecBase< float, D > uintBitsToFloat(VecOp< uint, D >) RET
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
int midpoint(int a, int b)
float length_squared(VecT a)
float reduce_min(float2 a)
float reduce_max(float2 a)
ccl_device_inline float interp(const float a, const float b, const float t)
Definition math_base.h:549
void foreach_visible(const LightRenderData &srt, float2 pixel, float linear_view_z, CallbackT &cb, ResourceT &res)
void tag_usage_vert(TagUsageTransparent &srt, TagUsage &, const int inst_index, const draw::View &views, const draw::Model &models, draw::Resource &res_id, const int vert_id, const VertIn &v_in, VertOut &v_out, float4 &out_position)
void tag_usage_frag(TagUsageTransparent &srt, const draw::View &views, const draw::Model &models, TagUsage &tag, const HiZ &hiz, const VertOut interp, const float4 frag_co)
PipelineGraphic tag_usage_transparent(usage::tag_usage_vert, usage::tag_usage_frag)
#define mix
#define min(a, b)
Definition sort.cc:38
float3 corners[8]
float4 bounding_corners[4]
float3 point_world_to_object(float3 P) const
float3 point_object_to_world(float3 lP) const
bool is_perspective() const
float3 point_world_to_view(float3 P) const
float4 point_world_to_homogenous(float3 P) const
float3 point_screen_to_world(float3 ssP) const
float3 point_view_to_world(float3 vP) const
float3 world_incident_vector(float3 P) const
float3 point_screen_to_view(float3 ssP) const
const UniformData & uniform_buf
void inflate_bounds(const ViewMatrices view, const ObjectMatrices obj, float3 ls_center, float3 &P, float3 &lP)
float ray_aabb(float3 ray_origin, float3 ray_direction, float3 aabb_min, float3 aabb_max)
void step_bounding_sphere(const ViewMatrices view, float3 vs_near_plane, float3 vs_view_direction, float near_t, float far_t, float3 &sphere_center, float &sphere_radius)
float pixel_size_at(const ViewMatrices view, float linear_depth)
float z
Definition sky_math.h:136