NvGpuEngine3d.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. 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. //TODO: Support independent blend properly.
  125. bool Enable = (ReadRegister(NvGpuEngine3dReg.IBlendEnable) & 1) != 0;
  126. Gpu.Renderer.SetBlendEnable(Enable);
  127. bool BlendSeparateAlpha = (ReadRegister(NvGpuEngine3dReg.IBlendNSeparateAlpha) & 1) != 0;
  128. GalBlendEquation EquationRgb = (GalBlendEquation)ReadRegister(NvGpuEngine3dReg.IBlendNEquationRgb);
  129. GalBlendFactor FuncSrcRgb = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncSrcRgb);
  130. GalBlendFactor FuncDstRgb = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncDstRgb);
  131. if (BlendSeparateAlpha)
  132. {
  133. GalBlendEquation EquationAlpha = (GalBlendEquation)ReadRegister(NvGpuEngine3dReg.IBlendNEquationAlpha);
  134. GalBlendFactor FuncSrcAlpha = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncSrcAlpha);
  135. GalBlendFactor FuncDstAlpha = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncDstAlpha);
  136. Gpu.Renderer.SetBlendSeparate(
  137. EquationRgb,
  138. EquationAlpha,
  139. FuncSrcRgb,
  140. FuncDstRgb,
  141. FuncSrcAlpha,
  142. FuncDstAlpha);
  143. }
  144. else
  145. {
  146. Gpu.Renderer.SetBlend(EquationRgb, FuncSrcRgb, FuncDstRgb);
  147. }
  148. }
  149. private void UploadTextures(AMemory Memory, long[] Tags)
  150. {
  151. long BaseShPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  152. int TextureCbIndex = ReadRegister(NvGpuEngine3dReg.TextureCbIndex);
  153. long BasePosition = ConstBuffers[TextureCbIndex].Position;
  154. long Size = (uint)ConstBuffers[TextureCbIndex].Size;
  155. //Note: On the emulator renderer, Texture Unit 0 is
  156. //reserved for drawing the frame buffer.
  157. int TexIndex = 1;
  158. for (int Index = 0; Index < Tags.Length; Index++)
  159. {
  160. foreach (ShaderDeclInfo DeclInfo in Gpu.Renderer.GetTextureUsage(Tags[Index]))
  161. {
  162. long Position = BasePosition + Index * Size;
  163. UploadTexture(Memory, Position, TexIndex, DeclInfo.Index);
  164. Gpu.Renderer.SetUniform1(DeclInfo.Name, TexIndex);
  165. TexIndex++;
  166. }
  167. }
  168. }
  169. private void UploadTexture(AMemory Memory, long BasePosition, int TexIndex, int HndIndex)
  170. {
  171. long Position = BasePosition + HndIndex * 4;
  172. int TextureHandle = Memory.ReadInt32(Position);
  173. int TicIndex = (TextureHandle >> 0) & 0xfffff;
  174. int TscIndex = (TextureHandle >> 20) & 0xfff;
  175. TryGetCpuAddr(NvGpuEngine3dReg.TexHeaderPoolOffset, out long TicPosition);
  176. TryGetCpuAddr(NvGpuEngine3dReg.TexSamplerPoolOffset, out long TscPosition);
  177. TicPosition += TicIndex * 0x20;
  178. TscPosition += TscIndex * 0x20;
  179. GalTextureSampler Sampler = TextureFactory.MakeSampler(Gpu, Memory, TscPosition);
  180. long TextureAddress = Memory.ReadInt64(TicPosition + 4) & 0xffffffffffff;
  181. if (FrameBuffers.Contains(TextureAddress))
  182. {
  183. //This texture is a frame buffer texture,
  184. //we shouldn't read anything from memory and bind
  185. //the frame buffer texture instead, since we're not
  186. //really writing anything to memory.
  187. Gpu.Renderer.BindFrameBufferTexture(TextureAddress, TexIndex, Sampler);
  188. }
  189. else
  190. {
  191. GalTexture Texture = TextureFactory.MakeTexture(Gpu, Memory, TicPosition);
  192. Gpu.Renderer.SetTextureAndSampler(TexIndex, Texture, Sampler);
  193. Gpu.Renderer.BindTexture(TexIndex);
  194. }
  195. }
  196. private void UploadUniforms(AMemory Memory)
  197. {
  198. long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  199. for (int Index = 0; Index < 5; Index++)
  200. {
  201. int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + (Index + 1) * 0x10);
  202. int Offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + (Index + 1) * 0x10);
  203. //Note: Vertex Program (B) is always enabled.
  204. bool Enable = (Control & 1) != 0 || Index == 0;
  205. if (!Enable)
  206. {
  207. continue;
  208. }
  209. for (int Cbuf = 0; Cbuf < ConstBuffers.Length; Cbuf++)
  210. {
  211. ConstBuffer Cb = ConstBuffers[Cbuf];
  212. if (Cb.Enabled)
  213. {
  214. long CbPosition = Cb.Position + Index * Cb.Size;
  215. byte[] Data = AMemoryHelper.ReadBytes(Memory, CbPosition, (uint)Cb.Size);
  216. Gpu.Renderer.SetConstBuffer(BasePosition + (uint)Offset, Cbuf, Data);
  217. }
  218. }
  219. }
  220. }
  221. private void UploadVertexArrays(AMemory Memory)
  222. {
  223. long IndexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress);
  224. int IndexSize = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat);
  225. int IndexFirst = ReadRegister(NvGpuEngine3dReg.IndexBatchFirst);
  226. int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount);
  227. GalIndexFormat IndexFormat = (GalIndexFormat)IndexSize;
  228. IndexSize = 1 << IndexSize;
  229. if (IndexSize > 4)
  230. {
  231. throw new InvalidOperationException();
  232. }
  233. if (IndexSize != 0)
  234. {
  235. IndexPosition = Gpu.GetCpuAddr(IndexPosition);
  236. int BufferSize = IndexCount * IndexSize;
  237. byte[] Data = AMemoryHelper.ReadBytes(Memory, IndexPosition, BufferSize);
  238. Gpu.Renderer.SetIndexArray(Data, IndexFormat);
  239. }
  240. List<GalVertexAttrib>[] Attribs = new List<GalVertexAttrib>[32];
  241. for (int Attr = 0; Attr < 16; Attr++)
  242. {
  243. int Packed = ReadRegister(NvGpuEngine3dReg.VertexAttribNFormat + Attr);
  244. int ArrayIndex = Packed & 0x1f;
  245. if (Attribs[ArrayIndex] == null)
  246. {
  247. Attribs[ArrayIndex] = new List<GalVertexAttrib>();
  248. }
  249. Attribs[ArrayIndex].Add(new GalVertexAttrib(
  250. ((Packed >> 6) & 0x1) != 0,
  251. (Packed >> 7) & 0x3fff,
  252. (GalVertexAttribSize)((Packed >> 21) & 0x3f),
  253. (GalVertexAttribType)((Packed >> 27) & 0x7),
  254. ((Packed >> 31) & 0x1) != 0));
  255. }
  256. for (int Index = 0; Index < 32; Index++)
  257. {
  258. int Control = ReadRegister(NvGpuEngine3dReg.VertexArrayNControl + Index * 4);
  259. bool Enable = (Control & 0x1000) != 0;
  260. if (!Enable)
  261. {
  262. continue;
  263. }
  264. long VertexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + Index * 4);
  265. long VertexEndPos = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNEndAddr + Index * 4);
  266. long Size = (VertexEndPos - VertexPosition) + 1;
  267. int Stride = Control & 0xfff;
  268. VertexPosition = Gpu.GetCpuAddr(VertexPosition);
  269. byte[] Data = AMemoryHelper.ReadBytes(Memory, VertexPosition, Size);
  270. GalVertexAttrib[] AttribArray = Attribs[Index]?.ToArray() ?? new GalVertexAttrib[0];
  271. Gpu.Renderer.SetVertexArray(Index, Stride, Data, AttribArray);
  272. int PrimCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl);
  273. GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff);
  274. if (IndexCount != 0)
  275. {
  276. Gpu.Renderer.DrawElements(Index, IndexFirst, PrimType);
  277. }
  278. else
  279. {
  280. Gpu.Renderer.DrawArrays(Index, PrimType);
  281. }
  282. }
  283. }
  284. private void QueryControl(AMemory Memory, NsGpuPBEntry PBEntry)
  285. {
  286. if (TryGetCpuAddr(NvGpuEngine3dReg.QueryAddress, out long Position))
  287. {
  288. int Seq = Registers[(int)NvGpuEngine3dReg.QuerySequence];
  289. int Ctrl = Registers[(int)NvGpuEngine3dReg.QueryControl];
  290. int Mode = Ctrl & 3;
  291. if (Mode == 0)
  292. {
  293. //Write mode.
  294. Memory.WriteInt32(Position, Seq);
  295. }
  296. }
  297. WriteRegister(PBEntry);
  298. }
  299. private void CbData(AMemory Memory, NsGpuPBEntry PBEntry)
  300. {
  301. if (TryGetCpuAddr(NvGpuEngine3dReg.ConstBufferNAddress, out long Position))
  302. {
  303. int Offset = ReadRegister(NvGpuEngine3dReg.ConstBufferNOffset);
  304. foreach (int Arg in PBEntry.Arguments)
  305. {
  306. Memory.WriteInt32(Position + Offset, Arg);
  307. Offset += 4;
  308. }
  309. WriteRegister(NvGpuEngine3dReg.ConstBufferNOffset, Offset);
  310. }
  311. }
  312. private void CbBind(AMemory Memory, NsGpuPBEntry PBEntry)
  313. {
  314. int Index = PBEntry.Arguments[0];
  315. bool Enabled = (Index & 1) != 0;
  316. Index = (Index >> 4) & 0x1f;
  317. if (TryGetCpuAddr(NvGpuEngine3dReg.ConstBufferNAddress, out long Position))
  318. {
  319. ConstBuffers[Index].Position = Position;
  320. ConstBuffers[Index].Enabled = Enabled;
  321. ConstBuffers[Index].Size = ReadRegister(NvGpuEngine3dReg.ConstBufferNSize);
  322. }
  323. }
  324. private int ReadCb(AMemory Memory, int Cbuf, int Offset)
  325. {
  326. long Position = ConstBuffers[Cbuf].Position;
  327. int Value = Memory.ReadInt32(Position + Offset);
  328. return Value;
  329. }
  330. private bool TryGetCpuAddr(NvGpuEngine3dReg Reg, out long Position)
  331. {
  332. Position = MakeInt64From2xInt32(Reg);
  333. Position = Gpu.GetCpuAddr(Position);
  334. return Position != -1;
  335. }
  336. private long MakeInt64From2xInt32(NvGpuEngine3dReg Reg)
  337. {
  338. return
  339. (long)Registers[(int)Reg + 0] << 32 |
  340. (uint)Registers[(int)Reg + 1];
  341. }
  342. private void WriteRegister(NsGpuPBEntry PBEntry)
  343. {
  344. int ArgsCount = PBEntry.Arguments.Count;
  345. if (ArgsCount > 0)
  346. {
  347. Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1];
  348. }
  349. }
  350. private int ReadRegister(NvGpuEngine3dReg Reg)
  351. {
  352. return Registers[(int)Reg];
  353. }
  354. private void WriteRegister(NvGpuEngine3dReg Reg, int Value)
  355. {
  356. Registers[(int)Reg] = Value;
  357. }
  358. public bool IsFrameBufferPosition(long Position)
  359. {
  360. return FrameBuffers.Contains(Position);
  361. }
  362. }
  363. }