Fix memory alignment on Nvidia cards

This commit is contained in:
Georg Hagen
2024-05-28 22:20:02 +02:00
parent 7dd16c66b9
commit bc0e117d83
3 changed files with 13 additions and 7 deletions

View File

@@ -11,6 +11,7 @@
#include <memory>
#include <vulkan/vulkan.hpp>
#include <functional>
#include "Base/Utils.hpp"
namespace OpenVulkano::Vulkan
{
@@ -44,6 +45,11 @@ namespace OpenVulkano::Vulkan
return size - used;
}
[[nodiscard]] size_t FreeSpace(size_t alignment) const
{
return size - Utils::Align(used, alignment);
}
void HandleChildMappingValidation() const;
void* Map()

View File

@@ -309,11 +309,11 @@ namespace OpenVulkano::Vulkan
vk::Buffer buffer = device.createBuffer(bufferCreateInfo);
const vk::MemoryRequirements memoryRequirements = device.getBufferMemoryRequirements(buffer);
uint32_t memtype = context->device->GetMemoryType(memoryRequirements.memoryTypeBits, properties);
if (memoryRequirements.size != size) Logger::DATA->warn("Memory Requirement Size ({0}) != Size ({1})", memoryRequirements.size, size);
MemoryAllocation* allocation = GetFreeMemoryAllocation(memoryRequirements.size, memtype);
uint32_t offset = allocation->used;
if (memoryRequirements.size != size) Logger::DATA->debug("Memory Requirement Size ({0}) != Size ({1})", memoryRequirements.size, size);
MemoryAllocation* allocation = GetFreeMemoryAllocation(memoryRequirements.size, memoryRequirements.alignment, memtype);
uint32_t offset = Utils::Align(allocation->used, memoryRequirements.alignment);
device.bindBufferMemory(buffer, allocation->memory, offset);
allocation->used += memoryRequirements.size;
allocation->used += memoryRequirements.size + (offset - allocation->used);
return new ManagedBuffer{ allocation, offset, size, buffer, usage, properties, nullptr };
}
@@ -326,12 +326,12 @@ namespace OpenVulkano::Vulkan
return alloc;
}
MemoryAllocation* ResourceManager::GetFreeMemoryAllocation(size_t size, uint32_t type, bool createIfAllFull)
MemoryAllocation* ResourceManager::GetFreeMemoryAllocation(size_t size, vk::DeviceSize alignment, uint32_t type, bool createIfAllFull)
{
MemoryAllocation* alloc = nullptr;
for (auto& allocation : allocations)
{
if (allocation->type == type && allocation->FreeSpace() >= size)
if (allocation->type == type && allocation->FreeSpace(alignment) >= size)
{
alloc = allocation.get();
break;

View File

@@ -117,7 +117,7 @@ namespace OpenVulkano
MemoryAllocation* CreateMemoryAllocation(size_t size, uint32_t type, bool addToCache = true);
MemoryAllocation* GetFreeMemoryAllocation(size_t size, uint32_t type, bool createIfAllFull = true);
MemoryAllocation* GetFreeMemoryAllocation(size_t size, size_t alignment, uint32_t type, bool createIfAllFull = true);
vk::DescriptorSetLayout* GetDescriptorLayoutSet(const DescriptorSetLayoutBinding& descriptorSetLayoutBinding);