NvGpuEngine2d.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using Ryujinx.Graphics.Gal;
  2. using Ryujinx.Graphics.Memory;
  3. using Ryujinx.Graphics.Texture;
  4. using System;
  5. namespace Ryujinx.Graphics
  6. {
  7. public class NvGpuEngine2d : INvGpuEngine
  8. {
  9. private enum CopyOperation
  10. {
  11. SrcCopyAnd,
  12. RopAnd,
  13. Blend,
  14. SrcCopy,
  15. Rop,
  16. SrcCopyPremult,
  17. BlendPremult
  18. }
  19. public int[] Registers { get; private set; }
  20. private NvGpu Gpu;
  21. public NvGpuEngine2d(NvGpu Gpu)
  22. {
  23. this.Gpu = Gpu;
  24. Registers = new int[0x238];
  25. }
  26. public void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  27. {
  28. WriteRegister(PBEntry);
  29. if ((NvGpuEngine2dReg)PBEntry.Method == NvGpuEngine2dReg.BlitSrcYInt)
  30. {
  31. TextureCopy(Vmm);
  32. }
  33. }
  34. private void TextureCopy(NvGpuVmm Vmm)
  35. {
  36. CopyOperation Operation = (CopyOperation)ReadRegister(NvGpuEngine2dReg.CopyOperation);
  37. int SrcFormat = ReadRegister(NvGpuEngine2dReg.SrcFormat);
  38. bool SrcLinear = ReadRegister(NvGpuEngine2dReg.SrcLinear) != 0;
  39. int SrcWidth = ReadRegister(NvGpuEngine2dReg.SrcWidth);
  40. int SrcHeight = ReadRegister(NvGpuEngine2dReg.SrcHeight);
  41. int SrcPitch = ReadRegister(NvGpuEngine2dReg.SrcPitch);
  42. int SrcBlkDim = ReadRegister(NvGpuEngine2dReg.SrcBlockDimensions);
  43. int DstFormat = ReadRegister(NvGpuEngine2dReg.DstFormat);
  44. bool DstLinear = ReadRegister(NvGpuEngine2dReg.DstLinear) != 0;
  45. int DstWidth = ReadRegister(NvGpuEngine2dReg.DstWidth);
  46. int DstHeight = ReadRegister(NvGpuEngine2dReg.DstHeight);
  47. int DstPitch = ReadRegister(NvGpuEngine2dReg.DstPitch);
  48. int DstBlkDim = ReadRegister(NvGpuEngine2dReg.DstBlockDimensions);
  49. GalImageFormat SrcImgFormat = ImageUtils.ConvertSurface((GalSurfaceFormat)SrcFormat);
  50. GalImageFormat DstImgFormat = ImageUtils.ConvertSurface((GalSurfaceFormat)DstFormat);
  51. GalMemoryLayout SrcLayout = GetLayout(SrcLinear);
  52. GalMemoryLayout DstLayout = GetLayout(DstLinear);
  53. int SrcBlockHeight = 1 << ((SrcBlkDim >> 4) & 0xf);
  54. int DstBlockHeight = 1 << ((DstBlkDim >> 4) & 0xf);
  55. long SrcAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.SrcAddress);
  56. long DstAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.DstAddress);
  57. long SrcKey = Vmm.GetPhysicalAddress(SrcAddress);
  58. long DstKey = Vmm.GetPhysicalAddress(DstAddress);
  59. GalImage SrcTexture = new GalImage(
  60. SrcWidth,
  61. SrcHeight, 1,
  62. SrcBlockHeight,
  63. SrcLayout,
  64. SrcImgFormat);
  65. GalImage DstTexture = new GalImage(
  66. DstWidth,
  67. DstHeight, 1,
  68. DstBlockHeight,
  69. DstLayout,
  70. DstImgFormat);
  71. Gpu.ResourceManager.SendTexture(Vmm, SrcKey, SrcTexture);
  72. Gpu.ResourceManager.SendTexture(Vmm, DstKey, DstTexture);
  73. int Width = Math.Min(SrcWidth, DstWidth);
  74. int Height = Math.Min(SrcHeight, DstHeight);
  75. Gpu.Renderer.RenderTarget.Copy(
  76. SrcKey,
  77. DstKey,
  78. 0,
  79. 0,
  80. Width,
  81. Height,
  82. 0,
  83. 0,
  84. Width,
  85. Height);
  86. }
  87. private static GalMemoryLayout GetLayout(bool Linear)
  88. {
  89. return Linear
  90. ? GalMemoryLayout.Pitch
  91. : GalMemoryLayout.BlockLinear;
  92. }
  93. private long MakeInt64From2xInt32(NvGpuEngine2dReg Reg)
  94. {
  95. return
  96. (long)Registers[(int)Reg + 0] << 32 |
  97. (uint)Registers[(int)Reg + 1];
  98. }
  99. private void WriteRegister(NvGpuPBEntry PBEntry)
  100. {
  101. int ArgsCount = PBEntry.Arguments.Count;
  102. if (ArgsCount > 0)
  103. {
  104. Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1];
  105. }
  106. }
  107. private int ReadRegister(NvGpuEngine2dReg Reg)
  108. {
  109. return Registers[(int)Reg];
  110. }
  111. }
  112. }