NvGpuEngine3d.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. 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. TextureAddress = Vmm.GetPhysicalAddress(TextureAddress);
  193. if (IsFrameBufferPosition(TextureAddress))
  194. {
  195. //This texture is a frame buffer texture,
  196. //we shouldn't read anything from memory and bind
  197. //the frame buffer texture instead, since we're not
  198. //really writing anything to memory.
  199. Gpu.Renderer.BindFrameBufferTexture(TextureAddress, TexIndex, Sampler);
  200. }
  201. else
  202. {
  203. GalTexture Texture = TextureFactory.MakeTexture(Gpu, Vmm, TicPosition);
  204. Gpu.Renderer.SetTextureAndSampler(TexIndex, Texture, Sampler);
  205. Gpu.Renderer.BindTexture(TexIndex);
  206. }
  207. }
  208. private void UploadUniforms(NvGpuVmm Vmm)
  209. {
  210. long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  211. for (int Index = 0; Index < 5; Index++)
  212. {
  213. int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + (Index + 1) * 0x10);
  214. int Offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + (Index + 1) * 0x10);
  215. //Note: Vertex Program (B) is always enabled.
  216. bool Enable = (Control & 1) != 0 || Index == 0;
  217. if (!Enable)
  218. {
  219. continue;
  220. }
  221. for (int Cbuf = 0; Cbuf < ConstBuffers.Length; Cbuf++)
  222. {
  223. ConstBuffer Cb = ConstBuffers[Index][Cbuf];
  224. if (Cb.Enabled)
  225. {
  226. byte[] Data = Vmm.ReadBytes(Cb.Position, (uint)Cb.Size);
  227. Gpu.Renderer.SetConstBuffer(BasePosition + (uint)Offset, Cbuf, Data);
  228. }
  229. }
  230. }
  231. }
  232. private void UploadVertexArrays(NvGpuVmm Vmm)
  233. {
  234. long IndexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress);
  235. int IndexSize = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat);
  236. int IndexFirst = ReadRegister(NvGpuEngine3dReg.IndexBatchFirst);
  237. int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount);
  238. GalIndexFormat IndexFormat = (GalIndexFormat)IndexSize;
  239. IndexSize = 1 << IndexSize;
  240. if (IndexSize > 4)
  241. {
  242. throw new InvalidOperationException();
  243. }
  244. if (IndexSize != 0)
  245. {
  246. int BufferSize = IndexCount * IndexSize;
  247. byte[] Data = Vmm.ReadBytes(IndexPosition, BufferSize);
  248. Gpu.Renderer.SetIndexArray(Data, IndexFormat);
  249. }
  250. List<GalVertexAttrib>[] Attribs = new List<GalVertexAttrib>[32];
  251. for (int Attr = 0; Attr < 16; Attr++)
  252. {
  253. int Packed = ReadRegister(NvGpuEngine3dReg.VertexAttribNFormat + Attr);
  254. int ArrayIndex = Packed & 0x1f;
  255. if (Attribs[ArrayIndex] == null)
  256. {
  257. Attribs[ArrayIndex] = new List<GalVertexAttrib>();
  258. }
  259. Attribs[ArrayIndex].Add(new GalVertexAttrib(
  260. Attr,
  261. ((Packed >> 6) & 0x1) != 0,
  262. (Packed >> 7) & 0x3fff,
  263. (GalVertexAttribSize)((Packed >> 21) & 0x3f),
  264. (GalVertexAttribType)((Packed >> 27) & 0x7),
  265. ((Packed >> 31) & 0x1) != 0));
  266. }
  267. for (int Index = 0; Index < 32; Index++)
  268. {
  269. int VertexFirst = ReadRegister(NvGpuEngine3dReg.VertexArrayFirst);
  270. int VertexCount = ReadRegister(NvGpuEngine3dReg.VertexArrayCount);
  271. int Control = ReadRegister(NvGpuEngine3dReg.VertexArrayNControl + Index * 4);
  272. bool Enable = (Control & 0x1000) != 0;
  273. long VertexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + Index * 4);
  274. long VertexEndPos = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNEndAddr + Index * 2);
  275. if (!Enable)
  276. {
  277. continue;
  278. }
  279. int Stride = Control & 0xfff;
  280. long Size = 0;
  281. if (IndexCount != 0)
  282. {
  283. Size = (VertexEndPos - VertexPosition) + 1;
  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 void QueryControl(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  308. {
  309. long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.QueryAddress);
  310. int Seq = Registers[(int)NvGpuEngine3dReg.QuerySequence];
  311. int Ctrl = Registers[(int)NvGpuEngine3dReg.QueryControl];
  312. int Mode = Ctrl & 3;
  313. if (Mode == 0)
  314. {
  315. //Write mode.
  316. Vmm.WriteInt32(Position, Seq);
  317. }
  318. WriteRegister(PBEntry);
  319. }
  320. private void CbData(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  321. {
  322. long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress);
  323. int Offset = ReadRegister(NvGpuEngine3dReg.ConstBufferOffset);
  324. foreach (int Arg in PBEntry.Arguments)
  325. {
  326. Vmm.WriteInt32(Position + Offset, Arg);
  327. Offset += 4;
  328. }
  329. WriteRegister(NvGpuEngine3dReg.ConstBufferOffset, Offset);
  330. }
  331. private void CbBind(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  332. {
  333. int Stage = (PBEntry.Method - 0x904) >> 3;
  334. int Index = PBEntry.Arguments[0];
  335. bool Enabled = (Index & 1) != 0;
  336. Index = (Index >> 4) & 0x1f;
  337. long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress);
  338. ConstBuffers[Stage][Index].Position = Position;
  339. ConstBuffers[Stage][Index].Enabled = Enabled;
  340. ConstBuffers[Stage][Index].Size = ReadRegister(NvGpuEngine3dReg.ConstBufferSize);
  341. }
  342. private long MakeInt64From2xInt32(NvGpuEngine3dReg Reg)
  343. {
  344. return
  345. (long)Registers[(int)Reg + 0] << 32 |
  346. (uint)Registers[(int)Reg + 1];
  347. }
  348. private void WriteRegister(NvGpuPBEntry PBEntry)
  349. {
  350. int ArgsCount = PBEntry.Arguments.Count;
  351. if (ArgsCount > 0)
  352. {
  353. Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1];
  354. }
  355. }
  356. private int ReadRegister(NvGpuEngine3dReg Reg)
  357. {
  358. return Registers[(int)Reg];
  359. }
  360. private void WriteRegister(NvGpuEngine3dReg Reg, int Value)
  361. {
  362. Registers[(int)Reg] = Value;
  363. }
  364. public bool IsFrameBufferPosition(long Position)
  365. {
  366. return FrameBuffers.Contains(Position);
  367. }
  368. }
  369. }