Blender V5.3
eevee_shadow_tag_update.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
13
14#pragma once
15
16#include "draw_shader_shared.hh"
17#include "eevee_defines.hh"
22
23namespace eevee::shadow {
24
25struct TagUpdate {
27
28 [[storage(5, read)]] const ObjectBounds (&bounds_buf)[];
29 [[storage(6, read)]] const uint (&resource_ids_buf)[];
30};
31
32struct VertIn {
34};
35
36struct VertOut {
38};
39
40[[vertex]]
42 [[resource_table]] TileMaps &tilemaps,
43 [[instance_id]] const int inst_per_tilemap_id,
44 [[in]] const VertIn &v_in,
45 [[out]] VertOut &v_out,
46 [[position]] float4 &out_position)
47{
48 v_out.tilemap_index = uint(inst_per_tilemap_id) % uint(srt.tilemap_count);
49 const uint inst_id = uint(inst_per_tilemap_id) / uint(srt.tilemap_count);
50 const uint resource_id = srt.resource_ids_buf[inst_id] & 0x7FFFFFFFu;
51
52 ObjectBounds bounds = srt.bounds_buf[resource_id];
53 if (!drw_bounds_are_valid(bounds)) {
54 out_position = float4(NAN_FLT);
55 return;
56 }
57
58 ShadowTileMapData tilemap = tilemaps.tilemaps_buf[v_out.tilemap_index];
59
60 if (is_local_light(tilemap.light_type)) {
61 /* Coarse culling to avoid objects beyond the far plane to appear in the flattened projection.
62 * This can result in a lot of false positive for small lights. */
63 float vs_center_z = transform_point(tilemap.viewmat, bounds.bounding_sphere.xyz).z;
64 if (vs_center_z + bounds.bounding_sphere.w < -tilemap.clip_far) {
65 out_position = float4(NAN_FLT);
66 return;
67 }
68 }
69
70 const float3 ls_N = v_in.pos;
71 /* Convert from -1..1 box shape to 0..1 box. */
72 const float3 ls_P = max(float3(0), v_in.pos);
73
74 const float3 P = ls_P.x * bounds.bounding_corners[1].xyz +
75 ls_P.y * bounds.bounding_corners[2].xyz +
76 ls_P.z * bounds.bounding_corners[3].xyz + bounds.bounding_corners[0].xyz;
77
78 /* These normalize should in theory never fail since the bounding boxes are inflated
79 * to never be flat. */
80 const float3 N = ls_N.x * normalize(float3(bounds.bounding_corners[1].xyz)) +
81 ls_N.y * normalize(float3(bounds.bounding_corners[2].xyz)) +
82 ls_N.z * normalize(float3(bounds.bounding_corners[3].xyz));
83
84 const float3 vs_P = transform_point(tilemap.viewmat, P);
85 const float3 vs_N = transform_direction(tilemap.viewmat, N);
86
87 /* Since the aspect ratio is always 1:1 we can use the view normal as the clip space expand
88 * direction. */
89 const float2 expand_dir = vs_N.xy;
90
91 out_position = tilemap.winmat * float4(vs_P, 1.0f);
92
93 /* To emulate conservative rasterization, we inflate the bounding box by 1 pixel. */
94 const float ndc_pixel_size = 2.0f / float(SHADOW_TILEMAP_RES);
95 out_position.xy += sign(expand_dir) * (ndc_pixel_size * out_position.w);
96
97 /* Make sure to bring all the geometry inside the view frustum.
98 * Mimics an infinite projection matrix. */
99 out_position.z *= 1e-5f;
100}
101
102[[fragment]]
104 [[resource_table]] TileMaps &tilemaps,
105 [[frag_coord]] const float4 frag_coord,
106 [[in]] const VertOut &v_out)
107{
108 ShadowTileMapData tilemap = tilemaps.tilemaps_buf[v_out.tilemap_index];
109
110 uint2 texel = uint2(frag_coord.xy);
111 /* Tag only LOD0. The lower LOD will be written by the tag_propagate pass. */
112 int tile_index = shadow_tile_offset(texel, tilemap.tiles_index, 0);
113 /* Tag using a transient flag that allows to differentiate new updates (to be propagated to LODs)
114 * from the existing persistent flag SHADOW_DO_UPDATE. */
115 atomicOr(tiles.tiles_buf[tile_index], uint(SHADOW_TAG_UPDATE));
116}
117
118/* Propagate the LOD0 update tag to the lower LOD tiles. */
119[[compute, local_size(SHADOW_TILEMAP_RES, SHADOW_TILEMAP_RES)]]
121 [[resource_table]] TileMaps &tilemaps,
122 [[global_invocation_id]] const uint3 global_id)
123{
124 ShadowTileMapData tilemap = tilemaps.tilemaps_buf[global_id.z];
125
126 uint2 texel = uint2(global_id.xy);
127 const int tile_index_lod0 = shadow_tile_offset(texel, tilemap.tiles_index, 0);
128 uint tile_data = tiles.tiles_buf[tile_index_lod0];
129 bool do_update = (tile_data & uint(SHADOW_TAG_UPDATE)) != 0;
130 if (do_update) {
131 /* Remove the transient flag. */
132 tile_data &= ~uint(SHADOW_TAG_UPDATE);
133 /* Assign persistent flag. */
134 tile_data |= uint(SHADOW_DO_UPDATE);
135 tiles.tiles_buf[tile_index_lod0] = tile_data;
136 }
137
138 if (do_update) {
139 /* TODO(fclem): Recursive downsampling. */
140 for (int lod = 1; lod <= SHADOW_TILEMAP_LOD; lod++) {
141 int tile_index = shadow_tile_offset(texel >> lod, tilemap.tiles_index, lod);
142 atomicOr(tiles.tiles_buf[tile_index], uint(SHADOW_DO_UPDATE));
143 }
144 }
145}
146
149
150} // namespace eevee::shadow
#define NAN_FLT
btScalar max
bool drw_bounds_are_valid(ObjectBounds bounds)
int shadow_tile_offset(uint2 tile, int tiles_index, int lod)
#define instance_id
#define storage(slot, qualifiers)
#define position
#define flat
#define frag_coord
#define push_constant
#define resource_table
#define attribute(slot)
#define global_invocation_id
#define fragment
#define compute
#define vertex
#define local_size(...)
VecBase< float, D > normalize(VecOp< float, D >) RET
constexpr T sign(T) RET
int atomicOr(int &, int) RET
float3 transform_direction(float3x3 mat, float3 direction)
float3 transform_point(float3x3 mat, float3 point)
ccl_gpu_kernel_postfix ccl_global KernelWorkTile * tiles
const int tile_index
#define N
void tag_update_frag(Tiles &tiles, TileMaps &tilemaps, const float4 frag_coord, const VertOut &v_out)
void tag_update_vert(TagUpdate &srt, TileMaps &tilemaps, const int inst_per_tilemap_id, const VertIn &v_in, VertOut &v_out, float4 &out_position)
PipelineCompute tag_update_propagate(tag_propagate)
PipelineGraphic tag_update(tag_update_vert, tag_update_frag)
void tag_propagate(Tiles &tiles, TileMaps &tilemaps, const uint3 global_id)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float4 bounding_corners[4]
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136