PipelineFull.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.Vulkan.Queries;
  3. using Silk.NET.Vulkan;
  4. using System;
  5. using System.Collections.Generic;
  6. namespace Ryujinx.Graphics.Vulkan
  7. {
  8. class PipelineFull : PipelineBase, IPipeline
  9. {
  10. private const ulong MinByteWeightForFlush = 256 * 1024 * 1024; // MB
  11. private bool _hasPendingQuery;
  12. private readonly List<QueryPool> _activeQueries;
  13. private CounterQueueEvent _activeConditionalRender;
  14. private readonly List<BufferedQuery> _pendingQueryCopies;
  15. private readonly List<BufferedQuery> _pendingQueryResets;
  16. private ulong _byteWeight;
  17. public PipelineFull(VulkanRenderer gd, Device device) : base(gd, device)
  18. {
  19. _activeQueries = new List<QueryPool>();
  20. _pendingQueryCopies = new();
  21. _pendingQueryResets = new List<BufferedQuery>();
  22. CommandBuffer = (Cbs = gd.CommandBufferPool.Rent()).CommandBuffer;
  23. }
  24. private void CopyPendingQuery()
  25. {
  26. foreach (var query in _pendingQueryCopies)
  27. {
  28. query.PoolCopy(Cbs);
  29. }
  30. lock (_pendingQueryResets)
  31. {
  32. foreach (var query in _pendingQueryResets)
  33. {
  34. query.PoolReset(CommandBuffer);
  35. }
  36. _pendingQueryResets.Clear();
  37. }
  38. _pendingQueryCopies.Clear();
  39. }
  40. public void ClearRenderTargetColor(int index, int layer, int layerCount, uint componentMask, ColorF color)
  41. {
  42. if (FramebufferParams == null)
  43. {
  44. return;
  45. }
  46. if (componentMask != 0xf)
  47. {
  48. // We can't use CmdClearAttachments if not writing all components,
  49. // because on Vulkan, the pipeline state does not affect clears.
  50. var dstTexture = FramebufferParams.GetAttachment(index);
  51. if (dstTexture == null)
  52. {
  53. return;
  54. }
  55. Span<float> clearColor = stackalloc float[4];
  56. clearColor[0] = color.Red;
  57. clearColor[1] = color.Green;
  58. clearColor[2] = color.Blue;
  59. clearColor[3] = color.Alpha;
  60. // TODO: Clear only the specified layer.
  61. Gd.HelperShader.Clear(
  62. Gd,
  63. dstTexture,
  64. clearColor,
  65. componentMask,
  66. (int)FramebufferParams.Width,
  67. (int)FramebufferParams.Height,
  68. FramebufferParams.AttachmentFormats[index],
  69. ClearScissor);
  70. }
  71. else
  72. {
  73. ClearRenderTargetColor(index, layer, layerCount, color);
  74. }
  75. }
  76. public void EndHostConditionalRendering()
  77. {
  78. if (Gd.Capabilities.SupportsConditionalRendering)
  79. {
  80. // Gd.ConditionalRenderingApi.CmdEndConditionalRendering(CommandBuffer);
  81. }
  82. else
  83. {
  84. // throw new NotSupportedException();
  85. }
  86. _activeConditionalRender?.ReleaseHostAccess();
  87. _activeConditionalRender = null;
  88. }
  89. public bool TryHostConditionalRendering(ICounterEvent value, ulong compare, bool isEqual)
  90. {
  91. // Compare an event and a constant value.
  92. if (value is CounterQueueEvent evt)
  93. {
  94. // Easy host conditional rendering when the check matches what GL can do:
  95. // - Event is of type samples passed.
  96. // - Result is not a combination of multiple queries.
  97. // - Comparing against 0.
  98. // - Event has not already been flushed.
  99. if (compare == 0 && evt.Type == CounterType.SamplesPassed && evt.ClearCounter)
  100. {
  101. if (!value.ReserveForHostAccess())
  102. {
  103. // If the event has been flushed, then just use the values on the CPU.
  104. // The query object may already be repurposed for another draw (eg. begin + end).
  105. return false;
  106. }
  107. if (Gd.Capabilities.SupportsConditionalRendering)
  108. {
  109. var buffer = evt.GetBuffer().Get(Cbs, 0, sizeof(long)).Value;
  110. var flags = isEqual ? ConditionalRenderingFlagsEXT.ConditionalRenderingInvertedBitExt : 0;
  111. var conditionalRenderingBeginInfo = new ConditionalRenderingBeginInfoEXT()
  112. {
  113. SType = StructureType.ConditionalRenderingBeginInfoExt,
  114. Buffer = buffer,
  115. Flags = flags
  116. };
  117. // Gd.ConditionalRenderingApi.CmdBeginConditionalRendering(CommandBuffer, conditionalRenderingBeginInfo);
  118. }
  119. _activeConditionalRender = evt;
  120. return true;
  121. }
  122. }
  123. // The GPU will flush the queries to CPU and evaluate the condition there instead.
  124. FlushPendingQuery(); // The thread will be stalled manually flushing the counter, so flush commands now.
  125. return false;
  126. }
  127. public bool TryHostConditionalRendering(ICounterEvent value, ICounterEvent compare, bool isEqual)
  128. {
  129. FlushPendingQuery(); // The thread will be stalled manually flushing the counter, so flush commands now.
  130. return false;
  131. }
  132. private void FlushPendingQuery()
  133. {
  134. if (_hasPendingQuery)
  135. {
  136. _hasPendingQuery = false;
  137. FlushCommandsImpl();
  138. }
  139. }
  140. public CommandBufferScoped GetPreloadCommandBuffer()
  141. {
  142. if (PreloadCbs == null)
  143. {
  144. PreloadCbs = Gd.CommandBufferPool.Rent();
  145. }
  146. return PreloadCbs.Value;
  147. }
  148. public void FlushCommandsIfWeightExceeding(IAuto disposedResource, ulong byteWeight)
  149. {
  150. bool usedByCurrentCb = disposedResource.HasCommandBufferDependency(Cbs);
  151. if (PreloadCbs != null && !usedByCurrentCb)
  152. {
  153. usedByCurrentCb = disposedResource.HasCommandBufferDependency(PreloadCbs.Value);
  154. }
  155. if (usedByCurrentCb)
  156. {
  157. // Since we can only free memory after the command buffer that uses a given resource was executed,
  158. // keeping the command buffer might cause a high amount of memory to be in use.
  159. // To prevent that, we force submit command buffers if the memory usage by resources
  160. // in use by the current command buffer is above a given limit, and those resources were disposed.
  161. _byteWeight += byteWeight;
  162. if (_byteWeight >= MinByteWeightForFlush)
  163. {
  164. FlushCommandsImpl();
  165. }
  166. }
  167. }
  168. public void FlushCommandsImpl()
  169. {
  170. EndRenderPass();
  171. foreach (var queryPool in _activeQueries)
  172. {
  173. Gd.Api.CmdEndQuery(CommandBuffer, queryPool, 0);
  174. }
  175. _byteWeight = 0;
  176. if (PreloadCbs != null)
  177. {
  178. PreloadCbs.Value.Dispose();
  179. PreloadCbs = null;
  180. }
  181. CommandBuffer = (Cbs = Gd.CommandBufferPool.ReturnAndRent(Cbs)).CommandBuffer;
  182. // Restore per-command buffer state.
  183. if (Pipeline != null)
  184. {
  185. Gd.Api.CmdBindPipeline(CommandBuffer, Pbp, Pipeline.Get(Cbs).Value);
  186. }
  187. foreach (var queryPool in _activeQueries)
  188. {
  189. Gd.Api.CmdResetQueryPool(CommandBuffer, queryPool, 0, 1);
  190. Gd.Api.CmdBeginQuery(CommandBuffer, queryPool, 0, 0);
  191. }
  192. SignalCommandBufferChange();
  193. }
  194. public void BeginQuery(BufferedQuery query, QueryPool pool, bool needsReset)
  195. {
  196. if (needsReset)
  197. {
  198. EndRenderPass();
  199. Gd.Api.CmdResetQueryPool(CommandBuffer, pool, 0, 1);
  200. lock (_pendingQueryResets)
  201. {
  202. _pendingQueryResets.Remove(query); // Might be present on here.
  203. }
  204. }
  205. Gd.Api.CmdBeginQuery(CommandBuffer, pool, 0, 0);
  206. _activeQueries.Add(pool);
  207. }
  208. public void EndQuery(QueryPool pool)
  209. {
  210. Gd.Api.CmdEndQuery(CommandBuffer, pool, 0);
  211. _activeQueries.Remove(pool);
  212. }
  213. public void ResetQuery(BufferedQuery query)
  214. {
  215. lock (_pendingQueryResets)
  216. {
  217. _pendingQueryResets.Add(query);
  218. }
  219. }
  220. public void CopyQueryResults(BufferedQuery query)
  221. {
  222. _pendingQueryCopies.Add(query);
  223. _hasPendingQuery = true;
  224. }
  225. protected override void SignalAttachmentChange()
  226. {
  227. FlushPendingQuery();
  228. }
  229. protected override void SignalRenderPassEnd()
  230. {
  231. CopyPendingQuery();
  232. }
  233. }
  234. }