PipelineFull.cs 10 KB

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