NvGpuEngine3d.cs 25 KB

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