Fix linux build

This commit is contained in:
2020-05-24 23:14:11 +02:00
parent 7ac08ebc2c
commit c0a234ab06
11 changed files with 215 additions and 42 deletions

View File

@@ -10,6 +10,8 @@ namespace openVulkanoCpp
class IShaderOwner
{
public:
virtual ~IShaderOwner() = default;
virtual void RemoveShader(VulkanShader* shader) = 0;
};
}

View File

@@ -1,4 +1,7 @@
#pragma once
#define CRASH_ON_MULTIPLE_MAPPINGS_TO_SAME_ALLOCATION
#include <vulkan/vulkan.hpp>
namespace openVulkanoCpp
@@ -8,21 +11,83 @@ namespace openVulkanoCpp
struct MemoryAllocation
{
vk::DeviceMemory memory;
size_t used, size;
uint32_t type;
size_t used;
const size_t size;
const uint32_t type;
const vk::Device device;
void* mapped;
MemoryAllocation(size_t size, uint32_t type)
private:
uint32_t mappedCount;
static constexpr uint32_t CHILD_MAPPED_FLAG = 1u << 31;
public:
MemoryAllocation(size_t size, uint32_t type, vk::Device device):
memory(nullptr), used(0), size(size), type(type), device(device), mapped(nullptr), mappedCount(0)
{
memory = nullptr;
this->size = size;
used = 0;
this->type = type;
}
size_t FreeSpace() const
{
return size - used;
}
void HandleChildMappingValidation() const
{
if (IsChildMapped())
{
Logger::RENDER->error("A single memory allocation should only be mapped once! Mapping a single allocation multiple times might not work or might not work reliable with all driver implementations.");
#ifdef CRASH_ON_MULTIPLE_MAPPINGS_TO_SAME_ALLOCATION
throw std::runtime_error("A single memory allocation should only be mapped once!");
#endif
}
}
void* Map()
{
HandleChildMappingValidation();
if (!mapped)
{
mapped = device.mapMemory(memory, 0, size, vk::MemoryMapFlags());
}
mappedCount++;
return mapped;
}
void UnMap()
{
if (mappedCount > 0)
{
mappedCount--;
if (mappedCount == 0)
{
device.unmapMemory(memory);
}
}
}
bool IsChildMapped() const
{
return mappedCount & CHILD_MAPPED_FLAG;
}
void* MapChild(size_t offset, vk::DeviceSize size)
{
HandleChildMappingValidation();
mappedCount |= CHILD_MAPPED_FLAG;
mappedCount++;
return device.mapMemory(memory, offset, size, vk::MemoryMapFlags());
}
void UnMapChild()
{
mappedCount &= ~CHILD_MAPPED_FLAG;
mappedCount--;
if (mappedCount == 0)
{
device.unmapMemory(memory);
}
}
};
struct ManagedBuffer
@@ -32,27 +97,43 @@ namespace openVulkanoCpp
vk::Buffer buffer;
vk::BufferUsageFlags usage;
vk::MemoryPropertyFlags properties;
vk::Device device;
void* mapped = nullptr;
bool IsLast()
bool IsLast() const
{
return (offset + size == allocation->used);
}
bool IsMapped() const
{
return mapped || allocation->mapped;
}
/**
* \brief Maps the buffer into the memory of the host.
* \tparam T The type of the buffers data.
* \param offset The offset from where to map the buffer.
* \param size The size to be mapped. VK_WHOLE_SIZE to map the whole buffer.
* \pparam longTermMapping If the mapping is intended to be held long term. Short term mappings must be freed before mapping a different region in the same memory allocation for them to work reliable with all drivers.
* \return The pointer to the mapped buffer.
*/
template <typename T = void>
T* Map(size_t offset = 0, vk::DeviceSize size = VK_WHOLE_SIZE)
T* Map(size_t offset = 0, vk::DeviceSize size = VK_WHOLE_SIZE, bool longTermMapping = true)
{
if (size == VK_WHOLE_SIZE) size = this->size;
mapped = device.mapMemory(allocation->memory, this->offset + offset, size, vk::MemoryMapFlags());
return static_cast<T*>(mapped);
if (!mapped)
{
if (allocation->mapped || longTermMapping)
{
mapped = static_cast<uint8_t*>(allocation->Map()) + offset + this->offset;
}
else
{
if (size == VK_WHOLE_SIZE) size = this->size;
mapped = allocation->MapChild(this->offset + offset, size);
}
return static_cast<T*>(mapped);
}
}
/**
@@ -60,32 +141,42 @@ namespace openVulkanoCpp
*/
void UnMap()
{
device.unmapMemory(allocation->memory);
mapped = nullptr;
if (mapped)
{
if (allocation->mapped)
{
allocation->UnMap();
}
else
{
allocation->UnMapChild();
}
mapped = nullptr;
}
}
void Copy(void* data) const
void Copy(void* data)
{
if(mapped)
if (mapped)
{
memcpy(mapped, data, size);
}
else
{
void* dataMapped = device.mapMemory(allocation->memory, offset, size);
void* dataMapped = Map(0, VK_WHOLE_SIZE, false);
memcpy(dataMapped, data, size);
device.unmapMemory(allocation->memory);
UnMap();
}
}
void Copy(void* data, uint32_t size, uint32_t offset) const
void Copy(void* data, uint32_t size, uint32_t offset)
{
if(mapped) memcpy(static_cast<char*>(mapped) + offset, data, size);
else
{
void* dataMapped = device.mapMemory(allocation->memory, this->offset + offset, size);
void* dataMapped = Map(offset, size, false);
memcpy(dataMapped, data, size);
device.unmapMemory(allocation->memory);
UnMap();
}
}
};

