Restructure some files regarding resource management
This commit is contained in:
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
namespace OpenVulkano::Vulkan
|
namespace OpenVulkano::Vulkan
|
||||||
{
|
{
|
||||||
struct VulkanShader;
|
class VulkanShader;
|
||||||
|
|
||||||
class IShaderOwner
|
class IShaderOwner
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,97 +8,12 @@
|
|||||||
|
|
||||||
#define CRASH_ON_MULTIPLE_MAPPINGS_TO_SAME_ALLOCATION
|
#define CRASH_ON_MULTIPLE_MAPPINGS_TO_SAME_ALLOCATION
|
||||||
|
|
||||||
|
#include "MemoryAllocation.hpp"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vulkan/vulkan.hpp>
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include "Base/Utils.hpp"
|
|
||||||
|
|
||||||
namespace OpenVulkano::Vulkan
|
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
|
struct ManagedBuffer
|
||||||
{
|
{
|
||||||
using Ptr = std::unique_ptr<ManagedBuffer, std::function<void(ManagedBuffer*)>>;
|
using Ptr = std::unique_ptr<ManagedBuffer, std::function<void(ManagedBuffer*)>>;
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "ManagedResource.hpp"
|
#include "ManagedBuffer.hpp"
|
||||||
#include "Base/Logger.hpp"
|
#include "Base/Logger.hpp"
|
||||||
|
|
||||||
namespace OpenVulkano::Vulkan
|
namespace OpenVulkano::Vulkan
|
||||||
97
openVulkanoCpp/Vulkan/Resources/MemoryAllocation.hpp
Normal file
97
openVulkanoCpp/Vulkan/Resources/MemoryAllocation.hpp
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -5,10 +5,13 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "ResourceManager.hpp"
|
#include "ResourceManager.hpp"
|
||||||
|
#include "ManagedBuffer.hpp"
|
||||||
|
#include "MemoryAllocation.hpp"
|
||||||
#include "Scene/Vertex.hpp"
|
#include "Scene/Vertex.hpp"
|
||||||
#include "Scene/Geometry.hpp"
|
#include "Scene/Geometry.hpp"
|
||||||
#include "Scene/Material.hpp"
|
#include "Scene/Material.hpp"
|
||||||
#include "Scene/UniformBuffer.hpp"
|
#include "Scene/UniformBuffer.hpp"
|
||||||
|
#include "Scene/Camera.hpp"
|
||||||
#include "Math/ByteSize.hpp"
|
#include "Math/ByteSize.hpp"
|
||||||
#include "Vulkan/Context.hpp"
|
#include "Vulkan/Context.hpp"
|
||||||
#include "Vulkan/Image.hpp"
|
#include "Vulkan/Image.hpp"
|
||||||
@@ -91,7 +94,6 @@ namespace OpenVulkano::Vulkan
|
|||||||
nodes.clear();
|
nodes.clear();
|
||||||
device.destroyDescriptorPool(descriptorPool);
|
device.destroyDescriptorPool(descriptorPool);
|
||||||
allocations.clear();
|
allocations.clear();
|
||||||
lastAllocation = nullptr;
|
|
||||||
toFree.clear();
|
toFree.clear();
|
||||||
recycleBuffers.clear();
|
recycleBuffers.clear();
|
||||||
descriptorSetLayoutCache.clear();
|
descriptorSetLayoutCache.clear();
|
||||||
@@ -372,7 +374,6 @@ namespace OpenVulkano::Vulkan
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(!alloc && createIfAllFull) alloc = CreateMemoryAllocation(64_MiB, type, true);
|
if(!alloc && createIfAllFull) alloc = CreateMemoryAllocation(64_MiB, type, true);
|
||||||
if(alloc) lastAllocation = alloc;
|
|
||||||
return alloc;
|
return alloc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,16 +9,15 @@
|
|||||||
// Workaround for libc++
|
// Workaround for libc++
|
||||||
#define __cpp_lib_three_way_comparison 201907
|
#define __cpp_lib_three_way_comparison 201907
|
||||||
|
|
||||||
#include "vulkan/vulkan.hpp"
|
|
||||||
#include "Base/ICloseable.hpp"
|
#include "Base/ICloseable.hpp"
|
||||||
#include "IShaderOwner.hpp"
|
#include "IShaderOwner.hpp"
|
||||||
#include "ManagedResource.hpp"
|
|
||||||
#include "Vulkan/Image.hpp"
|
#include "Vulkan/Image.hpp"
|
||||||
#include "Scene/Shader/DescriptorInputDescription.hpp"
|
#include "Scene/Shader/DescriptorInputDescription.hpp"
|
||||||
#include "Scene/Camera.hpp"
|
#include <vulkan/vulkan.hpp>
|
||||||
#include <mutex>
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
namespace OpenVulkano
|
namespace OpenVulkano
|
||||||
{
|
{
|
||||||
@@ -29,6 +28,7 @@ namespace OpenVulkano
|
|||||||
class Material;
|
class Material;
|
||||||
class Texture;
|
class Texture;
|
||||||
class Shader;
|
class Shader;
|
||||||
|
class Camera;
|
||||||
class UniformBuffer;
|
class UniformBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,8 +39,11 @@ namespace OpenVulkano
|
|||||||
class VulkanTexture;
|
class VulkanTexture;
|
||||||
class VulkanCamera;
|
class VulkanCamera;
|
||||||
class VulkanNode;
|
class VulkanNode;
|
||||||
|
class VulkanShader;
|
||||||
class VulkanUniformBuffer;
|
class VulkanUniformBuffer;
|
||||||
class UniformBuffer;
|
class UniformBuffer;
|
||||||
|
class ManagedBuffer;
|
||||||
|
class MemoryAllocation;
|
||||||
|
|
||||||
class ResourceManager : public ICloseable, public IShaderOwner
|
class ResourceManager : public ICloseable, public IShaderOwner
|
||||||
{
|
{
|
||||||
@@ -57,7 +60,6 @@ namespace OpenVulkano
|
|||||||
std::vector<std::unique_ptr<VulkanShader>> shaders;
|
std::vector<std::unique_ptr<VulkanShader>> shaders;
|
||||||
std::vector<std::unique_ptr<VulkanGeometry>> geometries;
|
std::vector<std::unique_ptr<VulkanGeometry>> geometries;
|
||||||
std::vector<std::unique_ptr<VulkanNode>> nodes;
|
std::vector<std::unique_ptr<VulkanNode>> nodes;
|
||||||
MemoryAllocation* lastAllocation = nullptr;
|
|
||||||
std::mutex mutex;
|
std::mutex mutex;
|
||||||
vk::DeviceSize uniformBufferAlignment;
|
vk::DeviceSize uniformBufferAlignment;
|
||||||
std::vector<std::vector<ManagedBuffer*>> toFree;
|
std::vector<std::vector<ManagedBuffer*>> toFree;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "UniformBuffer.hpp"
|
#include "UniformBuffer.hpp"
|
||||||
#include "ManagedResource.hpp"
|
#include "ManagedBuffer.hpp"
|
||||||
#include "ResourceManager.hpp"
|
#include "ResourceManager.hpp"
|
||||||
#include "Vulkan/VulkanDrawContext.hpp"
|
#include "Vulkan/VulkanDrawContext.hpp"
|
||||||
#include "Vulkan/Scene/VulkanShader.hpp"
|
#include "Vulkan/Scene/VulkanShader.hpp"
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
#include "IRecordable.hpp"
|
#include "IRecordable.hpp"
|
||||||
#include "Scene/Scene.hpp"
|
#include "Scene/Scene.hpp"
|
||||||
#include "Scene/Geometry.hpp"
|
#include "Scene/Geometry.hpp"
|
||||||
#include "Vulkan/Resources/ManagedResource.hpp"
|
#include "Vulkan/Resources/ManagedBuffer.hpp"
|
||||||
|
|
||||||
namespace OpenVulkano::Vulkan
|
namespace OpenVulkano::Vulkan
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -23,8 +23,9 @@ namespace OpenVulkano
|
|||||||
class Context;
|
class Context;
|
||||||
class IShaderOwner;
|
class IShaderOwner;
|
||||||
|
|
||||||
struct VulkanShader final : public ICloseable, public IRecordable
|
class VulkanShader final : public ICloseable, public IRecordable
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
Scene::Shader* shader = nullptr;
|
Scene::Shader* shader = nullptr;
|
||||||
vk::Device device;
|
vk::Device device;
|
||||||
std::vector<vk::ShaderModule> shaderModules; // TODO manage live time somewhere else to allow sharing of shader programs
|
std::vector<vk::ShaderModule> shaderModules; // TODO manage live time somewhere else to allow sharing of shader programs
|
||||||
|
|||||||
Reference in New Issue
Block a user