NvGpuEngine3d.cs 43 KB

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