Restructure some files regarding resource management

This commit is contained in:
Georg Hagen
2024-07-10 16:11:36 +02:00
parent 5c4e972722
commit ba7f0e6d62
9 changed files with 114 additions and 98 deletions

View File

@@ -8,7 +8,7 @@
namespace OpenVulkano::Vulkan
{
struct VulkanShader;
class VulkanShader;
class IShaderOwner
{

View File

@@ -8,97 +8,12 @@
#define CRASH_ON_MULTIPLE_MAPPINGS_TO_SAME_ALLOCATION
#include "MemoryAllocation.hpp"
#include <memory>
#include <vulkan/vulkan.hpp>
#include <functional>
#include "Base/Utils.hpp"
namespace OpenVulkano::Vulkan
{
struct MemoryAllocation
{
vk::DeviceMemory memory;
size_t used;
const size_t size;
const uint32_t type;
const vk::Device device;
void* mapped;
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)
{
}
~MemoryAllocation()
{
if (device && memory)
device.free(memory);
}
[[nodiscard]] size_t FreeSpace() const
{
return size - used;
}
[[nodiscard]] size_t FreeSpace(size_t alignment) const
{
return size - Utils::Align(used, alignment);
}
void HandleChildMappingValidation() const;
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);
}
}
}
[[nodiscard]] 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
{
using Ptr = std::unique_ptr<ManagedBuffer, std::function<void(ManagedBuffer*)>>;

View File

@@ -4,7 +4,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "ManagedResource.hpp"
#include "ManagedBuffer.hpp"
#include "Base/Logger.hpp"
namespace OpenVulkano::Vulkan

View File

@@ -0,0 +1,97 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "Base/Utils.hpp"
#include <vulkan/vulkan.hpp>
namespace OpenVulkano::Vulkan
{
struct MemoryAllocation
{
vk::DeviceMemory memory;
size_t used;
const size_t size;
const uint32_t type;
const vk::Device device;
void* mapped;
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)
{
}
~MemoryAllocation()
{
if (device && memory)
device.free(memory);
}
[[nodiscard]] size_t FreeSpace() const
{
return size - used;
}
[[nodiscard]] size_t FreeSpace(size_t alignment) const
{
return size - Utils::Align(used, alignment);
}
void HandleChildMappingValidation() const;
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);
}
}
}
[[nodiscard]] 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);
}
}
};
}

View File

@@ -5,10 +5,13 @@
*/
#include "ResourceManager.hpp"
#include "ManagedBuffer.hpp"
#include "MemoryAllocation.hpp"
#include "Scene/Vertex.hpp"
#include "Scene/Geometry.hpp"
#include "Scene/Material.hpp"
#include "Scene/UniformBuffer.hpp"
#include "Scene/Camera.hpp"
#include "Math/ByteSize.hpp"
#include "Vulkan/Context.hpp"
#include "Vulkan/Image.hpp"
@@ -91,7 +94,6 @@ namespace OpenVulkano::Vulkan
nodes.clear();
device.destroyDescriptorPool(descriptorPool);
allocations.clear();
lastAllocation = nullptr;
toFree.clear();
recycleBuffers.clear();
descriptorSetLayoutCache.clear();
@@ -372,7 +374,6 @@ namespace OpenVulkano::Vulkan
}
}
if(!alloc && createIfAllFull) alloc = CreateMemoryAllocation(64_MiB, type, true);
if(alloc) lastAllocation = alloc;
return alloc;
}

View File

@@ -9,16 +9,15 @@
// Workaround for libc++
#define __cpp_lib_three_way_comparison 201907
#include "vulkan/vulkan.hpp"
#include "Base/ICloseable.hpp"
#include "IShaderOwner.hpp"
#include "ManagedResource.hpp"
#include "Vulkan/Image.hpp"
#include "Scene/Shader/DescriptorInputDescription.hpp"
#include "Scene/Camera.hpp"
#include <mutex>
#include <vulkan/vulkan.hpp>
#include <functional>
#include <map>
#include <memory>
#include <mutex>
namespace OpenVulkano
{
@@ -29,6 +28,7 @@ namespace OpenVulkano
class Material;
class Texture;
class Shader;
class Camera;
class UniformBuffer;
}
@@ -39,8 +39,11 @@ namespace OpenVulkano
class VulkanTexture;
class VulkanCamera;
class VulkanNode;
class VulkanShader;
class VulkanUniformBuffer;
class UniformBuffer;
class ManagedBuffer;
class MemoryAllocation;
class ResourceManager : public ICloseable, public IShaderOwner
{
@@ -57,7 +60,6 @@ namespace OpenVulkano
std::vector<std::unique_ptr<VulkanShader>> shaders;
std::vector<std::unique_ptr<VulkanGeometry>> geometries;
std::vector<std::unique_ptr<VulkanNode>> nodes;
MemoryAllocation* lastAllocation = nullptr;
std::mutex mutex;
vk::DeviceSize uniformBufferAlignment;
std::vector<std::vector<ManagedBuffer*>> toFree;

View File

@@ -5,7 +5,7 @@
*/
#include "UniformBuffer.hpp"
#include "ManagedResource.hpp"
#include "ManagedBuffer.hpp"
#include "ResourceManager.hpp"
#include "Vulkan/VulkanDrawContext.hpp"
#include "Vulkan/Scene/VulkanShader.hpp"

View File

@@ -9,7 +9,7 @@
#include "IRecordable.hpp"
#include "Scene/Scene.hpp"
#include "Scene/Geometry.hpp"
#include "Vulkan/Resources/ManagedResource.hpp"
#include "Vulkan/Resources/ManagedBuffer.hpp"
namespace OpenVulkano::Vulkan
{

View File

@@ -23,8 +23,9 @@ namespace OpenVulkano
class Context;
class IShaderOwner;
struct VulkanShader final : public ICloseable, public IRecordable
class VulkanShader final : public ICloseable, public IRecordable
{
public:
Scene::Shader* shader = nullptr;
vk::Device device;
std::vector<vk::ShaderModule> shaderModules; // TODO manage live time somewhere else to allow sharing of shader programs