Blender V5.3
eevee_surf_shadow.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
21
23{
24 float3 transmittance = sd.transmittance;
25 closure_weights_reset(kg, sd, 0.0f);
26 return float4(0.0f, 0.0f, 0.0f, saturate(1.0f - average(transmittance)));
27}
28
29namespace eevee {
30
31struct SurfShadow {
33 read)]] const uint (&render_map_buf)[SHADOW_RENDER_MAP_SIZE];
34
36};
37
42 [[resource_table]] const Uniform &uni,
43 [[resource_table]] const draw::View &views,
44 [[resource_table]] const Sampling &sampling,
45 [[resource_table]] const UtilityTexture & /*util_tx*/,
46 [[in]] const VertOutCommon &interp,
47 [[in]] const VertOutShadow &shadow_iface,
48 [[in]] const VertOutShadowClipping &shadow_clip,
49 [[in]] [[condition(is_curves)]] const VertOutCurves &curves_interp,
50 [[in]] [[condition(is_pointcloud)]] const VertOutPointcloud &ptcloud_interp,
51 [[in]] [[condition(is_gsplat)]] const VertOutGSplat &gsplat_interp,
52 [[bary_coord]] [[condition(use_barycentric)]] const float3 bary_co,
53 [[front_facing]] const bool front_face,
54 [[frag_coord]] const float4 frag_co)
55{
56 float linear_depth = length(shadow_clip.position);
57
58 /* Clip to light shape. */
59 if (dot(shadow_clip.vector, shadow_clip.vector) < 1.0f) {
61 return;
62 }
63
64 if (pipe.use_transparency) [[static_branch]] {
65 const ViewMatrices view = views.get(shadow_iface.shadow_view_id);
66
67 float3 barycentric_co = float3(0.0f);
68 if (pipe.use_barycentric) [[static_branch]] {
69 barycentric_co = bary_co;
70 }
71
72 ShadingData sd = init_globals(pipe, uni, interp, view, front_face, frag_co);
73 if (pipe.is_mesh) [[static_branch]] {
74 init_globals_mesh(interp, sd, barycentric_co);
75 }
76 else if (pipe.is_curves) [[static_branch]] {
77 init_globals_curves(interp, curves_interp, sd, view);
78 }
79 else if (pipe.is_pointcloud) [[static_branch]] {
80 init_globals_pointcloud(ptcloud_interp, sd);
81 }
82
83 nodetree_surface(kg, sd, 0.0f);
84
85 if (pipe.is_gsplat) [[static_branch]] {
87 }
88
89 float noise_offset = sampling.rng_1D_get(SAMPLING_TRANSPARENCY);
90 float random_threshold = random::pcg_4d(float4(sd.P, noise_offset)).x;
91
92 float transparency = average(sd.transmittance);
93 if (transparency > random_threshold) {
95 return;
96 }
97 }
98
99 int2 texel_co = int2(frag_co.xy);
100
101 /* Using bitwise ops is way faster than integer ops. */
102 constexpr int page_shift = SHADOW_PAGE_LOD;
103 constexpr int page_mask = int(~(0xFFFFFFFFu << SHADOW_PAGE_LOD));
104
105 int2 tile_co = texel_co >> page_shift;
106 int2 texel_page = texel_co & page_mask;
107
108 int view_index = shadow_iface.shadow_view_id;
109
110 int render_page_index = shadow_render_page_index_get(view_index, tile_co);
111 uint page_packed = srt.render_map_buf[render_page_index];
112
113 int3 page = int3(shadow_page_unpack(page_packed));
114 /* If the page index is invalid this page shouldn't be rendered,
115 * however shadow_page_unpack clamps the result to a valid page.
116 * Instead of doing an early return (and introducing branching),
117 * we simply ensure the page layer is out-of-bounds. */
118 page.z = page_packed < SHADOW_MAX_PAGE ? page.z : -1;
119
120 int3 out_texel = int3((page.xy << page_shift) | texel_page, page.z);
121
122 uint u_depth = floatBitsToUint(linear_depth);
123
124 /* Bias to avoid rounding errors on very large clip values.
125 * This can happen easily after the addition of the world volume
126 * versioning script in 4.2.
127 * +1 should be enough but for some reason, some artifacts
128 * are only removed if adding 2 ULP.
129 * This is equivalent of calling `next_after`, but without the safety. */
130 u_depth += 2;
131
132 if (uni.uniform_buf.shadow.use_debug_cost) {
133 imageAtomicAdd(srt.shadow_atlas_img, out_texel, 1u);
134 }
135 else {
136 imageAtomicMin(srt.shadow_atlas_img, out_texel, u_depth);
137 }
138}
139
140} // namespace eevee
#define SHADOW_ATLAS_IMG_SLOT
#define SHADOW_RENDER_MAP_BUF_SLOT
Closure nodetree_surface(KernelGlobals &, ShadingData &, float)
void closure_weights_reset(KernelGlobals &kg, ShadingData &sd, float closure_rand)
int shadow_render_page_index_get(int view_index, int2 tile_coordinate_in_lod)
float4 closure_to_rgba_shadow(KernelGlobals &kg, ShadingData &sd, Closure)
#define texture_atomic
#define storage(slot, qualifiers)
#define image(slot, qualifiers, format)
#define front_facing
#define bary_coord
#define frag_coord
#define resource_table
#define static_branch
#define fragment
#define condition(cond)
void gpu_discard_fragment()
float dot(VecOp< float, D >, VecOp< float, D >) RET
VecBase< uint, D > floatBitsToUint(VecOp< float, D >) RET
float length(VecOp< float, D >) RET
ImageBase< uint, 2, true, true > uimage2DArrayAtomic
IMG_TEMPLATE uint imageAtomicAdd(T &, IntCoord, uint) RET
IMG_TEMPLATE uint imageAtomicMin(T &, IntCoord, uint) RET
float average(float2 a)
ccl_device_inline float interp(const float a, const float b, const float t)
Definition math_base.h:549
ShadingData init_globals(const eevee::PipelineConstants &pipe, const eevee::Uniform &uni, const VertOutCommon &interp, const ViewMatrices view, bool front_face, float4 fragment_co)
float3 gsplat_amend_transmittance(const VertOutGSplat &gsplat_interp, float3 transmittance)
void init_globals_mesh(const VertOutCommon &interp, ShadingData &sd, float3 barycentric_co)
void init_globals_pointcloud(const VertOutPointcloud &ptcloud_interp, ShadingData &sd)
void init_globals_curves(const VertOutCommon &interp, const VertOutCurves &curve_interp, ShadingData &sd, const ViewMatrices view)
void surf_shadow(KernelGlobals &kg, PipelineConstants &pipe, SurfShadow &srt, const Uniform &uni, const draw::View &views, const Sampling &sampling, const UtilityTexture &, const VertOutCommon &interp, const VertOutShadow &shadow_iface, const VertOutShadowClipping &shadow_clip, const VertOutCurves &curves_interp, const VertOutPointcloud &ptcloud_interp, const VertOutGSplat &gsplat_interp, const float3 bary_co, const bool front_face, const float4 frag_co)
float4 pcg_4d(float4 v)
#define saturate(a)
Definition smaa.cc:319
packed_float3 transmittance
const uint(& render_map_buf)[SHADOW_RENDER_MAP_SIZE]
uimage2DArrayAtomic shadow_atlas_img
float x
Definition sky_math.h:225