ThreedClass.cs 19 KB

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