rasterizer_accelerated.cpp 2.5 KB

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