wren
Vulkan-based game engine
Loading...
Searching...
No Matches
shader.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <shaderc/shaderc.h>
4#include <shaderc/status.h>
6// #include <wren/reflect/spirv_reflect.h>
7
8#include <filesystem>
9#include <map>
10#include <memory>
11#include <string>
12#include <vulkan/vulkan.hpp>
13#include <vulkan/vulkan_enums.hpp>
14#include <vulkan/vulkan_handles.hpp>
15#include <vulkan/vulkan_structs.hpp>
16#include <wren/math/vector.hpp>
17#include <wren/utils/result.hpp>
18
19DEFINE_ERROR_IMPL("shaderc", shaderc_compilation_status)
20BOOST_DESCRIBE_ENUM(shaderc_compilation_status,
21 shaderc_compilation_status_invalid_stage,
22 shaderc_compilation_status_compilation_error,
23 shaderc_compilation_status_internal_error,
24 shaderc_compilation_status_null_result_object,
25 shaderc_compilation_status_validation_error,
26 shaderc_compilation_status_transformation_error,
27 shaderc_compilation_status_configuration_error)
28
29namespace wren::reflect {
30using spirv_t = std::vector<uint32_t>;
31}
32
33namespace wren::vk {
34
35DESCRIBED_ENUM(ShaderType, Vertex, Fragment);
36
37struct ShaderModule {
38 reflect::spirv_t spirv;
39 ::vk::ShaderModule module;
40 std::shared_ptr<spv_reflect::ShaderModule> reflection;
41
42 ShaderModule() = default;
43 ShaderModule(reflect::spirv_t spirv, const ::vk::ShaderModule &module);
44
45 [[nodiscard]] auto get_vertex_input_bindings() const
46 -> std::vector<::vk::VertexInputBindingDescription>;
47
48 [[nodiscard]] auto get_vertex_input_attributes() const
49 -> std::vector<::vk::VertexInputAttributeDescription>;
50
51 [[nodiscard]] auto get_descriptor_set_layout_bindings() const
52 -> std::vector<::vk::DescriptorSetLayoutBinding>;
53};
54
55class Shader {
56 public:
57 using Ptr = std::shared_ptr<Shader>;
58
59 static auto create(const ::vk::Device &device,
60 const std::string &vertex_shader,
61 const std::string &fragment_shader) -> expected<Ptr>;
62
63 static auto create(const ::vk::Device &device,
64 const std::filesystem::path &shader_path) -> expected<Ptr>;
65
66 static auto compile_shader(const ::vk::Device &device,
67 const shaderc_shader_kind &shader_kind,
68 const std::string &filename,
69 const std::string &shader_source)
71
72 [[nodiscard]] auto get_pipeline() const { return pipeline_; }
73 [[nodiscard]] auto pipeline_layout() const { return pipeline_layout_; }
74 [[nodiscard]] auto descriptor_layout() const { return descriptor_layout_; }
75
76 void fragment_shader(const ShaderModule &fragment) {
77 fragment_shader_module_ = fragment;
78 }
79
80 void vertex_shader(const ShaderModule &vertex) {
81 vertex_shader_module_ = vertex;
82 }
83
84 auto create_graphics_pipeline(const ::vk::Device &device,
85 const ::vk::RenderPass &render_pass,
86 const math::Vec2f &size, bool depth)
87 -> expected<void>;
88
89 private:
90 static auto read_wren_shader_file(const std::filesystem::path &path)
91 -> expected<std::map<ShaderType, std::string>>;
92
93 ::vk::DescriptorSetLayout descriptor_layout_;
94 ::vk::PipelineLayout pipeline_layout_;
95 ::vk::Pipeline pipeline_;
96
97 ShaderModule vertex_shader_module_;
98 ShaderModule fragment_shader_module_;
99};
100
101} // namespace wren::vk
#define DESCRIBED_ENUM(E,...)
Definition enums.hpp:7
Definition ui.hpp:5
BOOST_DESCRIBE_ENUM(Result, eSuccess, eNotReady, eTimeout, eEventSet, eEventReset, eIncomplete, eErrorOutOfHostMemory, eErrorOutOfDeviceMemory, eErrorInitializationFailed, eErrorDeviceLost, eErrorMemoryMapFailed, eErrorLayerNotPresent, eErrorExtensionNotPresent, eErrorFeatureNotPresent, eErrorIncompatibleDriver, eErrorTooManyObjects, eErrorFormatNotSupported, eErrorFragmentedPool, eErrorUnknown, eErrorOutOfPoolMemory, eErrorOutOfPoolMemoryKHR, eErrorInvalidExternalHandle, eErrorInvalidExternalHandleKHR, eErrorFragmentation, eErrorFragmentationEXT, eErrorInvalidOpaqueCaptureAddress, eErrorInvalidDeviceAddressEXT, eErrorInvalidOpaqueCaptureAddressKHR, ePipelineCompileRequired, eErrorPipelineCompileRequiredEXT, ePipelineCompileRequiredEXT, eErrorSurfaceLostKHR, eErrorNativeWindowInUseKHR, eSuboptimalKHR, eErrorOutOfDateKHR, eErrorIncompatibleDisplayKHR, eErrorValidationFailedEXT, eErrorInvalidShaderNV, eErrorImageUsageNotSupportedKHR, eErrorVideoPictureLayoutNotSupportedKHR, eErrorVideoProfileOperationNotSupportedKHR, eErrorVideoProfileFormatNotSupportedKHR, eErrorVideoProfileCodecNotSupportedKHR, eErrorVideoStdVersionNotSupportedKHR, eErrorInvalidDrmFormatModifierPlaneLayoutEXT, eErrorNotPermittedKHR, eErrorNotPermittedEXT, eThreadIdleKHR, eThreadDoneKHR, eOperationDeferredKHR, eOperationNotDeferredKHR, eErrorCompressionExhaustedEXT)
Definition render_pass.hpp:19
std::expected< T, Err > expected
Definition result.hpp:49
#define DEFINE_ERROR_IMPL(CAT_NAME, ERROR_ENUM)
This macro creates the hooks into std::error_code for a given error enum.
Definition result.hpp:60