NvGpuEngine3d.cs 30 KB

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