2019-10-12 23:02:07 -07:00
|
|
|
using Ryujinx.Common;
|
|
|
|
using Ryujinx.Graphics.GAL;
|
|
|
|
using Ryujinx.Graphics.Gpu.State;
|
|
|
|
using System;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gpu.Engine
|
|
|
|
{
|
|
|
|
partial class Methods
|
|
|
|
{
|
2020-01-01 07:39:09 -08:00
|
|
|
private const int NsToTicksFractionNumerator = 384;
|
|
|
|
private const int NsToTicksFractionDenominator = 625;
|
|
|
|
|
2019-10-12 23:02:07 -07:00
|
|
|
private ulong _runningCounter;
|
|
|
|
|
2019-12-31 11:19:44 -08: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 18:46:14 -08:00
|
|
|
private void Report(GpuState state, int argument)
|
2019-10-12 23:02:07 -07:00
|
|
|
{
|
|
|
|
ReportMode mode = (ReportMode)(argument & 3);
|
|
|
|
|
|
|
|
ReportCounterType type = (ReportCounterType)((argument >> 23) & 0x1f);
|
|
|
|
|
|
|
|
switch (mode)
|
|
|
|
{
|
2019-11-21 18:46:14 -08:00
|
|
|
case ReportMode.Semaphore: ReportSemaphore(state); break;
|
|
|
|
case ReportMode.Counter: ReportCounter(state, type); break;
|
2019-10-12 23:02:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 11:19:44 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Writes a GPU semaphore value to guest memory.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="state">Current GPU state</param>
|
2019-11-21 18:46:14 -08:00
|
|
|
private void ReportSemaphore(GpuState state)
|
2019-10-12 23:02:07 -07:00
|
|
|
{
|
2019-11-21 18:46:14 -08:00
|
|
|
var rs = state.Get<ReportState>(MethodOffset.ReportState);
|
2019-10-12 23:02:07 -07:00
|
|
|
|
2019-11-21 18:46:14 -08:00
|
|
|
_context.MemoryAccessor.Write(rs.Address.Pack(), rs.Payload);
|
2019-10-12 23:02:07 -07:00
|
|
|
|
|
|
|
_context.AdvanceSequence();
|
|
|
|
}
|
|
|
|
|
2019-12-31 11:19:44 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Packed GPU counter data (including GPU timestamp) in memory.
|
|
|
|
/// </summary>
|
2019-10-12 23:02:07 -07:00
|
|
|
private struct CounterData
|
|
|
|
{
|
|
|
|
public ulong Counter;
|
|
|
|
public ulong Timestamp;
|
|
|
|
}
|
|
|
|
|
2019-12-31 11:19:44 -08: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 18:46:14 -08:00
|
|
|
private void ReportCounter(GpuState state, ReportCounterType type)
|
2019-10-12 23:02:07 -07:00
|
|
|
{
|
|
|
|
CounterData counterData = new CounterData();
|
|
|
|
|
|
|
|
ulong counter = 0;
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case ReportCounterType.Zero:
|
|
|
|
counter = 0;
|
|
|
|
break;
|
|
|
|
case ReportCounterType.SamplesPassed:
|
|
|
|
counter = _context.Renderer.GetCounter(CounterType.SamplesPassed);
|
|
|
|
break;
|
|
|
|
case ReportCounterType.PrimitivesGenerated:
|
|
|
|
counter = _context.Renderer.GetCounter(CounterType.PrimitivesGenerated);
|
|
|
|
break;
|
|
|
|
case ReportCounterType.TransformFeedbackPrimitivesWritten:
|
|
|
|
counter = _context.Renderer.GetCounter(CounterType.TransformFeedbackPrimitivesWritten);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong ticks;
|
|
|
|
|
|
|
|
if (GraphicsConfig.FastGpuTime)
|
|
|
|
{
|
|
|
|
ticks = _runningCounter++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ticks = ConvertNanosecondsToTicks((ulong)PerformanceCounter.ElapsedNanoseconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
counterData.Counter = counter;
|
|
|
|
counterData.Timestamp = ticks;
|
|
|
|
|
|
|
|
Span<CounterData> counterDataSpan = MemoryMarshal.CreateSpan(ref counterData, 1);
|
|
|
|
|
|
|
|
Span<byte> data = MemoryMarshal.Cast<CounterData, byte>(counterDataSpan);
|
|
|
|
|
2019-11-21 18:46:14 -08:00
|
|
|
var rs = state.Get<ReportState>(MethodOffset.ReportState);
|
2019-10-12 23:02:07 -07:00
|
|
|
|
2019-11-21 18:46:14 -08:00
|
|
|
_context.MemoryAccessor.Write(rs.Address.Pack(), data);
|
2019-10-12 23:02:07 -07:00
|
|
|
}
|
|
|
|
|
2019-12-31 11:19:44 -08:00
|
|
|
/// <summary>
|
|
|
|
/// Converts a nanoseconds timestamp value to Maxwell time ticks.
|
|
|
|
/// </summary>
|
2020-01-01 07:39:09 -08:00
|
|
|
/// <remarks>
|
|
|
|
/// The frequency is 614400000 Hz.
|
|
|
|
/// </remarks>
|
2019-12-31 11:19:44 -08:00
|
|
|
/// <param name="nanoseconds">Timestamp in nanoseconds</param>
|
|
|
|
/// <returns>Maxwell ticks</returns>
|
2019-10-12 23:02:07 -07: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 07:39:09 -08:00
|
|
|
ulong divided = nanoseconds / NsToTicksFractionDenominator;
|
2019-10-12 23:02:07 -07:00
|
|
|
|
2020-01-01 07:39:09 -08:00
|
|
|
ulong rounded = divided * NsToTicksFractionDenominator;
|
2019-10-12 23:02:07 -07:00
|
|
|
|
2020-01-01 07:39:09 -08:00
|
|
|
ulong errorBias = (nanoseconds - rounded) * NsToTicksFractionNumerator / NsToTicksFractionDenominator;
|
2019-10-12 23:02:07 -07:00
|
|
|
|
2020-01-01 07:39:09 -08:00
|
|
|
return divided * NsToTicksFractionNumerator + errorBias;
|
2019-10-12 23:02:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|