NvGpuEngine3d.cs 25 KB

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