Blender V5.3
eevee_lightprobe_sphere.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
8#include "eevee_defines.hh"
14
15namespace eevee {
16
17SphereProbePixelArea reinterpret_as_write_coord(int4 packed_coord)
18{
19 SphereProbePixelArea unpacked;
20 unpacked.offset = packed_coord.xy;
21 unpacked.extent = packed_coord.z;
22 unpacked.layer = packed_coord.w;
23 return unpacked;
24}
25
26SphereProbeUvArea reinterpret_as_atlas_coord(int4 packed_coord)
27{
28 SphereProbeUvArea unpacked;
29 unpacked.offset = intBitsToFloat(packed_coord.xy);
30 unpacked.scale = intBitsToFloat(packed_coord.z);
31 unpacked.layer = intBitsToFloat(packed_coord.w);
32 return unpacked;
33}
34
35} // namespace eevee
36
38
39/* local_texel is the texel coordinate inside the probe area [0..texel_area.extent) range.
40 * Returned vector is not normalized. */
41float3 texel_to_direction(float2 local_texel, SphereProbePixelArea texel_area, float2 &sampling_uv)
42{
43 /* UV in sampling area. No half pixel bias to texel as the octahedral map edges area lined up
44 * with texel center. Note that we don't use the last row & column of pixel, hence the -2 instead
45 * of -1. See miplvl_scale_bias. */
46 sampling_uv = local_texel / float2(texel_area.extent - 2);
47 /* Direction in world space. */
48 return octahedral_uv_to_direction(sampling_uv);
49}
50
51/* local_texel is the texel coordinate inside the probe area [0..texel_area.extent) range.
52 * Returned vector is not normalized. */
53float3 texel_to_direction(float2 local_texel, SphereProbePixelArea texel_area)
54{
55 float2 sampling_uv_unused;
56 return texel_to_direction(local_texel, texel_area, sampling_uv_unused);
57}
58
59/* Apply correct bias and scale for the given level of detail. */
60float2 miplvl_scale_bias(float mip_lvl, SphereProbeUvArea uv_area, float2 uv)
61{
62 /* Add 0.5 to avoid rounding error. */
63 int mip_0_res = int(float(SPHERE_PROBE_ATLAS_RES) * uv_area.scale + 0.5f);
64 float mip_lvl_res = float(mip_0_res >> int(mip_lvl));
65 float mip_lvl_res_inv = 1.0f / mip_lvl_res;
66 /* We place texel centers at the edges of the octahedron, to avoid artifacts caused by
67 * interpolating across the edges.
68 * The first pixel scaling aligns all the border edges (half pixel border).
69 * The second pixel scaling aligns the center edges (odd number of pixel). */
70 float scale = (mip_lvl_res - 2.0f) * mip_lvl_res_inv;
71 float offset = 0.5f * mip_lvl_res_inv;
72 return uv * scale + offset;
73}
74
76 float lod_min,
77 float lod_max,
78 SphereProbeUvArea uv_area,
79 float2 &altas_uv_min,
80 float2 &altas_uv_max)
81{
82 float2 octahedral_uv = octahedral_uv_from_direction(L);
83 /* We use a custom per mip level scaling and bias. This avoid some projection artifact and
84 * padding border waste. But we need to do the mipmap interpolation ourself. */
85 float2 local_uv_min = miplvl_scale_bias(lod_min, uv_area, octahedral_uv);
86 float2 local_uv_max = miplvl_scale_bias(lod_max, uv_area, octahedral_uv);
87 /* Remap into atlas location. */
88 altas_uv_min = local_uv_min * uv_area.scale + uv_area.offset;
89 altas_uv_max = local_uv_max * uv_area.scale + uv_area.offset;
90}
91
92/* Single mip variant. */
93float2 direction_to_uv(float3 L, float lod, SphereProbeUvArea uv_area)
94{
95 float2 altas_uv_min, altas_uv_max_unused;
96 direction_to_uv(L, lod, 0.0f, uv_area, altas_uv_min, altas_uv_max_unused);
97 return altas_uv_min;
98}
99
100float roughness_to_mix_fac(float roughness)
101{
102 constexpr float scale = 1.0f /
103 (SPHERE_PROBE_MIX_END_ROUGHNESS - SPHERE_PROBE_MIX_START_ROUGHNESS);
104 constexpr float bias = scale * SPHERE_PROBE_MIX_START_ROUGHNESS;
105 return square(saturate(roughness * scale - bias));
106}
107
108/* Input roughness is linear roughness (UI roughness). */
109float roughness_to_lod(float roughness)
110{
111 /* From "Moving Frostbite to Physically Based Rendering 3.0" eq 53. */
112 float ratio = saturate(roughness / SPHERE_PROBE_MIP_MAX_ROUGHNESS);
113 float ratio_sqrt = sqrt_fast(ratio);
114 /* Mix with linear to avoid mip 1 being too sharp. */
115 float mip_ratio = mix(ratio, ratio_sqrt, 0.4f);
116 return mip_ratio * float(SPHERE_PROBE_MIPMAP_LEVELS - 1);
117}
118
119/* Return linear roughness (UI roughness). */
120float lod_to_roughness(float lod)
121{
122 /* Inverse of sphere_probe_roughness_to_lod. */
123 float mip_ratio = lod / float(SPHERE_PROBE_MIPMAP_LEVELS - 1);
124 float a = mip_ratio;
125 constexpr float b = 0.6f; /* Factor of ratio. */
126 constexpr float c = 0.4f; /* Factor of ratio_sqrt. */
127 float b2 = square(b);
128 float c2 = square(c);
129 float c4 = square(c2);
130 /* In wolfram alpha we trust. */
131 float ratio = (-sqrt(4.0f * a * b * c2 + c4) + 2.0f * a * b + c2) / (2.0f * b2);
132 return ratio * SPHERE_PROBE_MIP_MAX_ROUGHNESS;
133}
134
135/* Return the best parallax corrected ray direction from the probe center. */
136float3 parallax_eval(SphereProbeData probe, float3 P, float3 L)
137{
138 bool is_world = (probe.influence_scale == 0.0f);
139 if (is_world) {
140 return L;
141 }
142 /* Correct reflection ray using parallax volume intersection. */
143 float3 lP = float4(P, 1.0f) * probe.world_to_probe_transposed;
144 float3 lL = (L * to_float3x3(probe.world_to_probe_transposed)) / probe.parallax_distance;
145
146 float dist = (probe.parallax_shape == SHAPE_ELLIPSOID) ?
149
150 /* Use distance in world space directly to recover intersection.
151 * This works because we assume no shear in the probe matrix. */
152 float3 L_new = P + L * dist - probe.location;
153
154 /* TODO(fclem): Roughness adjustment. */
155
156 return L_new;
157}
158
160{
161 /* To avoid color shift and negative values, we reduce saturation and directionality. */
162 ReflectionProbeLowFreqLight result;
163 result.ambient = sh.L0.M0.r + sh.L0.M0.g + sh.L0.M0.b;
164 /* Bias to avoid division by zero. */
165 result.ambient += 1e-6f;
166
167 float3x4 L1_per_band;
168 L1_per_band[0] = sh.L1.Mn1;
169 L1_per_band[1] = sh.L1.M0;
170 L1_per_band[2] = sh.L1.Mp1;
171
172 float4x3 L1_per_comp = transpose(L1_per_band);
173 result.direction = L1_per_comp[0] + L1_per_comp[1] + L1_per_comp[2];
174
175 return result;
176}
177
179 ReflectionProbeLowFreqLight numerator,
180 ReflectionProbeLowFreqLight denominator)
181{
182 /* TODO(fclem): Adjusting directionality is tricky.
183 * Needs to be revisited later on. For now only use the ambient term. */
184 return saturate(numerator.ambient / denominator.ambient);
185}
186
187} // namespace eevee::lightprobe::sphere
188
189namespace eevee {
190
195
197 [[uniform(SPHERE_PROBE_BUF_SLOT)]] SphereProbeData (&lightprobe_sphere_buf)[SPHERE_PROBE_MAX];
199
200 int select_probe(float3 P, float random_probe) const
201 {
202 for (int index = 0; index < SPHERE_PROBE_MAX; index++) {
203 SphereProbeData probe_data = lightprobe_sphere_buf[index];
204 /* SphereProbeData doesn't contain any gap, exit at first item that is invalid. */
205 if (probe_data.atlas_coord.layer == -1.0f) {
206 /* We hit the end of the array. Return last valid index. */
207 return index - 1;
208 }
209 /* NOTE: The vector-matrix multiplication swapped on purpose to cancel the matrix transpose.
210 */
211 float3 lP = float4(P, 1.0f) * probe_data.world_to_probe_transposed;
212 float gradient = (probe_data.influence_shape == SHAPE_ELLIPSOID) ?
213 length(lP) :
214 max(max(abs(lP.x), abs(lP.y)), abs(lP.z));
215 float score = saturate(probe_data.influence_bias - gradient * probe_data.influence_scale);
216 if (score > random_probe) {
217 return index;
218 }
219 }
220 /* This should never happen (world probe is always last). */
221 return SPHERE_PROBE_MAX - 1;
222 }
223
224 float4 sample_probe(float3 L, float lod, SphereProbeUvArea uv_area) const
225 {
226 float lod_min = floor(lod);
227 float lod_max = ceil(lod);
228 float mix_fac = lod - lod_min;
229
230 float2 altas_uv_min, altas_uv_max;
231 lightprobe::sphere::direction_to_uv(L, lod_min, lod_max, uv_area, altas_uv_min, altas_uv_max);
232
233 float4 color_min = textureLod(
234 lightprobe_spheres_tx, float3(altas_uv_min, uv_area.layer), lod_min);
235 float4 color_max = textureLod(
236 lightprobe_spheres_tx, float3(altas_uv_max, uv_area.layer), lod_max);
237 return mix(color_min, color_max, mix_fac);
238 }
239
245 float3 P,
246 float3 L,
247 float lod) const
248 {
249 SphereProbeData probe = lightprobe_sphere_buf[samp.spherical_id];
250 ReflectionProbeLowFreqLight shading_sh = lightprobe::sphere::extract_low_freq_lighting(
251 samp.volume_irradiance);
252 float normalization_fac = lightprobe::sphere::normalization_factor(
253 L, shading_sh, probe.low_freq_light);
255 return normalization_fac * this->sample_probe(L, lod, probe.atlas_coord).rgb;
256 }
257};
258
259} // namespace eevee
btScalar max
float line_unit_sphere_intersect_dist(float3 line_origin, float3 line_direction)
float line_unit_box_intersect_dist(float3 line_origin, float3 line_direction)
#define SPHERE_PROBE_TEX_SLOT
#define SPHERE_PROBE_BUF_SLOT
float2 octahedral_uv_from_direction(float3 co)
float3 octahedral_uv_to_direction(float2 co)
#define uniform(slot)
#define index(i)
#define sampler(slot)
VecBase< float, D > intBitsToFloat(VecOp< int, D >) RET
#define abs
#define floor
#define ceil
#define sqrt
MatBase< R, C > transpose(MatBase< C, R >) RET
float length(VecOp< float, D >) RET
MatBase< 4, 3 > float4x3
TEX_TEMPLATE DataVec textureLod(T, FltCoord, float) RET
SamplerBase< float, 2, false, true > sampler2DArray
float square(float v)
float sqrt_fast(float v)
float3x3 scale(float3x3 mat, float2 scale)
#define L
MatBase< float, 3, 4 > float3x4
ReflectionProbeLowFreqLight extract_low_freq_lighting(SphericalHarmonicL1< float4 > sh)
float normalization_factor(float3, ReflectionProbeLowFreqLight numerator, ReflectionProbeLowFreqLight denominator)
float3 texel_to_direction(float2 local_texel, SphereProbePixelArea texel_area, float2 &sampling_uv)
float2 miplvl_scale_bias(float mip_lvl, SphereProbeUvArea uv_area, float2 uv)
float roughness_to_mix_fac(float roughness)
float3 parallax_eval(SphereProbeData probe, float3 P, float3 L)
void direction_to_uv(float3 L, float lod_min, float lod_max, SphereProbeUvArea uv_area, float2 &altas_uv_min, float2 &altas_uv_max)
SphereProbePixelArea reinterpret_as_write_coord(int4 packed_coord)
SphereProbeUvArea reinterpret_as_atlas_coord(int4 packed_coord)
#define mix
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define saturate(a)
Definition smaa.cc:319
spherical_harmonics::BandL1< T > L1
spherical_harmonics::BandL0< T > L0
SphericalHarmonicL1< float4 > volume_irradiance
float3 spherical_sample_normalized_with_parallax(LightProbeSample samp, float3 P, float3 L, float lod) const
int select_probe(float3 P, float random_probe) const
float4 sample_probe(float3 L, float lod, SphereProbeUvArea uv_area) const
SphereProbeData(& lightprobe_sphere_buf)[SPHERE_PROBE_MAX]
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136