2019-10-13 02:02:07 -04:00
|
|
|
using Ryujinx.Common;
|
|
|
|
using Ryujinx.Graphics.GAL;
|
2020-04-22 02:00:11 -04:00
|
|
|
using Ryujinx.Graphics.Gpu.Memory;
|
2019-10-13 02:02:07 -04:00
|
|
|
using Ryujinx.Graphics.Gpu.State;
|
|
|
|
using System;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Engine
|
|
|
|
{
|
|
|
|
partial class Methods
|
|
|
|
{
|
2020-01-06 17:27:50 -05:00
|
|
|
private const int NsToTicksFractionNumerator = 384;
|
2020-01-01 10:39:09 -05:00
|
|
|
private const int NsToTicksFractionDenominator = 625;
|
|
|
|
|
2020-04-22 02:00:11 -04:00
|
|
|
private readonly CounterCache _counterCache = new CounterCache();
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Writes a GPU counter to guest memory.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="argument">Method call argument</param>
|
2019-11-21 21:46:14 -05:00
|
|
|
private void Report(GpuState state, int argument)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-05-29 04:51:10 -04:00
|
|
|
SemaphoreOperation op = (SemaphoreOperation)(argument & 3);
|
2019-10-13 02:02:07 -04:00
|
|
|
ReportCounterType type = (ReportCounterType)((argument >> 23) & 0x1f);
|
|
|
|
|
2020-05-29 04:51:10 -04:00
|
|
|
switch (op)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-05-29 04:51:10 -04:00
|
|
|
case SemaphoreOperation.Release: ReleaseSemaphore(state); break;
|
|
|
|
case SemaphoreOperation.Counter: ReportCounter(state, type); break;
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
2020-04-18 21:25:57 -04:00
|
|
|
/// Writes (or Releases) a GPU semaphore value to guest memory.
|
2019-12-31 14:19:44 -05:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2020-04-18 21:25:57 -04:00
|
|
|
private void ReleaseSemaphore(GpuState state)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-05-29 04:51:10 -04:00
|
|
|
var rs = state.Get<SemaphoreState>(MethodOffset.ReportState);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2019-11-21 21:46:14 -05:00
|
|
|
_context.MemoryAccessor.Write(rs.Address.Pack(), rs.Payload);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
_context.AdvanceSequence();
|
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Packed GPU counter data (including GPU timestamp) in memory.
|
|
|
|
/// </summary>
|
2019-10-13 02:02:07 -04:00
|
|
|
private struct CounterData
|
|
|
|
{
|
|
|
|
public ulong Counter;
|
|
|
|
public ulong Timestamp;
|
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Writes a GPU counter to guest memory.
|
|
|
|
/// This also writes the current timestamp value.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
|
|
|
/// <param name="type">Counter to be written to memory</param>
|
2019-11-21 21:46:14 -05:00
|
|
|
private void ReportCounter(GpuState state, ReportCounterType type)
|
2019-10-13 02:02:07 -04:00
|
|
|
{
|
2020-05-29 04:51:10 -04:00
|
|
|
var rs = state.Get<SemaphoreState>(MethodOffset.ReportState);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-05-03 22:24:59 -04:00
|
|
|
ulong gpuVa = rs.Address.Pack();
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-04-20 08:41:07 -04:00
|
|
|
ulong ticks = ConvertNanosecondsToTicks((ulong)PerformanceCounter.ElapsedNanoseconds);
|
2019-10-13 02:02:07 -04:00
|
|
|
|
|
|
|
if (GraphicsConfig.FastGpuTime)
|
|
|
|
{
|
2020-04-20 08:41:07 -04:00
|
|
|
// Divide by some amount to report time as if operations were performed faster than they really are.
|
|
|
|
// This can prevent some games from switching to a lower resolution because rendering is too slow.
|
|
|
|
ticks /= 256;
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2020-05-03 22:24:59 -04:00
|
|
|
ICounterEvent counter = null;
|
|
|
|
|
|
|
|
EventHandler<ulong> resultHandler = (object evt, ulong result) =>
|
|
|
|
{
|
2020-07-14 23:01:10 -04:00
|
|
|
CounterData counterData = new CounterData();
|
|
|
|
|
2020-05-03 22:24:59 -04:00
|
|
|
counterData.Counter = result;
|
|
|
|
counterData.Timestamp = ticks;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-05-03 22:24:59 -04:00
|
|
|
if (counter?.Invalid != true)
|
|
|
|
{
|
2020-07-14 23:01:10 -04:00
|
|
|
_context.MemoryAccessor.Write(gpuVa, counterData);
|
2020-05-03 22:24:59 -04:00
|
|
|
}
|
|
|
|
};
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-05-03 22:24:59 -04:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case ReportCounterType.Zero:
|
|
|
|
resultHandler(null, 0);
|
|
|
|
break;
|
|
|
|
case ReportCounterType.SamplesPassed:
|
|
|
|
counter = _context.Renderer.ReportCounter(CounterType.SamplesPassed, resultHandler);
|
|
|
|
break;
|
|
|
|
case ReportCounterType.PrimitivesGenerated:
|
|
|
|
counter = _context.Renderer.ReportCounter(CounterType.PrimitivesGenerated, resultHandler);
|
|
|
|
break;
|
|
|
|
case ReportCounterType.TransformFeedbackPrimitivesWritten:
|
|
|
|
counter = _context.Renderer.ReportCounter(CounterType.TransformFeedbackPrimitivesWritten, resultHandler);
|
|
|
|
break;
|
|
|
|
}
|
2020-04-22 02:00:11 -04:00
|
|
|
|
2020-05-03 22:24:59 -04:00
|
|
|
_counterCache.AddOrUpdate(gpuVa, counter);
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Converts a nanoseconds timestamp value to Maxwell time ticks.
|
|
|
|
/// </summary>
|
2020-01-01 10:39:09 -05:00
|
|
|
/// <remarks>
|
|
|
|
/// The frequency is 614400000 Hz.
|
|
|
|
/// </remarks>
|
2019-12-31 14:19:44 -05:00
|
|
|
/// <param name="nanoseconds">Timestamp in nanoseconds</param>
|
|
|
|
/// <returns>Maxwell ticks</returns>
|
2019-10-13 02:02:07 -04:00
|
|
|
private static ulong ConvertNanosecondsToTicks(ulong nanoseconds)
|
|
|
|
{
|
|
|
|
// We need to divide first to avoid overflows.
|
|
|
|
// We fix up the result later by calculating the difference and adding
|
|
|
|
// that to the result.
|
2020-01-01 10:39:09 -05:00
|
|
|
ulong divided = nanoseconds / NsToTicksFractionDenominator;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-01-01 10:39:09 -05:00
|
|
|
ulong rounded = divided * NsToTicksFractionDenominator;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-01-01 10:39:09 -05:00
|
|
|
ulong errorBias = (nanoseconds - rounded) * NsToTicksFractionNumerator / NsToTicksFractionDenominator;
|
2019-10-13 02:02:07 -04:00
|
|
|
|
2020-01-01 10:39:09 -05:00
|
|
|
return divided * NsToTicksFractionNumerator + errorBias;
|
2019-10-13 02:02:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|