374 lines
15 KiB
C++
374 lines
15 KiB
C++
#ifndef _VULKAN_HPP_
|
|
#define _VULKAN_HPP_
|
|
|
|
#include <vulkan/vulkan.h>
|
|
|
|
#include "rusty.hpp"
|
|
|
|
namespace vk {
|
|
|
|
class InstanceBuilder;
|
|
class DeviceBuilder;
|
|
class BufferBuilder;
|
|
class ImageBuilder;
|
|
class SamplerBuilder;
|
|
class SwapchainBuilder;
|
|
class RenderPassBuilder;
|
|
class PipelineBuilder;
|
|
class FramebufferBuilder;
|
|
class CommandPoolBuilder;
|
|
class CommandBufferBuilder;
|
|
class DescriptorSetLayoutBuilder;
|
|
class DescriptorPoolBuilder;
|
|
|
|
struct Queue {
|
|
VkQueue handle;
|
|
uint32_t queue_family_index;
|
|
uint32_t queue_index;
|
|
};
|
|
|
|
class PhysicalDevice {
|
|
public:
|
|
virtual ~PhysicalDevice() = default;
|
|
virtual VkPhysicalDevice handle() const = 0;
|
|
virtual const VkPhysicalDeviceProperties* properties() const = 0;
|
|
virtual const VkPhysicalDeviceMemoryProperties* mem_properties() const = 0;
|
|
virtual const VkPhysicalDeviceFeatures* supported_features() const = 0;
|
|
virtual rs::iterator<const VkExtensionProperties*> enumerate_extensions() const = 0;
|
|
virtual rs::iterator<const VkQueueFamilyProperties*> enumerate_queue_families() const = 0;
|
|
};
|
|
|
|
class Instance {
|
|
public:
|
|
static InstanceBuilder& begin_build();
|
|
virtual ~Instance() = default;
|
|
virtual VkInstance handle() const = 0;
|
|
virtual rs::iterator<const PhysicalDevice*> enumerate_physical_device() const = 0;
|
|
};
|
|
|
|
class Surface {
|
|
public:
|
|
static std::shared_ptr<Surface> from_raw_surface(VkSurfaceKHR surface, const Instance& inst, const PhysicalDevice& physical_device);
|
|
virtual ~Surface() = default;
|
|
virtual VkSurfaceKHR handle() const = 0;
|
|
virtual void refresh() = 0;
|
|
virtual const VkSurfaceCapabilitiesKHR* capabilities() const = 0;
|
|
virtual rs::iterator<const VkSurfaceFormatKHR*> enumerate_formats() const = 0;
|
|
virtual const VkSurfaceFormatKHR* default_format() const = 0;
|
|
};
|
|
|
|
class Device {
|
|
public:
|
|
static DeviceBuilder& begin_build(const PhysicalDevice& physical_device);
|
|
virtual ~Device() = default;
|
|
virtual VkDevice handle() const = 0;
|
|
virtual void wait_idle() const = 0;
|
|
virtual rs::iterator<const Queue*> enumerate_queues() const = 0;
|
|
virtual rs::vector<uint32_t> queue_family_in_use() const = 0;
|
|
};
|
|
|
|
class Buffer {
|
|
public:
|
|
static BufferBuilder& begin_build(const Device& device, const PhysicalDevice& physical_device);
|
|
virtual ~Buffer() = default;
|
|
virtual VkBuffer handle() const = 0;
|
|
virtual bool can_map() const = 0;
|
|
virtual void* map(uint32_t offset, uint32_t size) = 0;
|
|
virtual void unmap() = 0;
|
|
virtual void update_buffer(const void* data, uint32_t size) = 0;
|
|
};
|
|
|
|
class ImageView {
|
|
public:
|
|
virtual ~ImageView() = default;
|
|
virtual VkImageView handle() const = 0;
|
|
};
|
|
|
|
class Image {
|
|
public:
|
|
static ImageBuilder& begin_build(const Device& device, const PhysicalDevice& physical_device);
|
|
virtual ~Image() = default;
|
|
virtual VkImage handle() const = 0;
|
|
virtual std::shared_ptr<ImageView> create_view(VkFormat view_format, VkImageAspectFlags aspect) = 0;
|
|
};
|
|
|
|
class Sampler {
|
|
public:
|
|
static SamplerBuilder& begin_build(const Device& device);
|
|
virtual ~Sampler() = default;
|
|
virtual VkSampler handle() const = 0;
|
|
};
|
|
|
|
class ShaderModule {
|
|
public:
|
|
static std::shared_ptr<ShaderModule> load_from_spv(const Device& device, const std::string& filename);
|
|
static std::shared_ptr<ShaderModule> load_from_src(const Device& device, const std::string& filename, uint32_t kind);
|
|
virtual ~ShaderModule() = default;
|
|
virtual VkShaderModule handle() const = 0;
|
|
|
|
static const uint32_t shader_type_vertex = 0;
|
|
static const uint32_t shader_type_fragment = 1;
|
|
static const uint32_t shader_type_compute = 2;
|
|
static const uint32_t shader_type_geometry = 3;
|
|
static const uint32_t shader_type_tess_control = 4;
|
|
static const uint32_t shader_type_tess_evaluation = 5;
|
|
};
|
|
|
|
class Swapchain {
|
|
public:
|
|
static SwapchainBuilder& begin_build(const Device& device);
|
|
virtual ~Swapchain() = default;
|
|
virtual VkSwapchainKHR handle() const = 0;
|
|
virtual uint32_t image_count() const = 0;
|
|
virtual rs::vector<ImageView*> get_swapchain_images() = 0;
|
|
};
|
|
|
|
class RenderPass {
|
|
public:
|
|
static RenderPassBuilder& begin_build(const Device& device);
|
|
virtual ~RenderPass() = default;
|
|
virtual VkRenderPass handle() const = 0;
|
|
};
|
|
|
|
class DescriptorSet {
|
|
public:
|
|
virtual ~DescriptorSet() = default;
|
|
virtual VkDescriptorSet handle() const = 0;
|
|
virtual DescriptorSet& update_write(VkDescriptorType type, uint32_t binding, uint32_t start_index) = 0;
|
|
virtual DescriptorSet& update_write_buffer_info(const Buffer& buffer, uint32_t offset, uint32_t range, uint32_t rep_count) = 0;
|
|
virtual DescriptorSet& update_write_image_info(const ImageView& view, const Sampler& sampler) = 0;
|
|
virtual DescriptorSet& update_end() = 0;
|
|
};
|
|
|
|
class DescriptorSetLayout {
|
|
public:
|
|
static DescriptorSetLayoutBuilder& begin_build(const Device& device);
|
|
virtual ~DescriptorSetLayout() = default;
|
|
virtual VkDescriptorSetLayout handle() const = 0;
|
|
};
|
|
|
|
class DescriptorPool {
|
|
public:
|
|
static DescriptorPoolBuilder& begin_build(const Device& device);
|
|
virtual ~DescriptorPool() = default;
|
|
virtual VkDescriptorPool handle() const = 0;
|
|
virtual rs::vector<std::shared_ptr<DescriptorSet>> alloc_descriptor_set(const DescriptorSetLayout& layout, uint32_t count) = 0;
|
|
virtual void reset() = 0;
|
|
};
|
|
|
|
class Pipeline {
|
|
public:
|
|
static PipelineBuilder& begin_build(const Device& device);
|
|
virtual ~Pipeline() = default;
|
|
virtual VkPipeline handle() const = 0;
|
|
virtual VkPipelineLayout layout() const = 0;
|
|
};
|
|
|
|
class Framebuffer {
|
|
public:
|
|
static FramebufferBuilder& begin_build(const Device& device);
|
|
virtual ~Framebuffer() = default;
|
|
virtual VkFramebuffer handle() const = 0;
|
|
virtual VkExtent2D size() const = 0;
|
|
};
|
|
|
|
class CommandBuffer;
|
|
|
|
class CommandPipelineBarrierBuilder {
|
|
public:
|
|
virtual CommandPipelineBarrierBuilder& add_image_memory_barrior(const Image& img) = 0;
|
|
virtual CommandPipelineBarrierBuilder& image_set_layout(VkImageLayout old_layout, VkImageLayout new_layout) = 0;
|
|
virtual CommandPipelineBarrierBuilder& image_set_aspect_mask(VkImageAspectFlags aspect_mask) = 0;
|
|
virtual CommandPipelineBarrierBuilder& image_set_access_mask(VkAccessFlags src, VkAccessFlags dst) = 0;
|
|
virtual CommandBuffer& end_pipeline_barrior() = 0;
|
|
};
|
|
|
|
class CommandBufferCopyBuilder {
|
|
public:
|
|
virtual CommandBufferCopyBuilder& add_region(uint32_t src_offset, uint32_t dst_offset, uint32_t size) = 0;
|
|
virtual CommandBuffer& end_buffer_copy() = 0;
|
|
};
|
|
|
|
class CommandBufferImageCopyBuilder {
|
|
public:
|
|
virtual CommandBufferImageCopyBuilder& add_region(uint32_t offset, VkOffset3D img_offset, VkExtent3D img_extent) = 0;
|
|
virtual CommandBuffer& end_buffer_image_copy() = 0;
|
|
};
|
|
|
|
class CommandBuffer {
|
|
public:
|
|
virtual ~CommandBuffer() = default;
|
|
virtual VkCommandBuffer handle() const = 0;
|
|
virtual CommandBuffer& begin_command(VkCommandBufferUsageFlags flag) = 0;
|
|
virtual void end_command() = 0;
|
|
virtual CommandBuffer& begin_render_pass(const RenderPass& pass, const Framebuffer& buffer, VkClearValue c) = 0;
|
|
virtual CommandBuffer& end_render_pass() = 0;
|
|
virtual CommandBuffer& bind_pipeline(const Pipeline& pipeline) = 0;
|
|
virtual CommandBuffer& bind_pipeline_and_descriptor_sets(const Pipeline& pipeline, const DescriptorSet& sets) = 0;
|
|
virtual CommandBuffer& draw(uint32_t count) = 0;
|
|
virtual CommandBuffer& draw_vertices(const Buffer& vertex_buffer, uint32_t count) = 0;
|
|
virtual CommandBuffer& draw_indexed(const Buffer& vertex_buffer, const Buffer& index_buffer, uint32_t count) = 0;
|
|
virtual CommandPipelineBarrierBuilder& begin_pipeline_barrior(VkPipelineStageFlags src, VkPipelineStageFlags dst) = 0;
|
|
virtual CommandBufferCopyBuilder& begin_copy_buffer(const Buffer& src, const Buffer& dst) = 0;
|
|
virtual CommandBufferImageCopyBuilder& begin_copy_buffer_to_image(const Buffer& src, const Image& dst) = 0;
|
|
};
|
|
|
|
class CommandPool {
|
|
public:
|
|
static CommandPoolBuilder& begin_build(const Device& device);
|
|
virtual VkCommandPool handle() const = 0;
|
|
virtual rs::vector<std::shared_ptr<CommandBuffer>> alloc_command_buffer(VkCommandBufferLevel level, uint32_t count) = 0;
|
|
virtual CommandPool& alloc_command_buffer(rs::vector<std::shared_ptr<CommandBuffer>>& vec, VkCommandBufferLevel level, uint32_t count) = 0;
|
|
};
|
|
|
|
class Renderer {
|
|
public:
|
|
static std::shared_ptr<Renderer> create_renderer(const vk::Device& device, uint32_t img_count);
|
|
virtual ~Renderer() = default;
|
|
virtual uint32_t prepare_for_next_frame() = 0;
|
|
virtual uint32_t accquire_next_image(const Swapchain& swapchain) = 0;
|
|
virtual Renderer& submit_draw_command(const Queue& queue, const CommandBuffer& command_buffer) = 0;
|
|
virtual Renderer& present(const Queue& queue, const Swapchain& swapchain, uint32_t index) = 0;
|
|
virtual Renderer& wait_idle(const Queue& queue) = 0;
|
|
virtual Renderer& submit_onetime_command(const Queue& queue, rs::vector<std::shared_ptr<CommandBuffer>>& command_buffers) = 0;
|
|
virtual bool out_of_date() const = 0;
|
|
};
|
|
|
|
rs::vector<VkLayerProperties> list_layer_properties();
|
|
|
|
class InstanceBuilder {
|
|
public:
|
|
virtual InstanceBuilder& set_application_name(const char* name) = 0;
|
|
virtual InstanceBuilder& set_enging_name(const char* name) = 0;
|
|
virtual InstanceBuilder& set_application_version(uint32_t ver) = 0;
|
|
virtual InstanceBuilder& set_engine_version(uint32_t ver) = 0;
|
|
virtual InstanceBuilder& set_api_version(uint32_t ver) = 0;
|
|
virtual InstanceBuilder& set_layers(const rs::vector<const char*>& layers) = 0;
|
|
virtual InstanceBuilder& set_extensions(const rs::vector<const char*>& extensions) = 0;
|
|
virtual std::shared_ptr<Instance> end() = 0;
|
|
};
|
|
|
|
class DeviceBuilder {
|
|
public:
|
|
virtual DeviceBuilder& set_extensions(const rs::vector<const char*>& extensions) = 0;
|
|
virtual DeviceBuilder& add_queue_family(uint32_t queue_family_index, float priority) = 0;
|
|
virtual DeviceBuilder& enable_features(const VkPhysicalDeviceFeatures& features) = 0;
|
|
virtual std::shared_ptr<Device> end() = 0;
|
|
};
|
|
|
|
class BufferBuilder {
|
|
public:
|
|
virtual BufferBuilder& alloc_size(size_t size) = 0;
|
|
virtual BufferBuilder& set_usage(VkBufferUsageFlags usage) = 0;
|
|
virtual BufferBuilder& set_sharing_mode(VkSharingMode mode) = 0;
|
|
virtual BufferBuilder& set_cpu_accessible() = 0;
|
|
virtual BufferBuilder& set_device_local() = 0;
|
|
virtual std::shared_ptr<Buffer> end() = 0;
|
|
};
|
|
|
|
class ImageBuilder {
|
|
public:
|
|
virtual ImageBuilder& set_type(VkImageType type) = 0;
|
|
virtual ImageBuilder& set_format(VkFormat format) = 0;
|
|
virtual ImageBuilder& set_extent(VkExtent3D ext) = 0;
|
|
virtual ImageBuilder& set_mip_level(uint32_t level) = 0;
|
|
virtual ImageBuilder& set_array_layers(uint32_t layer) = 0;
|
|
virtual ImageBuilder& set_samples(VkSampleCountFlagBits samples) = 0;
|
|
virtual ImageBuilder& set_tilling(VkImageTiling tiling) = 0;
|
|
virtual ImageBuilder& set_usage(VkImageUsageFlags usage) = 0;
|
|
virtual ImageBuilder& set_sharing_mode(VkSharingMode mode) = 0;
|
|
virtual ImageBuilder& set_init_layout(VkImageLayout layout) = 0;
|
|
virtual ImageBuilder& set_cpu_accessible() = 0;
|
|
virtual ImageBuilder& set_device_local() = 0;
|
|
virtual std::shared_ptr<Image> end() = 0;
|
|
};
|
|
|
|
class SamplerBuilder {
|
|
public:
|
|
virtual SamplerBuilder& set_filter(VkFilter filter) = 0;
|
|
virtual SamplerBuilder& set_address_mode(VkSamplerAddressMode mode) = 0;
|
|
virtual SamplerBuilder& set_anisotrophy(float value) = 0;
|
|
virtual std::shared_ptr<Sampler> end() = 0;
|
|
};
|
|
|
|
class SwapchainBuilder {
|
|
public:
|
|
virtual SwapchainBuilder& set_surface(const Surface& surface) = 0;
|
|
virtual SwapchainBuilder& set_min_image_count(uint32_t count) = 0;
|
|
virtual SwapchainBuilder& set_format(VkFormat format) = 0;
|
|
virtual SwapchainBuilder& set_colorspace(VkColorSpaceKHR colorspace) = 0;
|
|
virtual SwapchainBuilder& set_extent(VkExtent2D ext) = 0;
|
|
virtual SwapchainBuilder& set_array_layer(uint32_t array_layer) = 0;
|
|
virtual SwapchainBuilder& set_image_usage(VkImageUsageFlags usage) = 0;
|
|
virtual SwapchainBuilder& set_queue_family_indices(const rs::vector<uint32_t>& queue_family_indices) = 0;
|
|
virtual SwapchainBuilder& set_transform(VkSurfaceTransformFlagBitsKHR transform) = 0;
|
|
virtual SwapchainBuilder& set_composite_alpha(VkCompositeAlphaFlagBitsKHR alpha) = 0;
|
|
virtual SwapchainBuilder& set_present_mode(VkPresentModeKHR mode) = 0;
|
|
virtual SwapchainBuilder& set_clipped(VkBool32 clipped) = 0;
|
|
virtual SwapchainBuilder& set_old_swapchain(Swapchain& old) = 0;
|
|
virtual std::shared_ptr<Swapchain> end() = 0;
|
|
};
|
|
|
|
class RenderPassBuilder {
|
|
public:
|
|
virtual RenderPassBuilder& set_format(VkFormat format) = 0;
|
|
virtual RenderPassBuilder& set_samples(VkSampleCountFlagBits samples) = 0;
|
|
virtual std::shared_ptr<RenderPass> end() = 0;
|
|
};
|
|
|
|
class DescriptorSetLayoutBuilder {
|
|
public:
|
|
virtual DescriptorSetLayoutBuilder& add_binding(VkDescriptorType type) = 0;
|
|
virtual DescriptorSetLayoutBuilder& set_count(uint32_t count) = 0;
|
|
virtual DescriptorSetLayoutBuilder& set_stage(VkShaderStageFlags stage) = 0;
|
|
virtual DescriptorSetLayoutBuilder& set_samplers() = 0;
|
|
virtual std::shared_ptr<DescriptorSetLayout> end() = 0;
|
|
};
|
|
|
|
class DescriptorPoolBuilder {
|
|
public:
|
|
virtual DescriptorPoolBuilder& add_pool_size(VkDescriptorType type, uint32_t count) = 0;
|
|
virtual DescriptorPoolBuilder& set_max_sets(uint32_t count) = 0;
|
|
virtual DescriptorPoolBuilder& set_flags(VkDescriptorPoolCreateFlags flag) = 0;
|
|
virtual std::shared_ptr<DescriptorPool> end() = 0;
|
|
};
|
|
|
|
class PipelineBuilder {
|
|
public:
|
|
virtual PipelineBuilder& set_render_pass(const RenderPass& render_pass) = 0;
|
|
virtual PipelineBuilder& set_vertex_shader(const ShaderModule& vert) = 0;
|
|
virtual PipelineBuilder& set_fragment_shader(const ShaderModule& frag) = 0;
|
|
virtual PipelineBuilder& normal_viewport(float w, float h) = 0;
|
|
virtual PipelineBuilder& normal_scissor(uint32_t w, uint32_t h) = 0;
|
|
virtual PipelineBuilder& draw_topology(VkPrimitiveTopology topology) = 0;
|
|
virtual PipelineBuilder& set_polygon_mode(VkPolygonMode mode) = 0;
|
|
virtual PipelineBuilder& set_cull_mode(VkCullModeFlags mode) = 0;
|
|
virtual PipelineBuilder& set_front_face(VkFrontFace front) = 0;
|
|
virtual PipelineBuilder& set_line_width(float width) = 0;
|
|
virtual PipelineBuilder& set_samples(VkSampleCountFlagBits samples) = 0;
|
|
virtual PipelineBuilder& bind_vertex_size(uint32_t sz) = 0;
|
|
virtual PipelineBuilder& add_binding_attribute(VkFormat format, uint32_t offset) = 0;
|
|
virtual PipelineBuilder& add_descriptor_set_layout(const DescriptorSetLayout& layout) = 0;
|
|
virtual std::shared_ptr<Pipeline> end() = 0;
|
|
};
|
|
|
|
class FramebufferBuilder {
|
|
public:
|
|
virtual FramebufferBuilder& set_render_pass(const RenderPass& render_pass) = 0;
|
|
virtual FramebufferBuilder& set_framebuffer_size(uint32_t width, uint32_t height) = 0;
|
|
virtual FramebufferBuilder& add_image_view(const ImageView& view) = 0;
|
|
virtual FramebufferBuilder& set_layer(uint32_t layer) = 0;
|
|
virtual std::shared_ptr<Framebuffer> end() = 0;
|
|
};
|
|
|
|
class CommandPoolBuilder {
|
|
public:
|
|
virtual CommandPoolBuilder& set_queue_family_index(uint32_t idx) = 0;
|
|
virtual CommandPoolBuilder& set_pool_flag(VkCommandPoolCreateFlags flags) = 0;
|
|
virtual std::shared_ptr<CommandPool> end() = 0;
|
|
};
|
|
|
|
}
|
|
|
|
#endif // _VULKAN_HPP_
|