Toast Engine
Loading...
Searching...
No Matches
hiz_pyramid.hpp
Go to the documentation of this file.
1
4
5#pragma once
6
7#include "gpu_sync.hpp"
8#include "shader_layout.hpp"
9#include "vulkan_pipeline.hpp"
10
11#include <glm/glm.hpp>
12#include <optional>
13#include <vector>
14
15namespace renderer {
16class VulkanCore;
17
20public:
22 void create(const VulkanCore& core, vk::ImageView depth, vk::Extent2D depth_extent);
23
25 void record(vk::CommandBuffer cmd, vk::Extent2D scene_extent, const glm::mat4& view_projection);
26
28 void beforeRead(sync::BarrierBatch& barriers);
29
31 [[nodiscard]]
32 auto isReady() const -> bool {
33 return m_view.has_value() && m_pipeline.isReady();
34 }
35
37 [[nodiscard]]
38 auto view() const -> vk::ImageView {
39 return m_view.has_value() ? **m_view : vk::ImageView {};
40 }
41
42 [[nodiscard]]
43 auto sampler() const -> vk::Sampler {
44 return *m_sampler;
45 }
46
48 [[nodiscard]]
49 auto builtExtent() const -> vk::Extent2D {
50 return m_built_extent;
51 }
52
53 [[nodiscard]]
54 auto builtLevels() const -> uint32_t {
55 return m_built_levels;
56 }
57
58 [[nodiscard]]
59 auto builtViewProjection() const -> const glm::mat4& {
60 return m_built_view_projection;
61 }
62
64 [[nodiscard]]
65 static auto levelCount(vk::Extent2D extent) -> uint32_t;
66
68 static constexpr uint32_t k_max_levels = 13;
69
70private:
71 void buildPipeline(const VulkanCore& core);
72
74 struct ReducePush {
75 glm::uvec2 source_size {0};
76 uint32_t level_count = 0;
77 uint32_t group_count = 0;
78 };
79
80 ShaderLayout m_layout;
81 VulkanPipeline m_pipeline;
82 vk::raii::Sampler m_sampler = nullptr;
83
84 std::optional<vma::raii::Image> m_image;
85 std::optional<vk::raii::ImageView> m_view;
86 std::vector<vk::raii::ImageView> m_level_views;
87 std::optional<vk::raii::DescriptorSet> m_set;
89 std::optional<vma::raii::Buffer> m_counter;
90 bool m_counter_cleared = false;
91 sync::ImageState m_state;
92 uint32_t m_levels = 0;
93
94 vk::Extent2D m_built_extent {0, 0};
95 uint32_t m_built_levels = 0;
96 glm::mat4 m_built_view_projection {1.0f};
97};
98
99}
Farthest depth pyramid where level k texel t covers pixels t << (k + 1) to ((t + 1) << (k + 1)) - 1.
Definition hiz_pyramid.hpp:19
auto builtExtent() const -> vk::Extent2D
What the last record() covered and saw with zero levels before the first one.
Definition hiz_pyramid.hpp:49
void beforeRead(sync::BarrierBatch &barriers)
Earlier writes land before a compute shader samples view().
Definition hiz_pyramid.cpp:165
static auto levelCount(vk::Extent2D extent) -> uint32_t
Levels until the scene extent halves down to one texel.
Definition hiz_pyramid.cpp:61
void record(vk::CommandBuffer cmd, vk::Extent2D scene_extent, const glm::mat4 &view_projection)
Depth in depth_sampled and only its top left scene_extent is reduced.
Definition hiz_pyramid.cpp:169
auto view() const -> vk::ImageView
Every level in eShaderReadOnlyOptimal once recorded.
Definition hiz_pyramid.hpp:38
void create(const VulkanCore &core, vk::ImageView depth, vk::Extent2D depth_extent)
After a wait for idle since an in flight frame may still read the previous pyramid.
Definition hiz_pyramid.cpp:66
static constexpr uint32_t k_max_levels
Mirrors hiz_reduce.slang kMaxLevels so level 0 reaches 4096 texels.
Definition hiz_pyramid.hpp:68
auto isReady() const -> bool
view() can be bound without it but only a ready pyramid is ever built
Definition hiz_pyramid.hpp:32
Definition vulkan_core.hpp:48
Skips a barrier only when neither side writes and the layout and next read stages are already covered...
Definition gpu_sync.hpp:105