Ryujinx/src/Ryujinx.Graphics.Vulkan/Queries/CounterQueueEvent.cs
TSRBerry 801b71a128
[Ryujinx.Graphics.Vulkan] Address dotnet-format issues (#5378)
* dotnet format style --severity info

Some changes were manually reverted.

* dotnet format analyzers --serverity info

Some changes have been minimally adapted.

* Restore a few unused methods and variables

* Silence dotnet format IDE0060 warnings

* Silence dotnet format IDE0059 warnings

* Address dotnet format CA1816 warnings

* Fix new dotnet-format issues after rebase

* Address most dotnet format whitespace warnings

* Apply dotnet format whitespace formatting

A few of them have been manually reverted and the corresponding warning was silenced

* Format if-blocks correctly

* Another rebase, another dotnet format run

* Run dotnet format whitespace after rebase

* Run dotnet format style after rebase

* Run dotnet format analyzers after rebase

* Run dotnet format style after rebase

* Run dotnet format after rebase and remove unused usings

- analyzers
- style
- whitespace

* Disable 'prefer switch expression' rule

* Add comments to disabled warnings

* Simplify properties and array initialization, Use const when possible, Remove trailing commas

* Run dotnet format after rebase

* Address IDE0251 warnings

* Address a few disabled IDE0060 warnings

* Silence IDE0060 in .editorconfig

* Revert "Simplify properties and array initialization, Use const when possible, Remove trailing commas"

This reverts commit 9462e4136c0a2100dc28b20cf9542e06790aa67e.

* dotnet format whitespace after rebase

* First dotnet format pass

* Fix naming rule violations

* Remove redundant code

* Rename generics

* Address review feedback

* Remove SetOrigin
2023-07-01 12:31:42 +02:00

170 lines
3.9 KiB
C#

using Ryujinx.Graphics.GAL;
using System;
using System.Threading;
namespace Ryujinx.Graphics.Vulkan.Queries
{
class CounterQueueEvent : ICounterEvent
{
public event EventHandler<ulong> OnResult;
public CounterType Type { get; }
public bool ClearCounter { get; private set; }
public bool Disposed { get; private set; }
public bool Invalid { get; set; }
public ulong DrawIndex { get; }
private readonly CounterQueue _queue;
private readonly BufferedQuery _counter;
private bool _hostAccessReserved;
private int _refCount = 1; // Starts with a reference from the counter queue.
private readonly object _lock = new();
private ulong _result = ulong.MaxValue;
private double _divisor = 1f;
public CounterQueueEvent(CounterQueue queue, CounterType type, ulong drawIndex)
{
_queue = queue;
_counter = queue.GetQueryObject();
Type = type;
DrawIndex = drawIndex;
_counter.Begin(_queue.ResetSequence);
}
public Auto<DisposableBuffer> GetBuffer()
{
return _counter.GetBuffer();
}
internal void Clear(bool counterReset)
{
if (counterReset)
{
_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)
{
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();
}
}
}