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