summaryrefslogtreecommitdiff
path: root/src/vulkan_helper.cpp
diff options
context:
space:
mode:
authorChuyan Zhang <me@zcy.moe>2024-09-07 00:40:38 -0700
committerChuyan Zhang <me@zcy.moe>2024-09-07 00:40:38 -0700
commit2ead02037dc89e987fbc0a021fe470e29d226cfd (patch)
tree473011eda50080299be844e69f82cec75aa4fb95 /src/vulkan_helper.cpp
parente12ca33626bdadedc3158cb69f2a4d2f9bbeeeb0 (diff)
downloadiris-2ead02037dc89e987fbc0a021fe470e29d226cfd.tar.gz
iris-2ead02037dc89e987fbc0a021fe470e29d226cfd.zip
Add more encapsulation, use VMA for allocation
Diffstat (limited to 'src/vulkan_helper.cpp')
-rw-r--r--src/vulkan_helper.cpp91
1 files changed, 89 insertions, 2 deletions
diff --git a/src/vulkan_helper.cpp b/src/vulkan_helper.cpp
index 84a2b6d..e533d2d 100644
--- a/src/vulkan_helper.cpp
+++ b/src/vulkan_helper.cpp
@@ -3,17 +3,19 @@
#include <cstdint>
#include <cstdlib>
#include <iostream>
+#include <memory>
+#include <vk_mem_alloc.h>
namespace iris {
-Device::Device(std::vector<std::string> layers, std::vector<std::string> instance_extensions) {
+Device::Device(std::vector<std::string> layers, std::vector<std::string> instance_extensions) {
VkApplicationInfo app_info = {
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
.pApplicationName = "IrisRenderer",
.applicationVersion = VK_MAKE_VERSION(1, 0, 0),
.pEngineName = "No Engine",
.engineVersion = VK_MAKE_VERSION(1, 0, 0),
- .apiVersion = VK_API_VERSION_1_0,
+ .apiVersion = VK_API_VERSION_1_3,
};
// Create the Vulkan instance
@@ -140,6 +142,91 @@ Device::Device(std::vector<std::string> layers, std::vector<std::string> instan
main_queue_family_index,
0,
&graphics_queue);
+
+ // Create the memory allocator
+ VmaAllocatorCreateInfo allocator_info = {
+ .physicalDevice = physical_device,
+ .device = device,
+ .instance = instance,
+ };
+ CHECK_VULKAN(vmaCreateAllocator(
+ &allocator_info,
+ &allocator));
+}
+
+Device::~Device() {
+ vmaDestroyAllocator(allocator);
+ vkDestroyDevice(device, VK_NULL_HANDLE);
+ vkDestroyInstance(instance, VK_NULL_HANDLE);
+}
+
+
+Buffer_t::~Buffer_t() {
+ vmaDestroyBuffer(allocator, buffer, allocation);
+}
+
+Buffer Device::create_buffer(VkDeviceSize size,
+ VkBufferUsageFlags usage,
+ VmaMemoryUsage memory_usage) {
+ VkBufferCreateInfo buffer_info = {
+ .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
+ .size = size,
+ .usage = usage,
+ };
+ VmaAllocationCreateInfo allocation_info = {
+ .usage = memory_usage,
+ };
+ Buffer_t buffer = {
+ .allocator = this->allocator,
+ .flags = usage,
+ .size = size,
+ };
+ CHECK_VULKAN(vmaCreateBuffer(
+ allocator,
+ &buffer_info,
+ &allocation_info,
+ &buffer.buffer,
+ &buffer.allocation,
+ VK_NULL_HANDLE));
+ return std::make_shared<Buffer_t>(buffer);
+}
+
+Texture2D_t::~Texture2D_t() {
+ vmaDestroyImage(allocator, image, allocation);
+ // TODO: optionally destroy image view, if created
+}
+
+Texture2D Device::create_texture(VkExtent2D extent,
+ VkFormat format,
+ VkImageUsageFlags usage,
+ VmaMemoryUsage memory_usage) {
+ VkImageCreateInfo image_info = {
+ .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
+ .imageType = VK_IMAGE_TYPE_2D,
+ .format = format,
+ .extent = {extent.width, extent.height, 1},
+ .mipLevels = 1,
+ .arrayLayers = 1,
+ .samples = VK_SAMPLE_COUNT_1_BIT,
+ .tiling = VK_IMAGE_TILING_OPTIMAL,
+ .usage = usage,
+ };
+ VmaAllocationCreateInfo allocation_info = {
+ .usage = memory_usage,
+ };
+ Texture2D_t texture = {
+ .allocator = this->allocator,
+ .flags = usage,
+ .extent = extent,
+ };
+ CHECK_VULKAN(vmaCreateImage(
+ allocator,
+ &image_info,
+ &allocation_info,
+ &texture.image,
+ &texture.allocation,
+ VK_NULL_HANDLE));
+ return std::make_shared<Texture2D_t>(texture);
}
} // namespace iris \ No newline at end of file