diff options
author | Chuyan Zhang <chuyan@ucsb.edu> | 2024-10-10 18:51:05 -0700 |
---|---|---|
committer | Chuyan Zhang <chuyan@ucsb.edu> | 2024-10-10 18:51:05 -0700 |
commit | 6271f5ce797bf12be64c710b66b1b9e93a239829 (patch) | |
tree | 44cc6a5ab5a20f7e55829f18b68a9f1191dac81d | |
parent | ba27f3c22e79b91a2573b7efd00c5a3bbdb96dbc (diff) | |
download | iris-main.tar.gz iris-main.zip |
-rw-r--r-- | include/gltf_loader.h (renamed from src/gltf_loader.h) | 0 | ||||
-rw-r--r-- | include/render_assets.h (renamed from src/render_assets.h) | 0 | ||||
-rw-r--r-- | include/render_graph.h (renamed from src/render_graph.h) | 18 | ||||
-rw-r--r-- | include/render_pass.h (renamed from src/render_pass.h) | 0 | ||||
-rw-r--r-- | include/resources/buffer.h | 37 | ||||
-rw-r--r-- | include/resources/image.h | 71 | ||||
-rw-r--r-- | include/shader.h (renamed from src/shader.h) | 0 | ||||
-rw-r--r-- | include/vulkan_helper.h (renamed from src/vulkan_helper.h) | 8 | ||||
-rw-r--r-- | include/vulkan_swapchain.h (renamed from src/vulkan_swapchain.h) | 0 | ||||
-rw-r--r-- | src/resources/buffer.cpp | 56 | ||||
-rw-r--r-- | src/resources/image.cpp | 43 | ||||
-rw-r--r-- | xmake.lua | 1 |
12 files changed, 233 insertions, 1 deletions
diff --git a/src/gltf_loader.h b/include/gltf_loader.h index 4b3190b..4b3190b 100644 --- a/src/gltf_loader.h +++ b/include/gltf_loader.h diff --git a/src/render_assets.h b/include/render_assets.h index 1819271..1819271 100644 --- a/src/render_assets.h +++ b/include/render_assets.h diff --git a/src/render_graph.h b/include/render_graph.h index f09d6cb..b32d3eb 100644 --- a/src/render_graph.h +++ b/include/render_graph.h @@ -6,6 +6,22 @@ namespace iris { +struct BufferDesc { + +}; + +struct ImageDesc { + +}; + +struct ImageWithSamplerDesc { + +}; + +struct AccelerationStructureDesc { + +}; + struct CreatedRenderResource { enum struct Type { Buffer, @@ -14,7 +30,7 @@ struct CreatedRenderResource { AccelerationStructure, } type; - + }; struct ImportedRenderResource { diff --git a/src/render_pass.h b/include/render_pass.h index b01bcae..b01bcae 100644 --- a/src/render_pass.h +++ b/include/render_pass.h diff --git a/include/resources/buffer.h b/include/resources/buffer.h new file mode 100644 index 0000000..7c6fe0e --- /dev/null +++ b/include/resources/buffer.h @@ -0,0 +1,37 @@ +#pragma once + +#include <cstdint> +#include <memory> +#include <optional> +#include <vk_mem_alloc.h> +#include <vulkan/vulkan_core.h> +namespace iris { + +struct BufferDesc { + uint64_t size; + std::optional<uint64_t> alignment; + VkBufferUsageFlags buffer_usage; + VmaMemoryUsage memory_usage; +}; + +struct Buffer_tt { + VkBuffer handle; + BufferDesc desc; + + // Used during release + VkDevice device; + VmaAllocator allocator; + VmaAllocation allocation; + void release(); + + // Used during map/unmap + std::optional<void *> mapped_data; + void *map(); + void unmap(); + + ~Buffer_tt() { release(); } +}; + +typedef std::shared_ptr<Buffer_tt> Buffer_s; + +} // namespace iris
\ No newline at end of file diff --git a/include/resources/image.h b/include/resources/image.h new file mode 100644 index 0000000..391c023 --- /dev/null +++ b/include/resources/image.h @@ -0,0 +1,71 @@ +#pragma once + +#include <unordered_map> +#include <vk_mem_alloc.h> +#include <vulkan/vulkan_core.h> +namespace iris { + +enum struct ImageType { + Texture1D, + Texture1DArray, + Texture2D, + Texture2DArray, + Texture3D, + TextureCube, + TextureCubeArray, +}; + +struct ImageDesc { + ImageType image_type; + VkImageUsageFlags image_usage; + VkImageCreateFlags create_flags; + VmaAllocationCreateFlags alloc_flags; + + VkFormat format; + VkExtent3D extent; + uint32_t mip_levels; + uint32_t array_layers; +}; + +struct ImageViewDesc { + VkImageViewType view_type; + VkFormat format; + VkImageAspectFlags aspect_mask; + uint32_t base_mip_level; + uint32_t mip_levels; + uint32_t base_array_layer; + uint32_t array_layers; + + bool operator==(const ImageViewDesc& other) const = default; +}; + +struct ImageViewDescHash { + std::size_t operator()(const ImageViewDesc& desc) const noexcept { + auto h = std::hash<int>{}(desc.view_type); + h ^= std::hash<int>{}(desc.format) + 0x9e3779b9 + (h << 6) + (h >> 2); + h ^= std::hash<int>{}(desc.aspect_mask) + 0x9e3779b9 + (h << 6) + (h >> 2); + h ^= std::hash<int>{}(desc.base_mip_level) + 0x9e3779b9 + (h << 6) + (h >> 2); + h ^= std::hash<int>{}(desc.mip_levels) + 0x9e3779b9 + (h << 6) + (h >> 2); + h ^= std::hash<int>{}(desc.base_array_layer) + 0x9e3779b9 + (h << 6) + (h >> 2); + h ^= std::hash<int>{}(desc.array_layers) + 0x9e3779b9 + (h << 6) + (h >> 2); + return h; + } +}; + +struct Image_tt { + VkImage handle; + ImageDesc desc; + + // Used during release + VkDevice device; + VmaAllocator allocator; + VmaAllocation allocation; + + // store all the image views + std::unordered_map<ImageViewDesc, VkImageView, ImageViewDescHash> image_views; + VkImageView get_image_view(const ImageViewDesc &desc); + + void release(); +}; + +} // namespace iris
\ No newline at end of file diff --git a/src/shader.h b/include/shader.h index b34fd92..b34fd92 100644 --- a/src/shader.h +++ b/include/shader.h diff --git a/src/vulkan_helper.h b/include/vulkan_helper.h index 04fcead..ec115fa 100644 --- a/src/vulkan_helper.h +++ b/include/vulkan_helper.h @@ -1,5 +1,7 @@ #pragma once +#include "resources/buffer.h" +#include "resources/image.h" #include <memory> #include <vulkan/vulkan_core.h> #include <vulkan/vk_enum_string_helper.h> @@ -112,6 +114,12 @@ struct Device { .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(); }; diff --git a/src/vulkan_swapchain.h b/include/vulkan_swapchain.h index 8d4604e..8d4604e 100644 --- a/src/vulkan_swapchain.h +++ b/include/vulkan_swapchain.h 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 @@ -66,6 +66,7 @@ target("glm") target("iris_renderer") set_kind("binary") add_files(path.join(src_dir, "**.cpp")) + add_includedirs("include") -- Link external libraries add_deps("argparse", "imgui", "tinygltf", "tinyobjloader", "stb", "glm") |