2#ifndef LIBSBX_GRAPHICS_PIPELINE_GRAPHICS_PIPELINE_HPP_
3#define LIBSBX_GRAPHICS_PIPELINE_GRAPHICS_PIPELINE_HPP_
9#include <unordered_map>
13#include <vulkan/vulkan.hpp>
15#include <fmt/format.h>
17#include <libsbx/utility/enum.hpp>
19#include <libsbx/memory/tracking_allocator.hpp>
21#include <libsbx/containers/static_vector.hpp>
23#include <libsbx/graphics/buffers/buffer.hpp>
24#include <libsbx/graphics/buffers/uniform_handler.hpp>
26#include <libsbx/graphics/pipeline/shader.hpp>
27#include <libsbx/graphics/pipeline/pipeline.hpp>
28#include <libsbx/graphics/pipeline/compiler.hpp>
29#include <libsbx/graphics/pipeline/vertex_input_description.hpp>
31#include <libsbx/graphics/descriptor/descriptor.hpp>
32#include <libsbx/graphics/descriptor/descriptor_set.hpp>
34#include <libsbx/graphics/images/image2d.hpp>
36#include <libsbx/graphics/render_graph.hpp>
37#include <libsbx/graphics/resource_storage.hpp>
39namespace sbx::graphics {
41enum class polygon_mode : std::int32_t {
42 fill = VK_POLYGON_MODE_FILL,
43 line = VK_POLYGON_MODE_LINE,
44 point = VK_POLYGON_MODE_POINT
47enum class cull_mode : std::int32_t {
48 none = VK_CULL_MODE_NONE,
49 front = VK_CULL_MODE_FRONT_BIT,
50 back = VK_CULL_MODE_BACK_BIT,
51 front_and_back = VK_CULL_MODE_FRONT_AND_BACK
54enum class front_face : std::int32_t {
55 counter_clockwise = VK_FRONT_FACE_COUNTER_CLOCKWISE,
56 clockwise = VK_FRONT_FACE_CLOCKWISE
60 std::float_t constant_factor{0.0f};
61 std::float_t clamp{0.0f};
62 std::float_t slope_factor{0.0f};
66 graphics::polygon_mode polygon_mode{graphics::polygon_mode::fill};
67 std::float_t line_width{1.0f};
68 graphics::cull_mode cull_mode{graphics::cull_mode::back};
69 graphics::front_face front_face{graphics::front_face::counter_clockwise};
70 std::optional<graphics::depth_bias>
depth_bias{};
73enum class primitive_topology : std::int32_t {
74 point_list = VK_PRIMITIVE_TOPOLOGY_POINT_LIST,
75 line_list = VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
76 line_strip = VK_PRIMITIVE_TOPOLOGY_LINE_STRIP,
77 triangle_list = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
78 triangle_strip = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
79 triangle_fan = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN,
80 line_list_with_adjacency = VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY,
81 line_strip_with_adjacency = VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY,
82 triangle_list_with_adjacency = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY,
83 triangle_strip_with_adjacency = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY,
84 patch_list = VK_PRIMITIVE_TOPOLOGY_PATCH_LIST,
87enum class depth : std::int32_t {
93enum class compare_operation : std::int32_t {
94 never = VK_COMPARE_OP_NEVER,
95 less = VK_COMPARE_OP_LESS,
96 equal = VK_COMPARE_OP_EQUAL,
97 less_or_equal = VK_COMPARE_OP_LESS_OR_EQUAL,
98 greater = VK_COMPARE_OP_GREATER,
99 not_equal = VK_COMPARE_OP_NOT_EQUAL,
100 greater_or_equal = VK_COMPARE_OP_GREATER_OR_EQUAL,
101 always = VK_COMPARE_OP_ALWAYS
105 graphics::depth depth{graphics::depth::read_write};
106 graphics::compare_operation compare_operation{graphics::compare_operation::less_or_equal};
107 bool uses_transparency{
false};
109 graphics::primitive_topology primitive_topology{graphics::primitive_topology::triangle_list};
117 struct rendering_create_info {
118 VkPipelineRenderingCreateInfo info;
119 std::vector<VkFormat> color_formats;
120 VkFormat depth_format;
121 VkFormat stencil_format;
128 std::unordered_map<SlangStage, std::vector<std::uint32_t>> shader_codes{};
135 ~graphics_pipeline()
override;
137 auto handle() const noexcept -> VkPipeline override;
139 auto has_variable_descriptors() const noexcept ->
bool override {
140 return _has_variable_descriptors;
143 auto descriptor_counts(std::uint32_t set)
const noexcept -> std::vector<std::uint32_t>
override {
144 auto counts = std::vector<std::uint32_t>{};
146 for (
const auto& binding_data : _set_data[set].binding_data) {
147 counts.push_back(binding_data.descriptor_count);
153 auto descriptor_set_layout(std::uint32_t set)
const noexcept -> VkDescriptorSetLayout
override;
155 auto descriptor_pool() const noexcept -> VkDescriptorPool override;
157 auto layout() const noexcept -> VkPipelineLayout override;
159 auto bind_point() const noexcept -> VkPipelineBindPoint override;
161 auto descriptor_block(const std::
string& name, std::uint32_t set) const -> const shader::uniform_block&
override {
162 if (
auto it = _set_data[set].uniform_blocks.find(name); it != _set_data[set].uniform_blocks.end()) {
166 throw std::runtime_error(fmt::format(
"Failed to find descriptor block '{}' in graphics pipeline '{}'", name, _name));
169 auto push_constant() const noexcept -> const std::optional<shader::uniform_block>&
override {
170 return _push_constant;
173 auto find_descriptor_binding(
const std::string& name, std::uint32_t set)
const -> std::optional<std::uint32_t>
override {
174 if (
auto it = _set_data[set].descriptor_bindings.find(name); it != _set_data[set].descriptor_bindings.end()) {
181 auto find_descriptor_type_at_binding(std::uint32_t set, std::uint32_t binding)
const -> std::optional<VkDescriptorType>
override {
182 if (_set_data[set].binding_data.size() <= binding) {
186 return _set_data[set].binding_data[binding].descriptor_type;
189 auto rendering_info() ->
const rendering_create_info& {
190 return _rendering_info;
195 struct per_binding_data {
196 VkDescriptorType descriptor_type;
197 std::uint32_t descriptor_count;
200 struct per_set_data {
201 std::unordered_map<std::string, shader::uniform> uniforms;
202 std::unordered_map<std::string, shader::uniform_block> uniform_blocks;
203 std::unordered_map<std::string, std::uint32_t> descriptor_bindings;
204 std::unordered_map<std::string, std::uint32_t> descriptor_sizes;
205 std::vector<per_binding_data> binding_data;
206 VkDescriptorSetLayout layout;
209 auto _initialize(
const std::vector<attachment_description>& attachments,
const pipeline_definition& definition,
const VkSpecializationInfo* specialization_info) -> void;
211 auto _update_definition(
const std::filesystem::path& path,
const pipeline_definition default_definition) -> pipeline_definition;
213 auto _get_stage_from_name(
const std::string& name)
const noexcept -> VkShaderStageFlagBits;
215 std::unordered_map<VkShaderStageFlagBits, std::unique_ptr<shader>> _shaders{};
217 std::vector<per_set_data> _set_data;
218 std::optional<shader::uniform_block> _push_constant;
221 VkPipelineLayout _layout{};
222 VkPipeline _handle{};
223 VkPipelineBindPoint _bind_point{};
224 bool _has_variable_descriptors{};
226 rendering_create_info _rendering_info;
228 VkDescriptorPool _descriptor_pool{};
232using graphics_pipeline_handle = resource_handle<graphics_pipeline>;
241 static constexpr auto values = std::array<entry_type, 3u>{
242 entry_type{sbx::graphics::polygon_mode::fill,
"fill"},
243 entry_type{sbx::graphics::polygon_mode::line,
"line"},
244 entry_type{sbx::graphics::polygon_mode::point,
"point"}
254 static constexpr auto values = std::array<entry_type, 3u>{
255 entry_type{sbx::graphics::depth::disabled,
"disabled"},
256 entry_type{sbx::graphics::depth::read_write,
"read_write"},
257 entry_type{sbx::graphics::depth::read_only,
"read_only"}
267 static constexpr auto values = std::array<entry_type, 4u>{
268 entry_type{sbx::graphics::cull_mode::back,
"back"},
269 entry_type{sbx::graphics::cull_mode::front,
"front"},
270 entry_type{sbx::graphics::cull_mode::front_and_back,
"front_and_back"},
271 entry_type{sbx::graphics::cull_mode::none,
"none"}
281 static constexpr auto values = std::array<entry_type, 2u>{
282 entry_type{sbx::graphics::front_face::clockwise,
"clockwise"},
283 entry_type{sbx::graphics::front_face::counter_clockwise,
"counter_clockwise"}
Definition: graphics_pipeline.hpp:113
Definition: pipeline.hpp:21
Definition: graphics_pipeline.hpp:59
Definition: graphics_pipeline.hpp:126
Definition: graphics_pipeline.hpp:104
Definition: graphics_pipeline.hpp:65