WIP Index and Vertex buffer allocation

This commit is contained in:
2022-01-22 01:01:08 -08:00
parent 0330d5b4fe
commit ad596a82f9
7 changed files with 57 additions and 1 deletions

View File

@@ -228,3 +228,9 @@ bool KRDevice::initialize(const std::vector<const char*>& deviceExtensions)
return true;
}
VmaAllocator KRDevice::getAllocator()
{
assert(m_allocator != VK_NULL_HANDLE);
return m_allocator;
}

View File

@@ -47,6 +47,8 @@ public:
void destroy();
bool initialize(const std::vector<const char*>& deviceExtensions);
VmaAllocator getAllocator();
VkPhysicalDevice m_device;
VkDevice m_logicalDevice;
VkPhysicalDeviceProperties m_deviceProperties;

View File

@@ -213,3 +213,8 @@ KrSurfaceHandle KRDeviceManager::getBestDeviceForSurface(const VkSurfaceKHR& sur
}
return deviceHandle;
}
unordered_map<KrDeviceHandle, std::unique_ptr<KRDevice>>& KRDeviceManager::getDevices()
{
return m_devices;
}

View File

@@ -50,6 +50,7 @@ public:
KRDevice& getDeviceInfo(KrDeviceHandle handle);
VkInstance& getVulkanInstance();
KrSurfaceHandle getBestDeviceForSurface(const VkSurfaceKHR& surface);
unordered_map<KrDeviceHandle, std::unique_ptr<KRDevice>>& getDevices();
private:
unordered_map<KrDeviceHandle, std::unique_ptr<KRDevice>> m_devices;

View File

@@ -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;

View File

@@ -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...
/*

View File

@@ -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);