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