BufferedQuery.cs 6.1 KB

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