NvGpuEngine3d.cs 33 KB

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