1#ifndef LIBSBX_GRAPHICS_PIPELINE_SHADER_HPP_
2#define LIBSBX_GRAPHICS_PIPELINE_SHADER_HPP_
6#include <unordered_map>
9#include <spirv_cross/spirv_cross.hpp>
11#include <vulkan/vulkan.hpp>
13#include <libsbx/utility/noncopyable.hpp>
15namespace sbx::graphics {
21 enum class data_type : std::uint8_t {
59 explicit uniform(std::uint32_t binding, std::uint32_t offset, std::uint32_t size, data_type type,
bool is_readonly,
bool is_writeonly, VkShaderStageFlags stage_flags)
64 _is_readonly{is_readonly},
65 _is_writeonly{is_writeonly},
66 _stage_flags{stage_flags} { }
68 auto binding()
const noexcept -> std::uint32_t {
72 auto offset()
const noexcept -> std::uint32_t {
76 auto size()
const noexcept -> std::uint32_t {
80 auto type()
const noexcept -> data_type {
84 auto is_readonly()
const noexcept ->
bool {
88 auto is_writeonly()
const noexcept ->
bool {
92 auto stage_flags()
const noexcept -> VkShaderStageFlags {
96 auto add_stage_flag(VkShaderStageFlags stage)
noexcept ->
void {
97 _stage_flags |= stage;
100 auto operator==(
const uniform& other)
const noexcept ->
bool {
101 return _binding == other._binding && _offset == other._offset && _size == other._size && _type == other._type && _is_readonly == other._is_readonly && _is_writeonly == other._is_writeonly && _stage_flags == other._stage_flags;
106 std::uint32_t _binding{};
107 std::uint32_t _offset{};
108 std::uint32_t _size{};
111 bool _is_writeonly{};
112 VkShaderStageFlags _stage_flags{};
120 enum class type : std::uint8_t {
126 explicit uniform_block(std::uint32_t binding, std::uint32_t size, VkShaderStageFlags stage_flags, type type, std::map<std::string, uniform> uniforms = {})
129 _stage_flags{stage_flags},
131 _uniforms{std::move(uniforms)} { }
133 auto binding()
const noexcept -> std::uint32_t {
137 auto size()
const noexcept -> std::uint32_t {
141 auto stage_flags()
const noexcept -> VkShaderStageFlags {
145 auto add_stage_flag(VkShaderStageFlags stage)
noexcept ->
void {
146 _stage_flags |= stage;
149 auto buffer_type()
const noexcept -> type {
153 auto uniforms()
const noexcept ->
const std::map<std::string, uniform>& {
157 auto find_uniform(
const std::string& name)
const noexcept -> std::optional<uniform> {
158 if (
auto entry = _uniforms.find(name); entry != _uniforms.end()) {
159 return entry->second;
165 auto operator==(
const uniform_block& other)
const noexcept ->
bool {
166 return _binding == other._binding && _size == other._size && _stage_flags == other._stage_flags && _type == other._type && _uniforms == other._uniforms;
171 std::uint32_t _binding{};
172 std::uint32_t _size{};
173 VkShaderStageFlags _stage_flags{};
175 std::map<std::string, uniform> _uniforms{};
183 explicit attribute(std::uint32_t binding, std::uint32_t size, VkShaderStageFlags stage_flags, data_type type)
186 _stage_flags{stage_flags},
189 auto binding()
const noexcept -> std::uint32_t {
193 auto size()
const noexcept -> std::uint32_t {
197 auto stage_flags()
const noexcept -> VkShaderStageFlags {
201 auto type()
const noexcept -> data_type {
205 auto operator==(
const attribute& other)
const noexcept ->
bool {
206 return _binding == other._binding && _size == other._size && _stage_flags == other._stage_flags && _type == other._type;
211 std::uint32_t _binding{};
212 std::uint32_t _size{};
213 VkShaderStageFlags _stage_flags{};
223 shader(
const std::filesystem::path& path, VkShaderStageFlagBits stage,
const std::vector<define>& defines = {});
227 auto handle() const noexcept -> const VkShaderModule&;
229 operator const VkShaderModule&() const noexcept;
231 auto stage() const noexcept -> VkShaderStageFlagBits;
233 auto uniforms() const noexcept -> const std::map<std::
string, uniform>& {
237 auto uniform_blocks() const noexcept -> const std::map<std::
string, uniform_block>& {
238 return _uniform_blocks;
243 auto _create_reflection(
const spirv_cross::Compiler& compiler) -> void;
245 auto _get_data_type(
const spirv_cross::SPIRType& type) -> data_type;
247 auto _data_type_to_string(data_type type) -> std::string;
249 std::map<std::string, uniform> _uniforms{};
250 std::map<std::string, uniform_block> _uniform_blocks{};
252 VkShaderStageFlagBits _stage{};
253 VkShaderModule _handle{};
Definition: separate_image2d_array.hpp:15
Definition: separate_sampler.hpp:12
Definition: shader.hpp:179
Definition: shader.hpp:17
Definition: shader.hpp:218
Definition: noncopyable.hpp:6