From 6e1e07cc07c055e5eed6ee434a7a1b4ffc766dab Mon Sep 17 00:00:00 2001 From: kearwood Date: Sat, 9 Jul 2022 23:52:35 -0700 Subject: [PATCH] Created helper function for Vulkan Buffer creation, KRDevice::createBuffer. Updated KRMeshManager to use KRDevice::createBuffer for vertex and index data. --- kraken/KRDevice.cpp | 14 ++++++++++++++ kraken/KRDevice.h | 1 + kraken/KRMeshManager.cpp | 35 ++++++++++++++++++----------------- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/kraken/KRDevice.cpp b/kraken/KRDevice.cpp index 4e4b781..d91d985 100644 --- a/kraken/KRDevice.cpp +++ b/kraken/KRDevice.cpp @@ -235,6 +235,20 @@ VmaAllocator KRDevice::getAllocator() return m_allocator; } +void KRDevice::createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer* buffer, VmaAllocation* allocation) +{ + VkBufferCreateInfo bufferInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; + bufferInfo.size = size; + bufferInfo.usage = usage; + + VmaAllocationCreateInfo allocInfo = {}; + allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; + allocInfo.requiredFlags = properties; + + VkResult res = vmaCreateBuffer(m_allocator, &bufferInfo, &allocInfo, buffer, allocation, nullptr); + // TODO - Error Handling... +} + KrResult KRDevice::selectSurfaceFormat(VkSurfaceKHR& surface, VkSurfaceFormatKHR& selectedFormat) { diff --git a/kraken/KRDevice.h b/kraken/KRDevice.h index 90bf0a3..8c5db88 100644 --- a/kraken/KRDevice.h +++ b/kraken/KRDevice.h @@ -47,6 +47,7 @@ public: bool initialize(const std::vector& deviceExtensions); VmaAllocator getAllocator(); + void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer* buffer, VmaAllocation* allocation); KrResult selectSurfaceFormat(VkSurfaceKHR& surface, VkSurfaceFormatKHR& surfaceFormat); KrResult selectDepthFormat(VkFormat& selectedDepthFormat); diff --git a/kraken/KRMeshManager.cpp b/kraken/KRMeshManager.cpp index 9be70ea..082e9a6 100755 --- a/kraken/KRMeshManager.cpp +++ b/kraken/KRMeshManager.cpp @@ -541,23 +541,19 @@ void KRMeshManager::KRVBOData::load() for (auto deviceItr = deviceManager->getDevices().begin(); deviceItr != deviceManager->getDevices().end() && iAllocation < KRENGINE_MAX_GPU_COUNT; deviceItr++, iAllocation++) { KRDevice& device = *(*deviceItr).second; KrDeviceHandle deviceHandle = (*deviceItr).first; + VmaAllocator allocator = device.getAllocator(); AllocationInfo& allocation = m_allocations[iAllocation]; - allocation.device = deviceHandle; - VmaAllocator allocator = device.getAllocator(); + device.createBuffer( + m_data->getSize(), + VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, + &allocation.vertex_buffer, + &allocation.vertex_allocation + ); - // TODO - Use staging buffers - - VkBufferCreateInfo bufferInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; - bufferInfo.size = m_data->getSize(); - bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; - - VmaAllocationCreateInfo allocInfo = {}; - allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; - allocInfo.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; - - VkResult res = vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &allocation.vertex_buffer, &allocation.vertex_allocation, nullptr); + // TODO - Use staging buffers #if KRENGINE_DEBUG_GPU_LABELS char debug_label[KRENGINE_DEBUG_GPU_LABEL_MAX_LEN]; @@ -585,7 +581,7 @@ void KRMeshManager::KRVBOData::load() debugInfo.objectHandle = (uint64_t)allocation.vertex_buffer; debugInfo.objectType = VK_OBJECT_TYPE_BUFFER; debugInfo.pObjectName = debug_label; - res = vkSetDebugUtilsObjectNameEXT(device.m_logicalDevice, &debugInfo); + VkResult res = vkSetDebugUtilsObjectNameEXT(device.m_logicalDevice, &debugInfo); #endif // KRENGINE_DEBUG_GPU_LABELS void* mappedData = nullptr; @@ -596,9 +592,14 @@ void KRMeshManager::KRVBOData::load() m_data->unlock(); if (m_index_data->getSize() > 0) { - bufferInfo.size = m_index_data->getSize(); - bufferInfo.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; - res = vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &allocation.index_buffer, &allocation.index_allocation, nullptr); + device.createBuffer( + m_index_data->getSize(), + VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, + &allocation.index_buffer, + &allocation.index_allocation + ); + #if KRENGINE_DEBUG_GPU_LABELS snprintf(debug_label, KRENGINE_DEBUG_GPU_LABEL_MAX_LEN, "%s Indexes: %s", type_label, m_debugLabel);