diff --git a/kraken/KRDevice.cpp b/kraken/KRDevice.cpp index 416481c..925f999 100644 --- a/kraken/KRDevice.cpp +++ b/kraken/KRDevice.cpp @@ -227,4 +227,10 @@ bool KRDevice::initialize(const std::vector& deviceExtensions) } return true; +} + +VmaAllocator KRDevice::getAllocator() +{ + assert(m_allocator != VK_NULL_HANDLE); + return m_allocator; } \ No newline at end of file diff --git a/kraken/KRDevice.h b/kraken/KRDevice.h index 3f1c3b1..a4d1d95 100644 --- a/kraken/KRDevice.h +++ b/kraken/KRDevice.h @@ -47,6 +47,8 @@ public: void destroy(); bool initialize(const std::vector& deviceExtensions); + VmaAllocator getAllocator(); + VkPhysicalDevice m_device; VkDevice m_logicalDevice; VkPhysicalDeviceProperties m_deviceProperties; diff --git a/kraken/KRDeviceManager.cpp b/kraken/KRDeviceManager.cpp index 29c5eff..16a6822 100644 --- a/kraken/KRDeviceManager.cpp +++ b/kraken/KRDeviceManager.cpp @@ -212,4 +212,9 @@ KrSurfaceHandle KRDeviceManager::getBestDeviceForSurface(const VkSurfaceKHR& sur } } return deviceHandle; -} \ No newline at end of file +} + +unordered_map>& KRDeviceManager::getDevices() +{ + return m_devices; +} diff --git a/kraken/KRDeviceManager.h b/kraken/KRDeviceManager.h index 908bdbf..6358f97 100644 --- a/kraken/KRDeviceManager.h +++ b/kraken/KRDeviceManager.h @@ -50,6 +50,7 @@ public: KRDevice& getDeviceInfo(KrDeviceHandle handle); VkInstance& getVulkanInstance(); KrSurfaceHandle getBestDeviceForSurface(const VkSurfaceKHR& surface); + unordered_map>& getDevices(); private: unordered_map> m_devices; diff --git a/kraken/KREngine-common.h b/kraken/KREngine-common.h index 98ef571..4fff255 100755 --- a/kraken/KREngine-common.h +++ b/kraken/KREngine-common.h @@ -33,6 +33,8 @@ #ifndef KRENGINE_COMMON_H #define KRENGINE_COMMON_H +#define KRENGINE_MAX_GPU_COUNT 4 + #include "public/kraken.h" #include "KRHelpers.h" using namespace kraken; diff --git a/kraken/KRMeshManager.cpp b/kraken/KRMeshManager.cpp index ee41bc8..b4ec010 100755 --- a/kraken/KRMeshManager.cpp +++ b/kraken/KRMeshManager.cpp @@ -534,10 +534,13 @@ KRMeshManager::KRVBOData::KRVBOData() m_last_frame_used = 0; m_last_frame_max_lod_coverage = 0.0f; + + memset(m_allocations, 0, sizeof(AllocationInfo) * KRENGINE_MAX_GPU_COUNT); } KRMeshManager::KRVBOData::KRVBOData(KRMeshManager *manager, KRDataBlock &data, KRDataBlock &index_data, int vertex_attrib_flags, bool static_vbo, vbo_type t) { + memset(m_allocations, 0, sizeof(AllocationInfo) * KRENGINE_MAX_GPU_COUNT); m_is_vbo_loaded = false; m_is_vbo_ready = false; init(manager, data,index_data,vertex_attrib_flags, static_vbo, t); @@ -573,11 +576,38 @@ KRMeshManager::KRVBOData::~KRVBOData() void KRMeshManager::KRVBOData::load() { + // TODO - We should load on each GPU only if there is a surface using the mesh if(isVBOLoaded()) { return; } + KRDeviceManager* deviceManager = m_manager->getContext().getDeviceManager(); + int iAllocation = 0; + + 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; + AllocationInfo& allocation = m_allocations[iAllocation]; + allocation.device = deviceHandle; + + VmaAllocator allocator = device.getAllocator(); + + 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; + + VkResult res = vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &allocation.vertex_buffer, &allocation.vertex_allocation, nullptr); + + 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); + } + } // TODO - Replace OpenGL code below... /* diff --git a/kraken/KRMeshManager.h b/kraken/KRMeshManager.h index 949cbc7..7fa70ad 100755 --- a/kraken/KRMeshManager.h +++ b/kraken/KRMeshManager.h @@ -118,6 +118,16 @@ public: bool m_static_vbo; bool m_is_vbo_loaded; bool m_is_vbo_ready; + + typedef struct { + KrDeviceHandle device; + VkBuffer vertex_buffer; + VmaAllocation vertex_allocation; + VkBuffer index_buffer; + VmaAllocation index_allocation; + } AllocationInfo; + + AllocationInfo m_allocations[KRENGINE_MAX_GPU_COUNT]; }; void bindVBO(KRVBOData *vbo_data, float lodCoverage);