mirror of
https://github.com/shadps4-emu/shadPS4.git
synced 2025-05-22 03:15:01 +00:00
video_core: Implement guest buffer manager (#373)
* video_core: Introduce buffer cache * video_core: Use multi level page table for caches * renderer_vulkan: Remove unused stream buffer * fix build * oops forgot optimize off
This commit is contained in:
parent
159be2c7f4
commit
381ba8c7a5
55 changed files with 2697 additions and 1039 deletions
|
@ -7,7 +7,7 @@
|
|||
#include "core/libraries/error_codes.h"
|
||||
#include "core/libraries/kernel/memory_management.h"
|
||||
#include "core/memory.h"
|
||||
#include "video_core/renderer_vulkan/vk_instance.h"
|
||||
#include "video_core/renderer_vulkan/vk_rasterizer.h"
|
||||
|
||||
namespace Core {
|
||||
|
||||
|
@ -172,7 +172,7 @@ int MemoryManager::MapMemory(void** out_addr, VAddr virtual_addr, size_t size, M
|
|||
|
||||
if (type == VMAType::Direct) {
|
||||
new_vma.phys_base = phys_addr;
|
||||
MapVulkanMemory(mapped_addr, size);
|
||||
rasterizer->MapMemory(mapped_addr, size);
|
||||
}
|
||||
if (type == VMAType::Flexible) {
|
||||
flexible_usage += size;
|
||||
|
@ -222,7 +222,7 @@ void MemoryManager::UnmapMemory(VAddr virtual_addr, size_t size) {
|
|||
const auto type = it->second.type;
|
||||
const bool has_backing = type == VMAType::Direct || type == VMAType::File;
|
||||
if (type == VMAType::Direct) {
|
||||
UnmapVulkanMemory(virtual_addr, size);
|
||||
rasterizer->UnmapMemory(virtual_addr, size);
|
||||
}
|
||||
if (type == VMAType::Flexible) {
|
||||
flexible_usage -= size;
|
||||
|
@ -263,7 +263,7 @@ int MemoryManager::QueryProtection(VAddr addr, void** start, void** end, u32* pr
|
|||
}
|
||||
|
||||
int MemoryManager::VirtualQuery(VAddr addr, int flags,
|
||||
Libraries::Kernel::OrbisVirtualQueryInfo* info) {
|
||||
::Libraries::Kernel::OrbisVirtualQueryInfo* info) {
|
||||
std::scoped_lock lk{mutex};
|
||||
|
||||
auto it = FindVMA(addr);
|
||||
|
@ -293,7 +293,7 @@ int MemoryManager::VirtualQuery(VAddr addr, int flags,
|
|||
}
|
||||
|
||||
int MemoryManager::DirectMemoryQuery(PAddr addr, bool find_next,
|
||||
Libraries::Kernel::OrbisQueryInfo* out_info) {
|
||||
::Libraries::Kernel::OrbisQueryInfo* out_info) {
|
||||
std::scoped_lock lk{mutex};
|
||||
|
||||
auto dmem_area = FindDmemArea(addr);
|
||||
|
@ -333,13 +333,6 @@ int MemoryManager::DirectQueryAvailable(PAddr search_start, PAddr search_end, si
|
|||
return ORBIS_OK;
|
||||
}
|
||||
|
||||
std::pair<vk::Buffer, size_t> MemoryManager::GetVulkanBuffer(VAddr addr) {
|
||||
auto it = mapped_memories.upper_bound(addr);
|
||||
it = std::prev(it);
|
||||
ASSERT(it != mapped_memories.end() && it->first <= addr);
|
||||
return std::make_pair(*it->second.buffer, addr - it->first);
|
||||
}
|
||||
|
||||
void MemoryManager::NameVirtualRange(VAddr virtual_addr, size_t size, std::string_view name) {
|
||||
auto it = FindVMA(virtual_addr);
|
||||
|
||||
|
@ -455,85 +448,6 @@ MemoryManager::DMemHandle MemoryManager::Split(DMemHandle dmem_handle, size_t of
|
|||
return dmem_map.emplace_hint(std::next(dmem_handle), new_area.base, new_area);
|
||||
};
|
||||
|
||||
void MemoryManager::MapVulkanMemory(VAddr addr, size_t size) {
|
||||
return;
|
||||
const vk::Device device = instance->GetDevice();
|
||||
const auto memory_props = instance->GetPhysicalDevice().getMemoryProperties();
|
||||
void* host_pointer = reinterpret_cast<void*>(addr);
|
||||
const auto host_mem_props = device.getMemoryHostPointerPropertiesEXT(
|
||||
vk::ExternalMemoryHandleTypeFlagBits::eHostAllocationEXT, host_pointer);
|
||||
ASSERT(host_mem_props.memoryTypeBits != 0);
|
||||
|
||||
int mapped_memory_type = -1;
|
||||
auto find_mem_type_with_flag = [&](const vk::MemoryPropertyFlags flags) {
|
||||
u32 host_mem_types = host_mem_props.memoryTypeBits;
|
||||
while (host_mem_types != 0) {
|
||||
// Try to find a cached memory type
|
||||
mapped_memory_type = std::countr_zero(host_mem_types);
|
||||
host_mem_types -= (1 << mapped_memory_type);
|
||||
|
||||
if ((memory_props.memoryTypes[mapped_memory_type].propertyFlags & flags) == flags) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
mapped_memory_type = -1;
|
||||
};
|
||||
|
||||
// First try to find a memory that is both coherent and cached
|
||||
find_mem_type_with_flag(vk::MemoryPropertyFlagBits::eHostCoherent |
|
||||
vk::MemoryPropertyFlagBits::eHostCached);
|
||||
if (mapped_memory_type == -1)
|
||||
// Then only coherent (lower performance)
|
||||
find_mem_type_with_flag(vk::MemoryPropertyFlagBits::eHostCoherent);
|
||||
|
||||
if (mapped_memory_type == -1) {
|
||||
LOG_CRITICAL(Render_Vulkan, "No coherent memory available for memory mapping");
|
||||
mapped_memory_type = std::countr_zero(host_mem_props.memoryTypeBits);
|
||||
}
|
||||
|
||||
const vk::StructureChain alloc_info = {
|
||||
vk::MemoryAllocateInfo{
|
||||
.allocationSize = size,
|
||||
.memoryTypeIndex = static_cast<uint32_t>(mapped_memory_type),
|
||||
},
|
||||
vk::ImportMemoryHostPointerInfoEXT{
|
||||
.handleType = vk::ExternalMemoryHandleTypeFlagBits::eHostAllocationEXT,
|
||||
.pHostPointer = host_pointer,
|
||||
},
|
||||
};
|
||||
|
||||
const auto [it, new_memory] = mapped_memories.try_emplace(addr);
|
||||
ASSERT_MSG(new_memory, "Attempting to remap already mapped vulkan memory");
|
||||
|
||||
auto& memory = it->second;
|
||||
memory.backing = device.allocateMemoryUnique(alloc_info.get());
|
||||
|
||||
constexpr vk::BufferUsageFlags MapFlags =
|
||||
vk::BufferUsageFlagBits::eIndexBuffer | vk::BufferUsageFlagBits::eVertexBuffer |
|
||||
vk::BufferUsageFlagBits::eTransferSrc | vk::BufferUsageFlagBits::eTransferDst |
|
||||
vk::BufferUsageFlagBits::eUniformBuffer | vk::BufferUsageFlagBits::eStorageBuffer;
|
||||
|
||||
const vk::StructureChain buffer_info = {
|
||||
vk::BufferCreateInfo{
|
||||
.size = size,
|
||||
.usage = MapFlags,
|
||||
.sharingMode = vk::SharingMode::eExclusive,
|
||||
},
|
||||
vk::ExternalMemoryBufferCreateInfoKHR{
|
||||
.handleTypes = vk::ExternalMemoryHandleTypeFlagBits::eHostAllocationEXT,
|
||||
}};
|
||||
memory.buffer = device.createBufferUnique(buffer_info.get());
|
||||
device.bindBufferMemory(*memory.buffer, *memory.backing, 0);
|
||||
}
|
||||
|
||||
void MemoryManager::UnmapVulkanMemory(VAddr addr, size_t size) {
|
||||
return;
|
||||
const auto it = mapped_memories.find(addr);
|
||||
ASSERT(it != mapped_memories.end() && it->second.buffer_size == size);
|
||||
mapped_memories.erase(it);
|
||||
}
|
||||
|
||||
int MemoryManager::GetDirectMemoryType(PAddr addr, int* directMemoryTypeOut,
|
||||
void** directMemoryStartOut, void** directMemoryEndOut) {
|
||||
std::scoped_lock lk{mutex};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue