mirror of
https://git.h3cjp.net/H3cJP/citra.git
synced 2024-11-24 09:22:59 +00:00
vulkan_memory_allocator: Add allocation support for download types
Implements the allocator logic to handle download memory types. This will try to use HOST_CACHED_BIT when available.
This commit is contained in:
parent
72541af3bc
commit
8f22f5470c
|
@ -5,7 +5,6 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <bit>
|
#include <bit>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <tuple>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "common/alignment.h"
|
#include "common/alignment.h"
|
||||||
|
@ -27,7 +26,7 @@ struct Range {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
[[nodiscard]] u64 GetAllocationChunkSize(u64 required_size) {
|
[[nodiscard]] u64 AllocationChunkSize(u64 required_size) {
|
||||||
static constexpr std::array sizes{
|
static constexpr std::array sizes{
|
||||||
0x1000ULL << 10, 0x1400ULL << 10, 0x1800ULL << 10, 0x1c00ULL << 10, 0x2000ULL << 10,
|
0x1000ULL << 10, 0x1400ULL << 10, 0x1800ULL << 10, 0x1c00ULL << 10, 0x2000ULL << 10,
|
||||||
0x3200ULL << 10, 0x4000ULL << 10, 0x6000ULL << 10, 0x8000ULL << 10, 0xA000ULL << 10,
|
0x3200ULL << 10, 0x4000ULL << 10, 0x6000ULL << 10, 0x8000ULL << 10, 0xA000ULL << 10,
|
||||||
|
@ -38,14 +37,28 @@ struct Range {
|
||||||
const auto it = std::ranges::lower_bound(sizes, required_size);
|
const auto it = std::ranges::lower_bound(sizes, required_size);
|
||||||
return it != sizes.end() ? *it : Common::AlignUp(required_size, 4ULL << 20);
|
return it != sizes.end() ? *it : Common::AlignUp(required_size, 4ULL << 20);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] VkMemoryPropertyFlags MemoryUsagePropertyFlags(MemoryUsage usage) {
|
||||||
|
switch (usage) {
|
||||||
|
case MemoryUsage::DeviceLocal:
|
||||||
|
return VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
|
||||||
|
case MemoryUsage::Upload:
|
||||||
|
return VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
|
||||||
|
case MemoryUsage::Download:
|
||||||
|
return VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
|
||||||
|
VK_MEMORY_PROPERTY_HOST_CACHED_BIT;
|
||||||
|
}
|
||||||
|
UNREACHABLE_MSG("Invalid memory usage={}", usage);
|
||||||
|
return VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
|
||||||
|
}
|
||||||
} // Anonymous namespace
|
} // Anonymous namespace
|
||||||
|
|
||||||
class MemoryAllocation {
|
class MemoryAllocation {
|
||||||
public:
|
public:
|
||||||
explicit MemoryAllocation(const Device& device_, vk::DeviceMemory memory_,
|
explicit MemoryAllocation(const Device& device_, vk::DeviceMemory memory_,
|
||||||
VkMemoryPropertyFlags properties_, u64 allocation_size_, u32 type_)
|
VkMemoryPropertyFlags properties, u64 allocation_size_, u32 type)
|
||||||
: device{device_}, memory{std::move(memory_)}, properties{properties_},
|
: device{device_}, memory{std::move(memory_)}, allocation_size{allocation_size_},
|
||||||
allocation_size{allocation_size_}, shifted_type{ShiftType(type_)} {}
|
property_flags{properties}, shifted_memory_type{1U << type} {}
|
||||||
|
|
||||||
[[nodiscard]] std::optional<MemoryCommit> Commit(VkDeviceSize size, VkDeviceSize alignment) {
|
[[nodiscard]] std::optional<MemoryCommit> Commit(VkDeviceSize size, VkDeviceSize alignment) {
|
||||||
const std::optional<u64> alloc = FindFreeRegion(size, alignment);
|
const std::optional<u64> alloc = FindFreeRegion(size, alignment);
|
||||||
|
@ -68,17 +81,16 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] std::span<u8> Map() {
|
[[nodiscard]] std::span<u8> Map() {
|
||||||
if (!memory_mapped_span.empty()) {
|
if (memory_mapped_span.empty()) {
|
||||||
return memory_mapped_span;
|
u8* const raw_pointer = memory.Map(0, allocation_size);
|
||||||
|
memory_mapped_span = std::span<u8>(raw_pointer, allocation_size);
|
||||||
}
|
}
|
||||||
u8* const raw_pointer = memory.Map(0, allocation_size);
|
|
||||||
memory_mapped_span = std::span<u8>(raw_pointer, allocation_size);
|
|
||||||
return memory_mapped_span;
|
return memory_mapped_span;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns whether this allocation is compatible with the arguments.
|
/// Returns whether this allocation is compatible with the arguments.
|
||||||
[[nodiscard]] bool IsCompatible(VkMemoryPropertyFlags wanted_properties, u32 type_mask) const {
|
[[nodiscard]] bool IsCompatible(VkMemoryPropertyFlags flags, u32 type_mask) const {
|
||||||
return (wanted_properties & properties) && (type_mask & shifted_type) != 0;
|
return (flags & property_flags) && (type_mask & shifted_memory_type) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -106,13 +118,13 @@ private:
|
||||||
return candidate;
|
return candidate;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Device& device; ///< Vulkan device.
|
const Device& device; ///< Vulkan device.
|
||||||
const vk::DeviceMemory memory; ///< Vulkan memory allocation handler.
|
const vk::DeviceMemory memory; ///< Vulkan memory allocation handler.
|
||||||
const VkMemoryPropertyFlags properties; ///< Vulkan properties.
|
const u64 allocation_size; ///< Size of this allocation.
|
||||||
const u64 allocation_size; ///< Size of this allocation.
|
const VkMemoryPropertyFlags property_flags; ///< Vulkan memory property flags.
|
||||||
const u32 shifted_type; ///< Stored Vulkan type of this allocation, shifted.
|
const u32 shifted_memory_type; ///< Shifted Vulkan memory type.
|
||||||
std::vector<Range> commits; ///< All commit ranges done from this allocation.
|
std::vector<Range> commits; ///< All commit ranges done from this allocation.
|
||||||
std::span<u8> memory_mapped_span; ///< Memory mapped span. Empty if not queried before.
|
std::span<u8> memory_mapped_span; ///< Memory mapped span. Empty if not queried before.
|
||||||
};
|
};
|
||||||
|
|
||||||
MemoryCommit::MemoryCommit(const Device& device_, MemoryAllocation* allocation_,
|
MemoryCommit::MemoryCommit(const Device& device_, MemoryAllocation* allocation_,
|
||||||
|
@ -138,10 +150,9 @@ MemoryCommit::MemoryCommit(MemoryCommit&& rhs) noexcept
|
||||||
interval{rhs.interval}, span{std::exchange(rhs.span, std::span<u8>{})} {}
|
interval{rhs.interval}, span{std::exchange(rhs.span, std::span<u8>{})} {}
|
||||||
|
|
||||||
std::span<u8> MemoryCommit::Map() {
|
std::span<u8> MemoryCommit::Map() {
|
||||||
if (!span.empty()) {
|
if (span.empty()) {
|
||||||
return span;
|
span = allocation->Map().subspan(interval.first, interval.second - interval.first);
|
||||||
}
|
}
|
||||||
span = allocation->Map().subspan(interval.first, interval.second - interval.first);
|
|
||||||
return span;
|
return span;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,25 +168,18 @@ MemoryAllocator::MemoryAllocator(const Device& device_)
|
||||||
MemoryAllocator::~MemoryAllocator() = default;
|
MemoryAllocator::~MemoryAllocator() = default;
|
||||||
|
|
||||||
MemoryCommit MemoryAllocator::Commit(const VkMemoryRequirements& requirements, MemoryUsage usage) {
|
MemoryCommit MemoryAllocator::Commit(const VkMemoryRequirements& requirements, MemoryUsage usage) {
|
||||||
const u64 chunk_size = GetAllocationChunkSize(requirements.size);
|
// Find the fastest memory flags we can afford with the current requirements
|
||||||
|
const VkMemoryPropertyFlags flags = MemoryPropertyFlags(requirements.memoryTypeBits, usage);
|
||||||
// When a host visible commit is asked, search for host visible and coherent, otherwise search
|
if (std::optional<MemoryCommit> commit = TryCommit(requirements, flags)) {
|
||||||
// for a fast device local type.
|
|
||||||
// TODO: Deduce memory types from usage in a better way
|
|
||||||
const bool host_visible = IsHostVisible(usage);
|
|
||||||
const VkMemoryPropertyFlags wanted_properties =
|
|
||||||
host_visible ? VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
|
||||||
: VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
|
|
||||||
if (std::optional<MemoryCommit> commit = TryAllocCommit(requirements, wanted_properties)) {
|
|
||||||
return std::move(*commit);
|
return std::move(*commit);
|
||||||
}
|
}
|
||||||
// Commit has failed, allocate more memory.
|
// Commit has failed, allocate more memory.
|
||||||
// TODO(Rodrigo): Handle out of memory situations in some way like flushing to guest memory.
|
// TODO(Rodrigo): Handle out of memory situations in some way like flushing to guest memory.
|
||||||
AllocMemory(wanted_properties, requirements.memoryTypeBits, chunk_size);
|
AllocMemory(flags, requirements.memoryTypeBits, AllocationChunkSize(requirements.size));
|
||||||
|
|
||||||
// Commit again, this time it won't fail since there's a fresh allocation above.
|
// Commit again, this time it won't fail since there's a fresh allocation above.
|
||||||
// If it does, there's a bug.
|
// If it does, there's a bug.
|
||||||
return TryAllocCommit(requirements, wanted_properties).value();
|
return TryCommit(requirements, flags).value();
|
||||||
}
|
}
|
||||||
|
|
||||||
MemoryCommit MemoryAllocator::Commit(const vk::Buffer& buffer, MemoryUsage usage) {
|
MemoryCommit MemoryAllocator::Commit(const vk::Buffer& buffer, MemoryUsage usage) {
|
||||||
|
@ -190,33 +194,22 @@ MemoryCommit MemoryAllocator::Commit(const vk::Image& image, MemoryUsage usage)
|
||||||
return commit;
|
return commit;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MemoryAllocator::AllocMemory(VkMemoryPropertyFlags wanted_properties, u32 type_mask,
|
void MemoryAllocator::AllocMemory(VkMemoryPropertyFlags flags, u32 type_mask, u64 size) {
|
||||||
u64 size) {
|
const u32 type = FindType(flags, type_mask).value();
|
||||||
const u32 type = [&] {
|
|
||||||
for (u32 type_index = 0; type_index < properties.memoryTypeCount; ++type_index) {
|
|
||||||
const auto flags = properties.memoryTypes[type_index].propertyFlags;
|
|
||||||
if ((type_mask & (1U << type_index)) && (flags & wanted_properties)) {
|
|
||||||
// The type matches in type and in the wanted properties.
|
|
||||||
return type_index;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
UNREACHABLE_MSG("Couldn't find a compatible memory type!");
|
|
||||||
return 0U;
|
|
||||||
}();
|
|
||||||
vk::DeviceMemory memory = device.GetLogical().AllocateMemory({
|
vk::DeviceMemory memory = device.GetLogical().AllocateMemory({
|
||||||
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
|
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
|
||||||
.pNext = nullptr,
|
.pNext = nullptr,
|
||||||
.allocationSize = size,
|
.allocationSize = size,
|
||||||
.memoryTypeIndex = type,
|
.memoryTypeIndex = type,
|
||||||
});
|
});
|
||||||
allocations.push_back(std::make_unique<MemoryAllocation>(device, std::move(memory),
|
allocations.push_back(
|
||||||
wanted_properties, size, type));
|
std::make_unique<MemoryAllocation>(device, std::move(memory), flags, size, type));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<MemoryCommit> MemoryAllocator::TryAllocCommit(
|
std::optional<MemoryCommit> MemoryAllocator::TryCommit(const VkMemoryRequirements& requirements,
|
||||||
const VkMemoryRequirements& requirements, VkMemoryPropertyFlags wanted_properties) {
|
VkMemoryPropertyFlags flags) {
|
||||||
for (auto& allocation : allocations) {
|
for (auto& allocation : allocations) {
|
||||||
if (!allocation->IsCompatible(wanted_properties, requirements.memoryTypeBits)) {
|
if (!allocation->IsCompatible(flags, requirements.memoryTypeBits)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (auto commit = allocation->Commit(requirements.size, requirements.alignment)) {
|
if (auto commit = allocation->Commit(requirements.size, requirements.alignment)) {
|
||||||
|
@ -226,6 +219,40 @@ std::optional<MemoryCommit> MemoryAllocator::TryAllocCommit(
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VkMemoryPropertyFlags MemoryAllocator::MemoryPropertyFlags(u32 type_mask, MemoryUsage usage) const {
|
||||||
|
return MemoryPropertyFlags(type_mask, MemoryUsagePropertyFlags(usage));
|
||||||
|
}
|
||||||
|
|
||||||
|
VkMemoryPropertyFlags MemoryAllocator::MemoryPropertyFlags(u32 type_mask,
|
||||||
|
VkMemoryPropertyFlags flags) const {
|
||||||
|
if (FindType(flags, type_mask)) {
|
||||||
|
// Found a memory type with those requirements
|
||||||
|
return flags;
|
||||||
|
}
|
||||||
|
if (flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) {
|
||||||
|
// Remove host cached bit in case it's not supported
|
||||||
|
return MemoryPropertyFlags(type_mask, flags & ~VK_MEMORY_PROPERTY_HOST_CACHED_BIT);
|
||||||
|
}
|
||||||
|
if (flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {
|
||||||
|
// Remove device local, if it's not supported by the requested resource
|
||||||
|
return MemoryPropertyFlags(type_mask, flags & ~VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
||||||
|
}
|
||||||
|
UNREACHABLE_MSG("No compatible memory types found");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<u32> MemoryAllocator::FindType(VkMemoryPropertyFlags flags, u32 type_mask) const {
|
||||||
|
for (u32 type_index = 0; type_index < properties.memoryTypeCount; ++type_index) {
|
||||||
|
const VkMemoryPropertyFlags type_flags = properties.memoryTypes[type_index].propertyFlags;
|
||||||
|
if ((type_mask & (1U << type_index)) && (type_flags & flags)) {
|
||||||
|
// The type matches in type and in the wanted properties.
|
||||||
|
return type_index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Failed to find index
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
bool IsHostVisible(MemoryUsage usage) noexcept {
|
bool IsHostVisible(MemoryUsage usage) noexcept {
|
||||||
switch (usage) {
|
switch (usage) {
|
||||||
case MemoryUsage::DeviceLocal:
|
case MemoryUsage::DeviceLocal:
|
||||||
|
|
|
@ -92,13 +92,22 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// Allocates a chunk of memory.
|
/// Allocates a chunk of memory.
|
||||||
void AllocMemory(VkMemoryPropertyFlags wanted_properties, u32 type_mask, u64 size);
|
void AllocMemory(VkMemoryPropertyFlags flags, u32 type_mask, u64 size);
|
||||||
|
|
||||||
/// Tries to allocate a memory commit.
|
/// Tries to allocate a memory commit.
|
||||||
std::optional<MemoryCommit> TryAllocCommit(const VkMemoryRequirements& requirements,
|
std::optional<MemoryCommit> TryCommit(const VkMemoryRequirements& requirements,
|
||||||
VkMemoryPropertyFlags wanted_properties);
|
VkMemoryPropertyFlags flags);
|
||||||
|
|
||||||
const Device& device; ///< Device handler.
|
/// Returns the fastest compatible memory property flags from a wanted usage.
|
||||||
|
VkMemoryPropertyFlags MemoryPropertyFlags(u32 type_mask, MemoryUsage usage) const;
|
||||||
|
|
||||||
|
/// Returns the fastest compatible memory property flags from the wanted flags.
|
||||||
|
VkMemoryPropertyFlags MemoryPropertyFlags(u32 type_mask, VkMemoryPropertyFlags flags) const;
|
||||||
|
|
||||||
|
/// Returns index to the fastest memory type compatible with the passed requirements.
|
||||||
|
std::optional<u32> FindType(VkMemoryPropertyFlags flags, u32 type_mask) const;
|
||||||
|
|
||||||
|
const Device& device; ///< Device handle.
|
||||||
const VkPhysicalDeviceMemoryProperties properties; ///< Physical device properties.
|
const VkPhysicalDeviceMemoryProperties properties; ///< Physical device properties.
|
||||||
std::vector<std::unique_ptr<MemoryAllocation>> allocations; ///< Current allocations.
|
std::vector<std::unique_ptr<MemoryAllocation>> allocations; ///< Current allocations.
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue