NvGpuEngine2d.cs 4.8 KB

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