NvGpuEngineM2mf.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using Ryujinx.Graphics.Memory;
  2. using Ryujinx.Graphics.Texture;
  3. using System.Collections.Generic;
  4. using Ryujinx.Profiler;
  5. namespace Ryujinx.Graphics.Graphics3d
  6. {
  7. class NvGpuEngineM2mf : INvGpuEngine
  8. {
  9. public int[] Registers { get; private set; }
  10. private NvGpu _gpu;
  11. private Dictionary<int, NvGpuMethod> _methods;
  12. public NvGpuEngineM2mf(NvGpu gpu)
  13. {
  14. _gpu = gpu;
  15. Registers = new int[0x1d6];
  16. _methods = new Dictionary<int, NvGpuMethod>();
  17. void AddMethod(int meth, int count, int stride, NvGpuMethod method)
  18. {
  19. while (count-- > 0)
  20. {
  21. _methods.Add(meth, method);
  22. meth += stride;
  23. }
  24. }
  25. AddMethod(0xc0, 1, 1, Execute);
  26. }
  27. public void CallMethod(NvGpuVmm vmm, GpuMethodCall methCall)
  28. {
  29. if (_methods.TryGetValue(methCall.Method, out NvGpuMethod method))
  30. {
  31. ProfileConfig profile = Profiles.GPU.EngineM2mf.CallMethod;
  32. profile.SessionItem = method.Method.Name;
  33. Profile.Begin(profile);
  34. method(vmm, methCall);
  35. Profile.End(profile);
  36. }
  37. else
  38. {
  39. WriteRegister(methCall);
  40. }
  41. }
  42. private void Execute(NvGpuVmm vmm, GpuMethodCall methCall)
  43. {
  44. Profile.Begin(Profiles.GPU.EngineM2mf.Execute);
  45. // TODO: Some registers and copy modes are still not implemented.
  46. int control = methCall.Argument;
  47. bool srcLinear = ((control >> 7) & 1) != 0;
  48. bool dstLinear = ((control >> 8) & 1) != 0;
  49. bool copy2D = ((control >> 9) & 1) != 0;
  50. long srcAddress = MakeInt64From2xInt32(NvGpuEngineM2mfReg.SrcAddress);
  51. long dstAddress = MakeInt64From2xInt32(NvGpuEngineM2mfReg.DstAddress);
  52. int srcPitch = ReadRegister(NvGpuEngineM2mfReg.SrcPitch);
  53. int dstPitch = ReadRegister(NvGpuEngineM2mfReg.DstPitch);
  54. int xCount = ReadRegister(NvGpuEngineM2mfReg.XCount);
  55. int yCount = ReadRegister(NvGpuEngineM2mfReg.YCount);
  56. int swizzle = ReadRegister(NvGpuEngineM2mfReg.Swizzle);
  57. int dstBlkDim = ReadRegister(NvGpuEngineM2mfReg.DstBlkDim);
  58. int dstSizeX = ReadRegister(NvGpuEngineM2mfReg.DstSizeX);
  59. int dstSizeY = ReadRegister(NvGpuEngineM2mfReg.DstSizeY);
  60. int dstSizeZ = ReadRegister(NvGpuEngineM2mfReg.DstSizeZ);
  61. int dstPosXY = ReadRegister(NvGpuEngineM2mfReg.DstPosXY);
  62. int dstPosZ = ReadRegister(NvGpuEngineM2mfReg.DstPosZ);
  63. int srcBlkDim = ReadRegister(NvGpuEngineM2mfReg.SrcBlkDim);
  64. int srcSizeX = ReadRegister(NvGpuEngineM2mfReg.SrcSizeX);
  65. int srcSizeY = ReadRegister(NvGpuEngineM2mfReg.SrcSizeY);
  66. int srcSizeZ = ReadRegister(NvGpuEngineM2mfReg.SrcSizeZ);
  67. int srcPosXY = ReadRegister(NvGpuEngineM2mfReg.SrcPosXY);
  68. int srcPosZ = ReadRegister(NvGpuEngineM2mfReg.SrcPosZ);
  69. int srcCpp = ((swizzle >> 20) & 7) + 1;
  70. int dstCpp = ((swizzle >> 24) & 7) + 1;
  71. int dstPosX = (dstPosXY >> 0) & 0xffff;
  72. int dstPosY = (dstPosXY >> 16) & 0xffff;
  73. int srcPosX = (srcPosXY >> 0) & 0xffff;
  74. int srcPosY = (srcPosXY >> 16) & 0xffff;
  75. int srcBlockHeight = 1 << ((srcBlkDim >> 4) & 0xf);
  76. int dstBlockHeight = 1 << ((dstBlkDim >> 4) & 0xf);
  77. long srcPa = vmm.GetPhysicalAddress(srcAddress);
  78. long dstPa = vmm.GetPhysicalAddress(dstAddress);
  79. if (copy2D)
  80. {
  81. if (srcLinear)
  82. {
  83. srcPosX = srcPosY = srcPosZ = 0;
  84. }
  85. if (dstLinear)
  86. {
  87. dstPosX = dstPosY = dstPosZ = 0;
  88. }
  89. if (srcLinear && dstLinear)
  90. {
  91. for (int y = 0; y < yCount; y++)
  92. {
  93. int srcOffset = (srcPosY + y) * srcPitch + srcPosX * srcCpp;
  94. int dstOffset = (dstPosY + y) * dstPitch + dstPosX * dstCpp;
  95. long src = srcPa + (uint)srcOffset;
  96. long dst = dstPa + (uint)dstOffset;
  97. vmm.Memory.CopyBytes(src, dst, xCount * srcCpp);
  98. }
  99. }
  100. else
  101. {
  102. ISwizzle srcSwizzle;
  103. if (srcLinear)
  104. {
  105. srcSwizzle = new LinearSwizzle(srcPitch, srcCpp, srcSizeX, srcSizeY);
  106. }
  107. else
  108. {
  109. srcSwizzle = new BlockLinearSwizzle(
  110. srcSizeX,
  111. srcSizeY, 1,
  112. srcBlockHeight, 1,
  113. srcCpp);
  114. }
  115. ISwizzle dstSwizzle;
  116. if (dstLinear)
  117. {
  118. dstSwizzle = new LinearSwizzle(dstPitch, dstCpp, srcSizeX, srcSizeY);
  119. }
  120. else
  121. {
  122. dstSwizzle = new BlockLinearSwizzle(
  123. dstSizeX,
  124. dstSizeY, 1,
  125. dstBlockHeight, 1,
  126. dstCpp);
  127. }
  128. // Calculate the bits per pixel
  129. int bpp = srcPitch / xCount;
  130. // Copying all the bits at the same time corrupts the texture, unknown why but probably because the texture isn't linear
  131. // To avoid this we will simply loop more times to cover all the bits,
  132. // this allows up to recalculate the memory locations for each iteration around the loop
  133. xCount *= bpp / srcCpp;
  134. for (int y = 0; y < yCount; y++)
  135. for (int x = 0; x < xCount; x++)
  136. {
  137. int srcOffset = srcSwizzle.GetSwizzleOffset(srcPosX + x, srcPosY + y, 0);
  138. int dstOffset = dstSwizzle.GetSwizzleOffset(dstPosX + x, dstPosY + y, 0);
  139. long src = srcPa + (uint)srcOffset;
  140. long dst = dstPa + (uint)dstOffset;
  141. vmm.Memory.CopyBytes(src, dst, srcCpp);
  142. }
  143. }
  144. }
  145. else
  146. {
  147. vmm.Memory.CopyBytes(srcPa, dstPa, xCount);
  148. }
  149. Profile.End(Profiles.GPU.EngineM2mf.Execute);
  150. }
  151. private long MakeInt64From2xInt32(NvGpuEngineM2mfReg reg)
  152. {
  153. return
  154. (long)Registers[(int)reg + 0] << 32 |
  155. (uint)Registers[(int)reg + 1];
  156. }
  157. private void WriteRegister(GpuMethodCall methCall)
  158. {
  159. Registers[methCall.Method] = methCall.Argument;
  160. }
  161. private int ReadRegister(NvGpuEngineM2mfReg reg)
  162. {
  163. return Registers[(int)reg];
  164. }
  165. private void WriteRegister(NvGpuEngineM2mfReg reg, int value)
  166. {
  167. Registers[(int)reg] = value;
  168. }
  169. }
  170. }