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/reflection/description.hpp>
21#include <libsbx/memory/tracking_allocator.hpp>
23#include <libsbx/containers/static_vector.hpp>
25#include <libsbx/graphics/buffers/buffer.hpp>
26#include <libsbx/graphics/buffers/uniform_handler.hpp>
28#include <libsbx/graphics/pipeline/shader.hpp>
29#include <libsbx/graphics/pipeline/pipeline.hpp>
30#include <libsbx/graphics/pipeline/compiler.hpp>
31#include <libsbx/graphics/pipeline/vertex_input_description.hpp>
33#include <libsbx/graphics/descriptor/descriptor.hpp>
34#include <libsbx/graphics/descriptor/descriptor_set.hpp>
36#include <libsbx/graphics/images/image2d.hpp>
38#include <libsbx/graphics/render_graph.hpp>
39#include <libsbx/graphics/resource_storage.hpp>
41namespace sbx::graphics {
43enum class polygon_mode : std::int32_t {
44 fill = VK_POLYGON_MODE_FILL,
45 line = VK_POLYGON_MODE_LINE,
46 point = VK_POLYGON_MODE_POINT
49enum class cull_mode : std::int32_t {
50 none = VK_CULL_MODE_NONE,
51 front = VK_CULL_MODE_FRONT_BIT,
52 back = VK_CULL_MODE_BACK_BIT,
53 front_and_back = VK_CULL_MODE_FRONT_AND_BACK
56enum class front_face : std::int32_t {
57 counter_clockwise = VK_FRONT_FACE_COUNTER_CLOCKWISE,
58 clockwise = VK_FRONT_FACE_CLOCKWISE
62 std::float_t constant_factor{0.0f};
63 std::float_t clamp{0.0f};
64 std::float_t slope_factor{0.0f};
68 graphics::polygon_mode polygon_mode{graphics::polygon_mode::fill};
69 std::float_t line_width{1.0f};
70 graphics::cull_mode cull_mode{graphics::cull_mode::back};
71 graphics::front_face front_face{graphics::front_face::counter_clockwise};
72 std::optional<graphics::depth_bias>
depth_bias{};
75enum class primitive_topology : std::int32_t {
76 point_list = VK_PRIMITIVE_TOPOLOGY_POINT_LIST,
77 line_list = VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
78 line_strip = VK_PRIMITIVE_TOPOLOGY_LINE_STRIP,
79 triangle_list = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
80 triangle_strip = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
81 triangle_fan = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN,
82 line_list_with_adjacency = VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY,
83 line_strip_with_adjacency = VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY,
84 triangle_list_with_adjacency = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY,
85 triangle_strip_with_adjacency = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY,
86 patch_list = VK_PRIMITIVE_TOPOLOGY_PATCH_LIST,
89enum class depth : std::int32_t {
95enum class compare_operation : std::int32_t {
96 never = VK_COMPARE_OP_NEVER,
97 less = VK_COMPARE_OP_LESS,
98 equal = VK_COMPARE_OP_EQUAL,
99 less_or_equal = VK_COMPARE_OP_LESS_OR_EQUAL,
100 greater = VK_COMPARE_OP_GREATER,
101 not_equal = VK_COMPARE_OP_NOT_EQUAL,
102 greater_or_equal = VK_COMPARE_OP_GREATER_OR_EQUAL,
103 always = VK_COMPARE_OP_ALWAYS
107 graphics::depth depth{graphics::depth::read_write};
108 graphics::compare_operation compare_operation{graphics::compare_operation::less_or_equal};
109 bool uses_transparency{
false};
111 graphics::primitive_topology primitive_topology{graphics::primitive_topology::triangle_list};
119 struct rendering_create_info {
120 VkPipelineRenderingCreateInfo info;
121 std::vector<VkFormat> color_formats;
122 VkFormat depth_format;
123 VkFormat stencil_format;
130 std::unordered_map<SlangStage, std::vector<std::uint32_t>> shader_codes{};
137 ~graphics_pipeline()
override;
139 auto handle() const noexcept -> VkPipeline override;
141 auto has_variable_descriptors() const noexcept ->
bool override {
142 return _has_variable_descriptors;
145 auto descriptor_counts(std::uint32_t set)
const noexcept -> std::vector<std::uint32_t>
override {
146 auto counts = std::vector<std::uint32_t>{};
148 for (
const auto& binding_data : _set_data[set].binding_data) {
149 counts.push_back(binding_data.descriptor_count);
155 auto descriptor_set_layout(std::uint32_t set)
const noexcept -> VkDescriptorSetLayout
override;
157 auto descriptor_pool() const noexcept -> VkDescriptorPool override;
159 auto layout() const noexcept -> VkPipelineLayout override;
161 auto bind_point() const noexcept -> VkPipelineBindPoint override;
163 auto descriptor_block(const std::
string& name, std::uint32_t set) const -> const shader::uniform_block&
override {
164 if (
auto it = _set_data[set].uniform_blocks.find(name); it != _set_data[set].uniform_blocks.end()) {
168 throw std::runtime_error(fmt::format(
"Failed to find descriptor block '{}' in graphics pipeline '{}'", name, _name));
171 auto push_constant() const noexcept -> const std::optional<shader::uniform_block>&
override {
172 return _push_constant;
175 auto find_descriptor_binding(
const std::string& name, std::uint32_t set)
const -> std::optional<std::uint32_t>
override {
176 if (
auto it = _set_data[set].descriptor_bindings.find(name); it != _set_data[set].descriptor_bindings.end()) {
183 auto find_descriptor_type_at_binding(std::uint32_t set, std::uint32_t binding)
const -> std::optional<VkDescriptorType>
override {
184 if (_set_data[set].binding_data.size() <= binding) {
188 return _set_data[set].binding_data[binding].descriptor_type;
191 auto rendering_info() ->
const rendering_create_info& {
192 return _rendering_info;
197 struct per_binding_data {
198 VkDescriptorType descriptor_type;
199 std::uint32_t descriptor_count;
202 struct per_set_data {
203 std::unordered_map<std::string, shader::uniform> uniforms;
204 std::unordered_map<std::string, shader::uniform_block> uniform_blocks;
205 std::unordered_map<std::string, std::uint32_t> descriptor_bindings;
206 std::unordered_map<std::string, std::uint32_t> descriptor_sizes;
207 std::vector<per_binding_data> binding_data;
208 VkDescriptorSetLayout layout;
211 auto _initialize(
const std::vector<attachment_description>& attachments,
const pipeline_definition& definition,
const VkSpecializationInfo* specialization_info) -> void;
213 auto _update_definition(
const std::filesystem::path& path,
const pipeline_definition default_definition) -> pipeline_definition;
215 auto _get_stage_from_name(
const std::string& name)
const noexcept -> VkShaderStageFlagBits;
217 std::unordered_map<VkShaderStageFlagBits, std::unique_ptr<shader>> _shaders{};
219 std::vector<per_set_data> _set_data;
220 std::optional<shader::uniform_block> _push_constant;
223 VkPipelineLayout _layout{};
224 VkPipeline _handle{};
225 VkPipelineBindPoint _bind_point{};
226 bool _has_variable_descriptors{};
228 rendering_create_info _rendering_info;
230 VkDescriptorPool _descriptor_pool{};
234using graphics_pipeline_handle = resource_handle<graphics_pipeline>;
241 static constexpr auto name() -> std::string_view {
242 return "polygon_mode";
245 static constexpr auto enumerators() {
246 return std::make_tuple(
247 enumerator{
"fill", sbx::graphics::polygon_mode::fill},
248 enumerator{
"line", sbx::graphics::polygon_mode::line},
249 enumerator{
"point", sbx::graphics::polygon_mode::point}
258 static constexpr auto name() -> std::string_view {
262 static constexpr auto enumerators() {
263 return std::make_tuple(
264 enumerator{
"disabled", sbx::graphics::depth::disabled},
265 enumerator{
"read_write", sbx::graphics::depth::read_write},
266 enumerator{
"read_only", sbx::graphics::depth::read_only}
275 static constexpr auto name() -> std::string_view {
279 static constexpr auto enumerators() {
280 return std::make_tuple(
281 enumerator{
"back", sbx::graphics::cull_mode::back},
282 enumerator{
"front", sbx::graphics::cull_mode::front},
283 enumerator{
"front_and_back", sbx::graphics::cull_mode::front_and_back},
284 enumerator{
"none", sbx::graphics::cull_mode::none}
293 static constexpr auto name() -> std::string_view {
297 static constexpr auto enumerators() {
298 return std::make_tuple(
299 enumerator{
"clockwise", sbx::graphics::front_face::clockwise},
300 enumerator{
"counter_clockwise", sbx::graphics::front_face::counter_clockwise}
Definition: graphics_pipeline.hpp:115
Definition: pipeline.hpp:21
Definition: graphics_pipeline.hpp:61
Definition: graphics_pipeline.hpp:128
Definition: graphics_pipeline.hpp:106
Definition: graphics_pipeline.hpp:67
Definition: description.hpp:16
Definition: enumerator.hpp:10