0
0
Fork 0
mirror of https://github.com/GreemDev/Ryujinx.git synced 2024-12-23 17:25:48 +00:00
Ryujinx/Ryujinx.Graphics.OpenGL/Queries/CounterQueueEvent.cs
riperiperi 6e9bd4de13
GPU: Scale counter results before addition (#4471)
* GPU: Scale counter results before addition

Counter results were being scaled on ReportCounter, which meant that the _total_ value of the counter was being scaled. Not only could this result in very large numbers and weird overflows if the game doesn't clear the counter, but it also caused the result to change drastically.

This PR changes scaling to be done when the value is added to the counter on the backend. This should evaluate the scale at the same time as before, on report counter, but avoiding the issue with scaling the total.

Fixes scaling in Warioware, at least in the demo, where it seems to compare old/new counters and broke down when scaling was enabled.

* Fix issues when result is partially uploaded.

Drivers tend to write the low half first, then the high half. Retry if the high half is FFFFFFFF.
2023-03-12 18:01:15 +01:00

163 lines
3.8 KiB
C#

using OpenTK.Graphics.OpenGL;
using Ryujinx.Graphics.GAL;
using System;
using System.Threading;
namespace Ryujinx.Graphics.OpenGL.Queries
{
class CounterQueueEvent : ICounterEvent
{
public event EventHandler<ulong> OnResult;
public QueryTarget Type { get; }
public bool ClearCounter { get; private set; }
public int Query => _counter.Query;
public bool Disposed { get; private set; }
public bool Invalid { get; set; }
public ulong DrawIndex { get; }
private CounterQueue _queue;
private BufferedQuery _counter;
private bool _hostAccessReserved = false;
private int _refCount = 1; // Starts with a reference from the counter queue.
private object _lock = new object();
private ulong _result = ulong.MaxValue;
private double _divisor = 1f;
public CounterQueueEvent(CounterQueue queue, QueryTarget type, ulong drawIndex)
{
_queue = queue;
_counter = queue.GetQueryObject();
Type = type;
DrawIndex = drawIndex;
_counter.Begin();
}
internal void Clear()
{
_counter.Reset();
ClearCounter = true;
}
internal void Complete(bool withResult, double divisor)
{
_counter.End(withResult);
_divisor = divisor;
}
internal bool TryConsume(ref ulong result, bool block, AutoResetEvent wakeSignal = null)
{
lock (_lock)
{
if (Disposed)
{
return true;
}
if (ClearCounter || Type == QueryTarget.Timestamp)
{
result = 0;
}
long queryResult;
if (block)
{
queryResult = _counter.AwaitResult(wakeSignal);
}
else
{
if (!_counter.TryGetResult(out queryResult))
{
return false;
}
}
result += _divisor == 1 ? (ulong)queryResult : (ulong)Math.Ceiling(queryResult / _divisor);
_result = result;
OnResult?.Invoke(this, result);
Dispose(); // Return the our resources to the pool.
return true;
}
}
public void Flush()
{
if (Disposed)
{
return;
}
// Tell the queue to process all events up to this one.
_queue.FlushTo(this);
}
public void DecrementRefCount()
{
if (Interlocked.Decrement(ref _refCount) == 0)
{
DisposeInternal();
}
}
public bool ReserveForHostAccess()
{
if (_hostAccessReserved)
{
return true;
}
if (IsValueAvailable())
{
return false;
}
if (Interlocked.Increment(ref _refCount) == 1)
{
Interlocked.Decrement(ref _refCount);
return false;
}
_hostAccessReserved = true;
return true;
}
public void ReleaseHostAccess()
{
_hostAccessReserved = false;
DecrementRefCount();
}
private void DisposeInternal()
{
_queue.ReturnQueryObject(_counter);
}
private bool IsValueAvailable()
{
return _result != ulong.MaxValue || _counter.TryGetResult(out _);
}
public void Dispose()
{
Disposed = true;
DecrementRefCount();
}
}
}