Methods.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Gpu.Image;
  4. using Ryujinx.Graphics.Gpu.Memory;
  5. using Ryujinx.Graphics.Gpu.Shader;
  6. using Ryujinx.Graphics.Gpu.State;
  7. using Ryujinx.Graphics.Shader;
  8. using System;
  9. using System.Runtime.InteropServices;
  10. namespace Ryujinx.Graphics.Gpu.Engine
  11. {
  12. using Texture = Image.Texture;
  13. /// <summary>
  14. /// GPU method implementations.
  15. /// </summary>
  16. partial class Methods
  17. {
  18. private readonly GpuContext _context;
  19. private readonly ShaderProgramInfo[] _currentProgramInfo;
  20. /// <summary>
  21. /// In-memory shader cache.
  22. /// </summary>
  23. public ShaderCache ShaderCache { get; }
  24. /// <summary>
  25. /// GPU buffer manager.
  26. /// </summary>
  27. public BufferManager BufferManager { get; }
  28. /// <summary>
  29. /// GPU texture manager.
  30. /// </summary>
  31. public TextureManager TextureManager { get; }
  32. private bool _isAnyVbInstanced;
  33. private bool _vsUsesInstanceId;
  34. /// <summary>
  35. /// Creates a new instance of the GPU methods class.
  36. /// </summary>
  37. /// <param name="context">GPU context</param>
  38. public Methods(GpuContext context)
  39. {
  40. _context = context;
  41. ShaderCache = new ShaderCache(_context);
  42. _currentProgramInfo = new ShaderProgramInfo[Constants.ShaderStages];
  43. BufferManager = new BufferManager(context);
  44. TextureManager = new TextureManager(context);
  45. }
  46. /// <summary>
  47. /// Register callback for GPU method calls that triggers an action on the GPU.
  48. /// </summary>
  49. /// <param name="state">GPU state where the triggers will be registered</param>
  50. public void RegisterCallbacks(GpuState state)
  51. {
  52. state.RegisterCallback(MethodOffset.LaunchDma, LaunchDma);
  53. state.RegisterCallback(MethodOffset.LoadInlineData, LoadInlineData);
  54. state.RegisterCallback(MethodOffset.Dispatch, Dispatch);
  55. state.RegisterCallback(MethodOffset.CopyBuffer, CopyBuffer);
  56. state.RegisterCallback(MethodOffset.CopyTexture, CopyTexture);
  57. state.RegisterCallback(MethodOffset.TextureBarrier, TextureBarrier);
  58. state.RegisterCallback(MethodOffset.InvalidateTextures, InvalidateTextures);
  59. state.RegisterCallback(MethodOffset.TextureBarrierTiled, TextureBarrierTiled);
  60. state.RegisterCallback(MethodOffset.ResetCounter, ResetCounter);
  61. state.RegisterCallback(MethodOffset.DrawEnd, DrawEnd);
  62. state.RegisterCallback(MethodOffset.DrawBegin, DrawBegin);
  63. state.RegisterCallback(MethodOffset.IndexBufferCount, SetIndexBufferCount);
  64. state.RegisterCallback(MethodOffset.Clear, Clear);
  65. state.RegisterCallback(MethodOffset.Report, Report);
  66. state.RegisterCallback(MethodOffset.FirmwareCall4, FirmwareCall4);
  67. state.RegisterCallback(MethodOffset.UniformBufferUpdateData, 16, UniformBufferUpdate);
  68. state.RegisterCallback(MethodOffset.UniformBufferBindVertex, UniformBufferBindVertex);
  69. state.RegisterCallback(MethodOffset.UniformBufferBindTessControl, UniformBufferBindTessControl);
  70. state.RegisterCallback(MethodOffset.UniformBufferBindTessEvaluation, UniformBufferBindTessEvaluation);
  71. state.RegisterCallback(MethodOffset.UniformBufferBindGeometry, UniformBufferBindGeometry);
  72. state.RegisterCallback(MethodOffset.UniformBufferBindFragment, UniformBufferBindFragment);
  73. }
  74. /// <summary>
  75. /// Updates host state based on the current guest GPU state.
  76. /// </summary>
  77. /// <param name="state">Guest GPU state</param>
  78. private void UpdateState(GpuState state)
  79. {
  80. // Shaders must be the first one to be updated if modified, because
  81. // some of the other state depends on information from the currently
  82. // bound shaders.
  83. if (state.QueryModified(MethodOffset.ShaderBaseAddress, MethodOffset.ShaderState))
  84. {
  85. UpdateShaderState(state);
  86. }
  87. if (state.QueryModified(MethodOffset.RasterizeEnable))
  88. {
  89. UpdateRasterizerState(state);
  90. }
  91. if (state.QueryModified(MethodOffset.RtColorState,
  92. MethodOffset.RtDepthStencilState,
  93. MethodOffset.RtControl,
  94. MethodOffset.RtDepthStencilSize,
  95. MethodOffset.RtDepthStencilEnable))
  96. {
  97. UpdateRenderTargetState(state, useControl: true);
  98. }
  99. if (state.QueryModified(MethodOffset.ScissorState))
  100. {
  101. UpdateScissorState(state);
  102. }
  103. if (state.QueryModified(MethodOffset.ViewVolumeClipControl))
  104. {
  105. UpdateDepthClampState(state);
  106. }
  107. if (state.QueryModified(MethodOffset.DepthTestEnable,
  108. MethodOffset.DepthWriteEnable,
  109. MethodOffset.DepthTestFunc))
  110. {
  111. UpdateDepthTestState(state);
  112. }
  113. if (state.QueryModified(MethodOffset.DepthMode,
  114. MethodOffset.ViewportTransform,
  115. MethodOffset.ViewportExtents))
  116. {
  117. UpdateViewportTransform(state);
  118. }
  119. if (state.QueryModified(MethodOffset.DepthBiasState,
  120. MethodOffset.DepthBiasFactor,
  121. MethodOffset.DepthBiasUnits,
  122. MethodOffset.DepthBiasClamp))
  123. {
  124. UpdateDepthBiasState(state);
  125. }
  126. if (state.QueryModified(MethodOffset.StencilBackMasks,
  127. MethodOffset.StencilTestState,
  128. MethodOffset.StencilBackTestState))
  129. {
  130. UpdateStencilTestState(state);
  131. }
  132. // Pools.
  133. if (state.QueryModified(MethodOffset.SamplerPoolState, MethodOffset.SamplerIndex))
  134. {
  135. UpdateSamplerPoolState(state);
  136. }
  137. if (state.QueryModified(MethodOffset.TexturePoolState))
  138. {
  139. UpdateTexturePoolState(state);
  140. }
  141. // Input assembler state.
  142. if (state.QueryModified(MethodOffset.VertexAttribState))
  143. {
  144. UpdateVertexAttribState(state);
  145. }
  146. if (state.QueryModified(MethodOffset.PointSize))
  147. {
  148. UpdatePointSizeState(state);
  149. }
  150. if (state.QueryModified(MethodOffset.PrimitiveRestartState))
  151. {
  152. UpdatePrimitiveRestartState(state);
  153. }
  154. if (state.QueryModified(MethodOffset.IndexBufferState))
  155. {
  156. UpdateIndexBufferState(state);
  157. }
  158. if (state.QueryModified(MethodOffset.VertexBufferDrawState,
  159. MethodOffset.VertexBufferInstanced,
  160. MethodOffset.VertexBufferState,
  161. MethodOffset.VertexBufferEndAddress))
  162. {
  163. UpdateVertexBufferState(state);
  164. }
  165. if (state.QueryModified(MethodOffset.FaceState))
  166. {
  167. UpdateFaceState(state);
  168. }
  169. if (state.QueryModified(MethodOffset.RtColorMaskShared, MethodOffset.RtColorMask))
  170. {
  171. UpdateRtColorMask(state);
  172. }
  173. if (state.QueryModified(MethodOffset.BlendIndependent,
  174. MethodOffset.BlendStateCommon,
  175. MethodOffset.BlendEnableCommon,
  176. MethodOffset.BlendEnable,
  177. MethodOffset.BlendState))
  178. {
  179. UpdateBlendState(state);
  180. }
  181. CommitBindings();
  182. }
  183. /// <summary>
  184. /// Updates Rasterizer primitive discard state based on guest gpu state.
  185. /// </summary>
  186. /// <param name="state">Current GPU state</param>
  187. private void UpdateRasterizerState(GpuState state)
  188. {
  189. Boolean32 enable = state.Get<Boolean32>(MethodOffset.RasterizeEnable);
  190. _context.Renderer.Pipeline.SetRasterizerDiscard(!enable);
  191. }
  192. /// <summary>
  193. /// Ensures that the bindings are visible to the host GPU.
  194. /// Note: this actually performs the binding using the host graphics API.
  195. /// </summary>
  196. private void CommitBindings()
  197. {
  198. UpdateStorageBuffers();
  199. BufferManager.CommitBindings();
  200. TextureManager.CommitGraphicsBindings();
  201. }
  202. /// <summary>
  203. /// Updates storage buffer bindings.
  204. /// </summary>
  205. private void UpdateStorageBuffers()
  206. {
  207. for (int stage = 0; stage < _currentProgramInfo.Length; stage++)
  208. {
  209. ShaderProgramInfo info = _currentProgramInfo[stage];
  210. if (info == null)
  211. {
  212. continue;
  213. }
  214. for (int index = 0; index < info.SBuffers.Count; index++)
  215. {
  216. BufferDescriptor sb = info.SBuffers[index];
  217. ulong sbDescAddress = BufferManager.GetGraphicsUniformBufferAddress(stage, 0);
  218. int sbDescOffset = 0x110 + stage * 0x100 + sb.Slot * 0x10;
  219. sbDescAddress += (ulong)sbDescOffset;
  220. ReadOnlySpan<byte> sbDescriptorData = _context.PhysicalMemory.GetSpan(sbDescAddress, 0x10);
  221. SbDescriptor sbDescriptor = MemoryMarshal.Cast<byte, SbDescriptor>(sbDescriptorData)[0];
  222. BufferManager.SetGraphicsStorageBuffer(stage, sb.Slot, sbDescriptor.PackAddress(), (uint)sbDescriptor.Size);
  223. }
  224. }
  225. }
  226. /// <summary>
  227. /// Updates render targets (color and depth-stencil buffers) based on current render target state.
  228. /// </summary>
  229. /// <param name="state">Current GPU state</param>
  230. /// <param name="useControl">Use draw buffers information from render target control register</param>
  231. private void UpdateRenderTargetState(GpuState state, bool useControl)
  232. {
  233. var rtControl = state.Get<RtControl>(MethodOffset.RtControl);
  234. int count = useControl ? rtControl.UnpackCount() : Constants.TotalRenderTargets;
  235. var msaaMode = state.Get<TextureMsaaMode>(MethodOffset.RtMsaaMode);
  236. int samplesInX = msaaMode.SamplesInX();
  237. int samplesInY = msaaMode.SamplesInY();
  238. for (int index = 0; index < Constants.TotalRenderTargets; index++)
  239. {
  240. int rtIndex = useControl ? rtControl.UnpackPermutationIndex(index) : index;
  241. var colorState = state.Get<RtColorState>(MethodOffset.RtColorState, rtIndex);
  242. if (index >= count || !IsRtEnabled(colorState))
  243. {
  244. TextureManager.SetRenderTargetColor(index, null);
  245. continue;
  246. }
  247. Texture color = TextureManager.FindOrCreateTexture(colorState, samplesInX, samplesInY);
  248. TextureManager.SetRenderTargetColor(index, color);
  249. if (color != null)
  250. {
  251. color.SignalModified();
  252. }
  253. }
  254. bool dsEnable = state.Get<Boolean32>(MethodOffset.RtDepthStencilEnable);
  255. Texture depthStencil = null;
  256. if (dsEnable)
  257. {
  258. var dsState = state.Get<RtDepthStencilState>(MethodOffset.RtDepthStencilState);
  259. var dsSize = state.Get<Size3D> (MethodOffset.RtDepthStencilSize);
  260. depthStencil = TextureManager.FindOrCreateTexture(dsState, dsSize, samplesInX, samplesInY);
  261. }
  262. TextureManager.SetRenderTargetDepthStencil(depthStencil);
  263. if (depthStencil != null)
  264. {
  265. depthStencil.SignalModified();
  266. }
  267. }
  268. /// <summary>
  269. /// Checks if a render target color buffer is used.
  270. /// </summary>
  271. /// <param name="colorState">Color buffer information</param>
  272. /// <returns>True if the specified buffer is enabled/used, false otherwise</returns>
  273. private static bool IsRtEnabled(RtColorState colorState)
  274. {
  275. // Colors are disabled by writing 0 to the format.
  276. return colorState.Format != 0 && colorState.WidthOrStride != 0;
  277. }
  278. /// <summary>
  279. /// Updates host scissor test state based on current GPU state.
  280. /// </summary>
  281. /// <param name="state">Current GPU state</param>
  282. private void UpdateScissorState(GpuState state)
  283. {
  284. for (int index = 0; index < Constants.TotalViewports; index++)
  285. {
  286. ScissorState scissor = state.Get<ScissorState>(MethodOffset.ScissorState, index);
  287. bool enable = scissor.Enable && (scissor.X1 != 0 || scissor.Y1 != 0 || scissor.X2 != 0xffff || scissor.Y2 != 0xffff);
  288. _context.Renderer.Pipeline.SetScissorEnable(index, enable);
  289. if (enable)
  290. {
  291. _context.Renderer.Pipeline.SetScissor(index, scissor.X1, scissor.Y1, scissor.X2 - scissor.X1, scissor.Y2 - scissor.Y1);
  292. }
  293. }
  294. }
  295. /// <summary>
  296. /// Updates host depth clamp state based on current GPU state.
  297. /// </summary>
  298. /// <param name="state">Current GPU state</param>
  299. private void UpdateDepthClampState(GpuState state)
  300. {
  301. ViewVolumeClipControl clip = state.Get<ViewVolumeClipControl>(MethodOffset.ViewVolumeClipControl);
  302. _context.Renderer.Pipeline.SetDepthClamp((clip & ViewVolumeClipControl.DepthClampNear) != 0,
  303. (clip & ViewVolumeClipControl.DepthClampFar) != 0);
  304. }
  305. /// <summary>
  306. /// Updates host depth test state based on current GPU state.
  307. /// </summary>
  308. /// <param name="state">Current GPU state</param>
  309. private void UpdateDepthTestState(GpuState state)
  310. {
  311. _context.Renderer.Pipeline.SetDepthTest(new DepthTestDescriptor(
  312. state.Get<Boolean32>(MethodOffset.DepthTestEnable),
  313. state.Get<Boolean32>(MethodOffset.DepthWriteEnable),
  314. state.Get<CompareOp>(MethodOffset.DepthTestFunc)));
  315. }
  316. /// <summary>
  317. /// Updates host viewport transform and clipping state based on current GPU state.
  318. /// </summary>
  319. /// <param name="state">Current GPU state</param>
  320. private void UpdateViewportTransform(GpuState state)
  321. {
  322. DepthMode depthMode = state.Get<DepthMode>(MethodOffset.DepthMode);
  323. _context.Renderer.Pipeline.SetDepthMode(depthMode);
  324. bool flipY = (state.Get<YControl>(MethodOffset.YControl) & YControl.NegateY) != 0;
  325. float yFlip = flipY ? -1 : 1;
  326. Viewport[] viewports = new Viewport[Constants.TotalViewports];
  327. for (int index = 0; index < Constants.TotalViewports; index++)
  328. {
  329. var transform = state.Get<ViewportTransform>(MethodOffset.ViewportTransform, index);
  330. var extents = state.Get<ViewportExtents> (MethodOffset.ViewportExtents, index);
  331. float x = transform.TranslateX - MathF.Abs(transform.ScaleX);
  332. float y = transform.TranslateY - MathF.Abs(transform.ScaleY);
  333. float width = transform.ScaleX * 2;
  334. float height = transform.ScaleY * 2 * yFlip;
  335. RectangleF region = new RectangleF(x, y, width, height);
  336. viewports[index] = new Viewport(
  337. region,
  338. transform.UnpackSwizzleX(),
  339. transform.UnpackSwizzleY(),
  340. transform.UnpackSwizzleZ(),
  341. transform.UnpackSwizzleW(),
  342. extents.DepthNear,
  343. extents.DepthFar);
  344. }
  345. _context.Renderer.Pipeline.SetViewports(0, viewports);
  346. }
  347. /// <summary>
  348. /// Updates host depth bias (also called polygon offset) state based on current GPU state.
  349. /// </summary>
  350. /// <param name="state">Current GPU state</param>
  351. private void UpdateDepthBiasState(GpuState state)
  352. {
  353. var depthBias = state.Get<DepthBiasState>(MethodOffset.DepthBiasState);
  354. float factor = state.Get<float>(MethodOffset.DepthBiasFactor);
  355. float units = state.Get<float>(MethodOffset.DepthBiasUnits);
  356. float clamp = state.Get<float>(MethodOffset.DepthBiasClamp);
  357. PolygonModeMask enables;
  358. enables = (depthBias.PointEnable ? PolygonModeMask.Point : 0);
  359. enables |= (depthBias.LineEnable ? PolygonModeMask.Line : 0);
  360. enables |= (depthBias.FillEnable ? PolygonModeMask.Fill : 0);
  361. _context.Renderer.Pipeline.SetDepthBias(enables, factor, units, clamp);
  362. }
  363. /// <summary>
  364. /// Updates host stencil test state based on current GPU state.
  365. /// </summary>
  366. /// <param name="state">Current GPU state</param>
  367. private void UpdateStencilTestState(GpuState state)
  368. {
  369. var backMasks = state.Get<StencilBackMasks> (MethodOffset.StencilBackMasks);
  370. var test = state.Get<StencilTestState> (MethodOffset.StencilTestState);
  371. var backTest = state.Get<StencilBackTestState>(MethodOffset.StencilBackTestState);
  372. CompareOp backFunc;
  373. StencilOp backSFail;
  374. StencilOp backDpPass;
  375. StencilOp backDpFail;
  376. int backFuncRef;
  377. int backFuncMask;
  378. int backMask;
  379. if (backTest.TwoSided)
  380. {
  381. backFunc = backTest.BackFunc;
  382. backSFail = backTest.BackSFail;
  383. backDpPass = backTest.BackDpPass;
  384. backDpFail = backTest.BackDpFail;
  385. backFuncRef = backMasks.FuncRef;
  386. backFuncMask = backMasks.FuncMask;
  387. backMask = backMasks.Mask;
  388. }
  389. else
  390. {
  391. backFunc = test.FrontFunc;
  392. backSFail = test.FrontSFail;
  393. backDpPass = test.FrontDpPass;
  394. backDpFail = test.FrontDpFail;
  395. backFuncRef = test.FrontFuncRef;
  396. backFuncMask = test.FrontFuncMask;
  397. backMask = test.FrontMask;
  398. }
  399. _context.Renderer.Pipeline.SetStencilTest(new StencilTestDescriptor(
  400. test.Enable,
  401. test.FrontFunc,
  402. test.FrontSFail,
  403. test.FrontDpPass,
  404. test.FrontDpFail,
  405. test.FrontFuncRef,
  406. test.FrontFuncMask,
  407. test.FrontMask,
  408. backFunc,
  409. backSFail,
  410. backDpPass,
  411. backDpFail,
  412. backFuncRef,
  413. backFuncMask,
  414. backMask));
  415. }
  416. /// <summary>
  417. /// Updates current sampler pool address and size based on guest GPU state.
  418. /// </summary>
  419. /// <param name="state">Current GPU state</param>
  420. private void UpdateSamplerPoolState(GpuState state)
  421. {
  422. var texturePool = state.Get<PoolState>(MethodOffset.TexturePoolState);
  423. var samplerPool = state.Get<PoolState>(MethodOffset.SamplerPoolState);
  424. var samplerIndex = state.Get<SamplerIndex>(MethodOffset.SamplerIndex);
  425. int maximumId = samplerIndex == SamplerIndex.ViaHeaderIndex
  426. ? texturePool.MaximumId
  427. : samplerPool.MaximumId;
  428. TextureManager.SetGraphicsSamplerPool(samplerPool.Address.Pack(), maximumId, samplerIndex);
  429. }
  430. /// <summary>
  431. /// Updates current texture pool address and size based on guest GPU state.
  432. /// </summary>
  433. /// <param name="state">Current GPU state</param>
  434. private void UpdateTexturePoolState(GpuState state)
  435. {
  436. var texturePool = state.Get<PoolState>(MethodOffset.TexturePoolState);
  437. TextureManager.SetGraphicsTexturePool(texturePool.Address.Pack(), texturePool.MaximumId);
  438. TextureManager.SetGraphicsTextureBufferIndex(state.Get<int>(MethodOffset.TextureBufferIndex));
  439. }
  440. /// <summary>
  441. /// Updates host vertex attributes based on guest GPU state.
  442. /// </summary>
  443. /// <param name="state">Current GPU state</param>
  444. private void UpdateVertexAttribState(GpuState state)
  445. {
  446. VertexAttribDescriptor[] vertexAttribs = new VertexAttribDescriptor[16];
  447. for (int index = 0; index < 16; index++)
  448. {
  449. var vertexAttrib = state.Get<VertexAttribState>(MethodOffset.VertexAttribState, index);
  450. if (!FormatTable.TryGetAttribFormat(vertexAttrib.UnpackFormat(), out Format format))
  451. {
  452. Logger.PrintDebug(LogClass.Gpu, $"Invalid attribute format 0x{vertexAttrib.UnpackFormat():X}.");
  453. format = Format.R32G32B32A32Float;
  454. }
  455. vertexAttribs[index] = new VertexAttribDescriptor(
  456. vertexAttrib.UnpackBufferIndex(),
  457. vertexAttrib.UnpackOffset(),
  458. vertexAttrib.UnpackIsConstant(),
  459. format);
  460. }
  461. _context.Renderer.Pipeline.SetVertexAttribs(vertexAttribs);
  462. }
  463. /// <summary>
  464. /// Updates host point size based on guest GPU state.
  465. /// </summary>
  466. /// <param name="state">Current GPU state</param>
  467. private void UpdatePointSizeState(GpuState state)
  468. {
  469. float size = state.Get<float>(MethodOffset.PointSize);
  470. _context.Renderer.Pipeline.SetPointSize(size);
  471. }
  472. /// <summary>
  473. /// Updates host primitive restart based on guest GPU state.
  474. /// </summary>
  475. /// <param name="state">Current GPU state</param>
  476. private void UpdatePrimitiveRestartState(GpuState state)
  477. {
  478. PrimitiveRestartState primitiveRestart = state.Get<PrimitiveRestartState>(MethodOffset.PrimitiveRestartState);
  479. _context.Renderer.Pipeline.SetPrimitiveRestart(
  480. primitiveRestart.Enable,
  481. primitiveRestart.Index);
  482. }
  483. /// <summary>
  484. /// Updates host index buffer binding based on guest GPU state.
  485. /// </summary>
  486. /// <param name="state">Current GPU state</param>
  487. private void UpdateIndexBufferState(GpuState state)
  488. {
  489. var indexBuffer = state.Get<IndexBufferState>(MethodOffset.IndexBufferState);
  490. _firstIndex = indexBuffer.First;
  491. _indexCount = indexBuffer.Count;
  492. if (_indexCount == 0)
  493. {
  494. return;
  495. }
  496. ulong gpuVa = indexBuffer.Address.Pack();
  497. // Do not use the end address to calculate the size, because
  498. // the result may be much larger than the real size of the index buffer.
  499. ulong size = (ulong)(_firstIndex + _indexCount);
  500. switch (indexBuffer.Type)
  501. {
  502. case IndexType.UShort: size *= 2; break;
  503. case IndexType.UInt: size *= 4; break;
  504. }
  505. BufferManager.SetIndexBuffer(gpuVa, size, indexBuffer.Type);
  506. // The index buffer affects the vertex buffer size calculation, we
  507. // need to ensure that they are updated.
  508. UpdateVertexBufferState(state);
  509. }
  510. /// <summary>
  511. /// Updates host vertex buffer bindings based on guest GPU state.
  512. /// </summary>
  513. /// <param name="state">Current GPU state</param>
  514. private void UpdateVertexBufferState(GpuState state)
  515. {
  516. _isAnyVbInstanced = false;
  517. for (int index = 0; index < 16; index++)
  518. {
  519. var vertexBuffer = state.Get<VertexBufferState>(MethodOffset.VertexBufferState, index);
  520. if (!vertexBuffer.UnpackEnable())
  521. {
  522. BufferManager.SetVertexBuffer(index, 0, 0, 0, 0);
  523. continue;
  524. }
  525. GpuVa endAddress = state.Get<GpuVa>(MethodOffset.VertexBufferEndAddress, index);
  526. ulong address = vertexBuffer.Address.Pack();
  527. int stride = vertexBuffer.UnpackStride();
  528. bool instanced = state.Get<Boolean32>(MethodOffset.VertexBufferInstanced + index);
  529. int divisor = instanced ? vertexBuffer.Divisor : 0;
  530. _isAnyVbInstanced |= divisor != 0;
  531. ulong size;
  532. if (_drawIndexed || stride == 0 || instanced)
  533. {
  534. // This size may be (much) larger than the real vertex buffer size.
  535. // Avoid calculating it this way, unless we don't have any other option.
  536. size = endAddress.Pack() - address + 1;
  537. }
  538. else
  539. {
  540. // For non-indexed draws, we can guess the size from the vertex count
  541. // and stride.
  542. int firstInstance = state.Get<int>(MethodOffset.FirstInstance);
  543. var drawState = state.Get<VertexBufferDrawState>(MethodOffset.VertexBufferDrawState);
  544. size = (ulong)((firstInstance + drawState.First + drawState.Count) * stride);
  545. }
  546. BufferManager.SetVertexBuffer(index, address, size, stride, divisor);
  547. }
  548. }
  549. /// <summary>
  550. /// Updates host face culling and orientation based on guest GPU state.
  551. /// </summary>
  552. /// <param name="state">Current GPU state</param>
  553. private void UpdateFaceState(GpuState state)
  554. {
  555. var face = state.Get<FaceState>(MethodOffset.FaceState);
  556. _context.Renderer.Pipeline.SetFaceCulling(face.CullEnable, face.CullFace);
  557. _context.Renderer.Pipeline.SetFrontFace(face.FrontFace);
  558. }
  559. /// <summary>
  560. /// Updates host render target color masks, based on guest GPU state.
  561. /// This defines which color channels are written to each color buffer.
  562. /// </summary>
  563. /// <param name="state">Current GPU state</param>
  564. private void UpdateRtColorMask(GpuState state)
  565. {
  566. bool rtColorMaskShared = state.Get<Boolean32>(MethodOffset.RtColorMaskShared);
  567. uint[] componentMasks = new uint[Constants.TotalRenderTargets];
  568. for (int index = 0; index < Constants.TotalRenderTargets; index++)
  569. {
  570. var colorMask = state.Get<RtColorMask>(MethodOffset.RtColorMask, rtColorMaskShared ? 0 : index);
  571. uint componentMask;
  572. componentMask = (colorMask.UnpackRed() ? 1u : 0u);
  573. componentMask |= (colorMask.UnpackGreen() ? 2u : 0u);
  574. componentMask |= (colorMask.UnpackBlue() ? 4u : 0u);
  575. componentMask |= (colorMask.UnpackAlpha() ? 8u : 0u);
  576. componentMasks[index] = componentMask;
  577. }
  578. _context.Renderer.Pipeline.SetRenderTargetColorMasks(componentMasks);
  579. }
  580. /// <summary>
  581. /// Updates host render target color buffer blending state, based on guest state.
  582. /// </summary>
  583. /// <param name="state">Current GPU state</param>
  584. private void UpdateBlendState(GpuState state)
  585. {
  586. bool blendIndependent = state.Get<Boolean32>(MethodOffset.BlendIndependent);
  587. for (int index = 0; index < 8; index++)
  588. {
  589. BlendDescriptor descriptor;
  590. if (blendIndependent)
  591. {
  592. bool enable = state.Get<Boolean32> (MethodOffset.BlendEnable, index);
  593. var blend = state.Get<BlendState>(MethodOffset.BlendState, index);
  594. descriptor = new BlendDescriptor(
  595. enable,
  596. blend.ColorOp,
  597. blend.ColorSrcFactor,
  598. blend.ColorDstFactor,
  599. blend.AlphaOp,
  600. blend.AlphaSrcFactor,
  601. blend.AlphaDstFactor);
  602. }
  603. else
  604. {
  605. bool enable = state.Get<Boolean32> (MethodOffset.BlendEnable, 0);
  606. var blend = state.Get<BlendStateCommon>(MethodOffset.BlendStateCommon);
  607. descriptor = new BlendDescriptor(
  608. enable,
  609. blend.ColorOp,
  610. blend.ColorSrcFactor,
  611. blend.ColorDstFactor,
  612. blend.AlphaOp,
  613. blend.AlphaSrcFactor,
  614. blend.AlphaDstFactor);
  615. }
  616. _context.Renderer.Pipeline.SetBlendState(index, descriptor);
  617. }
  618. }
  619. /// <summary>
  620. /// Storage buffer address and size information.
  621. /// </summary>
  622. private struct SbDescriptor
  623. {
  624. public uint AddressLow;
  625. public uint AddressHigh;
  626. public int Size;
  627. public int Padding;
  628. public ulong PackAddress()
  629. {
  630. return AddressLow | ((ulong)AddressHigh << 32);
  631. }
  632. }
  633. /// <summary>
  634. /// Updates host shaders based on the guest GPU state.
  635. /// </summary>
  636. /// <param name="state">Current GPU state</param>
  637. private void UpdateShaderState(GpuState state)
  638. {
  639. ShaderAddresses addresses = new ShaderAddresses();
  640. Span<ShaderAddresses> addressesSpan = MemoryMarshal.CreateSpan(ref addresses, 1);
  641. Span<ulong> addressesArray = MemoryMarshal.Cast<ShaderAddresses, ulong>(addressesSpan);
  642. ulong baseAddress = state.Get<GpuVa>(MethodOffset.ShaderBaseAddress).Pack();
  643. for (int index = 0; index < 6; index++)
  644. {
  645. var shader = state.Get<ShaderState>(MethodOffset.ShaderState, index);
  646. if (!shader.UnpackEnable() && index != 1)
  647. {
  648. continue;
  649. }
  650. addressesArray[index] = baseAddress + shader.Offset;
  651. }
  652. GraphicsShader gs = ShaderCache.GetGraphicsShader(state, addresses);
  653. _vsUsesInstanceId = gs.Shaders[0]?.Program.Info.UsesInstanceId ?? false;
  654. for (int stage = 0; stage < Constants.ShaderStages; stage++)
  655. {
  656. ShaderProgramInfo info = gs.Shaders[stage]?.Program.Info;
  657. _currentProgramInfo[stage] = info;
  658. if (info == null)
  659. {
  660. continue;
  661. }
  662. var textureBindings = new TextureBindingInfo[info.Textures.Count];
  663. for (int index = 0; index < info.Textures.Count; index++)
  664. {
  665. var descriptor = info.Textures[index];
  666. Target target = GetTarget(descriptor.Type);
  667. if (descriptor.IsBindless)
  668. {
  669. textureBindings[index] = new TextureBindingInfo(target, descriptor.CbufSlot, descriptor.CbufOffset);
  670. }
  671. else
  672. {
  673. textureBindings[index] = new TextureBindingInfo(target, descriptor.HandleIndex);
  674. }
  675. }
  676. TextureManager.SetGraphicsTextures(stage, textureBindings);
  677. var imageBindings = new TextureBindingInfo[info.Images.Count];
  678. for (int index = 0; index < info.Images.Count; index++)
  679. {
  680. var descriptor = info.Images[index];
  681. Target target = GetTarget(descriptor.Type);
  682. imageBindings[index] = new TextureBindingInfo(target, descriptor.HandleIndex);
  683. }
  684. TextureManager.SetGraphicsImages(stage, imageBindings);
  685. uint sbEnableMask = 0;
  686. uint ubEnableMask = 0;
  687. for (int index = 0; index < info.SBuffers.Count; index++)
  688. {
  689. sbEnableMask |= 1u << info.SBuffers[index].Slot;
  690. }
  691. for (int index = 0; index < info.CBuffers.Count; index++)
  692. {
  693. ubEnableMask |= 1u << info.CBuffers[index].Slot;
  694. }
  695. BufferManager.SetGraphicsStorageBufferEnableMask(stage, sbEnableMask);
  696. BufferManager.SetGraphicsUniformBufferEnableMask(stage, ubEnableMask);
  697. }
  698. _context.Renderer.Pipeline.SetProgram(gs.HostProgram);
  699. }
  700. /// <summary>
  701. /// Gets texture target from a sampler type.
  702. /// </summary>
  703. /// <param name="type">Sampler type</param>
  704. /// <returns>Texture target value</returns>
  705. private static Target GetTarget(SamplerType type)
  706. {
  707. type &= ~(SamplerType.Indexed | SamplerType.Shadow);
  708. switch (type)
  709. {
  710. case SamplerType.Texture1D:
  711. return Target.Texture1D;
  712. case SamplerType.TextureBuffer:
  713. return Target.TextureBuffer;
  714. case SamplerType.Texture1D | SamplerType.Array:
  715. return Target.Texture1DArray;
  716. case SamplerType.Texture2D:
  717. return Target.Texture2D;
  718. case SamplerType.Texture2D | SamplerType.Array:
  719. return Target.Texture2DArray;
  720. case SamplerType.Texture2D | SamplerType.Multisample:
  721. return Target.Texture2DMultisample;
  722. case SamplerType.Texture2D | SamplerType.Multisample | SamplerType.Array:
  723. return Target.Texture2DMultisampleArray;
  724. case SamplerType.Texture3D:
  725. return Target.Texture3D;
  726. case SamplerType.TextureCube:
  727. return Target.Cubemap;
  728. case SamplerType.TextureCube | SamplerType.Array:
  729. return Target.CubemapArray;
  730. }
  731. Logger.PrintWarning(LogClass.Gpu, $"Invalid sampler type \"{type}\".");
  732. return Target.Texture2D;
  733. }
  734. /// <summary>
  735. /// Issues a texture barrier.
  736. /// This waits until previous texture writes from the GPU to finish, before
  737. /// performing new operations with said textures.
  738. /// </summary>
  739. /// <param name="state">Current GPU state (unused)</param>
  740. /// <param name="argument">Method call argument (unused)</param>
  741. private void TextureBarrier(GpuState state, int argument)
  742. {
  743. _context.Renderer.Pipeline.TextureBarrier();
  744. }
  745. /// <summary>
  746. /// Invalidates all modified textures on the cache.
  747. /// </summary>
  748. /// <param name="state">Current GPU state (unused)</param>
  749. /// <param name="argument">Method call argument (unused)</param>
  750. private void InvalidateTextures(GpuState state, int argument)
  751. {
  752. TextureManager.Flush();
  753. }
  754. /// <summary>
  755. /// Issues a texture barrier.
  756. /// This waits until previous texture writes from the GPU to finish, before
  757. /// performing new operations with said textures.
  758. /// This performs a per-tile wait, it is only valid if both the previous write
  759. /// and current access has the same access patterns.
  760. /// This may be faster than the regular barrier on tile-based rasterizers.
  761. /// </summary>
  762. /// <param name="state">Current GPU state (unused)</param>
  763. /// <param name="argument">Method call argument (unused)</param>
  764. private void TextureBarrierTiled(GpuState state, int argument)
  765. {
  766. _context.Renderer.Pipeline.TextureBarrierTiled();
  767. }
  768. }
  769. }