Blender V5.3
eevee_deferred_thickness_amend.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
8
9#pragma once
10
12#include "eevee_hiz.bsl.hh"
19
21
29
30 void eval([[resource_table]] ShadowRenderData &srd, LightData light, const bool is_directional)
31 {
32 if (light.tilemap_index == LIGHT_NO_SHADOW) {
33 return;
34 }
35
36 LightVector lv = LightVector::get(light, is_directional, P);
37 float attenuation = light_attenuation_surface(light, is_directional, lv);
38 attenuation *= light_attenuation_facing(light, lv.L, lv.dist, -Ng, true);
39
40 if (attenuation < LIGHT_ATTENUATION_THRESHOLD) {
41 return;
42 }
43
44 float texel_radius = shadow_texel_radius_at_position(
45 srd.uniforms, srd.views, light, is_directional, P);
46
47 float3 P_offset = P;
48 /* Invert all biases to get value inside the surface.
49 * The additional offset is to make the pcf kernel fully inside the object. */
50 float normal_offset = shadow_normal_offset(Ng, lv.L, texel_radius);
51 P_offset -= Ng * normal_offset;
52 /* Inverting this bias means we will over estimate the distance. Which removes some artifacts.
53 */
54 P_offset -= texel_radius * shadow_pcf_offset(lv.L, Ng, pcf_random);
55
56 float occluder_delta = srd.shadow_sample(is_directional, light, P_offset);
57 if (occluder_delta > 0.0f) {
58 float hit_distance = abs(occluder_delta);
59 /* Add back the amount of offset we added to the original position.
60 * This avoids self shadowing issue. */
61 hit_distance += (normal_offset + 1.0f) * texel_radius;
62
63 if ((hit_distance > gbuffer_thickness.value() * 0.001f) &&
64 (hit_distance < gbuffer_thickness.value() * 1.0f))
65 {
66 float weight = 1.0f;
67 saturate(dot(lv.L, -Ng));
68 thickness_accum += hit_distance * weight;
69 weight_accum += weight;
70 }
71 }
72 }
73
74 void eval_directional([[resource_table]] ShadowRenderData &srd, uint /*l_idx*/, LightData light)
75 {
76 eval(srd, light, true);
77 }
78 void eval_local([[resource_table]] ShadowRenderData &srd, uint /*l_idx*/, LightData light)
79 {
80 eval(srd, light, false);
81 }
82};
83
84} // namespace eevee::thickness
85
86namespace eevee {
89} // namespace eevee
90
91namespace eevee {
92
97
98struct VertOut {
100};
101
102[[vertex]]
103void amend_vert([[vertex_id]] const int vert_id,
104 [[out]] VertOut &v_out,
105 [[position]] float4 &out_position)
106{
107 fullscreen_vertex(vert_id, out_position, v_out.uv);
108}
109
110/* Early fragment test is needed to discard fragment that do not need this processing. */
114 [[resource_table]] const draw::View &views,
115 [[resource_table]] const LightRenderData &lrd,
116 [[resource_table]] const Uniform &uni,
117 [[resource_table]] const HiZ &hiz,
118 [[in]] const VertOut &v_out,
119 [[frag_coord]] const float4 frag_co)
120{
121 int2 texel = int2(frag_co.xy);
122
123 /* Bias the shading point position because of depth buffer precision.
124 * Constant is taken from https://www.terathon.com/gdc07_lengyel.pdf. */
125 constexpr float bias = 2.4e-7f;
126 const float depth = texelFetch(hiz.hiz_tx, texel, 0).r - bias;
127
128 const ViewMatrices view = views.get(0);
129
130 const float3 P = view.point_screen_to_world(float3(v_out.uv, depth));
131 const float vPz = dot(view.forward(), P) - dot(view.forward(), view.position());
132
133 const float3 Ng = gbuffer::normal_unpack(imageLoad(srt.gbuf_normal_img, int3(texel, 0)).rg);
134
135 uchar data_layer = uni.pipeline_buf.gbuffer_additional_data_layer_id;
136 float2 data_packed = imageLoad(srt.gbuf_normal_img, int3(texel, int(data_layer))).rg;
137 Thickness gbuffer_thickness = gbuffer::thickness_unpack(data_packed.x);
138 if (gbuffer_thickness.value() == 0.0f) {
139 return;
140 }
141
142 [[resource_table]] const Sampling &sampling = srd.sampling;
143
145 .P = P,
146 .Ng = Ng,
147 .gbuffer_thickness = gbuffer_thickness,
148 .thickness_accum = 0.0f,
149 .weight_accum = 0.0f,
150 .pcf_random = random::pcg_4d(float4(frag_co.xyz, sampling.rng_1D_get(SAMPLING_SHADOW_X))).xy,
151 };
152
153 light::foreach_visible(lrd, frag_co.xy, vPz, ctx, srd);
154
155 /* The apparent thickness of an object behind its surface considering all shadow maps
156 * available. If no shadow-map has a record of the other side of the surface, do not amend
157 * the gbuffer thickness. */
158 if (ctx.weight_accum == 0.0f) {
159 return;
160 }
161
162 float shadow_thickness = ctx.thickness_accum / ctx.weight_accum;
163 if (shadow_thickness < gbuffer_thickness.value()) {
164 data_packed.x = gbuffer::thickness_pack(
165 Thickness::from(shadow_thickness, gbuffer_thickness.mode()));
166 imageStore(srt.gbuf_normal_img, int3(texel, int(data_layer)), float4(data_packed, 0.0f, 0.0f));
167 }
168}
169
172 eevee::ShadowRenderConstants{.shadow_random = true});
173
174} // namespace eevee
static Thickness from(float value, ThicknessMode mode)
float value() const
ThicknessMode mode() const
float light_attenuation_facing(LightData light, float3 L, float distance_to_light, float3 Ng, const bool is_transmission)
#define LIGHT_ATTENUATION_THRESHOLD
float light_attenuation_surface(LightData light, const bool is_directional, LightVector lv)
#define LIGHT_NO_SHADOW
#define position
#define image(slot, qualifiers, format)
#define early_fragment_tests
#define frag_coord
#define smooth
#define resource_table
#define sampler(slot)
#define fragment
#define vertex_id
#define vertex
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define abs
IMG_TEMPLATE DataVec imageLoad(const T &, IntCoord) RET
ImageBase< float, 2, true > image2DArray
IMG_TEMPLATE void imageStore(T &, IntCoord, DataVec)
SamplerBase< uint, 2, false, true > usampler2DArray
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
unsigned int uchar
void fullscreen_vertex(int vertex_id, float4 &out_position)
void foreach_visible(const LightRenderData &srt, float2 pixel, float linear_view_z, CallbackT &cb, ResourceT &res)
PipelineGraphic deferred_thickness_amend(amend_vert, amend_frag, eevee::ShadowRenderConstants{.shadow_random=true})
void amend_vert(const int vert_id, VertOut &v_out, float4 &out_position)
float3 shadow_pcf_offset(float3 L, float3 Ng, float2 random)
void amend_frag(ThicknessAmend &srt, ShadowRenderData &srd, const draw::View &views, const LightRenderData &lrd, const Uniform &uni, const HiZ &hiz, const VertOut &v_out, const float4 frag_co)
float shadow_normal_offset(float3 Ng, float3 L, float texel_radius)
float shadow_texel_radius_at_position(const Uniform &uni, const draw::View &views, LightData light, const bool is_directional, float3 P)
float thickness_pack(Thickness thickness)
Thickness thickness_unpack(float thickness_packed)
float3 normal_unpack(float2 N_packed)
float4 pcg_4d(float4 v)
#define saturate(a)
Definition smaa.cc:319
static LightVector get(LightData light, const bool is_directional, float3 P)
float3 forward() const
float3 point_screen_to_world(float3 ssP) const
float3 position() const
float rng_1D_get(const eSamplingDimension dim) const
void eval_local(ShadowRenderData &srd, uint, LightData light)
void eval(ShadowRenderData &srd, LightData light, const bool is_directional)
void eval_directional(ShadowRenderData &srd, uint, LightData light)
float x