NvGpuFifo.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using Ryujinx.Graphics.Memory;
  2. namespace Ryujinx.Graphics.Graphics3d
  3. {
  4. class NvGpuFifo
  5. {
  6. private const int MacrosCount = 0x80;
  7. private const int MacroIndexMask = MacrosCount - 1;
  8. //Note: The size of the macro memory is unknown, we just make
  9. //a guess here and use 256kb as the size. Increase if needed.
  10. private const int MmeWords = 256 * 256;
  11. private NvGpu _gpu;
  12. private NvGpuEngine[] _subChannels;
  13. private struct CachedMacro
  14. {
  15. public int Position { get; private set; }
  16. private bool _executionPending;
  17. private int _argument;
  18. private MacroInterpreter _interpreter;
  19. public CachedMacro(NvGpuFifo pFifo, INvGpuEngine engine, int position)
  20. {
  21. Position = position;
  22. _executionPending = false;
  23. _argument = 0;
  24. _interpreter = new MacroInterpreter(pFifo, engine);
  25. }
  26. public void StartExecution(int argument)
  27. {
  28. _argument = argument;
  29. _executionPending = true;
  30. }
  31. public void Execute(NvGpuVmm vmm, int[] mme)
  32. {
  33. if (_executionPending)
  34. {
  35. _executionPending = false;
  36. _interpreter?.Execute(vmm, mme, Position, _argument);
  37. }
  38. }
  39. public void PushArgument(int argument)
  40. {
  41. _interpreter?.Fifo.Enqueue(argument);
  42. }
  43. }
  44. private int _currMacroPosition;
  45. private int _currMacroBindIndex;
  46. private CachedMacro[] _macros;
  47. private int[] _mme;
  48. public NvGpuFifo(NvGpu gpu)
  49. {
  50. _gpu = gpu;
  51. _subChannels = new NvGpuEngine[8];
  52. _macros = new CachedMacro[MacrosCount];
  53. _mme = new int[MmeWords];
  54. }
  55. public void CallMethod(NvGpuVmm vmm, GpuMethodCall methCall)
  56. {
  57. if ((NvGpuFifoMeth)methCall.Method == NvGpuFifoMeth.BindChannel)
  58. {
  59. NvGpuEngine engine = (NvGpuEngine)methCall.Argument;
  60. _subChannels[methCall.SubChannel] = engine;
  61. }
  62. else
  63. {
  64. switch (_subChannels[methCall.SubChannel])
  65. {
  66. case NvGpuEngine._2d: Call2dMethod (vmm, methCall); break;
  67. case NvGpuEngine._3d: Call3dMethod (vmm, methCall); break;
  68. case NvGpuEngine.P2mf: CallP2mfMethod(vmm, methCall); break;
  69. case NvGpuEngine.M2mf: CallM2mfMethod(vmm, methCall); break;
  70. }
  71. }
  72. }
  73. private void Call2dMethod(NvGpuVmm vmm, GpuMethodCall methCall)
  74. {
  75. _gpu.Engine2d.CallMethod(vmm, methCall);
  76. }
  77. private void Call3dMethod(NvGpuVmm vmm, GpuMethodCall methCall)
  78. {
  79. if (methCall.Method < 0x80)
  80. {
  81. switch ((NvGpuFifoMeth)methCall.Method)
  82. {
  83. case NvGpuFifoMeth.SetMacroUploadAddress:
  84. {
  85. _currMacroPosition = methCall.Argument;
  86. break;
  87. }
  88. case NvGpuFifoMeth.SendMacroCodeData:
  89. {
  90. _mme[_currMacroPosition++] = methCall.Argument;
  91. break;
  92. }
  93. case NvGpuFifoMeth.SetMacroBindingIndex:
  94. {
  95. _currMacroBindIndex = methCall.Argument;
  96. break;
  97. }
  98. case NvGpuFifoMeth.BindMacro:
  99. {
  100. int position = methCall.Argument;
  101. _macros[_currMacroBindIndex] = new CachedMacro(this, _gpu.Engine3d, position);
  102. break;
  103. }
  104. default: CallP2mfMethod(vmm, methCall); break;
  105. }
  106. }
  107. else if (methCall.Method < 0xe00)
  108. {
  109. _gpu.Engine3d.CallMethod(vmm, methCall);
  110. }
  111. else
  112. {
  113. int macroIndex = (methCall.Method >> 1) & MacroIndexMask;
  114. if ((methCall.Method & 1) != 0)
  115. {
  116. _macros[macroIndex].PushArgument(methCall.Argument);
  117. }
  118. else
  119. {
  120. _macros[macroIndex].StartExecution(methCall.Argument);
  121. }
  122. if (methCall.IsLastCall)
  123. {
  124. _macros[macroIndex].Execute(vmm, _mme);
  125. }
  126. }
  127. }
  128. private void CallP2mfMethod(NvGpuVmm vmm, GpuMethodCall methCall)
  129. {
  130. _gpu.EngineP2mf.CallMethod(vmm, methCall);
  131. }
  132. private void CallM2mfMethod(NvGpuVmm vmm, GpuMethodCall methCall)
  133. {
  134. _gpu.EngineM2mf.CallMethod(vmm, methCall);
  135. }
  136. }
  137. }