BufferedQuery.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. _pipeline.BeginQuery(this, _queryPool, needsReset, _type == CounterType.SamplesPassed && resetSequence != null);
  87. }
  88. _resetSequence = null;
  89. }
  90. public unsafe void End(bool withResult)
  91. {
  92. if (_isSupported)
  93. {
  94. _pipeline.EndQuery(_queryPool);
  95. }
  96. if (withResult && _isSupported)
  97. {
  98. Marshal.WriteInt64(_bufferMap, _defaultValue);
  99. _pipeline.CopyQueryResults(this);
  100. }
  101. else
  102. {
  103. // Dummy result, just return 0.
  104. Marshal.WriteInt64(_bufferMap, 0);
  105. }
  106. }
  107. public bool TryGetResult(out long result)
  108. {
  109. result = Marshal.ReadInt64(_bufferMap);
  110. return result != _defaultValue;
  111. }
  112. public long AwaitResult(AutoResetEvent wakeSignal = null)
  113. {
  114. long data = _defaultValue;
  115. if (wakeSignal == null)
  116. {
  117. while (data == _defaultValue)
  118. {
  119. data = Marshal.ReadInt64(_bufferMap);
  120. }
  121. }
  122. else
  123. {
  124. int iterations = 0;
  125. while (data == _defaultValue && iterations++ < MaxQueryRetries)
  126. {
  127. data = Marshal.ReadInt64(_bufferMap);
  128. if (data == _defaultValue)
  129. {
  130. wakeSignal.WaitOne(1);
  131. }
  132. }
  133. if (iterations >= MaxQueryRetries)
  134. {
  135. Logger.Error?.Print(LogClass.Gpu, $"Error: Query result {_type} timed out. Took more than {MaxQueryRetries} tries.");
  136. }
  137. }
  138. return data;
  139. }
  140. public void PoolReset(CommandBuffer cmd, int resetSequence)
  141. {
  142. if (_isSupported)
  143. {
  144. _api.CmdResetQueryPool(cmd, _queryPool, 0, 1);
  145. }
  146. _resetSequence = resetSequence;
  147. }
  148. public void PoolCopy(CommandBufferScoped cbs)
  149. {
  150. var buffer = _buffer.GetBuffer(cbs.CommandBuffer, true).Get(cbs, 0, sizeof(long)).Value;
  151. QueryResultFlags flags = QueryResultFlags.ResultWaitBit;
  152. if (!_result32Bit)
  153. {
  154. flags |= QueryResultFlags.Result64Bit;
  155. }
  156. _api.CmdCopyQueryPoolResults(
  157. cbs.CommandBuffer,
  158. _queryPool,
  159. 0,
  160. 1,
  161. buffer,
  162. 0,
  163. (ulong)(_result32Bit ? sizeof(int) : sizeof(long)),
  164. flags);
  165. }
  166. public unsafe void Dispose()
  167. {
  168. _buffer.Dispose();
  169. if (_isSupported)
  170. {
  171. _api.DestroyQueryPool(_device, _queryPool, null);
  172. }
  173. _queryPool = default;
  174. }
  175. }
  176. }