NvGpuEngine3d.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. using Ryujinx.Graphics.Gal;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace Ryujinx.Core.Gpu
  5. {
  6. public class NvGpuEngine3d : INvGpuEngine
  7. {
  8. public int[] Registers { get; private set; }
  9. private NvGpu Gpu;
  10. private Dictionary<int, NvGpuMethod> Methods;
  11. private struct ConstBuffer
  12. {
  13. public bool Enabled;
  14. public long Position;
  15. public int Size;
  16. }
  17. private ConstBuffer[][] ConstBuffers;
  18. private HashSet<long> FrameBuffers;
  19. public NvGpuEngine3d(NvGpu Gpu)
  20. {
  21. this.Gpu = Gpu;
  22. Registers = new int[0xe00];
  23. Methods = new Dictionary<int, NvGpuMethod>();
  24. void AddMethod(int Meth, int Count, int Stride, NvGpuMethod Method)
  25. {
  26. while (Count-- > 0)
  27. {
  28. Methods.Add(Meth, Method);
  29. Meth += Stride;
  30. }
  31. }
  32. AddMethod(0x585, 1, 1, VertexEndGl);
  33. AddMethod(0x674, 1, 1, ClearBuffers);
  34. AddMethod(0x6c3, 1, 1, QueryControl);
  35. AddMethod(0x8e4, 16, 1, CbData);
  36. AddMethod(0x904, 5, 8, CbBind);
  37. ConstBuffers = new ConstBuffer[6][];
  38. for (int Index = 0; Index < ConstBuffers.Length; Index++)
  39. {
  40. ConstBuffers[Index] = new ConstBuffer[18];
  41. }
  42. FrameBuffers = new HashSet<long>();
  43. }
  44. public void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  45. {
  46. if (Methods.TryGetValue(PBEntry.Method, out NvGpuMethod Method))
  47. {
  48. Method(Vmm, PBEntry);
  49. }
  50. else
  51. {
  52. WriteRegister(PBEntry);
  53. }
  54. }
  55. private void VertexEndGl(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  56. {
  57. SetFrameBuffer(Vmm, 0);
  58. long[] Tags = UploadShaders(Vmm);
  59. Gpu.Renderer.BindProgram();
  60. SetAlphaBlending();
  61. UploadTextures(Vmm, Tags);
  62. UploadUniforms(Vmm);
  63. UploadVertexArrays(Vmm);
  64. }
  65. private void ClearBuffers(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  66. {
  67. int Arg0 = PBEntry.Arguments[0];
  68. int FbIndex = (Arg0 >> 6) & 0xf;
  69. int Layer = (Arg0 >> 10) & 0x3ff;
  70. GalClearBufferFlags Flags = (GalClearBufferFlags)(Arg0 & 0x3f);
  71. SetFrameBuffer(Vmm, 0);
  72. //TODO: Enable this once the frame buffer problems are fixed.
  73. //Gpu.Renderer.ClearBuffers(Layer, Flags);
  74. }
  75. private void SetFrameBuffer(NvGpuVmm Vmm, int FbIndex)
  76. {
  77. long VA = MakeInt64From2xInt32(NvGpuEngine3dReg.FrameBufferNAddress + FbIndex * 0x10);
  78. long PA = Vmm.GetPhysicalAddress(VA);
  79. FrameBuffers.Add(PA);
  80. int Width = ReadRegister(NvGpuEngine3dReg.FrameBufferNWidth + FbIndex * 0x10);
  81. int Height = ReadRegister(NvGpuEngine3dReg.FrameBufferNHeight + FbIndex * 0x10);
  82. //Note: Using the Width/Height results seems to give incorrect results.
  83. //Maybe the size of all frame buffers is hardcoded to screen size? This seems unlikely.
  84. Gpu.Renderer.CreateFrameBuffer(PA, 1280, 720);
  85. Gpu.Renderer.BindFrameBuffer(PA);
  86. }
  87. private long[] UploadShaders(NvGpuVmm Vmm)
  88. {
  89. long[] Tags = new long[5];
  90. long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  91. for (int Index = 0; Index < 6; Index++)
  92. {
  93. int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + Index * 0x10);
  94. int Offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + Index * 0x10);
  95. //Note: Vertex Program (B) is always enabled.
  96. bool Enable = (Control & 1) != 0 || Index == 1;
  97. if (!Enable)
  98. {
  99. continue;
  100. }
  101. long Tag = BasePosition + (uint)Offset;
  102. GalShaderType ShaderType = GetTypeFromProgram(Index);
  103. Tags[(int)ShaderType] = Tag;
  104. Gpu.Renderer.CreateShader(Vmm, Tag, ShaderType);
  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(NvGpuVmm Vmm, long[] Tags)
  157. {
  158. long BaseShPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  159. int TextureCbIndex = ReadRegister(NvGpuEngine3dReg.TextureCbIndex);
  160. //Note: On the emulator renderer, Texture Unit 0 is
  161. //reserved for drawing the frame buffer.
  162. int TexIndex = 1;
  163. for (int Index = 0; Index < Tags.Length; Index++)
  164. {
  165. foreach (ShaderDeclInfo DeclInfo in Gpu.Renderer.GetTextureUsage(Tags[Index]))
  166. {
  167. long Position = ConstBuffers[Index][TextureCbIndex].Position;
  168. UploadTexture(Vmm, Position, TexIndex, DeclInfo.Index);
  169. Gpu.Renderer.SetUniform1(DeclInfo.Name, TexIndex);
  170. TexIndex++;
  171. }
  172. }
  173. }
  174. private void UploadTexture(NvGpuVmm Vmm, long BasePosition, int TexIndex, int HndIndex)
  175. {
  176. long Position = BasePosition + HndIndex * 4;
  177. int TextureHandle = Vmm.ReadInt32(Position);
  178. int TicIndex = (TextureHandle >> 0) & 0xfffff;
  179. int TscIndex = (TextureHandle >> 20) & 0xfff;
  180. long TicPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexHeaderPoolOffset);
  181. long TscPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexSamplerPoolOffset);
  182. TicPosition += TicIndex * 0x20;
  183. TscPosition += TscIndex * 0x20;
  184. GalTextureSampler Sampler = TextureFactory.MakeSampler(Gpu, Vmm, TscPosition);
  185. long TextureAddress = Vmm.ReadInt64(TicPosition + 4) & 0xffffffffffff;
  186. TextureAddress = Vmm.GetPhysicalAddress(TextureAddress);
  187. if (IsFrameBufferPosition(TextureAddress))
  188. {
  189. //This texture is a frame buffer texture,
  190. //we shouldn't read anything from memory and bind
  191. //the frame buffer texture instead, since we're not
  192. //really writing anything to memory.
  193. Gpu.Renderer.BindFrameBufferTexture(TextureAddress, TexIndex, Sampler);
  194. }
  195. else
  196. {
  197. GalTexture Texture = TextureFactory.MakeTexture(Gpu, Vmm, TicPosition);
  198. Gpu.Renderer.SetTextureAndSampler(TexIndex, Texture, Sampler);
  199. Gpu.Renderer.BindTexture(TexIndex);
  200. }
  201. }
  202. private void UploadUniforms(NvGpuVmm Vmm)
  203. {
  204. long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  205. for (int Index = 0; Index < 5; Index++)
  206. {
  207. int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + (Index + 1) * 0x10);
  208. int Offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + (Index + 1) * 0x10);
  209. //Note: Vertex Program (B) is always enabled.
  210. bool Enable = (Control & 1) != 0 || Index == 0;
  211. if (!Enable)
  212. {
  213. continue;
  214. }
  215. for (int Cbuf = 0; Cbuf < ConstBuffers.Length; Cbuf++)
  216. {
  217. ConstBuffer Cb = ConstBuffers[Index][Cbuf];
  218. if (Cb.Enabled)
  219. {
  220. byte[] Data = Vmm.ReadBytes(Cb.Position, (uint)Cb.Size);
  221. Gpu.Renderer.SetConstBuffer(BasePosition + (uint)Offset, Cbuf, Data);
  222. }
  223. }
  224. }
  225. }
  226. private void UploadVertexArrays(NvGpuVmm Vmm)
  227. {
  228. long IndexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress);
  229. int IndexSize = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat);
  230. int IndexFirst = ReadRegister(NvGpuEngine3dReg.IndexBatchFirst);
  231. int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount);
  232. GalIndexFormat IndexFormat = (GalIndexFormat)IndexSize;
  233. IndexSize = 1 << IndexSize;
  234. if (IndexSize > 4)
  235. {
  236. throw new InvalidOperationException();
  237. }
  238. if (IndexSize != 0)
  239. {
  240. int BufferSize = IndexCount * IndexSize;
  241. byte[] Data = Vmm.ReadBytes(IndexPosition, BufferSize);
  242. Gpu.Renderer.SetIndexArray(Data, IndexFormat);
  243. }
  244. List<GalVertexAttrib>[] Attribs = new List<GalVertexAttrib>[32];
  245. for (int Attr = 0; Attr < 16; Attr++)
  246. {
  247. int Packed = ReadRegister(NvGpuEngine3dReg.VertexAttribNFormat + Attr);
  248. int ArrayIndex = Packed & 0x1f;
  249. if (Attribs[ArrayIndex] == null)
  250. {
  251. Attribs[ArrayIndex] = new List<GalVertexAttrib>();
  252. }
  253. Attribs[ArrayIndex].Add(new GalVertexAttrib(
  254. Attr,
  255. ((Packed >> 6) & 0x1) != 0,
  256. (Packed >> 7) & 0x3fff,
  257. (GalVertexAttribSize)((Packed >> 21) & 0x3f),
  258. (GalVertexAttribType)((Packed >> 27) & 0x7),
  259. ((Packed >> 31) & 0x1) != 0));
  260. }
  261. for (int Index = 0; Index < 32; Index++)
  262. {
  263. int VertexFirst = ReadRegister(NvGpuEngine3dReg.VertexArrayFirst);
  264. int VertexCount = ReadRegister(NvGpuEngine3dReg.VertexArrayCount);
  265. int Control = ReadRegister(NvGpuEngine3dReg.VertexArrayNControl + Index * 4);
  266. bool Enable = (Control & 0x1000) != 0;
  267. long VertexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + Index * 4);
  268. if (!Enable)
  269. {
  270. continue;
  271. }
  272. int Stride = Control & 0xfff;
  273. long Size = 0;
  274. if (IndexCount != 0)
  275. {
  276. Size = GetVertexCountFromIndexBuffer(
  277. Vmm,
  278. IndexPosition,
  279. IndexCount,
  280. IndexSize);
  281. }
  282. else
  283. {
  284. Size = VertexCount;
  285. }
  286. //TODO: Support cases where the Stride is 0.
  287. //In this case, we need to use the size of the attribute.
  288. Size *= Stride;
  289. byte[] Data = Vmm.ReadBytes(VertexPosition, Size);
  290. GalVertexAttrib[] AttribArray = Attribs[Index]?.ToArray() ?? new GalVertexAttrib[0];
  291. Gpu.Renderer.SetVertexArray(Index, Stride, Data, AttribArray);
  292. int PrimCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl);
  293. GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff);
  294. if (IndexCount != 0)
  295. {
  296. Gpu.Renderer.DrawElements(Index, IndexFirst, PrimType);
  297. }
  298. else
  299. {
  300. Gpu.Renderer.DrawArrays(Index, VertexFirst, VertexCount, PrimType);
  301. }
  302. }
  303. }
  304. private int GetVertexCountFromIndexBuffer(
  305. NvGpuVmm Vmm,
  306. long IndexPosition,
  307. int IndexCount,
  308. int IndexSize)
  309. {
  310. int MaxIndex = -1;
  311. if (IndexSize == 2)
  312. {
  313. while (IndexCount -- > 0)
  314. {
  315. ushort Value = Vmm.ReadUInt16(IndexPosition);
  316. IndexPosition += 2;
  317. if (MaxIndex < Value)
  318. {
  319. MaxIndex = Value;
  320. }
  321. }
  322. }
  323. else if (IndexSize == 1)
  324. {
  325. while (IndexCount -- > 0)
  326. {
  327. byte Value = Vmm.ReadByte(IndexPosition++);
  328. if (MaxIndex < Value)
  329. {
  330. MaxIndex = Value;
  331. }
  332. }
  333. }
  334. else if (IndexSize == 4)
  335. {
  336. while (IndexCount -- > 0)
  337. {
  338. uint Value = Vmm.ReadUInt32(IndexPosition);
  339. IndexPosition += 2;
  340. if (MaxIndex < Value)
  341. {
  342. MaxIndex = (int)Value;
  343. }
  344. }
  345. }
  346. else
  347. {
  348. throw new ArgumentOutOfRangeException(nameof(IndexSize));
  349. }
  350. return MaxIndex + 1;
  351. }
  352. private void QueryControl(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  353. {
  354. long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.QueryAddress);
  355. int Seq = Registers[(int)NvGpuEngine3dReg.QuerySequence];
  356. int Ctrl = Registers[(int)NvGpuEngine3dReg.QueryControl];
  357. int Mode = Ctrl & 3;
  358. if (Mode == 0)
  359. {
  360. //Write mode.
  361. Vmm.WriteInt32(Position, Seq);
  362. }
  363. WriteRegister(PBEntry);
  364. }
  365. private void CbData(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  366. {
  367. long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress);
  368. int Offset = ReadRegister(NvGpuEngine3dReg.ConstBufferOffset);
  369. foreach (int Arg in PBEntry.Arguments)
  370. {
  371. Vmm.WriteInt32(Position + Offset, Arg);
  372. Offset += 4;
  373. }
  374. WriteRegister(NvGpuEngine3dReg.ConstBufferOffset, Offset);
  375. }
  376. private void CbBind(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  377. {
  378. int Stage = (PBEntry.Method - 0x904) >> 3;
  379. int Index = PBEntry.Arguments[0];
  380. bool Enabled = (Index & 1) != 0;
  381. Index = (Index >> 4) & 0x1f;
  382. long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress);
  383. ConstBuffers[Stage][Index].Position = Position;
  384. ConstBuffers[Stage][Index].Enabled = Enabled;
  385. ConstBuffers[Stage][Index].Size = ReadRegister(NvGpuEngine3dReg.ConstBufferSize);
  386. }
  387. private long MakeInt64From2xInt32(NvGpuEngine3dReg Reg)
  388. {
  389. return
  390. (long)Registers[(int)Reg + 0] << 32 |
  391. (uint)Registers[(int)Reg + 1];
  392. }
  393. private void WriteRegister(NvGpuPBEntry PBEntry)
  394. {
  395. int ArgsCount = PBEntry.Arguments.Count;
  396. if (ArgsCount > 0)
  397. {
  398. Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1];
  399. }
  400. }
  401. private int ReadRegister(NvGpuEngine3dReg Reg)
  402. {
  403. return Registers[(int)Reg];
  404. }
  405. private void WriteRegister(NvGpuEngine3dReg Reg, int Value)
  406. {
  407. Registers[(int)Reg] = Value;
  408. }
  409. public bool IsFrameBufferPosition(long Position)
  410. {
  411. return FrameBuffers.Contains(Position);
  412. }
  413. }
  414. }