NvGpuEngine2d.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using Ryujinx.Graphics.Gal;
  2. using Ryujinx.HLE.Gpu.Memory;
  3. using Ryujinx.HLE.Gpu.Texture;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.HLE.Gpu.Engines
  6. {
  7. 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. private Dictionary<int, NvGpuMethod> Methods;
  22. public NvGpuEngine2d(NvGpu Gpu)
  23. {
  24. this.Gpu = Gpu;
  25. Registers = new int[0xe00];
  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(0xb5, 1, 1, TextureCopy);
  36. }
  37. public void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  38. {
  39. if (Methods.TryGetValue(PBEntry.Method, out NvGpuMethod Method))
  40. {
  41. Method(Vmm, PBEntry);
  42. }
  43. else
  44. {
  45. WriteRegister(PBEntry);
  46. }
  47. }
  48. private void TextureCopy(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  49. {
  50. CopyOperation Operation = (CopyOperation)ReadRegister(NvGpuEngine2dReg.CopyOperation);
  51. bool SrcLinear = ReadRegister(NvGpuEngine2dReg.SrcLinear) != 0;
  52. int SrcWidth = ReadRegister(NvGpuEngine2dReg.SrcWidth);
  53. int SrcHeight = ReadRegister(NvGpuEngine2dReg.SrcHeight);
  54. bool DstLinear = ReadRegister(NvGpuEngine2dReg.DstLinear) != 0;
  55. int DstWidth = ReadRegister(NvGpuEngine2dReg.DstWidth);
  56. int DstHeight = ReadRegister(NvGpuEngine2dReg.DstHeight);
  57. int DstPitch = ReadRegister(NvGpuEngine2dReg.DstPitch);
  58. int DstBlkDim = ReadRegister(NvGpuEngine2dReg.DstBlockDimensions);
  59. TextureSwizzle DstSwizzle = DstLinear
  60. ? TextureSwizzle.Pitch
  61. : TextureSwizzle.BlockLinear;
  62. int DstBlockHeight = 1 << ((DstBlkDim >> 4) & 0xf);
  63. long Key = Vmm.GetPhysicalAddress(MakeInt64From2xInt32(NvGpuEngine2dReg.SrcAddress));
  64. long SrcAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.SrcAddress);
  65. long DstAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.DstAddress);
  66. bool IsFbTexture = Gpu.Engine3d.IsFrameBufferPosition(Key);
  67. if (IsFbTexture && DstLinear)
  68. {
  69. DstSwizzle = TextureSwizzle.BlockLinear;
  70. }
  71. TextureInfo DstTexture = new TextureInfo(
  72. DstAddress,
  73. DstWidth,
  74. DstHeight,
  75. DstBlockHeight,
  76. DstBlockHeight,
  77. DstSwizzle,
  78. GalTextureFormat.A8B8G8R8);
  79. if (IsFbTexture)
  80. {
  81. //TODO: Change this when the correct frame buffer resolution is used.
  82. //Currently, the frame buffer size is hardcoded to 1280x720.
  83. SrcWidth = 1280;
  84. SrcHeight = 720;
  85. Gpu.Renderer.FrameBuffer.GetBufferData(Key, (byte[] Buffer) =>
  86. {
  87. CopyTexture(
  88. Vmm,
  89. DstTexture,
  90. Buffer,
  91. SrcWidth,
  92. SrcHeight);
  93. });
  94. }
  95. else
  96. {
  97. long Size = SrcWidth * SrcHeight * 4;
  98. byte[] Buffer = Vmm.ReadBytes(SrcAddress, Size);
  99. CopyTexture(
  100. Vmm,
  101. DstTexture,
  102. Buffer,
  103. SrcWidth,
  104. SrcHeight);
  105. }
  106. }
  107. private void CopyTexture(
  108. NvGpuVmm Vmm,
  109. TextureInfo Texture,
  110. byte[] Buffer,
  111. int Width,
  112. int Height)
  113. {
  114. TextureWriter.Write(Vmm, Texture, Buffer, Width, Height);
  115. }
  116. private long MakeInt64From2xInt32(NvGpuEngine2dReg Reg)
  117. {
  118. return
  119. (long)Registers[(int)Reg + 0] << 32 |
  120. (uint)Registers[(int)Reg + 1];
  121. }
  122. private void WriteRegister(NvGpuPBEntry PBEntry)
  123. {
  124. int ArgsCount = PBEntry.Arguments.Count;
  125. if (ArgsCount > 0)
  126. {
  127. Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1];
  128. }
  129. }
  130. private int ReadRegister(NvGpuEngine2dReg Reg)
  131. {
  132. return Registers[(int)Reg];
  133. }
  134. private void WriteRegister(NvGpuEngine2dReg Reg, int Value)
  135. {
  136. Registers[(int)Reg] = Value;
  137. }
  138. }
  139. }