rasterizer_accelerated.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <atomic>
  4. #include "common/assert.h"
  5. #include "common/common_types.h"
  6. #include "common/div_ceil.h"
  7. #include "core/memory.h"
  8. #include "video_core/rasterizer_accelerated.h"
  9. namespace VideoCore {
  10. using namespace Core::Memory;
  11. RasterizerAccelerated::RasterizerAccelerated(Memory& cpu_memory_) : cpu_memory{cpu_memory_} {}
  12. RasterizerAccelerated::~RasterizerAccelerated() = default;
  13. void RasterizerAccelerated::UpdatePagesCachedCount(VAddr addr, u64 size, int delta) {
  14. u64 uncache_begin = 0;
  15. u64 cache_begin = 0;
  16. u64 uncache_bytes = 0;
  17. u64 cache_bytes = 0;
  18. std::atomic_thread_fence(std::memory_order_acquire);
  19. const u64 page_end = Common::DivCeil(addr + size, YUZU_PAGESIZE);
  20. for (u64 page = addr >> YUZU_PAGEBITS; page != page_end; ++page) {
  21. std::atomic_uint16_t& count = cached_pages.at(page >> 2).Count(page);
  22. if (delta > 0) {
  23. ASSERT_MSG(count.load(std::memory_order::relaxed) < UINT16_MAX, "Count may overflow!");
  24. } else if (delta < 0) {
  25. ASSERT_MSG(count.load(std::memory_order::relaxed) > 0, "Count may underflow!");
  26. } else {
  27. ASSERT_MSG(false, "Delta must be non-zero!");
  28. }
  29. // Adds or subtracts 1, as count is a unsigned 8-bit value
  30. count.fetch_add(static_cast<u16>(delta), std::memory_order_release);
  31. // Assume delta is either -1 or 1
  32. if (count.load(std::memory_order::relaxed) == 0) {
  33. if (uncache_bytes == 0) {
  34. uncache_begin = page;
  35. }
  36. uncache_bytes += YUZU_PAGESIZE;
  37. } else if (uncache_bytes > 0) {
  38. cpu_memory.RasterizerMarkRegionCached(uncache_begin << YUZU_PAGEBITS, uncache_bytes,
  39. false);
  40. uncache_bytes = 0;
  41. }
  42. if (count.load(std::memory_order::relaxed) == 1 && delta > 0) {
  43. if (cache_bytes == 0) {
  44. cache_begin = page;
  45. }
  46. cache_bytes += YUZU_PAGESIZE;
  47. } else if (cache_bytes > 0) {
  48. cpu_memory.RasterizerMarkRegionCached(cache_begin << YUZU_PAGEBITS, cache_bytes, true);
  49. cache_bytes = 0;
  50. }
  51. }
  52. if (uncache_bytes > 0) {
  53. cpu_memory.RasterizerMarkRegionCached(uncache_begin << YUZU_PAGEBITS, uncache_bytes, false);
  54. }
  55. if (cache_bytes > 0) {
  56. cpu_memory.RasterizerMarkRegionCached(cache_begin << YUZU_PAGEBITS, cache_bytes, true);
  57. }
  58. }
  59. } // namespace VideoCore