NvGpuEngine3d.cs 42 KB

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