sandbox
Loading...
Searching...
No Matches
descriptor_handler.hpp
1#ifndef LIBSBX_GRAPHICS_DESCRIPTOR_DESCRIPTOR_HANDLER_HPP_
2#define LIBSBX_GRAPHICS_DESCRIPTOR_DESCRIPTOR_HANDLER_HPP_
3
4#include <memory>
5#include <map>
6#include <vector>
7#include <optional>
8
9#include <libsbx/memory/observer_ptr.hpp>
10
11#include <libsbx/graphics/render_pass/swapchain.hpp>
12
13#include <libsbx/graphics/buffers/uniform_handler.hpp>
14#include <libsbx/graphics/buffers/storage_handler.hpp>
15#include <libsbx/graphics/buffers/push_handler.hpp>
16
17#include <libsbx/graphics/pipeline/pipeline.hpp>
18#include <libsbx/graphics/pipeline/graphics_pipeline.hpp>
19
20#include <libsbx/graphics/descriptor/descriptor.hpp>
21#include <libsbx/graphics/descriptor/descriptor_set.hpp>
22
23namespace sbx::graphics {
24
26
27public:
28
29 inline static constexpr auto global_set_id = std::uint32_t{0u};
30 inline static constexpr auto per_draw_call_set_id = std::uint32_t{1u};
31
32 descriptor_handler(std::uint32_t set);
33
34 explicit descriptor_handler(const pipeline& pipeline, std::uint32_t set);
35
36 explicit descriptor_handler(const graphics_pipeline_handle& handle, std::uint32_t set);
37
39
40 template<typename Descriptor>
41 requires (std::is_base_of_v<descriptor, Descriptor>)
42 auto push(const std::string& name, const Descriptor& descriptor) -> void {
43 if (_pipeline) {
44 _push(name, descriptor);
45 }
46 }
47
48 auto push(const std::string& name, uniform_handler& uniform_handler) -> void;
49
50 auto push(const std::string& name, storage_handler& storage_handler) -> void;
51
52 // auto push(const std::string& name, push_handler& push_handler) -> void {
53 // if (_pipeline) {
54 // push_handler.update(_pipeline->descriptor_block(name, _set));
55 // }
56 // }
57
58 auto bind_descriptors(command_buffer& command_buffer) -> void;
59
60 auto descriptor_set() const noexcept -> VkDescriptorSet;
61
62 auto update(const pipeline& pipeline) -> bool;
63
64private:
65
66 struct descriptor_entry {
69 std::uint32_t binding;
70 }; // struct descriptor_entry
71
72 template<typename Descriptor>
73 requires (std::is_base_of_v<descriptor, Descriptor>)
74 auto _push(const std::string& name, const Descriptor& descriptor) -> void {
75 auto binding = _pipeline->find_descriptor_binding(name, _set);
76
77 if (!binding) {
78 throw std::runtime_error(fmt::format("Failed to find descriptor binding for descriptor '{}'", name));
79 }
80
81 auto descriptor_type = _pipeline->find_descriptor_type_at_binding(_set, *binding);
82
83 if (!descriptor_type) {
84
85 throw std::runtime_error(fmt::format("Failed to find descriptor type for descriptor '{}' set: {} binding {}", name, _set, *binding));
86 }
87
88 auto write_descriptor_set = descriptor.write_descriptor_set(*binding, *descriptor_type);
89
91 _descriptors.insert_or_assign(name, descriptor_entry{std::addressof(descriptor), std::move(write_descriptor_set), *binding});
92 _has_changed = true;
93 }
94
95 }
96
97 auto _recreate_descriptor_sets() -> void;
98
99 std::uint32_t _set;
100
102
103 std::array<std::unique_ptr<graphics::descriptor_set>, graphics::swapchain::max_frames_in_flight> _descriptor_sets{};
104
105 std::map<std::string, descriptor_entry> _descriptors{};
106 bool _has_changed{};
107
108}; // class descriptor_handler
109
110} // namespace sbx::graphics
111
112#endif // LIBSBX_GRAPHICS_DESCRIPTOR_DESCRIPTOR_HANDLER_HPP_
Definition: command_buffer.hpp:14
Definition: descriptor_handler.hpp:25
Definition: descriptor_set.hpp:12
Definition: descriptor.hpp:37
Definition: pipeline.hpp:20
Definition: resource_storage.hpp:17
Definition: storage_handler.hpp:16
Definition: uniform_handler.hpp:15
Definition: descriptor.hpp:12
A non-owning pointer that can be used to observe the value of a pointer.
Definition: observer_ptr.hpp:27