MacroHLE.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Graphics.Device;
  3. using Ryujinx.Graphics.GAL;
  4. using Ryujinx.Graphics.Gpu.Engine.GPFifo;
  5. using Ryujinx.Graphics.Gpu.Engine.Threed;
  6. using Ryujinx.Graphics.Gpu.Memory;
  7. using System;
  8. using System.Collections.Generic;
  9. namespace Ryujinx.Graphics.Gpu.Engine.MME
  10. {
  11. /// <summary>
  12. /// Macro High-level emulation.
  13. /// </summary>
  14. class MacroHLE : IMacroEE
  15. {
  16. private readonly GPFifoProcessor _processor;
  17. private readonly MacroHLEFunctionName _functionName;
  18. /// <summary>
  19. /// Arguments FIFO.
  20. /// </summary>
  21. public Queue<FifoWord> Fifo { get; }
  22. /// <summary>
  23. /// Creates a new instance of the HLE macro handler.
  24. /// </summary>
  25. /// <param name="context">GPU context the macro is being executed on</param>
  26. /// <param name="memoryManager">GPU memory manager</param>
  27. /// <param name="engine">3D engine where this macro is being called</param>
  28. /// <param name="functionName">Name of the HLE macro function to be called</param>
  29. public MacroHLE(GPFifoProcessor processor, MacroHLEFunctionName functionName)
  30. {
  31. _processor = processor;
  32. _functionName = functionName;
  33. Fifo = new Queue<FifoWord>();
  34. }
  35. /// <summary>
  36. /// Executes a macro program until it exits.
  37. /// </summary>
  38. /// <param name="code">Code of the program to execute</param>
  39. /// <param name="state">GPU state at the time of the call</param>
  40. /// <param name="arg0">Optional argument passed to the program, 0 if not used</param>
  41. public void Execute(ReadOnlySpan<int> code, IDeviceState state, int arg0)
  42. {
  43. switch (_functionName)
  44. {
  45. case MacroHLEFunctionName.MultiDrawElementsIndirectCount:
  46. MultiDrawElementsIndirectCount(state, arg0);
  47. break;
  48. default:
  49. throw new NotImplementedException(_functionName.ToString());
  50. }
  51. }
  52. /// <summary>
  53. /// Performs a indirect multi-draw, with parameters from a GPU buffer.
  54. /// </summary>
  55. /// <param name="state">GPU state at the time of the call</param>
  56. /// <param name="arg0">First argument of the call</param>
  57. private void MultiDrawElementsIndirectCount(IDeviceState state, int arg0)
  58. {
  59. int arg1 = FetchParam().Word;
  60. int arg2 = FetchParam().Word;
  61. int arg3 = FetchParam().Word;
  62. int startOffset = arg0;
  63. int endOffset = arg1;
  64. var topology = (PrimitiveTopology)arg2;
  65. int paddingWords = arg3;
  66. int maxDrawCount = endOffset - startOffset;
  67. int stride = paddingWords * 4 + 0x14;
  68. int indirectBufferSize = maxDrawCount * stride;
  69. ulong parameterBufferGpuVa = FetchParam().GpuVa;
  70. ulong indirectBufferGpuVa = 0;
  71. int indexCount = 0;
  72. for (int i = 0; i < maxDrawCount; i++)
  73. {
  74. var count = FetchParam();
  75. var instanceCount = FetchParam();
  76. var firstIndex = FetchParam();
  77. var baseVertex = FetchParam();
  78. var baseInstance = FetchParam();
  79. if (i == 0)
  80. {
  81. indirectBufferGpuVa = count.GpuVa;
  82. }
  83. indexCount = Math.Max(indexCount, count.Word + firstIndex.Word);
  84. if (i != maxDrawCount - 1)
  85. {
  86. for (int j = 0; j < paddingWords; j++)
  87. {
  88. FetchParam();
  89. }
  90. }
  91. }
  92. // It should be empty at this point, but clear it just to be safe.
  93. Fifo.Clear();
  94. var parameterBuffer = _processor.MemoryManager.Physical.BufferCache.GetGpuBufferRange(_processor.MemoryManager, parameterBufferGpuVa, 4);
  95. var indirectBuffer = _processor.MemoryManager.Physical.BufferCache.GetGpuBufferRange(_processor.MemoryManager, indirectBufferGpuVa, (ulong)indirectBufferSize);
  96. _processor.ThreedClass.MultiDrawIndirectCount(indexCount, topology, indirectBuffer, parameterBuffer, maxDrawCount, stride);
  97. }
  98. /// <summary>
  99. /// Fetches a arguments from the arguments FIFO.
  100. /// </summary>
  101. /// <returns>The call argument, or a 0 value with null address if the FIFO is empty</returns>
  102. private FifoWord FetchParam()
  103. {
  104. if (!Fifo.TryDequeue(out var value))
  105. {
  106. Logger.Warning?.Print(LogClass.Gpu, "Macro attempted to fetch an inexistent argument.");
  107. return new FifoWord(0UL, 0);
  108. }
  109. return value;
  110. }
  111. /// <summary>
  112. /// Performs a GPU method call.
  113. /// </summary>
  114. /// <param name="state">Current GPU state</param>
  115. /// <param name="methAddr">Address, in words, of the method</param>
  116. /// <param name="value">Call argument</param>
  117. private static void Send(IDeviceState state, int methAddr, int value)
  118. {
  119. state.Write(methAddr * 4, value);
  120. }
  121. }
  122. }