Blender V5.3
eevee_hizbuffer.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "eevee_instance.hh"
10
11#include "eevee_hizbuffer.hh"
12
13namespace blender::eevee {
14
15/* -------------------------------------------------------------------- */
19
21{
22 int2 render_extent = inst_.film.render_extent_get();
23 int2 probe_extent = int2(inst_.sphere_probes.probe_render_extent());
24 /* Padding to avoid complexity during down-sampling and screen tracing. */
25 int2 hiz_extent = math::ceil_to_multiple(math::max(render_extent, probe_extent),
26 int2(1u << (HIZ_MIP_COUNT - 1)));
27 /* Each thread update 4 LOD0 pixels. So the actual extent of each group is double of it size. */
28 int2 dispatch_size = math::divide_ceil(hiz_extent, int2(HIZ_GROUP_SIZE) * 2);
29
31 for ([[maybe_unused]] const int i : IndexRange(hiz_tx_.size())) {
32 hiz_tx_.current().ensure_2d(
33 gpu::TextureFormat::SFLOAT_32, hiz_extent, usage, nullptr, HIZ_MIP_COUNT);
34 hiz_tx_.current().ensure_mip_views();
35 GPU_texture_mipmap_mode(hiz_tx_.current(), true, false);
36 hiz_tx_.swap();
37 }
38
39 data_.uv_scale = float2(render_extent) / float2(hiz_extent);
40
41 /* TODO(@fclem): There might be occasions where we might not want to
42 * copy mip 0 for performance reasons if there is no need for it. */
43 bool update_mip_0 = true;
44
45 {
46 PassSimple &pass = hiz_update_ps_;
47 gpu::Shader *sh = inst_.shaders.static_shader_get(HIZ_UPDATE);
48 pass.init();
49 pass.specialize_constant(sh, "update_mip_0", update_mip_0);
50 pass.shader_set(sh);
51 pass.bind_ssbo("finished_tile_counter", atomic_tile_counter_);
52 /* TODO(fclem): Should be a parameter to avoid confusion. */
53 pass.bind_texture("depth_tx", &src_tx_);
54 pass.bind_image("out_mip_0", &hiz_mip_ref_[0]); // NOLINT(readability-container-data-pointer)
55 pass.bind_image("out_mip_1", &hiz_mip_ref_[1]);
56 pass.bind_image("out_mip_2", &hiz_mip_ref_[2]);
57 pass.bind_image("out_mip_3", &hiz_mip_ref_[3]);
58 pass.bind_image("out_mip_4", &hiz_mip_ref_[4]);
59 pass.bind_image("out_mip_5", &hiz_mip_ref_[5]);
60 pass.bind_image("out_mip_6", &hiz_mip_ref_[6]);
61 pass.dispatch(int3(dispatch_size, 1));
63 }
64 {
65 PassSimple &pass = hiz_update_layer_ps_;
66 gpu::Shader *sh = inst_.shaders.static_shader_get(HIZ_UPDATE_LAYER);
67 pass.init();
68 pass.specialize_constant(sh, "update_mip_0", update_mip_0);
69 pass.shader_set(sh);
70 pass.bind_ssbo("finished_tile_counter", atomic_tile_counter_);
71 /* TODO(fclem): Should be a parameter to avoid confusion. */
72 pass.bind_texture("depth_layered_tx", &src_tx_);
73 pass.bind_image("out_mip_0", &hiz_mip_ref_[0]); // NOLINT(readability-container-data-pointer)
74 pass.bind_image("out_mip_1", &hiz_mip_ref_[1]);
75 pass.bind_image("out_mip_2", &hiz_mip_ref_[2]);
76 pass.bind_image("out_mip_3", &hiz_mip_ref_[3]);
77 pass.bind_image("out_mip_4", &hiz_mip_ref_[4]);
78 pass.bind_image("out_mip_5", &hiz_mip_ref_[5]);
79 pass.bind_image("out_mip_6", &hiz_mip_ref_[6]);
80 pass.push_constant("layer_id", &layer_id_);
81 pass.dispatch(int3(dispatch_size, 1));
83 }
84
85 if (inst_.debug_mode == eDebugMode::DEBUG_HIZ_VALIDATION) {
86 debug_draw_ps_.init();
87 debug_draw_ps_.state_set(DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_CUSTOM);
88 debug_draw_ps_.shader_set(inst_.shaders.static_shader_get(HIZ_DEBUG));
89 debug_draw_ps_.bind_resources(this->front);
90 debug_draw_ps_.draw_procedural(GPU_PRIM_TRIS, 1, 3);
91 }
92}
93
95{
96 if (!is_dirty_) {
97 return;
98 }
99
100 /* After this, the front buffer become valid. */
101 front.ref_tx_ = hiz_tx_.current();
102
103 src_tx_ = *src_tx_ptr_;
104 for (const int i : IndexRange(HIZ_MIP_COUNT)) {
105 hiz_mip_ref_[i] = hiz_tx_.current().mip_view(i);
106 }
107
108 if (layer_id_ == -1) {
109 inst_.manager->submit(hiz_update_ps_);
110 }
111 else {
112 inst_.manager->submit(hiz_update_layer_ps_);
113 }
114
115 is_dirty_ = false;
116}
117
119{
120 if (inst_.debug_mode == eDebugMode::DEBUG_HIZ_VALIDATION) {
121 inst_.info_append(
122 "Debug Mode: HiZ Validation\n"
123 " - Red: pixel in front of HiZ tile value.\n"
124 " - Blue: No error.");
125 inst_.hiz_buffer.update();
126 GPU_framebuffer_bind(view_fb);
127 inst_.manager->submit(debug_draw_ps_, view);
128 }
129}
130
132
133} // namespace blender::eevee
constexpr IndexRange()=default
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)
void debug_draw(View &view, gpu::FrameBuffer *view_fb)
struct blender::eevee::HiZBuffer::@342214352322061324133130050175232001060230260232 front
detail::Pass< command::DrawCommandBuf > PassSimple
VecBase< T, Size > divide_ceil(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
VecBase< T, Size > ceil_to_multiple(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
T max(const T &a, const T &b)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
void GPU_texture_mipmap_mode(gpu::Texture *texture, bool use_mipmap, bool use_filter)
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_SHADER_WRITE
@ GPU_BARRIER_TEXTURE_FETCH
Definition GPU_state.hh:40
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_BLEND_CUSTOM
Definition draw_state.hh:65