NvGpuEngine3d.cs 45 KB

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