NvGpuEngineP2mf.cs 4.6 KB

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