NvGpuEngine3d.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. Attr,
  258. ((Packed >> 6) & 0x1) != 0,
  259. (Packed >> 7) & 0x3fff,
  260. (GalVertexAttribSize)((Packed >> 21) & 0x3f),
  261. (GalVertexAttribType)((Packed >> 27) & 0x7),
  262. ((Packed >> 31) & 0x1) != 0));
  263. }
  264. for (int Index = 0; Index < 32; Index++)
  265. {
  266. int VertexFirst = ReadRegister(NvGpuEngine3dReg.VertexArrayFirst);
  267. int VertexCount = ReadRegister(NvGpuEngine3dReg.VertexArrayCount);
  268. int Control = ReadRegister(NvGpuEngine3dReg.VertexArrayNControl + Index * 4);
  269. bool Enable = (Control & 0x1000) != 0;
  270. long VertexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + Index * 4);
  271. if (!Enable)
  272. {
  273. continue;
  274. }
  275. int Stride = Control & 0xfff;
  276. long Size = 0;
  277. if (IndexCount != 0)
  278. {
  279. Size = GetVertexCountFromIndexBuffer(
  280. Memory,
  281. IndexPosition,
  282. IndexCount,
  283. IndexSize);
  284. }
  285. else
  286. {
  287. Size = VertexCount;
  288. }
  289. //TODO: Support cases where the Stride is 0.
  290. //In this case, we need to use the size of the attribute.
  291. Size *= Stride;
  292. VertexPosition = Gpu.GetCpuAddr(VertexPosition);
  293. byte[] Data = AMemoryHelper.ReadBytes(Memory, VertexPosition, Size);
  294. GalVertexAttrib[] AttribArray = Attribs[Index]?.ToArray() ?? new GalVertexAttrib[0];
  295. Gpu.Renderer.SetVertexArray(Index, Stride, Data, AttribArray);
  296. int PrimCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl);
  297. GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff);
  298. if (IndexCount != 0)
  299. {
  300. Gpu.Renderer.DrawElements(Index, IndexFirst, PrimType);
  301. }
  302. else
  303. {
  304. Gpu.Renderer.DrawArrays(Index, VertexFirst, VertexCount, PrimType);
  305. }
  306. }
  307. }
  308. private int GetVertexCountFromIndexBuffer(
  309. AMemory Memory,
  310. long IndexPosition,
  311. int IndexCount,
  312. int IndexSize)
  313. {
  314. int MaxIndex = -1;
  315. if (IndexSize == 2)
  316. {
  317. while (IndexCount -- > 0)
  318. {
  319. ushort Value = Memory.ReadUInt16(IndexPosition);
  320. IndexPosition += 2;
  321. if (MaxIndex < Value)
  322. {
  323. MaxIndex = Value;
  324. }
  325. }
  326. }
  327. else if (IndexSize == 1)
  328. {
  329. while (IndexCount -- > 0)
  330. {
  331. byte Value = Memory.ReadByte(IndexPosition++);
  332. if (MaxIndex < Value)
  333. {
  334. MaxIndex = Value;
  335. }
  336. }
  337. }
  338. else if (IndexSize == 4)
  339. {
  340. while (IndexCount -- > 0)
  341. {
  342. uint Value = Memory.ReadUInt32(IndexPosition);
  343. IndexPosition += 2;
  344. if (MaxIndex < Value)
  345. {
  346. MaxIndex = (int)Value;
  347. }
  348. }
  349. }
  350. else
  351. {
  352. throw new ArgumentOutOfRangeException(nameof(IndexSize));
  353. }
  354. return MaxIndex + 1;
  355. }
  356. private void QueryControl(AMemory Memory, NsGpuPBEntry PBEntry)
  357. {
  358. if (TryGetCpuAddr(NvGpuEngine3dReg.QueryAddress, out long Position))
  359. {
  360. int Seq = Registers[(int)NvGpuEngine3dReg.QuerySequence];
  361. int Ctrl = Registers[(int)NvGpuEngine3dReg.QueryControl];
  362. int Mode = Ctrl & 3;
  363. if (Mode == 0)
  364. {
  365. //Write mode.
  366. Memory.WriteInt32(Position, Seq);
  367. }
  368. }
  369. WriteRegister(PBEntry);
  370. }
  371. private void CbData(AMemory Memory, NsGpuPBEntry PBEntry)
  372. {
  373. if (TryGetCpuAddr(NvGpuEngine3dReg.ConstBufferNAddress, out long Position))
  374. {
  375. int Offset = ReadRegister(NvGpuEngine3dReg.ConstBufferNOffset);
  376. foreach (int Arg in PBEntry.Arguments)
  377. {
  378. Memory.WriteInt32(Position + Offset, Arg);
  379. Offset += 4;
  380. }
  381. WriteRegister(NvGpuEngine3dReg.ConstBufferNOffset, Offset);
  382. }
  383. }
  384. private void CbBind(AMemory Memory, NsGpuPBEntry PBEntry)
  385. {
  386. int Index = PBEntry.Arguments[0];
  387. bool Enabled = (Index & 1) != 0;
  388. Index = (Index >> 4) & 0x1f;
  389. if (TryGetCpuAddr(NvGpuEngine3dReg.ConstBufferNAddress, out long Position))
  390. {
  391. ConstBuffers[Index].Position = Position;
  392. ConstBuffers[Index].Enabled = Enabled;
  393. ConstBuffers[Index].Size = ReadRegister(NvGpuEngine3dReg.ConstBufferNSize);
  394. }
  395. }
  396. private int ReadCb(AMemory Memory, int Cbuf, int Offset)
  397. {
  398. long Position = ConstBuffers[Cbuf].Position;
  399. int Value = Memory.ReadInt32(Position + Offset);
  400. return Value;
  401. }
  402. private bool TryGetCpuAddr(NvGpuEngine3dReg Reg, out long Position)
  403. {
  404. Position = MakeInt64From2xInt32(Reg);
  405. Position = Gpu.GetCpuAddr(Position);
  406. return Position != -1;
  407. }
  408. private long MakeInt64From2xInt32(NvGpuEngine3dReg Reg)
  409. {
  410. return
  411. (long)Registers[(int)Reg + 0] << 32 |
  412. (uint)Registers[(int)Reg + 1];
  413. }
  414. private void WriteRegister(NsGpuPBEntry PBEntry)
  415. {
  416. int ArgsCount = PBEntry.Arguments.Count;
  417. if (ArgsCount > 0)
  418. {
  419. Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1];
  420. }
  421. }
  422. private int ReadRegister(NvGpuEngine3dReg Reg)
  423. {
  424. return Registers[(int)Reg];
  425. }
  426. private void WriteRegister(NvGpuEngine3dReg Reg, int Value)
  427. {
  428. Registers[(int)Reg] = Value;
  429. }
  430. public bool IsFrameBufferPosition(long Position)
  431. {
  432. return FrameBuffers.Contains(Position);
  433. }
  434. }
  435. }