DrawManager.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.Gpu.Engine.Types;
  3. using Ryujinx.Graphics.Gpu.Memory;
  4. using System;
  5. namespace Ryujinx.Graphics.Gpu.Engine.Threed
  6. {
  7. /// <summary>
  8. /// Draw manager.
  9. /// </summary>
  10. class DrawManager
  11. {
  12. // Since we don't know the index buffer size for indirect draws,
  13. // we must assume a minimum and maximum size and use that for buffer data update purposes.
  14. private const int MinIndirectIndexCount = 0x10000;
  15. private const int MaxIndirectIndexCount = 0x4000000;
  16. private readonly GpuContext _context;
  17. private readonly GpuChannel _channel;
  18. private readonly DeviceStateWithShadow<ThreedClassState> _state;
  19. private readonly DrawState _drawState;
  20. private readonly SpecializationStateUpdater _currentSpecState;
  21. private bool _topologySet;
  22. private bool _instancedDrawPending;
  23. private bool _instancedIndexed;
  24. private bool _instancedIndexedInline;
  25. private int _instancedFirstIndex;
  26. private int _instancedFirstVertex;
  27. private int _instancedFirstInstance;
  28. private int _instancedIndexCount;
  29. private int _instancedDrawStateFirst;
  30. private int _instancedDrawStateCount;
  31. private int _instanceIndex;
  32. private const int VertexBufferFirstMethodOffset = 0x35d;
  33. private const int IndexBufferCountMethodOffset = 0x5f8;
  34. /// <summary>
  35. /// Creates a new instance of the draw manager.
  36. /// </summary>
  37. /// <param name="context">GPU context</param>
  38. /// <param name="channel">GPU channel</param>
  39. /// <param name="state">Channel state</param>
  40. /// <param name="drawState">Draw state</param>
  41. /// <param name="spec">Specialization state updater</param>
  42. public DrawManager(GpuContext context, GpuChannel channel, DeviceStateWithShadow<ThreedClassState> state, DrawState drawState, SpecializationStateUpdater spec)
  43. {
  44. _context = context;
  45. _channel = channel;
  46. _state = state;
  47. _drawState = drawState;
  48. _currentSpecState = spec;
  49. }
  50. /// <summary>
  51. /// Marks the entire state as dirty, forcing a full host state update before the next draw.
  52. /// </summary>
  53. public void ForceStateDirty()
  54. {
  55. _topologySet = false;
  56. }
  57. /// <summary>
  58. /// Pushes four 8-bit index buffer elements.
  59. /// </summary>
  60. /// <param name="argument">Method call argument</param>
  61. public void VbElementU8(int argument)
  62. {
  63. _drawState.IbStreamer.VbElementU8(_context.Renderer, argument);
  64. }
  65. /// <summary>
  66. /// Pushes two 16-bit index buffer elements.
  67. /// </summary>
  68. /// <param name="argument">Method call argument</param>
  69. public void VbElementU16(int argument)
  70. {
  71. _drawState.IbStreamer.VbElementU16(_context.Renderer, argument);
  72. }
  73. /// <summary>
  74. /// Pushes one 32-bit index buffer element.
  75. /// </summary>
  76. /// <param name="argument">Method call argument</param>
  77. public void VbElementU32(int argument)
  78. {
  79. _drawState.IbStreamer.VbElementU32(_context.Renderer, argument);
  80. }
  81. /// <summary>
  82. /// Finishes the draw call.
  83. /// This draws geometry on the bound buffers based on the current GPU state.
  84. /// </summary>
  85. /// <param name="engine">3D engine where this method is being called</param>
  86. /// <param name="argument">Method call argument</param>
  87. public void DrawEnd(ThreedClass engine, int argument)
  88. {
  89. DrawEnd(
  90. engine,
  91. _state.State.IndexBufferState.First,
  92. (int)_state.State.IndexBufferCount,
  93. _state.State.VertexBufferDrawState.First,
  94. _state.State.VertexBufferDrawState.Count);
  95. }
  96. /// <summary>
  97. /// Finishes the draw call.
  98. /// This draws geometry on the bound buffers based on the current GPU state.
  99. /// </summary>
  100. /// <param name="engine">3D engine where this method is being called</param>
  101. /// <param name="firstIndex">Index of the first index buffer element used on the draw</param>
  102. /// <param name="indexCount">Number of index buffer elements used on the draw</param>
  103. /// <param name="drawFirstVertex">Index of the first vertex used on the draw</param>
  104. /// <param name="drawVertexCount">Number of vertices used on the draw</param>
  105. private void DrawEnd(ThreedClass engine, int firstIndex, int indexCount, int drawFirstVertex, int drawVertexCount)
  106. {
  107. ConditionalRenderEnabled renderEnable = ConditionalRendering.GetRenderEnable(
  108. _context,
  109. _channel.MemoryManager,
  110. _state.State.RenderEnableAddress,
  111. _state.State.RenderEnableCondition);
  112. if (renderEnable == ConditionalRenderEnabled.False || _instancedDrawPending)
  113. {
  114. if (renderEnable == ConditionalRenderEnabled.False)
  115. {
  116. PerformDeferredDraws();
  117. }
  118. _drawState.DrawIndexed = false;
  119. if (renderEnable == ConditionalRenderEnabled.Host)
  120. {
  121. _context.Renderer.Pipeline.EndHostConditionalRendering();
  122. }
  123. return;
  124. }
  125. _drawState.FirstIndex = firstIndex;
  126. _drawState.IndexCount = indexCount;
  127. _currentSpecState.SetHasConstantBufferDrawParameters(false);
  128. engine.UpdateState();
  129. bool instanced = _drawState.VsUsesInstanceId || _drawState.IsAnyVbInstanced;
  130. if (instanced)
  131. {
  132. _instancedDrawPending = true;
  133. int ibCount = _drawState.IbStreamer.InlineIndexCount;
  134. _instancedIndexed = _drawState.DrawIndexed;
  135. _instancedIndexedInline = ibCount != 0;
  136. _instancedFirstIndex = firstIndex;
  137. _instancedFirstVertex = (int)_state.State.FirstVertex;
  138. _instancedFirstInstance = (int)_state.State.FirstInstance;
  139. _instancedIndexCount = ibCount != 0 ? ibCount : indexCount;
  140. var drawState = _state.State.VertexBufferDrawState;
  141. _instancedDrawStateFirst = drawState.First;
  142. _instancedDrawStateCount = drawState.Count;
  143. _drawState.DrawIndexed = false;
  144. if (renderEnable == ConditionalRenderEnabled.Host)
  145. {
  146. _context.Renderer.Pipeline.EndHostConditionalRendering();
  147. }
  148. return;
  149. }
  150. int firstInstance = (int)_state.State.FirstInstance;
  151. int inlineIndexCount = _drawState.IbStreamer.GetAndResetInlineIndexCount();
  152. if (inlineIndexCount != 0)
  153. {
  154. int firstVertex = (int)_state.State.FirstVertex;
  155. BufferRange br = new BufferRange(_drawState.IbStreamer.GetInlineIndexBuffer(), 0, inlineIndexCount * 4);
  156. _channel.BufferManager.SetIndexBuffer(br, IndexType.UInt);
  157. _context.Renderer.Pipeline.DrawIndexed(inlineIndexCount, 1, firstIndex, firstVertex, firstInstance);
  158. }
  159. else if (_drawState.DrawIndexed)
  160. {
  161. int firstVertex = (int)_state.State.FirstVertex;
  162. _context.Renderer.Pipeline.DrawIndexed(indexCount, 1, firstIndex, firstVertex, firstInstance);
  163. }
  164. else
  165. {
  166. var drawState = _state.State.VertexBufferDrawState;
  167. _context.Renderer.Pipeline.Draw(drawVertexCount, 1, drawFirstVertex, firstInstance);
  168. }
  169. _drawState.DrawIndexed = false;
  170. if (renderEnable == ConditionalRenderEnabled.Host)
  171. {
  172. _context.Renderer.Pipeline.EndHostConditionalRendering();
  173. }
  174. }
  175. /// <summary>
  176. /// Starts draw.
  177. /// This sets primitive type and instanced draw parameters.
  178. /// </summary>
  179. /// <param name="argument">Method call argument</param>
  180. public void DrawBegin(int argument)
  181. {
  182. bool incrementInstance = (argument & (1 << 26)) != 0;
  183. bool resetInstance = (argument & (1 << 27)) == 0;
  184. PrimitiveType type = (PrimitiveType)(argument & 0xffff);
  185. DrawBegin(incrementInstance, resetInstance, type);
  186. }
  187. /// <summary>
  188. /// Starts draw.
  189. /// This sets primitive type and instanced draw parameters.
  190. /// </summary>
  191. /// <param name="incrementInstance">Indicates if the current instance should be incremented</param>
  192. /// <param name="resetInstance">Indicates if the current instance should be set to zero</param>
  193. /// <param name="primitiveType">Primitive type</param>
  194. private void DrawBegin(bool incrementInstance, bool resetInstance, PrimitiveType primitiveType)
  195. {
  196. if (incrementInstance)
  197. {
  198. _instanceIndex++;
  199. }
  200. else if (resetInstance)
  201. {
  202. PerformDeferredDraws();
  203. _instanceIndex = 0;
  204. }
  205. PrimitiveTopology topology;
  206. if (_state.State.PrimitiveTypeOverrideEnable)
  207. {
  208. PrimitiveTypeOverride typeOverride = _state.State.PrimitiveTypeOverride;
  209. topology = typeOverride.Convert();
  210. }
  211. else
  212. {
  213. topology = primitiveType.Convert();
  214. }
  215. UpdateTopology(topology);
  216. }
  217. /// <summary>
  218. /// Updates the current primitive topology if needed.
  219. /// </summary>
  220. /// <param name="topology">New primitive topology</param>
  221. private void UpdateTopology(PrimitiveTopology topology)
  222. {
  223. if (_drawState.Topology != topology || !_topologySet)
  224. {
  225. _context.Renderer.Pipeline.SetPrimitiveTopology(topology);
  226. _currentSpecState.SetTopology(topology);
  227. _drawState.Topology = topology;
  228. _topologySet = true;
  229. }
  230. }
  231. /// <summary>
  232. /// Sets the index buffer count.
  233. /// This also sets internal state that indicates that the next draw is an indexed draw.
  234. /// </summary>
  235. /// <param name="argument">Method call argument</param>
  236. public void SetIndexBufferCount(int argument)
  237. {
  238. _drawState.DrawIndexed = true;
  239. }
  240. // TODO: Verify if the index type is implied from the method that is called,
  241. // or if it uses the state index type on hardware.
  242. /// <summary>
  243. /// Performs a indexed draw with 8-bit index buffer elements.
  244. /// </summary>
  245. /// <param name="engine">3D engine where this method is being called</param>
  246. /// <param name="argument">Method call argument</param>
  247. public void DrawIndexBuffer8BeginEndInstanceFirst(ThreedClass engine, int argument)
  248. {
  249. DrawIndexBufferBeginEndInstance(engine, argument, false);
  250. }
  251. /// <summary>
  252. /// Performs a indexed draw with 16-bit index buffer elements.
  253. /// </summary>
  254. /// <param name="engine">3D engine where this method is being called</param>
  255. /// <param name="argument">Method call argument</param>
  256. public void DrawIndexBuffer16BeginEndInstanceFirst(ThreedClass engine, int argument)
  257. {
  258. DrawIndexBufferBeginEndInstance(engine, argument, false);
  259. }
  260. /// <summary>
  261. /// Performs a indexed draw with 32-bit index buffer elements.
  262. /// </summary>
  263. /// <param name="engine">3D engine where this method is being called</param>
  264. /// <param name="argument">Method call argument</param>
  265. public void DrawIndexBuffer32BeginEndInstanceFirst(ThreedClass engine, int argument)
  266. {
  267. DrawIndexBufferBeginEndInstance(engine, argument, false);
  268. }
  269. /// <summary>
  270. /// Performs a indexed draw with 8-bit index buffer elements,
  271. /// while also pre-incrementing the current instance value.
  272. /// </summary>
  273. /// <param name="engine">3D engine where this method is being called</param>
  274. /// <param name="argument">Method call argument</param>
  275. public void DrawIndexBuffer8BeginEndInstanceSubsequent(ThreedClass engine, int argument)
  276. {
  277. DrawIndexBufferBeginEndInstance(engine, argument, true);
  278. }
  279. /// <summary>
  280. /// Performs a indexed draw with 16-bit index buffer elements,
  281. /// while also pre-incrementing the current instance value.
  282. /// </summary>
  283. /// <param name="engine">3D engine where this method is being called</param>
  284. /// <param name="argument">Method call argument</param>
  285. public void DrawIndexBuffer16BeginEndInstanceSubsequent(ThreedClass engine, int argument)
  286. {
  287. DrawIndexBufferBeginEndInstance(engine, argument, true);
  288. }
  289. /// <summary>
  290. /// Performs a indexed draw with 32-bit index buffer elements,
  291. /// while also pre-incrementing the current instance value.
  292. /// </summary>
  293. /// <param name="engine">3D engine where this method is being called</param>
  294. /// <param name="argument">Method call argument</param>
  295. public void DrawIndexBuffer32BeginEndInstanceSubsequent(ThreedClass engine, int argument)
  296. {
  297. DrawIndexBufferBeginEndInstance(engine, argument, true);
  298. }
  299. /// <summary>
  300. /// Performs a indexed draw with a low number of index buffer elements,
  301. /// while optionally also pre-incrementing the current instance value.
  302. /// </summary>
  303. /// <param name="engine">3D engine where this method is being called</param>
  304. /// <param name="argument">Method call argument</param>
  305. /// <param name="instanced">True to increment the current instance value, false otherwise</param>
  306. private void DrawIndexBufferBeginEndInstance(ThreedClass engine, int argument, bool instanced)
  307. {
  308. DrawBegin(instanced, !instanced, (PrimitiveType)((argument >> 28) & 0xf));
  309. int firstIndex = argument & 0xffff;
  310. int indexCount = (argument >> 16) & 0xfff;
  311. bool oldDrawIndexed = _drawState.DrawIndexed;
  312. _drawState.DrawIndexed = true;
  313. engine.ForceStateDirty(IndexBufferCountMethodOffset * 4);
  314. DrawEnd(engine, firstIndex, indexCount, 0, 0);
  315. _drawState.DrawIndexed = oldDrawIndexed;
  316. }
  317. /// <summary>
  318. /// Performs a non-indexed draw with the specified topology, index and count.
  319. /// </summary>
  320. /// <param name="engine">3D engine where this method is being called</param>
  321. /// <param name="argument">Method call argument</param>
  322. public void DrawVertexArrayBeginEndInstanceFirst(ThreedClass engine, int argument)
  323. {
  324. DrawVertexArrayBeginEndInstance(engine, argument, false);
  325. }
  326. /// <summary>
  327. /// Performs a non-indexed draw with the specified topology, index and count,
  328. /// while incrementing the current instance.
  329. /// </summary>
  330. /// <param name="engine">3D engine where this method is being called</param>
  331. /// <param name="argument">Method call argument</param>
  332. public void DrawVertexArrayBeginEndInstanceSubsequent(ThreedClass engine, int argument)
  333. {
  334. DrawVertexArrayBeginEndInstance(engine, argument, true);
  335. }
  336. /// <summary>
  337. /// Performs a indexed draw with a low number of index buffer elements,
  338. /// while optionally also pre-incrementing the current instance value.
  339. /// </summary>
  340. /// <param name="engine">3D engine where this method is being called</param>
  341. /// <param name="argument">Method call argument</param>
  342. /// <param name="instanced">True to increment the current instance value, false otherwise</param>
  343. private void DrawVertexArrayBeginEndInstance(ThreedClass engine, int argument, bool instanced)
  344. {
  345. DrawBegin(instanced, !instanced, (PrimitiveType)((argument >> 28) & 0xf));
  346. int firstVertex = argument & 0xffff;
  347. int vertexCount = (argument >> 16) & 0xfff;
  348. bool oldDrawIndexed = _drawState.DrawIndexed;
  349. _drawState.DrawIndexed = false;
  350. DrawEnd(engine, 0, 0, firstVertex, vertexCount);
  351. _drawState.DrawIndexed = oldDrawIndexed;
  352. }
  353. /// <summary>
  354. /// Performs a texture draw with a source texture and sampler ID, along with source
  355. /// and destination coordinates and sizes.
  356. /// </summary>
  357. /// <param name="engine">3D engine where this method is being called</param>
  358. /// <param name="argument">Method call argument</param>
  359. public void DrawTexture(ThreedClass engine, int argument)
  360. {
  361. static float FixedToFloat(int fixedValue)
  362. {
  363. return fixedValue * (1f / 4096);
  364. }
  365. float dstX0 = FixedToFloat(_state.State.DrawTextureDstX);
  366. float dstY0 = FixedToFloat(_state.State.DrawTextureDstY);
  367. float dstWidth = FixedToFloat(_state.State.DrawTextureDstWidth);
  368. float dstHeight = FixedToFloat(_state.State.DrawTextureDstHeight);
  369. // TODO: Confirm behaviour on hardware.
  370. // When this is active, the origin appears to be on the bottom.
  371. if (_state.State.YControl.HasFlag(YControl.NegateY))
  372. {
  373. dstY0 -= dstHeight;
  374. }
  375. float dstX1 = dstX0 + dstWidth;
  376. float dstY1 = dstY0 + dstHeight;
  377. float srcX0 = FixedToFloat(_state.State.DrawTextureSrcX);
  378. float srcY0 = FixedToFloat(_state.State.DrawTextureSrcY);
  379. float srcX1 = ((float)_state.State.DrawTextureDuDx / (1UL << 32)) * dstWidth + srcX0;
  380. float srcY1 = ((float)_state.State.DrawTextureDvDy / (1UL << 32)) * dstHeight + srcY0;
  381. engine.UpdateState(ulong.MaxValue & ~(1UL << StateUpdater.ShaderStateIndex));
  382. _channel.TextureManager.UpdateRenderTargets();
  383. int textureId = _state.State.DrawTextureTextureId;
  384. int samplerId = _state.State.DrawTextureSamplerId;
  385. (var texture, var sampler) = _channel.TextureManager.GetGraphicsTextureAndSampler(textureId, samplerId);
  386. srcX0 *= texture.ScaleFactor;
  387. srcY0 *= texture.ScaleFactor;
  388. srcX1 *= texture.ScaleFactor;
  389. srcY1 *= texture.ScaleFactor;
  390. float dstScale = _channel.TextureManager.RenderTargetScale;
  391. dstX0 *= dstScale;
  392. dstY0 *= dstScale;
  393. dstX1 *= dstScale;
  394. dstY1 *= dstScale;
  395. _context.Renderer.Pipeline.DrawTexture(
  396. texture?.HostTexture,
  397. sampler?.GetHostSampler(texture),
  398. new Extents2DF(srcX0, srcY0, srcX1, srcY1),
  399. new Extents2DF(dstX0, dstY0, dstX1, dstY1));
  400. }
  401. /// <summary>
  402. /// Performs a indexed or non-indexed draw.
  403. /// </summary>
  404. /// <param name="engine">3D engine where this method is being called</param>
  405. /// <param name="topology">Primitive topology</param>
  406. /// <param name="count">Index count for indexed draws, vertex count for non-indexed draws</param>
  407. /// <param name="instanceCount">Instance count</param>
  408. /// <param name="firstIndex">First index on the index buffer for indexed draws, ignored for non-indexed draws</param>
  409. /// <param name="firstVertex">First vertex on the vertex buffer</param>
  410. /// <param name="firstInstance">First instance</param>
  411. /// <param name="indexed">True if the draw is indexed, false otherwise</param>
  412. public void Draw(
  413. ThreedClass engine,
  414. PrimitiveTopology topology,
  415. int count,
  416. int instanceCount,
  417. int firstIndex,
  418. int firstVertex,
  419. int firstInstance,
  420. bool indexed)
  421. {
  422. UpdateTopology(topology);
  423. ConditionalRenderEnabled renderEnable = ConditionalRendering.GetRenderEnable(
  424. _context,
  425. _channel.MemoryManager,
  426. _state.State.RenderEnableAddress,
  427. _state.State.RenderEnableCondition);
  428. if (renderEnable == ConditionalRenderEnabled.False)
  429. {
  430. _drawState.DrawIndexed = false;
  431. return;
  432. }
  433. if (indexed)
  434. {
  435. _drawState.FirstIndex = firstIndex;
  436. _drawState.IndexCount = count;
  437. _state.State.FirstVertex = (uint)firstVertex;
  438. engine.ForceStateDirty(IndexBufferCountMethodOffset * 4);
  439. }
  440. else
  441. {
  442. _state.State.VertexBufferDrawState.First = firstVertex;
  443. _state.State.VertexBufferDrawState.Count = count;
  444. engine.ForceStateDirty(VertexBufferFirstMethodOffset * 4);
  445. }
  446. _state.State.FirstInstance = (uint)firstInstance;
  447. _drawState.DrawIndexed = indexed;
  448. _currentSpecState.SetHasConstantBufferDrawParameters(true);
  449. engine.UpdateState();
  450. if (indexed)
  451. {
  452. _context.Renderer.Pipeline.DrawIndexed(count, instanceCount, firstIndex, firstVertex, firstInstance);
  453. _state.State.FirstVertex = 0;
  454. }
  455. else
  456. {
  457. _context.Renderer.Pipeline.Draw(count, instanceCount, firstVertex, firstInstance);
  458. }
  459. _state.State.FirstInstance = 0;
  460. _drawState.DrawIndexed = false;
  461. if (renderEnable == ConditionalRenderEnabled.Host)
  462. {
  463. _context.Renderer.Pipeline.EndHostConditionalRendering();
  464. }
  465. }
  466. /// <summary>
  467. /// Performs a indirect draw, with parameters from a GPU buffer.
  468. /// </summary>
  469. /// <param name="engine">3D engine where this method is being called</param>
  470. /// <param name="topology">Primitive topology</param>
  471. /// <param name="indirectBufferAddress">Address of the buffer with the draw parameters, such as count, first index, etc</param>
  472. /// <param name="parameterBufferAddress">Address of the buffer with the draw count</param>
  473. /// <param name="maxDrawCount">Maximum number of draws that can be made</param>
  474. /// <param name="stride">Distance in bytes between each entry on the data pointed to by <paramref name="indirectBufferAddress"/></param>
  475. /// <param name="indexCount">Maximum number of indices that the draw can consume</param>
  476. /// <param name="drawType">Type of the indirect draw, which can be indexed or non-indexed, with or without a draw count</param>
  477. public void DrawIndirect(
  478. ThreedClass engine,
  479. PrimitiveTopology topology,
  480. ulong indirectBufferAddress,
  481. ulong parameterBufferAddress,
  482. int maxDrawCount,
  483. int stride,
  484. int indexCount,
  485. IndirectDrawType drawType)
  486. {
  487. UpdateTopology(topology);
  488. ConditionalRenderEnabled renderEnable = ConditionalRendering.GetRenderEnable(
  489. _context,
  490. _channel.MemoryManager,
  491. _state.State.RenderEnableAddress,
  492. _state.State.RenderEnableCondition);
  493. if (renderEnable == ConditionalRenderEnabled.False)
  494. {
  495. _drawState.DrawIndexed = false;
  496. return;
  497. }
  498. PhysicalMemory memory = _channel.MemoryManager.Physical;
  499. bool hasCount = (drawType & IndirectDrawType.Count) != 0;
  500. bool indexed = (drawType & IndirectDrawType.Indexed) != 0;
  501. if (indexed)
  502. {
  503. indexCount = Math.Clamp(indexCount, MinIndirectIndexCount, MaxIndirectIndexCount);
  504. _drawState.FirstIndex = 0;
  505. _drawState.IndexCount = indexCount;
  506. engine.ForceStateDirty(IndexBufferCountMethodOffset * 4);
  507. }
  508. _drawState.DrawIndexed = indexed;
  509. _drawState.DrawIndirect = true;
  510. _currentSpecState.SetHasConstantBufferDrawParameters(true);
  511. engine.UpdateState();
  512. if (hasCount)
  513. {
  514. var indirectBuffer = memory.BufferCache.GetBufferRange(indirectBufferAddress, (ulong)maxDrawCount * (ulong)stride);
  515. var parameterBuffer = memory.BufferCache.GetBufferRange(parameterBufferAddress, 4);
  516. if (indexed)
  517. {
  518. _context.Renderer.Pipeline.DrawIndexedIndirectCount(indirectBuffer, parameterBuffer, maxDrawCount, stride);
  519. }
  520. else
  521. {
  522. _context.Renderer.Pipeline.DrawIndirectCount(indirectBuffer, parameterBuffer, maxDrawCount, stride);
  523. }
  524. }
  525. else
  526. {
  527. var indirectBuffer = memory.BufferCache.GetBufferRange(indirectBufferAddress, (ulong)stride);
  528. if (indexed)
  529. {
  530. _context.Renderer.Pipeline.DrawIndexedIndirect(indirectBuffer);
  531. }
  532. else
  533. {
  534. _context.Renderer.Pipeline.DrawIndirect(indirectBuffer);
  535. }
  536. }
  537. _drawState.DrawIndexed = false;
  538. _drawState.DrawIndirect = false;
  539. if (renderEnable == ConditionalRenderEnabled.Host)
  540. {
  541. _context.Renderer.Pipeline.EndHostConditionalRendering();
  542. }
  543. }
  544. /// <summary>
  545. /// Perform any deferred draws.
  546. /// This is used for instanced draws.
  547. /// Since each instance is a separate draw, we defer the draw and accumulate the instance count.
  548. /// Once we detect the last instanced draw, then we perform the host instanced draw,
  549. /// with the accumulated instance count.
  550. /// </summary>
  551. public void PerformDeferredDraws()
  552. {
  553. // Perform any pending instanced draw.
  554. if (_instancedDrawPending)
  555. {
  556. _instancedDrawPending = false;
  557. bool indexedInline = _instancedIndexedInline;
  558. if (_instancedIndexed || indexedInline)
  559. {
  560. if (indexedInline)
  561. {
  562. int inlineIndexCount = _drawState.IbStreamer.GetAndResetInlineIndexCount();
  563. BufferRange br = new BufferRange(_drawState.IbStreamer.GetInlineIndexBuffer(), 0, inlineIndexCount * 4);
  564. _channel.BufferManager.SetIndexBuffer(br, IndexType.UInt);
  565. }
  566. _context.Renderer.Pipeline.DrawIndexed(
  567. _instancedIndexCount,
  568. _instanceIndex + 1,
  569. _instancedFirstIndex,
  570. _instancedFirstVertex,
  571. _instancedFirstInstance);
  572. }
  573. else
  574. {
  575. _context.Renderer.Pipeline.Draw(
  576. _instancedDrawStateCount,
  577. _instanceIndex + 1,
  578. _instancedDrawStateFirst,
  579. _instancedFirstInstance);
  580. }
  581. }
  582. }
  583. /// <summary>
  584. /// Clears the current color and depth-stencil buffers.
  585. /// Which buffers should be cleared can also be specified with the argument.
  586. /// </summary>
  587. /// <param name="engine">3D engine where this method is being called</param>
  588. /// <param name="argument">Method call argument</param>
  589. public void Clear(ThreedClass engine, int argument)
  590. {
  591. Clear(engine, argument, 1);
  592. }
  593. /// <summary>
  594. /// Clears the current color and depth-stencil buffers.
  595. /// Which buffers should be cleared can also specified with the arguments.
  596. /// </summary>
  597. /// <param name="engine">3D engine where this method is being called</param>
  598. /// <param name="argument">Method call argument</param>
  599. /// <param name="layerCount">For array and 3D textures, indicates how many layers should be cleared</param>
  600. public void Clear(ThreedClass engine, int argument, int layerCount)
  601. {
  602. ConditionalRenderEnabled renderEnable = ConditionalRendering.GetRenderEnable(
  603. _context,
  604. _channel.MemoryManager,
  605. _state.State.RenderEnableAddress,
  606. _state.State.RenderEnableCondition);
  607. if (renderEnable == ConditionalRenderEnabled.False)
  608. {
  609. return;
  610. }
  611. int index = (argument >> 6) & 0xf;
  612. int layer = (argument >> 10) & 0x3ff;
  613. engine.UpdateRenderTargetState(useControl: false, layered: layer != 0 || layerCount > 1, singleUse: index);
  614. // If there is a mismatch on the host clip region and the one explicitly defined by the guest
  615. // on the screen scissor state, then we need to force only one texture to be bound to avoid
  616. // host clipping.
  617. var screenScissorState = _state.State.ScreenScissorState;
  618. // Must happen after UpdateRenderTargetState to have up-to-date clip region values.
  619. bool clipMismatch = (screenScissorState.X | screenScissorState.Y) != 0 ||
  620. screenScissorState.Width != _channel.TextureManager.ClipRegionWidth ||
  621. screenScissorState.Height != _channel.TextureManager.ClipRegionHeight;
  622. bool clearAffectedByStencilMask = (_state.State.ClearFlags & 1) != 0;
  623. bool clearAffectedByScissor = (_state.State.ClearFlags & 0x100) != 0;
  624. bool needsCustomScissor = !clearAffectedByScissor || clipMismatch;
  625. // Scissor and rasterizer discard also affect clears.
  626. ulong updateMask = 1UL << StateUpdater.RasterizerStateIndex;
  627. if (!needsCustomScissor)
  628. {
  629. updateMask |= 1UL << StateUpdater.ScissorStateIndex;
  630. }
  631. engine.UpdateState(updateMask);
  632. if (needsCustomScissor)
  633. {
  634. int scissorX = screenScissorState.X;
  635. int scissorY = screenScissorState.Y;
  636. int scissorW = screenScissorState.Width;
  637. int scissorH = screenScissorState.Height;
  638. if (clearAffectedByScissor && _state.State.ScissorState[0].Enable)
  639. {
  640. ref var scissorState = ref _state.State.ScissorState[0];
  641. scissorX = Math.Max(scissorX, scissorState.X1);
  642. scissorY = Math.Max(scissorY, scissorState.Y1);
  643. scissorW = Math.Min(scissorW, scissorState.X2 - scissorState.X1);
  644. scissorH = Math.Min(scissorH, scissorState.Y2 - scissorState.Y1);
  645. }
  646. float scale = _channel.TextureManager.RenderTargetScale;
  647. if (scale != 1f)
  648. {
  649. scissorX = (int)(scissorX * scale);
  650. scissorY = (int)(scissorY * scale);
  651. scissorW = (int)MathF.Ceiling(scissorW * scale);
  652. scissorH = (int)MathF.Ceiling(scissorH * scale);
  653. }
  654. Span<Rectangle<int>> scissors = stackalloc Rectangle<int>[]
  655. {
  656. new Rectangle<int>(scissorX, scissorY, scissorW, scissorH)
  657. };
  658. _context.Renderer.Pipeline.SetScissors(scissors);
  659. }
  660. if (clipMismatch)
  661. {
  662. _channel.TextureManager.UpdateRenderTarget(index);
  663. }
  664. else
  665. {
  666. _channel.TextureManager.UpdateRenderTargets();
  667. }
  668. bool clearDepth = (argument & 1) != 0;
  669. bool clearStencil = (argument & 2) != 0;
  670. uint componentMask = (uint)((argument >> 2) & 0xf);
  671. if (componentMask != 0)
  672. {
  673. var clearColor = _state.State.ClearColors;
  674. ColorF color = new ColorF(clearColor.Red, clearColor.Green, clearColor.Blue, clearColor.Alpha);
  675. _context.Renderer.Pipeline.ClearRenderTargetColor(index, layer, layerCount, componentMask, color);
  676. }
  677. if (clearDepth || clearStencil)
  678. {
  679. float depthValue = _state.State.ClearDepthValue;
  680. int stencilValue = (int)_state.State.ClearStencilValue;
  681. int stencilMask = 0;
  682. if (clearStencil)
  683. {
  684. stencilMask = clearAffectedByStencilMask ? _state.State.StencilTestState.FrontMask : 0xff;
  685. }
  686. if (clipMismatch)
  687. {
  688. _channel.TextureManager.UpdateRenderTargetDepthStencil();
  689. }
  690. _context.Renderer.Pipeline.ClearRenderTargetDepthStencil(
  691. layer,
  692. layerCount,
  693. depthValue,
  694. clearDepth,
  695. stencilValue,
  696. stencilMask);
  697. }
  698. if (needsCustomScissor)
  699. {
  700. engine.UpdateScissorState();
  701. }
  702. engine.UpdateRenderTargetState(useControl: true);
  703. if (renderEnable == ConditionalRenderEnabled.Host)
  704. {
  705. _context.Renderer.Pipeline.EndHostConditionalRendering();
  706. }
  707. }
  708. }
  709. }