diff options
Diffstat (limited to 'include/vk-device.hpp')
| -rw-r--r-- | include/vk-device.hpp | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/include/vk-device.hpp b/include/vk-device.hpp new file mode 100644 index 0000000..e2e8c99 --- /dev/null +++ b/include/vk-device.hpp @@ -0,0 +1,163 @@ +#pragma once + +#include "freelist.hpp" +#include "vk-bindless.hpp" +#include "vk-command-buffer.hpp" + +using VmaAllocator = struct VmaAllocator_T*; + +struct command_buffer; +class window; + +class vkdevice { + struct queue { + VkQueue vk_queue = VK_NULL_HANDLE; + uint32_t index = UINT32_MAX; + VkQueueFlags usages; + VkCommandPool command_pool = VK_NULL_HANDLE; + }; + + public: + void init(); + void deinit(); + + static inline vkdevice& get() { + return render_device; + } + + handle<device_texture> create_texture(const texture_desc& desc); + + handle<device_sampler> create_sampler(const sampler_desc& desc); + + handle<device_buffer> create_buffer(const buffer_desc& desc); + + handle<device_pipeline> create_graphics_pipeline(const graphics_pipeline_desc& desc); + + handle<device_pipeline> create_compute_pipeline(const compute_pipeline_desc& desc); + + handle<device_surface> create_surface(const surface_desc& desc); + + handle<device_semaphore> create_semaphore(const semaphore_desc& desc, const char* name); + + void destroy_texture(handle<device_texture> handle); + + void destroy_sampler(handle<device_sampler> handle); + + void destroy_buffer(handle<device_buffer> handle); + + void destroy_pipeline(handle<device_pipeline> handle); + + void destroy_semaphore(handle<device_semaphore> handle); + + void destroy_surface(handle<device_surface> handle); + + template<typename buffer_type> + void allocate_command_buffers(buffer_type* buffers, size_t count, QueueType queue_type, const char* name) { + allocate_command_buffers(static_cast<command_buffer*>(buffers), count, queue_type, name); + } + + [[nodiscard]] inline device_texture& get_texture(handle<device_texture> handle) { + return textures[handle]; + } + + [[nodiscard]] inline device_sampler& get_sampler(handle<device_sampler> handle) { + return samplers[handle]; + } + + [[nodiscard]] inline device_buffer& get_buffer(handle<device_buffer> handle) { + return buffers[handle]; + } + + [[nodiscard]] inline const device_pipeline& get_pipeline(handle<device_pipeline> handle) const { + return pipelines[handle]; + } + + [[nodiscard]] inline device_surface& get_surface(handle<device_surface> handle) { + return surfaces[handle]; + } + + [[nodiscard]] inline device_semaphore& get_semaphore(handle<device_semaphore> handle) { + return semaphores[handle]; + } + + [[nodiscard]] inline const bindless_model& get_bindingmodel() const { + return bindless; + } + + const freelist<device_texture>& get_textures() const { + return textures; + } + + inline VkDevice get_device() const { + return device; + } + + inline VmaAllocator get_allocator() { + return gpu_allocator; + } + + inline const VkPhysicalDeviceLimits& get_device_limits() const { + return physical_device_props.properties.limits; + } + + // uint64_t submit(std::span<command_buffer> buffers, handle<device_semaphore> wait_handle, handle<device_semaphore> signal_handle, handle<device_semaphore> fence_handle); + uint32_t acquire_image_index(handle<device_surface> surface_handle); + uint64_t submit_before_present(handle<device_surface> surface_handle, command_buffer* buffers, uint32_t count); + uint64_t submit(command_buffer* buffers, uint32_t count); + + void present(handle<device_surface> surface_handle); + + void wait(); + + private: + vkdevice() = default; + ~vkdevice() = default; + + void allocate_command_buffers(command_buffer* buffers, size_t count, QueueType type, const char* name); + + void create_swapchain(const surface_desc& desc, device_surface& surface); + + // TODO: Wrap in a struct called adapter ? + void create_instance(); + + void create_device(); + + void create_memory_allocator(); + + void create_command_pools(); + + void pick_physical_device(); + + bool device_support_features(VkPhysicalDevice physical_dev); + + void create_debug_layer_callback(); + + queue queues[static_cast<uint32_t>(QueueType::MAX)]; + + VkInstance instance; + VkPhysicalDevice physical_device; + VkPhysicalDeviceProperties2 physical_device_props; + VkDevice device; + + VmaAllocator gpu_allocator; + + bindless_model bindless; + + static constexpr size_t max_allocable_command_buffers = 16; + static constexpr size_t max_submitable_command_buffers = 16; + + freelist<device_texture> textures; + freelist<device_sampler> samplers; + freelist<device_buffer> buffers; + freelist<device_pipeline> pipelines; + freelist<device_surface> surfaces; + freelist<device_semaphore> semaphores; + + device_semaphore timeline_semaphore; + + static vkdevice render_device; + +#ifdef _DEBUG + VkDebugUtilsMessengerEXT debug_messenger; +#endif // _DEBUG +}; |
