mirror of
https://github.com/ryujinx-mirror/ryujinx.git
synced 2025-04-17 14:34:05 -05:00

* 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 * Address or silence dotnet format IDE1006 warnings * Fix IDE0090 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 after rebase and remove unused usings - analyzers - style - whitespace * Add comments to disabled warnings * Simplify properties and array initialization, Use const when possible, Remove trailing commas * Start working on disabled 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 * Address review feedback
121 lines
3.6 KiB
C#
121 lines
3.6 KiB
C#
using OpenTK.Graphics.OpenGL;
|
|
using Ryujinx.Common.Logging;
|
|
using System;
|
|
using System.Runtime.InteropServices;
|
|
using System.Threading;
|
|
|
|
namespace Ryujinx.Graphics.OpenGL.Queries
|
|
{
|
|
class BufferedQuery : IDisposable
|
|
{
|
|
private const int MaxQueryRetries = 5000;
|
|
private const long DefaultValue = -1;
|
|
private const ulong HighMask = 0xFFFFFFFF00000000;
|
|
|
|
public int Query { get; }
|
|
|
|
private readonly int _buffer;
|
|
private readonly IntPtr _bufferMap;
|
|
private readonly QueryTarget _type;
|
|
|
|
public BufferedQuery(QueryTarget type)
|
|
{
|
|
_buffer = GL.GenBuffer();
|
|
Query = GL.GenQuery();
|
|
_type = type;
|
|
|
|
GL.BindBuffer(BufferTarget.QueryBuffer, _buffer);
|
|
|
|
unsafe
|
|
{
|
|
long defaultValue = DefaultValue;
|
|
GL.BufferStorage(BufferTarget.QueryBuffer, sizeof(long), (IntPtr)(&defaultValue), BufferStorageFlags.MapReadBit | BufferStorageFlags.MapWriteBit | BufferStorageFlags.MapPersistentBit);
|
|
}
|
|
_bufferMap = GL.MapBufferRange(BufferTarget.QueryBuffer, IntPtr.Zero, sizeof(long), BufferAccessMask.MapReadBit | BufferAccessMask.MapWriteBit | BufferAccessMask.MapPersistentBit);
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
GL.EndQuery(_type);
|
|
GL.BeginQuery(_type, Query);
|
|
}
|
|
|
|
public void Begin()
|
|
{
|
|
GL.BeginQuery(_type, Query);
|
|
}
|
|
|
|
public unsafe void End(bool withResult)
|
|
{
|
|
GL.EndQuery(_type);
|
|
|
|
if (withResult)
|
|
{
|
|
GL.BindBuffer(BufferTarget.QueryBuffer, _buffer);
|
|
|
|
Marshal.WriteInt64(_bufferMap, -1L);
|
|
GL.GetQueryObject(Query, GetQueryObjectParam.QueryResult, (long*)0);
|
|
GL.MemoryBarrier(MemoryBarrierFlags.QueryBufferBarrierBit | MemoryBarrierFlags.ClientMappedBufferBarrierBit);
|
|
}
|
|
else
|
|
{
|
|
// Dummy result, just return 0.
|
|
Marshal.WriteInt64(_bufferMap, 0L);
|
|
}
|
|
}
|
|
|
|
private static bool WaitingForValue(long data)
|
|
{
|
|
return data == DefaultValue ||
|
|
((ulong)data & HighMask) == (unchecked((ulong)DefaultValue) & HighMask);
|
|
}
|
|
|
|
public bool TryGetResult(out long result)
|
|
{
|
|
result = Marshal.ReadInt64(_bufferMap);
|
|
|
|
return !WaitingForValue(result);
|
|
}
|
|
|
|
public long AwaitResult(AutoResetEvent wakeSignal = null)
|
|
{
|
|
long data = DefaultValue;
|
|
|
|
if (wakeSignal == null)
|
|
{
|
|
while (WaitingForValue(data))
|
|
{
|
|
data = Marshal.ReadInt64(_bufferMap);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
int iterations = 0;
|
|
while (WaitingForValue(data) && iterations++ < MaxQueryRetries)
|
|
{
|
|
data = Marshal.ReadInt64(_bufferMap);
|
|
if (WaitingForValue(data))
|
|
{
|
|
wakeSignal.WaitOne(1);
|
|
}
|
|
}
|
|
|
|
if (iterations >= MaxQueryRetries)
|
|
{
|
|
Logger.Error?.Print(LogClass.Gpu, $"Error: Query result timed out. Took more than {MaxQueryRetries} tries.");
|
|
}
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
GL.BindBuffer(BufferTarget.QueryBuffer, _buffer);
|
|
GL.UnmapBuffer(BufferTarget.QueryBuffer);
|
|
GL.DeleteBuffer(_buffer);
|
|
GL.DeleteQuery(Query);
|
|
}
|
|
}
|
|
}
|