NvGpuEngine3d.cs 29 KB

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