sandbox
Loading...
Searching...
No Matches
graphics_pipeline.hpp
1#ifndef LIBSBX_GRAPHICS_PIPELINE_GRAPHICS_PIPELINE_HPP_
2#define LIBSBX_GRAPHICS_PIPELINE_GRAPHICS_PIPELINE_HPP_
3
4#include <optional>
5#include <filesystem>
6#include <cinttypes>
7#include <vector>
8#include <unordered_map>
9#include <map>
10#include <memory>
11
12#include <vulkan/vulkan.hpp>
13
14#include <fmt/format.h>
15
16#include <libsbx/utility/enum.hpp>
17
18#include <libsbx/graphics/buffers/buffer.hpp>
19#include <libsbx/graphics/buffers/uniform_handler.hpp>
20
21#include <libsbx/graphics/pipeline/shader.hpp>
22#include <libsbx/graphics/pipeline/pipeline.hpp>
23#include <libsbx/graphics/pipeline/vertex_input_description.hpp>
24
25#include <libsbx/graphics/descriptor/descriptor.hpp>
26#include <libsbx/graphics/descriptor/descriptor_set.hpp>
27
28#include <libsbx/graphics/images/image2d.hpp>
29
30namespace sbx::graphics {
31
32enum class polygon_mode : std::uint8_t {
33 fill = VK_POLYGON_MODE_FILL,
34 line = VK_POLYGON_MODE_LINE,
35 point = VK_POLYGON_MODE_POINT
36}; // enum class polygon_mode
37
38enum class cull_mode : std::uint8_t {
39 none = VK_CULL_MODE_NONE,
40 front = VK_CULL_MODE_FRONT_BIT,
41 back = VK_CULL_MODE_BACK_BIT,
42 front_and_back = VK_CULL_MODE_FRONT_AND_BACK
43}; // enum class cull_mode
44
45enum class front_face : std::uint8_t {
46 counter_clockwise = VK_FRONT_FACE_COUNTER_CLOCKWISE,
47 clockwise = VK_FRONT_FACE_CLOCKWISE
48}; // enum class front_face
49
50struct depth_bias {
51 std::float_t constant_factor{0.0f};
52 std::float_t clamp{0.0f};
53 std::float_t slope_factor{0.0f};
54}; // struct depth_bias
55
57 graphics::polygon_mode polygon_mode{polygon_mode::fill};
58 std::float_t line_width{1.0f};
59 graphics::cull_mode cull_mode{cull_mode::back};
60 graphics::front_face front_face{front_face::counter_clockwise};
61 std::optional<graphics::depth_bias> depth_bias{};
62}; // struct rasterization_state
63
64enum class primitive_topology : std::uint8_t {
65 point_list = VK_PRIMITIVE_TOPOLOGY_POINT_LIST,
66 line_list = VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
67 line_strip = VK_PRIMITIVE_TOPOLOGY_LINE_STRIP,
68 triangle_list = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
69 triangle_strip = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
70 triangle_fan = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN,
71 line_list_with_adjacency = VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY,
72 line_strip_with_adjacency = VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY,
73 triangle_list_with_adjacency = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY,
74 triangle_strip_with_adjacency = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY,
75 patch_list = VK_PRIMITIVE_TOPOLOGY_PATCH_LIST,
76}; // enum class primitive_topology
77
79 bool uses_depth{true};
80 bool uses_transparency{false};
82 graphics::primitive_topology primitive_topology{graphics::primitive_topology::triangle_list};
83 std::vector<graphics::shader::define> defines{};
84}; // struct pipeline_definition
85
86template<vertex Vertex>
88
89public:
90
91 using vertex_type = Vertex;
92
93 graphics_pipeline(const std::filesystem::path& path, const pipeline::stage& stage, const pipeline_definition& default_definition = pipeline_definition{});
94
95 ~graphics_pipeline() override;
96
97 auto handle() const noexcept -> const VkPipeline& override;
98
99 auto has_variable_descriptors() const noexcept -> bool override {
100 return _has_variable_descriptors;
101 }
102
103 auto descriptor_counts() const noexcept -> const std::unordered_map<std::uint32_t, std::uint32_t>& override {
104 return _descriptor_count_at_binding;
105 }
106
107 auto descriptor_set_layout() const noexcept -> const VkDescriptorSetLayout& override;
108
109 auto descriptor_pool() const noexcept -> const VkDescriptorPool& override;
110
111 auto layout() const noexcept -> const VkPipelineLayout& override;
112
113 auto bind_point() const noexcept -> VkPipelineBindPoint override;
114
115 auto stage() const noexcept -> const pipeline::stage& {
116 return _stage;
117 }
118
119 auto descriptor_block(const std::string& name) const -> const shader::uniform_block& override {
120 if (auto it = _uniform_blocks.find(name); it != _uniform_blocks.end()) {
121 return it->second;
122 }
123
124 throw std::runtime_error(fmt::format("Failed to find descriptor block '{}' in graphics pipeline '{}'", name, _name));
125 }
126
127 auto find_descriptor_binding(const std::string& name) const -> std::optional<std::uint32_t> override {
128 if (auto it = _descriptor_bindings.find(name); it != _descriptor_bindings.end()) {
129 return it->second;
130 }
131
132 return std::nullopt;
133 }
134
135 auto find_descriptor_type_at_binding(std::uint32_t binding) const -> std::optional<VkDescriptorType> override {
136 if (auto it = _descriptor_type_at_binding.find(binding); it != _descriptor_type_at_binding.end()) {
137 return it->second;
138 }
139
140 return std::nullopt;
141 }
142
143private:
144
145 auto _update_definition(const std::filesystem::path& path, const pipeline_definition default_definition) -> pipeline_definition;
146
147 auto _get_stage_from_name(const std::string& name) const noexcept -> VkShaderStageFlagBits;
148
149 std::unordered_map<VkShaderStageFlagBits, std::unique_ptr<shader>> _shaders{};
150
151 std::unordered_map<std::string, shader::uniform> _uniforms{};
152 std::unordered_map<std::string, shader::uniform_block> _uniform_blocks{};
153
154 std::unordered_map<std::uint32_t, VkDescriptorType> _descriptor_type_at_binding{};
155 std::unordered_map<std::uint32_t, std::uint32_t> _descriptor_count_at_binding{};
156
157 std::unordered_map<std::string, std::uint32_t> _descriptor_bindings{};
158 std::unordered_map<std::string, std::uint32_t> _descriptor_sizes{};
159
160 std::string _name{};
161 VkPipelineLayout _layout{};
162 VkPipeline _handle{};
163 VkPipelineBindPoint _bind_point{};
164 bool _has_variable_descriptors;
165
166 pipeline::stage _stage{};
167
168 VkDescriptorPool _descriptor_pool{};
169 VkDescriptorSetLayout _descriptor_set_layout{};
170
171}; // class graphics_pipeline
172
173} // namespace sbx::graphics
174
175template<>
176struct sbx::utility::enum_mapping<sbx::graphics::polygon_mode> {
177
179
180 static constexpr auto values = std::array<entry_type, 3u>{
181 entry_type{sbx::graphics::polygon_mode::fill, "fill"},
182 entry_type{sbx::graphics::polygon_mode::line, "line"},
183 entry_type{sbx::graphics::polygon_mode::point, "point"}
184 };
185
186}; // struct sbx::utility::enum_mapping
187
188#include <libsbx/graphics/pipeline/graphics_pipeline.ipp>
189
190#endif // LIBSBX_GRAPHICS_PIPELINE_GRAPHICS_PIPELINE_HPP_
Definition: graphics_pipeline.hpp:87
Definition: pipeline.hpp:18
Definition: shader.hpp:116
Definition: graphics_pipeline.hpp:50
Definition: pipeline.hpp:22
Definition: graphics_pipeline.hpp:78
Definition: graphics_pipeline.hpp:56
Definition: enum.hpp:27
Definition: enum.hpp:34