using Ryujinx.Graphics.Device;
using Ryujinx.Graphics.Gpu.Engine.GPFifo;
using System;
namespace Ryujinx.Graphics.Gpu.Engine.MME
{
///
/// GPU macro program.
///
struct Macro
{
///
/// Word offset of the code on the code memory.
///
public int Position { get; }
private IMacroEE _executionEngine;
private bool _executionPending;
private int _argument;
private MacroHLEFunctionName _hleFunction;
///
/// Creates a new instance of the GPU cached macro program.
///
/// Macro code start position
public Macro(int position)
{
Position = position;
_executionEngine = null;
_executionPending = false;
_argument = 0;
_hleFunction = MacroHLEFunctionName.None;
}
///
/// Sets the first argument for the macro call.
///
/// GPU context where the macro code is being executed
/// GPU GP FIFO command processor
/// Code to be executed
/// First argument
public void StartExecution(GpuContext context, GPFifoProcessor processor, ReadOnlySpan code, int argument)
{
_argument = argument;
_executionPending = true;
if (_executionEngine == null)
{
if (GraphicsConfig.EnableMacroHLE && MacroHLETable.TryGetMacroHLEFunction(code[Position..], context.Capabilities, out _hleFunction))
{
_executionEngine = new MacroHLE(processor, _hleFunction);
}
else if (GraphicsConfig.EnableMacroJit)
{
_executionEngine = new MacroJit();
}
else
{
_executionEngine = new MacroInterpreter();
}
}
// We don't consume the parameter buffer value, so we don't need to flush it.
// Doing so improves performance if the value was written by a GPU shader.
if (_hleFunction == MacroHLEFunctionName.DrawElementsIndirect)
{
context.GPFifo.SetFlushSkips(1);
}
else if (_hleFunction == MacroHLEFunctionName.MultiDrawElementsIndirectCount)
{
context.GPFifo.SetFlushSkips(2);
}
}
///
/// Starts executing the macro program code.
///
/// Program code
/// Current GPU state
public void Execute(ReadOnlySpan code, IDeviceState state)
{
if (_executionPending)
{
_executionPending = false;
_executionEngine?.Execute(code[Position..], state, _argument);
}
}
///
/// Pushes an argument to the macro call argument FIFO.
///
/// GPU virtual address where the command word is located
/// Argument to be pushed
public readonly void PushArgument(ulong gpuVa, int argument)
{
_executionEngine?.Fifo.Enqueue(new FifoWord(gpuVa, argument));
}
}
}