ThreedClass.cs 18 KB

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