ThreedClass.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. using Ryujinx.Graphics.Device;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Gpu.Engine.GPFifo;
  4. using Ryujinx.Graphics.Gpu.Engine.InlineToMemory;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Runtime.CompilerServices;
  8. namespace Ryujinx.Graphics.Gpu.Engine.Threed
  9. {
  10. /// <summary>
  11. /// Represents a 3D engine class.
  12. /// </summary>
  13. class ThreedClass : IDeviceState
  14. {
  15. private readonly GpuContext _context;
  16. private readonly GPFifoClass _fifoClass;
  17. private readonly DeviceStateWithShadow<ThreedClassState> _state;
  18. private readonly InlineToMemoryClass _i2mClass;
  19. private readonly DrawManager _drawManager;
  20. private readonly SemaphoreUpdater _semaphoreUpdater;
  21. private readonly ConstantBufferUpdater _cbUpdater;
  22. private readonly StateUpdater _stateUpdater;
  23. /// <summary>
  24. /// Creates a new instance of the 3D engine class.
  25. /// </summary>
  26. /// <param name="context">GPU context</param>
  27. /// <param name="channel">GPU channel</param>
  28. public ThreedClass(GpuContext context, GpuChannel channel, GPFifoClass fifoClass)
  29. {
  30. _context = context;
  31. _fifoClass = fifoClass;
  32. _state = new DeviceStateWithShadow<ThreedClassState>(new Dictionary<string, RwCallback>
  33. {
  34. { nameof(ThreedClassState.LaunchDma), new RwCallback(LaunchDma, null) },
  35. { nameof(ThreedClassState.LoadInlineData), new RwCallback(LoadInlineData, null) },
  36. { nameof(ThreedClassState.SyncpointAction), new RwCallback(IncrementSyncpoint, null) },
  37. { nameof(ThreedClassState.InvalidateSamplerCacheNoWfi), new RwCallback(InvalidateSamplerCacheNoWfi, null) },
  38. { nameof(ThreedClassState.InvalidateTextureHeaderCacheNoWfi), new RwCallback(InvalidateTextureHeaderCacheNoWfi, null) },
  39. { nameof(ThreedClassState.TextureBarrier), new RwCallback(TextureBarrier, null) },
  40. { nameof(ThreedClassState.TextureBarrierTiled), new RwCallback(TextureBarrierTiled, null) },
  41. { nameof(ThreedClassState.DrawTextureSrcY), new RwCallback(DrawTexture, null) },
  42. { nameof(ThreedClassState.VbElementU8), new RwCallback(VbElementU8, null) },
  43. { nameof(ThreedClassState.VbElementU16), new RwCallback(VbElementU16, null) },
  44. { nameof(ThreedClassState.VbElementU32), new RwCallback(VbElementU32, null) },
  45. { nameof(ThreedClassState.ResetCounter), new RwCallback(ResetCounter, null) },
  46. { nameof(ThreedClassState.RenderEnableCondition), new RwCallback(null, Zero) },
  47. { nameof(ThreedClassState.DrawEnd), new RwCallback(DrawEnd, null) },
  48. { nameof(ThreedClassState.DrawBegin), new RwCallback(DrawBegin, null) },
  49. { nameof(ThreedClassState.DrawIndexedSmall), new RwCallback(DrawIndexedSmall, null) },
  50. { nameof(ThreedClassState.DrawIndexedSmall2), new RwCallback(DrawIndexedSmall2, null) },
  51. { nameof(ThreedClassState.DrawIndexedSmallIncInstance), new RwCallback(DrawIndexedSmallIncInstance, null) },
  52. { nameof(ThreedClassState.DrawIndexedSmallIncInstance2), new RwCallback(DrawIndexedSmallIncInstance2, null) },
  53. { nameof(ThreedClassState.IndexBufferCount), new RwCallback(SetIndexBufferCount, null) },
  54. { nameof(ThreedClassState.Clear), new RwCallback(Clear, null) },
  55. { nameof(ThreedClassState.SemaphoreControl), new RwCallback(Report, null) },
  56. { nameof(ThreedClassState.SetFalcon04), new RwCallback(SetFalcon04, null) },
  57. { nameof(ThreedClassState.UniformBufferUpdateData), new RwCallback(ConstantBufferUpdate, null) },
  58. { nameof(ThreedClassState.UniformBufferBindVertex), new RwCallback(ConstantBufferBindVertex, null) },
  59. { nameof(ThreedClassState.UniformBufferBindTessControl), new RwCallback(ConstantBufferBindTessControl, null) },
  60. { nameof(ThreedClassState.UniformBufferBindTessEvaluation), new RwCallback(ConstantBufferBindTessEvaluation, null) },
  61. { nameof(ThreedClassState.UniformBufferBindGeometry), new RwCallback(ConstantBufferBindGeometry, null) },
  62. { nameof(ThreedClassState.UniformBufferBindFragment), new RwCallback(ConstantBufferBindFragment, null) }
  63. });
  64. _i2mClass = new InlineToMemoryClass(context, channel, initializeState: false);
  65. var spec = new SpecializationStateUpdater();
  66. var drawState = new DrawState();
  67. _drawManager = new DrawManager(context, channel, _state, drawState, spec);
  68. _semaphoreUpdater = new SemaphoreUpdater(context, channel, _state);
  69. _cbUpdater = new ConstantBufferUpdater(channel, _state);
  70. _stateUpdater = new StateUpdater(context, channel, _state, drawState, spec);
  71. // This defaults to "always", even without any register write.
  72. // Reads just return 0, regardless of what was set there.
  73. _state.State.RenderEnableCondition = Condition.Always;
  74. }
  75. /// <summary>
  76. /// Reads data from the class registers.
  77. /// </summary>
  78. /// <param name="offset">Register byte offset</param>
  79. /// <returns>Data at the specified offset</returns>
  80. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  81. public int Read(int offset) => _state.Read(offset);
  82. /// <summary>
  83. /// Writes data to the class registers.
  84. /// </summary>
  85. /// <param name="offset">Register byte offset</param>
  86. /// <param name="data">Data to be written</param>
  87. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  88. public void Write(int offset, int data)
  89. {
  90. _state.WriteWithRedundancyCheck(offset, data, out bool valueChanged);
  91. if (valueChanged)
  92. {
  93. _stateUpdater.SetDirty(offset);
  94. }
  95. }
  96. /// <summary>
  97. /// Sets the shadow ram control value of all sub-channels.
  98. /// </summary>
  99. /// <param name="control">New shadow ram control value</param>
  100. public void SetShadowRamControl(int control)
  101. {
  102. _state.State.SetMmeShadowRamControl = (uint)control;
  103. }
  104. /// <summary>
  105. /// Updates current host state for all registers modified since the last call to this method.
  106. /// </summary>
  107. public void UpdateState()
  108. {
  109. _fifoClass.CreatePendingSyncs();
  110. _cbUpdater.FlushUboDirty();
  111. _stateUpdater.Update();
  112. }
  113. /// <summary>
  114. /// Updates current host state for all registers modified since the last call to this method.
  115. /// </summary>
  116. /// <param name="mask">Mask where each bit set indicates that the respective state group index should be checked</param>
  117. public void UpdateState(ulong mask)
  118. {
  119. _stateUpdater.Update(mask);
  120. }
  121. /// <summary>
  122. /// Updates render targets (color and depth-stencil buffers) based on current render target state.
  123. /// </summary>
  124. /// <param name="useControl">Use draw buffers information from render target control register</param>
  125. /// <param name="layered">Indicates if the texture is layered</param>
  126. /// <param name="singleUse">If this is not -1, it indicates that only the given indexed target will be used.</param>
  127. public void UpdateRenderTargetState(bool useControl, bool layered = false, int singleUse = -1)
  128. {
  129. _stateUpdater.UpdateRenderTargetState(useControl, layered, singleUse);
  130. }
  131. /// <summary>
  132. /// Updates scissor based on current render target state.
  133. /// </summary>
  134. public void UpdateScissorState()
  135. {
  136. _stateUpdater.UpdateScissorState();
  137. }
  138. /// <summary>
  139. /// Marks the entire state as dirty, forcing a full host state update before the next draw.
  140. /// </summary>
  141. public void ForceStateDirty()
  142. {
  143. _drawManager.ForceStateDirty();
  144. _stateUpdater.SetAllDirty();
  145. }
  146. /// <summary>
  147. /// Marks the specified register offset as dirty, forcing the associated state to update on the next draw.
  148. /// </summary>
  149. /// <param name="offset">Register offset</param>
  150. public void ForceStateDirty(int offset)
  151. {
  152. _stateUpdater.SetDirty(offset);
  153. }
  154. /// <summary>
  155. /// Forces the shaders to be rebound on the next draw.
  156. /// </summary>
  157. public void ForceShaderUpdate()
  158. {
  159. _stateUpdater.ForceShaderUpdate();
  160. }
  161. /// <summary>
  162. /// Create any syncs from WaitForIdle command that are currently pending.
  163. /// </summary>
  164. public void CreatePendingSyncs()
  165. {
  166. _fifoClass.CreatePendingSyncs();
  167. }
  168. /// <summary>
  169. /// Flushes any queued UBO updates.
  170. /// </summary>
  171. public void FlushUboDirty()
  172. {
  173. _cbUpdater.FlushUboDirty();
  174. }
  175. /// <summary>
  176. /// Perform any deferred draws.
  177. /// </summary>
  178. public void PerformDeferredDraws()
  179. {
  180. _drawManager.PerformDeferredDraws();
  181. }
  182. /// <summary>
  183. /// Updates the currently bound constant buffer.
  184. /// </summary>
  185. /// <param name="data">Data to be written to the buffer</param>
  186. public void ConstantBufferUpdate(ReadOnlySpan<int> data)
  187. {
  188. _cbUpdater.Update(data);
  189. }
  190. /// <summary>
  191. /// Launches the Inline-to-Memory DMA copy operation.
  192. /// </summary>
  193. /// <param name="argument">Method call argument</param>
  194. private void LaunchDma(int argument)
  195. {
  196. _i2mClass.LaunchDma(ref Unsafe.As<ThreedClassState, InlineToMemoryClassState>(ref _state.State), argument);
  197. }
  198. /// <summary>
  199. /// Pushes a block of data to the Inline-to-Memory engine.
  200. /// </summary>
  201. /// <param name="data">Data to push</param>
  202. public void LoadInlineData(ReadOnlySpan<int> data)
  203. {
  204. _i2mClass.LoadInlineData(data);
  205. }
  206. /// <summary>
  207. /// Pushes a word of data to the Inline-to-Memory engine.
  208. /// </summary>
  209. /// <param name="argument">Method call argument</param>
  210. private void LoadInlineData(int argument)
  211. {
  212. _i2mClass.LoadInlineData(argument);
  213. }
  214. /// <summary>
  215. /// Performs an incrementation on a syncpoint.
  216. /// </summary>
  217. /// <param name="argument">Method call argument</param>
  218. public void IncrementSyncpoint(int argument)
  219. {
  220. uint syncpointId = (uint)argument & 0xFFFF;
  221. _context.AdvanceSequence();
  222. _context.CreateHostSyncIfNeeded(true);
  223. _context.Renderer.UpdateCounters(); // Poll the query counters, the game may want an updated result.
  224. _context.Synchronization.IncrementSyncpoint(syncpointId);
  225. }
  226. /// <summary>
  227. /// Invalidates the cache with the sampler descriptors from the sampler pool.
  228. /// </summary>
  229. /// <param name="argument">Method call argument (unused)</param>
  230. private void InvalidateSamplerCacheNoWfi(int argument)
  231. {
  232. _context.AdvanceSequence();
  233. }
  234. /// <summary>
  235. /// Invalidates the cache with the texture descriptors from the texture pool.
  236. /// </summary>
  237. /// <param name="argument">Method call argument (unused)</param>
  238. private void InvalidateTextureHeaderCacheNoWfi(int argument)
  239. {
  240. _context.AdvanceSequence();
  241. }
  242. /// <summary>
  243. /// Issues a texture barrier.
  244. /// This waits until previous texture writes from the GPU to finish, before
  245. /// performing new operations with said textures.
  246. /// </summary>
  247. /// <param name="argument">Method call argument (unused)</param>
  248. private void TextureBarrier(int argument)
  249. {
  250. _context.Renderer.Pipeline.TextureBarrier();
  251. }
  252. /// <summary>
  253. /// Issues a texture barrier.
  254. /// This waits until previous texture writes from the GPU to finish, before
  255. /// performing new operations with said textures.
  256. /// This performs a per-tile wait, it is only valid if both the previous write
  257. /// and current access has the same access patterns.
  258. /// This may be faster than the regular barrier on tile-based rasterizers.
  259. /// </summary>
  260. /// <param name="argument">Method call argument (unused)</param>
  261. private void TextureBarrierTiled(int argument)
  262. {
  263. _context.Renderer.Pipeline.TextureBarrierTiled();
  264. }
  265. /// <summary>
  266. /// Draws a texture, without needing to specify shader programs.
  267. /// </summary>
  268. /// <param name="argument">Method call argument</param>
  269. private void DrawTexture(int argument)
  270. {
  271. _drawManager.DrawTexture(this, argument);
  272. }
  273. /// <summary>
  274. /// Pushes four 8-bit index buffer elements.
  275. /// </summary>
  276. /// <param name="argument">Method call argument</param>
  277. private void VbElementU8(int argument)
  278. {
  279. _drawManager.VbElementU8(argument);
  280. }
  281. /// <summary>
  282. /// Pushes two 16-bit index buffer elements.
  283. /// </summary>
  284. /// <param name="argument">Method call argument</param>
  285. private void VbElementU16(int argument)
  286. {
  287. _drawManager.VbElementU16(argument);
  288. }
  289. /// <summary>
  290. /// Pushes one 32-bit index buffer element.
  291. /// </summary>
  292. /// <param name="argument">Method call argument</param>
  293. private void VbElementU32(int argument)
  294. {
  295. _drawManager.VbElementU32(argument);
  296. }
  297. /// <summary>
  298. /// Resets the value of an internal GPU counter back to zero.
  299. /// </summary>
  300. /// <param name="argument">Method call argument</param>
  301. private void ResetCounter(int argument)
  302. {
  303. _semaphoreUpdater.ResetCounter(argument);
  304. }
  305. /// <summary>
  306. /// Finishes the draw call.
  307. /// This draws geometry on the bound buffers based on the current GPU state.
  308. /// </summary>
  309. /// <param name="argument">Method call argument</param>
  310. private void DrawEnd(int argument)
  311. {
  312. _drawManager.DrawEnd(this, argument);
  313. }
  314. /// <summary>
  315. /// Starts draw.
  316. /// This sets primitive type and instanced draw parameters.
  317. /// </summary>
  318. /// <param name="argument">Method call argument</param>
  319. private void DrawBegin(int argument)
  320. {
  321. _drawManager.DrawBegin(argument);
  322. }
  323. /// <summary>
  324. /// Sets the index buffer count.
  325. /// This also sets internal state that indicates that the next draw is an indexed draw.
  326. /// </summary>
  327. /// <param name="argument">Method call argument</param>
  328. private void SetIndexBufferCount(int argument)
  329. {
  330. _drawManager.SetIndexBufferCount(argument);
  331. }
  332. /// <summary>
  333. /// Performs a indexed draw with a low number of index buffer elements.
  334. /// </summary>
  335. /// <param name="argument">Method call argument</param>
  336. private void DrawIndexedSmall(int argument)
  337. {
  338. _drawManager.DrawIndexedSmall(this, argument);
  339. }
  340. /// <summary>
  341. /// Performs a indexed draw with a low number of index buffer elements.
  342. /// </summary>
  343. /// <param name="argument">Method call argument</param>
  344. private void DrawIndexedSmall2(int argument)
  345. {
  346. _drawManager.DrawIndexedSmall2(this, argument);
  347. }
  348. /// <summary>
  349. /// Performs a indexed draw with a low number of index buffer elements,
  350. /// while also pre-incrementing the current instance value.
  351. /// </summary>
  352. /// <param name="argument">Method call argument</param>
  353. private void DrawIndexedSmallIncInstance(int argument)
  354. {
  355. _drawManager.DrawIndexedSmallIncInstance(this, argument);
  356. }
  357. /// <summary>
  358. /// Performs a indexed draw with a low number of index buffer elements,
  359. /// while also pre-incrementing the current instance value.
  360. /// </summary>
  361. /// <param name="argument">Method call argument</param>
  362. private void DrawIndexedSmallIncInstance2(int argument)
  363. {
  364. _drawManager.DrawIndexedSmallIncInstance2(this, argument);
  365. }
  366. /// <summary>
  367. /// Clears the current color and depth-stencil buffers.
  368. /// Which buffers should be cleared is also specified on the argument.
  369. /// </summary>
  370. /// <param name="argument">Method call argument</param>
  371. private void Clear(int argument)
  372. {
  373. _drawManager.Clear(this, argument);
  374. }
  375. /// <summary>
  376. /// Writes a GPU counter to guest memory.
  377. /// </summary>
  378. /// <param name="argument">Method call argument</param>
  379. private void Report(int argument)
  380. {
  381. _semaphoreUpdater.Report(argument);
  382. }
  383. /// <summary>
  384. /// Performs high-level emulation of Falcon microcode function number "4".
  385. /// </summary>
  386. /// <param name="argument">Method call argument</param>
  387. private void SetFalcon04(int argument)
  388. {
  389. _state.State.SetMmeShadowScratch[0] = 1;
  390. }
  391. /// <summary>
  392. /// Updates the uniform buffer data with inline data.
  393. /// </summary>
  394. /// <param name="argument">New uniform buffer data word</param>
  395. private void ConstantBufferUpdate(int argument)
  396. {
  397. _cbUpdater.Update(argument);
  398. }
  399. /// <summary>
  400. /// Binds a uniform buffer for the vertex shader stage.
  401. /// </summary>
  402. /// <param name="argument">Method call argument</param>
  403. private void ConstantBufferBindVertex(int argument)
  404. {
  405. _cbUpdater.BindVertex(argument);
  406. }
  407. /// <summary>
  408. /// Binds a uniform buffer for the tessellation control shader stage.
  409. /// </summary>
  410. /// <param name="argument">Method call argument</param>
  411. private void ConstantBufferBindTessControl(int argument)
  412. {
  413. _cbUpdater.BindTessControl(argument);
  414. }
  415. /// <summary>
  416. /// Binds a uniform buffer for the tessellation evaluation shader stage.
  417. /// </summary>
  418. /// <param name="argument">Method call argument</param>
  419. private void ConstantBufferBindTessEvaluation(int argument)
  420. {
  421. _cbUpdater.BindTessEvaluation(argument);
  422. }
  423. /// <summary>
  424. /// Binds a uniform buffer for the geometry shader stage.
  425. /// </summary>
  426. /// <param name="argument">Method call argument</param>
  427. private void ConstantBufferBindGeometry(int argument)
  428. {
  429. _cbUpdater.BindGeometry(argument);
  430. }
  431. /// <summary>
  432. /// Binds a uniform buffer for the fragment shader stage.
  433. /// </summary>
  434. /// <param name="argument">Method call argument</param>
  435. private void ConstantBufferBindFragment(int argument)
  436. {
  437. _cbUpdater.BindFragment(argument);
  438. }
  439. /// <summary>
  440. /// Generic register read function that just returns 0.
  441. /// </summary>
  442. /// <returns>Zero</returns>
  443. private static int Zero()
  444. {
  445. return 0;
  446. }
  447. /// <summary>
  448. /// Performs a indexed or non-indexed draw.
  449. /// </summary>
  450. /// <param name="topology">Primitive topology</param>
  451. /// <param name="count">Index count for indexed draws, vertex count for non-indexed draws</param>
  452. /// <param name="instanceCount">Instance count</param>
  453. /// <param name="firstIndex">First index on the index buffer for indexed draws, ignored for non-indexed draws</param>
  454. /// <param name="firstVertex">First vertex on the vertex buffer</param>
  455. /// <param name="firstInstance">First instance</param>
  456. /// <param name="indexed">True if the draw is indexed, false otherwise</param>
  457. public void Draw(
  458. PrimitiveTopology topology,
  459. int count,
  460. int instanceCount,
  461. int firstIndex,
  462. int firstVertex,
  463. int firstInstance,
  464. bool indexed)
  465. {
  466. _drawManager.Draw(this, topology, count, instanceCount, firstIndex, firstVertex, firstInstance, indexed);
  467. }
  468. /// <summary>
  469. /// Performs a indirect draw, with parameters from a GPU buffer.
  470. /// </summary>
  471. /// <param name="topology">Primitive topology</param>
  472. /// <param name="indirectBufferAddress">Address of the buffer with the draw parameters, such as count, first index, etc</param>
  473. /// <param name="parameterBufferAddress">Address of the buffer with the draw count</param>
  474. /// <param name="maxDrawCount">Maximum number of draws that can be made</param>
  475. /// <param name="stride">Distance in bytes between each entry on the data pointed to by <paramref name="indirectBufferAddress"/></param>
  476. /// <param name="indexCount">Maximum number of indices that the draw can consume</param>
  477. /// <param name="drawType">Type of the indirect draw, which can be indexed or non-indexed, with or without a draw count</param>
  478. public void DrawIndirect(
  479. PrimitiveTopology topology,
  480. ulong indirectBufferAddress,
  481. ulong parameterBufferAddress,
  482. int maxDrawCount,
  483. int stride,
  484. int indexCount,
  485. IndirectDrawType drawType)
  486. {
  487. _drawManager.DrawIndirect(this, topology, indirectBufferAddress, parameterBufferAddress, maxDrawCount, stride, indexCount, drawType);
  488. }
  489. /// <summary>
  490. /// Clears the current color and depth-stencil buffers.
  491. /// Which buffers should be cleared can also specified with the arguments.
  492. /// </summary>
  493. /// <param name="argument">Method call argument</param>
  494. /// <param name="layerCount">For array and 3D textures, indicates how many layers should be cleared</param>
  495. public void Clear(int argument, int layerCount)
  496. {
  497. _drawManager.Clear(this, argument, layerCount);
  498. }
  499. }
  500. }