20 return instance_.scene->eevee.gi_cubemap_resolution / 2;
25 if (!instance_.is_viewport() || instance_.is_image_render) {
30 update_probes_this_sample_ = update_probes_next_sample_;
32 do_display_draw_ =
false;
37 LightProbeModule &light_probes = instance_.light_probes;
50 pass.
bind_ssbo(
"out_sh", &tmp_spherical_harmonics_);
51 pass.
bind_ssbo(
"out_sun", &tmp_sunlight_);
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_));
57 pass.
dispatch(&dispatch_probe_pack_);
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_));
71 pass.
dispatch(&dispatch_probe_convolve_);
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_);
88 pass.
push_constant(
"probe_remap_dispatch_size", &dispatch_probe_pack_);
90 pass.
bind_ssbo(
"sunlight_buf", &instance_.world.sunlight);
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);
104 pass.
dispatch(&dispatch_probe_select_);
109bool SphereProbeModule::ensure_atlas()
115 if (probes_tx_.
ensure_2d_array(gpu::TextureFormat::SPHERE_PROBE_FORMAT,
116 int2(SPHERE_PROBE_ATLAS_RES),
120 SPHERE_PROBE_MIPMAP_LEVELS))
125 for (
auto i :
IndexRange(SPHERE_PROBE_MIPMAP_LEVELS)) {
138 const bool atlas_resized = ensure_atlas();
140 instance_.light_probes.world_sphere_.do_render =
true;
142 const bool world_updated = instance_.light_probes.world_sphere_.do_render;
144 update_probes_next_sample_ =
false;
145 for (
SphereProbe &probe : instance_.light_probes.sphere_map_.values()) {
146 if (atlas_resized || world_updated) {
148 probe.do_render =
true;
150 if (probe.do_render) {
154 update_probes_next_sample_ =
true;
158 if (instance_.is_viewport()) {
164 if (instance_.do_lightprobe_sphere_sync()) {
165 instance_.sampling.reset();
168 if (update_probes_next_sample_ && (instance_.do_lightprobe_sphere_sync() ==
false)) {
174void SphereProbeModule::ensure_cubemap_render_target(
int resolution)
179 cubemap_tx_.
ensure_cube(gpu::TextureFormat::SFLOAT_32_32_32_32, resolution, usage);
182void SphereProbeModule::release_render_target()
187SphereProbeModule::UpdateInfo SphereProbeModule::update_info_from_probe(SphereProbe &probe)
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;
196 probe.do_render =
false;
197 probe.use_for_render =
true;
203 return instance_.light_probes.world_sphere_;
206std::optional<SphereProbeModule::UpdateInfo> SphereProbeModule::world_update_info_pop()
210 return update_info_from_probe(world_probe);
215std::optional<SphereProbeModule::UpdateInfo> SphereProbeModule::probe_update_info_pop()
222 for (SphereProbe &probe : instance_.light_probes.sphere_map_.values()) {
223 if (!probe.do_render) {
226 return update_info_from_probe(probe);
233 bool convolve_octahedral,
234 bool extract_spherical_harmonics,
235 int extract_sun_index)
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(
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_);
248 if (convolve_octahedral) {
250 for (
auto i :
IndexRange(SPHERE_PROBE_MIPMAP_LEVELS - 1)) {
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(
259 instance_.manager->submit(convolve_ps_);
264 if (extract_spherical_harmonics) {
265 instance_.manager->submit(sum_sh_ps_);
268 extract_sun_index_ = extract_sun_index;
269 instance_.manager->submit(sum_sun_ps_);
271 if (extract_spherical_harmonics || extract_sun_) {
272 instance_.lookdev.store_world_probe_data(
273 probes_tx_, atlas_coord, spherical_harmonics_, instance_.world.sunlight);
283 for (
auto &probe : instance_.light_probes.sphere_map_.values()) {
285 if (lightprobe_sphere_count_ >= SPHERE_PROBE_MAX - 1) {
288 if (!probe.use_for_render) {
292 probe_active.
append(&probe);
321 for (
auto &probe : probe_active) {
322 data_buf_[probe_id++] = *probe;
325 data_buf_[probe_id++] = instance_.light_probes.world_sphere_;
327 if (probe_id < SPHERE_PROBE_MAX) {
328 data_buf_[probe_id].atlas_coord.layer = -1;
330 data_buf_.push_update();
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_);
337 sync_display(probe_active);
342 do_display_draw_ =
false;
347 int display_index = 0;
349 if (probe_active[i]->viewport_display) {
352 sph_data.
display_size = probe_active[i]->viewport_display_size;
356 if (display_index == 0) {
359 do_display_draw_ =
true;
360 display_data_buf_.resize(display_index);
361 display_data_buf_.push_update();
366 if (!do_display_draw_) {
370 viewport_display_ps_.init();
374 viewport_display_ps_.framebuffer_set(&view_fb);
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);
380 instance_.manager->submit(viewport_display_ps_, view);
constexpr IndexRange()=default
void append(const T &value)
IndexRange index_range() const
T & get_or_resize(int64_t index)
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)
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
void set_view(View &view)
const SphereProbe & world_sphere_probe() const
int probe_render_extent() const
void viewport_draw(View &view, gpu::FrameBuffer *view_fb)
detail::Pass< command::DrawCommandBuf > PassSimple
@ SPHERE_PROBE_IRRADIANCE
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)
BMesh const char void * data
@ GPU_BARRIER_TEXTURE_FETCH
@ GPU_BARRIER_SHADER_STORAGE
@ DRW_STATE_CLIP_CONTROL_UNIT_RANGE
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()
struct SphereProbeUvArea atlas_coord