NvGpuEngine3d.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. //TODO: Find a better way to calculate the size.
  103. int Size = 0x20000;
  104. byte[] Code = Vmm.ReadBytes(Tag, Size);
  105. GalShaderType ShaderType = GetTypeFromProgram(Index);
  106. Tags[(int)ShaderType] = Tag;
  107. Gpu.Renderer.CreateShader(Tag, ShaderType, Code);
  108. Gpu.Renderer.BindShader(Tag);
  109. }
  110. int RawSX = ReadRegister(NvGpuEngine3dReg.ViewportScaleX);
  111. int RawSY = ReadRegister(NvGpuEngine3dReg.ViewportScaleY);
  112. float SX = BitConverter.Int32BitsToSingle(RawSX);
  113. float SY = BitConverter.Int32BitsToSingle(RawSY);
  114. float SignX = MathF.Sign(SX);
  115. float SignY = MathF.Sign(SY);
  116. Gpu.Renderer.SetUniform2F(GalConsts.FlipUniformName, SignX, SignY);
  117. return Tags;
  118. }
  119. private static GalShaderType GetTypeFromProgram(int Program)
  120. {
  121. switch (Program)
  122. {
  123. case 0:
  124. case 1: return GalShaderType.Vertex;
  125. case 2: return GalShaderType.TessControl;
  126. case 3: return GalShaderType.TessEvaluation;
  127. case 4: return GalShaderType.Geometry;
  128. case 5: return GalShaderType.Fragment;
  129. }
  130. throw new ArgumentOutOfRangeException(nameof(Program));
  131. }
  132. private void SetAlphaBlending()
  133. {
  134. //TODO: Support independent blend properly.
  135. bool Enable = (ReadRegister(NvGpuEngine3dReg.IBlendNEnable) & 1) != 0;
  136. Gpu.Renderer.SetBlendEnable(Enable);
  137. bool BlendSeparateAlpha = (ReadRegister(NvGpuEngine3dReg.IBlendNSeparateAlpha) & 1) != 0;
  138. GalBlendEquation EquationRgb = (GalBlendEquation)ReadRegister(NvGpuEngine3dReg.IBlendNEquationRgb);
  139. GalBlendFactor FuncSrcRgb = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncSrcRgb);
  140. GalBlendFactor FuncDstRgb = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncDstRgb);
  141. if (BlendSeparateAlpha)
  142. {
  143. GalBlendEquation EquationAlpha = (GalBlendEquation)ReadRegister(NvGpuEngine3dReg.IBlendNEquationAlpha);
  144. GalBlendFactor FuncSrcAlpha = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncSrcAlpha);
  145. GalBlendFactor FuncDstAlpha = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncDstAlpha);
  146. Gpu.Renderer.SetBlendSeparate(
  147. EquationRgb,
  148. EquationAlpha,
  149. FuncSrcRgb,
  150. FuncDstRgb,
  151. FuncSrcAlpha,
  152. FuncDstAlpha);
  153. }
  154. else
  155. {
  156. Gpu.Renderer.SetBlend(EquationRgb, FuncSrcRgb, FuncDstRgb);
  157. }
  158. }
  159. private void UploadTextures(NvGpuVmm Vmm, long[] Tags)
  160. {
  161. long BaseShPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  162. int TextureCbIndex = ReadRegister(NvGpuEngine3dReg.TextureCbIndex);
  163. //Note: On the emulator renderer, Texture Unit 0 is
  164. //reserved for drawing the frame buffer.
  165. int TexIndex = 1;
  166. for (int Index = 0; Index < Tags.Length; Index++)
  167. {
  168. foreach (ShaderDeclInfo DeclInfo in Gpu.Renderer.GetTextureUsage(Tags[Index]))
  169. {
  170. long Position = ConstBuffers[Index][TextureCbIndex].Position;
  171. UploadTexture(Vmm, Position, TexIndex, DeclInfo.Index);
  172. Gpu.Renderer.SetUniform1(DeclInfo.Name, TexIndex);
  173. TexIndex++;
  174. }
  175. }
  176. }
  177. private void UploadTexture(NvGpuVmm Vmm, long BasePosition, int TexIndex, int HndIndex)
  178. {
  179. long Position = BasePosition + HndIndex * 4;
  180. int TextureHandle = Vmm.ReadInt32(Position);
  181. int TicIndex = (TextureHandle >> 0) & 0xfffff;
  182. int TscIndex = (TextureHandle >> 20) & 0xfff;
  183. long TicPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexHeaderPoolOffset);
  184. long TscPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexSamplerPoolOffset);
  185. TicPosition += TicIndex * 0x20;
  186. TscPosition += TscIndex * 0x20;
  187. GalTextureSampler Sampler = TextureFactory.MakeSampler(Gpu, Vmm, TscPosition);
  188. long TextureAddress = Vmm.ReadInt64(TicPosition + 4) & 0xffffffffffff;
  189. TextureAddress = Vmm.GetPhysicalAddress(TextureAddress);
  190. if (IsFrameBufferPosition(TextureAddress))
  191. {
  192. //This texture is a frame buffer texture,
  193. //we shouldn't read anything from memory and bind
  194. //the frame buffer texture instead, since we're not
  195. //really writing anything to memory.
  196. Gpu.Renderer.BindFrameBufferTexture(TextureAddress, TexIndex, Sampler);
  197. }
  198. else
  199. {
  200. GalTexture Texture = TextureFactory.MakeTexture(Gpu, Vmm, TicPosition);
  201. Gpu.Renderer.SetTextureAndSampler(TexIndex, Texture, Sampler);
  202. Gpu.Renderer.BindTexture(TexIndex);
  203. }
  204. }
  205. private void UploadUniforms(NvGpuVmm Vmm)
  206. {
  207. long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  208. for (int Index = 0; Index < 5; Index++)
  209. {
  210. int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + (Index + 1) * 0x10);
  211. int Offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + (Index + 1) * 0x10);
  212. //Note: Vertex Program (B) is always enabled.
  213. bool Enable = (Control & 1) != 0 || Index == 0;
  214. if (!Enable)
  215. {
  216. continue;
  217. }
  218. for (int Cbuf = 0; Cbuf < ConstBuffers.Length; Cbuf++)
  219. {
  220. ConstBuffer Cb = ConstBuffers[Index][Cbuf];
  221. if (Cb.Enabled)
  222. {
  223. byte[] Data = Vmm.ReadBytes(Cb.Position, (uint)Cb.Size);
  224. Gpu.Renderer.SetConstBuffer(BasePosition + (uint)Offset, Cbuf, Data);
  225. }
  226. }
  227. }
  228. }
  229. private void UploadVertexArrays(NvGpuVmm Vmm)
  230. {
  231. long IndexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress);
  232. int IndexSize = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat);
  233. int IndexFirst = ReadRegister(NvGpuEngine3dReg.IndexBatchFirst);
  234. int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount);
  235. GalIndexFormat IndexFormat = (GalIndexFormat)IndexSize;
  236. IndexSize = 1 << IndexSize;
  237. if (IndexSize > 4)
  238. {
  239. throw new InvalidOperationException();
  240. }
  241. if (IndexSize != 0)
  242. {
  243. int BufferSize = IndexCount * IndexSize;
  244. byte[] Data = Vmm.ReadBytes(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. Vmm,
  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. byte[] Data = Vmm.ReadBytes(VertexPosition, Size);
  293. GalVertexAttrib[] AttribArray = Attribs[Index]?.ToArray() ?? new GalVertexAttrib[0];
  294. Gpu.Renderer.SetVertexArray(Index, Stride, Data, AttribArray);
  295. int PrimCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl);
  296. GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff);
  297. if (IndexCount != 0)
  298. {
  299. Gpu.Renderer.DrawElements(Index, IndexFirst, PrimType);
  300. }
  301. else
  302. {
  303. Gpu.Renderer.DrawArrays(Index, VertexFirst, VertexCount, PrimType);
  304. }
  305. }
  306. }
  307. private int GetVertexCountFromIndexBuffer(
  308. NvGpuVmm Vmm,
  309. long IndexPosition,
  310. int IndexCount,
  311. int IndexSize)
  312. {
  313. int MaxIndex = -1;
  314. if (IndexSize == 2)
  315. {
  316. while (IndexCount -- > 0)
  317. {
  318. ushort Value = Vmm.ReadUInt16(IndexPosition);
  319. IndexPosition += 2;
  320. if (MaxIndex < Value)
  321. {
  322. MaxIndex = Value;
  323. }
  324. }
  325. }
  326. else if (IndexSize == 1)
  327. {
  328. while (IndexCount -- > 0)
  329. {
  330. byte Value = Vmm.ReadByte(IndexPosition++);
  331. if (MaxIndex < Value)
  332. {
  333. MaxIndex = Value;
  334. }
  335. }
  336. }
  337. else if (IndexSize == 4)
  338. {
  339. while (IndexCount -- > 0)
  340. {
  341. uint Value = Vmm.ReadUInt32(IndexPosition);
  342. IndexPosition += 2;
  343. if (MaxIndex < Value)
  344. {
  345. MaxIndex = (int)Value;
  346. }
  347. }
  348. }
  349. else
  350. {
  351. throw new ArgumentOutOfRangeException(nameof(IndexSize));
  352. }
  353. return MaxIndex + 1;
  354. }
  355. private void QueryControl(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  356. {
  357. long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.QueryAddress);
  358. int Seq = Registers[(int)NvGpuEngine3dReg.QuerySequence];
  359. int Ctrl = Registers[(int)NvGpuEngine3dReg.QueryControl];
  360. int Mode = Ctrl & 3;
  361. if (Mode == 0)
  362. {
  363. //Write mode.
  364. Vmm.WriteInt32(Position, Seq);
  365. }
  366. WriteRegister(PBEntry);
  367. }
  368. private void CbData(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  369. {
  370. long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress);
  371. int Offset = ReadRegister(NvGpuEngine3dReg.ConstBufferOffset);
  372. foreach (int Arg in PBEntry.Arguments)
  373. {
  374. Vmm.WriteInt32(Position + Offset, Arg);
  375. Offset += 4;
  376. }
  377. WriteRegister(NvGpuEngine3dReg.ConstBufferOffset, Offset);
  378. }
  379. private void CbBind(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  380. {
  381. int Stage = (PBEntry.Method - 0x904) >> 3;
  382. int Index = PBEntry.Arguments[0];
  383. bool Enabled = (Index & 1) != 0;
  384. Index = (Index >> 4) & 0x1f;
  385. long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress);
  386. ConstBuffers[Stage][Index].Position = Position;
  387. ConstBuffers[Stage][Index].Enabled = Enabled;
  388. ConstBuffers[Stage][Index].Size = ReadRegister(NvGpuEngine3dReg.ConstBufferSize);
  389. }
  390. private long MakeInt64From2xInt32(NvGpuEngine3dReg Reg)
  391. {
  392. return
  393. (long)Registers[(int)Reg + 0] << 32 |
  394. (uint)Registers[(int)Reg + 1];
  395. }
  396. private void WriteRegister(NvGpuPBEntry PBEntry)
  397. {
  398. int ArgsCount = PBEntry.Arguments.Count;
  399. if (ArgsCount > 0)
  400. {
  401. Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1];
  402. }
  403. }
  404. private int ReadRegister(NvGpuEngine3dReg Reg)
  405. {
  406. return Registers[(int)Reg];
  407. }
  408. private void WriteRegister(NvGpuEngine3dReg Reg, int Value)
  409. {
  410. Registers[(int)Reg] = Value;
  411. }
  412. public bool IsFrameBufferPosition(long Position)
  413. {
  414. return FrameBuffers.Contains(Position);
  415. }
  416. }
  417. }