NvGpuEngineP2mf.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using Ryujinx.Graphics.Memory;
  2. using Ryujinx.Graphics.Texture;
  3. using System.Collections.Generic;
  4. namespace Ryujinx.Graphics
  5. {
  6. class NvGpuEngineP2mf : INvGpuEngine
  7. {
  8. public int[] Registers { get; private set; }
  9. private NvGpu Gpu;
  10. private Dictionary<int, NvGpuMethod> Methods;
  11. private int CopyStartX;
  12. private int CopyStartY;
  13. private int CopyWidth;
  14. private int CopyHeight;
  15. private int CopyGobBlockHeight;
  16. private long CopyAddress;
  17. private int CopyOffset;
  18. private int CopySize;
  19. private bool CopyLinear;
  20. private byte[] Buffer;
  21. public NvGpuEngineP2mf(NvGpu Gpu)
  22. {
  23. this.Gpu = Gpu;
  24. Registers = new int[0x80];
  25. Methods = new Dictionary<int, NvGpuMethod>();
  26. void AddMethod(int Meth, int Count, int Stride, NvGpuMethod Method)
  27. {
  28. while (Count-- > 0)
  29. {
  30. Methods.Add(Meth, Method);
  31. Meth += Stride;
  32. }
  33. }
  34. AddMethod(0x6c, 1, 1, Execute);
  35. AddMethod(0x6d, 1, 1, PushData);
  36. }
  37. public void CallMethod(NvGpuVmm Vmm, GpuMethodCall MethCall)
  38. {
  39. if (Methods.TryGetValue(MethCall.Method, out NvGpuMethod Method))
  40. {
  41. Method(Vmm, MethCall);
  42. }
  43. else
  44. {
  45. WriteRegister(MethCall);
  46. }
  47. }
  48. private void Execute(NvGpuVmm Vmm, GpuMethodCall MethCall)
  49. {
  50. //TODO: Some registers and copy modes are still not implemented.
  51. int Control = MethCall.Argument;
  52. long DstAddress = MakeInt64From2xInt32(NvGpuEngineP2mfReg.DstAddress);
  53. int DstPitch = ReadRegister(NvGpuEngineP2mfReg.DstPitch);
  54. int DstBlkDim = ReadRegister(NvGpuEngineP2mfReg.DstBlockDim);
  55. int DstX = ReadRegister(NvGpuEngineP2mfReg.DstX);
  56. int DstY = ReadRegister(NvGpuEngineP2mfReg.DstY);
  57. int DstWidth = ReadRegister(NvGpuEngineP2mfReg.DstWidth);
  58. int DstHeight = ReadRegister(NvGpuEngineP2mfReg.DstHeight);
  59. int LineLengthIn = ReadRegister(NvGpuEngineP2mfReg.LineLengthIn);
  60. int LineCount = ReadRegister(NvGpuEngineP2mfReg.LineCount);
  61. CopyLinear = (Control & 1) != 0;
  62. CopyGobBlockHeight = 1 << ((DstBlkDim >> 4) & 0xf);
  63. CopyStartX = DstX;
  64. CopyStartY = DstY;
  65. CopyWidth = DstWidth;
  66. CopyHeight = DstHeight;
  67. CopyAddress = DstAddress;
  68. CopyOffset = 0;
  69. CopySize = LineLengthIn * LineCount;
  70. Buffer = new byte[CopySize];
  71. }
  72. private void PushData(NvGpuVmm Vmm, GpuMethodCall MethCall)
  73. {
  74. if (Buffer == null)
  75. {
  76. return;
  77. }
  78. for (int Shift = 0; Shift < 32 && CopyOffset < CopySize; Shift += 8, CopyOffset++)
  79. {
  80. Buffer[CopyOffset] = (byte)(MethCall.Argument >> Shift);
  81. }
  82. if (MethCall.IsLastCall)
  83. {
  84. if (CopyLinear)
  85. {
  86. Vmm.WriteBytes(CopyAddress, Buffer);
  87. }
  88. else
  89. {
  90. BlockLinearSwizzle Swizzle = new BlockLinearSwizzle(CopyWidth, 1, CopyGobBlockHeight);
  91. int SrcOffset = 0;
  92. for (int Y = CopyStartY; Y < CopyHeight && SrcOffset < CopySize; Y++)
  93. for (int X = CopyStartX; X < CopyWidth && SrcOffset < CopySize; X++)
  94. {
  95. int DstOffset = Swizzle.GetSwizzleOffset(X, Y);
  96. Vmm.WriteByte(CopyAddress + DstOffset, Buffer[SrcOffset++]);
  97. }
  98. }
  99. Buffer = null;
  100. }
  101. }
  102. private long MakeInt64From2xInt32(NvGpuEngineP2mfReg Reg)
  103. {
  104. return
  105. (long)Registers[(int)Reg + 0] << 32 |
  106. (uint)Registers[(int)Reg + 1];
  107. }
  108. private void WriteRegister(GpuMethodCall MethCall)
  109. {
  110. Registers[MethCall.Method] = MethCall.Argument;
  111. }
  112. private int ReadRegister(NvGpuEngineP2mfReg Reg)
  113. {
  114. return Registers[(int)Reg];
  115. }
  116. private void WriteRegister(NvGpuEngineP2mfReg Reg, int Value)
  117. {
  118. Registers[(int)Reg] = Value;
  119. }
  120. }
  121. }