diff options
| author | 2019-08-29 13:07:01 -0400 | |
|---|---|---|
| committer | 2019-08-29 13:07:01 -0400 | |
| commit | e4246158393fc011b16a4c1ef023c167cde9d4f8 (patch) | |
| tree | 461f4e0a45a1d44bc2f81a3ef7f35cb10cece680 /src/video_core/buffer_cache | |
| parent | Merge pull request #2758 from ReinUsesLisp/packed-tid (diff) | |
| parent | Buffer Cache: Adress Feedback. (diff) | |
| download | yuzu-e4246158393fc011b16a4c1ef023c167cde9d4f8.tar.gz yuzu-e4246158393fc011b16a4c1ef023c167cde9d4f8.tar.xz yuzu-e4246158393fc011b16a4c1ef023c167cde9d4f8.zip | |
Merge pull request #2783 from FernandoS27/new-buffer-cache
Implement a New LLE Buffer Cache
Diffstat (limited to 'src/video_core/buffer_cache')
| -rw-r--r-- | src/video_core/buffer_cache/buffer_block.h | 77 | ||||
| -rw-r--r-- | src/video_core/buffer_cache/buffer_cache.h | 449 | ||||
| -rw-r--r-- | src/video_core/buffer_cache/map_interval.h | 89 |
3 files changed, 615 insertions, 0 deletions
diff --git a/src/video_core/buffer_cache/buffer_block.h b/src/video_core/buffer_cache/buffer_block.h new file mode 100644 index 000000000..d2124443f --- /dev/null +++ b/src/video_core/buffer_cache/buffer_block.h | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | // Copyright 2019 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <unordered_set> | ||
| 8 | #include <utility> | ||
| 9 | |||
| 10 | #include "common/alignment.h" | ||
| 11 | #include "common/common_types.h" | ||
| 12 | #include "video_core/gpu.h" | ||
| 13 | |||
| 14 | namespace VideoCommon { | ||
| 15 | |||
| 16 | class BufferBlock { | ||
| 17 | public: | ||
| 18 | bool Overlaps(const CacheAddr start, const CacheAddr end) const { | ||
| 19 | return (cache_addr < end) && (cache_addr_end > start); | ||
| 20 | } | ||
| 21 | |||
| 22 | bool IsInside(const CacheAddr other_start, const CacheAddr other_end) const { | ||
| 23 | return cache_addr <= other_start && other_end <= cache_addr_end; | ||
| 24 | } | ||
| 25 | |||
| 26 | u8* GetWritableHostPtr() const { | ||
| 27 | return FromCacheAddr(cache_addr); | ||
| 28 | } | ||
| 29 | |||
| 30 | u8* GetWritableHostPtr(std::size_t offset) const { | ||
| 31 | return FromCacheAddr(cache_addr + offset); | ||
| 32 | } | ||
| 33 | |||
| 34 | std::size_t GetOffset(const CacheAddr in_addr) { | ||
| 35 | return static_cast<std::size_t>(in_addr - cache_addr); | ||
| 36 | } | ||
| 37 | |||
| 38 | CacheAddr GetCacheAddr() const { | ||
| 39 | return cache_addr; | ||
| 40 | } | ||
| 41 | |||
| 42 | CacheAddr GetCacheAddrEnd() const { | ||
| 43 | return cache_addr_end; | ||
| 44 | } | ||
| 45 | |||
| 46 | void SetCacheAddr(const CacheAddr new_addr) { | ||
| 47 | cache_addr = new_addr; | ||
| 48 | cache_addr_end = new_addr + size; | ||
| 49 | } | ||
| 50 | |||
| 51 | std::size_t GetSize() const { | ||
| 52 | return size; | ||
| 53 | } | ||
| 54 | |||
| 55 | void SetEpoch(u64 new_epoch) { | ||
| 56 | epoch = new_epoch; | ||
| 57 | } | ||
| 58 | |||
| 59 | u64 GetEpoch() { | ||
| 60 | return epoch; | ||
| 61 | } | ||
| 62 | |||
| 63 | protected: | ||
| 64 | explicit BufferBlock(CacheAddr cache_addr, const std::size_t size) : size{size} { | ||
| 65 | SetCacheAddr(cache_addr); | ||
| 66 | } | ||
| 67 | ~BufferBlock() = default; | ||
| 68 | |||
| 69 | private: | ||
| 70 | CacheAddr cache_addr{}; | ||
| 71 | CacheAddr cache_addr_end{}; | ||
| 72 | u64 pages{}; | ||
| 73 | std::size_t size{}; | ||
| 74 | u64 epoch{}; | ||
| 75 | }; | ||
| 76 | |||
| 77 | } // namespace VideoCommon | ||
diff --git a/src/video_core/buffer_cache/buffer_cache.h b/src/video_core/buffer_cache/buffer_cache.h new file mode 100644 index 000000000..38ce16ed5 --- /dev/null +++ b/src/video_core/buffer_cache/buffer_cache.h | |||
| @@ -0,0 +1,449 @@ | |||
| 1 | // Copyright 2019 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include <memory> | ||
| 9 | #include <mutex> | ||
| 10 | #include <unordered_map> | ||
| 11 | #include <unordered_set> | ||
| 12 | #include <utility> | ||
| 13 | #include <vector> | ||
| 14 | |||
| 15 | #include "common/alignment.h" | ||
| 16 | #include "common/common_types.h" | ||
| 17 | #include "core/core.h" | ||
| 18 | #include "video_core/buffer_cache/buffer_block.h" | ||
| 19 | #include "video_core/buffer_cache/map_interval.h" | ||
| 20 | #include "video_core/memory_manager.h" | ||
| 21 | |||
| 22 | namespace VideoCore { | ||
| 23 | class RasterizerInterface; | ||
| 24 | } | ||
| 25 | |||
| 26 | namespace VideoCommon { | ||
| 27 | |||
| 28 | using MapInterval = std::shared_ptr<MapIntervalBase>; | ||
| 29 | |||
| 30 | template <typename TBuffer, typename TBufferType, typename StreamBuffer> | ||
| 31 | class BufferCache { | ||
| 32 | public: | ||
| 33 | using BufferInfo = std::pair<const TBufferType*, u64>; | ||
| 34 | |||
| 35 | BufferInfo UploadMemory(GPUVAddr gpu_addr, std::size_t size, std::size_t alignment = 4, | ||
| 36 | bool is_written = false) { | ||
| 37 | std::lock_guard lock{mutex}; | ||
| 38 | |||
| 39 | auto& memory_manager = system.GPU().MemoryManager(); | ||
| 40 | const auto host_ptr = memory_manager.GetPointer(gpu_addr); | ||
| 41 | if (!host_ptr) { | ||
| 42 | return {GetEmptyBuffer(size), 0}; | ||
| 43 | } | ||
| 44 | const auto cache_addr = ToCacheAddr(host_ptr); | ||
| 45 | |||
| 46 | // Cache management is a big overhead, so only cache entries with a given size. | ||
| 47 | // TODO: Figure out which size is the best for given games. | ||
| 48 | constexpr std::size_t max_stream_size = 0x800; | ||
| 49 | if (size < max_stream_size) { | ||
| 50 | if (!is_written && !IsRegionWritten(cache_addr, cache_addr + size - 1)) { | ||
| 51 | return StreamBufferUpload(host_ptr, size, alignment); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | auto block = GetBlock(cache_addr, size); | ||
| 56 | auto map = MapAddress(block, gpu_addr, cache_addr, size); | ||
| 57 | if (is_written) { | ||
| 58 | map->MarkAsModified(true, GetModifiedTicks()); | ||
| 59 | if (!map->IsWritten()) { | ||
| 60 | map->MarkAsWritten(true); | ||
| 61 | MarkRegionAsWritten(map->GetStart(), map->GetEnd() - 1); | ||
| 62 | } | ||
| 63 | } else { | ||
| 64 | if (map->IsWritten()) { | ||
| 65 | WriteBarrier(); | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | const u64 offset = static_cast<u64>(block->GetOffset(cache_addr)); | ||
| 70 | |||
| 71 | return {ToHandle(block), offset}; | ||
| 72 | } | ||
| 73 | |||
| 74 | /// Uploads from a host memory. Returns the OpenGL buffer where it's located and its offset. | ||
| 75 | BufferInfo UploadHostMemory(const void* raw_pointer, std::size_t size, | ||
| 76 | std::size_t alignment = 4) { | ||
| 77 | std::lock_guard lock{mutex}; | ||
| 78 | return StreamBufferUpload(raw_pointer, size, alignment); | ||
| 79 | } | ||
| 80 | |||
| 81 | void Map(std::size_t max_size) { | ||
| 82 | std::lock_guard lock{mutex}; | ||
| 83 | |||
| 84 | std::tie(buffer_ptr, buffer_offset_base, invalidated) = stream_buffer->Map(max_size, 4); | ||
| 85 | buffer_offset = buffer_offset_base; | ||
| 86 | } | ||
| 87 | |||
| 88 | /// Finishes the upload stream, returns true on bindings invalidation. | ||
| 89 | bool Unmap() { | ||
| 90 | std::lock_guard lock{mutex}; | ||
| 91 | |||
| 92 | stream_buffer->Unmap(buffer_offset - buffer_offset_base); | ||
| 93 | return std::exchange(invalidated, false); | ||
| 94 | } | ||
| 95 | |||
| 96 | void TickFrame() { | ||
| 97 | ++epoch; | ||
| 98 | while (!pending_destruction.empty()) { | ||
| 99 | if (pending_destruction.front()->GetEpoch() + 1 > epoch) { | ||
| 100 | break; | ||
| 101 | } | ||
| 102 | pending_destruction.pop_front(); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | /// Write any cached resources overlapping the specified region back to memory | ||
| 107 | void FlushRegion(CacheAddr addr, std::size_t size) { | ||
| 108 | std::lock_guard lock{mutex}; | ||
| 109 | |||
| 110 | std::vector<MapInterval> objects = GetMapsInRange(addr, size); | ||
| 111 | std::sort(objects.begin(), objects.end(), [](const MapInterval& a, const MapInterval& b) { | ||
| 112 | return a->GetModificationTick() < b->GetModificationTick(); | ||
| 113 | }); | ||
| 114 | for (auto& object : objects) { | ||
| 115 | if (object->IsModified() && object->IsRegistered()) { | ||
| 116 | FlushMap(object); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | /// Mark the specified region as being invalidated | ||
| 122 | void InvalidateRegion(CacheAddr addr, u64 size) { | ||
| 123 | std::lock_guard lock{mutex}; | ||
| 124 | |||
| 125 | std::vector<MapInterval> objects = GetMapsInRange(addr, size); | ||
| 126 | for (auto& object : objects) { | ||
| 127 | if (object->IsRegistered()) { | ||
| 128 | Unregister(object); | ||
| 129 | } | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | virtual const TBufferType* GetEmptyBuffer(std::size_t size) = 0; | ||
| 134 | |||
| 135 | protected: | ||
| 136 | explicit BufferCache(VideoCore::RasterizerInterface& rasterizer, Core::System& system, | ||
| 137 | std::unique_ptr<StreamBuffer> stream_buffer) | ||
| 138 | : rasterizer{rasterizer}, system{system}, stream_buffer{std::move(stream_buffer)}, | ||
| 139 | stream_buffer_handle{this->stream_buffer->GetHandle()} {} | ||
| 140 | |||
| 141 | ~BufferCache() = default; | ||
| 142 | |||
| 143 | virtual const TBufferType* ToHandle(const TBuffer& storage) = 0; | ||
| 144 | |||
| 145 | virtual void WriteBarrier() = 0; | ||
| 146 | |||
| 147 | virtual TBuffer CreateBlock(CacheAddr cache_addr, std::size_t size) = 0; | ||
| 148 | |||
| 149 | virtual void UploadBlockData(const TBuffer& buffer, std::size_t offset, std::size_t size, | ||
| 150 | const u8* data) = 0; | ||
| 151 | |||
| 152 | virtual void DownloadBlockData(const TBuffer& buffer, std::size_t offset, std::size_t size, | ||
| 153 | u8* data) = 0; | ||
| 154 | |||
| 155 | virtual void CopyBlock(const TBuffer& src, const TBuffer& dst, std::size_t src_offset, | ||
| 156 | std::size_t dst_offset, std::size_t size) = 0; | ||
| 157 | |||
| 158 | /// Register an object into the cache | ||
| 159 | void Register(const MapInterval& new_map, bool inherit_written = false) { | ||
| 160 | const CacheAddr cache_ptr = new_map->GetStart(); | ||
| 161 | const std::optional<VAddr> cpu_addr = | ||
| 162 | system.GPU().MemoryManager().GpuToCpuAddress(new_map->GetGpuAddress()); | ||
| 163 | if (!cache_ptr || !cpu_addr) { | ||
| 164 | LOG_CRITICAL(HW_GPU, "Failed to register buffer with unmapped gpu_address 0x{:016x}", | ||
| 165 | new_map->GetGpuAddress()); | ||
| 166 | return; | ||
| 167 | } | ||
| 168 | const std::size_t size = new_map->GetEnd() - new_map->GetStart(); | ||
| 169 | new_map->SetCpuAddress(*cpu_addr); | ||
| 170 | new_map->MarkAsRegistered(true); | ||
| 171 | const IntervalType interval{new_map->GetStart(), new_map->GetEnd()}; | ||
| 172 | mapped_addresses.insert({interval, new_map}); | ||
| 173 | rasterizer.UpdatePagesCachedCount(*cpu_addr, size, 1); | ||
| 174 | if (inherit_written) { | ||
| 175 | MarkRegionAsWritten(new_map->GetStart(), new_map->GetEnd() - 1); | ||
| 176 | new_map->MarkAsWritten(true); | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | /// Unregisters an object from the cache | ||
| 181 | void Unregister(MapInterval& map) { | ||
| 182 | const std::size_t size = map->GetEnd() - map->GetStart(); | ||
| 183 | rasterizer.UpdatePagesCachedCount(map->GetCpuAddress(), size, -1); | ||
| 184 | map->MarkAsRegistered(false); | ||
| 185 | if (map->IsWritten()) { | ||
| 186 | UnmarkRegionAsWritten(map->GetStart(), map->GetEnd() - 1); | ||
| 187 | } | ||
| 188 | const IntervalType delete_interval{map->GetStart(), map->GetEnd()}; | ||
| 189 | mapped_addresses.erase(delete_interval); | ||
| 190 | } | ||
| 191 | |||
| 192 | private: | ||
| 193 | MapInterval CreateMap(const CacheAddr start, const CacheAddr end, const GPUVAddr gpu_addr) { | ||
| 194 | return std::make_shared<MapIntervalBase>(start, end, gpu_addr); | ||
| 195 | } | ||
| 196 | |||
| 197 | MapInterval MapAddress(const TBuffer& block, const GPUVAddr gpu_addr, | ||
| 198 | const CacheAddr cache_addr, const std::size_t size) { | ||
| 199 | |||
| 200 | std::vector<MapInterval> overlaps = GetMapsInRange(cache_addr, size); | ||
| 201 | if (overlaps.empty()) { | ||
| 202 | const CacheAddr cache_addr_end = cache_addr + size; | ||
| 203 | MapInterval new_map = CreateMap(cache_addr, cache_addr_end, gpu_addr); | ||
| 204 | u8* host_ptr = FromCacheAddr(cache_addr); | ||
| 205 | UploadBlockData(block, block->GetOffset(cache_addr), size, host_ptr); | ||
| 206 | Register(new_map); | ||
| 207 | return new_map; | ||
| 208 | } | ||
| 209 | |||
| 210 | const CacheAddr cache_addr_end = cache_addr + size; | ||
| 211 | if (overlaps.size() == 1) { | ||
| 212 | MapInterval& current_map = overlaps[0]; | ||
| 213 | if (current_map->IsInside(cache_addr, cache_addr_end)) { | ||
| 214 | return current_map; | ||
| 215 | } | ||
| 216 | } | ||
| 217 | CacheAddr new_start = cache_addr; | ||
| 218 | CacheAddr new_end = cache_addr_end; | ||
| 219 | bool write_inheritance = false; | ||
| 220 | bool modified_inheritance = false; | ||
| 221 | // Calculate new buffer parameters | ||
| 222 | for (auto& overlap : overlaps) { | ||
| 223 | new_start = std::min(overlap->GetStart(), new_start); | ||
| 224 | new_end = std::max(overlap->GetEnd(), new_end); | ||
| 225 | write_inheritance |= overlap->IsWritten(); | ||
| 226 | modified_inheritance |= overlap->IsModified(); | ||
| 227 | } | ||
| 228 | GPUVAddr new_gpu_addr = gpu_addr + new_start - cache_addr; | ||
| 229 | for (auto& overlap : overlaps) { | ||
| 230 | Unregister(overlap); | ||
| 231 | } | ||
| 232 | UpdateBlock(block, new_start, new_end, overlaps); | ||
| 233 | MapInterval new_map = CreateMap(new_start, new_end, new_gpu_addr); | ||
| 234 | if (modified_inheritance) { | ||
| 235 | new_map->MarkAsModified(true, GetModifiedTicks()); | ||
| 236 | } | ||
| 237 | Register(new_map, write_inheritance); | ||
| 238 | return new_map; | ||
| 239 | } | ||
| 240 | |||
| 241 | void UpdateBlock(const TBuffer& block, CacheAddr start, CacheAddr end, | ||
| 242 | std::vector<MapInterval>& overlaps) { | ||
| 243 | const IntervalType base_interval{start, end}; | ||
| 244 | IntervalSet interval_set{}; | ||
| 245 | interval_set.add(base_interval); | ||
| 246 | for (auto& overlap : overlaps) { | ||
| 247 | const IntervalType subtract{overlap->GetStart(), overlap->GetEnd()}; | ||
| 248 | interval_set.subtract(subtract); | ||
| 249 | } | ||
| 250 | for (auto& interval : interval_set) { | ||
| 251 | std::size_t size = interval.upper() - interval.lower(); | ||
| 252 | if (size > 0) { | ||
| 253 | u8* host_ptr = FromCacheAddr(interval.lower()); | ||
| 254 | UploadBlockData(block, block->GetOffset(interval.lower()), size, host_ptr); | ||
| 255 | } | ||
| 256 | } | ||
| 257 | } | ||
| 258 | |||
| 259 | std::vector<MapInterval> GetMapsInRange(CacheAddr addr, std::size_t size) { | ||
| 260 | if (size == 0) { | ||
| 261 | return {}; | ||
| 262 | } | ||
| 263 | |||
| 264 | std::vector<MapInterval> objects{}; | ||
| 265 | const IntervalType interval{addr, addr + size}; | ||
| 266 | for (auto& pair : boost::make_iterator_range(mapped_addresses.equal_range(interval))) { | ||
| 267 | objects.push_back(pair.second); | ||
| 268 | } | ||
| 269 | |||
| 270 | return objects; | ||
| 271 | } | ||
| 272 | |||
| 273 | /// Returns a ticks counter used for tracking when cached objects were last modified | ||
| 274 | u64 GetModifiedTicks() { | ||
| 275 | return ++modified_ticks; | ||
| 276 | } | ||
| 277 | |||
| 278 | void FlushMap(MapInterval map) { | ||
| 279 | std::size_t size = map->GetEnd() - map->GetStart(); | ||
| 280 | TBuffer block = blocks[map->GetStart() >> block_page_bits]; | ||
| 281 | u8* host_ptr = FromCacheAddr(map->GetStart()); | ||
| 282 | DownloadBlockData(block, block->GetOffset(map->GetStart()), size, host_ptr); | ||
| 283 | map->MarkAsModified(false, 0); | ||
| 284 | } | ||
| 285 | |||
| 286 | BufferInfo StreamBufferUpload(const void* raw_pointer, std::size_t size, | ||
| 287 | std::size_t alignment) { | ||
| 288 | AlignBuffer(alignment); | ||
| 289 | const std::size_t uploaded_offset = buffer_offset; | ||
| 290 | std::memcpy(buffer_ptr, raw_pointer, size); | ||
| 291 | |||
| 292 | buffer_ptr += size; | ||
| 293 | buffer_offset += size; | ||
| 294 | return {&stream_buffer_handle, uploaded_offset}; | ||
| 295 | } | ||
| 296 | |||
| 297 | void AlignBuffer(std::size_t alignment) { | ||
| 298 | // Align the offset, not the mapped pointer | ||
| 299 | const std::size_t offset_aligned = Common::AlignUp(buffer_offset, alignment); | ||
| 300 | buffer_ptr += offset_aligned - buffer_offset; | ||
| 301 | buffer_offset = offset_aligned; | ||
| 302 | } | ||
| 303 | |||
| 304 | TBuffer EnlargeBlock(TBuffer buffer) { | ||
| 305 | const std::size_t old_size = buffer->GetSize(); | ||
| 306 | const std::size_t new_size = old_size + block_page_size; | ||
| 307 | const CacheAddr cache_addr = buffer->GetCacheAddr(); | ||
| 308 | TBuffer new_buffer = CreateBlock(cache_addr, new_size); | ||
| 309 | CopyBlock(buffer, new_buffer, 0, 0, old_size); | ||
| 310 | buffer->SetEpoch(epoch); | ||
| 311 | pending_destruction.push_back(buffer); | ||
| 312 | const CacheAddr cache_addr_end = cache_addr + new_size - 1; | ||
| 313 | u64 page_start = cache_addr >> block_page_bits; | ||
| 314 | const u64 page_end = cache_addr_end >> block_page_bits; | ||
| 315 | while (page_start <= page_end) { | ||
| 316 | blocks[page_start] = new_buffer; | ||
| 317 | ++page_start; | ||
| 318 | } | ||
| 319 | return new_buffer; | ||
| 320 | } | ||
| 321 | |||
| 322 | TBuffer MergeBlocks(TBuffer first, TBuffer second) { | ||
| 323 | const std::size_t size_1 = first->GetSize(); | ||
| 324 | const std::size_t size_2 = second->GetSize(); | ||
| 325 | const CacheAddr first_addr = first->GetCacheAddr(); | ||
| 326 | const CacheAddr second_addr = second->GetCacheAddr(); | ||
| 327 | const CacheAddr new_addr = std::min(first_addr, second_addr); | ||
| 328 | const std::size_t new_size = size_1 + size_2; | ||
| 329 | TBuffer new_buffer = CreateBlock(new_addr, new_size); | ||
| 330 | CopyBlock(first, new_buffer, 0, new_buffer->GetOffset(first_addr), size_1); | ||
| 331 | CopyBlock(second, new_buffer, 0, new_buffer->GetOffset(second_addr), size_2); | ||
| 332 | first->SetEpoch(epoch); | ||
| 333 | second->SetEpoch(epoch); | ||
| 334 | pending_destruction.push_back(first); | ||
| 335 | pending_destruction.push_back(second); | ||
| 336 | const CacheAddr cache_addr_end = new_addr + new_size - 1; | ||
| 337 | u64 page_start = new_addr >> block_page_bits; | ||
| 338 | const u64 page_end = cache_addr_end >> block_page_bits; | ||
| 339 | while (page_start <= page_end) { | ||
| 340 | blocks[page_start] = new_buffer; | ||
| 341 | ++page_start; | ||
| 342 | } | ||
| 343 | return new_buffer; | ||
| 344 | } | ||
| 345 | |||
| 346 | TBuffer GetBlock(const CacheAddr cache_addr, const std::size_t size) { | ||
| 347 | TBuffer found{}; | ||
| 348 | const CacheAddr cache_addr_end = cache_addr + size - 1; | ||
| 349 | u64 page_start = cache_addr >> block_page_bits; | ||
| 350 | const u64 page_end = cache_addr_end >> block_page_bits; | ||
| 351 | const u64 num_pages = page_end - page_start + 1; | ||
| 352 | while (page_start <= page_end) { | ||
| 353 | auto it = blocks.find(page_start); | ||
| 354 | if (it == blocks.end()) { | ||
| 355 | if (found) { | ||
| 356 | found = EnlargeBlock(found); | ||
| 357 | } else { | ||
| 358 | const CacheAddr start_addr = (page_start << block_page_bits); | ||
| 359 | found = CreateBlock(start_addr, block_page_size); | ||
| 360 | blocks[page_start] = found; | ||
| 361 | } | ||
| 362 | } else { | ||
| 363 | if (found) { | ||
| 364 | if (found == it->second) { | ||
| 365 | ++page_start; | ||
| 366 | continue; | ||
| 367 | } | ||
| 368 | found = MergeBlocks(found, it->second); | ||
| 369 | } else { | ||
| 370 | found = it->second; | ||
| 371 | } | ||
| 372 | } | ||
| 373 | ++page_start; | ||
| 374 | } | ||
| 375 | return found; | ||
| 376 | } | ||
| 377 | |||
| 378 | void MarkRegionAsWritten(const CacheAddr start, const CacheAddr end) { | ||
| 379 | u64 page_start = start >> write_page_bit; | ||
| 380 | const u64 page_end = end >> write_page_bit; | ||
| 381 | while (page_start <= page_end) { | ||
| 382 | auto it = written_pages.find(page_start); | ||
| 383 | if (it != written_pages.end()) { | ||
| 384 | it->second = it->second + 1; | ||
| 385 | } else { | ||
| 386 | written_pages[page_start] = 1; | ||
| 387 | } | ||
| 388 | page_start++; | ||
| 389 | } | ||
| 390 | } | ||
| 391 | |||
| 392 | void UnmarkRegionAsWritten(const CacheAddr start, const CacheAddr end) { | ||
| 393 | u64 page_start = start >> write_page_bit; | ||
| 394 | const u64 page_end = end >> write_page_bit; | ||
| 395 | while (page_start <= page_end) { | ||
| 396 | auto it = written_pages.find(page_start); | ||
| 397 | if (it != written_pages.end()) { | ||
| 398 | if (it->second > 1) { | ||
| 399 | it->second = it->second - 1; | ||
| 400 | } else { | ||
| 401 | written_pages.erase(it); | ||
| 402 | } | ||
| 403 | } | ||
| 404 | page_start++; | ||
| 405 | } | ||
| 406 | } | ||
| 407 | |||
| 408 | bool IsRegionWritten(const CacheAddr start, const CacheAddr end) const { | ||
| 409 | u64 page_start = start >> write_page_bit; | ||
| 410 | const u64 page_end = end >> write_page_bit; | ||
| 411 | while (page_start <= page_end) { | ||
| 412 | if (written_pages.count(page_start) > 0) { | ||
| 413 | return true; | ||
| 414 | } | ||
| 415 | page_start++; | ||
| 416 | } | ||
| 417 | return false; | ||
| 418 | } | ||
| 419 | |||
| 420 | std::unique_ptr<StreamBuffer> stream_buffer; | ||
| 421 | TBufferType stream_buffer_handle{}; | ||
| 422 | |||
| 423 | bool invalidated = false; | ||
| 424 | |||
| 425 | u8* buffer_ptr = nullptr; | ||
| 426 | u64 buffer_offset = 0; | ||
| 427 | u64 buffer_offset_base = 0; | ||
| 428 | |||
| 429 | using IntervalSet = boost::icl::interval_set<CacheAddr>; | ||
| 430 | using IntervalCache = boost::icl::interval_map<CacheAddr, MapInterval>; | ||
| 431 | using IntervalType = typename IntervalCache::interval_type; | ||
| 432 | IntervalCache mapped_addresses{}; | ||
| 433 | |||
| 434 | static constexpr u64 write_page_bit{11}; | ||
| 435 | std::unordered_map<u64, u32> written_pages{}; | ||
| 436 | |||
| 437 | static constexpr u64 block_page_bits{21}; | ||
| 438 | static constexpr u64 block_page_size{1 << block_page_bits}; | ||
| 439 | std::unordered_map<u64, TBuffer> blocks{}; | ||
| 440 | |||
| 441 | std::list<TBuffer> pending_destruction{}; | ||
| 442 | u64 epoch{}; | ||
| 443 | u64 modified_ticks{}; | ||
| 444 | VideoCore::RasterizerInterface& rasterizer; | ||
| 445 | Core::System& system; | ||
| 446 | std::recursive_mutex mutex; | ||
| 447 | }; | ||
| 448 | |||
| 449 | } // namespace VideoCommon | ||
diff --git a/src/video_core/buffer_cache/map_interval.h b/src/video_core/buffer_cache/map_interval.h new file mode 100644 index 000000000..3a104d5cd --- /dev/null +++ b/src/video_core/buffer_cache/map_interval.h | |||
| @@ -0,0 +1,89 @@ | |||
| 1 | // Copyright 2019 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "common/common_types.h" | ||
| 8 | #include "video_core/gpu.h" | ||
| 9 | |||
| 10 | namespace VideoCommon { | ||
| 11 | |||
| 12 | class MapIntervalBase { | ||
| 13 | public: | ||
| 14 | MapIntervalBase(const CacheAddr start, const CacheAddr end, const GPUVAddr gpu_addr) | ||
| 15 | : start{start}, end{end}, gpu_addr{gpu_addr} {} | ||
| 16 | |||
| 17 | void SetCpuAddress(VAddr new_cpu_addr) { | ||
| 18 | cpu_addr = new_cpu_addr; | ||
| 19 | } | ||
| 20 | |||
| 21 | VAddr GetCpuAddress() const { | ||
| 22 | return cpu_addr; | ||
| 23 | } | ||
| 24 | |||
| 25 | GPUVAddr GetGpuAddress() const { | ||
| 26 | return gpu_addr; | ||
| 27 | } | ||
| 28 | |||
| 29 | bool IsInside(const CacheAddr other_start, const CacheAddr other_end) const { | ||
| 30 | return (start <= other_start && other_end <= end); | ||
| 31 | } | ||
| 32 | |||
| 33 | bool operator==(const MapIntervalBase& rhs) const { | ||
| 34 | return std::tie(start, end) == std::tie(rhs.start, rhs.end); | ||
| 35 | } | ||
| 36 | |||
| 37 | bool operator!=(const MapIntervalBase& rhs) const { | ||
| 38 | return !operator==(rhs); | ||
| 39 | } | ||
| 40 | |||
| 41 | void MarkAsRegistered(const bool registered) { | ||
| 42 | is_registered = registered; | ||
| 43 | } | ||
| 44 | |||
| 45 | bool IsRegistered() const { | ||
| 46 | return is_registered; | ||
| 47 | } | ||
| 48 | |||
| 49 | CacheAddr GetStart() const { | ||
| 50 | return start; | ||
| 51 | } | ||
| 52 | |||
| 53 | CacheAddr GetEnd() const { | ||
| 54 | return end; | ||
| 55 | } | ||
| 56 | |||
| 57 | void MarkAsModified(const bool is_modified_, const u64 tick) { | ||
| 58 | is_modified = is_modified_; | ||
| 59 | ticks = tick; | ||
| 60 | } | ||
| 61 | |||
| 62 | bool IsModified() const { | ||
| 63 | return is_modified; | ||
| 64 | } | ||
| 65 | |||
| 66 | u64 GetModificationTick() const { | ||
| 67 | return ticks; | ||
| 68 | } | ||
| 69 | |||
| 70 | void MarkAsWritten(const bool is_written_) { | ||
| 71 | is_written = is_written_; | ||
| 72 | } | ||
| 73 | |||
| 74 | bool IsWritten() const { | ||
| 75 | return is_written; | ||
| 76 | } | ||
| 77 | |||
| 78 | private: | ||
| 79 | CacheAddr start; | ||
| 80 | CacheAddr end; | ||
| 81 | GPUVAddr gpu_addr; | ||
| 82 | VAddr cpu_addr{}; | ||
| 83 | bool is_written{}; | ||
| 84 | bool is_modified{}; | ||
| 85 | bool is_registered{}; | ||
| 86 | u64 ticks{}; | ||
| 87 | }; | ||
| 88 | |||
| 89 | } // namespace VideoCommon | ||