summaryrefslogtreecommitdiff
path: root/include/vulkan_helper.h
diff options
context:
space:
mode:
authorChuyan Zhang <chuyan@ucsb.edu>2024-10-10 18:51:05 -0700
committerChuyan Zhang <chuyan@ucsb.edu>2024-10-10 18:51:05 -0700
commit6271f5ce797bf12be64c710b66b1b9e93a239829 (patch)
tree44cc6a5ab5a20f7e55829f18b68a9f1191dac81d /include/vulkan_helper.h
parentba27f3c22e79b91a2573b7efd00c5a3bbdb96dbc (diff)
downloadiris-6271f5ce797bf12be64c710b66b1b9e93a239829.tar.gz
iris-6271f5ce797bf12be64c710b66b1b9e93a239829.zip
move to new resource abstractionHEADmain
Diffstat (limited to 'include/vulkan_helper.h')
-rw-r--r--include/vulkan_helper.h126
1 files changed, 126 insertions, 0 deletions
diff --git a/include/vulkan_helper.h b/include/vulkan_helper.h
new file mode 100644
index 0000000..ec115fa
--- /dev/null
+++ b/include/vulkan_helper.h
@@ -0,0 +1,126 @@
+#pragma once
+
+#include "resources/buffer.h"
+#include "resources/image.h"
+#include <memory>
+#include <vulkan/vulkan_core.h>
+#include <vulkan/vk_enum_string_helper.h>
+#include <vk_mem_alloc.h>
+#include <spdlog/spdlog.h>
+
+#include <cstdint>
+#include <vector>
+#include <string>
+
+#define CHECK_VULKAN(result) \
+ do { \
+ VkResult res = result; \
+ if (res != VK_SUCCESS) { \
+ spdlog::error("Vulkan error: {}", string_VkResult(res)); \
+ abort(); \
+ } \
+ } while (0)
+
+namespace iris {
+
+struct Buffer_t {
+ VkBuffer buffer;
+ VkDevice device;
+ VmaAllocator allocator;
+ VmaAllocation allocation;
+ VkBufferUsageFlags flags;
+ VkDeviceSize size;
+ void *mapped_data = nullptr;
+
+ void* map();
+ void unmap();
+ void release();
+ ~Buffer_t() { release(); }
+};
+
+typedef std::shared_ptr<Buffer_t> Buffer;
+
+struct Texture2D_t {
+ VkImage image;
+ VkDevice device;
+ VmaAllocator allocator;
+ VmaAllocation allocation;
+ VkImageView image_view;
+ VkImageLayout layout;
+
+ VkImageUsageFlags flags;
+ VkExtent2D extent;
+
+ void release();
+ ~Texture2D_t() { release(); }
+};
+
+typedef std::shared_ptr<Texture2D_t> Texture2D;
+
+// This is a really brute-force implementation,
+// every command pool contains only 1 command buffer
+struct CommandBuffer {
+ VkDevice device;
+ VkCommandPool pool;
+ VkCommandBuffer buffer;
+ VkFence fence;
+ VkQueue queue;
+
+ CommandBuffer(VkDevice device, uint32_t queue_family_index, VkQueue queue);
+ void release();
+ void begin(VkCommandBufferUsageFlags flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT);
+ void submit_sync();
+ ~CommandBuffer() { release(); }
+};
+
+struct Device {
+ VkInstance instance;
+ VkPhysicalDevice physical_device;
+ VkDevice device;
+ uint32_t main_queue_family_index;
+ VkQueue graphics_queue;
+ VmaAllocator allocator;
+#ifdef USE_VULKAN_VALIDATION_LAYERS
+ VkDebugReportCallbackEXT debugReportCallback;
+ VkDebugUtilsMessengerEXT debugUtilsMessenger;
+#endif
+
+ Device() = delete;
+ Device(
+ std::vector<std::string> layers,
+ std::vector<std::string> instance_extensions);
+ void destroy();
+
+ Buffer create_buffer(
+ VkDeviceSize size,
+ VkBufferUsageFlags usage,
+ VmaAllocationCreateInfo create_info = {
+ .usage = VMA_MEMORY_USAGE_AUTO,
+ });
+
+ Texture2D create_texture(
+ VkExtent2D extent,
+ VkFormat format,
+ VkImageUsageFlags usage,
+ VmaAllocationCreateInfo create_info = {
+ .usage = VMA_MEMORY_USAGE_AUTO,
+ });
+
+ Texture2D create_texture_from_image(
+ const char* filename,
+ VkFormat format,
+ VkImageUsageFlags usage,
+ VmaAllocationCreateInfo create_info = {
+ .usage = VMA_MEMORY_USAGE_AUTO,
+ });
+
+ std::shared_ptr<Buffer_tt> create_buffer(
+ BufferDesc desc);
+
+ std::shared_ptr<Image_tt> create_image(
+ ImageDesc desc);
+
+ CommandBuffer create_command_buffer();
+};
+
+} // namespace iris \ No newline at end of file