Blender V5.3
eevee_shadow_tag_usage.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 "eevee_defines.hh"
14#include "eevee_hiz.bsl.hh"
24
26
27struct TagUsage {
33
34 public:
35 void tag_usage_tile(LightData light, uint2 tile_co, int lod, int tilemap_index)
36 {
37 if (tilemap_index > light.tilemap_max_get()) {
38 return;
39 }
40
41 tile_co >>= uint(lod);
42 int index = shadow_tile_offset(tile_co, tilemaps.tilemaps_buf[tilemap_index].tiles_index, lod);
43 atomicOr(tiles.tiles_buf[index], uint(SHADOW_IS_USED));
44 }
45
46 void tag_usage_tilemap_directional_at_level(uint l_idx, float3 P, int level)
47 {
48 LightData light = light_data.light_buf[l_idx];
49
50 if (light.tilemap_index == LIGHT_NO_SHADOW) {
51 return;
52 }
53
55
56 level = clamp(level, light.sun.clipmap_lod_min, light.sun.clipmap_lod_max);
57
60 }
61
63 uint l_idx, float3 P, float3 V, float radius, int lod_bias)
64 {
65 LightData light = light_data.light_buf[l_idx];
66
67 if (light.tilemap_index == LIGHT_NO_SHADOW) {
68 return;
69 }
70
72
73 LightSunData sun = light.sun;
74
75 if (radius == 0.0f) {
76 int level = shadow_directional_level(light, lP - light.position());
77 level = clamp(level + lod_bias, sun.clipmap_lod_min, sun.clipmap_lod_max);
80 }
81 else {
82 float3 start_lP = light_world_to_local_direction(light, P - V * radius);
83 float3 end_lP = light_world_to_local_direction(light, P + V * radius);
84 int min_level = shadow_directional_level(light, start_lP - light.position());
85 int max_level = shadow_directional_level(light, end_lP - light.position());
86 min_level = clamp(min_level + lod_bias, sun.clipmap_lod_min, sun.clipmap_lod_max);
87 max_level = clamp(max_level + lod_bias, sun.clipmap_lod_min, sun.clipmap_lod_max);
88
89 for (int level = min_level; level <= max_level; level++) {
91 light, lP - float3(radius, radius, 0.0f), level);
93 light, lP + float3(radius, radius, 0.0f), level);
94
95 for (uint x = coord_min.tilemap_tile.x; x <= coord_max.tilemap_tile.x; x++) {
96 for (uint y = coord_min.tilemap_tile.y; y <= coord_max.tilemap_tile.y; y++) {
97 tag_usage_tile(light, uint2(x, y), 0, coord_min.tilemap_index);
98 }
99 }
100 }
101 }
102 }
103
104 void tag_usage_tilemap_punctual(uint l_idx, float3 P, float radius, int lod_bias)
105 {
106 LightData light = light_data.light_buf[l_idx];
107
108 if (light.tilemap_index == LIGHT_NO_SHADOW) {
109 return;
110 }
111
113 float dist_to_light = max(length(lP) - radius, 1e-5f);
114 if (dist_to_light > light.local.local.influence_radius_max) {
115 return;
116 }
117 if (is_spot_light(light.type)) {
118 /* Early out if out of cone. */
119 float angle_tan = length(lP.xy / dist_to_light);
120 if (angle_tan > light.spot.spot_tan) {
121 return;
122 }
123 }
124 else if (is_area_light(light.type)) {
125 /* Early out if on the wrong side. */
126 if (lP.z - radius > 0.0f) {
127 return;
128 }
129 }
130
131 /* Transform to shadow local space. */
132 lP -= light.local.local.shadow_position;
133
134 const ViewMatrices view = views.get(0);
135
137 lP,
138 view.is_perspective(),
139 view.z_distance(P),
140 uniforms.uniform_buf.shadow.film_pixel_radius);
141 lod = clamp(lod + lod_bias, 0, SHADOW_TILEMAP_LOD);
142
143 if (radius == 0.0f) {
144 int face_id = shadow_punctual_face_index_get(lP);
148 }
149 else {
150 uint faces = 0u;
151 for (int x = -1; x <= 1; x += 2) {
152 for (int y = -1; y <= 1; y += 2) {
153 for (int z = -1; z <= 1; z += 2) {
154 float3 _lP = lP + float3(x, y, z) * radius;
155 faces |= 1u << shadow_punctual_face_index_get(_lP);
156 }
157 }
158 }
159
160 for (int face_id = 0; face_id < 6; face_id++) {
161 if ((faces & (1u << uint(face_id))) == 0u) {
162 continue;
163 }
164
165 int tilemap_index = light.tilemap_index + face_id;
167
168 float3 offset = float3(radius, radius, 0);
169 ShadowCoordinates coord_min = shadow_punctual_coordinates(light, _lP - offset, face_id);
170 ShadowCoordinates coord_max = shadow_punctual_coordinates(light, _lP + offset, face_id);
171
172 for (uint x = coord_min.tilemap_tile.x; x <= coord_max.tilemap_tile.x; x++) {
173 for (uint y = coord_min.tilemap_tile.y; y <= coord_max.tilemap_tile.y; y++) {
174 tag_usage_tile(light, uint2(x, y), lod, tilemap_index);
175 }
176 }
177 }
178 }
179 }
180};
181
190 float radius;
192
193 void eval_directional(TagUsage &srt, uint index, LightData /*light*/)
194 {
196 }
197
198 void eval_local(TagUsage &srt, uint index, LightData /*light*/)
199 {
201 }
202};
203
207
208 void eval_directional(TagUsage &srt, uint index, LightData /*light*/)
209 {
211 }
212
213 void eval_local(TagUsage &srt, uint index, LightData /*light*/)
214 {
216 }
217};
218
219} // namespace eevee::shadow::usage
220
221namespace eevee::light {
222
224 const LightRenderData &,
225 float2,
226 float,
229
232
233} // namespace eevee::light
234
235namespace eevee::shadow::usage {
236
240
245[[compute, local_size(SHADOW_DEPTH_SCAN_GROUP_SIZE, SHADOW_DEPTH_SCAN_GROUP_SIZE)]]
247 [[resource_table]] const HiZ &hiz,
248 [[resource_table]] const draw::View &views,
249 [[resource_table]] TagUsage &tag,
250 [[global_invocation_id]] const uint3 global_id)
251{
252 int2 texel = int2(global_id.xy);
253 int2 tex_size = srt.input_depth_extent;
254
255 if (!in_range_inclusive(texel, int2(0), int2(tex_size - 1))) {
256 return;
257 }
258
259 float depth = texelFetch(hiz.hiz_tx, texel, 0).r;
260 if (depth == 1.0f) {
261 return;
262 }
263
264 const ViewMatrices view = views.get(0);
265
266 float2 uv = (float2(texel) + 0.5f) / float2(tex_size);
267 float3 vP = view.point_screen_to_view(float3(uv, depth));
268
269 TagPixelCtx ctx = {
270 .P = view.point_view_to_world(vP),
271 .V = float3(0.0f),
272 .radius = 0.0f,
273 .lod_bias = 0,
274 };
275
276 light::foreach_visible(tag.light_data, float2(global_id.xy), vP.z, ctx, tag);
277}
278
282
284 [[storage(SURFEL_BUF_SLOT, read_write)]] Surfel (&surfel_buf)[];
285 [[storage(CAPTURE_BUF_SLOT, read)]] CaptureInfoData &capture_info_buf;
286};
287
294 [[resource_table]] TagUsage &tag,
295 [[resource_table]] SurfelCapture &capture,
296 [[global_invocation_id]] const uint3 global_id)
297{
298 uint index = global_id.x;
299 if (index >= capture.capture_info_buf.surfel_len) {
300 return;
301 }
302
303 Surfel surfel = capture.surfel_buf[index];
304
305 TagSurfelCtx ctx = {
306 .P = surfel.position,
307 .directional_lvl = srt.directional_level,
308 };
309
310 light::foreach(tag.light_data, ctx, tag);
311}
312
318 [[resource_table]] TagUsage &tag,
319 [[resource_table]] const Uniform &uni,
320 [[resource_table]] const draw::View &views,
321 [[resource_table]] const Sampling &sampling,
322 [[resource_table]] const HiZ &hiz,
323 [[global_invocation_id]] const uint3 global_id)
324{
325 int3 froxel = int3(global_id);
326
327 if (any(greaterThanEqual(froxel, uni.uniform_buf.volumes.tex_size))) {
328 return;
329 }
330
331 float3 extinction = imageLoadFast(volume.in_extinction_img, froxel).rgb;
332 float3 scattering = imageLoadFast(volume.in_scattering_img, froxel).rgb;
333
334 if (is_zero(extinction) || is_zero(scattering)) {
335 return;
336 }
337
338 const ViewMatrices view = views.get(0);
339
340 float offset = sampling.rng_1D_get(SAMPLING_VOLUME_W);
341 float jitter = random::interleaved_gradient(float2(froxel.xy), 0.0f, offset);
342
343 float3 uvw = (float3(froxel) + float3(0.5f, 0.5f, jitter)) *
344 uni.uniform_buf.volumes.inv_tex_size;
345 float3 ss_P = volume_resolve_to_screen(uni, view, uvw);
346 float3 vP = view.point_screen_to_view(float3(ss_P.xy, ss_P.z));
347 float3 P = view.point_view_to_world(vP);
348
349 float depth = texelFetch(hiz.hiz_tx, froxel.xy, uni.uniform_buf.volumes.tile_size_lod).r;
350 if (depth < ss_P.z) {
351 return;
352 }
353
354 float2 pixel = ((float2(froxel.xy) + 0.5f) * uni.uniform_buf.volumes.inv_tex_size.xy) *
355 uni.uniform_buf.volumes.main_view_extent;
356
357 int bias = uni.uniform_buf.volumes.tile_size_lod;
358
359 TagPixelCtx ctx = {
360 .P = P,
361 .V = view.world_incident_vector(P),
362 .radius = 0.01f,
363 .lod_bias = bias,
364 };
365
366 light::foreach_visible(tag.light_data, pixel, vP.z, ctx, tag);
367}
368
369} // namespace eevee::shadow::usage
370
371namespace eevee::shadow {
372
376
377} // namespace eevee::shadow
btScalar max
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
#define SURFEL_GROUP_SIZE
#define CAPTURE_BUF_SLOT
#define VOLUME_GROUP_SIZE
#define SURFEL_BUF_SLOT
float3 light_world_to_local_point(LightData light, float3 point)
float3 light_world_to_local_direction(LightData light, float3 L)
#define LIGHT_NO_SHADOW
ShadowCoordinates shadow_directional_coordinates_at_level(LightData light, float3 lP, int level)
int shadow_tile_offset(uint2 tile, int tiles_index, int lod)
ShadowCoordinates shadow_punctual_coordinates(LightData light, float3 lP, int face_id)
int shadow_punctual_face_index_get(float3 lL)
int shadow_punctual_level(LightData light, float3 lP, bool is_perspective, float distance_to_camera, float film_pixel_radius)
float3 shadow_punctual_local_position_to_face_local(int face_id, float3 lL)
int shadow_directional_level(LightData light, float3 lP)
float3 volume_resolve_to_screen(const eevee::Uniform &uni, const ViewMatrices view, float3 coord)
#define storage(slot, qualifiers)
#define index(i)
#define push_constant
#define resource_table
#define global_invocation_id
#define compute
#define local_size(...)
VecBase< bool, D > greaterThanEqual(VecOp< T, D >, VecOp< T, D >) RET
bool any(VecOp< bool, D >) RET
constexpr T clamp(T, U, U) RET
float length(VecOp< float, D >) RET
int atomicOr(int &, int) RET
#define imageLoadFast
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
bool is_zero(float3x3 a)
#define in_range_inclusive(val, min_v, max_v)
template void light::foreach< shadow::usage::TagSurfelCtx, shadow::usage::TagUsage >(const LightRenderData &, shadow::usage::TagSurfelCtx &, shadow::usage::TagUsage &)
void foreach(const LightRenderData &srt, CallbackT &cb, ResourceT &res)
void foreach_visible(const LightRenderData &srt, float2 pixel, float linear_view_z, CallbackT &cb, ResourceT &res)
template void light::foreach_visible< shadow::usage::TagPixelCtx, shadow::usage::TagUsage >(const LightRenderData &, float2, float, shadow::usage::TagPixelCtx &, shadow::usage::TagUsage &)
void tag_usage_surfel(TagUsageSurfel &srt, TagUsage &tag, SurfelCapture &capture, const uint3 global_id)
void tag_usage_opaque(TagUsageOpaque &srt, const HiZ &hiz, const draw::View &views, TagUsage &tag, const uint3 global_id)
void tag_usage_volume(UnifiedVolumeProperties &volume, TagUsage &tag, const Uniform &uni, const draw::View &views, const Sampling &sampling, const HiZ &hiz, const uint3 global_id)
PipelineCompute tag_usage_surfels(usage::tag_usage_surfel)
PipelineCompute tag_usage_volume(usage::tag_usage_volume)
PipelineCompute tag_usage_opaque(usage::tag_usage_opaque)
float interleaved_gradient(float2 pixel, float seed, float offset)
bool is_perspective() const
float z_distance(float3 P) const
float3 point_view_to_world(float3 vP) const
float3 world_incident_vector(float3 P) const
float3 point_screen_to_view(float3 ssP) const
ViewMatrices get(uint view_id) const
const LightData(& light_buf)[]
const UniformData & uniform_buf
ShadowTileMapData(& tilemaps_buf)[]
void eval_directional(TagUsage &srt, uint index, LightData)
void eval_local(TagUsage &srt, uint index, LightData)
void eval_directional(TagUsage &srt, uint index, LightData)
void eval_local(TagUsage &srt, uint index, LightData)
void tag_usage_tilemap_punctual(uint l_idx, float3 P, float radius, int lod_bias)
void tag_usage_tilemap_directional_at_level(uint l_idx, float3 P, int level)
void tag_usage_tile(LightData light, uint2 tile_co, int lod, int tilemap_index)
void shadow_tag_usage_tilemap_directional(uint l_idx, float3 P, float3 V, float radius, int lod_bias)
float z
Definition sky_math.h:136
CCL_NAMESPACE_BEGIN struct Window V