From 6bfe4715f05be10c73a788abd8727293a7eca77e Mon Sep 17 00:00:00 2001 From: gdkchan Date: Wed, 22 Apr 2020 03:00:11 -0300 Subject: Initial conditional rendering support (#1012) * Initial conditional rendering support * Properly reset state * Support conditional modes and skeleton a counter cache for future host conditional rendering * Address PR feedback --- Ryujinx.Graphics.Gpu/Memory/CounterCache.cs | 140 ++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 Ryujinx.Graphics.Gpu/Memory/CounterCache.cs (limited to 'Ryujinx.Graphics.Gpu/Memory/CounterCache.cs') diff --git a/Ryujinx.Graphics.Gpu/Memory/CounterCache.cs b/Ryujinx.Graphics.Gpu/Memory/CounterCache.cs new file mode 100644 index 00000000..3110cdcc --- /dev/null +++ b/Ryujinx.Graphics.Gpu/Memory/CounterCache.cs @@ -0,0 +1,140 @@ +using System.Collections.Generic; + +namespace Ryujinx.Graphics.Gpu.Memory +{ + /// + /// Represents the GPU counter cache. + /// + class CounterCache + { + private struct CounterEntry + { + public ulong Address { get; } + + public CounterEntry(ulong address) + { + Address = address; + } + } + + private readonly List _items; + + /// + /// Creates a new instance of the GPU counter cache. + /// + public CounterCache() + { + _items = new List(); + } + + /// + /// Adds a new counter to the counter cache, or updates a existing one. + /// + /// GPU virtual address where the counter will be written in memory + public void AddOrUpdate(ulong gpuVa) + { + int index = BinarySearch(gpuVa); + + CounterEntry entry = new CounterEntry(gpuVa); + + if (index < 0) + { + _items.Insert(~index, entry); + } + else + { + _items[index] = entry; + } + } + + /// + /// Handles removal of counters written to a memory region being unmapped. + /// + /// Sender object + /// Event arguments + public void MemoryUnmappedHandler(object sender, UnmapEventArgs e) => RemoveRange(e.Address, e.Size); + + private void RemoveRange(ulong gpuVa, ulong size) + { + int index = BinarySearch(gpuVa + size - 1); + + if (index < 0) + { + index = ~index; + } + + if (index >= _items.Count || !InRange(gpuVa, size, _items[index].Address)) + { + return; + } + + int count = 1; + + while (index > 0 && InRange(gpuVa, size, _items[index - 1].Address)) + { + index--; + count++; + } + + _items.RemoveRange(index, count); + } + + /// + /// Checks whenever an address falls inside a given range. + /// + /// Range start address + /// Range size + /// Address being checked + /// True if the address falls inside the range, false otherwise + private static bool InRange(ulong startVa, ulong size, ulong gpuVa) + { + return gpuVa >= startVa && gpuVa < startVa + size; + } + + /// + /// Check if any counter value was written to the specified GPU virtual memory address. + /// + /// GPU virtual address + /// True if any counter value was written on the specified address, false otherwise + public bool Contains(ulong gpuVa) + { + return BinarySearch(gpuVa) >= 0; + } + + /// + /// Performs binary search of an address on the list. + /// + /// Address to search + /// Index of the item, or complement of the index of the nearest item with lower value + private int BinarySearch(ulong address) + { + int left = 0; + int right = _items.Count - 1; + + while (left <= right) + { + int range = right - left; + + int middle = left + (range >> 1); + + CounterEntry item = _items[middle]; + + if (item.Address == address) + { + return middle; + } + + if (address < item.Address) + { + right = middle - 1; + } + else + { + left = middle + 1; + } + } + + return ~left; + } + } +} -- cgit v1.2.3