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
19#include <libsbx/graphics/descriptor/descriptor.hpp>
20#include <libsbx/graphics/descriptor/descriptor_set.hpp>
21
22namespace sbx::graphics {
23
25
26public:
27
28 inline static constexpr auto global_set_id = std::uint32_t{0u};
29 inline static constexpr auto per_draw_call_set_id = std::uint32_t{1u};
30
31 descriptor_handler(std::uint32_t set);
32
33 explicit descriptor_handler(const pipeline& pipeline, std::uint32_t set);
34
36
37 template<typename Descriptor>
38 requires (std::is_base_of_v<descriptor, Descriptor>)
39 auto push(const std::string& name, const Descriptor& descriptor) -> void {
40 if (_pipeline) {
41 _push(name, descriptor);
42 }
43 }
44
45 auto push(const std::string& name, uniform_handler& uniform_handler) -> void;
46
47 auto push(const std::string& name, storage_handler& storage_handler) -> void;
48
49 // auto push(const std::string& name, push_handler& push_handler) -> void {
50 // if (_pipeline) {
51 // push_handler.update(_pipeline->descriptor_block(name, _set));
52 // }
53 // }
54
55 auto bind_descriptors(command_buffer& command_buffer) -> void;
56
57 auto descriptor_set() const noexcept -> VkDescriptorSet;
58
59 auto update(const pipeline& pipeline) -> bool;
60
61private:
62
63 struct descriptor_entry {
66 std::uint32_t binding;
67 }; // struct descriptor_entry
68
69 template<typename Descriptor>
70 requires (std::is_base_of_v<descriptor, Descriptor>)
71 auto _push(const std::string& name, const Descriptor& descriptor) -> void {
72 auto binding = _pipeline->find_descriptor_binding(name, _set);
73
74 if (!binding) {
75 throw std::runtime_error(fmt::format("Failed to find descriptor binding for descriptor '{}'", name));
76 }
77
78 auto descriptor_type = _pipeline->find_descriptor_type_at_binding(_set, *binding);
79
80 if (!descriptor_type) {
81 throw std::runtime_error(fmt::format("Failed to find descriptor type for descriptor '{}'", name));
82 }
83
84 auto write_descriptor_set = descriptor.write_descriptor_set(*binding, *descriptor_type);
85
87 _descriptors.insert_or_assign(name, descriptor_entry{std::addressof(descriptor), std::move(write_descriptor_set), *binding});
88 _has_changed = true;
89 }
90
91 }
92
93 auto _recreate_descriptor_sets() -> void;
94
95 std::uint32_t _set;
96
98
99 std::array<std::unique_ptr<graphics::descriptor_set>, graphics::swapchain::max_frames_in_flight> _descriptor_sets{};
100
101 std::map<std::string, descriptor_entry> _descriptors{};
102 bool _has_changed{};
103
104}; // class descriptor_handler
105
106} // namespace sbx::graphics
107
108#endif // LIBSBX_GRAPHICS_DESCRIPTOR_DESCRIPTOR_HANDLER_HPP_
Definition: command_buffer.hpp:14
Definition: descriptor_handler.hpp:24
Definition: descriptor_set.hpp:12
Definition: descriptor.hpp:37
Definition: pipeline.hpp:18
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