PipelineFull.cs 9.7 KB

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