mirror of
https://git.naxdy.org/Mirror/Ryujinx.git
synced 2024-12-28 11:33:06 +00:00
646d4dd5c7
This fixes a rare bug where reporting a counter for a region containing 0 draws could deadlock the GPU. If this write overlaps with a tracking action, then the GPU could end up waiting on something that it's meant to do in the future, so it would just get stuck. Before, this reported immediately and wrote the result to guest memory (tracked) from the backend thread. The backend thread cannot be allowed to trigger read actions that wait on the GPU, as it will end up waiting on itself, and never advancing. In the case of backend multithreading's `SyncMap`, it would try to wait for a backend sync object that does not yet exist, as the sync object would exist according to the GPU and tracking, but it has not yet been created by the backend (and never will be, since it's stuck). The fix is to queue the 0 draw event just like any other, its _bufferMap value is just forced to 0. This affects games that use Conditional Rendering: SMO, Splatoon 2, MK8. Was generally indicated by a red message in log saying that the query result timed out after 5000 tries, but not always the cause.
113 lines
3.3 KiB
C#
113 lines
3.3 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;
|
|
|
|
public int Query { get; }
|
|
|
|
private int _buffer;
|
|
private IntPtr _bufferMap;
|
|
private 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);
|
|
}
|
|
}
|
|
|
|
public bool TryGetResult(out long result)
|
|
{
|
|
result = Marshal.ReadInt64(_bufferMap);
|
|
|
|
return result != DefaultValue;
|
|
}
|
|
|
|
public long AwaitResult(AutoResetEvent wakeSignal = null)
|
|
{
|
|
long data = DefaultValue;
|
|
|
|
if (wakeSignal == null)
|
|
{
|
|
while (data == DefaultValue)
|
|
{
|
|
data = Marshal.ReadInt64(_bufferMap);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
int iterations = 0;
|
|
while (data == DefaultValue && iterations++ < MaxQueryRetries)
|
|
{
|
|
data = Marshal.ReadInt64(_bufferMap);
|
|
if (data == DefaultValue)
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
}
|