PipelineFull.cs 12 KB

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