NvGpuEngine3d.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  1. using Ryujinx.Common;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Graphics.Gal;
  4. using Ryujinx.Graphics.Memory;
  5. using Ryujinx.Graphics.Texture;
  6. using System;
  7. using System.Collections.Generic;
  8. namespace Ryujinx.Graphics.Graphics3d
  9. {
  10. class NvGpuEngine3d : INvGpuEngine
  11. {
  12. public int[] Registers { get; private set; }
  13. private NvGpu Gpu;
  14. private Dictionary<int, NvGpuMethod> Methods;
  15. private struct ConstBuffer
  16. {
  17. public bool Enabled;
  18. public long Position;
  19. public int Size;
  20. }
  21. private ConstBuffer[][] ConstBuffers;
  22. // Viewport dimensions kept for scissor test limits
  23. private int ViewportX0 = 0;
  24. private int ViewportY0 = 0;
  25. private int ViewportX1 = 0;
  26. private int ViewportY1 = 0;
  27. private int ViewportWidth = 0;
  28. private int ViewportHeight = 0;
  29. private int CurrentInstance = 0;
  30. public NvGpuEngine3d(NvGpu Gpu)
  31. {
  32. this.Gpu = Gpu;
  33. Registers = new int[0xe00];
  34. Methods = new Dictionary<int, NvGpuMethod>();
  35. void AddMethod(int Meth, int Count, int Stride, NvGpuMethod Method)
  36. {
  37. while (Count-- > 0)
  38. {
  39. Methods.Add(Meth, Method);
  40. Meth += Stride;
  41. }
  42. }
  43. AddMethod(0x585, 1, 1, VertexEndGl);
  44. AddMethod(0x674, 1, 1, ClearBuffers);
  45. AddMethod(0x6c3, 1, 1, QueryControl);
  46. AddMethod(0x8e4, 16, 1, CbData);
  47. AddMethod(0x904, 5, 8, CbBind);
  48. ConstBuffers = new ConstBuffer[6][];
  49. for (int Index = 0; Index < ConstBuffers.Length; Index++)
  50. {
  51. ConstBuffers[Index] = new ConstBuffer[18];
  52. }
  53. //Ensure that all components are enabled by default.
  54. //FIXME: Is this correct?
  55. WriteRegister(NvGpuEngine3dReg.ColorMaskN, 0x1111);
  56. WriteRegister(NvGpuEngine3dReg.FrameBufferSrgb, 1);
  57. WriteRegister(NvGpuEngine3dReg.FrontFace, (int)GalFrontFace.CW);
  58. for (int Index = 0; Index < GalPipelineState.RenderTargetsCount; Index++)
  59. {
  60. WriteRegister(NvGpuEngine3dReg.IBlendNEquationRgb + Index * 8, (int)GalBlendEquation.FuncAdd);
  61. WriteRegister(NvGpuEngine3dReg.IBlendNFuncSrcRgb + Index * 8, (int)GalBlendFactor.One);
  62. WriteRegister(NvGpuEngine3dReg.IBlendNFuncDstRgb + Index * 8, (int)GalBlendFactor.Zero);
  63. WriteRegister(NvGpuEngine3dReg.IBlendNEquationAlpha + Index * 8, (int)GalBlendEquation.FuncAdd);
  64. WriteRegister(NvGpuEngine3dReg.IBlendNFuncSrcAlpha + Index * 8, (int)GalBlendFactor.One);
  65. WriteRegister(NvGpuEngine3dReg.IBlendNFuncDstAlpha + Index * 8, (int)GalBlendFactor.Zero);
  66. }
  67. }
  68. public void CallMethod(NvGpuVmm Vmm, GpuMethodCall MethCall)
  69. {
  70. if (Methods.TryGetValue(MethCall.Method, out NvGpuMethod Method))
  71. {
  72. Method(Vmm, MethCall);
  73. }
  74. else
  75. {
  76. WriteRegister(MethCall);
  77. }
  78. }
  79. private void VertexEndGl(NvGpuVmm Vmm, GpuMethodCall MethCall)
  80. {
  81. LockCaches();
  82. GalPipelineState State = new GalPipelineState();
  83. // Framebuffer must be run configured because viewport dimensions may be used in other methods
  84. SetFrameBuffer(State);
  85. for (int FbIndex = 0; FbIndex < 8; FbIndex++)
  86. {
  87. SetFrameBuffer(Vmm, FbIndex);
  88. }
  89. SetFrontFace(State);
  90. SetCullFace(State);
  91. SetDepth(State);
  92. SetStencil(State);
  93. SetScissor(State);
  94. SetBlending(State);
  95. SetColorMask(State);
  96. SetPrimitiveRestart(State);
  97. SetZeta(Vmm);
  98. SetRenderTargets();
  99. long[] Keys = UploadShaders(Vmm);
  100. Gpu.Renderer.Shader.BindProgram();
  101. UploadTextures(Vmm, State, Keys);
  102. UploadConstBuffers(Vmm, State, Keys);
  103. UploadVertexArrays(Vmm, State);
  104. DispatchRender(Vmm, State);
  105. UnlockCaches();
  106. }
  107. private void LockCaches()
  108. {
  109. Gpu.Renderer.Buffer.LockCache();
  110. Gpu.Renderer.Rasterizer.LockCaches();
  111. Gpu.Renderer.Texture.LockCache();
  112. }
  113. private void UnlockCaches()
  114. {
  115. Gpu.Renderer.Buffer.UnlockCache();
  116. Gpu.Renderer.Rasterizer.UnlockCaches();
  117. Gpu.Renderer.Texture.UnlockCache();
  118. }
  119. private void ClearBuffers(NvGpuVmm Vmm, GpuMethodCall MethCall)
  120. {
  121. int Attachment = (MethCall.Argument >> 6) & 0xf;
  122. GalClearBufferFlags Flags = (GalClearBufferFlags)(MethCall.Argument & 0x3f);
  123. float Red = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 0);
  124. float Green = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 1);
  125. float Blue = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 2);
  126. float Alpha = ReadRegisterFloat(NvGpuEngine3dReg.ClearNColor + 3);
  127. float Depth = ReadRegisterFloat(NvGpuEngine3dReg.ClearDepth);
  128. int Stencil = ReadRegister(NvGpuEngine3dReg.ClearStencil);
  129. SetFrameBuffer(Vmm, Attachment);
  130. SetZeta(Vmm);
  131. SetRenderTargets();
  132. Gpu.Renderer.RenderTarget.Bind();
  133. Gpu.Renderer.Rasterizer.ClearBuffers(Flags, Attachment, Red, Green, Blue, Alpha, Depth, Stencil);
  134. Gpu.Renderer.Pipeline.ResetDepthMask();
  135. Gpu.Renderer.Pipeline.ResetColorMask(Attachment);
  136. }
  137. private void SetFrameBuffer(NvGpuVmm Vmm, int FbIndex)
  138. {
  139. long VA = MakeInt64From2xInt32(NvGpuEngine3dReg.FrameBufferNAddress + FbIndex * 0x10);
  140. int SurfFormat = ReadRegister(NvGpuEngine3dReg.FrameBufferNFormat + FbIndex * 0x10);
  141. if (VA == 0 || SurfFormat == 0)
  142. {
  143. Gpu.Renderer.RenderTarget.UnbindColor(FbIndex);
  144. return;
  145. }
  146. long Key = Vmm.GetPhysicalAddress(VA);
  147. int Width = ReadRegister(NvGpuEngine3dReg.FrameBufferNWidth + FbIndex * 0x10);
  148. int Height = ReadRegister(NvGpuEngine3dReg.FrameBufferNHeight + FbIndex * 0x10);
  149. int ArrayMode = ReadRegister(NvGpuEngine3dReg.FrameBufferNArrayMode + FbIndex * 0x10);
  150. int LayerCount = ArrayMode & 0xFFFF;
  151. int LayerStride = ReadRegister(NvGpuEngine3dReg.FrameBufferNLayerStride + FbIndex * 0x10);
  152. int BaseLayer = ReadRegister(NvGpuEngine3dReg.FrameBufferNBaseLayer + FbIndex * 0x10);
  153. int BlockDim = ReadRegister(NvGpuEngine3dReg.FrameBufferNBlockDim + FbIndex * 0x10);
  154. int GobBlockHeight = 1 << ((BlockDim >> 4) & 7);
  155. GalMemoryLayout Layout = (GalMemoryLayout)((BlockDim >> 12) & 1);
  156. float TX = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNTranslateX + FbIndex * 8);
  157. float TY = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNTranslateY + FbIndex * 8);
  158. float SX = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNScaleX + FbIndex * 8);
  159. float SY = ReadRegisterFloat(NvGpuEngine3dReg.ViewportNScaleY + FbIndex * 8);
  160. ViewportX0 = (int)MathF.Max(0, TX - MathF.Abs(SX));
  161. ViewportY0 = (int)MathF.Max(0, TY - MathF.Abs(SY));
  162. ViewportX1 = (int)(TX + MathF.Abs(SX));
  163. ViewportY1 = (int)(TY + MathF.Abs(SY));
  164. GalImageFormat Format = ImageUtils.ConvertSurface((GalSurfaceFormat)SurfFormat);
  165. GalImage Image = new GalImage(Width, Height, 1, 1, 1, GobBlockHeight, 1, Layout, Format, GalTextureTarget.TwoD);
  166. Gpu.ResourceManager.SendColorBuffer(Vmm, Key, FbIndex, Image);
  167. Gpu.Renderer.RenderTarget.SetViewport(FbIndex, ViewportX0, ViewportY0, ViewportX1 - ViewportX0, ViewportY1 - ViewportY0);
  168. }
  169. private void SetFrameBuffer(GalPipelineState State)
  170. {
  171. State.FramebufferSrgb = ReadRegisterBool(NvGpuEngine3dReg.FrameBufferSrgb);
  172. State.FlipX = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleX);
  173. State.FlipY = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleY);
  174. int ScreenYControl = ReadRegister(NvGpuEngine3dReg.ScreenYControl);
  175. bool NegateY = (ScreenYControl & 1) != 0;
  176. if (NegateY)
  177. {
  178. State.FlipY = -State.FlipY;
  179. }
  180. }
  181. private void SetZeta(NvGpuVmm Vmm)
  182. {
  183. long VA = MakeInt64From2xInt32(NvGpuEngine3dReg.ZetaAddress);
  184. int ZetaFormat = ReadRegister(NvGpuEngine3dReg.ZetaFormat);
  185. int BlockDim = ReadRegister(NvGpuEngine3dReg.ZetaBlockDimensions);
  186. int GobBlockHeight = 1 << ((BlockDim >> 4) & 7);
  187. GalMemoryLayout Layout = (GalMemoryLayout)((BlockDim >> 12) & 1); //?
  188. bool ZetaEnable = ReadRegisterBool(NvGpuEngine3dReg.ZetaEnable);
  189. if (VA == 0 || ZetaFormat == 0 || !ZetaEnable)
  190. {
  191. Gpu.Renderer.RenderTarget.UnbindZeta();
  192. return;
  193. }
  194. long Key = Vmm.GetPhysicalAddress(VA);
  195. int Width = ReadRegister(NvGpuEngine3dReg.ZetaHoriz);
  196. int Height = ReadRegister(NvGpuEngine3dReg.ZetaVert);
  197. GalImageFormat Format = ImageUtils.ConvertZeta((GalZetaFormat)ZetaFormat);
  198. // TODO: Support non 2D?
  199. GalImage Image = new GalImage(Width, Height, 1, 1, 1, GobBlockHeight, 1, Layout, Format, GalTextureTarget.TwoD);
  200. Gpu.ResourceManager.SendZetaBuffer(Vmm, Key, Image);
  201. }
  202. private long[] UploadShaders(NvGpuVmm Vmm)
  203. {
  204. long[] Keys = new long[5];
  205. long BasePosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  206. int Index = 1;
  207. int VpAControl = ReadRegister(NvGpuEngine3dReg.ShaderNControl);
  208. bool VpAEnable = (VpAControl & 1) != 0;
  209. if (VpAEnable)
  210. {
  211. //Note: The maxwell supports 2 vertex programs, usually
  212. //only VP B is used, but in some cases VP A is also used.
  213. //In this case, it seems to function as an extra vertex
  214. //shader stage.
  215. //The graphics abstraction layer has a special overload for this
  216. //case, which should merge the two shaders into one vertex shader.
  217. int VpAOffset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset);
  218. int VpBOffset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + 0x10);
  219. long VpAPos = BasePosition + (uint)VpAOffset;
  220. long VpBPos = BasePosition + (uint)VpBOffset;
  221. Keys[(int)GalShaderType.Vertex] = VpBPos;
  222. Gpu.Renderer.Shader.Create(Vmm, VpAPos, VpBPos, GalShaderType.Vertex);
  223. Gpu.Renderer.Shader.Bind(VpBPos);
  224. Index = 2;
  225. }
  226. for (; Index < 6; Index++)
  227. {
  228. GalShaderType Type = GetTypeFromProgram(Index);
  229. int Control = ReadRegister(NvGpuEngine3dReg.ShaderNControl + Index * 0x10);
  230. int Offset = ReadRegister(NvGpuEngine3dReg.ShaderNOffset + Index * 0x10);
  231. //Note: Vertex Program (B) is always enabled.
  232. bool Enable = (Control & 1) != 0 || Index == 1;
  233. if (!Enable)
  234. {
  235. Gpu.Renderer.Shader.Unbind(Type);
  236. continue;
  237. }
  238. long Key = BasePosition + (uint)Offset;
  239. Keys[(int)Type] = Key;
  240. Gpu.Renderer.Shader.Create(Vmm, Key, Type);
  241. Gpu.Renderer.Shader.Bind(Key);
  242. }
  243. return Keys;
  244. }
  245. private static GalShaderType GetTypeFromProgram(int Program)
  246. {
  247. switch (Program)
  248. {
  249. case 0:
  250. case 1: return GalShaderType.Vertex;
  251. case 2: return GalShaderType.TessControl;
  252. case 3: return GalShaderType.TessEvaluation;
  253. case 4: return GalShaderType.Geometry;
  254. case 5: return GalShaderType.Fragment;
  255. }
  256. throw new ArgumentOutOfRangeException(nameof(Program));
  257. }
  258. private void SetFrontFace(GalPipelineState State)
  259. {
  260. float SignX = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleX);
  261. float SignY = GetFlipSign(NvGpuEngine3dReg.ViewportNScaleY);
  262. GalFrontFace FrontFace = (GalFrontFace)ReadRegister(NvGpuEngine3dReg.FrontFace);
  263. //Flipping breaks facing. Flipping front facing too fixes it
  264. if (SignX != SignY)
  265. {
  266. switch (FrontFace)
  267. {
  268. case GalFrontFace.CW: FrontFace = GalFrontFace.CCW; break;
  269. case GalFrontFace.CCW: FrontFace = GalFrontFace.CW; break;
  270. }
  271. }
  272. State.FrontFace = FrontFace;
  273. }
  274. private void SetCullFace(GalPipelineState State)
  275. {
  276. State.CullFaceEnabled = ReadRegisterBool(NvGpuEngine3dReg.CullFaceEnable);
  277. if (State.CullFaceEnabled)
  278. {
  279. State.CullFace = (GalCullFace)ReadRegister(NvGpuEngine3dReg.CullFace);
  280. }
  281. }
  282. private void SetDepth(GalPipelineState State)
  283. {
  284. State.DepthTestEnabled = ReadRegisterBool(NvGpuEngine3dReg.DepthTestEnable);
  285. State.DepthWriteEnabled = ReadRegisterBool(NvGpuEngine3dReg.DepthWriteEnable);
  286. if (State.DepthTestEnabled)
  287. {
  288. State.DepthFunc = (GalComparisonOp)ReadRegister(NvGpuEngine3dReg.DepthTestFunction);
  289. }
  290. State.DepthRangeNear = ReadRegisterFloat(NvGpuEngine3dReg.DepthRangeNNear);
  291. State.DepthRangeFar = ReadRegisterFloat(NvGpuEngine3dReg.DepthRangeNFar);
  292. }
  293. private void SetStencil(GalPipelineState State)
  294. {
  295. State.StencilTestEnabled = ReadRegisterBool(NvGpuEngine3dReg.StencilEnable);
  296. if (State.StencilTestEnabled)
  297. {
  298. State.StencilBackFuncFunc = (GalComparisonOp)ReadRegister(NvGpuEngine3dReg.StencilBackFuncFunc);
  299. State.StencilBackFuncRef = ReadRegister(NvGpuEngine3dReg.StencilBackFuncRef);
  300. State.StencilBackFuncMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilBackFuncMask);
  301. State.StencilBackOpFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilBackOpFail);
  302. State.StencilBackOpZFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilBackOpZFail);
  303. State.StencilBackOpZPass = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilBackOpZPass);
  304. State.StencilBackMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilBackMask);
  305. State.StencilFrontFuncFunc = (GalComparisonOp)ReadRegister(NvGpuEngine3dReg.StencilFrontFuncFunc);
  306. State.StencilFrontFuncRef = ReadRegister(NvGpuEngine3dReg.StencilFrontFuncRef);
  307. State.StencilFrontFuncMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilFrontFuncMask);
  308. State.StencilFrontOpFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilFrontOpFail);
  309. State.StencilFrontOpZFail = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilFrontOpZFail);
  310. State.StencilFrontOpZPass = (GalStencilOp)ReadRegister(NvGpuEngine3dReg.StencilFrontOpZPass);
  311. State.StencilFrontMask = (uint)ReadRegister(NvGpuEngine3dReg.StencilFrontMask);
  312. }
  313. }
  314. private void SetScissor(GalPipelineState State)
  315. {
  316. int count = 0;
  317. for (int Index = 0; Index < GalPipelineState.RenderTargetsCount; Index++)
  318. {
  319. State.ScissorTestEnabled[Index] = ReadRegisterBool(NvGpuEngine3dReg.ScissorEnable + Index * 4);
  320. if (State.ScissorTestEnabled[Index])
  321. {
  322. uint ScissorHorizontal = (uint)ReadRegister(NvGpuEngine3dReg.ScissorHorizontal + Index * 4);
  323. uint ScissorVertical = (uint)ReadRegister(NvGpuEngine3dReg.ScissorVertical + Index * 4);
  324. int left = (int)(ScissorHorizontal & 0xFFFF); // Left, lower 16 bits
  325. int right = (int)(ScissorHorizontal >> 16); // Right, upper 16 bits
  326. int bottom = (int)(ScissorVertical & 0xFFFF); // Bottom, lower 16 bits
  327. int top = (int)(ScissorVertical >> 16); // Top, upper 16 bits
  328. int width = Math.Abs(right - left);
  329. int height = Math.Abs(top - bottom);
  330. // If the scissor test covers the whole possible viewport, i.e. uninititalized, disable scissor test
  331. if ((width > NvGpu.MaxViewportSize && height > NvGpu.MaxViewportSize) || width <= 0 || height <= 0)
  332. {
  333. State.ScissorTestEnabled[Index] = false;
  334. continue;
  335. }
  336. // Keep track of how many scissor tests are active.
  337. // If only 1, and it's the first user should apply to all viewports
  338. count++;
  339. // Flip X
  340. if (State.FlipX == -1)
  341. {
  342. left = ViewportX1 - (left - ViewportX0);
  343. right = ViewportX1 - (right - ViewportX0);
  344. }
  345. // Ensure X is in the right order
  346. if (left > right)
  347. {
  348. int temp = left;
  349. left = right;
  350. right = temp;
  351. }
  352. // Flip Y
  353. if (State.FlipY == -1)
  354. {
  355. bottom = ViewportY1 - (bottom - ViewportY0);
  356. top = ViewportY1 - (top - ViewportY0);
  357. }
  358. // Ensure Y is in the right order
  359. if (bottom > top)
  360. {
  361. int temp = top;
  362. top = bottom;
  363. bottom = temp;
  364. }
  365. // Handle out of active viewport dimensions
  366. left = Math.Clamp(left, ViewportX0, ViewportX1);
  367. right = Math.Clamp(right, ViewportX0, ViewportX1);
  368. top = Math.Clamp(top, ViewportY0, ViewportY1);
  369. bottom = Math.Clamp(bottom, ViewportY0, ViewportY1);
  370. // Save values to state
  371. State.ScissorTestX[Index] = left;
  372. State.ScissorTestY[Index] = bottom;
  373. State.ScissorTestWidth[Index] = right - left;
  374. State.ScissorTestHeight[Index] = top - bottom;
  375. }
  376. }
  377. State.ScissorTestCount = count;
  378. }
  379. private void SetBlending(GalPipelineState State)
  380. {
  381. bool BlendIndependent = ReadRegisterBool(NvGpuEngine3dReg.BlendIndependent);
  382. State.BlendIndependent = BlendIndependent;
  383. for (int Index = 0; Index < GalPipelineState.RenderTargetsCount; Index++)
  384. {
  385. if (BlendIndependent)
  386. {
  387. State.Blends[Index].Enabled = ReadRegisterBool(NvGpuEngine3dReg.IBlendNEnable + Index);
  388. if (State.Blends[Index].Enabled)
  389. {
  390. State.Blends[Index].SeparateAlpha = ReadRegisterBool(NvGpuEngine3dReg.IBlendNSeparateAlpha + Index * 8);
  391. State.Blends[Index].EquationRgb = ReadBlendEquation(NvGpuEngine3dReg.IBlendNEquationRgb + Index * 8);
  392. State.Blends[Index].FuncSrcRgb = ReadBlendFactor (NvGpuEngine3dReg.IBlendNFuncSrcRgb + Index * 8);
  393. State.Blends[Index].FuncDstRgb = ReadBlendFactor (NvGpuEngine3dReg.IBlendNFuncDstRgb + Index * 8);
  394. State.Blends[Index].EquationAlpha = ReadBlendEquation(NvGpuEngine3dReg.IBlendNEquationAlpha + Index * 8);
  395. State.Blends[Index].FuncSrcAlpha = ReadBlendFactor (NvGpuEngine3dReg.IBlendNFuncSrcAlpha + Index * 8);
  396. State.Blends[Index].FuncDstAlpha = ReadBlendFactor (NvGpuEngine3dReg.IBlendNFuncDstAlpha + Index * 8);
  397. }
  398. }
  399. else
  400. {
  401. //It seems that even when independent blend is disabled, the first IBlend enable
  402. //register is still set to indicate whenever blend is enabled or not (?).
  403. State.Blends[Index].Enabled = ReadRegisterBool(NvGpuEngine3dReg.IBlendNEnable);
  404. if (State.Blends[Index].Enabled)
  405. {
  406. State.Blends[Index].SeparateAlpha = ReadRegisterBool(NvGpuEngine3dReg.BlendSeparateAlpha);
  407. State.Blends[Index].EquationRgb = ReadBlendEquation(NvGpuEngine3dReg.BlendEquationRgb);
  408. State.Blends[Index].FuncSrcRgb = ReadBlendFactor (NvGpuEngine3dReg.BlendFuncSrcRgb);
  409. State.Blends[Index].FuncDstRgb = ReadBlendFactor (NvGpuEngine3dReg.BlendFuncDstRgb);
  410. State.Blends[Index].EquationAlpha = ReadBlendEquation(NvGpuEngine3dReg.BlendEquationAlpha);
  411. State.Blends[Index].FuncSrcAlpha = ReadBlendFactor (NvGpuEngine3dReg.BlendFuncSrcAlpha);
  412. State.Blends[Index].FuncDstAlpha = ReadBlendFactor (NvGpuEngine3dReg.BlendFuncDstAlpha);
  413. }
  414. }
  415. }
  416. }
  417. private GalBlendEquation ReadBlendEquation(NvGpuEngine3dReg Register)
  418. {
  419. return (GalBlendEquation)ReadRegister(Register);
  420. }
  421. private GalBlendFactor ReadBlendFactor(NvGpuEngine3dReg Register)
  422. {
  423. return (GalBlendFactor)ReadRegister(Register);
  424. }
  425. private void SetColorMask(GalPipelineState State)
  426. {
  427. bool ColorMaskCommon = ReadRegisterBool(NvGpuEngine3dReg.ColorMaskCommon);
  428. State.ColorMaskCommon = ColorMaskCommon;
  429. for (int Index = 0; Index < GalPipelineState.RenderTargetsCount; Index++)
  430. {
  431. int ColorMask = ReadRegister(NvGpuEngine3dReg.ColorMaskN + (ColorMaskCommon ? 0 : Index));
  432. State.ColorMasks[Index].Red = ((ColorMask >> 0) & 0xf) != 0;
  433. State.ColorMasks[Index].Green = ((ColorMask >> 4) & 0xf) != 0;
  434. State.ColorMasks[Index].Blue = ((ColorMask >> 8) & 0xf) != 0;
  435. State.ColorMasks[Index].Alpha = ((ColorMask >> 12) & 0xf) != 0;
  436. }
  437. }
  438. private void SetPrimitiveRestart(GalPipelineState State)
  439. {
  440. State.PrimitiveRestartEnabled = ReadRegisterBool(NvGpuEngine3dReg.PrimRestartEnable);
  441. if (State.PrimitiveRestartEnabled)
  442. {
  443. State.PrimitiveRestartIndex = (uint)ReadRegister(NvGpuEngine3dReg.PrimRestartIndex);
  444. }
  445. }
  446. private void SetRenderTargets()
  447. {
  448. //Commercial games do not seem to
  449. //bool SeparateFragData = ReadRegisterBool(NvGpuEngine3dReg.RTSeparateFragData);
  450. uint Control = (uint)(ReadRegister(NvGpuEngine3dReg.RTControl));
  451. uint Count = Control & 0xf;
  452. if (Count > 0)
  453. {
  454. int[] Map = new int[Count];
  455. for (int Index = 0; Index < Count; Index++)
  456. {
  457. int Shift = 4 + Index * 3;
  458. Map[Index] = (int)((Control >> Shift) & 7);
  459. }
  460. Gpu.Renderer.RenderTarget.SetMap(Map);
  461. }
  462. else
  463. {
  464. Gpu.Renderer.RenderTarget.SetMap(null);
  465. }
  466. }
  467. private void UploadTextures(NvGpuVmm Vmm, GalPipelineState State, long[] Keys)
  468. {
  469. long BaseShPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.ShaderAddress);
  470. int TextureCbIndex = ReadRegister(NvGpuEngine3dReg.TextureCbIndex);
  471. List<(long, GalImage, GalTextureSampler)> UnboundTextures = new List<(long, GalImage, GalTextureSampler)>();
  472. for (int Index = 0; Index < Keys.Length; Index++)
  473. {
  474. foreach (ShaderDeclInfo DeclInfo in Gpu.Renderer.Shader.GetTextureUsage(Keys[Index]))
  475. {
  476. long Position;
  477. if (DeclInfo.IsCb)
  478. {
  479. Position = ConstBuffers[Index][DeclInfo.Cbuf].Position;
  480. }
  481. else
  482. {
  483. Position = ConstBuffers[Index][TextureCbIndex].Position;
  484. }
  485. int TextureHandle = Vmm.ReadInt32(Position + DeclInfo.Index * 4);
  486. UnboundTextures.Add(UploadTexture(Vmm, TextureHandle));
  487. }
  488. }
  489. for (int Index = 0; Index < UnboundTextures.Count; Index++)
  490. {
  491. (long Key, GalImage Image, GalTextureSampler Sampler) = UnboundTextures[Index];
  492. if (Key == 0)
  493. {
  494. continue;
  495. }
  496. Gpu.Renderer.Texture.Bind(Key, Index, Image);
  497. Gpu.Renderer.Texture.SetSampler(Image, Sampler);
  498. }
  499. }
  500. private (long, GalImage, GalTextureSampler) UploadTexture(NvGpuVmm Vmm, int TextureHandle)
  501. {
  502. if (TextureHandle == 0)
  503. {
  504. //FIXME: Some games like puyo puyo will use handles with the value 0.
  505. //This is a bug, most likely caused by sync issues.
  506. return (0, default(GalImage), default(GalTextureSampler));
  507. }
  508. bool LinkedTsc = ReadRegisterBool(NvGpuEngine3dReg.LinkedTsc);
  509. int TicIndex = (TextureHandle >> 0) & 0xfffff;
  510. int TscIndex = LinkedTsc ? TicIndex : (TextureHandle >> 20) & 0xfff;
  511. long TicPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexHeaderPoolOffset);
  512. long TscPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.TexSamplerPoolOffset);
  513. TicPosition += TicIndex * 0x20;
  514. TscPosition += TscIndex * 0x20;
  515. GalImage Image = TextureFactory.MakeTexture(Vmm, TicPosition);
  516. GalTextureSampler Sampler = TextureFactory.MakeSampler(Gpu, Vmm, TscPosition);
  517. long Key = Vmm.ReadInt64(TicPosition + 4) & 0xffffffffffff;
  518. if (Image.Layout == GalMemoryLayout.BlockLinear)
  519. {
  520. Key &= ~0x1ffL;
  521. }
  522. else if (Image.Layout == GalMemoryLayout.Pitch)
  523. {
  524. Key &= ~0x1fL;
  525. }
  526. Key = Vmm.GetPhysicalAddress(Key);
  527. if (Key == -1)
  528. {
  529. //FIXME: Shouldn't ignore invalid addresses.
  530. return (0, default(GalImage), default(GalTextureSampler));
  531. }
  532. Gpu.ResourceManager.SendTexture(Vmm, Key, Image);
  533. return (Key, Image, Sampler);
  534. }
  535. private void UploadConstBuffers(NvGpuVmm Vmm, GalPipelineState State, long[] Keys)
  536. {
  537. for (int Stage = 0; Stage < Keys.Length; Stage++)
  538. {
  539. foreach (ShaderDeclInfo DeclInfo in Gpu.Renderer.Shader.GetConstBufferUsage(Keys[Stage]))
  540. {
  541. ConstBuffer Cb = ConstBuffers[Stage][DeclInfo.Cbuf];
  542. if (!Cb.Enabled)
  543. {
  544. continue;
  545. }
  546. long Key = Vmm.GetPhysicalAddress(Cb.Position);
  547. if (Gpu.ResourceManager.MemoryRegionModified(Vmm, Key, Cb.Size, NvGpuBufferType.ConstBuffer))
  548. {
  549. if (Vmm.TryGetHostAddress(Cb.Position, Cb.Size, out IntPtr CbPtr))
  550. {
  551. Gpu.Renderer.Buffer.SetData(Key, Cb.Size, CbPtr);
  552. }
  553. else
  554. {
  555. Gpu.Renderer.Buffer.SetData(Key, Vmm.ReadBytes(Cb.Position, Cb.Size));
  556. }
  557. }
  558. State.ConstBufferKeys[Stage][DeclInfo.Cbuf] = Key;
  559. }
  560. }
  561. }
  562. private void UploadVertexArrays(NvGpuVmm Vmm, GalPipelineState State)
  563. {
  564. long IbPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress);
  565. long IboKey = Vmm.GetPhysicalAddress(IbPosition);
  566. int IndexEntryFmt = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat);
  567. int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount);
  568. int PrimCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl);
  569. GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff);
  570. GalIndexFormat IndexFormat = (GalIndexFormat)IndexEntryFmt;
  571. int IndexEntrySize = 1 << IndexEntryFmt;
  572. if (IndexEntrySize > 4)
  573. {
  574. throw new InvalidOperationException("Invalid index entry size \"" + IndexEntrySize + "\"!");
  575. }
  576. if (IndexCount != 0)
  577. {
  578. int IbSize = IndexCount * IndexEntrySize;
  579. bool IboCached = Gpu.Renderer.Rasterizer.IsIboCached(IboKey, (uint)IbSize);
  580. bool UsesLegacyQuads =
  581. PrimType == GalPrimitiveType.Quads ||
  582. PrimType == GalPrimitiveType.QuadStrip;
  583. if (!IboCached || Gpu.ResourceManager.MemoryRegionModified(Vmm, IboKey, (uint)IbSize, NvGpuBufferType.Index))
  584. {
  585. if (!UsesLegacyQuads)
  586. {
  587. if (Vmm.TryGetHostAddress(IbPosition, IbSize, out IntPtr IbPtr))
  588. {
  589. Gpu.Renderer.Rasterizer.CreateIbo(IboKey, IbSize, IbPtr);
  590. }
  591. else
  592. {
  593. Gpu.Renderer.Rasterizer.CreateIbo(IboKey, IbSize, Vmm.ReadBytes(IbPosition, IbSize));
  594. }
  595. }
  596. else
  597. {
  598. byte[] Buffer = Vmm.ReadBytes(IbPosition, IbSize);
  599. if (PrimType == GalPrimitiveType.Quads)
  600. {
  601. Buffer = QuadHelper.ConvertQuadsToTris(Buffer, IndexEntrySize, IndexCount);
  602. }
  603. else /* if (PrimType == GalPrimitiveType.QuadStrip) */
  604. {
  605. Buffer = QuadHelper.ConvertQuadStripToTris(Buffer, IndexEntrySize, IndexCount);
  606. }
  607. Gpu.Renderer.Rasterizer.CreateIbo(IboKey, IbSize, Buffer);
  608. }
  609. }
  610. if (!UsesLegacyQuads)
  611. {
  612. Gpu.Renderer.Rasterizer.SetIndexArray(IbSize, IndexFormat);
  613. }
  614. else
  615. {
  616. if (PrimType == GalPrimitiveType.Quads)
  617. {
  618. Gpu.Renderer.Rasterizer.SetIndexArray(QuadHelper.ConvertSizeQuadsToTris(IbSize), IndexFormat);
  619. }
  620. else /* if (PrimType == GalPrimitiveType.QuadStrip) */
  621. {
  622. Gpu.Renderer.Rasterizer.SetIndexArray(QuadHelper.ConvertSizeQuadStripToTris(IbSize), IndexFormat);
  623. }
  624. }
  625. }
  626. List<GalVertexAttrib>[] Attribs = new List<GalVertexAttrib>[32];
  627. for (int Attr = 0; Attr < 16; Attr++)
  628. {
  629. int Packed = ReadRegister(NvGpuEngine3dReg.VertexAttribNFormat + Attr);
  630. int ArrayIndex = Packed & 0x1f;
  631. if (Attribs[ArrayIndex] == null)
  632. {
  633. Attribs[ArrayIndex] = new List<GalVertexAttrib>();
  634. }
  635. long VbPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + ArrayIndex * 4);
  636. if (VbPosition == 0)
  637. {
  638. continue;
  639. }
  640. bool IsConst = ((Packed >> 6) & 1) != 0;
  641. int Offset = (Packed >> 7) & 0x3fff;
  642. GalVertexAttribSize Size = (GalVertexAttribSize)((Packed >> 21) & 0x3f);
  643. GalVertexAttribType Type = (GalVertexAttribType)((Packed >> 27) & 0x7);
  644. bool IsRgba = ((Packed >> 31) & 1) != 0;
  645. // Check vertex array is enabled to avoid out of bounds exception when reading bytes
  646. bool Enable = (ReadRegister(NvGpuEngine3dReg.VertexArrayNControl + ArrayIndex * 4) & 0x1000) != 0;
  647. //Note: 16 is the maximum size of an attribute,
  648. //having a component size of 32-bits with 4 elements (a vec4).
  649. if (Enable)
  650. {
  651. byte[] Data = Vmm.ReadBytes(VbPosition + Offset, 16);
  652. Attribs[ArrayIndex].Add(new GalVertexAttrib(Attr, IsConst, Offset, Data, Size, Type, IsRgba));
  653. }
  654. }
  655. State.VertexBindings = new GalVertexBinding[32];
  656. for (int Index = 0; Index < 32; Index++)
  657. {
  658. if (Attribs[Index] == null)
  659. {
  660. continue;
  661. }
  662. int Control = ReadRegister(NvGpuEngine3dReg.VertexArrayNControl + Index * 4);
  663. bool Enable = (Control & 0x1000) != 0;
  664. if (!Enable)
  665. {
  666. continue;
  667. }
  668. long VbPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNAddress + Index * 4);
  669. long VbEndPos = MakeInt64From2xInt32(NvGpuEngine3dReg.VertexArrayNEndAddr + Index * 2);
  670. int VertexDivisor = ReadRegister(NvGpuEngine3dReg.VertexArrayNDivisor + Index * 4);
  671. bool Instanced = ReadRegisterBool(NvGpuEngine3dReg.VertexArrayNInstance + Index);
  672. int Stride = Control & 0xfff;
  673. if (Instanced && VertexDivisor != 0)
  674. {
  675. VbPosition += Stride * (CurrentInstance / VertexDivisor);
  676. }
  677. if (VbPosition > VbEndPos)
  678. {
  679. //Instance is invalid, ignore the draw call
  680. continue;
  681. }
  682. long VboKey = Vmm.GetPhysicalAddress(VbPosition);
  683. long VbSize = (VbEndPos - VbPosition) + 1;
  684. int ModifiedVbSize = (int)VbSize;
  685. // If quads convert size to triangle length
  686. if (Stride == 0)
  687. {
  688. if (PrimType == GalPrimitiveType.Quads)
  689. {
  690. ModifiedVbSize = QuadHelper.ConvertSizeQuadsToTris(ModifiedVbSize);
  691. }
  692. else if (PrimType == GalPrimitiveType.QuadStrip)
  693. {
  694. ModifiedVbSize = QuadHelper.ConvertSizeQuadStripToTris(ModifiedVbSize);
  695. }
  696. }
  697. bool VboCached = Gpu.Renderer.Rasterizer.IsVboCached(VboKey, ModifiedVbSize);
  698. if (!VboCached || Gpu.ResourceManager.MemoryRegionModified(Vmm, VboKey, VbSize, NvGpuBufferType.Vertex))
  699. {
  700. if ((PrimType == GalPrimitiveType.Quads | PrimType == GalPrimitiveType.QuadStrip) && Stride != 0)
  701. {
  702. // Convert quad buffer to triangles
  703. byte[] data = Vmm.ReadBytes(VbPosition, VbSize);
  704. if (PrimType == GalPrimitiveType.Quads)
  705. {
  706. data = QuadHelper.ConvertQuadsToTris(data, Stride, (int)(VbSize / Stride));
  707. }
  708. else
  709. {
  710. data = QuadHelper.ConvertQuadStripToTris(data, Stride, (int)(VbSize / Stride));
  711. }
  712. Gpu.Renderer.Rasterizer.CreateVbo(VboKey, data);
  713. }
  714. else if (Vmm.TryGetHostAddress(VbPosition, VbSize, out IntPtr VbPtr))
  715. {
  716. Gpu.Renderer.Rasterizer.CreateVbo(VboKey, (int)VbSize, VbPtr);
  717. }
  718. else
  719. {
  720. Gpu.Renderer.Rasterizer.CreateVbo(VboKey, Vmm.ReadBytes(VbPosition, VbSize));
  721. }
  722. }
  723. State.VertexBindings[Index].Enabled = true;
  724. State.VertexBindings[Index].Stride = Stride;
  725. State.VertexBindings[Index].VboKey = VboKey;
  726. State.VertexBindings[Index].Instanced = Instanced;
  727. State.VertexBindings[Index].Divisor = VertexDivisor;
  728. State.VertexBindings[Index].Attribs = Attribs[Index].ToArray();
  729. }
  730. }
  731. private void DispatchRender(NvGpuVmm Vmm, GalPipelineState State)
  732. {
  733. int IndexCount = ReadRegister(NvGpuEngine3dReg.IndexBatchCount);
  734. int PrimCtrl = ReadRegister(NvGpuEngine3dReg.VertexBeginGl);
  735. GalPrimitiveType PrimType = (GalPrimitiveType)(PrimCtrl & 0xffff);
  736. bool InstanceNext = ((PrimCtrl >> 26) & 1) != 0;
  737. bool InstanceCont = ((PrimCtrl >> 27) & 1) != 0;
  738. if (InstanceNext && InstanceCont)
  739. {
  740. throw new InvalidOperationException("GPU tried to increase and reset instance count at the same time");
  741. }
  742. if (InstanceNext)
  743. {
  744. CurrentInstance++;
  745. }
  746. else if (!InstanceCont)
  747. {
  748. CurrentInstance = 0;
  749. }
  750. State.Instance = CurrentInstance;
  751. Gpu.Renderer.Pipeline.Bind(State);
  752. Gpu.Renderer.RenderTarget.Bind();
  753. if (IndexCount != 0)
  754. {
  755. int IndexEntryFmt = ReadRegister(NvGpuEngine3dReg.IndexArrayFormat);
  756. int IndexFirst = ReadRegister(NvGpuEngine3dReg.IndexBatchFirst);
  757. int VertexBase = ReadRegister(NvGpuEngine3dReg.VertexArrayElemBase);
  758. long IndexPosition = MakeInt64From2xInt32(NvGpuEngine3dReg.IndexArrayAddress);
  759. long IboKey = Vmm.GetPhysicalAddress(IndexPosition);
  760. //Quad primitive types were deprecated on OpenGL 3.x,
  761. //they are converted to a triangles index buffer on IB creation,
  762. //so we should use the triangles type here too.
  763. if (PrimType == GalPrimitiveType.Quads || PrimType == GalPrimitiveType.QuadStrip)
  764. {
  765. //Note: We assume that index first points to the first
  766. //vertex of a quad, if it points to the middle of a
  767. //quad (First % 4 != 0 for Quads) then it will not work properly.
  768. if (PrimType == GalPrimitiveType.Quads)
  769. {
  770. IndexFirst = QuadHelper.ConvertSizeQuadsToTris(IndexFirst);
  771. }
  772. else // QuadStrip
  773. {
  774. IndexFirst = QuadHelper.ConvertSizeQuadStripToTris(IndexFirst);
  775. }
  776. PrimType = GalPrimitiveType.Triangles;
  777. }
  778. Gpu.Renderer.Rasterizer.DrawElements(IboKey, IndexFirst, VertexBase, PrimType);
  779. }
  780. else
  781. {
  782. int VertexFirst = ReadRegister(NvGpuEngine3dReg.VertexArrayFirst);
  783. int VertexCount = ReadRegister(NvGpuEngine3dReg.VertexArrayCount);
  784. //Quad primitive types were deprecated on OpenGL 3.x,
  785. //they are converted to a triangles index buffer on IB creation,
  786. //so we should use the triangles type here too.
  787. if (PrimType == GalPrimitiveType.Quads || PrimType == GalPrimitiveType.QuadStrip)
  788. {
  789. //Note: We assume that index first points to the first
  790. //vertex of a quad, if it points to the middle of a
  791. //quad (First % 4 != 0 for Quads) then it will not work properly.
  792. if (PrimType == GalPrimitiveType.Quads)
  793. {
  794. VertexFirst = QuadHelper.ConvertSizeQuadsToTris(VertexFirst);
  795. }
  796. else // QuadStrip
  797. {
  798. VertexFirst = QuadHelper.ConvertSizeQuadStripToTris(VertexFirst);
  799. }
  800. PrimType = GalPrimitiveType.Triangles;
  801. VertexCount = QuadHelper.ConvertSizeQuadsToTris(VertexCount);
  802. }
  803. Gpu.Renderer.Rasterizer.DrawArrays(VertexFirst, VertexCount, PrimType);
  804. }
  805. // Reset pipeline for host OpenGL calls
  806. Gpu.Renderer.Pipeline.Unbind(State);
  807. //Is the GPU really clearing those registers after draw?
  808. WriteRegister(NvGpuEngine3dReg.IndexBatchFirst, 0);
  809. WriteRegister(NvGpuEngine3dReg.IndexBatchCount, 0);
  810. }
  811. private enum QueryMode
  812. {
  813. WriteSeq,
  814. Sync,
  815. WriteCounterAndTimestamp
  816. }
  817. private void QueryControl(NvGpuVmm Vmm, GpuMethodCall MethCall)
  818. {
  819. WriteRegister(MethCall);
  820. long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.QueryAddress);
  821. int Seq = Registers[(int)NvGpuEngine3dReg.QuerySequence];
  822. int Ctrl = Registers[(int)NvGpuEngine3dReg.QueryControl];
  823. QueryMode Mode = (QueryMode)(Ctrl & 3);
  824. switch (Mode)
  825. {
  826. case QueryMode.WriteSeq: Vmm.WriteInt32(Position, Seq); break;
  827. case QueryMode.WriteCounterAndTimestamp:
  828. {
  829. //TODO: Implement counters.
  830. long Counter = 1;
  831. long Timestamp = PerformanceCounter.ElapsedMilliseconds;
  832. Vmm.WriteInt64(Position + 0, Counter);
  833. Vmm.WriteInt64(Position + 8, Timestamp);
  834. break;
  835. }
  836. }
  837. }
  838. private void CbData(NvGpuVmm Vmm, GpuMethodCall MethCall)
  839. {
  840. long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress);
  841. int Offset = ReadRegister(NvGpuEngine3dReg.ConstBufferOffset);
  842. Vmm.WriteInt32(Position + Offset, MethCall.Argument);
  843. WriteRegister(NvGpuEngine3dReg.ConstBufferOffset, Offset + 4);
  844. Gpu.ResourceManager.ClearPbCache(NvGpuBufferType.ConstBuffer);
  845. }
  846. private void CbBind(NvGpuVmm Vmm, GpuMethodCall MethCall)
  847. {
  848. int Stage = (MethCall.Method - 0x904) >> 3;
  849. int Index = MethCall.Argument;
  850. bool Enabled = (Index & 1) != 0;
  851. Index = (Index >> 4) & 0x1f;
  852. long Position = MakeInt64From2xInt32(NvGpuEngine3dReg.ConstBufferAddress);
  853. long CbKey = Vmm.GetPhysicalAddress(Position);
  854. int Size = ReadRegister(NvGpuEngine3dReg.ConstBufferSize);
  855. if (!Gpu.Renderer.Buffer.IsCached(CbKey, Size))
  856. {
  857. Gpu.Renderer.Buffer.Create(CbKey, Size);
  858. }
  859. ConstBuffer Cb = ConstBuffers[Stage][Index];
  860. if (Cb.Position != Position || Cb.Enabled != Enabled || Cb.Size != Size)
  861. {
  862. ConstBuffers[Stage][Index].Position = Position;
  863. ConstBuffers[Stage][Index].Enabled = Enabled;
  864. ConstBuffers[Stage][Index].Size = Size;
  865. }
  866. }
  867. private float GetFlipSign(NvGpuEngine3dReg Reg)
  868. {
  869. return MathF.Sign(ReadRegisterFloat(Reg));
  870. }
  871. private long MakeInt64From2xInt32(NvGpuEngine3dReg Reg)
  872. {
  873. return
  874. (long)Registers[(int)Reg + 0] << 32 |
  875. (uint)Registers[(int)Reg + 1];
  876. }
  877. private void WriteRegister(GpuMethodCall MethCall)
  878. {
  879. Registers[MethCall.Method] = MethCall.Argument;
  880. }
  881. private int ReadRegister(NvGpuEngine3dReg Reg)
  882. {
  883. return Registers[(int)Reg];
  884. }
  885. private float ReadRegisterFloat(NvGpuEngine3dReg Reg)
  886. {
  887. return BitConverter.Int32BitsToSingle(ReadRegister(Reg));
  888. }
  889. private bool ReadRegisterBool(NvGpuEngine3dReg Reg)
  890. {
  891. return (ReadRegister(Reg) & 1) != 0;
  892. }
  893. private void WriteRegister(NvGpuEngine3dReg Reg, int Value)
  894. {
  895. Registers[(int)Reg] = Value;
  896. }
  897. }
  898. }