From e3482e9ce5cf0ced307f491c920f93ee31b9b6c1 Mon Sep 17 00:00:00 2001 From: Sergey Kosarevsky Date: Sun, 7 Apr 2024 22:40:54 -0700 Subject: [PATCH] Don't store VkDevice and VmaAllocationCreateInfo in VulkanImage --- lvk/vulkan/VulkanClasses.cpp | 33 +++++++++++++++------------------ lvk/vulkan/VulkanClasses.h | 34 ++++++++++++++++------------------ 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/lvk/vulkan/VulkanClasses.cpp b/lvk/vulkan/VulkanClasses.cpp index afbfbed983..7e10173c77 100644 --- a/lvk/vulkan/VulkanClasses.cpp +++ b/lvk/vulkan/VulkanClasses.cpp @@ -758,11 +758,9 @@ void lvk::VulkanBuffer::bufferSubData(const VulkanContext& ctx, size_t offset, s } lvk::VulkanImage::VulkanImage(VulkanImage&& img) : ctx_(img.ctx_) { - std::swap(vkDevice_, img.vkDevice_); std::swap(vkImage_, img.vkImage_); std::swap(vkUsageFlags_, img.vkUsageFlags_); std::swap(vkMemory_, img.vkMemory_); - std::swap(vmaAllocInfo_, img.vmaAllocInfo_); std::swap(vmaAllocation_, img.vmaAllocation_); std::swap(vkFormatProperties_, img.vkFormatProperties_); std::swap(vkExtent_, img.vkExtent_); @@ -781,11 +779,9 @@ lvk::VulkanImage::VulkanImage(VulkanImage&& img) : ctx_(img.ctx_) { lvk::VulkanImage& lvk::VulkanImage::operator=(VulkanImage&& img) { std::swap(ctx_, img.ctx_); - std::swap(vkDevice_, img.vkDevice_); std::swap(vkImage_, img.vkImage_); std::swap(vkUsageFlags_, img.vkUsageFlags_); std::swap(vkMemory_, img.vkMemory_); - std::swap(vmaAllocInfo_, img.vmaAllocInfo_); std::swap(vmaAllocation_, img.vmaAllocation_); std::swap(vkFormatProperties_, img.vkFormatProperties_); std::swap(vkExtent_, img.vkExtent_); @@ -810,7 +806,6 @@ lvk::VulkanImage::VulkanImage(lvk::VulkanContext& ctx, VkExtent3D extent, const char* debugName) : ctx_(&ctx), - vkDevice_(device), vkImage_(image), vkUsageFlags_(usageFlags), isSwapchainImage_(true), @@ -819,7 +814,7 @@ lvk::VulkanImage::VulkanImage(lvk::VulkanContext& ctx, vkImageFormat_(imageFormat), isDepthFormat_(isDepthFormat(imageFormat)), isStencilFormat_(isStencilFormat(imageFormat)) { - VK_ASSERT(lvk::setDebugObjectName(vkDevice_, VK_OBJECT_TYPE_IMAGE, (uint64_t)vkImage_, debugName)); + VK_ASSERT(lvk::setDebugObjectName(device, VK_OBJECT_TYPE_IMAGE, (uint64_t)vkImage_, debugName)); } lvk::VulkanImage::VulkanImage(lvk::VulkanContext& ctx, @@ -836,7 +831,6 @@ lvk::VulkanImage::VulkanImage(lvk::VulkanContext& ctx, VkSampleCountFlagBits samples, const char* debugName) : ctx_(&ctx), - vkDevice_(device), vkUsageFlags_(usageFlags), vkExtent_(extent), vkType_(type), @@ -875,8 +869,11 @@ lvk::VulkanImage::VulkanImage(lvk::VulkanContext& ctx, }; if (LVK_VULKAN_USE_VMA) { - vmaAllocInfo_.usage = memFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT ? VMA_MEMORY_USAGE_CPU_TO_GPU : VMA_MEMORY_USAGE_AUTO; - VkResult result = vmaCreateImage((VmaAllocator)ctx_->getVmaAllocator(), &ci, &vmaAllocInfo_, &vkImage_, &vmaAllocation_, nullptr); + VmaAllocationCreateInfo vmaAllocInfo = { + .usage = memFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT ? VMA_MEMORY_USAGE_CPU_TO_GPU : VMA_MEMORY_USAGE_AUTO, + }; + + VkResult result = vmaCreateImage((VmaAllocator)ctx_->getVmaAllocator(), &ci, &vmaAllocInfo, &vkImage_, &vmaAllocation_, nullptr); if (!LVK_VERIFY(result == VK_SUCCESS)) { LLOGW("failed: error result: %d, memflags: %d, imageformat: %d\n", result, memFlags, vkImageFormat_); @@ -888,24 +885,24 @@ lvk::VulkanImage::VulkanImage(lvk::VulkanContext& ctx, } } else { // create image - VK_ASSERT(vkCreateImage(vkDevice_, &ci, nullptr, &vkImage_)); + VK_ASSERT(vkCreateImage(device, &ci, nullptr, &vkImage_)); // back the image with some memory { VkMemoryRequirements memRequirements; vkGetImageMemoryRequirements(device, vkImage_, &memRequirements); - VK_ASSERT(lvk::allocateMemory(ctx_->getVkPhysicalDevice(), vkDevice_, &memRequirements, memFlags, &vkMemory_)); - VK_ASSERT(vkBindImageMemory(vkDevice_, vkImage_, vkMemory_, 0)); + VK_ASSERT(lvk::allocateMemory(ctx_->getVkPhysicalDevice(), device, &memRequirements, memFlags, &vkMemory_)); + VK_ASSERT(vkBindImageMemory(device, vkImage_, vkMemory_, 0)); } // handle memory-mapped images if (memFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { - VK_ASSERT(vkMapMemory(vkDevice_, vkMemory_, 0, VK_WHOLE_SIZE, 0, &mappedPtr_)); + VK_ASSERT(vkMapMemory(device, vkMemory_, 0, VK_WHOLE_SIZE, 0, &mappedPtr_)); } } - VK_ASSERT(lvk::setDebugObjectName(vkDevice_, VK_OBJECT_TYPE_IMAGE, (uint64_t)vkImage_, debugName)); + VK_ASSERT(lvk::setDebugObjectName(device, VK_OBJECT_TYPE_IMAGE, (uint64_t)vkImage_, debugName)); // Get physical device's properties for the image's format vkGetPhysicalDeviceFormatProperties(ctx_->getVkPhysicalDevice(), vkImageFormat_, &vkFormatProperties_); @@ -927,9 +924,9 @@ lvk::VulkanImage::~VulkanImage() { })); } else { if (mappedPtr_) { - vkUnmapMemory(vkDevice_, vkMemory_); + vkUnmapMemory(ctx_->getVkDevice(), vkMemory_); } - ctx_->deferredTask(std::packaged_task([device = vkDevice_, image = vkImage_, memory = vkMemory_]() { + ctx_->deferredTask(std::packaged_task([device = ctx_->getVkDevice(), image = vkImage_, memory = vkMemory_]() { vkDestroyImage(device, image, nullptr); if (memory != VK_NULL_HANDLE) { vkFreeMemory(device, memory, nullptr); @@ -958,8 +955,8 @@ VkImageView lvk::VulkanImage::createImageView(VkImageViewType type, .subresourceRange = {aspectMask, baseLevel, numLevels ? numLevels : numLevels_, baseLayer, numLayers}, }; VkImageView vkView = VK_NULL_HANDLE; - VK_ASSERT(vkCreateImageView(vkDevice_, &ci, nullptr, &vkView)); - VK_ASSERT(lvk::setDebugObjectName(vkDevice_, VK_OBJECT_TYPE_IMAGE_VIEW, (uint64_t)vkView, debugName)); + VK_ASSERT(vkCreateImageView(ctx_->getVkDevice(), &ci, nullptr, &vkView)); + VK_ASSERT(lvk::setDebugObjectName(ctx_->getVkDevice(), VK_OBJECT_TYPE_IMAGE_VIEW, (uint64_t)vkView, debugName)); return vkView; } diff --git a/lvk/vulkan/VulkanClasses.h b/lvk/vulkan/VulkanClasses.h index a0593b7fa9..6b1b2bd61d 100644 --- a/lvk/vulkan/VulkanClasses.h +++ b/lvk/vulkan/VulkanClasses.h @@ -31,10 +31,10 @@ struct DeviceQueues final { struct VulkanBuffer final { void bufferSubData(const VulkanContext& ctx, size_t offset, size_t size, const void* data); void getBufferSubData(const VulkanContext& ctx, size_t offset, size_t size, void* data); - inline [[nodiscard]] uint8_t* getMappedPtr() const { + [[nodiscard]] inline uint8_t* getMappedPtr() const { return static_cast(mappedPtr_); } - inline [[nodiscard]] bool isMapped() const { + [[nodiscard]] inline bool isMapped() const { return mappedPtr_ != nullptr; } void flushMappedMemory(const VulkanContext& ctx, VkDeviceSize offset, VkDeviceSize size) const; @@ -84,26 +84,26 @@ class VulkanImage final { VulkanImage& operator=(VulkanImage&&); // clang-format off - bool isSampledImage() const { return (vkUsageFlags_ & VK_IMAGE_USAGE_SAMPLED_BIT) > 0; } - bool isStorageImage() const { return (vkUsageFlags_ & VK_IMAGE_USAGE_STORAGE_BIT) > 0; } + [[nodiscard]] inline bool isSampledImage() const { return (vkUsageFlags_ & VK_IMAGE_USAGE_SAMPLED_BIT) > 0; } + [[nodiscard]] inline bool isStorageImage() const { return (vkUsageFlags_ & VK_IMAGE_USAGE_STORAGE_BIT) > 0; } // clang-format on /* * Setting `numLevels` to a non-zero value will override `mipLevels_` value from the original Vulkan image, and can be used to create * image views with different number of levels. */ - VkImageView createImageView(VkImageViewType type, - VkFormat format, - VkImageAspectFlags aspectMask, - uint32_t baseLevel, - uint32_t numLevels = VK_REMAINING_MIP_LEVELS, - uint32_t baseLayer = 0, - uint32_t numLayers = 1, - const VkComponentMapping mapping = {.r = VK_COMPONENT_SWIZZLE_IDENTITY, - .g = VK_COMPONENT_SWIZZLE_IDENTITY, - .b = VK_COMPONENT_SWIZZLE_IDENTITY, - .a = VK_COMPONENT_SWIZZLE_IDENTITY}, - const char* debugName = nullptr) const; + [[nodiscard]] VkImageView createImageView(VkImageViewType type, + VkFormat format, + VkImageAspectFlags aspectMask, + uint32_t baseLevel, + uint32_t numLevels = VK_REMAINING_MIP_LEVELS, + uint32_t baseLayer = 0, + uint32_t numLayers = 1, + const VkComponentMapping mapping = {.r = VK_COMPONENT_SWIZZLE_IDENTITY, + .g = VK_COMPONENT_SWIZZLE_IDENTITY, + .b = VK_COMPONENT_SWIZZLE_IDENTITY, + .a = VK_COMPONENT_SWIZZLE_IDENTITY}, + const char* debugName = nullptr) const; void generateMipmap(VkCommandBuffer commandBuffer) const; @@ -120,11 +120,9 @@ class VulkanImage final { public: lvk::VulkanContext* ctx_ = nullptr; - VkDevice vkDevice_ = VK_NULL_HANDLE; VkImage vkImage_ = VK_NULL_HANDLE; VkImageUsageFlags vkUsageFlags_ = 0; VkDeviceMemory vkMemory_ = VK_NULL_HANDLE; - VmaAllocationCreateInfo vmaAllocInfo_ = {}; VmaAllocation vmaAllocation_ = VK_NULL_HANDLE; VkFormatProperties vkFormatProperties_ = {}; VkExtent3D vkExtent_ = {0, 0, 0};