2020-04-22 08:00:11 +02:00
|
|
|
|
using Ryujinx.Common.Logging;
|
2020-05-04 04:24:59 +02:00
|
|
|
|
using Ryujinx.Graphics.GAL;
|
2021-07-11 22:20:40 +02:00
|
|
|
|
using Ryujinx.Graphics.Gpu.Engine.Types;
|
2021-06-29 19:32:02 +02:00
|
|
|
|
using Ryujinx.Graphics.Gpu.Memory;
|
2020-04-22 08:00:11 +02:00
|
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Engine.Threed
|
2020-04-22 08:00:11 +02:00
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Helper methods used for conditional rendering.
|
|
|
|
|
/// </summary>
|
|
|
|
|
static class ConditionalRendering
|
2020-04-22 08:00:11 +02:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks if draws and clears should be performed, according
|
|
|
|
|
/// to currently set conditional rendering conditions.
|
|
|
|
|
/// </summary>
|
2021-07-11 22:20:40 +02:00
|
|
|
|
/// <param name="context">GPU context</param>
|
|
|
|
|
/// <param name="memoryManager">Memory manager bound to the channel currently executing</param>
|
|
|
|
|
/// <param name="address">Conditional rendering buffer address</param>
|
|
|
|
|
/// <param name="condition">Conditional rendering condition</param>
|
2020-04-22 08:00:11 +02:00
|
|
|
|
/// <returns>True if rendering is enabled, false otherwise</returns>
|
2021-07-11 22:20:40 +02:00
|
|
|
|
public static ConditionalRenderEnabled GetRenderEnable(GpuContext context, MemoryManager memoryManager, GpuVa address, Condition condition)
|
2020-04-22 08:00:11 +02:00
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
|
switch (condition)
|
2020-04-22 08:00:11 +02:00
|
|
|
|
{
|
|
|
|
|
case Condition.Always:
|
2020-05-04 04:24:59 +02:00
|
|
|
|
return ConditionalRenderEnabled.True;
|
2020-04-22 08:00:11 +02:00
|
|
|
|
case Condition.Never:
|
2020-05-04 04:24:59 +02:00
|
|
|
|
return ConditionalRenderEnabled.False;
|
2020-04-22 08:00:11 +02:00
|
|
|
|
case Condition.ResultNonZero:
|
2021-07-11 22:20:40 +02:00
|
|
|
|
return CounterNonZero(context, memoryManager, address.Pack());
|
2020-04-22 08:00:11 +02:00
|
|
|
|
case Condition.Equal:
|
2021-07-11 22:20:40 +02:00
|
|
|
|
return CounterCompare(context, memoryManager, address.Pack(), true);
|
2020-04-22 08:00:11 +02:00
|
|
|
|
case Condition.NotEqual:
|
2021-07-11 22:20:40 +02:00
|
|
|
|
return CounterCompare(context, memoryManager, address.Pack(), false);
|
2020-04-22 08:00:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
|
Logger.Warning?.Print(LogClass.Gpu, $"Invalid conditional render condition \"{condition}\".");
|
2020-04-22 08:00:11 +02:00
|
|
|
|
|
2020-05-04 04:24:59 +02:00
|
|
|
|
return ConditionalRenderEnabled.True;
|
2020-04-22 08:00:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks if the counter value at a given GPU memory address is non-zero.
|
|
|
|
|
/// </summary>
|
2021-07-11 22:20:40 +02:00
|
|
|
|
/// <param name="context">GPU context</param>
|
|
|
|
|
/// <param name="memoryManager">Memory manager bound to the channel currently executing</param>
|
2020-04-22 08:00:11 +02:00
|
|
|
|
/// <param name="gpuVa">GPU virtual address of the counter value</param>
|
2020-05-04 04:24:59 +02:00
|
|
|
|
/// <returns>True if the value is not zero, false otherwise. Returns host if handling with host conditional rendering</returns>
|
2021-07-11 22:20:40 +02:00
|
|
|
|
private static ConditionalRenderEnabled CounterNonZero(GpuContext context, MemoryManager memoryManager, ulong gpuVa)
|
2020-04-22 08:00:11 +02:00
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
|
ICounterEvent evt = memoryManager.CounterCache.FindEvent(gpuVa);
|
2020-05-04 04:24:59 +02:00
|
|
|
|
|
|
|
|
|
if (evt == null)
|
2020-04-22 08:00:11 +02:00
|
|
|
|
{
|
2020-05-04 04:24:59 +02:00
|
|
|
|
return ConditionalRenderEnabled.False;
|
2020-04-22 08:00:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
|
if (context.Renderer.Pipeline.TryHostConditionalRendering(evt, 0L, false))
|
2020-05-04 04:24:59 +02:00
|
|
|
|
{
|
|
|
|
|
return ConditionalRenderEnabled.Host;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
evt.Flush();
|
2022-11-16 18:53:04 +01:00
|
|
|
|
return (memoryManager.Read<ulong>(gpuVa, true) != 0) ? ConditionalRenderEnabled.True : ConditionalRenderEnabled.False;
|
2020-05-04 04:24:59 +02:00
|
|
|
|
}
|
2020-04-22 08:00:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks if the counter at a given GPU memory address passes a specified equality comparison.
|
|
|
|
|
/// </summary>
|
2021-07-11 22:20:40 +02:00
|
|
|
|
/// <param name="context">GPU context</param>
|
|
|
|
|
/// <param name="memoryManager">Memory manager bound to the channel currently executing</param>
|
2020-04-22 08:00:11 +02:00
|
|
|
|
/// <param name="gpuVa">GPU virtual address</param>
|
|
|
|
|
/// <param name="isEqual">True to check if the values are equal, false to check if they are not equal</param>
|
2020-05-04 04:24:59 +02:00
|
|
|
|
/// <returns>True if the condition is met, false otherwise. Returns host if handling with host conditional rendering</returns>
|
2021-07-11 22:20:40 +02:00
|
|
|
|
private static ConditionalRenderEnabled CounterCompare(GpuContext context, MemoryManager memoryManager, ulong gpuVa, bool isEqual)
|
2020-04-22 08:00:11 +02:00
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
|
ICounterEvent evt = FindEvent(memoryManager.CounterCache, gpuVa);
|
|
|
|
|
ICounterEvent evt2 = FindEvent(memoryManager.CounterCache, gpuVa + 16);
|
2020-05-04 04:24:59 +02:00
|
|
|
|
|
|
|
|
|
bool useHost;
|
|
|
|
|
|
|
|
|
|
if (evt != null && evt2 == null)
|
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
|
useHost = context.Renderer.Pipeline.TryHostConditionalRendering(evt, memoryManager.Read<ulong>(gpuVa + 16), isEqual);
|
2020-05-04 04:24:59 +02:00
|
|
|
|
}
|
|
|
|
|
else if (evt == null && evt2 != null)
|
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
|
useHost = context.Renderer.Pipeline.TryHostConditionalRendering(evt2, memoryManager.Read<ulong>(gpuVa), isEqual);
|
2020-05-04 04:24:59 +02:00
|
|
|
|
}
|
2021-01-26 22:42:12 +01:00
|
|
|
|
else if (evt != null && evt2 != null)
|
2020-04-22 08:00:11 +02:00
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
|
useHost = context.Renderer.Pipeline.TryHostConditionalRendering(evt, evt2, isEqual);
|
2020-04-22 08:00:11 +02:00
|
|
|
|
}
|
2021-01-26 22:42:12 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
useHost = false;
|
|
|
|
|
}
|
2020-04-22 08:00:11 +02:00
|
|
|
|
|
2020-05-04 04:24:59 +02:00
|
|
|
|
if (useHost)
|
|
|
|
|
{
|
|
|
|
|
return ConditionalRenderEnabled.Host;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
evt?.Flush();
|
|
|
|
|
evt2?.Flush();
|
2020-04-22 08:00:11 +02:00
|
|
|
|
|
2022-11-16 18:53:04 +01:00
|
|
|
|
ulong x = memoryManager.Read<ulong>(gpuVa, true);
|
|
|
|
|
ulong y = memoryManager.Read<ulong>(gpuVa + 16, true);
|
2020-05-04 04:24:59 +02:00
|
|
|
|
|
|
|
|
|
return (isEqual ? x == y : x != y) ? ConditionalRenderEnabled.True : ConditionalRenderEnabled.False;
|
|
|
|
|
}
|
2020-04-22 08:00:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Tries to find a counter that is supposed to be written at the specified address,
|
2020-05-04 04:24:59 +02:00
|
|
|
|
/// returning the related event.
|
2020-04-22 08:00:11 +02:00
|
|
|
|
/// </summary>
|
2021-06-29 19:32:02 +02:00
|
|
|
|
/// <param name="counterCache">GPU counter cache to search on</param>
|
2020-04-22 08:00:11 +02:00
|
|
|
|
/// <param name="gpuVa">GPU virtual address where the counter is supposed to be written</param>
|
2020-05-04 04:24:59 +02:00
|
|
|
|
/// <returns>The counter event, or null if not present</returns>
|
2021-06-29 19:32:02 +02:00
|
|
|
|
private static ICounterEvent FindEvent(CounterCache counterCache, ulong gpuVa)
|
2020-04-22 08:00:11 +02:00
|
|
|
|
{
|
2021-06-29 19:32:02 +02:00
|
|
|
|
return counterCache.FindEvent(gpuVa);
|
2020-04-22 08:00:11 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|