NvGpuEngine3d.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Graphics.Gal;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.Graphics.Gpu
  6. {
  7. 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[] Cbs;
  19. private bool HasDataToRender;
  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. Cbs = new ConstBuffer[18];
  39. }
  40. public void CallMethod(AMemory Memory, NsGpuPBEntry PBEntry)
  41. {
  42. if (Methods.TryGetValue(PBEntry.Method, out NvGpuMethod Method))
  43. {
  44. Method(Memory, PBEntry);
  45. }
  46. else
  47. {
  48. WriteRegister(PBEntry);
  49. }
  50. }
  51. private void VertexEndGl(AMemory Memory, NsGpuPBEntry PBEntry)
  52. {
  53. SetFrameBuffer(0);
  54. long[] Tags = UploadShaders(Memory);
  55. Gpu.Renderer.BindProgram();
  56. SetAlphaBlending();
  57. UploadTextures(Memory, Tags);
  58. UploadUniforms(Memory);
  59. UploadVertexArrays(Memory);
  60. HasDataToRender = true;
  61. }
  62. private void ClearBuffers(AMemory Memory, NsGpuPBEntry PBEntry)
  63. {
  64. if (HasDataToRender)
  65. {
  66. HasDataToRender = false;
  67. Gpu.Renderer.DrawFrameBuffer(0);
  68. }
  69. int Arg0 = PBEntry.Arguments[0];
  70. int FbIndex = (Arg0 >> 6) & 0xf;
  71. int Layer = (Arg0 >> 10) & 0x3ff;
  72. GalClearBufferFlags Flags = (GalClearBufferFlags)(Arg0 & 0x3f);
  73. SetFrameBuffer(0);
  74. Gpu.Renderer.ClearBuffers(Layer, Flags);
  75. }
  76. private void SetFrameBuffer(int FbIndex)
  77. {
  78. int Width = ReadRegister(NvGpuEngine3dReg.FrameBufferNWidth + FbIndex * 0x10);
  79. int Height = ReadRegister(NvGpuEngine3dReg.FrameBufferNHeight + FbIndex * 0x10);
  80. Gpu.Renderer.SetFb(FbIndex, Width, Height);
  81. Gpu.Renderer.BindFrameBuffer(FbIndex);
  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. return Tags;
  108. }
  109. private static GalShaderType GetTypeFromProgram(int Program)
  110. {
  111. switch (Program)
  112. {
  113. case 0:
  114. case 1: return GalShaderType.Vertex;
  115. case 2: return GalShaderType.TessControl;
  116. case 3: return GalShaderType.TessEvaluation;
  117. case 4: return GalShaderType.Geometry;
  118. case 5: return GalShaderType.Fragment;
  119. }
  120. throw new ArgumentOutOfRangeException(nameof(Program));
  121. }
  122. private void SetAlphaBlending()
  123. {
  124. bool BlendEnableMaster = (ReadRegister(NvGpuEngine3dReg.BlendEnableMaster) & 1) != 0;
  125. Gpu.Renderer.SetBlendEnable(BlendEnableMaster);
  126. bool BlendSeparateAlpha = (ReadRegister(NvGpuEngine3dReg.BlendSeparateAlpha) & 1) != 0;
  127. GalBlendEquation EquationRgb = (GalBlendEquation)ReadRegister(NvGpuEngine3dReg.BlendEquationRgb);
  128. GalBlendFactor FuncSrcRgb = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.BlendFuncSrcRgb);
  129. GalBlendFactor FuncDstRgb = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.BlendFuncDstRgb);
  130. if (BlendSeparateAlpha)
  131. {
  132. GalBlendEquation EquationAlpha = (GalBlendEquation)ReadRegister(NvGpuEngine3dReg.BlendEquationAlpha);
  133. GalBlendFactor FuncSrcAlpha = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.BlendFuncSrcAlpha);
  134. GalBlendFactor FuncDstAlpha = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.BlendFuncDstAlpha);
  135. Gpu.Renderer.SetBlendSeparate(
  136. EquationRgb,
  137. EquationAlpha,
  138. FuncSrcRgb,
  139. FuncDstRgb,
  140. FuncSrcAlpha,
  141. FuncDstAlpha);
  142. }
  143. else
  144. {
  145. Gpu.Renderer.SetBlend(EquationRgb, FuncSrcRgb, FuncDstRgb);
  146. }
  147. }
  148. private void UploadTextures(AMemory Memory, long[] Tags)
  149. {
  150. long BaseShPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  151. int TextureCbIndex = ReadRegister(NvGpuEngine3dReg.TextureCbIndex);
  152. long BasePosition = Cbs[TextureCbIndex].Position;
  153. long Size = (uint)Cbs[TextureCbIndex].Size;
  154. int TexIndex = 0;
  155. for (int Index = 0; Index < Tags.Length; Index++)
  156. {
  157. foreach (ShaderDeclInfo DeclInfo in Gpu.Renderer.GetTextureUsage(Tags[Index]))
  158. {
  159. long Position = BasePosition + Index * Size;
  160. UploadTexture(Memory, Position, TexIndex, DeclInfo.Index);
  161. Gpu.Renderer.SetUniform1(DeclInfo.Name, TexIndex);
  162. TexIndex++;
  163. }
  164. }
  165. }
  166. private void UploadTexture(AMemory Memory, long BasePosition, int TexIndex, int HndIndex)
  167. {
  168. long Position = BasePosition + HndIndex * 4;
  169. int TextureHandle = Memory.ReadInt32(Position);
  170. int TicIndex = (TextureHandle >> 0) & 0xfffff;
  171. int TscIndex = (TextureHandle >> 20) & 0xfff;
  172. TryGetCpuAddr(NvGpuEngine3dReg.TexHeaderPoolOffset, out long TicPosition);
  173. TryGetCpuAddr(NvGpuEngine3dReg.TexSamplerPoolOffset, out long TscPosition);
  174. TicPosition += TicIndex * 0x20;
  175. TscPosition += TscIndex * 0x20;
  176. Gpu.Renderer.SetTexture(TexIndex, TextureFactory.MakeTexture(Gpu, Memory, TicPosition));
  177. Gpu.Renderer.SetSampler(TexIndex, TextureFactory.MakeSampler(Gpu, Memory, TscPosition));
  178. }
  179. private void UploadUniforms(AMemory Memory)
  180. {
  181. long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  182. for (int Index = 0; Index < 5; Index++)
  183. {
  184. int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + (Index + 1) * 0x10);
  185. int Offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + (Index + 1) * 0x10);
  186. //Note: Vertex Program (B) is always enabled.
  187. bool Enable = (Control & 1) != 0 || Index == 0;
  188. if (!Enable)
  189. {
  190. continue;
  191. }
  192. for (int Cbuf = 0; Cbuf < Cbs.Length; Cbuf++)
  193. {
  194. ConstBuffer Cb = Cbs[Cbuf];
  195. if (Cb.Enabled)
  196. {
  197. long CbPosition = Cb.Position + Index * Cb.Size;
  198. byte[] Data = AMemoryHelper.ReadBytes(Memory, CbPosition, (uint)Cb.Size);
  199. Gpu.Renderer.SetConstBuffer(BasePosition + (uint)Offset, Cbuf, Data);
  200. }
  201. }
  202. }
  203. }
  204. private void UploadVertexArrays(AMemory Memory)
  205. {
  206. long IndexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress);
  207. int IndexSize = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat);
  208. int IndexFirst = ReadRegister(NvGpuEngine3dReg.IndexBatchFirst);
  209. int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount);
  210. GalIndexFormat IndexFormat = (GalIndexFormat)IndexSize;
  211. IndexSize = 1 << IndexSize;
  212. if (IndexSize > 4)
  213. {
  214. throw new InvalidOperationException();
  215. }
  216. if (IndexSize != 0)
  217. {
  218. IndexPosition = Gpu.GetCpuAddr(IndexPosition);
  219. int BufferSize = IndexCount * IndexSize;
  220. byte[] Data = AMemoryHelper.ReadBytes(Memory, IndexPosition, BufferSize);
  221. Gpu.Renderer.SetIndexArray(Data, IndexFormat);
  222. }
  223. List<GalVertexAttrib>[] Attribs = new List<GalVertexAttrib>[32];
  224. for (int Attr = 0; Attr < 16; Attr++)
  225. {
  226. int Packed = ReadRegister(NvGpuEngine3dReg.VertexAttribNFormat + Attr);
  227. int ArrayIndex = Packed & 0x1f;
  228. if (Attribs[ArrayIndex] == null)
  229. {
  230. Attribs[ArrayIndex] = new List<GalVertexAttrib>();
  231. }
  232. Attribs[ArrayIndex].Add(new GalVertexAttrib(
  233. ((Packed >> 6) & 0x1) != 0,
  234. (Packed >> 7) & 0x3fff,
  235. (GalVertexAttribSize)((Packed >> 21) & 0x3f),
  236. (GalVertexAttribType)((Packed >> 27) & 0x7),
  237. ((Packed >> 31) & 0x1) != 0));
  238. }
  239. for (int Index = 0; Index < 32; Index++)
  240. {
  241. int Control = ReadRegister(NvGpuEngine3dReg.VertexArrayNControl + Index * 4);
  242. bool Enable = (Control & 0x1000) != 0;
  243. if (!Enable)
  244. {
  245. continue;
  246. }
  247. long VertexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + Index * 4);
  248. long VertexEndPos = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNEndAddr + Index * 4);
  249. long Size = (VertexEndPos - VertexPosition) + 1;
  250. int Stride = Control & 0xfff;
  251. VertexPosition = Gpu.GetCpuAddr(VertexPosition);
  252. byte[] Data = AMemoryHelper.ReadBytes(Memory, VertexPosition, Size);
  253. GalVertexAttrib[] AttribArray = Attribs[Index]?.ToArray() ?? new GalVertexAttrib[0];
  254. Gpu.Renderer.SetVertexArray(Index, Stride, Data, AttribArray);
  255. int PrimCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl);
  256. GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff);
  257. if (IndexCount != 0)
  258. {
  259. Gpu.Renderer.DrawElements(Index, IndexFirst, PrimType);
  260. }
  261. else
  262. {
  263. Gpu.Renderer.DrawArrays(Index, PrimType);
  264. }
  265. }
  266. }
  267. private void QueryControl(AMemory Memory, NsGpuPBEntry PBEntry)
  268. {
  269. if (TryGetCpuAddr(NvGpuEngine3dReg.QueryAddress, out long Position))
  270. {
  271. int Seq = Registers[(int)NvGpuEngine3dReg.QuerySequence];
  272. int Ctrl = Registers[(int)NvGpuEngine3dReg.QueryControl];
  273. int Mode = Ctrl & 3;
  274. if (Mode == 0)
  275. {
  276. //Write.
  277. Memory.WriteInt32(Position, Seq);
  278. }
  279. }
  280. WriteRegister(PBEntry);
  281. }
  282. private void CbData(AMemory Memory, NsGpuPBEntry PBEntry)
  283. {
  284. if (TryGetCpuAddr(NvGpuEngine3dReg.ConstBufferNAddress, out long Position))
  285. {
  286. int Offset = ReadRegister(NvGpuEngine3dReg.ConstBufferNOffset);
  287. foreach (int Arg in PBEntry.Arguments)
  288. {
  289. Memory.WriteInt32(Position + Offset, Arg);
  290. Offset += 4;
  291. }
  292. WriteRegister(NvGpuEngine3dReg.ConstBufferNOffset, Offset);
  293. }
  294. }
  295. private void CbBind(AMemory Memory, NsGpuPBEntry PBEntry)
  296. {
  297. int Index = PBEntry.Arguments[0];
  298. bool Enabled = (Index & 1) != 0;
  299. Index = (Index >> 4) & 0x1f;
  300. if (TryGetCpuAddr(NvGpuEngine3dReg.ConstBufferNAddress, out long Position))
  301. {
  302. Cbs[Index].Position = Position;
  303. Cbs[Index].Enabled = Enabled;
  304. Cbs[Index].Size = ReadRegister(NvGpuEngine3dReg.ConstBufferNSize);
  305. }
  306. }
  307. private int ReadCb(AMemory Memory, int Cbuf, int Offset)
  308. {
  309. long Position = Cbs[Cbuf].Position;
  310. int Value = Memory.ReadInt32(Position + Offset);
  311. return Value;
  312. }
  313. private bool TryGetCpuAddr(NvGpuEngine3dReg Reg, out long Position)
  314. {
  315. Position = MakeInt64From2xInt32(Reg);
  316. Position = Gpu.GetCpuAddr(Position);
  317. return Position != -1;
  318. }
  319. private long MakeInt64From2xInt32(NvGpuEngine3dReg Reg)
  320. {
  321. return
  322. (long)Registers[(int)Reg + 0] << 32 |
  323. (uint)Registers[(int)Reg + 1];
  324. }
  325. private void WriteRegister(NsGpuPBEntry PBEntry)
  326. {
  327. int ArgsCount = PBEntry.Arguments.Count;
  328. if (ArgsCount > 0)
  329. {
  330. Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1];
  331. }
  332. }
  333. private int ReadRegister(NvGpuEngine3dReg Reg)
  334. {
  335. return Registers[(int)Reg];
  336. }
  337. private void WriteRegister(NvGpuEngine3dReg Reg, int Value)
  338. {
  339. Registers[(int)Reg] = Value;
  340. }
  341. }
  342. }