NvGpuEngine3d.cs 26 KB

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