2018-08-07 16:42:21 +02:00
|
|
|
#include "dxvk_device.h"
|
2017-10-10 23:32:13 +02:00
|
|
|
#include "dxvk_memory.h"
|
|
|
|
|
|
|
|
namespace dxvk {
|
|
|
|
|
2018-05-29 14:48:27 +02:00
|
|
|
DxvkMemory::DxvkMemory() { }
|
2017-10-10 23:32:13 +02:00
|
|
|
DxvkMemory::DxvkMemory(
|
2018-05-29 14:48:27 +02:00
|
|
|
DxvkMemoryAllocator* alloc,
|
|
|
|
DxvkMemoryChunk* chunk,
|
|
|
|
DxvkMemoryType* type,
|
|
|
|
VkDeviceMemory memory,
|
|
|
|
VkDeviceSize offset,
|
|
|
|
VkDeviceSize length,
|
|
|
|
void* mapPtr)
|
|
|
|
: m_alloc (alloc),
|
|
|
|
m_chunk (chunk),
|
|
|
|
m_type (type),
|
2017-12-16 16:48:42 +01:00
|
|
|
m_memory (memory),
|
|
|
|
m_offset (offset),
|
|
|
|
m_length (length),
|
2018-05-29 14:48:27 +02:00
|
|
|
m_mapPtr (mapPtr) { }
|
2017-10-10 23:32:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
DxvkMemory::DxvkMemory(DxvkMemory&& other)
|
2018-05-29 14:48:27 +02:00
|
|
|
: m_alloc (std::exchange(other.m_alloc, nullptr)),
|
|
|
|
m_chunk (std::exchange(other.m_chunk, nullptr)),
|
|
|
|
m_type (std::exchange(other.m_type, nullptr)),
|
2018-03-06 20:34:34 +03:00
|
|
|
m_memory (std::exchange(other.m_memory, VkDeviceMemory(VK_NULL_HANDLE))),
|
2017-12-16 16:48:42 +01:00
|
|
|
m_offset (std::exchange(other.m_offset, 0)),
|
|
|
|
m_length (std::exchange(other.m_length, 0)),
|
|
|
|
m_mapPtr (std::exchange(other.m_mapPtr, nullptr)) { }
|
2017-10-10 23:32:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
DxvkMemory& DxvkMemory::operator = (DxvkMemory&& other) {
|
2018-04-03 15:32:00 +02:00
|
|
|
this->free();
|
2018-05-29 14:48:27 +02:00
|
|
|
m_alloc = std::exchange(other.m_alloc, nullptr);
|
2017-12-16 16:48:42 +01:00
|
|
|
m_chunk = std::exchange(other.m_chunk, nullptr);
|
2018-05-29 14:48:27 +02:00
|
|
|
m_type = std::exchange(other.m_type, nullptr);
|
2018-03-06 20:34:34 +03:00
|
|
|
m_memory = std::exchange(other.m_memory, VkDeviceMemory(VK_NULL_HANDLE));
|
2017-12-16 16:48:42 +01:00
|
|
|
m_offset = std::exchange(other.m_offset, 0);
|
|
|
|
m_length = std::exchange(other.m_length, 0);
|
|
|
|
m_mapPtr = std::exchange(other.m_mapPtr, nullptr);
|
2017-10-10 23:32:13 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DxvkMemory::~DxvkMemory() {
|
2018-04-03 15:32:00 +02:00
|
|
|
this->free();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DxvkMemory::free() {
|
2018-05-29 14:48:27 +02:00
|
|
|
if (m_alloc != nullptr)
|
|
|
|
m_alloc->free(*this);
|
2017-10-10 23:32:13 +02:00
|
|
|
}
|
|
|
|
|
2017-12-16 16:48:42 +01:00
|
|
|
|
|
|
|
DxvkMemoryChunk::DxvkMemoryChunk(
|
2018-05-29 14:48:27 +02:00
|
|
|
DxvkMemoryAllocator* alloc,
|
|
|
|
DxvkMemoryType* type,
|
|
|
|
DxvkDeviceMemory memory)
|
|
|
|
: m_alloc(alloc), m_type(type), m_memory(memory) {
|
2017-12-16 16:48:42 +01:00
|
|
|
// Mark the entire chunk as free
|
2018-05-29 14:48:27 +02:00
|
|
|
m_freeList.push_back(FreeSlice { 0, memory.memSize });
|
2017-12-16 16:48:42 +01:00
|
|
|
}
|
2017-10-10 23:32:13 +02:00
|
|
|
|
2017-12-16 16:48:42 +01:00
|
|
|
|
|
|
|
DxvkMemoryChunk::~DxvkMemoryChunk() {
|
2018-05-29 14:48:27 +02:00
|
|
|
// This call is technically not thread-safe, but it
|
|
|
|
// doesn't need to be since we don't free chunks
|
|
|
|
m_alloc->freeDeviceMemory(m_type, m_memory);
|
2017-10-10 23:32:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-17 09:08:00 +02:00
|
|
|
DxvkMemory DxvkMemoryChunk::alloc(
|
|
|
|
VkMemoryPropertyFlags flags,
|
|
|
|
VkDeviceSize size,
|
|
|
|
VkDeviceSize align) {
|
|
|
|
// Property flags must be compatible. This could
|
|
|
|
// be refined a bit in the future if necessary.
|
|
|
|
if (m_memory.memFlags != flags)
|
|
|
|
return DxvkMemory();
|
|
|
|
|
2017-12-16 18:10:55 +01:00
|
|
|
// If the chunk is full, return
|
|
|
|
if (m_freeList.size() == 0)
|
2017-12-16 16:48:42 +01:00
|
|
|
return DxvkMemory();
|
|
|
|
|
|
|
|
// Select the slice to allocate from in a worst-fit
|
|
|
|
// manner. This may help keep fragmentation low.
|
|
|
|
auto bestSlice = m_freeList.begin();
|
|
|
|
|
|
|
|
for (auto slice = m_freeList.begin(); slice != m_freeList.end(); slice++) {
|
2017-12-16 18:10:55 +01:00
|
|
|
if (slice->length == size) {
|
2017-12-16 16:48:42 +01:00
|
|
|
bestSlice = slice;
|
2017-12-16 18:10:55 +01:00
|
|
|
break;
|
|
|
|
} else if (slice->length > bestSlice->length) {
|
|
|
|
bestSlice = slice;
|
|
|
|
}
|
2017-12-16 16:48:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// We need to align the allocation to the requested alignment
|
|
|
|
const VkDeviceSize sliceStart = bestSlice->offset;
|
|
|
|
const VkDeviceSize sliceEnd = bestSlice->offset + bestSlice->length;
|
|
|
|
|
|
|
|
const VkDeviceSize allocStart = dxvk::align(sliceStart, align);
|
|
|
|
const VkDeviceSize allocEnd = dxvk::align(allocStart + size, align);
|
2017-10-10 23:32:13 +02:00
|
|
|
|
2017-12-16 16:48:42 +01:00
|
|
|
if (allocEnd > sliceEnd)
|
|
|
|
return DxvkMemory();
|
|
|
|
|
|
|
|
// We can use this slice, but we'll have to add
|
|
|
|
// the unused parts of it back to the free list.
|
|
|
|
m_freeList.erase(bestSlice);
|
|
|
|
|
|
|
|
if (allocStart != sliceStart)
|
|
|
|
m_freeList.push_back({ sliceStart, allocStart - sliceStart });
|
|
|
|
|
|
|
|
if (allocEnd != sliceEnd)
|
|
|
|
m_freeList.push_back({ allocEnd, sliceEnd - allocEnd });
|
|
|
|
|
|
|
|
// Create the memory object with the aligned slice
|
2018-05-29 14:48:27 +02:00
|
|
|
return DxvkMemory(m_alloc, this, m_type,
|
|
|
|
m_memory.memHandle, allocStart, allocEnd - allocStart,
|
|
|
|
reinterpret_cast<char*>(m_memory.memPointer) + allocStart);
|
2017-10-10 23:32:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-16 16:48:42 +01:00
|
|
|
void DxvkMemoryChunk::free(
|
|
|
|
VkDeviceSize offset,
|
|
|
|
VkDeviceSize length) {
|
|
|
|
// Remove adjacent entries from the free list and then add
|
|
|
|
// a new slice that covers all those entries. Without doing
|
|
|
|
// so, the slice could not be reused for larger allocations.
|
|
|
|
auto curr = m_freeList.begin();
|
2017-10-10 23:32:13 +02:00
|
|
|
|
2017-12-16 16:48:42 +01:00
|
|
|
while (curr != m_freeList.end()) {
|
|
|
|
if (curr->offset == offset + length) {
|
|
|
|
length += curr->length;
|
|
|
|
curr = m_freeList.erase(curr);
|
|
|
|
} else if (curr->offset + curr->length == offset) {
|
|
|
|
offset -= curr->length;
|
|
|
|
length += curr->length;
|
|
|
|
curr = m_freeList.erase(curr);
|
|
|
|
} else {
|
|
|
|
curr++;
|
2017-10-10 23:32:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-16 16:48:42 +01:00
|
|
|
m_freeList.push_back({ offset, length });
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-07 16:42:21 +02:00
|
|
|
DxvkMemoryAllocator::DxvkMemoryAllocator(const DxvkDevice* device)
|
|
|
|
: m_vkd (device->vkd()),
|
|
|
|
m_devProps (device->adapter()->deviceProperties()),
|
|
|
|
m_memProps (device->adapter()->memoryProperties()),
|
|
|
|
m_allowOvercommit (device->config().allowMemoryOvercommit) {
|
2018-05-29 14:48:27 +02:00
|
|
|
for (uint32_t i = 0; i < m_memProps.memoryHeapCount; i++) {
|
2018-07-09 19:18:39 +02:00
|
|
|
VkDeviceSize heapSize = m_memProps.memoryHeaps[i].size;
|
|
|
|
|
2018-05-29 14:48:27 +02:00
|
|
|
m_memHeaps[i].properties = m_memProps.memoryHeaps[i];
|
2018-07-09 19:18:39 +02:00
|
|
|
m_memHeaps[i].chunkSize = pickChunkSize(heapSize);
|
2018-05-29 14:48:27 +02:00
|
|
|
m_memHeaps[i].stats = DxvkMemoryStats { 0, 0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < m_memProps.memoryTypeCount; i++) {
|
|
|
|
m_memTypes[i].heap = &m_memHeaps[m_memProps.memoryTypes[i].heapIndex];
|
|
|
|
m_memTypes[i].memType = m_memProps.memoryTypes[i];
|
|
|
|
m_memTypes[i].memTypeId = i;
|
|
|
|
}
|
2017-12-16 16:48:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DxvkMemoryAllocator::~DxvkMemoryAllocator() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DxvkMemory DxvkMemoryAllocator::alloc(
|
2018-06-24 02:55:42 -06:00
|
|
|
const VkMemoryRequirements* req,
|
|
|
|
const VkMemoryDedicatedAllocateInfoKHR* dedAllocInfo,
|
|
|
|
VkMemoryPropertyFlags flags) {
|
2018-05-29 14:48:27 +02:00
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
|
|
|
2018-06-24 02:55:42 -06:00
|
|
|
DxvkMemory result = this->tryAlloc(req, dedAllocInfo, flags);
|
2017-12-16 16:48:42 +01:00
|
|
|
|
2018-05-29 14:48:27 +02:00
|
|
|
if (!result && (flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT))
|
2018-06-24 02:55:42 -06:00
|
|
|
result = this->tryAlloc(req, dedAllocInfo, flags & ~VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
2018-02-27 12:36:44 +01:00
|
|
|
|
2018-05-29 14:48:27 +02:00
|
|
|
if (!result) {
|
2018-04-14 13:03:14 +02:00
|
|
|
Logger::err(str::format(
|
|
|
|
"DxvkMemoryAllocator: Memory allocation failed",
|
2018-06-24 02:55:42 -06:00
|
|
|
"\n Size: ", req->size,
|
|
|
|
"\n Alignment: ", req->alignment,
|
2018-04-14 13:03:14 +02:00
|
|
|
"\n Mem flags: ", "0x", std::hex, flags,
|
2018-06-24 02:55:42 -06:00
|
|
|
"\n Mem types: ", "0x", std::hex, req->memoryTypeBits));
|
2018-04-14 13:03:14 +02:00
|
|
|
throw DxvkError("DxvkMemoryAllocator: Memory allocation failed");
|
2018-02-27 12:36:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-29 14:48:27 +02:00
|
|
|
DxvkMemoryStats DxvkMemoryAllocator::getMemoryStats() {
|
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
2018-05-29 14:56:41 +02:00
|
|
|
|
2018-04-03 15:32:00 +02:00
|
|
|
DxvkMemoryStats totalStats;
|
|
|
|
|
2018-05-29 14:48:27 +02:00
|
|
|
for (size_t i = 0; i < m_memProps.memoryHeapCount; i++) {
|
|
|
|
totalStats.memoryAllocated += m_memHeaps[i].stats.memoryAllocated;
|
|
|
|
totalStats.memoryUsed += m_memHeaps[i].stats.memoryUsed;
|
2018-04-03 15:32:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return totalStats;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-27 12:36:44 +01:00
|
|
|
DxvkMemory DxvkMemoryAllocator::tryAlloc(
|
2018-06-24 02:55:42 -06:00
|
|
|
const VkMemoryRequirements* req,
|
|
|
|
const VkMemoryDedicatedAllocateInfoKHR* dedAllocInfo,
|
|
|
|
VkMemoryPropertyFlags flags) {
|
2018-02-27 12:36:44 +01:00
|
|
|
DxvkMemory result;
|
2018-06-24 02:55:42 -06:00
|
|
|
|
2018-05-29 14:48:27 +02:00
|
|
|
for (uint32_t i = 0; i < m_memProps.memoryTypeCount && !result; i++) {
|
2018-06-24 02:55:42 -06:00
|
|
|
const bool supported = (req->memoryTypeBits & (1u << i)) != 0;
|
2018-05-29 14:48:27 +02:00
|
|
|
const bool adequate = (m_memTypes[i].memType.propertyFlags & flags) == flags;
|
2017-12-16 16:48:42 +01:00
|
|
|
|
2018-05-29 14:48:27 +02:00
|
|
|
if (supported && adequate) {
|
2018-06-24 02:55:42 -06:00
|
|
|
result = this->tryAllocFromType(&m_memTypes[i],
|
2018-09-17 09:08:00 +02:00
|
|
|
flags, req->size, req->alignment, dedAllocInfo);
|
2018-05-29 14:48:27 +02:00
|
|
|
}
|
2017-12-16 16:48:42 +01:00
|
|
|
}
|
|
|
|
|
2018-02-27 12:36:44 +01:00
|
|
|
return result;
|
2017-12-16 16:48:42 +01:00
|
|
|
}
|
|
|
|
|
2018-05-29 14:48:27 +02:00
|
|
|
|
|
|
|
DxvkMemory DxvkMemoryAllocator::tryAllocFromType(
|
2018-06-24 02:55:42 -06:00
|
|
|
DxvkMemoryType* type,
|
2018-09-17 09:08:00 +02:00
|
|
|
VkMemoryPropertyFlags flags,
|
2018-06-24 02:55:42 -06:00
|
|
|
VkDeviceSize size,
|
|
|
|
VkDeviceSize align,
|
|
|
|
const VkMemoryDedicatedAllocateInfoKHR* dedAllocInfo) {
|
2018-05-29 14:48:27 +02:00
|
|
|
DxvkMemory memory;
|
|
|
|
|
2018-07-09 19:18:39 +02:00
|
|
|
if ((size >= type->heap->chunkSize / 4) || dedAllocInfo) {
|
2018-09-17 09:08:00 +02:00
|
|
|
DxvkDeviceMemory devMem = this->tryAllocDeviceMemory(
|
|
|
|
type, flags, size, dedAllocInfo);
|
2018-05-29 14:48:27 +02:00
|
|
|
|
|
|
|
if (devMem.memHandle != VK_NULL_HANDLE)
|
|
|
|
memory = DxvkMemory(this, nullptr, type, devMem.memHandle, 0, size, devMem.memPointer);
|
|
|
|
} else {
|
|
|
|
for (uint32_t i = 0; i < type->chunks.size() && !memory; i++)
|
2018-09-17 09:08:00 +02:00
|
|
|
memory = type->chunks[i]->alloc(flags, size, align);
|
2018-05-29 14:48:27 +02:00
|
|
|
|
|
|
|
if (!memory) {
|
2018-07-09 19:18:39 +02:00
|
|
|
DxvkDeviceMemory devMem = tryAllocDeviceMemory(
|
2018-09-17 09:08:00 +02:00
|
|
|
type, flags, type->heap->chunkSize, nullptr);
|
2018-05-29 14:48:27 +02:00
|
|
|
|
|
|
|
if (devMem.memHandle == VK_NULL_HANDLE)
|
|
|
|
return DxvkMemory();
|
|
|
|
|
|
|
|
Rc<DxvkMemoryChunk> chunk = new DxvkMemoryChunk(this, type, devMem);
|
2018-09-17 09:08:00 +02:00
|
|
|
memory = chunk->alloc(flags, size, align);
|
2018-05-29 14:48:27 +02:00
|
|
|
|
|
|
|
type->chunks.push_back(std::move(chunk));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memory)
|
|
|
|
type->heap->stats.memoryUsed += memory.m_length;
|
|
|
|
|
|
|
|
return memory;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DxvkDeviceMemory DxvkMemoryAllocator::tryAllocDeviceMemory(
|
2018-06-24 02:55:42 -06:00
|
|
|
DxvkMemoryType* type,
|
2018-09-17 09:08:00 +02:00
|
|
|
VkMemoryPropertyFlags flags,
|
2018-06-24 02:55:42 -06:00
|
|
|
VkDeviceSize size,
|
|
|
|
const VkMemoryDedicatedAllocateInfoKHR* dedAllocInfo) {
|
2018-05-29 14:56:41 +02:00
|
|
|
if ((type->memType.propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)
|
2018-08-07 16:42:21 +02:00
|
|
|
&& (type->heap->stats.memoryAllocated + size > type->heap->properties.size)
|
|
|
|
&& (!m_allowOvercommit))
|
2018-05-29 14:48:27 +02:00
|
|
|
return DxvkDeviceMemory();
|
|
|
|
|
|
|
|
DxvkDeviceMemory result;
|
2018-09-17 09:08:00 +02:00
|
|
|
result.memSize = size;
|
|
|
|
result.memFlags = flags;
|
2018-06-24 02:55:42 -06:00
|
|
|
|
2018-05-29 14:48:27 +02:00
|
|
|
VkMemoryAllocateInfo info;
|
|
|
|
info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
2018-06-24 02:55:42 -06:00
|
|
|
info.pNext = dedAllocInfo;
|
2018-05-29 14:48:27 +02:00
|
|
|
info.allocationSize = size;
|
|
|
|
info.memoryTypeIndex = type->memTypeId;
|
|
|
|
|
|
|
|
if (m_vkd->vkAllocateMemory(m_vkd->device(), &info, nullptr, &result.memHandle) != VK_SUCCESS)
|
|
|
|
return DxvkDeviceMemory();
|
|
|
|
|
2018-09-17 09:08:00 +02:00
|
|
|
if (flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
|
2018-05-29 14:48:27 +02:00
|
|
|
VkResult status = m_vkd->vkMapMemory(m_vkd->device(), result.memHandle, 0, VK_WHOLE_SIZE, 0, &result.memPointer);
|
|
|
|
|
|
|
|
if (status != VK_SUCCESS) {
|
|
|
|
Logger::err(str::format("DxvkMemoryAllocator: Mapping memory failed with ", status));
|
|
|
|
return DxvkDeviceMemory();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type->heap->stats.memoryAllocated += size;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DxvkMemoryAllocator::free(
|
|
|
|
const DxvkMemory& memory) {
|
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
|
|
memory.m_type->heap->stats.memoryUsed -= memory.m_length;
|
|
|
|
|
|
|
|
if (memory.m_chunk != nullptr) {
|
|
|
|
this->freeChunkMemory(
|
|
|
|
memory.m_type,
|
|
|
|
memory.m_chunk,
|
|
|
|
memory.m_offset,
|
|
|
|
memory.m_length);
|
|
|
|
} else {
|
|
|
|
DxvkDeviceMemory devMem;
|
|
|
|
devMem.memHandle = memory.m_memory;
|
|
|
|
devMem.memPointer = nullptr;
|
|
|
|
devMem.memSize = memory.m_length;
|
|
|
|
this->freeDeviceMemory(memory.m_type, devMem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DxvkMemoryAllocator::freeChunkMemory(
|
|
|
|
DxvkMemoryType* type,
|
|
|
|
DxvkMemoryChunk* chunk,
|
|
|
|
VkDeviceSize offset,
|
|
|
|
VkDeviceSize length) {
|
|
|
|
chunk->free(offset, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DxvkMemoryAllocator::freeDeviceMemory(
|
|
|
|
DxvkMemoryType* type,
|
|
|
|
DxvkDeviceMemory memory) {
|
|
|
|
m_vkd->vkFreeMemory(m_vkd->device(), memory.memHandle, nullptr);
|
|
|
|
type->heap->stats.memoryAllocated -= memory.memSize;
|
|
|
|
}
|
2018-07-09 19:18:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
VkDeviceSize DxvkMemoryAllocator::pickChunkSize(VkDeviceSize heapSize) const {
|
|
|
|
// Pick a reasonable chunk size depending on the memory
|
|
|
|
// heap size. Small chunk sizes can reduce fragmentation
|
|
|
|
// and are therefore preferred for small memory heaps.
|
|
|
|
constexpr VkDeviceSize MaxChunkSize = 64 * 1024 * 1024;
|
|
|
|
constexpr VkDeviceSize MinChunkCount = 16;
|
|
|
|
|
|
|
|
return std::min(heapSize / MinChunkCount, MaxChunkSize);
|
|
|
|
}
|
2018-05-29 14:48:27 +02:00
|
|
|
|
2017-10-10 23:32:13 +02:00
|
|
|
}
|