Blender V5.3
eevee_lightprobe_sphere.cc
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
10#include "eevee_instance.hh"
11
12namespace blender::eevee {
13
14/* -------------------------------------------------------------------- */
17
19{
20 return instance_.scene->eevee.gi_cubemap_resolution / 2;
21}
22
24{
25 if (!instance_.is_viewport() || instance_.is_image_render) {
26 /* TODO(jbakker): should we check on the subtype as well? Now it also populates even when
27 * there are other light probes in the scene. */
28 update_probes_next_sample_ = DEG_id_type_any_exists(instance_.depsgraph, ID_LP);
29 }
30 update_probes_this_sample_ = update_probes_next_sample_;
31
32 do_display_draw_ = false;
33}
34
36{
37 LightProbeModule &light_probes = instance_.light_probes;
38 SphereProbeData &world_data = *static_cast<SphereProbeData *>(&light_probes.world_sphere_);
39 {
40 gpu::Shader *shader = instance_.shaders.static_shader_get(SPHERE_PROBE_REMAP);
41
42 PassSimple &pass = remap_ps_;
43 pass.init();
44 pass.specialize_constant(shader, "extract_sh", &extract_sh_);
45 pass.specialize_constant(shader, "extract_sun", &extract_sun_);
46 pass.shader_set(shader);
47 pass.bind_texture("cubemap_tx", &cubemap_tx_);
48 pass.bind_texture("atlas_tx", &probes_tx_);
49 pass.bind_image("atlas_img", &probes_tx_);
50 pass.bind_ssbo("out_sh", &tmp_spherical_harmonics_);
51 pass.bind_ssbo("out_sun", &tmp_sunlight_);
52 pass.push_constant("do_remap_mip0", &do_remap_mip0_);
53 pass.push_constant("probe_coord_packed", reinterpret_cast<int4 *>(&probe_sampling_coord_));
54 pass.push_constant("write_coord_packed", reinterpret_cast<int4 *>(&probe_write_coord_));
55 pass.push_constant("world_coord_packed", reinterpret_cast<int4 *>(&world_data.atlas_coord));
56 pass.bind_resources(instance_.uniform_data);
57 pass.dispatch(&dispatch_probe_pack_);
58 }
59 {
60 PassSimple &pass = convolve_ps_;
61 pass.init();
62 pass.shader_set(instance_.shaders.static_shader_get(SPHERE_PROBE_CONVOLVE));
63 pass.bind_texture("cubemap_tx", &cubemap_tx_);
64 pass.bind_texture("in_atlas_mip_tx", &convolve_input_);
65 pass.bind_image("out_atlas_mip_img", &convolve_output_);
66 pass.push_constant("probe_coord_packed", reinterpret_cast<int4 *>(&probe_sampling_coord_));
67 pass.push_constant("write_coord_packed", reinterpret_cast<int4 *>(&probe_write_coord_));
68 pass.push_constant("read_coord_packed", reinterpret_cast<int4 *>(&probe_read_coord_));
69 pass.push_constant("read_lod", &convolve_lod_);
71 pass.dispatch(&dispatch_probe_convolve_);
72 }
73 {
74 PassSimple &pass = sum_sh_ps_;
75 pass.init();
76 pass.shader_set(instance_.shaders.static_shader_get(SPHERE_PROBE_IRRADIANCE));
77 pass.push_constant("probe_remap_dispatch_size", &dispatch_probe_pack_);
78 pass.bind_ssbo("in_sh", &tmp_spherical_harmonics_);
79 pass.bind_ssbo("out_sh", &spherical_harmonics_);
81 pass.dispatch(1);
82 }
83 {
84 PassSimple &pass = sum_sun_ps_;
85 pass.init();
86 pass.shader_set(instance_.shaders.static_shader_get(SPHERE_PROBE_SUNLIGHT));
87 pass.push_constant("sun_id", &extract_sun_index_);
88 pass.push_constant("probe_remap_dispatch_size", &dispatch_probe_pack_);
89 pass.bind_ssbo("in_sun", &tmp_sunlight_);
90 pass.bind_ssbo("sunlight_buf", &instance_.world.sunlight);
92 pass.dispatch(1);
94 }
95 {
96 PassSimple &pass = select_ps_;
97 pass.init();
98 pass.shader_set(instance_.shaders.static_shader_get(SPHERE_PROBE_SELECT));
99 pass.push_constant("lightprobe_sphere_count", &lightprobe_sphere_count_);
100 pass.bind_ssbo("lightprobe_sphere_buf", &data_buf_);
101 instance_.volume_probes.bind_resources(pass);
102 instance_.sampling.bind_resources(pass);
103 pass.bind_resources(instance_.uniform_data);
104 pass.dispatch(&dispatch_probe_select_);
106 }
107}
108
109bool SphereProbeModule::ensure_atlas()
110{
111 /* Make sure the atlas is always initialized even if there is nothing to render to it to fulfill
112 * the resource bindings. */
114
115 if (probes_tx_.ensure_2d_array(gpu::TextureFormat::SPHERE_PROBE_FORMAT,
116 int2(SPHERE_PROBE_ATLAS_RES),
117 instance_.light_probes.sphere_layer_count(),
118 usage,
119 nullptr,
120 SPHERE_PROBE_MIPMAP_LEVELS))
121 {
122 probes_tx_.ensure_mip_views();
123 /* TODO(fclem): Clearing means that we need to render all probes again.
124 * If existing data exists, copy it using `CopyImageSubData`. */
125 for (auto i : IndexRange(SPHERE_PROBE_MIPMAP_LEVELS)) {
126 /* Avoid undefined pixel data. Clear all mips. */
127 float4 data(0.0f);
129 }
130 GPU_texture_mipmap_mode(probes_tx_, true, true);
131 return true;
132 }
133 return false;
134}
135
137{
138 const bool atlas_resized = ensure_atlas();
139 if (atlas_resized) {
140 instance_.light_probes.world_sphere_.do_render = true;
141 }
142 const bool world_updated = instance_.light_probes.world_sphere_.do_render;
143 /* Detect if we need to render probe objects. */
144 update_probes_next_sample_ = false;
145 for (SphereProbe &probe : instance_.light_probes.sphere_map_.values()) {
146 if (atlas_resized || world_updated) {
147 /* Last minute tagging. */
148 probe.do_render = true;
149 }
150 if (probe.do_render) {
151 /* Tag the next redraw to warm up the probe pipeline.
152 * Keep doing this until there is no update.
153 * This avoids stuttering when moving a light-probe. */
154 update_probes_next_sample_ = true;
155 }
156 }
157
158 if (instance_.is_viewport()) {
159 /* When reflection probes are synced the sampling must be reset.
160 *
161 * This fixes issues when using a single non-projected sample. Without resetting the
162 * previous rendered viewport will be drawn and reflection probes will not be updated.
163 * #Instance::render_sample */
164 if (instance_.do_lightprobe_sphere_sync()) {
165 instance_.sampling.reset();
166 }
167 /* If we cannot render probes this redraw make sure we request another redraw. */
168 if (update_probes_next_sample_ && (instance_.do_lightprobe_sphere_sync() == false)) {
170 }
171 }
172}
173
174void SphereProbeModule::ensure_cubemap_render_target(int resolution)
175{
177 /* 32 bit float because sun light extraction reads this before anything clamps it, and a world
178 * can be far brighter than the 65504 a 16 bit float holds. The atlas stays 16 bit. */
179 cubemap_tx_.ensure_cube(gpu::TextureFormat::SFLOAT_32_32_32_32, resolution, usage);
180}
181
182void SphereProbeModule::release_render_target()
183{
184 cubemap_tx_.free();
185}
186
187SphereProbeModule::UpdateInfo SphereProbeModule::update_info_from_probe(SphereProbe &probe)
188{
189 SphereProbeModule::UpdateInfo info = {};
190 info.atlas_coord = probe.atlas_coord;
191 info.cube_target_extent = probe.atlas_coord.area_extent() / 2;
192 info.clipping_distances = probe.clipping_distances;
193 info.probe_pos = probe.location;
194 info.do_render = probe.do_render;
195
196 probe.do_render = false;
197 probe.use_for_render = true;
198 return info;
199}
200
202{
203 return instance_.light_probes.world_sphere_;
204}
205
206std::optional<SphereProbeModule::UpdateInfo> SphereProbeModule::world_update_info_pop()
207{
208 SphereProbe &world_probe = instance_.light_probes.world_sphere_;
209 if (world_probe.do_render) {
210 return update_info_from_probe(world_probe);
211 }
212 return std::nullopt;
213}
214
215std::optional<SphereProbeModule::UpdateInfo> SphereProbeModule::probe_update_info_pop()
216{
217 if (!instance_.do_lightprobe_sphere_sync()) {
218 /* Do not update probes during this sample as we did not sync the draw::Passes. */
219 return std::nullopt;
220 }
221
222 for (SphereProbe &probe : instance_.light_probes.sphere_map_.values()) {
223 if (!probe.do_render) {
224 continue;
225 }
226 return update_info_from_probe(probe);
227 }
228
229 return std::nullopt;
230}
231
232void SphereProbeModule::remap_to_octahedral_projection(const SphereProbeAtlasCoord &atlas_coord,
233 bool convolve_octahedral,
234 bool extract_spherical_harmonics,
235 int extract_sun_index)
236{
237 /* Update shader parameters that change per dispatch. */
238 probe_sampling_coord_ = atlas_coord.as_sampling_coord();
239 probe_write_coord_ = atlas_coord.as_write_coord(0);
240 int resolution = probe_write_coord_.extent;
241 dispatch_probe_pack_ = int3(
242 int2(math::divide_ceil(int2(resolution), int2(SPHERE_PROBE_REMAP_GROUP_SIZE))), 1);
243 extract_sh_ = extract_spherical_harmonics;
244 extract_sun_ = extract_sun_index != -1;
245 do_remap_mip0_ = convolve_octahedral;
246 instance_.manager->submit(remap_ps_);
247
248 if (convolve_octahedral) {
249 /* Populate the mip levels */
250 for (auto i : IndexRange(SPHERE_PROBE_MIPMAP_LEVELS - 1)) {
251 convolve_lod_ = i;
252 convolve_input_ = probes_tx_.mip_view(i);
253 convolve_output_ = probes_tx_.mip_view(i + 1);
254 probe_read_coord_ = atlas_coord.as_write_coord(i);
255 probe_write_coord_ = atlas_coord.as_write_coord(i + 1);
256 int out_mip_res = probe_write_coord_.extent;
257 dispatch_probe_convolve_ = int3(
258 math::divide_ceil(int2(out_mip_res), int2(SPHERE_PROBE_GROUP_SIZE)), 1);
259 instance_.manager->submit(convolve_ps_);
260 }
261 }
262
263 /* This is only true for the world probe. */
264 if (extract_spherical_harmonics) {
265 instance_.manager->submit(sum_sh_ps_);
266 }
267 if (extract_sun_) {
268 extract_sun_index_ = extract_sun_index;
269 instance_.manager->submit(sum_sun_ps_);
270 }
271 if (extract_spherical_harmonics || extract_sun_) {
272 instance_.lookdev.store_world_probe_data(
273 probes_tx_, atlas_coord, spherical_harmonics_, instance_.world.sunlight);
274 }
275
276 /* Sync with atlas usage for shading. */
278}
279
281{
282 Vector<SphereProbe *> probe_active;
283 for (auto &probe : instance_.light_probes.sphere_map_.values()) {
284 /* Last slot is reserved for the world probe. */
285 if (lightprobe_sphere_count_ >= SPHERE_PROBE_MAX - 1) {
286 break;
287 }
288 if (!probe.use_for_render) {
289 continue;
290 }
291 /* TODO(fclem): Culling. */
292 probe_active.append(&probe);
293 }
294
295 /* Stable sorting of probes. */
296 std::ranges::sort(probe_active, [](const SphereProbe *a, const SphereProbe *b) {
297 if (a->volume != b->volume) {
298 /* Smallest first. */
299 return a->volume < b->volume;
300 }
301 /* Volumes are identical. Any arbitrary criteria can be used to sort them.
302 * Use position to avoid unstable result caused by depsgraph non deterministic eval
303 * order. This could also become a priority parameter. */
304 float3 _a = a->location;
305 float3 _b = b->location;
306 if (_a.x != _b.x) {
307 return _a.x < _b.x;
308 }
309 if (_a.y != _b.y) {
310 return _a.y < _b.y;
311 }
312 if (_a.z != _b.z) {
313 return _a.z < _b.z;
314 }
315 /* Fallback to memory address, since there's no good alternative. */
316 return a < b;
317 });
318
319 /* Push all sorted data to the UBO. */
320 int probe_id = 0;
321 for (auto &probe : probe_active) {
322 data_buf_[probe_id++] = *probe;
323 }
324 /* Add world probe at the end. */
325 data_buf_[probe_id++] = instance_.light_probes.world_sphere_;
326 /* Tag the end of the array. */
327 if (probe_id < SPHERE_PROBE_MAX) {
328 data_buf_[probe_id].atlas_coord.layer = -1;
329 }
330 data_buf_.push_update();
331
332 lightprobe_sphere_count_ = probe_id;
333 dispatch_probe_select_.x = divide_ceil_u(lightprobe_sphere_count_,
334 SPHERE_PROBE_SELECT_GROUP_SIZE);
335 instance_.manager->submit(select_ps_);
336
337 sync_display(probe_active);
338}
339
340void SphereProbeModule::sync_display(Vector<SphereProbe *> &probe_active)
341{
342 do_display_draw_ = false;
343 if (!instance_.draw_overlays) {
344 return;
345 }
346
347 int display_index = 0;
348 for (int i : probe_active.index_range()) {
349 if (probe_active[i]->viewport_display) {
350 SphereProbeDisplayData &sph_data = display_data_buf_.get_or_resize(display_index++);
351 sph_data.probe_index = i;
352 sph_data.display_size = probe_active[i]->viewport_display_size;
353 }
354 }
355
356 if (display_index == 0) {
357 return;
358 }
359 do_display_draw_ = true;
360 display_data_buf_.resize(display_index);
361 display_data_buf_.push_update();
362}
363
365{
366 if (!do_display_draw_) {
367 return;
368 }
369
370 viewport_display_ps_.init();
371 viewport_display_ps_.state_set(DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH |
373 instance_.film.depth.test_state | DRW_STATE_CULL_BACK);
374 viewport_display_ps_.framebuffer_set(&view_fb);
375 viewport_display_ps_.shader_set(instance_.shaders.static_shader_get(DISPLAY_PROBE_SPHERE));
376 viewport_display_ps_.bind_resources(*this);
377 viewport_display_ps_.bind_ssbo("display_data_buf", display_data_buf_);
378 viewport_display_ps_.draw_procedural(GPU_PRIM_TRIS, 1, display_data_buf_.size() * 6);
379
380 instance_.manager->submit(viewport_display_ps_, view);
381}
382
384
385} // namespace blender::eevee
constexpr IndexRange()=default
void append(const T &value)
IndexRange index_range() const
bool ensure_mip_views(bool cube_as_array=false)
bool ensure_2d_array(gpu::TextureFormat format, int2 extent, int layers, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, const float *data=nullptr, int mip_len=1)
gpu::Texture * mip_view(int miplvl)
bool ensure_cube(gpu::TextureFormat format, int extent, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, float *data=nullptr, int mip_len=1)
void bind_resources(U &resources)
Definition draw_pass.hh:459
void shader_set(gpu::Shader *shader)
void bind_texture(const char *name, gpu::Texture *texture, GPUSamplerState state=sampler_auto)
void specialize_constant(gpu::Shader *shader, const char *name, const float &data)
void bind_image(const char *name, gpu::Texture *image)
void dispatch(int group_len)
void barrier(GPUBarrier type)
void push_constant(const char *name, const float &data)
void bind_ssbo(const char *name, gpu::StorageBuf *buffer)
bool do_lightprobe_sphere_sync() const
LightProbeModule light_probes
const SphereProbe & world_sphere_probe() const
void viewport_draw(View &view, gpu::FrameBuffer *view_fb)
detail::Pass< command::DrawCommandBuf > PassSimple
VecBase< T, Size > divide_ceil(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
VecBase< int32_t, 4 > int4
VecBase< float, 4 > float4
void GPU_texture_mipmap_mode(gpu::Texture *texture, bool use_mipmap, bool use_filter)
bool DEG_id_type_any_exists(const Depsgraph *depsgraph, short id_type)
VecBase< int32_t, 2 > int2
VecBase< int32_t, 3 > int3
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_SHADER_WRITE
@ GPU_TEXTURE_USAGE_ATTACHMENT
void GPU_memory_barrier(GPUBarrier barrier)
Definition gpu_state.cc:305
BMesh const char void * data
@ GPU_BARRIER_TEXTURE_FETCH
Definition GPU_state.hh:40
@ GPU_BARRIER_SHADER_STORAGE
Definition GPU_state.hh:51
@ GPU_BARRIER_UNIFORM
Definition GPU_state.hh:57
@ DRW_STATE_CULL_BACK
Definition draw_state.hh:45
@ DRW_STATE_CLIP_CONTROL_UNIT_RANGE
Definition draw_state.hh:71
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:31
void GPU_texture_clear(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
MINLINE uint divide_ceil_u(uint a, uint b)
VecBase< float, 3 > float3
void DRW_viewport_request_redraw()