BufferedQuery.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Graphics.GAL;
  3. using Silk.NET.Vulkan;
  4. using System;
  5. using System.Runtime.InteropServices;
  6. using System.Threading;
  7. namespace Ryujinx.Graphics.Vulkan.Queries
  8. {
  9. class BufferedQuery : IDisposable
  10. {
  11. private const int MaxQueryRetries = 5000;
  12. private const long DefaultValue = -1;
  13. private const long DefaultValueInt = 0xFFFFFFFF;
  14. private readonly Vk _api;
  15. private readonly Device _device;
  16. private readonly PipelineFull _pipeline;
  17. private QueryPool _queryPool;
  18. private readonly BufferHolder _buffer;
  19. private readonly IntPtr _bufferMap;
  20. private readonly CounterType _type;
  21. private bool _result32Bit;
  22. private bool _isSupported;
  23. private long _defaultValue;
  24. private int? _resetSequence;
  25. public unsafe BufferedQuery(VulkanRenderer gd, Device device, PipelineFull pipeline, CounterType type, bool result32Bit)
  26. {
  27. _api = gd.Api;
  28. _device = device;
  29. _pipeline = pipeline;
  30. _type = type;
  31. _result32Bit = result32Bit;
  32. _isSupported = QueryTypeSupported(gd, type);
  33. if (_isSupported)
  34. {
  35. QueryPipelineStatisticFlags flags = type == CounterType.PrimitivesGenerated ?
  36. QueryPipelineStatisticFlags.GeometryShaderPrimitivesBit : 0;
  37. var queryPoolCreateInfo = new QueryPoolCreateInfo()
  38. {
  39. SType = StructureType.QueryPoolCreateInfo,
  40. QueryCount = 1,
  41. QueryType = GetQueryType(type),
  42. PipelineStatistics = flags
  43. };
  44. gd.Api.CreateQueryPool(device, queryPoolCreateInfo, null, out _queryPool).ThrowOnError();
  45. }
  46. var buffer = gd.BufferManager.Create(gd, sizeof(long), forConditionalRendering: true);
  47. _bufferMap = buffer.Map(0, sizeof(long));
  48. _defaultValue = result32Bit ? DefaultValueInt : DefaultValue;
  49. Marshal.WriteInt64(_bufferMap, _defaultValue);
  50. _buffer = buffer;
  51. }
  52. private bool QueryTypeSupported(VulkanRenderer gd, CounterType type)
  53. {
  54. return type switch
  55. {
  56. CounterType.SamplesPassed => true,
  57. CounterType.PrimitivesGenerated => gd.Capabilities.SupportsPipelineStatisticsQuery,
  58. CounterType.TransformFeedbackPrimitivesWritten => gd.Capabilities.SupportsTransformFeedbackQueries,
  59. _ => false
  60. };
  61. }
  62. private static QueryType GetQueryType(CounterType type)
  63. {
  64. return type switch
  65. {
  66. CounterType.SamplesPassed => QueryType.Occlusion,
  67. CounterType.PrimitivesGenerated => QueryType.PipelineStatistics,
  68. CounterType.TransformFeedbackPrimitivesWritten => QueryType.TransformFeedbackStreamExt,
  69. _ => QueryType.Occlusion
  70. };
  71. }
  72. public Auto<DisposableBuffer> GetBuffer()
  73. {
  74. return _buffer.GetBuffer();
  75. }
  76. public void Reset()
  77. {
  78. End(false);
  79. Begin(null);
  80. }
  81. public void Begin(int? resetSequence)
  82. {
  83. if (_isSupported)
  84. {
  85. bool needsReset = resetSequence == null || _resetSequence == null || resetSequence.Value != _resetSequence.Value;
  86. bool isOcclusion = _type == CounterType.SamplesPassed;
  87. _pipeline.BeginQuery(this, _queryPool, needsReset, isOcclusion, isOcclusion && resetSequence != null);
  88. }
  89. _resetSequence = null;
  90. }
  91. public unsafe void End(bool withResult)
  92. {
  93. if (_isSupported)
  94. {
  95. _pipeline.EndQuery(_queryPool);
  96. }
  97. if (withResult && _isSupported)
  98. {
  99. Marshal.WriteInt64(_bufferMap, _defaultValue);
  100. _pipeline.CopyQueryResults(this);
  101. }
  102. else
  103. {
  104. // Dummy result, just return 0.
  105. Marshal.WriteInt64(_bufferMap, 0);
  106. }
  107. }
  108. public bool TryGetResult(out long result)
  109. {
  110. result = Marshal.ReadInt64(_bufferMap);
  111. return result != _defaultValue;
  112. }
  113. public long AwaitResult(AutoResetEvent wakeSignal = null)
  114. {
  115. long data = _defaultValue;
  116. if (wakeSignal == null)
  117. {
  118. while (data == _defaultValue)
  119. {
  120. data = Marshal.ReadInt64(_bufferMap);
  121. }
  122. }
  123. else
  124. {
  125. int iterations = 0;
  126. while (data == _defaultValue && iterations++ < MaxQueryRetries)
  127. {
  128. data = Marshal.ReadInt64(_bufferMap);
  129. if (data == _defaultValue)
  130. {
  131. wakeSignal.WaitOne(1);
  132. }
  133. }
  134. if (iterations >= MaxQueryRetries)
  135. {
  136. Logger.Error?.Print(LogClass.Gpu, $"Error: Query result {_type} timed out. Took more than {MaxQueryRetries} tries.");
  137. }
  138. }
  139. return data;
  140. }
  141. public void PoolReset(CommandBuffer cmd, int resetSequence)
  142. {
  143. if (_isSupported)
  144. {
  145. _api.CmdResetQueryPool(cmd, _queryPool, 0, 1);
  146. }
  147. _resetSequence = resetSequence;
  148. }
  149. public void PoolCopy(CommandBufferScoped cbs)
  150. {
  151. var buffer = _buffer.GetBuffer(cbs.CommandBuffer, true).Get(cbs, 0, sizeof(long)).Value;
  152. QueryResultFlags flags = QueryResultFlags.ResultWaitBit;
  153. if (!_result32Bit)
  154. {
  155. flags |= QueryResultFlags.Result64Bit;
  156. }
  157. _api.CmdCopyQueryPoolResults(
  158. cbs.CommandBuffer,
  159. _queryPool,
  160. 0,
  161. 1,
  162. buffer,
  163. 0,
  164. (ulong)(_result32Bit ? sizeof(int) : sizeof(long)),
  165. flags);
  166. }
  167. public unsafe void Dispose()
  168. {
  169. _buffer.Dispose();
  170. if (_isSupported)
  171. {
  172. _api.DestroyQueryPool(_device, _queryPool, null);
  173. }
  174. _queryPool = default;
  175. }
  176. }
  177. }