PipelineFull.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. FramebufferParams.GetAttachmentComponentType(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.InvertedBitExt : 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 (AutoFlush.ShouldFlushQuery())
  135. {
  136. FlushCommandsImpl();
  137. }
  138. }
  139. public CommandBufferScoped GetPreloadCommandBuffer()
  140. {
  141. if (PreloadCbs == null)
  142. {
  143. PreloadCbs = Gd.CommandBufferPool.Rent();
  144. }
  145. return PreloadCbs.Value;
  146. }
  147. public void FlushCommandsIfWeightExceeding(IAuto disposedResource, ulong byteWeight)
  148. {
  149. bool usedByCurrentCb = disposedResource.HasCommandBufferDependency(Cbs);
  150. if (PreloadCbs != null && !usedByCurrentCb)
  151. {
  152. usedByCurrentCb = disposedResource.HasCommandBufferDependency(PreloadCbs.Value);
  153. }
  154. if (usedByCurrentCb)
  155. {
  156. // Since we can only free memory after the command buffer that uses a given resource was executed,
  157. // keeping the command buffer might cause a high amount of memory to be in use.
  158. // To prevent that, we force submit command buffers if the memory usage by resources
  159. // in use by the current command buffer is above a given limit, and those resources were disposed.
  160. _byteWeight += byteWeight;
  161. if (_byteWeight >= MinByteWeightForFlush)
  162. {
  163. FlushCommandsImpl();
  164. }
  165. }
  166. }
  167. public void Restore()
  168. {
  169. if (Pipeline != null)
  170. {
  171. Gd.Api.CmdBindPipeline(CommandBuffer, Pbp, Pipeline.Get(Cbs).Value);
  172. }
  173. SignalCommandBufferChange();
  174. DynamicState.ReplayIfDirty(Gd.Api, CommandBuffer);
  175. }
  176. public void FlushCommandsImpl()
  177. {
  178. AutoFlush.RegisterFlush(DrawCount);
  179. EndRenderPass();
  180. foreach (var queryPool in _activeQueries)
  181. {
  182. Gd.Api.CmdEndQuery(CommandBuffer, queryPool, 0);
  183. }
  184. _byteWeight = 0;
  185. if (PreloadCbs != null)
  186. {
  187. PreloadCbs.Value.Dispose();
  188. PreloadCbs = null;
  189. }
  190. CommandBuffer = (Cbs = Gd.CommandBufferPool.ReturnAndRent(Cbs)).CommandBuffer;
  191. Gd.RegisterFlush();
  192. // Restore per-command buffer state.
  193. foreach (var queryPool in _activeQueries)
  194. {
  195. Gd.Api.CmdResetQueryPool(CommandBuffer, queryPool, 0, 1);
  196. Gd.Api.CmdBeginQuery(CommandBuffer, queryPool, 0, Gd.Capabilities.SupportsPreciseOcclusionQueries ? QueryControlFlags.PreciseBit : 0);
  197. }
  198. Restore();
  199. }
  200. public void BeginQuery(BufferedQuery query, QueryPool pool, bool needsReset)
  201. {
  202. if (needsReset)
  203. {
  204. EndRenderPass();
  205. Gd.Api.CmdResetQueryPool(CommandBuffer, pool, 0, 1);
  206. lock (_pendingQueryResets)
  207. {
  208. _pendingQueryResets.Remove(query); // Might be present on here.
  209. }
  210. }
  211. Gd.Api.CmdBeginQuery(CommandBuffer, pool, 0, Gd.Capabilities.SupportsPreciseOcclusionQueries ? QueryControlFlags.PreciseBit : 0);
  212. _activeQueries.Add(pool);
  213. }
  214. public void EndQuery(QueryPool pool)
  215. {
  216. Gd.Api.CmdEndQuery(CommandBuffer, pool, 0);
  217. _activeQueries.Remove(pool);
  218. }
  219. public void ResetQuery(BufferedQuery query)
  220. {
  221. lock (_pendingQueryResets)
  222. {
  223. _pendingQueryResets.Add(query);
  224. }
  225. }
  226. public void CopyQueryResults(BufferedQuery query)
  227. {
  228. _pendingQueryCopies.Add(query);
  229. if (AutoFlush.RegisterPendingQuery())
  230. {
  231. FlushCommandsImpl();
  232. }
  233. }
  234. protected override void SignalAttachmentChange()
  235. {
  236. if (AutoFlush.ShouldFlush(DrawCount))
  237. {
  238. FlushCommandsImpl();
  239. }
  240. }
  241. protected override void SignalRenderPassEnd()
  242. {
  243. CopyPendingQuery();
  244. }
  245. }
  246. }