ThreedClass.cs 17 KB

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