2018-02-07 18:54:35 -08:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-04-23 08:57:12 -07:00
|
|
|
#include "common/alignment.h"
|
2018-02-07 18:54:35 -08:00
|
|
|
#include "common/assert.h"
|
2021-01-22 13:31:08 -08:00
|
|
|
#include "common/logging/log.h"
|
2019-07-08 22:19:27 -07:00
|
|
|
#include "core/core.h"
|
2021-02-12 17:58:31 -08:00
|
|
|
#include "core/hle/kernel/k_page_table.h"
|
2019-07-08 22:19:27 -07:00
|
|
|
#include "core/hle/kernel/process.h"
|
2019-02-23 21:15:35 -08:00
|
|
|
#include "core/memory.h"
|
2020-02-19 17:03:52 -08:00
|
|
|
#include "video_core/gpu.h"
|
2018-02-11 20:44:12 -08:00
|
|
|
#include "video_core/memory_manager.h"
|
2020-02-15 14:47:15 -08:00
|
|
|
#include "video_core/rasterizer_interface.h"
|
2020-10-26 20:07:36 -07:00
|
|
|
#include "video_core/renderer_base.h"
|
2018-02-07 18:54:35 -08:00
|
|
|
|
2018-02-11 20:44:12 -08:00
|
|
|
namespace Tegra {
|
2018-02-07 18:54:35 -08:00
|
|
|
|
2020-06-10 20:58:57 -07:00
|
|
|
MemoryManager::MemoryManager(Core::System& system_)
|
|
|
|
: system{system_}, page_table(page_table_size) {}
|
2018-11-23 09:58:55 -08:00
|
|
|
|
2019-05-09 16:04:41 -07:00
|
|
|
MemoryManager::~MemoryManager() = default;
|
|
|
|
|
2021-01-04 23:09:39 -08:00
|
|
|
void MemoryManager::BindRasterizer(VideoCore::RasterizerInterface* rasterizer_) {
|
|
|
|
rasterizer = rasterizer_;
|
2020-06-10 20:58:57 -07:00
|
|
|
}
|
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
GPUVAddr MemoryManager::UpdateRange(GPUVAddr gpu_addr, PageEntry page_entry, std::size_t size) {
|
|
|
|
u64 remaining_size{size};
|
|
|
|
for (u64 offset{}; offset < size; offset += page_size) {
|
|
|
|
if (remaining_size < page_size) {
|
|
|
|
SetPageEntry(gpu_addr + offset, page_entry + offset, remaining_size);
|
|
|
|
} else {
|
|
|
|
SetPageEntry(gpu_addr + offset, page_entry + offset);
|
|
|
|
}
|
|
|
|
remaining_size -= page_size;
|
|
|
|
}
|
2019-03-03 20:54:16 -08:00
|
|
|
return gpu_addr;
|
|
|
|
}
|
2018-02-07 18:54:35 -08:00
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
GPUVAddr MemoryManager::Map(VAddr cpu_addr, GPUVAddr gpu_addr, std::size_t size) {
|
2021-01-22 13:31:08 -08:00
|
|
|
const auto it = std::ranges::lower_bound(map_ranges, gpu_addr, {}, &MapRange::first);
|
|
|
|
if (it != map_ranges.end() && it->first == gpu_addr) {
|
|
|
|
it->second = size;
|
|
|
|
} else {
|
|
|
|
map_ranges.insert(it, MapRange{gpu_addr, size});
|
|
|
|
}
|
2020-07-25 21:16:21 -07:00
|
|
|
return UpdateRange(gpu_addr, cpu_addr, size);
|
|
|
|
}
|
2019-03-20 19:58:49 -07:00
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
GPUVAddr MemoryManager::MapAllocate(VAddr cpu_addr, std::size_t size, std::size_t align) {
|
|
|
|
return Map(cpu_addr, *FindFreeRange(size, align), size);
|
2019-03-03 20:54:16 -08:00
|
|
|
}
|
2018-10-31 19:20:37 -07:00
|
|
|
|
2020-10-26 20:07:36 -07:00
|
|
|
GPUVAddr MemoryManager::MapAllocate32(VAddr cpu_addr, std::size_t size) {
|
|
|
|
const std::optional<GPUVAddr> gpu_addr = FindFreeRange(size, 1, true);
|
|
|
|
ASSERT(gpu_addr);
|
|
|
|
return Map(cpu_addr, *gpu_addr, size);
|
|
|
|
}
|
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
void MemoryManager::Unmap(GPUVAddr gpu_addr, std::size_t size) {
|
2021-01-22 13:31:08 -08:00
|
|
|
if (size == 0) {
|
2020-07-25 21:16:21 -07:00
|
|
|
return;
|
|
|
|
}
|
2021-01-22 13:31:08 -08:00
|
|
|
const auto it = std::ranges::lower_bound(map_ranges, gpu_addr, {}, &MapRange::first);
|
|
|
|
if (it != map_ranges.end()) {
|
|
|
|
ASSERT(it->first == gpu_addr);
|
|
|
|
map_ranges.erase(it);
|
|
|
|
} else {
|
|
|
|
UNREACHABLE_MSG("Unmapping non-existent GPU address=0x{:x}", gpu_addr);
|
|
|
|
}
|
2020-07-25 21:16:21 -07:00
|
|
|
// Flush and invalidate through the GPU interface, to be asynchronous if possible.
|
2020-12-29 21:25:23 -08:00
|
|
|
const std::optional<VAddr> cpu_addr = GpuToCpuAddress(gpu_addr);
|
|
|
|
ASSERT(cpu_addr);
|
|
|
|
|
|
|
|
rasterizer->UnmapMemory(*cpu_addr, size);
|
2019-03-20 19:58:49 -07:00
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
UpdateRange(gpu_addr, PageEntry::State::Unmapped, size);
|
2019-03-03 20:54:16 -08:00
|
|
|
}
|
2018-07-24 08:19:51 -07:00
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
std::optional<GPUVAddr> MemoryManager::AllocateFixed(GPUVAddr gpu_addr, std::size_t size) {
|
|
|
|
for (u64 offset{}; offset < size; offset += page_size) {
|
|
|
|
if (!GetPageEntry(gpu_addr + offset).IsUnmapped()) {
|
2020-09-22 14:31:53 -07:00
|
|
|
return std::nullopt;
|
2020-07-25 21:16:21 -07:00
|
|
|
}
|
|
|
|
}
|
2018-10-31 19:20:37 -07:00
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
return UpdateRange(gpu_addr, PageEntry::State::Allocated, size);
|
|
|
|
}
|
2019-03-20 19:58:49 -07:00
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
GPUVAddr MemoryManager::Allocate(std::size_t size, std::size_t align) {
|
|
|
|
return *AllocateFixed(*FindFreeRange(size, align), size);
|
2018-02-07 18:54:35 -08:00
|
|
|
}
|
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
void MemoryManager::TryLockPage(PageEntry page_entry, std::size_t size) {
|
|
|
|
if (!page_entry.IsValid()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-24 08:19:51 -07:00
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
ASSERT(system.CurrentProcess()
|
|
|
|
->PageTable()
|
|
|
|
.LockForDeviceAddressSpace(page_entry.ToAddress(), size)
|
|
|
|
.IsSuccess());
|
|
|
|
}
|
2018-10-31 19:20:37 -07:00
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
void MemoryManager::TryUnlockPage(PageEntry page_entry, std::size_t size) {
|
|
|
|
if (!page_entry.IsValid()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-19 17:03:52 -08:00
|
|
|
|
2019-07-08 23:17:44 -07:00
|
|
|
ASSERT(system.CurrentProcess()
|
2020-04-08 19:51:31 -07:00
|
|
|
->PageTable()
|
2020-07-25 21:16:21 -07:00
|
|
|
.UnlockForDeviceAddressSpace(page_entry.ToAddress(), size)
|
2019-07-08 22:19:27 -07:00
|
|
|
.IsSuccess());
|
2018-02-07 18:54:35 -08:00
|
|
|
}
|
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
PageEntry MemoryManager::GetPageEntry(GPUVAddr gpu_addr) const {
|
|
|
|
return page_table[PageEntryIndex(gpu_addr)];
|
|
|
|
}
|
2019-04-05 17:22:53 -07:00
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
void MemoryManager::SetPageEntry(GPUVAddr gpu_addr, PageEntry page_entry, std::size_t size) {
|
|
|
|
// TODO(bunnei): We should lock/unlock device regions. This currently causes issues due to
|
|
|
|
// improper tracking, but should be fixed in the future.
|
2018-10-31 19:20:37 -07:00
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
//// Unlock the old page
|
|
|
|
// TryUnlockPage(page_table[PageEntryIndex(gpu_addr)], size);
|
2018-02-07 18:54:35 -08:00
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
//// Lock the new page
|
|
|
|
// TryLockPage(page_entry, size);
|
2018-02-07 18:54:35 -08:00
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
page_table[PageEntryIndex(gpu_addr)] = page_entry;
|
2019-03-09 11:06:51 -08:00
|
|
|
}
|
|
|
|
|
2020-10-26 20:07:36 -07:00
|
|
|
std::optional<GPUVAddr> MemoryManager::FindFreeRange(std::size_t size, std::size_t align,
|
|
|
|
bool start_32bit_address) const {
|
2020-07-25 21:16:21 -07:00
|
|
|
if (!align) {
|
|
|
|
align = page_size;
|
|
|
|
} else {
|
|
|
|
align = Common::AlignUp(align, page_size);
|
2019-03-09 11:06:51 -08:00
|
|
|
}
|
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
u64 available_size{};
|
2020-10-26 20:07:36 -07:00
|
|
|
GPUVAddr gpu_addr{start_32bit_address ? address_space_start_low : address_space_start};
|
2020-07-25 21:16:21 -07:00
|
|
|
while (gpu_addr + available_size < address_space_size) {
|
|
|
|
if (GetPageEntry(gpu_addr + available_size).IsUnmapped()) {
|
|
|
|
available_size += page_size;
|
|
|
|
|
|
|
|
if (available_size >= size) {
|
|
|
|
return gpu_addr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gpu_addr += available_size + page_size;
|
|
|
|
available_size = 0;
|
|
|
|
|
|
|
|
const auto remainder{gpu_addr % align};
|
|
|
|
if (remainder) {
|
|
|
|
gpu_addr = (gpu_addr - remainder) + align;
|
|
|
|
}
|
|
|
|
}
|
2018-10-31 19:20:37 -07:00
|
|
|
}
|
|
|
|
|
2020-09-22 14:31:53 -07:00
|
|
|
return std::nullopt;
|
2019-03-03 20:54:16 -08:00
|
|
|
}
|
2018-10-31 19:20:37 -07:00
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
std::optional<VAddr> MemoryManager::GpuToCpuAddress(GPUVAddr gpu_addr) const {
|
|
|
|
const auto page_entry{GetPageEntry(gpu_addr)};
|
|
|
|
if (!page_entry.IsValid()) {
|
2020-09-22 14:31:53 -07:00
|
|
|
return std::nullopt;
|
2019-03-09 11:06:51 -08:00
|
|
|
}
|
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
return page_entry.ToAddress() + (gpu_addr & page_mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T MemoryManager::Read(GPUVAddr addr) const {
|
|
|
|
if (auto page_pointer{GetPointer(addr)}; page_pointer) {
|
2019-03-03 20:54:16 -08:00
|
|
|
// NOTE: Avoid adding any extra logic to this fast-path block
|
|
|
|
T value;
|
2020-04-08 09:08:06 -07:00
|
|
|
std::memcpy(&value, page_pointer, sizeof(T));
|
2019-03-03 20:54:16 -08:00
|
|
|
return value;
|
2018-02-07 18:54:35 -08:00
|
|
|
}
|
|
|
|
|
2020-04-08 19:51:31 -07:00
|
|
|
UNREACHABLE();
|
|
|
|
|
2019-03-03 20:54:16 -08:00
|
|
|
return {};
|
2018-02-07 18:54:35 -08:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:54:16 -08:00
|
|
|
template <typename T>
|
2019-03-09 11:06:51 -08:00
|
|
|
void MemoryManager::Write(GPUVAddr addr, T data) {
|
2020-07-25 21:16:21 -07:00
|
|
|
if (auto page_pointer{GetPointer(addr)}; page_pointer) {
|
2019-03-03 20:54:16 -08:00
|
|
|
// NOTE: Avoid adding any extra logic to this fast-path block
|
2020-04-08 09:08:06 -07:00
|
|
|
std::memcpy(page_pointer, &data, sizeof(T));
|
2019-03-03 20:54:16 -08:00
|
|
|
return;
|
|
|
|
}
|
2018-05-20 12:21:06 -07:00
|
|
|
|
2020-04-08 19:51:31 -07:00
|
|
|
UNREACHABLE();
|
2019-03-03 20:54:16 -08:00
|
|
|
}
|
2018-07-24 08:19:51 -07:00
|
|
|
|
2019-04-05 17:30:46 -07:00
|
|
|
template u8 MemoryManager::Read<u8>(GPUVAddr addr) const;
|
|
|
|
template u16 MemoryManager::Read<u16>(GPUVAddr addr) const;
|
|
|
|
template u32 MemoryManager::Read<u32>(GPUVAddr addr) const;
|
|
|
|
template u64 MemoryManager::Read<u64>(GPUVAddr addr) const;
|
2019-03-03 20:54:16 -08:00
|
|
|
template void MemoryManager::Write<u8>(GPUVAddr addr, u8 data);
|
|
|
|
template void MemoryManager::Write<u16>(GPUVAddr addr, u16 data);
|
|
|
|
template void MemoryManager::Write<u32>(GPUVAddr addr, u32 data);
|
|
|
|
template void MemoryManager::Write<u64>(GPUVAddr addr, u64 data);
|
2018-10-31 19:20:37 -07:00
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
u8* MemoryManager::GetPointer(GPUVAddr gpu_addr) {
|
|
|
|
if (!GetPageEntry(gpu_addr).IsValid()) {
|
2019-03-09 11:06:51 -08:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
const auto address{GpuToCpuAddress(gpu_addr)};
|
|
|
|
if (!address) {
|
|
|
|
return {};
|
2019-04-05 17:25:25 -07:00
|
|
|
}
|
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
return system.Memory().GetPointer(*address);
|
2019-04-05 17:25:25 -07:00
|
|
|
}
|
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
const u8* MemoryManager::GetPointer(GPUVAddr gpu_addr) const {
|
|
|
|
if (!GetPageEntry(gpu_addr).IsValid()) {
|
2019-04-05 17:25:25 -07:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
const auto address{GpuToCpuAddress(gpu_addr)};
|
|
|
|
if (!address) {
|
|
|
|
return {};
|
2018-05-20 12:21:06 -07:00
|
|
|
}
|
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
return system.Memory().GetPointer(*address);
|
2019-04-16 12:45:24 -07:00
|
|
|
}
|
|
|
|
|
2021-01-22 13:31:08 -08:00
|
|
|
size_t MemoryManager::BytesToMapEnd(GPUVAddr gpu_addr) const noexcept {
|
|
|
|
auto it = std::ranges::upper_bound(map_ranges, gpu_addr, {}, &MapRange::first);
|
|
|
|
--it;
|
|
|
|
return it->second - (gpu_addr - it->first);
|
|
|
|
}
|
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
void MemoryManager::ReadBlock(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size) const {
|
2019-03-28 18:58:28 -07:00
|
|
|
std::size_t remaining_size{size};
|
2020-06-19 19:02:56 -07:00
|
|
|
std::size_t page_index{gpu_src_addr >> page_bits};
|
|
|
|
std::size_t page_offset{gpu_src_addr & page_mask};
|
2019-03-28 18:58:28 -07:00
|
|
|
|
|
|
|
while (remaining_size > 0) {
|
|
|
|
const std::size_t copy_amount{
|
|
|
|
std::min(static_cast<std::size_t>(page_size) - page_offset, remaining_size)};
|
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
if (const auto page_addr{GpuToCpuAddress(page_index << page_bits)}; page_addr) {
|
|
|
|
const auto src_addr{*page_addr + page_offset};
|
|
|
|
|
|
|
|
// Flush must happen on the rasterizer interface, such that memory is always synchronous
|
|
|
|
// when it is read (even when in asynchronous GPU mode). Fixes Dead Cells title menu.
|
2020-06-10 20:58:57 -07:00
|
|
|
rasterizer->FlushRegion(src_addr, copy_amount);
|
2020-07-25 21:16:21 -07:00
|
|
|
system.Memory().ReadBlockUnsafe(src_addr, dest_buffer, copy_amount);
|
|
|
|
}
|
2019-03-28 18:58:28 -07:00
|
|
|
|
|
|
|
page_index++;
|
|
|
|
page_offset = 0;
|
|
|
|
dest_buffer = static_cast<u8*>(dest_buffer) + copy_amount;
|
|
|
|
remaining_size -= copy_amount;
|
|
|
|
}
|
2019-03-03 20:54:16 -08:00
|
|
|
}
|
2019-03-28 18:58:28 -07:00
|
|
|
|
2020-06-19 19:02:56 -07:00
|
|
|
void MemoryManager::ReadBlockUnsafe(GPUVAddr gpu_src_addr, void* dest_buffer,
|
2019-04-16 07:11:35 -07:00
|
|
|
const std::size_t size) const {
|
2019-04-15 20:01:35 -07:00
|
|
|
std::size_t remaining_size{size};
|
2020-06-19 19:02:56 -07:00
|
|
|
std::size_t page_index{gpu_src_addr >> page_bits};
|
|
|
|
std::size_t page_offset{gpu_src_addr & page_mask};
|
2019-04-15 20:01:35 -07:00
|
|
|
|
|
|
|
while (remaining_size > 0) {
|
|
|
|
const std::size_t copy_amount{
|
|
|
|
std::min(static_cast<std::size_t>(page_size) - page_offset, remaining_size)};
|
2020-07-25 21:16:21 -07:00
|
|
|
|
|
|
|
if (const auto page_addr{GpuToCpuAddress(page_index << page_bits)}; page_addr) {
|
|
|
|
const auto src_addr{*page_addr + page_offset};
|
|
|
|
system.Memory().ReadBlockUnsafe(src_addr, dest_buffer, copy_amount);
|
2019-04-19 17:35:54 -07:00
|
|
|
} else {
|
|
|
|
std::memset(dest_buffer, 0, copy_amount);
|
|
|
|
}
|
2020-07-25 21:16:21 -07:00
|
|
|
|
2019-04-15 20:01:35 -07:00
|
|
|
page_index++;
|
|
|
|
page_offset = 0;
|
|
|
|
dest_buffer = static_cast<u8*>(dest_buffer) + copy_amount;
|
|
|
|
remaining_size -= copy_amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
void MemoryManager::WriteBlock(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size) {
|
2019-03-28 18:58:28 -07:00
|
|
|
std::size_t remaining_size{size};
|
2020-06-19 19:02:56 -07:00
|
|
|
std::size_t page_index{gpu_dest_addr >> page_bits};
|
|
|
|
std::size_t page_offset{gpu_dest_addr & page_mask};
|
2019-03-28 18:58:28 -07:00
|
|
|
|
|
|
|
while (remaining_size > 0) {
|
|
|
|
const std::size_t copy_amount{
|
|
|
|
std::min(static_cast<std::size_t>(page_size) - page_offset, remaining_size)};
|
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
if (const auto page_addr{GpuToCpuAddress(page_index << page_bits)}; page_addr) {
|
|
|
|
const auto dest_addr{*page_addr + page_offset};
|
|
|
|
|
|
|
|
// Invalidate must happen on the rasterizer interface, such that memory is always
|
|
|
|
// synchronous when it is written (even when in asynchronous GPU mode).
|
2020-06-10 20:58:57 -07:00
|
|
|
rasterizer->InvalidateRegion(dest_addr, copy_amount);
|
2020-07-25 21:16:21 -07:00
|
|
|
system.Memory().WriteBlockUnsafe(dest_addr, src_buffer, copy_amount);
|
|
|
|
}
|
2019-03-28 18:58:28 -07:00
|
|
|
|
|
|
|
page_index++;
|
|
|
|
page_offset = 0;
|
|
|
|
src_buffer = static_cast<const u8*>(src_buffer) + copy_amount;
|
|
|
|
remaining_size -= copy_amount;
|
|
|
|
}
|
2019-03-03 20:54:16 -08:00
|
|
|
}
|
|
|
|
|
2020-06-19 19:02:56 -07:00
|
|
|
void MemoryManager::WriteBlockUnsafe(GPUVAddr gpu_dest_addr, const void* src_buffer,
|
2020-07-25 21:16:21 -07:00
|
|
|
std::size_t size) {
|
2019-04-15 20:01:35 -07:00
|
|
|
std::size_t remaining_size{size};
|
2020-06-19 19:02:56 -07:00
|
|
|
std::size_t page_index{gpu_dest_addr >> page_bits};
|
|
|
|
std::size_t page_offset{gpu_dest_addr & page_mask};
|
2019-04-15 20:01:35 -07:00
|
|
|
|
|
|
|
while (remaining_size > 0) {
|
|
|
|
const std::size_t copy_amount{
|
|
|
|
std::min(static_cast<std::size_t>(page_size) - page_offset, remaining_size)};
|
2020-07-25 21:16:21 -07:00
|
|
|
|
|
|
|
if (const auto page_addr{GpuToCpuAddress(page_index << page_bits)}; page_addr) {
|
|
|
|
const auto dest_addr{*page_addr + page_offset};
|
|
|
|
system.Memory().WriteBlockUnsafe(dest_addr, src_buffer, copy_amount);
|
2019-04-19 17:35:54 -07:00
|
|
|
}
|
2020-07-25 21:16:21 -07:00
|
|
|
|
2019-04-15 20:01:35 -07:00
|
|
|
page_index++;
|
|
|
|
page_offset = 0;
|
|
|
|
src_buffer = static_cast<const u8*>(src_buffer) + copy_amount;
|
|
|
|
remaining_size -= copy_amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 14:08:15 -08:00
|
|
|
void MemoryManager::FlushRegion(GPUVAddr gpu_addr, size_t size) const {
|
|
|
|
size_t remaining_size{size};
|
|
|
|
size_t page_index{gpu_addr >> page_bits};
|
|
|
|
size_t page_offset{gpu_addr & page_mask};
|
|
|
|
while (remaining_size > 0) {
|
|
|
|
const size_t num_bytes{std::min(page_size - page_offset, remaining_size)};
|
|
|
|
if (const auto page_addr{GpuToCpuAddress(page_index << page_bits)}; page_addr) {
|
|
|
|
rasterizer->FlushRegion(*page_addr + page_offset, num_bytes);
|
|
|
|
}
|
|
|
|
++page_index;
|
|
|
|
page_offset = 0;
|
|
|
|
remaining_size -= num_bytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-25 21:16:21 -07:00
|
|
|
void MemoryManager::CopyBlock(GPUVAddr gpu_dest_addr, GPUVAddr gpu_src_addr, std::size_t size) {
|
2020-04-08 09:08:06 -07:00
|
|
|
std::vector<u8> tmp_buffer(size);
|
2020-06-19 19:02:56 -07:00
|
|
|
ReadBlock(gpu_src_addr, tmp_buffer.data(), size);
|
2021-01-21 14:08:43 -08:00
|
|
|
|
|
|
|
// The output block must be flushed in case it has data modified from the GPU.
|
|
|
|
// Fixes NPC geometry in Zombie Panic in Wonderland DX
|
|
|
|
FlushRegion(gpu_dest_addr, size);
|
2020-06-19 19:02:56 -07:00
|
|
|
WriteBlock(gpu_dest_addr, tmp_buffer.data(), size);
|
2018-10-12 18:52:16 -07:00
|
|
|
}
|
|
|
|
|
2020-08-23 21:37:54 -07:00
|
|
|
bool MemoryManager::IsGranularRange(GPUVAddr gpu_addr, std::size_t size) const {
|
2020-07-25 21:16:21 -07:00
|
|
|
const auto cpu_addr{GpuToCpuAddress(gpu_addr)};
|
|
|
|
if (!cpu_addr) {
|
2020-08-23 21:37:54 -07:00
|
|
|
return false;
|
2019-03-03 20:54:16 -08:00
|
|
|
}
|
2020-07-25 21:16:21 -07:00
|
|
|
const std::size_t page{(*cpu_addr & Core::Memory::PAGE_MASK) + size};
|
|
|
|
return page <= Core::Memory::PAGE_SIZE;
|
2018-02-07 18:54:35 -08:00
|
|
|
}
|
|
|
|
|
2018-02-11 20:44:12 -08:00
|
|
|
} // namespace Tegra
|