PipelineFull.cs 9.5 KB

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