Buffer Cache: Use vAddr instead of physical memory.

This commit is contained in:
Fernando Sahmkow 2020-04-05 17:23:49 -04:00
parent 6ee316cb8f
commit 7fcd0fee6d
12 changed files with 254 additions and 106 deletions

View File

@ -257,10 +257,59 @@ struct Memory::Impl {
} }
} }
void ReadBlockUnsafe(const Kernel::Process& process, const VAddr src_addr, void* dest_buffer,
const std::size_t size) {
const auto& page_table = process.VMManager().page_table;
std::size_t remaining_size = size;
std::size_t page_index = src_addr >> PAGE_BITS;
std::size_t page_offset = src_addr & PAGE_MASK;
while (remaining_size > 0) {
const std::size_t copy_amount =
std::min(static_cast<std::size_t>(PAGE_SIZE) - page_offset, remaining_size);
const auto current_vaddr = static_cast<VAddr>((page_index << PAGE_BITS) + page_offset);
switch (page_table.attributes[page_index]) {
case Common::PageType::Unmapped: {
LOG_ERROR(HW_Memory,
"Unmapped ReadBlock @ 0x{:016X} (start address = 0x{:016X}, size = {})",
current_vaddr, src_addr, size);
std::memset(dest_buffer, 0, copy_amount);
break;
}
case Common::PageType::Memory: {
DEBUG_ASSERT(page_table.pointers[page_index]);
const u8* const src_ptr =
page_table.pointers[page_index] + page_offset + (page_index << PAGE_BITS);
std::memcpy(dest_buffer, src_ptr, copy_amount);
break;
}
case Common::PageType::RasterizerCachedMemory: {
const u8* const host_ptr = GetPointerFromVMA(process, current_vaddr);
std::memcpy(dest_buffer, host_ptr, copy_amount);
break;
}
default:
UNREACHABLE();
}
page_index++;
page_offset = 0;
dest_buffer = static_cast<u8*>(dest_buffer) + copy_amount;
remaining_size -= copy_amount;
}
}
void ReadBlock(const VAddr src_addr, void* dest_buffer, const std::size_t size) { void ReadBlock(const VAddr src_addr, void* dest_buffer, const std::size_t size) {
ReadBlock(*system.CurrentProcess(), src_addr, dest_buffer, size); ReadBlock(*system.CurrentProcess(), src_addr, dest_buffer, size);
} }
void ReadBlockUnsafe(const VAddr src_addr, void* dest_buffer, const std::size_t size) {
ReadBlockUnsafe(*system.CurrentProcess(), src_addr, dest_buffer, size);
}
void WriteBlock(const Kernel::Process& process, const VAddr dest_addr, const void* src_buffer, void WriteBlock(const Kernel::Process& process, const VAddr dest_addr, const void* src_buffer,
const std::size_t size) { const std::size_t size) {
const auto& page_table = process.VMManager().page_table; const auto& page_table = process.VMManager().page_table;
@ -305,10 +354,57 @@ struct Memory::Impl {
} }
} }
void WriteBlockUnsafe(const Kernel::Process& process, const VAddr dest_addr,
const void* src_buffer, const std::size_t size) {
const auto& page_table = process.VMManager().page_table;
std::size_t remaining_size = size;
std::size_t page_index = dest_addr >> PAGE_BITS;
std::size_t page_offset = dest_addr & PAGE_MASK;
while (remaining_size > 0) {
const std::size_t copy_amount =
std::min(static_cast<std::size_t>(PAGE_SIZE) - page_offset, remaining_size);
const auto current_vaddr = static_cast<VAddr>((page_index << PAGE_BITS) + page_offset);
switch (page_table.attributes[page_index]) {
case Common::PageType::Unmapped: {
LOG_ERROR(HW_Memory,
"Unmapped WriteBlock @ 0x{:016X} (start address = 0x{:016X}, size = {})",
current_vaddr, dest_addr, size);
break;
}
case Common::PageType::Memory: {
DEBUG_ASSERT(page_table.pointers[page_index]);
u8* const dest_ptr =
page_table.pointers[page_index] + page_offset + (page_index << PAGE_BITS);
std::memcpy(dest_ptr, src_buffer, copy_amount);
break;
}
case Common::PageType::RasterizerCachedMemory: {
u8* const host_ptr = GetPointerFromVMA(process, current_vaddr);
std::memcpy(host_ptr, src_buffer, copy_amount);
break;
}
default:
UNREACHABLE();
}
page_index++;
page_offset = 0;
src_buffer = static_cast<const u8*>(src_buffer) + copy_amount;
remaining_size -= copy_amount;
}
}
void WriteBlock(const VAddr dest_addr, const void* src_buffer, const std::size_t size) { void WriteBlock(const VAddr dest_addr, const void* src_buffer, const std::size_t size) {
WriteBlock(*system.CurrentProcess(), dest_addr, src_buffer, size); WriteBlock(*system.CurrentProcess(), dest_addr, src_buffer, size);
} }
void WriteBlockUnsafe(const VAddr dest_addr, const void* src_buffer, const std::size_t size) {
WriteBlockUnsafe(*system.CurrentProcess(), dest_addr, src_buffer, size);
}
void ZeroBlock(const Kernel::Process& process, const VAddr dest_addr, const std::size_t size) { void ZeroBlock(const Kernel::Process& process, const VAddr dest_addr, const std::size_t size) {
const auto& page_table = process.VMManager().page_table; const auto& page_table = process.VMManager().page_table;
std::size_t remaining_size = size; std::size_t remaining_size = size;
@ -696,6 +792,15 @@ void Memory::ReadBlock(const VAddr src_addr, void* dest_buffer, const std::size_
impl->ReadBlock(src_addr, dest_buffer, size); impl->ReadBlock(src_addr, dest_buffer, size);
} }
void Memory::ReadBlockUnsafe(const Kernel::Process& process, const VAddr src_addr,
void* dest_buffer, const std::size_t size) {
impl->ReadBlockUnsafe(process, src_addr, dest_buffer, size);
}
void Memory::ReadBlockUnsafe(const VAddr src_addr, void* dest_buffer, const std::size_t size) {
impl->ReadBlockUnsafe(src_addr, dest_buffer, size);
}
void Memory::WriteBlock(const Kernel::Process& process, VAddr dest_addr, const void* src_buffer, void Memory::WriteBlock(const Kernel::Process& process, VAddr dest_addr, const void* src_buffer,
std::size_t size) { std::size_t size) {
impl->WriteBlock(process, dest_addr, src_buffer, size); impl->WriteBlock(process, dest_addr, src_buffer, size);
@ -705,6 +810,16 @@ void Memory::WriteBlock(const VAddr dest_addr, const void* src_buffer, const std
impl->WriteBlock(dest_addr, src_buffer, size); impl->WriteBlock(dest_addr, src_buffer, size);
} }
void Memory::WriteBlockUnsafe(const Kernel::Process& process, VAddr dest_addr,
const void* src_buffer, std::size_t size) {
impl->WriteBlockUnsafe(process, dest_addr, src_buffer, size);
}
void Memory::WriteBlockUnsafe(const VAddr dest_addr, const void* src_buffer,
const std::size_t size) {
impl->WriteBlockUnsafe(dest_addr, src_buffer, size);
}
void Memory::ZeroBlock(const Kernel::Process& process, VAddr dest_addr, std::size_t size) { void Memory::ZeroBlock(const Kernel::Process& process, VAddr dest_addr, std::size_t size) {
impl->ZeroBlock(process, dest_addr, size); impl->ZeroBlock(process, dest_addr, size);
} }

View File

@ -294,6 +294,9 @@ public:
void ReadBlock(const Kernel::Process& process, VAddr src_addr, void* dest_buffer, void ReadBlock(const Kernel::Process& process, VAddr src_addr, void* dest_buffer,
std::size_t size); std::size_t size);
void ReadBlockUnsafe(const Kernel::Process& process, VAddr src_addr, void* dest_buffer,
std::size_t size);
/** /**
* Reads a contiguous block of bytes from the current process' address space. * Reads a contiguous block of bytes from the current process' address space.
* *
@ -312,6 +315,8 @@ public:
*/ */
void ReadBlock(VAddr src_addr, void* dest_buffer, std::size_t size); void ReadBlock(VAddr src_addr, void* dest_buffer, std::size_t size);
void ReadBlockUnsafe(VAddr src_addr, void* dest_buffer, std::size_t size);
/** /**
* Writes a range of bytes into a given process' address space at the specified * Writes a range of bytes into a given process' address space at the specified
* virtual address. * virtual address.
@ -335,6 +340,9 @@ public:
void WriteBlock(const Kernel::Process& process, VAddr dest_addr, const void* src_buffer, void WriteBlock(const Kernel::Process& process, VAddr dest_addr, const void* src_buffer,
std::size_t size); std::size_t size);
void WriteBlockUnsafe(const Kernel::Process& process, VAddr dest_addr, const void* src_buffer,
std::size_t size);
/** /**
* Writes a range of bytes into the current process' address space at the specified * Writes a range of bytes into the current process' address space at the specified
* virtual address. * virtual address.
@ -356,6 +364,8 @@ public:
*/ */
void WriteBlock(VAddr dest_addr, const void* src_buffer, std::size_t size); void WriteBlock(VAddr dest_addr, const void* src_buffer, std::size_t size);
void WriteBlockUnsafe(VAddr dest_addr, const void* src_buffer, std::size_t size);
/** /**
* Fills the specified address range within a process' address space with zeroes. * Fills the specified address range within a process' address space with zeroes.
* *

View File

@ -15,37 +15,29 @@ namespace VideoCommon {
class BufferBlock { class BufferBlock {
public: public:
bool Overlaps(const CacheAddr start, const CacheAddr end) const { bool Overlaps(const VAddr start, const VAddr end) const {
return (cache_addr < end) && (cache_addr_end > start); return (cpu_addr < end) && (cpu_addr_end > start);
} }
bool IsInside(const CacheAddr other_start, const CacheAddr other_end) const { bool IsInside(const VAddr other_start, const VAddr other_end) const {
return cache_addr <= other_start && other_end <= cache_addr_end; return cpu_addr <= other_start && other_end <= cpu_addr_end;
} }
u8* GetWritableHostPtr() const { std::size_t GetOffset(const VAddr in_addr) {
return FromCacheAddr(cache_addr); return static_cast<std::size_t>(in_addr - cpu_addr);
} }
u8* GetWritableHostPtr(std::size_t offset) const { VAddr GetCpuAddr() const {
return FromCacheAddr(cache_addr + offset); return cpu_addr;
} }
std::size_t GetOffset(const CacheAddr in_addr) { VAddr GetCpuAddrEnd() const {
return static_cast<std::size_t>(in_addr - cache_addr); return cpu_addr_end;
} }
CacheAddr GetCacheAddr() const { void SetCpuAddr(const VAddr new_addr) {
return cache_addr; cpu_addr = new_addr;
} cpu_addr_end = new_addr + size;
CacheAddr GetCacheAddrEnd() const {
return cache_addr_end;
}
void SetCacheAddr(const CacheAddr new_addr) {
cache_addr = new_addr;
cache_addr_end = new_addr + size;
} }
std::size_t GetSize() const { std::size_t GetSize() const {
@ -61,14 +53,14 @@ public:
} }
protected: protected:
explicit BufferBlock(CacheAddr cache_addr, const std::size_t size) : size{size} { explicit BufferBlock(VAddr cpu_addr, const std::size_t size) : size{size} {
SetCacheAddr(cache_addr); SetCpuAddr(cpu_addr);
} }
~BufferBlock() = default; ~BufferBlock() = default;
private: private:
CacheAddr cache_addr{}; VAddr cpu_addr{};
CacheAddr cache_addr_end{}; VAddr cpu_addr_end{};
std::size_t size{}; std::size_t size{};
u64 epoch{}; u64 epoch{};
}; };

View File

@ -37,28 +37,45 @@ public:
bool is_written = false, bool use_fast_cbuf = false) { bool is_written = false, bool use_fast_cbuf = false) {
std::lock_guard lock{mutex}; std::lock_guard lock{mutex};
auto& memory_manager = system.GPU().MemoryManager(); const std::optional<VAddr> cpu_addr_opt =
const auto host_ptr = memory_manager.GetPointer(gpu_addr); system.GPU().MemoryManager().GpuToCpuAddress(gpu_addr);
if (!host_ptr) {
if (!cpu_addr_opt) {
return {GetEmptyBuffer(size), 0}; return {GetEmptyBuffer(size), 0};
} }
const auto cache_addr = ToCacheAddr(host_ptr);
VAddr cpu_addr = *cpu_addr_opt;
// Cache management is a big overhead, so only cache entries with a given size. // Cache management is a big overhead, so only cache entries with a given size.
// TODO: Figure out which size is the best for given games. // TODO: Figure out which size is the best for given games.
constexpr std::size_t max_stream_size = 0x800; constexpr std::size_t max_stream_size = 0x800;
if (use_fast_cbuf || size < max_stream_size) { if (use_fast_cbuf || size < max_stream_size) {
if (!is_written && !IsRegionWritten(cache_addr, cache_addr + size - 1)) { if (!is_written && !IsRegionWritten(cpu_addr, cpu_addr + size - 1)) {
auto& memory_manager = system.GPU().MemoryManager();
if (use_fast_cbuf) { if (use_fast_cbuf) {
return ConstBufferUpload(host_ptr, size); if (Tegra::MemoryManager::IsGranularRange(gpu_addr, size)) {
const auto host_ptr = memory_manager.GetPointer(gpu_addr);
return ConstBufferUpload(host_ptr, size);
} else {
staging_buffer.resize(size);
memory_manager.ReadBlockUnsafe(gpu_addr, staging_buffer.data(), size);
return ConstBufferUpload(staging_buffer.data(), size);
}
} else { } else {
return StreamBufferUpload(host_ptr, size, alignment); if (Tegra::MemoryManager::IsGranularRange(gpu_addr, size)) {
const auto host_ptr = memory_manager.GetPointer(gpu_addr);
return StreamBufferUpload(host_ptr, size, alignment);
} else {
staging_buffer.resize(size);
memory_manager.ReadBlockUnsafe(gpu_addr, staging_buffer.data(), size);
return StreamBufferUpload(staging_buffer.data(), size, alignment);
}
} }
} }
} }
auto block = GetBlock(cache_addr, size); auto block = GetBlock(cpu_addr, size);
auto map = MapAddress(block, gpu_addr, cache_addr, size); auto map = MapAddress(block, gpu_addr, cpu_addr, size);
if (is_written) { if (is_written) {
map->MarkAsModified(true, GetModifiedTicks()); map->MarkAsModified(true, GetModifiedTicks());
if (!map->IsWritten()) { if (!map->IsWritten()) {
@ -71,7 +88,7 @@ public:
} }
} }
const u64 offset = static_cast<u64>(block->GetOffset(cache_addr)); const u64 offset = static_cast<u64>(block->GetOffset(cpu_addr));
return {ToHandle(block), offset}; return {ToHandle(block), offset};
} }
@ -112,7 +129,7 @@ public:
} }
/// Write any cached resources overlapping the specified region back to memory /// Write any cached resources overlapping the specified region back to memory
void FlushRegion(CacheAddr addr, std::size_t size) { void FlushRegion(VAddr addr, std::size_t size) {
std::lock_guard lock{mutex}; std::lock_guard lock{mutex};
std::vector<MapInterval> objects = GetMapsInRange(addr, size); std::vector<MapInterval> objects = GetMapsInRange(addr, size);
@ -127,7 +144,7 @@ public:
} }
/// Mark the specified region as being invalidated /// Mark the specified region as being invalidated
void InvalidateRegion(CacheAddr addr, u64 size) { void InvalidateRegion(VAddr addr, u64 size) {
std::lock_guard lock{mutex}; std::lock_guard lock{mutex};
std::vector<MapInterval> objects = GetMapsInRange(addr, size); std::vector<MapInterval> objects = GetMapsInRange(addr, size);
@ -152,7 +169,7 @@ protected:
virtual void WriteBarrier() = 0; virtual void WriteBarrier() = 0;
virtual TBuffer CreateBlock(CacheAddr cache_addr, std::size_t size) = 0; virtual TBuffer CreateBlock(VAddr cpu_addr, std::size_t size) = 0;
virtual void UploadBlockData(const TBuffer& buffer, std::size_t offset, std::size_t size, virtual void UploadBlockData(const TBuffer& buffer, std::size_t offset, std::size_t size,
const u8* data) = 0; const u8* data) = 0;
@ -169,20 +186,17 @@ protected:
/// Register an object into the cache /// Register an object into the cache
void Register(const MapInterval& new_map, bool inherit_written = false) { void Register(const MapInterval& new_map, bool inherit_written = false) {
const CacheAddr cache_ptr = new_map->GetStart(); const VAddr cpu_addr = new_map->GetStart();
const std::optional<VAddr> cpu_addr = if (!cpu_addr) {
system.GPU().MemoryManager().GpuToCpuAddress(new_map->GetGpuAddress());
if (!cache_ptr || !cpu_addr) {
LOG_CRITICAL(HW_GPU, "Failed to register buffer with unmapped gpu_address 0x{:016x}", LOG_CRITICAL(HW_GPU, "Failed to register buffer with unmapped gpu_address 0x{:016x}",
new_map->GetGpuAddress()); new_map->GetGpuAddress());
return; return;
} }
const std::size_t size = new_map->GetEnd() - new_map->GetStart(); const std::size_t size = new_map->GetEnd() - new_map->GetStart();
new_map->SetCpuAddress(*cpu_addr);
new_map->MarkAsRegistered(true); new_map->MarkAsRegistered(true);
const IntervalType interval{new_map->GetStart(), new_map->GetEnd()}; const IntervalType interval{new_map->GetStart(), new_map->GetEnd()};
mapped_addresses.insert({interval, new_map}); mapped_addresses.insert({interval, new_map});
rasterizer.UpdatePagesCachedCount(*cpu_addr, size, 1); rasterizer.UpdatePagesCachedCount(cpu_addr, size, 1);
if (inherit_written) { if (inherit_written) {
MarkRegionAsWritten(new_map->GetStart(), new_map->GetEnd() - 1); MarkRegionAsWritten(new_map->GetStart(), new_map->GetEnd() - 1);
new_map->MarkAsWritten(true); new_map->MarkAsWritten(true);
@ -192,7 +206,7 @@ protected:
/// Unregisters an object from the cache /// Unregisters an object from the cache
void Unregister(MapInterval& map) { void Unregister(MapInterval& map) {
const std::size_t size = map->GetEnd() - map->GetStart(); const std::size_t size = map->GetEnd() - map->GetStart();
rasterizer.UpdatePagesCachedCount(map->GetCpuAddress(), size, -1); rasterizer.UpdatePagesCachedCount(map->GetStart(), size, -1);
map->MarkAsRegistered(false); map->MarkAsRegistered(false);
if (map->IsWritten()) { if (map->IsWritten()) {
UnmarkRegionAsWritten(map->GetStart(), map->GetEnd() - 1); UnmarkRegionAsWritten(map->GetStart(), map->GetEnd() - 1);
@ -202,32 +216,39 @@ protected:
} }
private: private:
MapInterval CreateMap(const CacheAddr start, const CacheAddr end, const GPUVAddr gpu_addr) { MapInterval CreateMap(const VAddr start, const VAddr end, const GPUVAddr gpu_addr) {
return std::make_shared<MapIntervalBase>(start, end, gpu_addr); return std::make_shared<MapIntervalBase>(start, end, gpu_addr);
} }
MapInterval MapAddress(const TBuffer& block, const GPUVAddr gpu_addr, MapInterval MapAddress(const TBuffer& block, const GPUVAddr gpu_addr, const VAddr cpu_addr,
const CacheAddr cache_addr, const std::size_t size) { const std::size_t size) {
std::vector<MapInterval> overlaps = GetMapsInRange(cache_addr, size); std::vector<MapInterval> overlaps = GetMapsInRange(cpu_addr, size);
if (overlaps.empty()) { if (overlaps.empty()) {
const CacheAddr cache_addr_end = cache_addr + size; auto& memory_manager = system.GPU().MemoryManager();
MapInterval new_map = CreateMap(cache_addr, cache_addr_end, gpu_addr); const VAddr cpu_addr_end = cpu_addr + size;
u8* host_ptr = FromCacheAddr(cache_addr); MapInterval new_map = CreateMap(cpu_addr, cpu_addr_end, gpu_addr);
UploadBlockData(block, block->GetOffset(cache_addr), size, host_ptr); if (Tegra::MemoryManager::IsGranularRange(gpu_addr, size)) {
u8* host_ptr = memory_manager.GetPointer(gpu_addr);
UploadBlockData(block, block->GetOffset(cpu_addr), size, host_ptr);
} else {
staging_buffer.resize(size);
memory_manager.ReadBlockUnsafe(gpu_addr, staging_buffer.data(), size);
UploadBlockData(block, block->GetOffset(cpu_addr), size, staging_buffer.data());
}
Register(new_map); Register(new_map);
return new_map; return new_map;
} }
const CacheAddr cache_addr_end = cache_addr + size; const VAddr cpu_addr_end = cpu_addr + size;
if (overlaps.size() == 1) { if (overlaps.size() == 1) {
MapInterval& current_map = overlaps[0]; MapInterval& current_map = overlaps[0];
if (current_map->IsInside(cache_addr, cache_addr_end)) { if (current_map->IsInside(cpu_addr, cpu_addr_end)) {
return current_map; return current_map;
} }
} }
CacheAddr new_start = cache_addr; VAddr new_start = cpu_addr;
CacheAddr new_end = cache_addr_end; VAddr new_end = cpu_addr_end;
bool write_inheritance = false; bool write_inheritance = false;
bool modified_inheritance = false; bool modified_inheritance = false;
// Calculate new buffer parameters // Calculate new buffer parameters
@ -237,7 +258,7 @@ private:
write_inheritance |= overlap->IsWritten(); write_inheritance |= overlap->IsWritten();
modified_inheritance |= overlap->IsModified(); modified_inheritance |= overlap->IsModified();
} }
GPUVAddr new_gpu_addr = gpu_addr + new_start - cache_addr; GPUVAddr new_gpu_addr = gpu_addr + new_start - cpu_addr;
for (auto& overlap : overlaps) { for (auto& overlap : overlaps) {
Unregister(overlap); Unregister(overlap);
} }
@ -250,7 +271,7 @@ private:
return new_map; return new_map;
} }
void UpdateBlock(const TBuffer& block, CacheAddr start, CacheAddr end, void UpdateBlock(const TBuffer& block, VAddr start, VAddr end,
std::vector<MapInterval>& overlaps) { std::vector<MapInterval>& overlaps) {
const IntervalType base_interval{start, end}; const IntervalType base_interval{start, end};
IntervalSet interval_set{}; IntervalSet interval_set{};
@ -262,13 +283,15 @@ private:
for (auto& interval : interval_set) { for (auto& interval : interval_set) {
std::size_t size = interval.upper() - interval.lower(); std::size_t size = interval.upper() - interval.lower();
if (size > 0) { if (size > 0) {
u8* host_ptr = FromCacheAddr(interval.lower()); staging_buffer.resize(size);
UploadBlockData(block, block->GetOffset(interval.lower()), size, host_ptr); system.Memory().ReadBlockUnsafe(interval.lower(), staging_buffer.data(), size);
UploadBlockData(block, block->GetOffset(interval.lower()), size,
staging_buffer.data());
} }
} }
} }
std::vector<MapInterval> GetMapsInRange(CacheAddr addr, std::size_t size) { std::vector<MapInterval> GetMapsInRange(VAddr addr, std::size_t size) {
if (size == 0) { if (size == 0) {
return {}; return {};
} }
@ -290,8 +313,9 @@ private:
void FlushMap(MapInterval map) { void FlushMap(MapInterval map) {
std::size_t size = map->GetEnd() - map->GetStart(); std::size_t size = map->GetEnd() - map->GetStart();
TBuffer block = blocks[map->GetStart() >> block_page_bits]; TBuffer block = blocks[map->GetStart() >> block_page_bits];
u8* host_ptr = FromCacheAddr(map->GetStart()); staging_buffer.resize(size);
DownloadBlockData(block, block->GetOffset(map->GetStart()), size, host_ptr); DownloadBlockData(block, block->GetOffset(map->GetStart()), size, staging_buffer.data());
system.Memory().WriteBlockUnsafe(map->GetStart(), staging_buffer.data(), size);
map->MarkAsModified(false, 0); map->MarkAsModified(false, 0);
} }
@ -316,14 +340,14 @@ private:
TBuffer EnlargeBlock(TBuffer buffer) { TBuffer EnlargeBlock(TBuffer buffer) {
const std::size_t old_size = buffer->GetSize(); const std::size_t old_size = buffer->GetSize();
const std::size_t new_size = old_size + block_page_size; const std::size_t new_size = old_size + block_page_size;
const CacheAddr cache_addr = buffer->GetCacheAddr(); const VAddr cpu_addr = buffer->GetCpuAddr();
TBuffer new_buffer = CreateBlock(cache_addr, new_size); TBuffer new_buffer = CreateBlock(cpu_addr, new_size);
CopyBlock(buffer, new_buffer, 0, 0, old_size); CopyBlock(buffer, new_buffer, 0, 0, old_size);
buffer->SetEpoch(epoch); buffer->SetEpoch(epoch);
pending_destruction.push_back(buffer); pending_destruction.push_back(buffer);
const CacheAddr cache_addr_end = cache_addr + new_size - 1; const VAddr cpu_addr_end = cpu_addr + new_size - 1;
u64 page_start = cache_addr >> block_page_bits; u64 page_start = cpu_addr >> block_page_bits;
const u64 page_end = cache_addr_end >> block_page_bits; const u64 page_end = cpu_addr_end >> block_page_bits;
while (page_start <= page_end) { while (page_start <= page_end) {
blocks[page_start] = new_buffer; blocks[page_start] = new_buffer;
++page_start; ++page_start;
@ -334,9 +358,9 @@ private:
TBuffer MergeBlocks(TBuffer first, TBuffer second) { TBuffer MergeBlocks(TBuffer first, TBuffer second) {
const std::size_t size_1 = first->GetSize(); const std::size_t size_1 = first->GetSize();
const std::size_t size_2 = second->GetSize(); const std::size_t size_2 = second->GetSize();
const CacheAddr first_addr = first->GetCacheAddr(); const VAddr first_addr = first->GetCpuAddr();
const CacheAddr second_addr = second->GetCacheAddr(); const VAddr second_addr = second->GetCpuAddr();
const CacheAddr new_addr = std::min(first_addr, second_addr); const VAddr new_addr = std::min(first_addr, second_addr);
const std::size_t new_size = size_1 + size_2; const std::size_t new_size = size_1 + size_2;
TBuffer new_buffer = CreateBlock(new_addr, new_size); TBuffer new_buffer = CreateBlock(new_addr, new_size);
CopyBlock(first, new_buffer, 0, new_buffer->GetOffset(first_addr), size_1); CopyBlock(first, new_buffer, 0, new_buffer->GetOffset(first_addr), size_1);
@ -345,9 +369,9 @@ private:
second->SetEpoch(epoch); second->SetEpoch(epoch);
pending_destruction.push_back(first); pending_destruction.push_back(first);
pending_destruction.push_back(second); pending_destruction.push_back(second);
const CacheAddr cache_addr_end = new_addr + new_size - 1; const VAddr cpu_addr_end = new_addr + new_size - 1;
u64 page_start = new_addr >> block_page_bits; u64 page_start = new_addr >> block_page_bits;
const u64 page_end = cache_addr_end >> block_page_bits; const u64 page_end = cpu_addr_end >> block_page_bits;
while (page_start <= page_end) { while (page_start <= page_end) {
blocks[page_start] = new_buffer; blocks[page_start] = new_buffer;
++page_start; ++page_start;
@ -355,18 +379,18 @@ private:
return new_buffer; return new_buffer;
} }
TBuffer GetBlock(const CacheAddr cache_addr, const std::size_t size) { TBuffer GetBlock(const VAddr cpu_addr, const std::size_t size) {
TBuffer found{}; TBuffer found{};
const CacheAddr cache_addr_end = cache_addr + size - 1; const VAddr cpu_addr_end = cpu_addr + size - 1;
u64 page_start = cache_addr >> block_page_bits; u64 page_start = cpu_addr >> block_page_bits;
const u64 page_end = cache_addr_end >> block_page_bits; const u64 page_end = cpu_addr_end >> block_page_bits;
while (page_start <= page_end) { while (page_start <= page_end) {
auto it = blocks.find(page_start); auto it = blocks.find(page_start);
if (it == blocks.end()) { if (it == blocks.end()) {
if (found) { if (found) {
found = EnlargeBlock(found); found = EnlargeBlock(found);
} else { } else {
const CacheAddr start_addr = (page_start << block_page_bits); const VAddr start_addr = (page_start << block_page_bits);
found = CreateBlock(start_addr, block_page_size); found = CreateBlock(start_addr, block_page_size);
blocks[page_start] = found; blocks[page_start] = found;
} }
@ -386,7 +410,7 @@ private:
return found; return found;
} }
void MarkRegionAsWritten(const CacheAddr start, const CacheAddr end) { void MarkRegionAsWritten(const VAddr start, const VAddr end) {
u64 page_start = start >> write_page_bit; u64 page_start = start >> write_page_bit;
const u64 page_end = end >> write_page_bit; const u64 page_end = end >> write_page_bit;
while (page_start <= page_end) { while (page_start <= page_end) {
@ -400,7 +424,7 @@ private:
} }
} }
void UnmarkRegionAsWritten(const CacheAddr start, const CacheAddr end) { void UnmarkRegionAsWritten(const VAddr start, const VAddr end) {
u64 page_start = start >> write_page_bit; u64 page_start = start >> write_page_bit;
const u64 page_end = end >> write_page_bit; const u64 page_end = end >> write_page_bit;
while (page_start <= page_end) { while (page_start <= page_end) {
@ -416,7 +440,7 @@ private:
} }
} }
bool IsRegionWritten(const CacheAddr start, const CacheAddr end) const { bool IsRegionWritten(const VAddr start, const VAddr end) const {
u64 page_start = start >> write_page_bit; u64 page_start = start >> write_page_bit;
const u64 page_end = end >> write_page_bit; const u64 page_end = end >> write_page_bit;
while (page_start <= page_end) { while (page_start <= page_end) {
@ -440,8 +464,8 @@ private:
u64 buffer_offset = 0; u64 buffer_offset = 0;
u64 buffer_offset_base = 0; u64 buffer_offset_base = 0;
using IntervalSet = boost::icl::interval_set<CacheAddr>; using IntervalSet = boost::icl::interval_set<VAddr>;
using IntervalCache = boost::icl::interval_map<CacheAddr, MapInterval>; using IntervalCache = boost::icl::interval_map<VAddr, MapInterval>;
using IntervalType = typename IntervalCache::interval_type; using IntervalType = typename IntervalCache::interval_type;
IntervalCache mapped_addresses; IntervalCache mapped_addresses;
@ -456,6 +480,8 @@ private:
u64 epoch = 0; u64 epoch = 0;
u64 modified_ticks = 0; u64 modified_ticks = 0;
std::vector<u8> staging_buffer;
std::recursive_mutex mutex; std::recursive_mutex mutex;
}; };

View File

@ -11,7 +11,7 @@ namespace VideoCommon {
class MapIntervalBase { class MapIntervalBase {
public: public:
MapIntervalBase(const CacheAddr start, const CacheAddr end, const GPUVAddr gpu_addr) MapIntervalBase(const VAddr start, const VAddr end, const GPUVAddr gpu_addr)
: start{start}, end{end}, gpu_addr{gpu_addr} {} : start{start}, end{end}, gpu_addr{gpu_addr} {}
void SetCpuAddress(VAddr new_cpu_addr) { void SetCpuAddress(VAddr new_cpu_addr) {
@ -26,7 +26,7 @@ public:
return gpu_addr; return gpu_addr;
} }
bool IsInside(const CacheAddr other_start, const CacheAddr other_end) const { bool IsInside(const VAddr other_start, const VAddr other_end) const {
return (start <= other_start && other_end <= end); return (start <= other_start && other_end <= end);
} }
@ -46,11 +46,11 @@ public:
return is_registered; return is_registered;
} }
CacheAddr GetStart() const { VAddr GetStart() const {
return start; return start;
} }
CacheAddr GetEnd() const { VAddr GetEnd() const {
return end; return end;
} }
@ -76,8 +76,8 @@ public:
} }
private: private:
CacheAddr start; VAddr start;
CacheAddr end; VAddr end;
GPUVAddr gpu_addr; GPUVAddr gpu_addr;
VAddr cpu_addr{}; VAddr cpu_addr{};
bool is_written{}; bool is_written{};

View File

@ -97,6 +97,11 @@ public:
void WriteBlockUnsafe(GPUVAddr dest_addr, const void* src_buffer, std::size_t size); void WriteBlockUnsafe(GPUVAddr dest_addr, const void* src_buffer, std::size_t size);
void CopyBlockUnsafe(GPUVAddr dest_addr, GPUVAddr src_addr, std::size_t size); void CopyBlockUnsafe(GPUVAddr dest_addr, GPUVAddr src_addr, std::size_t size);
static bool IsGranularRange(GPUVAddr gpu_addr, std::size_t size) {
const std::size_t page = (gpu_addr & page_mask) + size;
return page <= page_size;
}
private: private:
using VMAMap = std::map<GPUVAddr, VirtualMemoryArea>; using VMAMap = std::map<GPUVAddr, VirtualMemoryArea>;
using VMAHandle = VMAMap::const_iterator; using VMAHandle = VMAMap::const_iterator;

View File

@ -21,8 +21,8 @@ using Maxwell = Tegra::Engines::Maxwell3D::Regs;
MICROPROFILE_DEFINE(OpenGL_Buffer_Download, "OpenGL", "Buffer Download", MP_RGB(192, 192, 128)); MICROPROFILE_DEFINE(OpenGL_Buffer_Download, "OpenGL", "Buffer Download", MP_RGB(192, 192, 128));
CachedBufferBlock::CachedBufferBlock(CacheAddr cache_addr, const std::size_t size) CachedBufferBlock::CachedBufferBlock(VAddr cpu_addr, const std::size_t size)
: VideoCommon::BufferBlock{cache_addr, size} { : VideoCommon::BufferBlock{cpu_addr, size} {
gl_buffer.Create(); gl_buffer.Create();
glNamedBufferData(gl_buffer.handle, static_cast<GLsizeiptr>(size), nullptr, GL_DYNAMIC_DRAW); glNamedBufferData(gl_buffer.handle, static_cast<GLsizeiptr>(size), nullptr, GL_DYNAMIC_DRAW);
} }
@ -47,8 +47,8 @@ OGLBufferCache::~OGLBufferCache() {
glDeleteBuffers(static_cast<GLsizei>(std::size(cbufs)), std::data(cbufs)); glDeleteBuffers(static_cast<GLsizei>(std::size(cbufs)), std::data(cbufs));
} }
Buffer OGLBufferCache::CreateBlock(CacheAddr cache_addr, std::size_t size) { Buffer OGLBufferCache::CreateBlock(VAddr cpu_addr, std::size_t size) {
return std::make_shared<CachedBufferBlock>(cache_addr, size); return std::make_shared<CachedBufferBlock>(cpu_addr, size);
} }
void OGLBufferCache::WriteBarrier() { void OGLBufferCache::WriteBarrier() {

View File

@ -31,7 +31,7 @@ using GenericBufferCache = VideoCommon::BufferCache<Buffer, GLuint, OGLStreamBuf
class CachedBufferBlock : public VideoCommon::BufferBlock { class CachedBufferBlock : public VideoCommon::BufferBlock {
public: public:
explicit CachedBufferBlock(CacheAddr cache_addr, const std::size_t size); explicit CachedBufferBlock(VAddr cpu_addr, const std::size_t size);
~CachedBufferBlock(); ~CachedBufferBlock();
const GLuint* GetHandle() const { const GLuint* GetHandle() const {
@ -55,7 +55,7 @@ public:
} }
protected: protected:
Buffer CreateBlock(CacheAddr cache_addr, std::size_t size) override; Buffer CreateBlock(VAddr cpu_addr, std::size_t size) override;
void WriteBarrier() override; void WriteBarrier() override;

View File

@ -663,7 +663,7 @@ void RasterizerOpenGL::FlushRegion(VAddr addr, u64 size) {
} }
CacheAddr cache_addr = ToCacheAddr(system.Memory().GetPointer(addr)); CacheAddr cache_addr = ToCacheAddr(system.Memory().GetPointer(addr));
texture_cache.FlushRegion(addr, size); texture_cache.FlushRegion(addr, size);
buffer_cache.FlushRegion(cache_addr, size); buffer_cache.FlushRegion(addr, size);
query_cache.FlushRegion(cache_addr, size); query_cache.FlushRegion(cache_addr, size);
} }
@ -675,7 +675,7 @@ void RasterizerOpenGL::InvalidateRegion(VAddr addr, u64 size) {
CacheAddr cache_addr = ToCacheAddr(system.Memory().GetPointer(addr)); CacheAddr cache_addr = ToCacheAddr(system.Memory().GetPointer(addr));
texture_cache.InvalidateRegion(addr, size); texture_cache.InvalidateRegion(addr, size);
shader_cache.InvalidateRegion(cache_addr, size); shader_cache.InvalidateRegion(cache_addr, size);
buffer_cache.InvalidateRegion(cache_addr, size); buffer_cache.InvalidateRegion(addr, size);
query_cache.InvalidateRegion(cache_addr, size); query_cache.InvalidateRegion(cache_addr, size);
} }

View File

@ -42,8 +42,8 @@ auto CreateStreamBuffer(const VKDevice& device, VKScheduler& scheduler) {
} // Anonymous namespace } // Anonymous namespace
CachedBufferBlock::CachedBufferBlock(const VKDevice& device, VKMemoryManager& memory_manager, CachedBufferBlock::CachedBufferBlock(const VKDevice& device, VKMemoryManager& memory_manager,
CacheAddr cache_addr, std::size_t size) VAddr cpu_addr, std::size_t size)
: VideoCommon::BufferBlock{cache_addr, size} { : VideoCommon::BufferBlock{cpu_addr, size} {
const vk::BufferCreateInfo buffer_ci({}, static_cast<vk::DeviceSize>(size), const vk::BufferCreateInfo buffer_ci({}, static_cast<vk::DeviceSize>(size),
BufferUsage | vk::BufferUsageFlagBits::eTransferSrc | BufferUsage | vk::BufferUsageFlagBits::eTransferSrc |
vk::BufferUsageFlagBits::eTransferDst, vk::BufferUsageFlagBits::eTransferDst,
@ -68,8 +68,8 @@ VKBufferCache::VKBufferCache(VideoCore::RasterizerInterface& rasterizer, Core::S
VKBufferCache::~VKBufferCache() = default; VKBufferCache::~VKBufferCache() = default;
Buffer VKBufferCache::CreateBlock(CacheAddr cache_addr, std::size_t size) { Buffer VKBufferCache::CreateBlock(VAddr cpu_addr, std::size_t size) {
return std::make_shared<CachedBufferBlock>(device, memory_manager, cache_addr, size); return std::make_shared<CachedBufferBlock>(device, memory_manager, cpu_addr, size);
} }
const vk::Buffer* VKBufferCache::ToHandle(const Buffer& buffer) { const vk::Buffer* VKBufferCache::ToHandle(const Buffer& buffer) {

View File

@ -30,7 +30,7 @@ class VKScheduler;
class CachedBufferBlock final : public VideoCommon::BufferBlock { class CachedBufferBlock final : public VideoCommon::BufferBlock {
public: public:
explicit CachedBufferBlock(const VKDevice& device, VKMemoryManager& memory_manager, explicit CachedBufferBlock(const VKDevice& device, VKMemoryManager& memory_manager,
CacheAddr cache_addr, std::size_t size); VAddr cpu_addr, std::size_t size);
~CachedBufferBlock(); ~CachedBufferBlock();
const vk::Buffer* GetHandle() const { const vk::Buffer* GetHandle() const {
@ -55,7 +55,7 @@ public:
protected: protected:
void WriteBarrier() override {} void WriteBarrier() override {}
Buffer CreateBlock(CacheAddr cache_addr, std::size_t size) override; Buffer CreateBlock(VAddr cpu_addr, std::size_t size) override;
const vk::Buffer* ToHandle(const Buffer& buffer) override; const vk::Buffer* ToHandle(const Buffer& buffer) override;

View File

@ -501,7 +501,7 @@ void RasterizerVulkan::FlushRegion(VAddr addr, u64 size) {
} }
CacheAddr cache_addr = ToCacheAddr(system.Memory().GetPointer(addr)); CacheAddr cache_addr = ToCacheAddr(system.Memory().GetPointer(addr));
texture_cache.FlushRegion(addr, size); texture_cache.FlushRegion(addr, size);
buffer_cache.FlushRegion(cache_addr, size); buffer_cache.FlushRegion(addr, size);
query_cache.FlushRegion(cache_addr, size); query_cache.FlushRegion(cache_addr, size);
} }
@ -512,7 +512,7 @@ void RasterizerVulkan::InvalidateRegion(VAddr addr, u64 size) {
CacheAddr cache_addr = ToCacheAddr(system.Memory().GetPointer(addr)); CacheAddr cache_addr = ToCacheAddr(system.Memory().GetPointer(addr));
texture_cache.InvalidateRegion(addr, size); texture_cache.InvalidateRegion(addr, size);
pipeline_cache.InvalidateRegion(cache_addr, size); pipeline_cache.InvalidateRegion(cache_addr, size);
buffer_cache.InvalidateRegion(cache_addr, size); buffer_cache.InvalidateRegion(addr, size);
query_cache.InvalidateRegion(cache_addr, size); query_cache.InvalidateRegion(cache_addr, size);
} }