Skip to content

Commit

Permalink
Don't store VkDevice and VmaAllocationCreateInfo in VulkanImage
Browse files Browse the repository at this point in the history
  • Loading branch information
corporateshark committed Apr 8, 2024
1 parent c5961fa commit e3482e9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 36 deletions.
33 changes: 15 additions & 18 deletions lvk/vulkan/VulkanClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
Expand All @@ -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_);
Expand All @@ -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),
Expand All @@ -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,
Expand All @@ -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),
Expand Down Expand Up @@ -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_);
Expand All @@ -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_);
Expand All @@ -927,9 +924,9 @@ lvk::VulkanImage::~VulkanImage() {
}));
} else {
if (mappedPtr_) {
vkUnmapMemory(vkDevice_, vkMemory_);
vkUnmapMemory(ctx_->getVkDevice(), vkMemory_);
}
ctx_->deferredTask(std::packaged_task<void()>([device = vkDevice_, image = vkImage_, memory = vkMemory_]() {
ctx_->deferredTask(std::packaged_task<void()>([device = ctx_->getVkDevice(), image = vkImage_, memory = vkMemory_]() {
vkDestroyImage(device, image, nullptr);
if (memory != VK_NULL_HANDLE) {
vkFreeMemory(device, memory, nullptr);
Expand Down Expand Up @@ -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;
}
Expand Down
34 changes: 16 additions & 18 deletions lvk/vulkan/VulkanClasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t*>(mappedPtr_);
}
inline [[nodiscard]] bool isMapped() const {
[[nodiscard]] inline bool isMapped() const {
return mappedPtr_ != nullptr;
}
void flushMappedMemory(const VulkanContext& ctx, VkDeviceSize offset, VkDeviceSize size) const;
Expand Down Expand Up @@ -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;

Expand All @@ -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};
Expand Down

0 comments on commit e3482e9

Please sign in to comment.