summaryrefslogtreecommitdiff
path: root/src/resources
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 /src/resources
parentba27f3c22e79b91a2573b7efd00c5a3bbdb96dbc (diff)
downloadiris-main.tar.gz
iris-main.zip
move to new resource abstractionHEADmain
Diffstat (limited to 'src/resources')
-rw-r--r--src/resources/buffer.cpp56
-rw-r--r--src/resources/image.cpp43
2 files changed, 99 insertions, 0 deletions
diff --git a/src/resources/buffer.cpp b/src/resources/buffer.cpp
new file mode 100644
index 0000000..8568466
--- /dev/null
+++ b/src/resources/buffer.cpp
@@ -0,0 +1,56 @@
+#include "resources/buffer.h"
+#include "vulkan_helper.h"
+#include <memory>
+
+namespace iris {
+
+void *Buffer_tt::map() {
+ if (!mapped_data.has_value()) {
+ CHECK_VULKAN(vmaMapMemory(allocator, allocation, &mapped_data.value()));
+ }
+ return mapped_data.value();
+}
+
+void Buffer_tt::unmap() {
+ if (mapped_data.has_value()) {
+ vmaUnmapMemory(allocator, allocation);
+ mapped_data.reset();
+ } else {
+ spdlog::warn("Buffer is not mapped, skipping");
+ }
+}
+
+void Buffer_tt::release() {
+ if (handle != VK_NULL_HANDLE) {
+ spdlog::debug("Destroying buffer: 0x{:x}", (uint64_t)handle);
+ vkDeviceWaitIdle(device);
+ vmaDestroyBuffer(allocator, handle, allocation);
+ handle = VK_NULL_HANDLE;
+ }
+}
+
+std::shared_ptr<Buffer_tt> Device::create_buffer(BufferDesc desc) {
+ VkBufferCreateInfo buffer_create_info = {
+ .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
+ .size = desc.size,
+ .usage = desc.buffer_usage,
+ };
+ VmaAllocationCreateInfo alloc_create_info = {
+ .usage = desc.memory_usage,
+ };
+ std::shared_ptr<Buffer_tt> buffer = std::make_shared<Buffer_tt>();
+ buffer->device = this->device;
+ buffer->allocator = this->allocator;
+ buffer->desc = desc;
+ CHECK_VULKAN(vmaCreateBuffer(
+ allocator,
+ &buffer_create_info,
+ &alloc_create_info,
+ &buffer->handle,
+ &buffer->allocation,
+ VK_NULL_HANDLE));
+ spdlog::debug("Created buffer: 0x{:x}", (uint64_t)buffer->handle);
+ return buffer;
+}
+
+} // namespace iris \ No newline at end of file
diff --git a/src/resources/image.cpp b/src/resources/image.cpp
new file mode 100644
index 0000000..0c51b2d
--- /dev/null
+++ b/src/resources/image.cpp
@@ -0,0 +1,43 @@
+#include "resources/image.h"
+#include "vulkan_helper.h"
+#include <vulkan/vulkan_core.h>
+
+namespace iris {
+
+VkImageView Image_tt::get_image_view(const ImageViewDesc &desc) {
+ auto it = image_views.find(desc);
+ if (it != image_views.end()) {
+ return it->second;
+ }
+
+ // Not found, create the image view
+ VkImageViewCreateInfo view_create_info = {
+ .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
+ .image = handle,
+ .viewType = desc.view_type,
+ .format = desc.format,
+ .components = {
+ .r = VK_COMPONENT_SWIZZLE_IDENTITY,
+ .g = VK_COMPONENT_SWIZZLE_IDENTITY,
+ .b = VK_COMPONENT_SWIZZLE_IDENTITY,
+ .a = VK_COMPONENT_SWIZZLE_IDENTITY,
+ },
+ .subresourceRange = {
+ .aspectMask = desc.aspect_mask,
+ .baseMipLevel = desc.base_mip_level,
+ .levelCount = desc.mip_levels,
+ .baseArrayLayer = desc.base_array_layer,
+ .layerCount = desc.array_layers,
+ },
+ };
+ VkImageView view;
+ CHECK_VULKAN(vkCreateImageView(
+ device,
+ &view_create_info,
+ nullptr,
+ &view));
+ image_views[desc] = view;
+ return view;
+}
+
+} // namespace iris \ No newline at end of file