NvGpuEngine3d.cs 17 KB

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