2018-11-17 05:01:31 +01:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Threading;
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
namespace Ryujinx.Graphics.Gpu
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// GPU DMA pusher, used to push commands to the GPU.
|
|
|
|
/// </summary>
|
2018-11-17 05:01:31 +01:00
|
|
|
public class DmaPusher
|
|
|
|
{
|
2019-10-13 08:02:07 +02:00
|
|
|
private ConcurrentQueue<ulong> _ibBuffer;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
private ulong _dmaPut;
|
|
|
|
private ulong _dmaGet;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Internal GPFIFO state.
|
|
|
|
/// </summary>
|
2018-11-17 05:01:31 +01:00
|
|
|
private struct DmaState
|
|
|
|
{
|
|
|
|
public int Method;
|
|
|
|
public int SubChannel;
|
|
|
|
public int MethodCount;
|
|
|
|
public bool NonIncrementing;
|
|
|
|
public bool IncrementOnce;
|
|
|
|
public int LengthPending;
|
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private DmaState _state;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private bool _sliEnable;
|
|
|
|
private bool _sliActive;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private bool _ibEnable;
|
|
|
|
private bool _nonMain;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
private ulong _dmaMGet;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
private GpuContext _context;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
private AutoResetEvent _event;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new instance of the GPU DMA pusher.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="context">GPU context that the pusher belongs to</param>
|
2019-10-13 08:02:07 +02:00
|
|
|
internal DmaPusher(GpuContext context)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-10-13 08:02:07 +02:00
|
|
|
_context = context;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
_ibBuffer = new ConcurrentQueue<ulong>();
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_ibEnable = true;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_event = new AutoResetEvent(false);
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Pushes a GPFIFO entry.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="entry">GPFIFO entry</param>
|
2019-10-13 08:02:07 +02:00
|
|
|
public void Push(ulong entry)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-10-13 08:02:07 +02:00
|
|
|
_ibBuffer.Enqueue(entry);
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_event.Set();
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Waits until commands are pushed to the FIFO.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>True if commands were received, false if wait timed out</returns>
|
2018-11-17 05:01:31 +01:00
|
|
|
public bool WaitForCommands()
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
return _event.WaitOne(8);
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Processes commands pushed to the FIFO.
|
|
|
|
/// </summary>
|
2018-11-17 05:01:31 +01:00
|
|
|
public void DispatchCalls()
|
|
|
|
{
|
|
|
|
while (Step());
|
|
|
|
}
|
|
|
|
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Processes a single command on the FIFO.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
2018-11-17 05:01:31 +01:00
|
|
|
private bool Step()
|
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
if (_dmaGet != _dmaPut)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-10-13 08:02:07 +02:00
|
|
|
int word = _context.MemoryAccessor.ReadInt32(_dmaGet);
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_dmaGet += 4;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
if (!_nonMain)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
_dmaMGet = _dmaGet;
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
if (_state.LengthPending != 0)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
_state.LengthPending = 0;
|
|
|
|
_state.MethodCount = word & 0xffffff;
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
2019-03-04 02:45:25 +01:00
|
|
|
else if (_state.MethodCount != 0)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
if (!_sliEnable || _sliActive)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
CallMethod(word);
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
if (!_state.NonIncrementing)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
_state.Method++;
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
if (_state.IncrementOnce)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
_state.NonIncrementing = true;
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_state.MethodCount--;
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-07-02 04:39:22 +02:00
|
|
|
int submissionMode = (word >> 29) & 7;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-07-02 04:39:22 +02:00
|
|
|
switch (submissionMode)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
|
|
|
case 1:
|
2019-07-02 04:39:22 +02:00
|
|
|
// Incrementing.
|
2019-03-04 02:45:25 +01:00
|
|
|
SetNonImmediateState(word);
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_state.NonIncrementing = false;
|
|
|
|
_state.IncrementOnce = false;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
2019-07-02 04:39:22 +02:00
|
|
|
// Non-incrementing.
|
2019-03-04 02:45:25 +01:00
|
|
|
SetNonImmediateState(word);
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_state.NonIncrementing = true;
|
|
|
|
_state.IncrementOnce = false;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
2019-07-02 04:39:22 +02:00
|
|
|
// Immediate.
|
2019-03-04 02:45:25 +01:00
|
|
|
_state.Method = (word >> 0) & 0x1fff;
|
|
|
|
_state.SubChannel = (word >> 13) & 7;
|
|
|
|
_state.NonIncrementing = true;
|
|
|
|
_state.IncrementOnce = false;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
CallMethod((word >> 16) & 0x1fff);
|
2018-11-17 05:01:31 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
2019-07-02 04:39:22 +02:00
|
|
|
// Increment-once.
|
2019-03-04 02:45:25 +01:00
|
|
|
SetNonImmediateState(word);
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_state.NonIncrementing = false;
|
|
|
|
_state.IncrementOnce = true;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
else if (_ibEnable && _ibBuffer.TryDequeue(out ulong entry))
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-10-13 08:02:07 +02:00
|
|
|
ulong length = (entry >> 42) & 0x1fffff;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-03-04 02:45:25 +01:00
|
|
|
_dmaGet = entry & 0xfffffffffc;
|
|
|
|
_dmaPut = _dmaGet + length * 4;
|
2018-11-17 05:01:31 +01:00
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
_nonMain = (entry & (1UL << 41)) != 0;
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Sets current non-immediate method call state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="word">Compressed method word</param>
|
2019-03-04 02:45:25 +01:00
|
|
|
private void SetNonImmediateState(int word)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-03-04 02:45:25 +01:00
|
|
|
_state.Method = (word >> 0) & 0x1fff;
|
|
|
|
_state.SubChannel = (word >> 13) & 7;
|
|
|
|
_state.MethodCount = (word >> 16) & 0x1fff;
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 21:08:20 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Forwards the method call to GPU engines.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="argument">Call argument</param>
|
2019-03-04 02:45:25 +01:00
|
|
|
private void CallMethod(int argument)
|
2018-11-17 05:01:31 +01:00
|
|
|
{
|
2019-10-13 08:02:07 +02:00
|
|
|
_context.Fifo.CallMethod(new MethodParams(
|
2019-03-04 02:45:25 +01:00
|
|
|
_state.Method,
|
|
|
|
argument,
|
|
|
|
_state.SubChannel,
|
|
|
|
_state.MethodCount));
|
2018-11-17 05:01:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|