NvGpuEngine3d.cs 28 KB

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