2#ifndef LIBSBX_GRAPHICS_PIPELINE_SHADER_HPP_
3#define LIBSBX_GRAPHICS_PIPELINE_SHADER_HPP_
7#include <unordered_map>
10#include <spirv_cross/spirv_cross.hpp>
12#include <vulkan/vulkan.hpp>
14#include <libsbx/utility/noncopyable.hpp>
16#include <libsbx/containers/static_vector.hpp>
18namespace sbx::graphics {
24 enum class data_type : std::uint8_t {
64 explicit uniform(std::uint32_t set, std::uint32_t binding, std::uint32_t offset, std::uint32_t size, data_type type,
bool is_readonly,
bool is_writeonly, VkShaderStageFlags stage_flags)
70 _is_readonly{is_readonly},
71 _is_writeonly{is_writeonly},
72 _stage_flags{stage_flags} { }
74 auto set()
const noexcept -> std::uint32_t {
78 auto binding()
const noexcept -> std::uint32_t {
82 auto offset()
const noexcept -> std::uint32_t {
86 auto size()
const noexcept -> std::uint32_t {
90 auto type()
const noexcept -> data_type {
94 auto is_readonly()
const noexcept ->
bool {
98 auto is_writeonly()
const noexcept ->
bool {
102 auto stage_flags()
const noexcept -> VkShaderStageFlags {
106 auto add_stage_flag(VkShaderStageFlags stage)
noexcept ->
void {
107 _stage_flags |= stage;
110 auto operator==(
const uniform& other)
const noexcept ->
bool {
111 return _set == other._set && _binding == other._binding && _offset == other._offset && _size == other._size && _type == other._type && _is_readonly == other._is_readonly && _is_writeonly == other._is_writeonly;
116 std::uint32_t _set{};
117 std::uint32_t _binding{};
118 std::uint32_t _offset{};
119 std::uint32_t _size{};
122 bool _is_writeonly{};
123 VkShaderStageFlags _stage_flags{};
131 enum class type : std::uint8_t {
137 explicit uniform_block(std::uint32_t set, std::uint32_t binding, std::uint32_t size, VkShaderStageFlags stage_flags, type type, std::map<std::string, uniform> uniforms = {})
141 _stage_flags{stage_flags},
143 _uniforms{std::move(uniforms)} { }
145 auto set()
const noexcept -> std::uint32_t {
149 auto binding()
const noexcept -> std::uint32_t {
153 auto size()
const noexcept -> std::uint32_t {
157 auto stage_flags()
const noexcept -> VkShaderStageFlags {
161 auto add_stage_flag(VkShaderStageFlags stage)
noexcept ->
void {
162 _stage_flags |= stage;
165 auto buffer_type()
const noexcept -> type {
169 auto uniforms()
const noexcept ->
const std::map<std::string, uniform>& {
173 auto find_uniform(
const std::string& name)
const noexcept -> std::optional<uniform> {
174 if (
auto entry = _uniforms.find(name); entry != _uniforms.end()) {
175 return entry->second;
181 auto operator==(
const uniform_block& other)
const noexcept ->
bool {
182 return _set == other._set && _binding == other._binding && _size == other._size && _type == other._type && _uniforms == other._uniforms;
187 std::uint32_t _set{};
188 std::uint32_t _binding{};
189 std::uint32_t _size{};
190 VkShaderStageFlags _stage_flags{};
192 std::map<std::string, uniform> _uniforms{};
200 explicit attribute(std::uint32_t binding, std::uint32_t size, VkShaderStageFlags stage_flags, data_type type)
203 _stage_flags{stage_flags},
206 auto binding()
const noexcept -> std::uint32_t {
210 auto size()
const noexcept -> std::uint32_t {
214 auto stage_flags()
const noexcept -> VkShaderStageFlags {
218 auto type()
const noexcept -> data_type {
222 auto operator==(
const attribute& other)
const noexcept ->
bool {
223 return _binding == other._binding && _size == other._size && _stage_flags == other._stage_flags && _type == other._type;
228 std::uint32_t _binding{};
229 std::uint32_t _size{};
230 VkShaderStageFlags _stage_flags{};
240 using handle_type = VkShaderModule;
242 shader(
const std::filesystem::path& path, VkShaderStageFlagBits stage);
244 shader(
const std::vector<std::uint32_t>& code, VkShaderStageFlagBits stage);
248 auto handle() const noexcept -> handle_type;
250 operator handle_type() const noexcept;
252 auto stage() const noexcept -> VkShaderStageFlagBits;
254 auto set_uniforms() const noexcept -> const std::vector<std::unordered_map<std::
string,
uniform>>& {
255 return _set_uniforms;
258 auto set_uniform_blocks() const noexcept -> const std::vector<std::unordered_map<std::
string, uniform_block>>& {
259 return _set_uniform_blocks;
262 auto uniforms(std::uint32_t set)
const noexcept ->
const std::unordered_map<std::string, uniform>& {
263 return _set_uniforms[set];
266 auto uniform_blocks(std::uint32_t set)
const noexcept ->
const std::unordered_map<std::string, uniform_block>& {
267 return _set_uniform_blocks[set];
272 auto _create_reflection(
const spirv_cross::Compiler& compiler) -> void;
274 auto _get_data_type(
const spirv_cross::SPIRType& type) -> data_type;
276 auto _data_type_to_string(data_type type) -> std::string;
278 std::vector<std::unordered_map<std::string, uniform>> _set_uniforms{};
279 std::vector<std::unordered_map<std::string, uniform_block>> _set_uniform_blocks{};
281 VkShaderStageFlagBits _stage{};
282 handle_type _handle{};
Definition: sampler_state.hpp:11
Definition: separate_image2d_array.hpp:16
Definition: shader.hpp:196
Definition: shader.hpp:20
Definition: shader.hpp:235
Definition: noncopyable.hpp:7