Blender V5.3
eevee_lightprobe_volume_load.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
11
13
14struct AtlasStore {
16
17 void store(float4 sh_coefficient, int2 atlas_coord, int layer, uint3 local_id)
18 {
20 int3(atlas_coord, layer * IRRADIANCE_GRID_BRICK_SIZE) + int3(local_id),
21 sh_coefficient);
22 }
23};
24
25struct LoadGrid {
33
42
44
46 {
47 input_coord = clamp(input_coord, int3(0), textureSize(irradiance_a_tx, 0) - 1);
48
50 sh.L0.M0 = texelFetch(irradiance_a_tx, input_coord, 0);
51 sh.L1.Mn1 = texelFetch(irradiance_b_tx, input_coord, 0);
52 sh.L1.M0 = texelFetch(irradiance_c_tx, input_coord, 0);
53 sh.L1.Mp1 = texelFetch(irradiance_d_tx, input_coord, 0);
54
55 /* Load visibility separately as it might not be in the same texture. */
56 sh.L0.M0.a = texelFetch(visibility_a_tx, input_coord, 0).a;
57 sh.L1.Mn1.a = texelFetch(visibility_b_tx, input_coord, 0).a;
58 sh.L1.M0.a = texelFetch(visibility_c_tx, input_coord, 0).a;
59 sh.L1.Mp1.a = texelFetch(visibility_d_tx, input_coord, 0).a;
60 return sh;
61 }
62};
63
73void load_grid([[resource_table]] const LoadGrid &srt,
75 [[resource_table]] AtlasStore &atlas,
76 [[work_group_id]] const uint3 group_id,
77 [[local_invocation_id]] const uint3 local_id)
78{
79 int brick_index = lightprobe::volume::grid_brick_index_get(lvd.grids_infos_buf[srt.grid_index],
80 int3(group_id));
81
82 int3 grid_size = textureSize(srt.irradiance_a_tx, 0);
83 /* Brick coordinate in the source grid. */
84 int3 brick_coord = int3(group_id);
85 /* Add padding border to allow bilinear filtering. */
86 int3 texel_coord = brick_coord * (IRRADIANCE_GRID_BRICK_SIZE - 1) + int3(local_id);
87 /* Add padding to the grid to allow interpolation to outside grid. */
88 texel_coord -= 1;
89
90 int3 input_coord = clamp(texel_coord, int3(0), grid_size - 1);
91
92 bool is_padding_voxel = !all(equal(texel_coord, input_coord));
93
94 /* Brick coordinate in the destination atlas. */
95 IrradianceBrick brick = irradiance_brick_unpack(lvd.bricks_infos_buf[brick_index]);
96 int2 output_coord = int2(brick.atlas_coord);
97
99
100 float validity = texelFetch(srt.validity_tx, input_coord, 0).r;
101 if (validity > srt.dilation_threshold) {
102 /* Grid sample is valid. Single load. */
103 sh_local = srt.irradiance_load(input_coord);
104 }
105 else {
106 /* Grid sample is invalid. Dilate adjacent samples inside the search region. */
107 /* NOTE: Still load the center sample and give it low weight in case there is not valid sample
108 * in the neighborhood. */
109 float weight_accum = 1e-8f;
110 sh_local = spherical_harmonics::mul(srt.irradiance_load(input_coord), weight_accum);
111 int radius = int(srt.dilation_radius);
112 for (int x = -radius; x <= radius; x++) {
113 for (int y = -radius; y <= radius; y++) {
114 for (int z = -radius; z <= radius; z++) {
115 if (x == 0 && y == 0 && z == 0) {
116 continue;
117 }
118 int3 offset = int3(x, y, z);
119 int3 neighbor_coord = input_coord + offset;
120 float neighbor_validity = texelFetch(srt.validity_tx, neighbor_coord, 0).r;
121 /* Skip invalid neighbor samples. */
122 if (neighbor_validity < srt.dilation_threshold) {
123 continue;
124 }
125 float dist_sqr = length_squared(float3(offset));
126 if (dist_sqr > square(srt.dilation_radius)) {
127 continue;
128 }
129 float weight = 1.0f / dist_sqr;
130 sh_local = spherical_harmonics::madd(
131 srt.irradiance_load(neighbor_coord), weight, sh_local);
132 weight_accum += weight;
133 }
134 }
135 }
136 float inv_weight_accum = safe_rcp(weight_accum);
137 sh_local = spherical_harmonics::mul(sh_local, inv_weight_accum);
138 }
139
140 /* Rotate Spherical Harmonic into world space. */
141 float3x3 grid_to_world_rot = normalize(
142 to_float3x3(lvd.grids_infos_buf[srt.grid_index].world_to_grid_transposed));
143 sh_local = spherical_harmonics::rotate(grid_to_world_rot, sh_local);
144
145 SphericalHarmonicL1<float4> sh_visibility;
146 sh_visibility.L0.M0 = sh_local.L0.M0.aaaa;
147 sh_visibility.L1.Mn1 = sh_local.L1.Mn1.aaaa;
148 sh_visibility.L1.M0 = sh_local.L1.M0.aaaa;
149 sh_visibility.L1.Mp1 = sh_local.L1.Mp1.aaaa;
150
152 srt.grid_local_to_world, grid_size, input_coord);
153
154 SphericalHarmonicL1<float4> sh_distant = lvd.sample_probe_no_dithered_no_biases(
155 P, srt.grid_start_index);
156
157 if (is_padding_voxel) {
158 /* Padding voxels just contain the distant lighting. */
159 sh_local = sh_distant;
160 }
161 else {
162 /* Mask distant lighting by local visibility. */
163 sh_distant = spherical_harmonics::triple_product(sh_visibility, sh_distant);
164 /* Apply intensity scaling. */
165 sh_local = spherical_harmonics::mul(sh_local, srt.grid_intensity_factor);
166 /* Add local lighting to distant lighting. */
167 sh_local = spherical_harmonics::add(sh_local, sh_distant);
168 }
169
170 sh_local = spherical_harmonics::dering(sh_local);
171
172 atlas.store(sh_local.L0.M0, output_coord, 0, local_id);
173 atlas.store(sh_local.L1.Mn1, output_coord, 1, local_id);
174 atlas.store(sh_local.L1.M0, output_coord, 2, local_id);
175 atlas.store(sh_local.L1.Mp1, output_coord, 3, local_id);
176
177 if ((local_id.z % 4u) == 0u) {
178 /* Encode 4 cells into one volume sample. */
179 int4 cell_validity_bits = int4(0);
180 /* Encode validity of each samples in the grid cell. */
181 for (int cell = 0; cell < 4; cell++) [[unroll]] {
182 for (int i = 0; i < 8; i++) {
183 int3 sample_position = lightprobe::volume::grid_cell_corner(i);
184 int3 coord_texel = texel_coord + int3(0, 0, cell) + sample_position;
185 int3 coord_input = clamp(coord_texel, int3(0), grid_size - 1);
186 float validity = texelFetch(srt.validity_tx, coord_input, 0).r;
187 bool is_padding_voxel = !all(equal(coord_texel, coord_input));
188 if ((validity > srt.validity_threshold) || is_padding_voxel) {
189 cell_validity_bits[cell] |= (1 << i);
190 }
191 }
192 }
193 /* NOTE: We could use another sampler to reduce the memory overhead, but that would take
194 * another sampler slot for forward materials. */
195 atlas.store(float4(cell_validity_bits), output_coord, 4, local_id);
196 }
197}
198
199struct LoadWorld {
200 [[storage(1, read)]] const SphereProbeHarmonic &harmonic_buf;
201
203};
204
215 [[resource_table]] AtlasStore &atlas,
216 [[local_invocation_id]] const uint3 local_id)
217{
218 int brick_index = lvd.grids_infos_buf[srt.grid_index].brick_offset;
219
220 /* Brick coordinate in the destination atlas. */
221 IrradianceBrick brick = irradiance_brick_unpack(lvd.bricks_infos_buf[brick_index]);
222 int2 output_coord = int2(brick.atlas_coord);
223
225 sh.L0.M0 = srt.harmonic_buf.L0_M0;
226 sh.L1.Mn1 = srt.harmonic_buf.L1_Mn1;
227 sh.L1.M0 = srt.harmonic_buf.L1_M0;
228 sh.L1.Mp1 = srt.harmonic_buf.L1_Mp1;
229
231
232 atlas.store(sh.L0.M0, output_coord, 0, local_id);
233 atlas.store(sh.L1.Mn1, output_coord, 1, local_id);
234 atlas.store(sh.L1.M0, output_coord, 2, local_id);
235 atlas.store(sh.L1.Mp1, output_coord, 3, local_id);
236}
237
238} // namespace eevee::lightprobe::volume
239
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
#define IRRADIANCE_GRID_BRICK_SIZE
#define VOLUME_PROBE_FORMAT
PipelineCompute eevee_lightprobe_volume_world(eevee::lightprobe::volume::load_world)
PipelineCompute eevee_lightprobe_volume_load(eevee::lightprobe::volume::load_grid)
#define storage(slot, qualifiers)
#define image(slot, qualifiers, format)
#define work_group_id
#define push_constant
#define resource_table
#define sampler(slot)
#define local_invocation_id
#define unroll
#define compute
#define layer
#define local_size(...)
VecBase< float, D > normalize(VecOp< float, D >) RET
bool all(VecOp< bool, D >) RET
VecBase< bool, D > equal(VecOp< T, D >, VecOp< T, D >) RET
constexpr T clamp(T, U, U) RET
ImageBase< float, 3 > image3D
IMG_TEMPLATE void imageStore(T &, IntCoord, DataVec)
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
SamplerBase< float, 3 > sampler3D
TEX_TEMPLATE SizeVec textureSize(T, int) RET
float square(float v)
float safe_rcp(float a)
float length_squared(VecT a)
MatBase< float, 4, 4 > float4x4
int3 grid_cell_corner(int cell_corner_id)
void load_world(const LoadWorld &srt, const LightprobeVolumeRenderData &lvd, AtlasStore &atlas, const uint3 local_id)
float3 grid_sample_position(float4x4 grid_local_to_world_mat, int3 grid_res, int3 cell_coord)
int grid_brick_index_get(VolumeProbeData grid_data, int3 brick_coord)
void load_grid(const LoadGrid &srt, const LightprobeVolumeRenderData &lvd, AtlasStore &atlas, const uint3 group_id, const uint3 local_id)
SphericalHarmonicL1< float4 > add(SphericalHarmonicL1< float4 > a, SphericalHarmonicL1< float4 > b)
SphericalHarmonicL1< float4 > mul(SphericalHarmonicL1< float4 > a, float b)
SphericalHarmonicL1< float4 > rotate(float3x3 rotation, SphericalHarmonicL1< float4 > sh)
SphericalHarmonicL1< float4 > triple_product(SphericalHarmonicL1< float4 > a, SphericalHarmonicL1< float4 > b)
SphericalHarmonicL1< float4 > madd(SphericalHarmonicL1< float4 > a, float b, SphericalHarmonicL1< float4 > c)
SphericalHarmonicL1< float4 > dering(SphericalHarmonicL1< float4 > sh)
spherical_harmonics::BandL1< T > L1
spherical_harmonics::BandL0< T > L0
void store(float4 sh_coefficient, int2 atlas_coord, int layer, uint3 local_id)
SphericalHarmonicL1< float4 > irradiance_load(int3 input_coord) const