ThreedClass.cs 21 KB

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