diff options
Diffstat (limited to 'src/video_core/rasterizer_accelerated.cpp')
| -rw-r--r-- | src/video_core/rasterizer_accelerated.cpp | 99 |
1 files changed, 47 insertions, 52 deletions
diff --git a/src/video_core/rasterizer_accelerated.cpp b/src/video_core/rasterizer_accelerated.cpp index 3c9477f6e..f200a650f 100644 --- a/src/video_core/rasterizer_accelerated.cpp +++ b/src/video_core/rasterizer_accelerated.cpp | |||
| @@ -3,7 +3,6 @@ | |||
| 3 | 3 | ||
| 4 | #include <atomic> | 4 | #include <atomic> |
| 5 | 5 | ||
| 6 | #include "common/alignment.h" | ||
| 7 | #include "common/assert.h" | 6 | #include "common/assert.h" |
| 8 | #include "common/common_types.h" | 7 | #include "common/common_types.h" |
| 9 | #include "common/div_ceil.h" | 8 | #include "common/div_ceil.h" |
| @@ -12,65 +11,61 @@ | |||
| 12 | 11 | ||
| 13 | namespace VideoCore { | 12 | namespace VideoCore { |
| 14 | 13 | ||
| 15 | static constexpr u16 IdentityValue = 1; | ||
| 16 | |||
| 17 | using namespace Core::Memory; | 14 | using namespace Core::Memory; |
| 18 | 15 | ||
| 19 | RasterizerAccelerated::RasterizerAccelerated(Memory& cpu_memory_) : map{}, cpu_memory{cpu_memory_} { | 16 | RasterizerAccelerated::RasterizerAccelerated(Memory& cpu_memory_) |
| 20 | // We are tracking CPU memory, which cannot map more than 39 bits. | 17 | : cached_pages(std::make_unique<CachedPages>()), cpu_memory{cpu_memory_} {} |
| 21 | const VAddr start_address = 0; | ||
| 22 | const VAddr end_address = (1ULL << 39); | ||
| 23 | const IntervalType address_space_interval(start_address, end_address); | ||
| 24 | const auto value = std::make_pair(address_space_interval, IdentityValue); | ||
| 25 | |||
| 26 | map.add(value); | ||
| 27 | } | ||
| 28 | 18 | ||
| 29 | RasterizerAccelerated::~RasterizerAccelerated() = default; | 19 | RasterizerAccelerated::~RasterizerAccelerated() = default; |
| 30 | 20 | ||
| 31 | void RasterizerAccelerated::UpdatePagesCachedCount(VAddr addr, u64 size, bool cache) { | 21 | void RasterizerAccelerated::UpdatePagesCachedCount(VAddr addr, u64 size, int delta) { |
| 32 | std::scoped_lock lk{map_lock}; | 22 | u64 uncache_begin = 0; |
| 33 | 23 | u64 cache_begin = 0; | |
| 34 | // Align sizes. | 24 | u64 uncache_bytes = 0; |
| 35 | addr = Common::AlignDown(addr, YUZU_PAGESIZE); | 25 | u64 cache_bytes = 0; |
| 36 | size = Common::AlignUp(size, YUZU_PAGESIZE); | 26 | |
| 37 | 27 | std::atomic_thread_fence(std::memory_order_acquire); | |
| 38 | // Declare the overall interval we are going to operate on. | 28 | const u64 page_end = Common::DivCeil(addr + size, YUZU_PAGESIZE); |
| 39 | const VAddr start_address = addr; | 29 | for (u64 page = addr >> YUZU_PAGEBITS; page != page_end; ++page) { |
| 40 | const VAddr end_address = addr + size; | 30 | std::atomic_uint16_t& count = cached_pages->at(page >> 2).Count(page); |
| 41 | const IntervalType modification_range(start_address, end_address); | 31 | |
| 42 | 32 | if (delta > 0) { | |
| 43 | // Find the boundaries of where to iterate. | 33 | ASSERT_MSG(count.load(std::memory_order::relaxed) < UINT16_MAX, "Count may overflow!"); |
| 44 | const auto lower = map.lower_bound(modification_range); | 34 | } else if (delta < 0) { |
| 45 | const auto upper = map.upper_bound(modification_range); | 35 | ASSERT_MSG(count.load(std::memory_order::relaxed) > 0, "Count may underflow!"); |
| 46 | 36 | } else { | |
| 47 | // Iterate over the contained intervals. | 37 | ASSERT_MSG(false, "Delta must be non-zero!"); |
| 48 | for (auto it = lower; it != upper; it++) { | 38 | } |
| 49 | // Intersect interval range with modification range. | ||
| 50 | const auto current_range = modification_range & it->first; | ||
| 51 | |||
| 52 | // Calculate the address and size to operate over. | ||
| 53 | const auto current_addr = current_range.lower(); | ||
| 54 | const auto current_size = current_range.upper() - current_addr; | ||
| 55 | |||
| 56 | // Get the current value of the range. | ||
| 57 | const auto value = it->second; | ||
| 58 | 39 | ||
| 59 | if (cache && value == IdentityValue) { | 40 | // Adds or subtracts 1, as count is a unsigned 8-bit value |
| 60 | // If we are going to cache, and the value is not yet referenced, then cache this range. | 41 | count.fetch_add(static_cast<u16>(delta), std::memory_order_release); |
| 61 | cpu_memory.RasterizerMarkRegionCached(current_addr, current_size, true); | 42 | |
| 62 | } else if (!cache && value == IdentityValue + 1) { | 43 | // Assume delta is either -1 or 1 |
| 63 | // If we are going to uncache, and this is the last reference, then uncache this range. | 44 | if (count.load(std::memory_order::relaxed) == 0) { |
| 64 | cpu_memory.RasterizerMarkRegionCached(current_addr, current_size, false); | 45 | if (uncache_bytes == 0) { |
| 46 | uncache_begin = page; | ||
| 47 | } | ||
| 48 | uncache_bytes += YUZU_PAGESIZE; | ||
| 49 | } else if (uncache_bytes > 0) { | ||
| 50 | cpu_memory.RasterizerMarkRegionCached(uncache_begin << YUZU_PAGEBITS, uncache_bytes, | ||
| 51 | false); | ||
| 52 | uncache_bytes = 0; | ||
| 53 | } | ||
| 54 | if (count.load(std::memory_order::relaxed) == 1 && delta > 0) { | ||
| 55 | if (cache_bytes == 0) { | ||
| 56 | cache_begin = page; | ||
| 57 | } | ||
| 58 | cache_bytes += YUZU_PAGESIZE; | ||
| 59 | } else if (cache_bytes > 0) { | ||
| 60 | cpu_memory.RasterizerMarkRegionCached(cache_begin << YUZU_PAGEBITS, cache_bytes, true); | ||
| 61 | cache_bytes = 0; | ||
| 65 | } | 62 | } |
| 66 | } | 63 | } |
| 67 | 64 | if (uncache_bytes > 0) { | |
| 68 | // Update the set. | 65 | cpu_memory.RasterizerMarkRegionCached(uncache_begin << YUZU_PAGEBITS, uncache_bytes, false); |
| 69 | const auto value = std::make_pair(modification_range, IdentityValue); | 66 | } |
| 70 | if (cache) { | 67 | if (cache_bytes > 0) { |
| 71 | map.add(value); | 68 | cpu_memory.RasterizerMarkRegionCached(cache_begin << YUZU_PAGEBITS, cache_bytes, true); |
| 72 | } else { | ||
| 73 | map.subtract(value); | ||
| 74 | } | 69 | } |
| 75 | } | 70 | } |
| 76 | 71 | ||