ThreedClass.cs 20 KB

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