NvGpuEngine3d.cs 30 KB

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