aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Gpu/GpuContext.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Ryujinx.Graphics.Gpu/GpuContext.cs')
-rw-r--r--Ryujinx.Graphics.Gpu/GpuContext.cs44
1 files changed, 44 insertions, 0 deletions
diff --git a/Ryujinx.Graphics.Gpu/GpuContext.cs b/Ryujinx.Graphics.Gpu/GpuContext.cs
index ddc95b2c..8ea7c91f 100644
--- a/Ryujinx.Graphics.Gpu/GpuContext.cs
+++ b/Ryujinx.Graphics.Gpu/GpuContext.cs
@@ -1,3 +1,4 @@
+using Ryujinx.Common;
using Ryujinx.Graphics.GAL;
using Ryujinx.Graphics.Gpu.Engine.GPFifo;
using Ryujinx.Graphics.Gpu.Memory;
@@ -15,6 +16,9 @@ namespace Ryujinx.Graphics.Gpu
/// </summary>
public sealed class GpuContext : IDisposable
{
+ private const int NsToTicksFractionNumerator = 384;
+ private const int NsToTicksFractionDenominator = 625;
+
/// <summary>
/// Event signaled when the host emulation context is ready to be used by the gpu context.
/// </summary>
@@ -181,6 +185,46 @@ namespace Ryujinx.Graphics.Gpu
}
/// <summary>
+ /// Converts a nanoseconds timestamp value to Maxwell time ticks.
+ /// </summary>
+ /// <remarks>
+ /// The frequency is 614400000 Hz.
+ /// </remarks>
+ /// <param name="nanoseconds">Timestamp in nanoseconds</param>
+ /// <returns>Maxwell ticks</returns>
+ 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.
+ ulong divided = nanoseconds / NsToTicksFractionDenominator;
+
+ ulong rounded = divided * NsToTicksFractionDenominator;
+
+ ulong errorBias = (nanoseconds - rounded) * NsToTicksFractionNumerator / NsToTicksFractionDenominator;
+
+ return divided * NsToTicksFractionNumerator + errorBias;
+ }
+
+ /// <summary>
+ /// Gets the value of the GPU timer.
+ /// </summary>
+ /// <returns>The current GPU timestamp</returns>
+ public ulong GetTimestamp()
+ {
+ ulong ticks = ConvertNanosecondsToTicks((ulong)PerformanceCounter.ElapsedNanoseconds);
+
+ if (GraphicsConfig.FastGpuTime)
+ {
+ // 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;
+ }
+
+ return ticks;
+ }
+
+ /// <summary>
/// Shader cache state update handler.
/// </summary>
/// <param name="state">Current state of the shader cache load process</param>