NvGpuEngine3d.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. using Ryujinx.Graphics.Gal;
  2. using Ryujinx.HLE.Gpu.Memory;
  3. using Ryujinx.HLE.Gpu.Texture;
  4. using System;
  5. using System.Collections.Generic;
  6. namespace Ryujinx.HLE.Gpu.Engines
  7. {
  8. class NvGpuEngine3d : INvGpuEngine
  9. {
  10. public int[] Registers { get; private set; }
  11. private NvGpu Gpu;
  12. private Dictionary<int, NvGpuMethod> Methods;
  13. private struct ConstBuffer
  14. {
  15. public bool Enabled;
  16. public long Position;
  17. public int Size;
  18. }
  19. private ConstBuffer[][] ConstBuffers;
  20. private HashSet<long> FrameBuffers;
  21. public NvGpuEngine3d(NvGpu Gpu)
  22. {
  23. this.Gpu = Gpu;
  24. Registers = new int[0xe00];
  25. Methods = new Dictionary<int, NvGpuMethod>();
  26. void AddMethod(int Meth, int Count, int Stride, NvGpuMethod Method)
  27. {
  28. while (Count-- > 0)
  29. {
  30. Methods.Add(Meth, Method);
  31. Meth += Stride;
  32. }
  33. }
  34. AddMethod(0x585, 1, 1, VertexEndGl);
  35. AddMethod(0x674, 1, 1, ClearBuffers);
  36. AddMethod(0x6c3, 1, 1, QueryControl);
  37. AddMethod(0x8e4, 16, 1, CbData);
  38. AddMethod(0x904, 5, 8, CbBind);
  39. ConstBuffers = new ConstBuffer[6][];
  40. for (int Index = 0; Index < ConstBuffers.Length; Index++)
  41. {
  42. ConstBuffers[Index] = new ConstBuffer[18];
  43. }
  44. FrameBuffers = new HashSet<long>();
  45. }
  46. public void CallMethod(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  47. {
  48. if (Methods.TryGetValue(PBEntry.Method, out NvGpuMethod Method))
  49. {
  50. Method(Vmm, PBEntry);
  51. }
  52. else
  53. {
  54. WriteRegister(PBEntry);
  55. }
  56. }
  57. private void VertexEndGl(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  58. {
  59. SetFrameBuffer(Vmm, 0);
  60. long[] Keys = UploadShaders(Vmm);
  61. Gpu.Renderer.Shader.BindProgram();
  62. SetAlphaBlending();
  63. UploadTextures(Vmm, Keys);
  64. UploadUniforms(Vmm);
  65. UploadVertexArrays(Vmm);
  66. }
  67. private void ClearBuffers(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  68. {
  69. int Arg0 = PBEntry.Arguments[0];
  70. int FbIndex = (Arg0 >> 6) & 0xf;
  71. int Layer = (Arg0 >> 10) & 0x3ff;
  72. GalClearBufferFlags Flags = (GalClearBufferFlags)(Arg0 & 0x3f);
  73. SetFrameBuffer(Vmm, 0);
  74. //TODO: Enable this once the frame buffer problems are fixed.
  75. //Gpu.Renderer.ClearBuffers(Layer, Flags);
  76. }
  77. private void SetFrameBuffer(NvGpuVmm Vmm, int FbIndex)
  78. {
  79. long VA = MakeInt64From2xInt32(NvGpuEngine3dReg.FrameBufferNAddress + FbIndex * 0x10);
  80. long PA = Vmm.GetPhysicalAddress(VA);
  81. FrameBuffers.Add(PA);
  82. int Width = ReadRegister(NvGpuEngine3dReg.FrameBufferNWidth + FbIndex * 0x10);
  83. int Height = ReadRegister(NvGpuEngine3dReg.FrameBufferNHeight + FbIndex * 0x10);
  84. //Note: Using the Width/Height results seems to give incorrect results.
  85. //Maybe the size of all frame buffers is hardcoded to screen size? This seems unlikely.
  86. Gpu.Renderer.FrameBuffer.Create(PA, 1280, 720);
  87. Gpu.Renderer.FrameBuffer.Bind(PA);
  88. }
  89. private long[] UploadShaders(NvGpuVmm Vmm)
  90. {
  91. long[] Keys = new long[5];
  92. long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  93. for (int Index = 0; Index < 6; Index++)
  94. {
  95. int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + Index * 0x10);
  96. int Offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + Index * 0x10);
  97. //Note: Vertex Program (B) is always enabled.
  98. bool Enable = (Control & 1) != 0 || Index == 1;
  99. if (!Enable)
  100. {
  101. continue;
  102. }
  103. long Key = BasePosition + (uint)Offset;
  104. GalShaderType ShaderType = GetTypeFromProgram(Index);
  105. Keys[(int)ShaderType] = Key;
  106. Gpu.Renderer.Shader.Create(Vmm, Key, ShaderType);
  107. Gpu.Renderer.Shader.Bind(Key);
  108. }
  109. int RawSX = ReadRegister(NvGpuEngine3dReg.ViewportScaleX);
  110. int RawSY = ReadRegister(NvGpuEngine3dReg.ViewportScaleY);
  111. float SX = BitConverter.Int32BitsToSingle(RawSX);
  112. float SY = BitConverter.Int32BitsToSingle(RawSY);
  113. float SignX = MathF.Sign(SX);
  114. float SignY = MathF.Sign(SY);
  115. Gpu.Renderer.Shader.SetFlip(SignX, SignY);
  116. return Keys;
  117. }
  118. private static GalShaderType GetTypeFromProgram(int Program)
  119. {
  120. switch (Program)
  121. {
  122. case 0:
  123. case 1: return GalShaderType.Vertex;
  124. case 2: return GalShaderType.TessControl;
  125. case 3: return GalShaderType.TessEvaluation;
  126. case 4: return GalShaderType.Geometry;
  127. case 5: return GalShaderType.Fragment;
  128. }
  129. throw new ArgumentOutOfRangeException(nameof(Program));
  130. }
  131. private void SetAlphaBlending()
  132. {
  133. //TODO: Support independent blend properly.
  134. bool Enable = (ReadRegister(NvGpuEngine3dReg.IBlendNEnable) & 1) != 0;
  135. if (Enable)
  136. {
  137. Gpu.Renderer.Blend.Enable();
  138. }
  139. else
  140. {
  141. Gpu.Renderer.Blend.Disable();
  142. }
  143. if (!Enable)
  144. {
  145. //If blend is not enabled, then the other values have no effect.
  146. //Note that if it is disabled, the register may contain invalid values.
  147. return;
  148. }
  149. bool BlendSeparateAlpha = (ReadRegister(NvGpuEngine3dReg.IBlendNSeparateAlpha) & 1) != 0;
  150. GalBlendEquation EquationRgb = (GalBlendEquation)ReadRegister(NvGpuEngine3dReg.IBlendNEquationRgb);
  151. GalBlendFactor FuncSrcRgb = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncSrcRgb);
  152. GalBlendFactor FuncDstRgb = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncDstRgb);
  153. if (BlendSeparateAlpha)
  154. {
  155. GalBlendEquation EquationAlpha = (GalBlendEquation)ReadRegister(NvGpuEngine3dReg.IBlendNEquationAlpha);
  156. GalBlendFactor FuncSrcAlpha = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncSrcAlpha);
  157. GalBlendFactor FuncDstAlpha = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncDstAlpha);
  158. Gpu.Renderer.Blend.SetSeparate(
  159. EquationRgb,
  160. EquationAlpha,
  161. FuncSrcRgb,
  162. FuncDstRgb,
  163. FuncSrcAlpha,
  164. FuncDstAlpha);
  165. }
  166. else
  167. {
  168. Gpu.Renderer.Blend.Set(EquationRgb, FuncSrcRgb, FuncDstRgb);
  169. }
  170. }
  171. private void UploadTextures(NvGpuVmm Vmm, long[] Keys)
  172. {
  173. long BaseShPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  174. int TextureCbIndex = ReadRegister(NvGpuEngine3dReg.TextureCbIndex);
  175. //Note: On the emulator renderer, Texture Unit 0 is
  176. //reserved for drawing the frame buffer.
  177. int TexIndex = 1;
  178. for (int Index = 0; Index < Keys.Length; Index++)
  179. {
  180. foreach (ShaderDeclInfo DeclInfo in Gpu.Renderer.Shader.GetTextureUsage(Keys[Index]))
  181. {
  182. long Position = ConstBuffers[Index][TextureCbIndex].Position;
  183. UploadTexture(Vmm, Position, TexIndex, DeclInfo.Index);
  184. Gpu.Renderer.Shader.EnsureTextureBinding(DeclInfo.Name, TexIndex);
  185. TexIndex++;
  186. }
  187. }
  188. }
  189. private void UploadTexture(NvGpuVmm Vmm, long BasePosition, int TexIndex, int HndIndex)
  190. {
  191. long Position = BasePosition + HndIndex * 4;
  192. int TextureHandle = Vmm.ReadInt32(Position);
  193. if (TextureHandle == 0)
  194. {
  195. //TODO: Is this correct?
  196. //Some games like puyo puyo will have 0 handles.
  197. //It may be just normal behaviour or a bug caused by sync issues.
  198. //The game does initialize the value properly after through.
  199. return;
  200. }
  201. int TicIndex = (TextureHandle >> 0) & 0xfffff;
  202. int TscIndex = (TextureHandle >> 20) & 0xfff;
  203. long TicPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexHeaderPoolOffset);
  204. long TscPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexSamplerPoolOffset);
  205. TicPosition += TicIndex * 0x20;
  206. TscPosition += TscIndex * 0x20;
  207. GalTextureSampler Sampler = TextureFactory.MakeSampler(Gpu, Vmm, TscPosition);
  208. long TextureAddress = Vmm.ReadInt64(TicPosition + 4) & 0xffffffffffff;
  209. long Key = TextureAddress;
  210. TextureAddress = Vmm.GetPhysicalAddress(TextureAddress);
  211. if (IsFrameBufferPosition(TextureAddress))
  212. {
  213. //This texture is a frame buffer texture,
  214. //we shouldn't read anything from memory and bind
  215. //the frame buffer texture instead, since we're not
  216. //really writing anything to memory.
  217. Gpu.Renderer.FrameBuffer.BindTexture(TextureAddress, TexIndex);
  218. }
  219. else
  220. {
  221. GalTexture NewTexture = TextureFactory.MakeTexture(Vmm, TicPosition);
  222. long Size = (uint)TextureHelper.GetTextureSize(NewTexture);
  223. bool HasCachedTexture = false;
  224. if (Gpu.Renderer.Texture.TryGetCachedTexture(Key, Size, out GalTexture Texture))
  225. {
  226. if (NewTexture.Equals(Texture) && !Vmm.IsRegionModified(Key, Size, NvGpuBufferType.Texture))
  227. {
  228. Gpu.Renderer.Texture.Bind(Key, TexIndex);
  229. HasCachedTexture = true;
  230. }
  231. }
  232. if (!HasCachedTexture)
  233. {
  234. byte[] Data = TextureFactory.GetTextureData(Vmm, TicPosition);
  235. Gpu.Renderer.Texture.Create(Key, Data, NewTexture);
  236. }
  237. Gpu.Renderer.Texture.Bind(Key, TexIndex);
  238. }
  239. Gpu.Renderer.Texture.SetSampler(Sampler);
  240. }
  241. private void UploadUniforms(NvGpuVmm Vmm)
  242. {
  243. long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  244. for (int Index = 0; Index < 5; Index++)
  245. {
  246. int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + (Index + 1) * 0x10);
  247. int Offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + (Index + 1) * 0x10);
  248. //Note: Vertex Program (B) is always enabled.
  249. bool Enable = (Control & 1) != 0 || Index == 0;
  250. if (!Enable)
  251. {
  252. continue;
  253. }
  254. for (int Cbuf = 0; Cbuf < ConstBuffers[Index].Length; Cbuf++)
  255. {
  256. ConstBuffer Cb = ConstBuffers[Index][Cbuf];
  257. if (Cb.Enabled)
  258. {
  259. byte[] Data = Vmm.ReadBytes(Cb.Position, (uint)Cb.Size);
  260. Gpu.Renderer.Shader.SetConstBuffer(BasePosition + (uint)Offset, Cbuf, Data);
  261. }
  262. }
  263. }
  264. }
  265. private void UploadVertexArrays(NvGpuVmm Vmm)
  266. {
  267. long IndexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress);
  268. int IndexEntryFmt = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat);
  269. int IndexFirst = ReadRegister(NvGpuEngine3dReg.IndexBatchFirst);
  270. int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount);
  271. GalIndexFormat IndexFormat = (GalIndexFormat)IndexEntryFmt;
  272. int IndexEntrySize = 1 << IndexEntryFmt;
  273. if (IndexEntrySize > 4)
  274. {
  275. throw new InvalidOperationException();
  276. }
  277. if (IndexCount != 0)
  278. {
  279. int IbSize = IndexCount * IndexEntrySize;
  280. bool IboCached = Gpu.Renderer.Rasterizer.IsIboCached(IndexPosition, (uint)IbSize);
  281. if (!IboCached || Vmm.IsRegionModified(IndexPosition, (uint)IbSize, NvGpuBufferType.Index))
  282. {
  283. byte[] Data = Vmm.ReadBytes(IndexPosition, (uint)IbSize);
  284. Gpu.Renderer.Rasterizer.CreateIbo(IndexPosition, Data);
  285. }
  286. Gpu.Renderer.Rasterizer.SetIndexArray(IndexPosition, IbSize, IndexFormat);
  287. }
  288. List<GalVertexAttrib>[] Attribs = new List<GalVertexAttrib>[32];
  289. for (int Attr = 0; Attr < 16; Attr++)
  290. {
  291. int Packed = ReadRegister(NvGpuEngine3dReg.VertexAttribNFormat + Attr);
  292. int ArrayIndex = Packed & 0x1f;
  293. if (Attribs[ArrayIndex] == null)
  294. {
  295. Attribs[ArrayIndex] = new List<GalVertexAttrib>();
  296. }
  297. Attribs[ArrayIndex].Add(new GalVertexAttrib(
  298. Attr,
  299. ((Packed >> 6) & 0x1) != 0,
  300. (Packed >> 7) & 0x3fff,
  301. (GalVertexAttribSize)((Packed >> 21) & 0x3f),
  302. (GalVertexAttribType)((Packed >> 27) & 0x7),
  303. ((Packed >> 31) & 0x1) != 0));
  304. }
  305. int VertexFirst = ReadRegister(NvGpuEngine3dReg.VertexArrayFirst);
  306. int VertexCount = ReadRegister(NvGpuEngine3dReg.VertexArrayCount);
  307. int PrimCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl);
  308. for (int Index = 0; Index < 32; Index++)
  309. {
  310. if (Attribs[Index] == null)
  311. {
  312. continue;
  313. }
  314. int Control = ReadRegister(NvGpuEngine3dReg.VertexArrayNControl + Index * 4);
  315. bool Enable = (Control & 0x1000) != 0;
  316. long VertexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + Index * 4);
  317. long VertexEndPos = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNEndAddr + Index * 2);
  318. if (!Enable)
  319. {
  320. continue;
  321. }
  322. int Stride = Control & 0xfff;
  323. long VbSize = 0;
  324. if (IndexCount != 0)
  325. {
  326. VbSize = (VertexEndPos - VertexPosition) + 1;
  327. }
  328. else
  329. {
  330. VbSize = VertexCount * Stride;
  331. }
  332. bool VboCached = Gpu.Renderer.Rasterizer.IsVboCached(VertexPosition, VbSize);
  333. if (!VboCached || Vmm.IsRegionModified(VertexPosition, VbSize, NvGpuBufferType.Vertex))
  334. {
  335. byte[] Data = Vmm.ReadBytes(VertexPosition, VbSize);
  336. Gpu.Renderer.Rasterizer.CreateVbo(VertexPosition, Data);
  337. }
  338. Gpu.Renderer.Rasterizer.SetVertexArray(Index, Stride, VertexPosition, Attribs[Index].ToArray());
  339. }
  340. GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff);
  341. if (IndexCount != 0)
  342. {
  343. Gpu.Renderer.Rasterizer.DrawElements(IndexPosition, IndexFirst, PrimType);
  344. }
  345. else
  346. {
  347. Gpu.Renderer.Rasterizer.DrawArrays(VertexFirst, VertexCount, PrimType);
  348. }
  349. }
  350. private void QueryControl(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  351. {
  352. long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.QueryAddress);
  353. int Seq = Registers[(int)NvGpuEngine3dReg.QuerySequence];
  354. int Ctrl = Registers[(int)NvGpuEngine3dReg.QueryControl];
  355. int Mode = Ctrl & 3;
  356. if (Mode == 0)
  357. {
  358. //Write mode.
  359. Vmm.WriteInt32(Position, Seq);
  360. }
  361. WriteRegister(PBEntry);
  362. }
  363. private void CbData(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  364. {
  365. long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress);
  366. int Offset = ReadRegister(NvGpuEngine3dReg.ConstBufferOffset);
  367. foreach (int Arg in PBEntry.Arguments)
  368. {
  369. Vmm.WriteInt32(Position + Offset, Arg);
  370. Offset += 4;
  371. }
  372. WriteRegister(NvGpuEngine3dReg.ConstBufferOffset, Offset);
  373. }
  374. private void CbBind(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  375. {
  376. int Stage = (PBEntry.Method - 0x904) >> 3;
  377. int Index = PBEntry.Arguments[0];
  378. bool Enabled = (Index & 1) != 0;
  379. Index = (Index >> 4) & 0x1f;
  380. long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress);
  381. ConstBuffers[Stage][Index].Position = Position;
  382. ConstBuffers[Stage][Index].Enabled = Enabled;
  383. ConstBuffers[Stage][Index].Size = ReadRegister(NvGpuEngine3dReg.ConstBufferSize);
  384. }
  385. private long MakeInt64From2xInt32(NvGpuEngine3dReg Reg)
  386. {
  387. return
  388. (long)Registers[(int)Reg + 0] << 32 |
  389. (uint)Registers[(int)Reg + 1];
  390. }
  391. private void WriteRegister(NvGpuPBEntry PBEntry)
  392. {
  393. int ArgsCount = PBEntry.Arguments.Count;
  394. if (ArgsCount > 0)
  395. {
  396. Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1];
  397. }
  398. }
  399. private int ReadRegister(NvGpuEngine3dReg Reg)
  400. {
  401. return Registers[(int)Reg];
  402. }
  403. private void WriteRegister(NvGpuEngine3dReg Reg, int Value)
  404. {
  405. Registers[(int)Reg] = Value;
  406. }
  407. public bool IsFrameBufferPosition(long Position)
  408. {
  409. return FrameBuffers.Contains(Position);
  410. }
  411. }
  412. }