NvGpuEngine3d.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. SetCullFace();
  63. SetDepth();
  64. SetAlphaBlending();
  65. UploadTextures(Vmm, Keys);
  66. UploadUniforms(Vmm);
  67. UploadVertexArrays(Vmm);
  68. }
  69. private void ClearBuffers(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  70. {
  71. int Arg0 = PBEntry.Arguments[0];
  72. int FbIndex = (Arg0 >> 6) & 0xf;
  73. GalClearBufferFlags Flags = (GalClearBufferFlags)(Arg0 & 0x3f);
  74. SetFrameBuffer(Vmm, FbIndex);
  75. Gpu.Renderer.Rasterizer.ClearBuffers(Flags);
  76. }
  77. private void SetFrameBuffer(NvGpuVmm Vmm, int FbIndex)
  78. {
  79. long VA = MakeInt64From2xInt32(NvGpuEngine3dReg.FrameBufferNAddress + FbIndex * 0x10);
  80. long Key = Vmm.GetPhysicalAddress(VA);
  81. FrameBuffers.Add(Key);
  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(Key, 1280, 720);
  87. Gpu.Renderer.FrameBuffer.Bind(Key);
  88. }
  89. private long[] UploadShaders(NvGpuVmm Vmm)
  90. {
  91. long[] Keys = new long[5];
  92. long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  93. int Index = 1;
  94. int VpAControl = ReadRegister(NvGpuEngine3dReg.ShaderNControl);
  95. bool VpAEnable = (VpAControl & 1) != 0;
  96. if (VpAEnable)
  97. {
  98. //Note: The maxwell supports 2 vertex programs, usually
  99. //only VP B is used, but in some cases VP A is also used.
  100. //In this case, it seems to function as an extra vertex
  101. //shader stage.
  102. //The graphics abstraction layer has a special overload for this
  103. //case, which should merge the two shaders into one vertex shader.
  104. int VpAOffset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset);
  105. int VpBOffset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + 0x10);
  106. long VpAPos = BasePosition + (uint)VpAOffset;
  107. long VpBPos = BasePosition + (uint)VpBOffset;
  108. Gpu.Renderer.Shader.Create(Vmm, VpAPos, VpBPos, GalShaderType.Vertex);
  109. Gpu.Renderer.Shader.Bind(VpBPos);
  110. Index = 2;
  111. }
  112. for (; Index < 6; Index++)
  113. {
  114. int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + Index * 0x10);
  115. int Offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + Index * 0x10);
  116. //Note: Vertex Program (B) is always enabled.
  117. bool Enable = (Control & 1) != 0 || Index == 1;
  118. if (!Enable)
  119. {
  120. continue;
  121. }
  122. long Key = BasePosition + (uint)Offset;
  123. GalShaderType ShaderType = GetTypeFromProgram(Index);
  124. Keys[(int)ShaderType] = Key;
  125. Gpu.Renderer.Shader.Create(Vmm, Key, ShaderType);
  126. Gpu.Renderer.Shader.Bind(Key);
  127. }
  128. int RawSX = ReadRegister(NvGpuEngine3dReg.ViewportScaleX);
  129. int RawSY = ReadRegister(NvGpuEngine3dReg.ViewportScaleY);
  130. float SX = BitConverter.Int32BitsToSingle(RawSX);
  131. float SY = BitConverter.Int32BitsToSingle(RawSY);
  132. float SignX = MathF.Sign(SX);
  133. float SignY = MathF.Sign(SY);
  134. Gpu.Renderer.Shader.SetFlip(SignX, SignY);
  135. return Keys;
  136. }
  137. private static GalShaderType GetTypeFromProgram(int Program)
  138. {
  139. switch (Program)
  140. {
  141. case 0:
  142. case 1: return GalShaderType.Vertex;
  143. case 2: return GalShaderType.TessControl;
  144. case 3: return GalShaderType.TessEvaluation;
  145. case 4: return GalShaderType.Geometry;
  146. case 5: return GalShaderType.Fragment;
  147. }
  148. throw new ArgumentOutOfRangeException(nameof(Program));
  149. }
  150. private void SetCullFace()
  151. {
  152. //TODO.
  153. }
  154. private void SetDepth()
  155. {
  156. //TODO.
  157. }
  158. private void SetAlphaBlending()
  159. {
  160. //TODO: Support independent blend properly.
  161. bool Enable = (ReadRegister(NvGpuEngine3dReg.IBlendNEnable) & 1) != 0;
  162. if (Enable)
  163. {
  164. Gpu.Renderer.Blend.Enable();
  165. }
  166. else
  167. {
  168. Gpu.Renderer.Blend.Disable();
  169. }
  170. if (!Enable)
  171. {
  172. //If blend is not enabled, then the other values have no effect.
  173. //Note that if it is disabled, the register may contain invalid values.
  174. return;
  175. }
  176. bool BlendSeparateAlpha = (ReadRegister(NvGpuEngine3dReg.IBlendNSeparateAlpha) & 1) != 0;
  177. GalBlendEquation EquationRgb = (GalBlendEquation)ReadRegister(NvGpuEngine3dReg.IBlendNEquationRgb);
  178. GalBlendFactor FuncSrcRgb = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncSrcRgb);
  179. GalBlendFactor FuncDstRgb = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncDstRgb);
  180. if (BlendSeparateAlpha)
  181. {
  182. GalBlendEquation EquationAlpha = (GalBlendEquation)ReadRegister(NvGpuEngine3dReg.IBlendNEquationAlpha);
  183. GalBlendFactor FuncSrcAlpha = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncSrcAlpha);
  184. GalBlendFactor FuncDstAlpha = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncDstAlpha);
  185. Gpu.Renderer.Blend.SetSeparate(
  186. EquationRgb,
  187. EquationAlpha,
  188. FuncSrcRgb,
  189. FuncDstRgb,
  190. FuncSrcAlpha,
  191. FuncDstAlpha);
  192. }
  193. else
  194. {
  195. Gpu.Renderer.Blend.Set(EquationRgb, FuncSrcRgb, FuncDstRgb);
  196. }
  197. }
  198. private void UploadTextures(NvGpuVmm Vmm, long[] Keys)
  199. {
  200. long BaseShPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  201. int TextureCbIndex = ReadRegister(NvGpuEngine3dReg.TextureCbIndex);
  202. //Note: On the emulator renderer, Texture Unit 0 is
  203. //reserved for drawing the frame buffer.
  204. int TexIndex = 1;
  205. for (int Index = 0; Index < Keys.Length; Index++)
  206. {
  207. foreach (ShaderDeclInfo DeclInfo in Gpu.Renderer.Shader.GetTextureUsage(Keys[Index]))
  208. {
  209. long Position = ConstBuffers[Index][TextureCbIndex].Position;
  210. UploadTexture(Vmm, Position, TexIndex, DeclInfo.Index);
  211. Gpu.Renderer.Shader.EnsureTextureBinding(DeclInfo.Name, TexIndex);
  212. TexIndex++;
  213. }
  214. }
  215. }
  216. private void UploadTexture(NvGpuVmm Vmm, long BasePosition, int TexIndex, int HndIndex)
  217. {
  218. long Position = BasePosition + HndIndex * 4;
  219. int TextureHandle = Vmm.ReadInt32(Position);
  220. if (TextureHandle == 0)
  221. {
  222. //TODO: Is this correct?
  223. //Some games like puyo puyo will have 0 handles.
  224. //It may be just normal behaviour or a bug caused by sync issues.
  225. //The game does initialize the value properly after through.
  226. return;
  227. }
  228. int TicIndex = (TextureHandle >> 0) & 0xfffff;
  229. int TscIndex = (TextureHandle >> 20) & 0xfff;
  230. long TicPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexHeaderPoolOffset);
  231. long TscPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexSamplerPoolOffset);
  232. TicPosition += TicIndex * 0x20;
  233. TscPosition += TscIndex * 0x20;
  234. GalTextureSampler Sampler = TextureFactory.MakeSampler(Gpu, Vmm, TscPosition);
  235. long TextureAddress = Vmm.ReadInt64(TicPosition + 4) & 0xffffffffffff;
  236. long Key = TextureAddress;
  237. TextureAddress = Vmm.GetPhysicalAddress(TextureAddress);
  238. if (IsFrameBufferPosition(TextureAddress))
  239. {
  240. //This texture is a frame buffer texture,
  241. //we shouldn't read anything from memory and bind
  242. //the frame buffer texture instead, since we're not
  243. //really writing anything to memory.
  244. Gpu.Renderer.FrameBuffer.BindTexture(TextureAddress, TexIndex);
  245. }
  246. else
  247. {
  248. GalTexture NewTexture = TextureFactory.MakeTexture(Vmm, TicPosition);
  249. long Size = (uint)TextureHelper.GetTextureSize(NewTexture);
  250. bool HasCachedTexture = false;
  251. if (Gpu.Renderer.Texture.TryGetCachedTexture(Key, Size, out GalTexture Texture))
  252. {
  253. if (NewTexture.Equals(Texture) && !Vmm.IsRegionModified(Key, Size, NvGpuBufferType.Texture))
  254. {
  255. Gpu.Renderer.Texture.Bind(Key, TexIndex);
  256. HasCachedTexture = true;
  257. }
  258. }
  259. if (!HasCachedTexture)
  260. {
  261. byte[] Data = TextureFactory.GetTextureData(Vmm, TicPosition);
  262. Gpu.Renderer.Texture.Create(Key, Data, NewTexture);
  263. }
  264. Gpu.Renderer.Texture.Bind(Key, TexIndex);
  265. }
  266. Gpu.Renderer.Texture.SetSampler(Sampler);
  267. }
  268. private void UploadUniforms(NvGpuVmm Vmm)
  269. {
  270. long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  271. for (int Index = 0; Index < 5; Index++)
  272. {
  273. int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + (Index + 1) * 0x10);
  274. int Offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + (Index + 1) * 0x10);
  275. //Note: Vertex Program (B) is always enabled.
  276. bool Enable = (Control & 1) != 0 || Index == 0;
  277. if (!Enable)
  278. {
  279. continue;
  280. }
  281. for (int Cbuf = 0; Cbuf < ConstBuffers[Index].Length; Cbuf++)
  282. {
  283. ConstBuffer Cb = ConstBuffers[Index][Cbuf];
  284. if (Cb.Enabled)
  285. {
  286. byte[] Data = Vmm.ReadBytes(Cb.Position, (uint)Cb.Size);
  287. Gpu.Renderer.Shader.SetConstBuffer(BasePosition + (uint)Offset, Cbuf, Data);
  288. }
  289. }
  290. }
  291. }
  292. private void UploadVertexArrays(NvGpuVmm Vmm)
  293. {
  294. long IndexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress);
  295. int IndexEntryFmt = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat);
  296. int IndexFirst = ReadRegister(NvGpuEngine3dReg.IndexBatchFirst);
  297. int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount);
  298. GalIndexFormat IndexFormat = (GalIndexFormat)IndexEntryFmt;
  299. int IndexEntrySize = 1 << IndexEntryFmt;
  300. if (IndexEntrySize > 4)
  301. {
  302. throw new InvalidOperationException();
  303. }
  304. if (IndexCount != 0)
  305. {
  306. int IbSize = IndexCount * IndexEntrySize;
  307. bool IboCached = Gpu.Renderer.Rasterizer.IsIboCached(IndexPosition, (uint)IbSize);
  308. if (!IboCached || Vmm.IsRegionModified(IndexPosition, (uint)IbSize, NvGpuBufferType.Index))
  309. {
  310. byte[] Data = Vmm.ReadBytes(IndexPosition, (uint)IbSize);
  311. Gpu.Renderer.Rasterizer.CreateIbo(IndexPosition, Data);
  312. }
  313. Gpu.Renderer.Rasterizer.SetIndexArray(IndexPosition, IbSize, IndexFormat);
  314. }
  315. List<GalVertexAttrib>[] Attribs = new List<GalVertexAttrib>[32];
  316. for (int Attr = 0; Attr < 16; Attr++)
  317. {
  318. int Packed = ReadRegister(NvGpuEngine3dReg.VertexAttribNFormat + Attr);
  319. int ArrayIndex = Packed & 0x1f;
  320. if (Attribs[ArrayIndex] == null)
  321. {
  322. Attribs[ArrayIndex] = new List<GalVertexAttrib>();
  323. }
  324. Attribs[ArrayIndex].Add(new GalVertexAttrib(
  325. Attr,
  326. ((Packed >> 6) & 0x1) != 0,
  327. (Packed >> 7) & 0x3fff,
  328. (GalVertexAttribSize)((Packed >> 21) & 0x3f),
  329. (GalVertexAttribType)((Packed >> 27) & 0x7),
  330. ((Packed >> 31) & 0x1) != 0));
  331. }
  332. int VertexFirst = ReadRegister(NvGpuEngine3dReg.VertexArrayFirst);
  333. int VertexCount = ReadRegister(NvGpuEngine3dReg.VertexArrayCount);
  334. int PrimCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl);
  335. for (int Index = 0; Index < 32; Index++)
  336. {
  337. if (Attribs[Index] == null)
  338. {
  339. continue;
  340. }
  341. int Control = ReadRegister(NvGpuEngine3dReg.VertexArrayNControl + Index * 4);
  342. bool Enable = (Control & 0x1000) != 0;
  343. long VertexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + Index * 4);
  344. long VertexEndPos = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNEndAddr + Index * 2);
  345. if (!Enable)
  346. {
  347. continue;
  348. }
  349. int Stride = Control & 0xfff;
  350. long VbSize = (VertexEndPos - VertexPosition) + 1;
  351. bool VboCached = Gpu.Renderer.Rasterizer.IsVboCached(VertexPosition, VbSize);
  352. if (!VboCached || Vmm.IsRegionModified(VertexPosition, VbSize, NvGpuBufferType.Vertex))
  353. {
  354. byte[] Data = Vmm.ReadBytes(VertexPosition, VbSize);
  355. Gpu.Renderer.Rasterizer.CreateVbo(VertexPosition, Data);
  356. }
  357. Gpu.Renderer.Rasterizer.SetVertexArray(Index, Stride, VertexPosition, Attribs[Index].ToArray());
  358. }
  359. GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff);
  360. if (IndexCount != 0)
  361. {
  362. int VertexBase = ReadRegister(NvGpuEngine3dReg.VertexArrayElemBase);
  363. Gpu.Renderer.Rasterizer.DrawElements(IndexPosition, IndexFirst, VertexBase, PrimType);
  364. }
  365. else
  366. {
  367. Gpu.Renderer.Rasterizer.DrawArrays(VertexFirst, VertexCount, PrimType);
  368. }
  369. }
  370. private void QueryControl(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  371. {
  372. long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.QueryAddress);
  373. int Seq = Registers[(int)NvGpuEngine3dReg.QuerySequence];
  374. int Ctrl = Registers[(int)NvGpuEngine3dReg.QueryControl];
  375. int Mode = Ctrl & 3;
  376. if (Mode == 0)
  377. {
  378. //Write mode.
  379. Vmm.WriteInt32(Position, Seq);
  380. }
  381. WriteRegister(PBEntry);
  382. }
  383. private void CbData(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  384. {
  385. long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress);
  386. int Offset = ReadRegister(NvGpuEngine3dReg.ConstBufferOffset);
  387. foreach (int Arg in PBEntry.Arguments)
  388. {
  389. Vmm.WriteInt32(Position + Offset, Arg);
  390. Offset += 4;
  391. }
  392. WriteRegister(NvGpuEngine3dReg.ConstBufferOffset, Offset);
  393. }
  394. private void CbBind(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  395. {
  396. int Stage = (PBEntry.Method - 0x904) >> 3;
  397. int Index = PBEntry.Arguments[0];
  398. bool Enabled = (Index & 1) != 0;
  399. Index = (Index >> 4) & 0x1f;
  400. long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress);
  401. ConstBuffers[Stage][Index].Position = Position;
  402. ConstBuffers[Stage][Index].Enabled = Enabled;
  403. ConstBuffers[Stage][Index].Size = ReadRegister(NvGpuEngine3dReg.ConstBufferSize);
  404. }
  405. private long MakeInt64From2xInt32(NvGpuEngine3dReg Reg)
  406. {
  407. return
  408. (long)Registers[(int)Reg + 0] << 32 |
  409. (uint)Registers[(int)Reg + 1];
  410. }
  411. private void WriteRegister(NvGpuPBEntry PBEntry)
  412. {
  413. int ArgsCount = PBEntry.Arguments.Count;
  414. if (ArgsCount > 0)
  415. {
  416. Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1];
  417. }
  418. }
  419. private int ReadRegister(NvGpuEngine3dReg Reg)
  420. {
  421. return Registers[(int)Reg];
  422. }
  423. private void WriteRegister(NvGpuEngine3dReg Reg, int Value)
  424. {
  425. Registers[(int)Reg] = Value;
  426. }
  427. public bool IsFrameBufferPosition(long Position)
  428. {
  429. return FrameBuffers.Contains(Position);
  430. }
  431. }
  432. }