NvGpuEngine3d.cs 45 KB

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