DrawManager.cs 33 KB

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