From 152f7a2d28a816d83e37828e3dc068785844cdbf Mon Sep 17 00:00:00 2001 From: Kearwood Gilbert Date: Thu, 8 Jun 2023 23:08:42 -0700 Subject: [PATCH] Move KRDevice::createImage to KRTexture::allocate --- kraken/KRDevice.cpp | 42 ---------------------------------------- kraken/KRDevice.h | 10 ++-------- kraken/KRTexture.cpp | 40 ++++++++++++++++++++++++++++++++++++++ kraken/KRTexture.h | 7 +++++++ kraken/KRTexture2D.cpp | 2 +- kraken/KRTextureCube.cpp | 2 +- 6 files changed, 51 insertions(+), 52 deletions(-) diff --git a/kraken/KRDevice.cpp b/kraken/KRDevice.cpp index bfbd4d7..b24346a 100644 --- a/kraken/KRDevice.cpp +++ b/kraken/KRDevice.cpp @@ -591,48 +591,6 @@ void KRDevice::setDebugLabel(const VkDevice& device, const char* debugLabel) #endif // KRENGINE_DEBUG_GPU_LABELS - -bool KRDevice::createImage(Vector2i dimensions, VkImageCreateFlags imageCreateFlags, VkMemoryPropertyFlags properties, VkImage* image, VmaAllocation* allocation -#if KRENGINE_DEBUG_GPU_LABELS - , const char* debug_label -#endif -) -{ - VkImageCreateInfo imageInfo{}; - imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; - imageInfo.imageType = VK_IMAGE_TYPE_2D; - imageInfo.extent.width = static_cast(dimensions.x); - imageInfo.extent.height = static_cast(dimensions.y); - imageInfo.extent.depth = 1; - imageInfo.mipLevels = 1; - imageInfo.arrayLayers = 1; - imageInfo.format = VK_FORMAT_R8G8B8A8_SRGB; - imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL; - imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; - imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; - imageInfo.samples = VK_SAMPLE_COUNT_1_BIT; - imageInfo.flags = imageCreateFlags; - - - uint32_t queueFamilyIndices[2] = {}; - imageInfo.pQueueFamilyIndices = queueFamilyIndices; - imageInfo.queueFamilyIndexCount = 0; - getQueueFamiliesForSharing(queueFamilyIndices, &imageInfo.queueFamilyIndexCount, &imageInfo.sharingMode); - - VmaAllocationCreateInfo allocInfo = {}; - allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; - allocInfo.requiredFlags = properties; - - VkResult res = vmaCreateImage(m_allocator, &imageInfo, &allocInfo, image, allocation, nullptr); - if (res != VK_SUCCESS) { - return false; - } -#if KRENGINE_DEBUG_GPU_LABELS - setDebugLabel(*image, debug_label); -#endif - return true; -} - bool KRDevice::createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer* buffer, VmaAllocation* allocation #if KRENGINE_DEBUG_GPU_LABELS , const char* debug_label diff --git a/kraken/KRDevice.h b/kraken/KRDevice.h index 057ac4c..ca658f2 100644 --- a/kraken/KRDevice.h +++ b/kraken/KRDevice.h @@ -64,12 +64,6 @@ public: #endif ); - bool createImage(Vector2i dimensions, VkImageCreateFlags imageCreateFlags, VkMemoryPropertyFlags properties, VkImage* image, VmaAllocation* allocation -#if KRENGINE_DEBUG_GPU_LABELS - , const char* debug_label -#endif - ); - KrResult selectSurfaceFormat(VkSurfaceKHR& surface, VkSurfaceFormatKHR& surfaceFormat) const; KrResult selectDepthFormat(VkFormat& selectedDepthFormat) const; KrResult selectPresentMode(VkSurfaceKHR& surface, VkPresentModeKHR& selectedPresentMode) const; @@ -126,10 +120,10 @@ public: // This will be used for uploading assets procedurally generated while recording the graphics command buffer. // TODO - We should allocate at least two of these and double-buffer for increased CPU-GPU concurrency StagingBufferInfo m_graphicsStagingBuffer; -private: - void checkFlushStreamBuffer(size_t size); void getQueueFamiliesForSharing(uint32_t* queueFamilyIndices, uint32_t* familyCount, VkSharingMode* sharingMode); +private: + void checkFlushStreamBuffer(size_t size); // Initialization helper functions bool getAndCheckDeviceCapabilities(const std::vector& deviceExtensions); diff --git a/kraken/KRTexture.cpp b/kraken/KRTexture.cpp index a624dc0..2a9a67a 100755 --- a/kraken/KRTexture.cpp +++ b/kraken/KRTexture.cpp @@ -280,3 +280,43 @@ VkImage KRTexture::getImage(KrDeviceHandle device) return VK_NULL_HANDLE; } +bool KRTexture::allocate(KRDevice& device, Vector2i dimensions, VkImageCreateFlags imageCreateFlags, VkMemoryPropertyFlags properties, VkImage* image, VmaAllocation* allocation +#if KRENGINE_DEBUG_GPU_LABELS +, const char* debug_label +#endif +) +{ + VkImageCreateInfo imageInfo{}; + imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; + imageInfo.imageType = VK_IMAGE_TYPE_2D; + imageInfo.extent.width = static_cast(dimensions.x); + imageInfo.extent.height = static_cast(dimensions.y); + imageInfo.extent.depth = 1; + imageInfo.mipLevels = 1; + imageInfo.arrayLayers = 1; + imageInfo.format = VK_FORMAT_R8G8B8A8_SRGB; + imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL; + imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; + imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; + imageInfo.samples = VK_SAMPLE_COUNT_1_BIT; + imageInfo.flags = imageCreateFlags; + + + uint32_t queueFamilyIndices[2] = {}; + imageInfo.pQueueFamilyIndices = queueFamilyIndices; + imageInfo.queueFamilyIndexCount = 0; + device.getQueueFamiliesForSharing(queueFamilyIndices, &imageInfo.queueFamilyIndexCount, &imageInfo.sharingMode); + + VmaAllocationCreateInfo allocInfo = {}; + allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; + allocInfo.requiredFlags = properties; + + VkResult res = vmaCreateImage(device.getAllocator(), &imageInfo, &allocInfo, image, allocation, nullptr); + if (res != VK_SUCCESS) { + return false; + } +#if KRENGINE_DEBUG_GPU_LABELS + device.setDebugLabel(*image, debug_label); +#endif + return true; +} diff --git a/kraken/KRTexture.h b/kraken/KRTexture.h index 9e32b9b..7ce46ac 100755 --- a/kraken/KRTexture.h +++ b/kraken/KRTexture.h @@ -38,6 +38,7 @@ class KRDataBlock; class KRCamera; class KRDeviceManager; +class KRDevice; class KRTexture : public KRResource { @@ -125,6 +126,12 @@ protected: float m_last_frame_max_lod_coverage; texture_usage_t m_last_frame_usage; + bool allocate(KRDevice& device, Vector2i dimensions, VkImageCreateFlags imageCreateFlags, VkMemoryPropertyFlags properties, VkImage* image, VmaAllocation* allocation +#if KRENGINE_DEBUG_GPU_LABELS + , const char* debug_label +#endif + ); + private: std::atomic m_textureMemUsed; std::atomic m_newTextureMemUsed; diff --git a/kraken/KRTexture2D.cpp b/kraken/KRTexture2D.cpp index 2bb38fa..9805cd1 100755 --- a/kraken/KRTexture2D.cpp +++ b/kraken/KRTexture2D.cpp @@ -64,7 +64,7 @@ bool KRTexture2D::createGPUTexture(int lod_max_dim) texture.allocation = VK_NULL_HANDLE; texture.image = VK_NULL_HANDLE; - if (!device.createImage(getDimensions(), 0, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &texture.image, &texture.allocation + if (!allocate(device, getDimensions(), 0, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &texture.image, &texture.allocation #if KRENGINE_DEBUG_GPU_LABELS , getName().c_str() #endif diff --git a/kraken/KRTextureCube.cpp b/kraken/KRTextureCube.cpp index a900c0d..93b6f93 100755 --- a/kraken/KRTextureCube.cpp +++ b/kraken/KRTextureCube.cpp @@ -101,7 +101,7 @@ bool KRTextureCube::createGPUTexture(int lod_max_dim) texture.allocation = VK_NULL_HANDLE; texture.image = VK_NULL_HANDLE; - if (!device.createImage(dimensions, VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &texture.image, &texture.allocation + if (!allocate(device, dimensions, VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &texture.image, &texture.allocation #if KRENGINE_DEBUG_GPU_LABELS , getName().c_str() #endif