NvGpuEngine3d.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Graphics.Gal;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.Graphics.Gpu
  6. {
  7. public class NvGpuEngine3d : INvGpuEngine
  8. {
  9. public int[] Registers { get; private set; }
  10. private NsGpu Gpu;
  11. private Dictionary<int, NvGpuMethod> Methods;
  12. private struct ConstBuffer
  13. {
  14. public bool Enabled;
  15. public long Position;
  16. public int Size;
  17. }
  18. private ConstBuffer[] ConstBuffers;
  19. private HashSet<long> FrameBuffers;
  20. public NvGpuEngine3d(NsGpu 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(0x585, 1, 1, VertexEndGl);
  34. AddMethod(0x674, 1, 1, ClearBuffers);
  35. AddMethod(0x6c3, 1, 1, QueryControl);
  36. AddMethod(0x8e4, 16, 1, CbData);
  37. AddMethod(0x904, 1, 1, CbBind);
  38. ConstBuffers = new ConstBuffer[18];
  39. FrameBuffers = new HashSet<long>();
  40. }
  41. public void CallMethod(AMemory Memory, NsGpuPBEntry PBEntry)
  42. {
  43. if (Methods.TryGetValue(PBEntry.Method, out NvGpuMethod Method))
  44. {
  45. Method(Memory, PBEntry);
  46. }
  47. else
  48. {
  49. WriteRegister(PBEntry);
  50. }
  51. }
  52. private void VertexEndGl(AMemory Memory, NsGpuPBEntry PBEntry)
  53. {
  54. SetFrameBuffer(0);
  55. long[] Tags = UploadShaders(Memory);
  56. Gpu.Renderer.BindProgram();
  57. SetAlphaBlending();
  58. UploadTextures(Memory, Tags);
  59. UploadUniforms(Memory);
  60. UploadVertexArrays(Memory);
  61. }
  62. private void ClearBuffers(AMemory Memory, NsGpuPBEntry PBEntry)
  63. {
  64. int Arg0 = PBEntry.Arguments[0];
  65. int FbIndex = (Arg0 >> 6) & 0xf;
  66. int Layer = (Arg0 >> 10) & 0x3ff;
  67. GalClearBufferFlags Flags = (GalClearBufferFlags)(Arg0 & 0x3f);
  68. SetFrameBuffer(0);
  69. //TODO: Enable this once the frame buffer problems are fixed.
  70. //Gpu.Renderer.ClearBuffers(Layer, Flags);
  71. }
  72. private void SetFrameBuffer(int FbIndex)
  73. {
  74. long Address = MakeInt64From2xInt32(NvGpuEngine3dReg.FrameBufferNAddress + FbIndex * 0x10);
  75. FrameBuffers.Add(Address);
  76. int Width = ReadRegister(NvGpuEngine3dReg.FrameBufferNWidth + FbIndex * 0x10);
  77. int Height = ReadRegister(NvGpuEngine3dReg.FrameBufferNHeight + FbIndex * 0x10);
  78. //Note: Using the Width/Height results seems to give incorrect results.
  79. //Maybe the size of all frame buffers is hardcoded to screen size? This seems unlikely.
  80. Gpu.Renderer.CreateFrameBuffer(Address, 1280, 720);
  81. Gpu.Renderer.BindFrameBuffer(Address);
  82. }
  83. private long[] UploadShaders(AMemory Memory)
  84. {
  85. long[] Tags = new long[5];
  86. long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  87. for (int Index = 0; Index < 6; Index++)
  88. {
  89. int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + Index * 0x10);
  90. int Offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + Index * 0x10);
  91. //Note: Vertex Program (B) is always enabled.
  92. bool Enable = (Control & 1) != 0 || Index == 1;
  93. if (!Enable)
  94. {
  95. continue;
  96. }
  97. long Tag = BasePosition + (uint)Offset;
  98. long Position = Gpu.GetCpuAddr(Tag);
  99. //TODO: Find a better way to calculate the size.
  100. int Size = 0x20000;
  101. byte[] Code = AMemoryHelper.ReadBytes(Memory, Position, (uint)Size);
  102. GalShaderType ShaderType = GetTypeFromProgram(Index);
  103. Tags[(int)ShaderType] = Tag;
  104. Gpu.Renderer.CreateShader(Tag, ShaderType, Code);
  105. Gpu.Renderer.BindShader(Tag);
  106. }
  107. int RawSX = ReadRegister(NvGpuEngine3dReg.ViewportScaleX);
  108. int RawSY = ReadRegister(NvGpuEngine3dReg.ViewportScaleY);
  109. float SX = BitConverter.Int32BitsToSingle(RawSX);
  110. float SY = BitConverter.Int32BitsToSingle(RawSY);
  111. float SignX = MathF.Sign(SX);
  112. float SignY = MathF.Sign(SY);
  113. Gpu.Renderer.SetUniform2F(GalConsts.FlipUniformName, SignX, SignY);
  114. return Tags;
  115. }
  116. private static GalShaderType GetTypeFromProgram(int Program)
  117. {
  118. switch (Program)
  119. {
  120. case 0:
  121. case 1: return GalShaderType.Vertex;
  122. case 2: return GalShaderType.TessControl;
  123. case 3: return GalShaderType.TessEvaluation;
  124. case 4: return GalShaderType.Geometry;
  125. case 5: return GalShaderType.Fragment;
  126. }
  127. throw new ArgumentOutOfRangeException(nameof(Program));
  128. }
  129. private void SetAlphaBlending()
  130. {
  131. //TODO: Support independent blend properly.
  132. bool Enable = (ReadRegister(NvGpuEngine3dReg.IBlendNEnable) & 1) != 0;
  133. Gpu.Renderer.SetBlendEnable(Enable);
  134. bool BlendSeparateAlpha = (ReadRegister(NvGpuEngine3dReg.IBlendNSeparateAlpha) & 1) != 0;
  135. GalBlendEquation EquationRgb = (GalBlendEquation)ReadRegister(NvGpuEngine3dReg.IBlendNEquationRgb);
  136. GalBlendFactor FuncSrcRgb = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncSrcRgb);
  137. GalBlendFactor FuncDstRgb = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncDstRgb);
  138. if (BlendSeparateAlpha)
  139. {
  140. GalBlendEquation EquationAlpha = (GalBlendEquation)ReadRegister(NvGpuEngine3dReg.IBlendNEquationAlpha);
  141. GalBlendFactor FuncSrcAlpha = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncSrcAlpha);
  142. GalBlendFactor FuncDstAlpha = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncDstAlpha);
  143. Gpu.Renderer.SetBlendSeparate(
  144. EquationRgb,
  145. EquationAlpha,
  146. FuncSrcRgb,
  147. FuncDstRgb,
  148. FuncSrcAlpha,
  149. FuncDstAlpha);
  150. }
  151. else
  152. {
  153. Gpu.Renderer.SetBlend(EquationRgb, FuncSrcRgb, FuncDstRgb);
  154. }
  155. }
  156. private void UploadTextures(AMemory Memory, long[] Tags)
  157. {
  158. long BaseShPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  159. int TextureCbIndex = ReadRegister(NvGpuEngine3dReg.TextureCbIndex);
  160. long BasePosition = ConstBuffers[TextureCbIndex].Position;
  161. long Size = (uint)ConstBuffers[TextureCbIndex].Size;
  162. //Note: On the emulator renderer, Texture Unit 0 is
  163. //reserved for drawing the frame buffer.
  164. int TexIndex = 1;
  165. for (int Index = 0; Index < Tags.Length; Index++)
  166. {
  167. foreach (ShaderDeclInfo DeclInfo in Gpu.Renderer.GetTextureUsage(Tags[Index]))
  168. {
  169. long Position = BasePosition + Index * Size;
  170. UploadTexture(Memory, Position, TexIndex, DeclInfo.Index);
  171. Gpu.Renderer.SetUniform1(DeclInfo.Name, TexIndex);
  172. TexIndex++;
  173. }
  174. }
  175. }
  176. private void UploadTexture(AMemory Memory, long BasePosition, int TexIndex, int HndIndex)
  177. {
  178. long Position = BasePosition + HndIndex * 4;
  179. int TextureHandle = Memory.ReadInt32(Position);
  180. int TicIndex = (TextureHandle >> 0) & 0xfffff;
  181. int TscIndex = (TextureHandle >> 20) & 0xfff;
  182. TryGetCpuAddr(NvGpuEngine3dReg.TexHeaderPoolOffset, out long TicPosition);
  183. TryGetCpuAddr(NvGpuEngine3dReg.TexSamplerPoolOffset, out long TscPosition);
  184. TicPosition += TicIndex * 0x20;
  185. TscPosition += TscIndex * 0x20;
  186. GalTextureSampler Sampler = TextureFactory.MakeSampler(Gpu, Memory, TscPosition);
  187. long TextureAddress = Memory.ReadInt64(TicPosition + 4) & 0xffffffffffff;
  188. if (FrameBuffers.Contains(TextureAddress))
  189. {
  190. //This texture is a frame buffer texture,
  191. //we shouldn't read anything from memory and bind
  192. //the frame buffer texture instead, since we're not
  193. //really writing anything to memory.
  194. Gpu.Renderer.BindFrameBufferTexture(TextureAddress, TexIndex, Sampler);
  195. }
  196. else
  197. {
  198. GalTexture Texture = TextureFactory.MakeTexture(Gpu, Memory, TicPosition);
  199. Gpu.Renderer.SetTextureAndSampler(TexIndex, Texture, Sampler);
  200. Gpu.Renderer.BindTexture(TexIndex);
  201. }
  202. }
  203. private void UploadUniforms(AMemory Memory)
  204. {
  205. long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  206. for (int Index = 0; Index < 5; Index++)
  207. {
  208. int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + (Index + 1) * 0x10);
  209. int Offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + (Index + 1) * 0x10);
  210. //Note: Vertex Program (B) is always enabled.
  211. bool Enable = (Control & 1) != 0 || Index == 0;
  212. if (!Enable)
  213. {
  214. continue;
  215. }
  216. for (int Cbuf = 0; Cbuf < ConstBuffers.Length; Cbuf++)
  217. {
  218. ConstBuffer Cb = ConstBuffers[Cbuf];
  219. if (Cb.Enabled)
  220. {
  221. long CbPosition = Cb.Position + Index * Cb.Size;
  222. byte[] Data = AMemoryHelper.ReadBytes(Memory, CbPosition, (uint)Cb.Size);
  223. Gpu.Renderer.SetConstBuffer(BasePosition + (uint)Offset, Cbuf, Data);
  224. }
  225. }
  226. }
  227. }
  228. private void UploadVertexArrays(AMemory Memory)
  229. {
  230. long IndexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress);
  231. int IndexSize = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat);
  232. int IndexFirst = ReadRegister(NvGpuEngine3dReg.IndexBatchFirst);
  233. int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount);
  234. GalIndexFormat IndexFormat = (GalIndexFormat)IndexSize;
  235. IndexSize = 1 << IndexSize;
  236. if (IndexSize > 4)
  237. {
  238. throw new InvalidOperationException();
  239. }
  240. if (IndexSize != 0)
  241. {
  242. IndexPosition = Gpu.GetCpuAddr(IndexPosition);
  243. int BufferSize = IndexCount * IndexSize;
  244. byte[] Data = AMemoryHelper.ReadBytes(Memory, IndexPosition, BufferSize);
  245. Gpu.Renderer.SetIndexArray(Data, IndexFormat);
  246. }
  247. List<GalVertexAttrib>[] Attribs = new List<GalVertexAttrib>[32];
  248. for (int Attr = 0; Attr < 16; Attr++)
  249. {
  250. int Packed = ReadRegister(NvGpuEngine3dReg.VertexAttribNFormat + Attr);
  251. int ArrayIndex = Packed & 0x1f;
  252. if (Attribs[ArrayIndex] == null)
  253. {
  254. Attribs[ArrayIndex] = new List<GalVertexAttrib>();
  255. }
  256. Attribs[ArrayIndex].Add(new GalVertexAttrib(
  257. ((Packed >> 6) & 0x1) != 0,
  258. (Packed >> 7) & 0x3fff,
  259. (GalVertexAttribSize)((Packed >> 21) & 0x3f),
  260. (GalVertexAttribType)((Packed >> 27) & 0x7),
  261. ((Packed >> 31) & 0x1) != 0));
  262. }
  263. for (int Index = 0; Index < 32; Index++)
  264. {
  265. int VertexFirst = ReadRegister(NvGpuEngine3dReg.VertexArrayFirst);
  266. int VertexCount = ReadRegister(NvGpuEngine3dReg.VertexArrayCount);
  267. int Control = ReadRegister(NvGpuEngine3dReg.VertexArrayNControl + Index * 4);
  268. bool Enable = (Control & 0x1000) != 0;
  269. if (!Enable)
  270. {
  271. continue;
  272. }
  273. long VertexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + Index * 4);
  274. long VertexEndPos = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNEndAddr + Index * 4);
  275. long Size = (VertexEndPos - VertexPosition) + 1;
  276. int Stride = Control & 0xfff;
  277. VertexPosition = Gpu.GetCpuAddr(VertexPosition);
  278. byte[] Data = AMemoryHelper.ReadBytes(Memory, VertexPosition, Size);
  279. GalVertexAttrib[] AttribArray = Attribs[Index]?.ToArray() ?? new GalVertexAttrib[0];
  280. Gpu.Renderer.SetVertexArray(Index, Stride, Data, AttribArray);
  281. int PrimCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl);
  282. GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff);
  283. if (IndexCount != 0)
  284. {
  285. Gpu.Renderer.DrawElements(Index, IndexFirst, PrimType);
  286. }
  287. else
  288. {
  289. Gpu.Renderer.DrawArrays(Index, VertexFirst, VertexCount, PrimType);
  290. }
  291. }
  292. }
  293. private void QueryControl(AMemory Memory, NsGpuPBEntry PBEntry)
  294. {
  295. if (TryGetCpuAddr(NvGpuEngine3dReg.QueryAddress, out long Position))
  296. {
  297. int Seq = Registers[(int)NvGpuEngine3dReg.QuerySequence];
  298. int Ctrl = Registers[(int)NvGpuEngine3dReg.QueryControl];
  299. int Mode = Ctrl & 3;
  300. if (Mode == 0)
  301. {
  302. //Write mode.
  303. Memory.WriteInt32(Position, Seq);
  304. }
  305. }
  306. WriteRegister(PBEntry);
  307. }
  308. private void CbData(AMemory Memory, NsGpuPBEntry PBEntry)
  309. {
  310. if (TryGetCpuAddr(NvGpuEngine3dReg.ConstBufferNAddress, out long Position))
  311. {
  312. int Offset = ReadRegister(NvGpuEngine3dReg.ConstBufferNOffset);
  313. foreach (int Arg in PBEntry.Arguments)
  314. {
  315. Memory.WriteInt32(Position + Offset, Arg);
  316. Offset += 4;
  317. }
  318. WriteRegister(NvGpuEngine3dReg.ConstBufferNOffset, Offset);
  319. }
  320. }
  321. private void CbBind(AMemory Memory, NsGpuPBEntry PBEntry)
  322. {
  323. int Index = PBEntry.Arguments[0];
  324. bool Enabled = (Index & 1) != 0;
  325. Index = (Index >> 4) & 0x1f;
  326. if (TryGetCpuAddr(NvGpuEngine3dReg.ConstBufferNAddress, out long Position))
  327. {
  328. ConstBuffers[Index].Position = Position;
  329. ConstBuffers[Index].Enabled = Enabled;
  330. ConstBuffers[Index].Size = ReadRegister(NvGpuEngine3dReg.ConstBufferNSize);
  331. }
  332. }
  333. private int ReadCb(AMemory Memory, int Cbuf, int Offset)
  334. {
  335. long Position = ConstBuffers[Cbuf].Position;
  336. int Value = Memory.ReadInt32(Position + Offset);
  337. return Value;
  338. }
  339. private bool TryGetCpuAddr(NvGpuEngine3dReg Reg, out long Position)
  340. {
  341. Position = MakeInt64From2xInt32(Reg);
  342. Position = Gpu.GetCpuAddr(Position);
  343. return Position != -1;
  344. }
  345. private long MakeInt64From2xInt32(NvGpuEngine3dReg Reg)
  346. {
  347. return
  348. (long)Registers[(int)Reg + 0] << 32 |
  349. (uint)Registers[(int)Reg + 1];
  350. }
  351. private void WriteRegister(NsGpuPBEntry PBEntry)
  352. {
  353. int ArgsCount = PBEntry.Arguments.Count;
  354. if (ArgsCount > 0)
  355. {
  356. Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1];
  357. }
  358. }
  359. private int ReadRegister(NvGpuEngine3dReg Reg)
  360. {
  361. return Registers[(int)Reg];
  362. }
  363. private void WriteRegister(NvGpuEngine3dReg Reg, int Value)
  364. {
  365. Registers[(int)Reg] = Value;
  366. }
  367. public bool IsFrameBufferPosition(long Position)
  368. {
  369. return FrameBuffers.Contains(Position);
  370. }
  371. }
  372. }