View File

@@ -217,12 +217,12 @@ namespace openVulkanoCpp
uint32_t offset = allocation->used;
device.bindBufferMemory(buffer, allocation->memory, offset);
allocation->used += memoryRequirements.size;
return new ManagedBuffer{ allocation, offset, size, buffer, usage, properties, device, nullptr };
return new ManagedBuffer{ allocation, offset, size, buffer, usage, properties, nullptr };
}
MemoryAllocation* CreateMemoryAllocation(size_t size, uint32_t type, bool addToCache = true)
{
MemoryAllocation* alloc = new MemoryAllocation(size, type);
MemoryAllocation* alloc = new MemoryAllocation(size, type, device);
const vk::MemoryAllocateInfo allocInfo = { size, type };
alloc->memory = device.allocateMemory(allocInfo);
if (addToCache) allocations.push_back(alloc);

View File

@@ -22,15 +22,15 @@ namespace openVulkanoCpp
this->allocSizeFrame = allocSizeFrame;
vk::DescriptorPoolSize poolSize = { vk::DescriptorType::eUniformBufferDynamic, 1 };
const vk::DescriptorPoolCreateInfo poolCreateInfo = { {}, 1, 1, &poolSize };
descPool = buffer->device.createDescriptorPool(poolCreateInfo);
descPool = buffer->allocation->device.createDescriptorPool(poolCreateInfo);
const vk::DescriptorSetAllocateInfo descSetAllocInfo = { descPool, 1, descriptorSetLayout };
descSet = buffer->device.allocateDescriptorSets(descSetAllocInfo)[0];
descSet = buffer->allocation->device.allocateDescriptorSets(descSetAllocInfo)[0];
vk::DescriptorBufferInfo bufferInfo = { buffer->buffer, 0, allocSizeFrame };
vk::WriteDescriptorSet writeDescriptorSet = { descSet };
writeDescriptorSet.descriptorCount = 1;
writeDescriptorSet.descriptorType = vk::DescriptorType::eUniformBufferDynamic;
writeDescriptorSet.pBufferInfo = &bufferInfo;
buffer->device.updateDescriptorSets(1, &writeDescriptorSet, 0, nullptr);
buffer->allocation->device.updateDescriptorSets(1, &writeDescriptorSet, 0, nullptr);
}
void Record(vk::CommandBuffer& cmdBuffer, uint32_t bufferId) override
@@ -47,7 +47,7 @@ namespace openVulkanoCpp
void Close() override
{
buffer->device.destroyDescriptorPool(descPool);
buffer->allocation->device.destroyDescriptorPool(descPool);
}
};
}