NvGpuEngine3d.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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. LockCaches();
  60. SetFrameBuffer(Vmm, 0);
  61. long[] Keys = UploadShaders(Vmm);
  62. Gpu.Renderer.Shader.BindProgram();
  63. //Note: Uncomment SetFrontFace SetCullFace when flipping issues are solved
  64. //SetFrontFace();
  65. //SetCullFace();
  66. SetDepth();
  67. SetStencil();
  68. SetAlphaBlending();
  69. SetPrimitiveRestart();
  70. UploadTextures(Vmm, Keys);
  71. UploadUniforms(Vmm);
  72. UploadVertexArrays(Vmm);
  73. UnlockCaches();
  74. }
  75. private void LockCaches()
  76. {
  77. Gpu.Renderer.Rasterizer.LockCaches();
  78. Gpu.Renderer.Texture.LockCache();
  79. }
  80. private void UnlockCaches()
  81. {
  82. Gpu.Renderer.Rasterizer.UnlockCaches();
  83. Gpu.Renderer.Texture.UnlockCache();
  84. }
  85. private void ClearBuffers(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  86. {
  87. int Arg0 = PBEntry.Arguments[0];
  88. int FbIndex = (Arg0 >> 6) & 0xf;
  89. GalClearBufferFlags Flags = (GalClearBufferFlags)(Arg0 & 0x3f);
  90. SetFrameBuffer(Vmm, FbIndex);
  91. Gpu.Renderer.Rasterizer.ClearBuffers(Flags);
  92. }
  93. private void SetFrameBuffer(NvGpuVmm Vmm, int FbIndex)
  94. {
  95. long VA = MakeInt64From2xInt32(NvGpuEngine3dReg.FrameBufferNAddress + FbIndex * 0x10);
  96. long Key = Vmm.GetPhysicalAddress(VA);
  97. FrameBuffers.Add(Key);
  98. int Width = ReadRegister(NvGpuEngine3dReg.FrameBufferNWidth + FbIndex * 0x10);
  99. int Height = ReadRegister(NvGpuEngine3dReg.FrameBufferNHeight + FbIndex * 0x10);
  100. float TX = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNTranslateX + FbIndex * 4);
  101. float TY = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNTranslateY + FbIndex * 4);
  102. float SX = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNScaleX + FbIndex * 4);
  103. float SY = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNScaleY + FbIndex * 4);
  104. int VpX = (int)MathF.Max(0, TX - MathF.Abs(SX));
  105. int VpY = (int)MathF.Max(0, TY - MathF.Abs(SY));
  106. int VpW = (int)(TX + MathF.Abs(SX)) - VpX;
  107. int VpH = (int)(TY + MathF.Abs(SY)) - VpY;
  108. Gpu.Renderer.FrameBuffer.Create(Key, Width, Height);
  109. Gpu.Renderer.FrameBuffer.Bind(Key);
  110. Gpu.Renderer.FrameBuffer.SetViewport(VpX, VpY, VpW, VpH);
  111. }
  112. private long[] UploadShaders(NvGpuVmm Vmm)
  113. {
  114. long[] Keys = new long[5];
  115. long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  116. int Index = 1;
  117. int VpAControl = ReadRegister(NvGpuEngine3dReg.ShaderNControl);
  118. bool VpAEnable = (VpAControl & 1) != 0;
  119. if (VpAEnable)
  120. {
  121. //Note: The maxwell supports 2 vertex programs, usually
  122. //only VP B is used, but in some cases VP A is also used.
  123. //In this case, it seems to function as an extra vertex
  124. //shader stage.
  125. //The graphics abstraction layer has a special overload for this
  126. //case, which should merge the two shaders into one vertex shader.
  127. int VpAOffset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset);
  128. int VpBOffset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + 0x10);
  129. long VpAPos = BasePosition + (uint)VpAOffset;
  130. long VpBPos = BasePosition + (uint)VpBOffset;
  131. Gpu.Renderer.Shader.Create(Vmm, VpAPos, VpBPos, GalShaderType.Vertex);
  132. Gpu.Renderer.Shader.Bind(VpBPos);
  133. Index = 2;
  134. }
  135. for (; Index < 6; Index++)
  136. {
  137. GalShaderType Type = GetTypeFromProgram(Index);
  138. int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + Index * 0x10);
  139. int Offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + Index * 0x10);
  140. //Note: Vertex Program (B) is always enabled.
  141. bool Enable = (Control & 1) != 0 || Index == 1;
  142. if (!Enable)
  143. {
  144. Gpu.Renderer.Shader.Unbind(Type);
  145. continue;
  146. }
  147. long Key = BasePosition + (uint)Offset;
  148. Keys[(int)Type] = Key;
  149. Gpu.Renderer.Shader.Create(Vmm, Key, Type);
  150. Gpu.Renderer.Shader.Bind(Key);
  151. }
  152. float SignX = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleX);
  153. float SignY = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleY);
  154. Gpu.Renderer.Shader.SetFlip(SignX, SignY);
  155. return Keys;
  156. }
  157. private static GalShaderType GetTypeFromProgram(int Program)
  158. {
  159. switch (Program)
  160. {
  161. case 0:
  162. case 1: return GalShaderType.Vertex;
  163. case 2: return GalShaderType.TessControl;
  164. case 3: return GalShaderType.TessEvaluation;
  165. case 4: return GalShaderType.Geometry;
  166. case 5: return GalShaderType.Fragment;
  167. }
  168. throw new ArgumentOutOfRangeException(nameof(Program));
  169. }
  170. private void SetFrontFace()
  171. {
  172. float SignX = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleX);
  173. float SignY = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleY);
  174. GalFrontFace FrontFace = (GalFrontFace)ReadRegister(NvGpuEngine3dReg.FrontFace);
  175. //Flipping breaks facing. Flipping front facing too fixes it
  176. if (SignX != SignY)
  177. {
  178. switch (FrontFace)
  179. {
  180. case GalFrontFace.CW:
  181. FrontFace = GalFrontFace.CCW;
  182. break;
  183. case GalFrontFace.CCW:
  184. FrontFace = GalFrontFace.CW;
  185. break;
  186. }
  187. }
  188. Gpu.Renderer.Rasterizer.SetFrontFace(FrontFace);
  189. }
  190. private void SetCullFace()
  191. {
  192. bool Enable = (ReadRegister(NvGpuEngine3dReg.CullFaceEnable) & 1) != 0;
  193. if (Enable)
  194. {
  195. Gpu.Renderer.Rasterizer.EnableCullFace();
  196. }
  197. else
  198. {
  199. Gpu.Renderer.Rasterizer.DisableCullFace();
  200. }
  201. if (!Enable)
  202. {
  203. return;
  204. }
  205. GalCullFace CullFace = (GalCullFace)ReadRegister(NvGpuEngine3dReg.CullFace);
  206. Gpu.Renderer.Rasterizer.SetCullFace(CullFace);
  207. }
  208. private void SetDepth()
  209. {
  210. float ClearDepth = ReadRegisterFloat(NvGpuEngine3dReg.ClearDepth);
  211. Gpu.Renderer.Rasterizer.SetClearDepth(ClearDepth);
  212. bool Enable = (ReadRegister(NvGpuEngine3dReg.DepthTestEnable) & 1) != 0;
  213. if (Enable)
  214. {
  215. Gpu.Renderer.Rasterizer.EnableDepthTest();
  216. }
  217. else
  218. {
  219. Gpu.Renderer.Rasterizer.DisableDepthTest();
  220. }
  221. if (!Enable)
  222. {
  223. return;
  224. }
  225. GalComparisonOp Func = (GalComparisonOp)ReadRegister(NvGpuEngine3dReg.DepthTestFunction);
  226. Gpu.Renderer.Rasterizer.SetDepthFunction(Func);
  227. }
  228. private void SetStencil()
  229. {
  230. int ClearStencil = ReadRegister(NvGpuEngine3dReg.ClearStencil);
  231. Gpu.Renderer.Rasterizer.SetClearStencil(ClearStencil);
  232. bool Enable = (ReadRegister(NvGpuEngine3dReg.StencilEnable) & 1) != 0;
  233. if (Enable)
  234. {
  235. Gpu.Renderer.Rasterizer.EnableStencilTest();
  236. }
  237. else
  238. {
  239. Gpu.Renderer.Rasterizer.DisableStencilTest();
  240. }
  241. if (!Enable)
  242. {
  243. return;
  244. }
  245. void SetFaceStencil(
  246. bool IsFrontFace,
  247. NvGpuEngine3dReg Func,
  248. NvGpuEngine3dReg FuncRef,
  249. NvGpuEngine3dReg FuncMask,
  250. NvGpuEngine3dReg OpFail,
  251. NvGpuEngine3dReg OpZFail,
  252. NvGpuEngine3dReg OpZPass,
  253. NvGpuEngine3dReg Mask)
  254. {
  255. Gpu.Renderer.Rasterizer.SetStencilFunction(
  256. IsFrontFace,
  257. (GalComparisonOp)ReadRegister(Func),
  258. ReadRegister(FuncRef),
  259. ReadRegister(FuncMask));
  260. Gpu.Renderer.Rasterizer.SetStencilOp(
  261. IsFrontFace,
  262. (GalStencilOp)ReadRegister(OpFail),
  263. (GalStencilOp)ReadRegister(OpZFail),
  264. (GalStencilOp)ReadRegister(OpZPass));
  265. Gpu.Renderer.Rasterizer.SetStencilMask(IsFrontFace, ReadRegister(Mask));
  266. }
  267. SetFaceStencil(false,
  268. NvGpuEngine3dReg.StencilBackFuncFunc,
  269. NvGpuEngine3dReg.StencilBackFuncRef,
  270. NvGpuEngine3dReg.StencilBackFuncMask,
  271. NvGpuEngine3dReg.StencilBackOpFail,
  272. NvGpuEngine3dReg.StencilBackOpZFail,
  273. NvGpuEngine3dReg.StencilBackOpZPass,
  274. NvGpuEngine3dReg.StencilBackMask);
  275. SetFaceStencil(true,
  276. NvGpuEngine3dReg.StencilFrontFuncFunc,
  277. NvGpuEngine3dReg.StencilFrontFuncRef,
  278. NvGpuEngine3dReg.StencilFrontFuncMask,
  279. NvGpuEngine3dReg.StencilFrontOpFail,
  280. NvGpuEngine3dReg.StencilFrontOpZFail,
  281. NvGpuEngine3dReg.StencilFrontOpZPass,
  282. NvGpuEngine3dReg.StencilFrontMask);
  283. }
  284. private void SetAlphaBlending()
  285. {
  286. //TODO: Support independent blend properly.
  287. bool Enable = (ReadRegister(NvGpuEngine3dReg.IBlendNEnable) & 1) != 0;
  288. if (Enable)
  289. {
  290. Gpu.Renderer.Blend.Enable();
  291. }
  292. else
  293. {
  294. Gpu.Renderer.Blend.Disable();
  295. }
  296. if (!Enable)
  297. {
  298. //If blend is not enabled, then the other values have no effect.
  299. //Note that if it is disabled, the register may contain invalid values.
  300. return;
  301. }
  302. bool BlendSeparateAlpha = (ReadRegister(NvGpuEngine3dReg.IBlendNSeparateAlpha) & 1) != 0;
  303. GalBlendEquation EquationRgb = (GalBlendEquation)ReadRegister(NvGpuEngine3dReg.IBlendNEquationRgb);
  304. GalBlendFactor FuncSrcRgb = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncSrcRgb);
  305. GalBlendFactor FuncDstRgb = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncDstRgb);
  306. if (BlendSeparateAlpha)
  307. {
  308. GalBlendEquation EquationAlpha = (GalBlendEquation)ReadRegister(NvGpuEngine3dReg.IBlendNEquationAlpha);
  309. GalBlendFactor FuncSrcAlpha = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncSrcAlpha);
  310. GalBlendFactor FuncDstAlpha = (GalBlendFactor)ReadRegister(NvGpuEngine3dReg.IBlendNFuncDstAlpha);
  311. Gpu.Renderer.Blend.SetSeparate(
  312. EquationRgb,
  313. EquationAlpha,
  314. FuncSrcRgb,
  315. FuncDstRgb,
  316. FuncSrcAlpha,
  317. FuncDstAlpha);
  318. }
  319. else
  320. {
  321. Gpu.Renderer.Blend.Set(EquationRgb, FuncSrcRgb, FuncDstRgb);
  322. }
  323. }
  324. private void SetPrimitiveRestart()
  325. {
  326. bool Enable = (ReadRegister(NvGpuEngine3dReg.PrimRestartEnable) & 1) != 0;
  327. if (Enable)
  328. {
  329. Gpu.Renderer.Rasterizer.EnablePrimitiveRestart();
  330. }
  331. else
  332. {
  333. Gpu.Renderer.Rasterizer.DisablePrimitiveRestart();
  334. }
  335. if (!Enable)
  336. {
  337. return;
  338. }
  339. uint Index = (uint)ReadRegister(NvGpuEngine3dReg.PrimRestartIndex);
  340. Gpu.Renderer.Rasterizer.SetPrimitiveRestartIndex(Index);
  341. }
  342. private void UploadTextures(NvGpuVmm Vmm, long[] Keys)
  343. {
  344. long BaseShPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  345. int TextureCbIndex = ReadRegister(NvGpuEngine3dReg.TextureCbIndex);
  346. //Note: On the emulator renderer, Texture Unit 0 is
  347. //reserved for drawing the frame buffer.
  348. int TexIndex = 1;
  349. for (int Index = 0; Index < Keys.Length; Index++)
  350. {
  351. foreach (ShaderDeclInfo DeclInfo in Gpu.Renderer.Shader.GetTextureUsage(Keys[Index]))
  352. {
  353. long Position = ConstBuffers[Index][TextureCbIndex].Position;
  354. UploadTexture(Vmm, Position, TexIndex, DeclInfo.Index);
  355. Gpu.Renderer.Shader.EnsureTextureBinding(DeclInfo.Name, TexIndex);
  356. TexIndex++;
  357. }
  358. }
  359. }
  360. private void UploadTexture(NvGpuVmm Vmm, long BasePosition, int TexIndex, int HndIndex)
  361. {
  362. long Position = BasePosition + HndIndex * 4;
  363. int TextureHandle = Vmm.ReadInt32(Position);
  364. if (TextureHandle == 0)
  365. {
  366. //TODO: Is this correct?
  367. //Some games like puyo puyo will have 0 handles.
  368. //It may be just normal behaviour or a bug caused by sync issues.
  369. //The game does initialize the value properly after through.
  370. return;
  371. }
  372. int TicIndex = (TextureHandle >> 0) & 0xfffff;
  373. int TscIndex = (TextureHandle >> 20) & 0xfff;
  374. long TicPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexHeaderPoolOffset);
  375. long TscPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexSamplerPoolOffset);
  376. TicPosition += TicIndex * 0x20;
  377. TscPosition += TscIndex * 0x20;
  378. GalTextureSampler Sampler = TextureFactory.MakeSampler(Gpu, Vmm, TscPosition);
  379. long Key = Vmm.ReadInt64(TicPosition + 4) & 0xffffffffffff;
  380. Key = Vmm.GetPhysicalAddress(Key);
  381. if (IsFrameBufferPosition(Key))
  382. {
  383. //This texture is a frame buffer texture,
  384. //we shouldn't read anything from memory and bind
  385. //the frame buffer texture instead, since we're not
  386. //really writing anything to memory.
  387. Gpu.Renderer.FrameBuffer.BindTexture(Key, TexIndex);
  388. }
  389. else
  390. {
  391. GalTexture NewTexture = TextureFactory.MakeTexture(Vmm, TicPosition);
  392. long Size = (uint)TextureHelper.GetTextureSize(NewTexture);
  393. bool HasCachedTexture = false;
  394. if (Gpu.Renderer.Texture.TryGetCachedTexture(Key, Size, out GalTexture Texture))
  395. {
  396. if (NewTexture.Equals(Texture) && !Vmm.IsRegionModified(Key, Size, NvGpuBufferType.Texture))
  397. {
  398. Gpu.Renderer.Texture.Bind(Key, TexIndex);
  399. HasCachedTexture = true;
  400. }
  401. }
  402. if (!HasCachedTexture)
  403. {
  404. byte[] Data = TextureFactory.GetTextureData(Vmm, TicPosition);
  405. Gpu.Renderer.Texture.Create(Key, Data, NewTexture);
  406. }
  407. Gpu.Renderer.Texture.Bind(Key, TexIndex);
  408. }
  409. Gpu.Renderer.Texture.SetSampler(Sampler);
  410. }
  411. private void UploadUniforms(NvGpuVmm Vmm)
  412. {
  413. long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  414. for (int Index = 0; Index < 5; Index++)
  415. {
  416. int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + (Index + 1) * 0x10);
  417. int Offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + (Index + 1) * 0x10);
  418. //Note: Vertex Program (B) is always enabled.
  419. bool Enable = (Control & 1) != 0 || Index == 0;
  420. if (!Enable)
  421. {
  422. continue;
  423. }
  424. for (int Cbuf = 0; Cbuf < ConstBuffers[Index].Length; Cbuf++)
  425. {
  426. ConstBuffer Cb = ConstBuffers[Index][Cbuf];
  427. if (Cb.Enabled)
  428. {
  429. IntPtr DataAddress = Vmm.GetHostAddress(Cb.Position, Cb.Size);
  430. Gpu.Renderer.Shader.SetConstBuffer(BasePosition + (uint)Offset, Cbuf, Cb.Size, DataAddress);
  431. }
  432. }
  433. }
  434. }
  435. private void UploadVertexArrays(NvGpuVmm Vmm)
  436. {
  437. long IndexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress);
  438. long IboKey = Vmm.GetPhysicalAddress(IndexPosition);
  439. int IndexEntryFmt = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat);
  440. int IndexFirst = ReadRegister(NvGpuEngine3dReg.IndexBatchFirst);
  441. int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount);
  442. GalIndexFormat IndexFormat = (GalIndexFormat)IndexEntryFmt;
  443. int IndexEntrySize = 1 << IndexEntryFmt;
  444. if (IndexEntrySize > 4)
  445. {
  446. throw new InvalidOperationException();
  447. }
  448. if (IndexCount != 0)
  449. {
  450. int IbSize = IndexCount * IndexEntrySize;
  451. bool IboCached = Gpu.Renderer.Rasterizer.IsIboCached(IboKey, (uint)IbSize);
  452. if (!IboCached || Vmm.IsRegionModified(IboKey, (uint)IbSize, NvGpuBufferType.Index))
  453. {
  454. IntPtr DataAddress = Vmm.GetHostAddress(IndexPosition, IbSize);
  455. Gpu.Renderer.Rasterizer.CreateIbo(IboKey, IbSize, DataAddress);
  456. }
  457. Gpu.Renderer.Rasterizer.SetIndexArray(IbSize, IndexFormat);
  458. }
  459. List<GalVertexAttrib>[] Attribs = new List<GalVertexAttrib>[32];
  460. for (int Attr = 0; Attr < 16; Attr++)
  461. {
  462. int Packed = ReadRegister(NvGpuEngine3dReg.VertexAttribNFormat + Attr);
  463. int ArrayIndex = Packed & 0x1f;
  464. if (Attribs[ArrayIndex] == null)
  465. {
  466. Attribs[ArrayIndex] = new List<GalVertexAttrib>();
  467. }
  468. Attribs[ArrayIndex].Add(new GalVertexAttrib(
  469. Attr,
  470. ((Packed >> 6) & 0x1) != 0,
  471. (Packed >> 7) & 0x3fff,
  472. (GalVertexAttribSize)((Packed >> 21) & 0x3f),
  473. (GalVertexAttribType)((Packed >> 27) & 0x7),
  474. ((Packed >> 31) & 0x1) != 0));
  475. }
  476. int VertexFirst = ReadRegister(NvGpuEngine3dReg.VertexArrayFirst);
  477. int VertexCount = ReadRegister(NvGpuEngine3dReg.VertexArrayCount);
  478. int PrimCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl);
  479. for (int Index = 0; Index < 32; Index++)
  480. {
  481. if (Attribs[Index] == null)
  482. {
  483. continue;
  484. }
  485. int Control = ReadRegister(NvGpuEngine3dReg.VertexArrayNControl + Index * 4);
  486. bool Enable = (Control & 0x1000) != 0;
  487. long VertexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + Index * 4);
  488. long VertexEndPos = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNEndAddr + Index * 2);
  489. if (!Enable)
  490. {
  491. continue;
  492. }
  493. long VboKey = Vmm.GetPhysicalAddress(VertexPosition);
  494. int Stride = Control & 0xfff;
  495. long VbSize = (VertexEndPos - VertexPosition) + 1;
  496. bool VboCached = Gpu.Renderer.Rasterizer.IsVboCached(VboKey, VbSize);
  497. if (!VboCached || Vmm.IsRegionModified(VboKey, VbSize, NvGpuBufferType.Vertex))
  498. {
  499. IntPtr DataAddress = Vmm.GetHostAddress(VertexPosition, VbSize);
  500. Gpu.Renderer.Rasterizer.CreateVbo(VboKey, (int)VbSize, DataAddress);
  501. }
  502. Gpu.Renderer.Rasterizer.SetVertexArray(Stride, VboKey, Attribs[Index].ToArray());
  503. }
  504. GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff);
  505. if (IndexCount != 0)
  506. {
  507. int VertexBase = ReadRegister(NvGpuEngine3dReg.VertexArrayElemBase);
  508. Gpu.Renderer.Rasterizer.DrawElements(IboKey, IndexFirst, VertexBase, PrimType);
  509. }
  510. else
  511. {
  512. Gpu.Renderer.Rasterizer.DrawArrays(VertexFirst, VertexCount, PrimType);
  513. }
  514. }
  515. private void QueryControl(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  516. {
  517. long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.QueryAddress);
  518. int Seq = Registers[(int)NvGpuEngine3dReg.QuerySequence];
  519. int Ctrl = Registers[(int)NvGpuEngine3dReg.QueryControl];
  520. int Mode = Ctrl & 3;
  521. if (Mode == 0)
  522. {
  523. //Write mode.
  524. Vmm.WriteInt32(Position, Seq);
  525. }
  526. WriteRegister(PBEntry);
  527. }
  528. private void CbData(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  529. {
  530. long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress);
  531. int Offset = ReadRegister(NvGpuEngine3dReg.ConstBufferOffset);
  532. foreach (int Arg in PBEntry.Arguments)
  533. {
  534. Vmm.WriteInt32(Position + Offset, Arg);
  535. Offset += 4;
  536. }
  537. WriteRegister(NvGpuEngine3dReg.ConstBufferOffset, Offset);
  538. }
  539. private void CbBind(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)
  540. {
  541. int Stage = (PBEntry.Method - 0x904) >> 3;
  542. int Index = PBEntry.Arguments[0];
  543. bool Enabled = (Index & 1) != 0;
  544. Index = (Index >> 4) & 0x1f;
  545. long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress);
  546. ConstBuffers[Stage][Index].Position = Position;
  547. ConstBuffers[Stage][Index].Enabled = Enabled;
  548. ConstBuffers[Stage][Index].Size = ReadRegister(NvGpuEngine3dReg.ConstBufferSize);
  549. }
  550. private float GetFlipSign(NvGpuEngine3dReg Reg)
  551. {
  552. return MathF.Sign(ReadRegisterFloat(Reg));
  553. }
  554. private long MakeInt64From2xInt32(NvGpuEngine3dReg Reg)
  555. {
  556. return
  557. (long)Registers[(int)Reg + 0] << 32 |
  558. (uint)Registers[(int)Reg + 1];
  559. }
  560. private void WriteRegister(NvGpuPBEntry PBEntry)
  561. {
  562. int ArgsCount = PBEntry.Arguments.Count;
  563. if (ArgsCount > 0)
  564. {
  565. Registers[PBEntry.Method] = PBEntry.Arguments[ArgsCount - 1];
  566. }
  567. }
  568. private int ReadRegister(NvGpuEngine3dReg Reg)
  569. {
  570. return Registers[(int)Reg];
  571. }
  572. private float ReadRegisterFloat(NvGpuEngine3dReg Reg)
  573. {
  574. return BitConverter.Int32BitsToSingle(ReadRegister(Reg));
  575. }
  576. private void WriteRegister(NvGpuEngine3dReg Reg, int Value)
  577. {
  578. Registers[(int)Reg] = Value;
  579. }
  580. public bool IsFrameBufferPosition(long Position)
  581. {
  582. return FrameBuffers.Contains(Position);
  583. }
  584. }
  585. }