NvGpuEngine2d.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using Ryujinx.Graphics.Gal;
  2. using Ryujinx.Graphics.Memory;
  3. using Ryujinx.Graphics.Texture;
  4. using System;
  5. using System.Collections.Generic;
  6. namespace Ryujinx.Graphics
  7. {
  8. public class NvGpuEngine2d : INvGpuEngine
  9. {
  10. private enum CopyOperation
  11. {
  12. SrcCopyAnd,
  13. RopAnd,
  14. Blend,
  15. SrcCopy,
  16. Rop,
  17. SrcCopyPremult,
  18. BlendPremult
  19. }
  20. public int[] Registers { get; private set; }
  21. private NvGpu Gpu;
  22. private Dictionary<int, NvGpuMethod> Methods;
  23. public NvGpuEngine2d(NvGpu Gpu)
  24. {
  25. this.Gpu = Gpu;
  26. Registers = new int[0xe00];
  27. Methods = new Dictionary<int, NvGpuMethod>();
  28. void AddMethod(int Meth, int Count, int Stride, NvGpuMethod Method)
  29. {
  30. while (Count-- > 0)
  31. {
  32. Methods.Add(Meth, Method);
  33. Meth += Stride;
  34. }
  35. }
  36. AddMethod(0xb5, 1, 1, TextureCopy);
  37. }
  38. public void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  39. {
  40. if (Methods.TryGetValue(PBEntry.Method, out NvGpuMethod Method))
  41. {
  42. Method(Vmm, PBEntry);
  43. }
  44. else
  45. {
  46. WriteRegister(PBEntry);
  47. }
  48. }
  49. private void TextureCopy(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  50. {
  51. CopyOperation Operation = (CopyOperation)ReadRegister(NvGpuEngine2dReg.CopyOperation);
  52. bool SrcLinear = ReadRegister(NvGpuEngine2dReg.SrcLinear) != 0;
  53. int SrcWidth = ReadRegister(NvGpuEngine2dReg.SrcWidth);
  54. int SrcHeight = ReadRegister(NvGpuEngine2dReg.SrcHeight);
  55. int SrcPitch = ReadRegister(NvGpuEngine2dReg.SrcPitch);
  56. int SrcBlkDim = ReadRegister(NvGpuEngine2dReg.SrcBlockDimensions);
  57. bool DstLinear = ReadRegister(NvGpuEngine2dReg.DstLinear) != 0;
  58. int DstWidth = ReadRegister(NvGpuEngine2dReg.DstWidth);
  59. int DstHeight = ReadRegister(NvGpuEngine2dReg.DstHeight);
  60. int DstPitch = ReadRegister(NvGpuEngine2dReg.DstPitch);
  61. int DstBlkDim = ReadRegister(NvGpuEngine2dReg.DstBlockDimensions);
  62. TextureSwizzle SrcSwizzle = SrcLinear
  63. ? TextureSwizzle.Pitch
  64. : TextureSwizzle.BlockLinear;
  65. TextureSwizzle DstSwizzle = DstLinear
  66. ? TextureSwizzle.Pitch
  67. : TextureSwizzle.BlockLinear;
  68. int SrcBlockHeight = 1 << ((SrcBlkDim >> 4) & 0xf);
  69. int DstBlockHeight = 1 << ((DstBlkDim >> 4) & 0xf);
  70. long SrcAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.SrcAddress);
  71. long DstAddress = MakeInt64From2xInt32(NvGpuEngine2dReg.DstAddress);
  72. long SrcKey = Vmm.GetPhysicalAddress(SrcAddress);
  73. long DstKey = Vmm.GetPhysicalAddress(DstAddress);
  74. bool IsSrcFb = Gpu.Engine3d.IsFrameBufferPosition(SrcKey);
  75. bool IsDstFb = Gpu.Engine3d.IsFrameBufferPosition(DstKey);
  76. TextureInfo SrcTexture()
  77. {
  78. return new TextureInfo(
  79. SrcAddress,
  80. SrcWidth,
  81. SrcHeight,
  82. SrcPitch,
  83. SrcBlockHeight, 1,
  84. SrcSwizzle,
  85. GalImageFormat.A8B8G8R8 | GalImageFormat.Unorm);
  86. }
  87. TextureInfo DstTexture()
  88. {
  89. return new TextureInfo(
  90. DstAddress,
  91. DstWidth,
  92. DstHeight,
  93. DstPitch,
  94. DstBlockHeight, 1,
  95. DstSwizzle,
  96. GalImageFormat.A8B8G8R8 | GalImageFormat.Unorm);
  97. }
  98. //TODO: fb -> fb copies, tex -> fb copies, formats other than RGBA8,
  99. //make it throw for unimpl stuff (like the copy mode)...
  100. if (IsSrcFb && IsDstFb)
  101. {
  102. //Frame Buffer -> Frame Buffer copy.
  103. Gpu.Renderer.RenderTarget.Copy(
  104. SrcKey,
  105. DstKey,
  106. 0,
  107. 0,
  108. SrcWidth,
  109. SrcHeight,
  110. 0,
  111. 0,
  112. DstWidth,
  113. DstHeight);
  114. }
  115. if (IsSrcFb)
  116. {
  117. //Frame Buffer -> Texture copy.
  118. Gpu.Renderer.RenderTarget.GetBufferData(SrcKey, (byte[] Buffer) =>
  119. {
  120. TextureInfo Src = SrcTexture();
  121. TextureInfo Dst = DstTexture();
  122. if (Src.Width != Dst.Width ||
  123. Src.Height != Dst.Height)
  124. {
  125. throw new NotImplementedException("Texture resizing is not supported");
  126. }
  127. TextureWriter.Write(Vmm, Dst, Buffer);
  128. });
  129. }
  130. else if (IsDstFb)
  131. {
  132. byte[] Buffer = TextureReader.Read(Vmm, SrcTexture());
  133. Gpu.Renderer.RenderTarget.SetBufferData(
  134. DstKey,
  135. DstWidth,
  136. DstHeight,
  137. Buffer);
  138. }
  139. else
  140. {
  141. //Texture -> Texture copy.
  142. TextureInfo Src = SrcTexture();
  143. TextureInfo Dst = DstTexture();
  144. if (Src.Width != Dst.Width ||
  145. Src.Height != Dst.Height)
  146. {
  147. throw new NotImplementedException("Texture resizing is not supported");
  148. }
  149. TextureWriter.Write(Vmm, Dst, TextureReader.Read(Vmm, Src));
  150. }
  151. }
  152. private long MakeInt64From2xInt32(NvGpuEngine2dReg Reg)
  153. {
  154. return
  155. (long)Registers[(int)Reg + 0] << 32 |
  156. (uint)Registers[(int)Reg + 1];
  157. }
  158. private void WriteRegister(NvGpuPBEntry PBEntry)
  159. {
  160. int ArgsCount = PBEntry.Arguments.Count;
  161. if (ArgsCount > 0)
  162. {
  163. Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1];
  164. }
  165. }
  166. private int ReadRegister(NvGpuEngine2dReg Reg)
  167. {
  168. return Registers[(int)Reg];
  169. }
  170. private void WriteRegister(NvGpuEngine2dReg Reg, int Value)
  171. {
  172. Registers[(int)Reg] = Value;
  173. }
  174. }
  175. }