NvGpuEngine3d.cs 42 KB

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