Blender V5.3
eevee_forward_lib.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
5#pragma once
6
12
13#include "draw_model.bsl.hh"
22#include "gpu_shader_codegen_lib.glsl"
23
24namespace eevee {
25
27 const ShadingData &sd,
28 const ViewMatrices view,
29 uint resource_id,
31 float2 frag_co,
32 float3 &radiance,
33 float3 &transmittance)
34{
35 const eevee::PipelineConstants &pipe = kg.pipe;
37 LightEvalIterator &lights = kg.light_eval;
38 const Uniform &uni = kg.uniforms;
39 LightprobeRenderData &lightprobes = kg.lightprobes;
40 LightprobePlaneRenderData &lightprobe_planes = kg.lightprobe_planes;
41 LightEvalData &srt = lights.inner;
42 draw::Infos &infos = kg.infos;
43
44 float vPz = dot(view.forward(), sd.P) - dot(view.forward(), view.position());
46
48 for (uint i = 0u; i < 3; i++) [[unroll]] {
50 ClosureUndetermined cl = sd.closure_get(pipe, uchar(i)).data;
51 ctx.stack.cl[i] = closure_light_new(kg.util_tx, cl, V);
52 }
53 }
54
55 ctx.P = sd.P;
56 ctx.Ng = sd.Ng;
57 ctx.texel = frag_co;
58 ctx.thickness = thickness;
59
60 /* TODO(fclem): If transmission (no SSS) is present, we could reduce LIGHT_CLOSURE_EVAL_COUNT
61 * by 1 for this evaluation and skip evaluating the transmission closure twice. */
62 ObjectInfos object_infos = infos.get(resource_id);
66 ctx.ray_count = uni.uniform_buf.shadow.ray_count;
67 ctx.ray_step_count = uni.uniform_buf.shadow.step_count;
68
69 lights.eval_reflection(ctx, vPz);
70
72 ClosureUndetermined cl_transmit = sd.closure_get(pipe, 0).data;
73 if (closure_has_transmission(cl_transmit.type) ||
74 cl_transmit.type == CLOSURE_BSSRDF_BURLEY_ID)
75 {
77 ctx_tr.stack.cl[0] = closure_light_new(kg.util_tx, cl_transmit, V, thickness);
78
79 /* NOTE: Only evaluates `stack.cl[0]`. */
80 lights.eval_transmission(ctx_tr, vPz);
81
82 if (cl_transmit.type == CLOSURE_BSSRDF_BURLEY_ID) {
83 if (pipe.use_sss) [[static_branch]] {
84 /* Apply transmission profile onto transmitted light and sum with reflected light. */
85 float3 sss_profile = subsurface_transmission(
86 kg.util_tx, to_closure_subsurface(cl_transmit).sss_radius, thickness.value());
87 ctx.stack.cl[0].light_shadowed += ctx_tr.stack.cl[0].light_shadowed * sss_profile;
88 ctx.stack.cl[0].light_unshadowed += ctx_tr.stack.cl[0].light_unshadowed * sss_profile;
89 }
90 }
91 else {
92 ctx.stack.cl[0].light_shadowed = ctx_tr.stack.cl[0].light_shadowed;
93 ctx.stack.cl[0].light_unshadowed = ctx_tr.stack.cl[0].light_unshadowed;
94 }
95 }
96 }
97
98 LightProbeSample samp = lightprobes.load(frag_co, sd.P, sd.N, V);
99
100 float clamp_indirect_sh = uni.uniform_buf.clamp.surface_indirect;
102 clamp_indirect_sh);
103
104 /* Planar reflection. */
105 float3 planar_probe_radiance = float3(0.0f);
106 float3 average_N = sd.Ng * 0.001f;
107 /* Disable if only rough surfaces. */
108 if (pipe.use_reflection) [[static_branch]] {
109 /* Get average normal. */
110 for (uint i = 0u; i < 3; i++) [[unroll]] {
112 ClosureUndetermined cl = sd.closure_get(pipe, uchar(i)).data;
113 average_N += cl.N * cl.weight();
114 }
115 }
116 average_N = safe_normalize(average_N);
117
118 const int planar_id = lightprobe_planes.select_probe(sd.P, average_N);
119
120 if (planar_id == -1) {
121 average_N = float3(0.0f);
122 }
123 else {
125 lightprobe_planes.probe_planar_buf[planar_id], sd.P, average_N, V);
126
127 float2 ndc_P_reflected = view.point_world_to_ndc(P_reflected).xy;
128 /* Planar probes are rendered upside down. */
129 ndc_P_reflected.y = -ndc_P_reflected.y;
130 float2 texel = view.ndc_to_screen(ndc_P_reflected);
131
132 planar_probe_radiance =
133 textureLod(lightprobe_planes.planar_radiance_tx, float3(texel, planar_id), 0.0).rgb;
134 /* Discard background hits. */
135 if (textureLod(lightprobe_planes.planar_depth_tx, float3(texel, planar_id), 0.0).r ==
136 reverse_z::read(1.0f))
137 {
138 average_N = float3(0.0f);
139 }
140 }
141 }
142
143 /* Combine all radiance. */
144 float3 radiance_direct = float3(0.0f);
145 float3 radiance_indirect = float3(0.0f);
146
147 for (uint i = 0u; i < 3; i++) [[unroll]] {
149 ClosureUndetermined cl = sd.closure_get_resolved(pipe, uchar(i), 1.0f);
150 if (cl.weight() > CLOSURE_WEIGHT_CUTOFF) {
151 float3 direct_light = ctx.stack.cl[i].light_shadowed;
152 float3 indirect_light = lightprobes.eval(samp, cl, sd.P, V, thickness);
153
154 if (kg.pipe.use_reflection) [[static_branch]] {
155 if (cl.type == CLOSURE_BSDF_MICROFACET_GGX_REFLECTION_ID) {
156 const float blend = saturate(to_closure_reflection(cl).roughness * -10.0f + 1.0f) *
157 saturate(dot(average_N, cl.N) * 100.0f - 99.0f);
158 indirect_light = mix(indirect_light, planar_probe_radiance, blend);
159 }
160 }
161
162 if ((cl.type == CLOSURE_BSDF_TRANSLUCENT_ID ||
164 (thickness.value() != 0.0f))
165 {
166 /* We model two transmission event, so the surface color need to be applied twice. */
167 cl.color *= cl.color;
168 }
169
170 radiance_direct += direct_light * cl.color;
171 radiance_indirect += indirect_light * cl.color;
172 }
173 }
174 }
175 /* Light clamping. */
176 float clamp_direct = uni.uniform_buf.clamp.surface_direct;
177 float clamp_indirect = uni.uniform_buf.clamp.surface_indirect;
178
179 radiance_direct = colorspace::brightness_clamp_max(radiance_direct, clamp_direct);
180 radiance_indirect = colorspace::brightness_clamp_max(radiance_indirect, clamp_indirect);
181
182 radiance_direct *= uni.uniform_buf.clamp.direct_scale;
183 radiance_indirect *= uni.uniform_buf.clamp.indirect_scale;
184
185 radiance = radiance_direct + radiance_indirect + sd.emission;
186
187 transmittance = sd.transmittance;
188 }
189 else {
190 radiance = float3(0.0f);
191 transmittance = float3(0.0f);
192 }
193}
194
195} // namespace eevee
uint receiver_light_set_get(ObjectInfos object_infos)
ClosureLight closure_light_new(const UtilityTexture &util_tx, ClosureUndetermined cl, float3 V, Thickness thickness)
#define CLOSURE_WEIGHT_CUTOFF
float3 subsurface_transmission(const UtilityTexture &util, float3 sss_radii, float thickness)
#define static_branch
#define unroll
float dot(VecOp< float, D >, VecOp< float, D >) RET
TEX_TEMPLATE DataVec textureLod(T, FltCoord, float) RET
unsigned int uchar
VecT safe_normalize(VecT vector)
@ CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID
@ CLOSURE_BSSRDF_BURLEY_ID
@ CLOSURE_BSDF_TRANSLUCENT_ID
float3 brightness_clamp_max(float3 color, float limit)
EvalCtx< true > init_from_reflect_ctx(EvalCtx< false > ctx)
float3 parallax(PlanarProbeData planar, float3 P, float3 N, float3 V)
void forward_lighting_eval(KernelGlobals &kg, const ShadingData &sd, const ViewMatrices view, uint resource_id, Thickness thickness, float2 frag_co, float3 &radiance, float3 &transmittance)
T read(T depth_buffer_value)
SphericalHarmonicL1< float4 > clamp_energy(SphericalHarmonicL1< float4 > sh, float clamp_value)
#define mix
#define saturate(a)
Definition smaa.cc:319
eevee::LightprobePlaneRenderData lightprobe_planes
eevee::PipelineConstants pipe
eevee::LightEvalIterator light_eval
eevee::LightprobeRenderData lightprobes
float shadow_terminator_normal_offset
float shadow_terminator_geometry_offset
Reservoir< ClosureUndetermined > closure_get(const eevee::PipelineConstants &pipe, uchar i) const
packed_float3 transmittance
ClosureUndetermined closure_get_resolved(const eevee::PipelineConstants &pipe, uchar i, float additional_weight) const
packed_float3 emission
float3 forward() const
T ndc_to_screen(T ndc_P) const
float3 point_world_to_ndc(float3 P) const
float3 position() const
float3 world_incident_vector(float3 P) const
ObjectInfos get(uint resource_id) const
LightEvalConstants constants
void eval_transmission(light::EvalCtx< true > &ctx, float vPz)
void eval_reflection(light::EvalCtx< false > &ctx, float vPz)
SphericalHarmonicL1< float4 > volume_irradiance
PlanarProbeData(& probe_planar_buf)[PLANAR_PROBE_MAX]
int select_probe(float3 P, float3 N) const
float3 eval(const UtilityTexture &util_tx, LightProbeSample samp, ClosureSubsurface cl, float3, float3, Thickness thickness) const
LightProbeSample load(float2 screen_texel, float3 P, float3 Ng, float3 V) const
const UniformData & uniform_buf
ClosureStack< is_transmission > stack
float y
CCL_NAMESPACE_BEGIN struct Window V