説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

AdaptivePerformanceProfilerStats.cs 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using Unity.Collections.LowLevel.Unsafe;
  7. using Unity.Profiling;
  8. using UnityEngine.Profiling;
  9. using UnityEngine;
  10. /// <summary>
  11. /// Profiler Stats reporting helper class. Stores all adaptive performance counters and helper functions.
  12. /// </summary>
  13. public static class AdaptivePerformanceProfilerStats
  14. {
  15. /// <summary>
  16. /// Profiler Category is set to scripts for Adaptive Performance.
  17. /// </summary>
  18. public static readonly ProfilerCategory AdaptivePerformanceProfilerCategory = ProfilerCategory.Scripts;
  19. /// <summary>
  20. /// Profiler counter to report cpu frametime.
  21. /// </summary>
  22. public static ProfilerCounter<float> CurrentCPUCounter = new ProfilerCounter<float>(AdaptivePerformanceProfilerCategory, "CPU frametime", ProfilerMarkerDataUnit.TimeNanoseconds);
  23. /// <summary>
  24. /// Profiler counter to report cpu average frametime.
  25. /// </summary>
  26. public static ProfilerCounter<float> AvgCPUCounter = new ProfilerCounter<float>(AdaptivePerformanceProfilerCategory, "CPU avg frametime", ProfilerMarkerDataUnit.TimeNanoseconds);
  27. /// <summary>
  28. /// Profiler counter to report gpu frametime.
  29. /// </summary>
  30. public static ProfilerCounter<float> CurrentGPUCounter = new ProfilerCounter<float>(AdaptivePerformanceProfilerCategory, "GPU frametime", ProfilerMarkerDataUnit.TimeNanoseconds);
  31. /// <summary>
  32. /// Profiler counter to report gpu average frametime.
  33. /// </summary>
  34. public static ProfilerCounter<float> AvgGPUCounter = new ProfilerCounter<float>(AdaptivePerformanceProfilerCategory, "GPU avg frametime", ProfilerMarkerDataUnit.TimeNanoseconds);
  35. /// <summary>
  36. /// Profiler counter to report cpu performance level.
  37. /// </summary>
  38. public static ProfilerCounter<int> CurrentCPULevelCounter = new ProfilerCounter<int>(AdaptivePerformanceProfilerCategory, "CPU performance level", ProfilerMarkerDataUnit.Count);
  39. /// <summary>
  40. /// Profiler counter to report gpu performance level.
  41. /// </summary>
  42. public static ProfilerCounter<int> CurrentGPULevelCounter = new ProfilerCounter<int>(AdaptivePerformanceProfilerCategory, "GPU performance level", ProfilerMarkerDataUnit.Count);
  43. /// <summary>
  44. /// Profiler counter to report frametime.
  45. /// </summary>
  46. public static ProfilerCounter<float> CurrentFrametimeCounter = new ProfilerCounter<float>(AdaptivePerformanceProfilerCategory, "Frametime", ProfilerMarkerDataUnit.TimeNanoseconds);
  47. /// <summary>
  48. /// Profiler counter to report average frametime.
  49. /// </summary>
  50. public static ProfilerCounter<float> AvgFrametimeCounter = new ProfilerCounter<float>(AdaptivePerformanceProfilerCategory, "Avg frametime", ProfilerMarkerDataUnit.TimeNanoseconds);
  51. /// <summary>
  52. /// Profiler counter to report the thermal warning level.
  53. /// </summary>
  54. public static ProfilerCounter<int> WarningLevelCounter = new ProfilerCounter<int>(AdaptivePerformanceProfilerCategory, "Thermal Warning Level", ProfilerMarkerDataUnit.Count);
  55. /// <summary>
  56. /// Profiler counter to report the temperature level.
  57. /// </summary>
  58. public static ProfilerCounter<float> TemperatureLevelCounter = new ProfilerCounter<float>(AdaptivePerformanceProfilerCategory, "Temperature Level", ProfilerMarkerDataUnit.Count);
  59. /// <summary>
  60. /// Profiler counter to report the temperature trend.
  61. /// </summary>
  62. public static ProfilerCounter<float> TemperatureTrendCounter = new ProfilerCounter<float>(AdaptivePerformanceProfilerCategory, "Temperature Trend", ProfilerMarkerDataUnit.Count);
  63. /// <summary>
  64. /// Profiler counter to report the bottleneck.
  65. /// </summary>
  66. public static ProfilerCounter<int> BottleneckCounter = new ProfilerCounter<int>(AdaptivePerformanceProfilerCategory, "Bottleneck", ProfilerMarkerDataUnit.Count);
  67. /// <summary>
  68. /// Profiler counter to report the performance mode.
  69. /// </summary>
  70. public static ProfilerCounter<int> PerformanceModeCounter = new ProfilerCounter<int>(AdaptivePerformanceProfilerCategory, "Performance Mode", ProfilerMarkerDataUnit.Count);
  71. /// <summary>
  72. /// GUID for the Adaptive Performance Profile Module definition.
  73. /// </summary>
  74. public static readonly Guid kAdaptivePerformanceProfilerModuleGuid = new Guid("42c5aeb7-fb77-4172-a384-34063f1bd332");
  75. /// <summary>
  76. /// The Scaler data tag defines a tag for the scalers to send them via the emit frame data function.
  77. /// </summary>
  78. public static readonly int kScalerDataTag = 0;
  79. static Dictionary<string, ScalerInfo> scalerInfos = new Dictionary<string, ScalerInfo>();
  80. /// <summary>
  81. /// ScalerInfo is a struct used to collect and send scaler info to the profile collectively.
  82. /// </summary>
  83. [StructLayout(LayoutKind.Sequential)]
  84. public unsafe struct ScalerInfo
  85. {
  86. /// <summary>
  87. /// The name of the scaler. 320 characters max.
  88. /// </summary>
  89. public fixed byte scalerName[320];
  90. /// <summary>
  91. /// If the scaler is currently enabled.
  92. /// </summary>
  93. public uint enabled;
  94. /// <summary>
  95. /// The override state of the scaler.
  96. /// </summary>
  97. public int overrideLevel;
  98. /// <summary>
  99. /// The current level of the scaler.
  100. /// </summary>
  101. public int currentLevel;
  102. /// <summary>
  103. /// The maximum level of the scaler.
  104. /// </summary>
  105. public int maxLevel;
  106. /// <summary>
  107. /// The actual scale of the scaler.
  108. /// </summary>
  109. public float scale;
  110. /// <summary>
  111. /// State if the scaler is currently applied.
  112. /// </summary>
  113. public uint applied;
  114. }
  115. /// <summary>
  116. /// Adaptive Performance sends scaler data to the profiler each frame. It is collected from multiple places with this method and flushed once with <see cref="FlushScalerDataToProfilerStream"/>.
  117. /// </summary>
  118. /// <param name="scalerName"> The name of the scaler. 320 characters max. </param>
  119. /// <param name="enabled"> If the scaler is currently enabled.</param>
  120. /// <param name="overrideLevel">The override state of the scaler.</param>
  121. /// <param name="currentLevel">The current level of the scaler.</param>
  122. /// <param name="scale">The actual scale of the scaler.</param>
  123. /// <param name="applied">If the scaler is currently applied.</param>
  124. /// <param name="maxLevel">The maximum level of the scaler.</param>
  125. [Conditional("ENABLE_PROFILER")]
  126. public static void EmitScalerDataToProfilerStream(string scalerName, bool enabled, int overrideLevel, int currentLevel, float scale, bool applied, int maxLevel)
  127. {
  128. if (!Profiler.enabled || scalerName.Length == 0)
  129. return;
  130. ScalerInfo scalerInfo;
  131. bool existingInfo = scalerInfos.TryGetValue(scalerName, out scalerInfo);
  132. if (!existingInfo)
  133. scalerInfo = new ScalerInfo();
  134. byte[] scalerNameBytes = Encoding.ASCII.GetBytes(scalerName);
  135. scalerInfo.enabled = (uint)(enabled ? 1 : 0);
  136. scalerInfo.overrideLevel = overrideLevel;
  137. scalerInfo.currentLevel = currentLevel;
  138. scalerInfo.scale = scale;
  139. scalerInfo.maxLevel = maxLevel;
  140. scalerInfo.applied = (uint)(applied ? 1 : 0);
  141. unsafe
  142. {
  143. fixed(byte* pSource = scalerNameBytes)
  144. {
  145. UnsafeUtility.MemCpy(scalerInfo.scalerName, pSource, scalerNameBytes.Length);
  146. }
  147. }
  148. if (!existingInfo)
  149. scalerInfos.Add(scalerName, scalerInfo);
  150. else
  151. scalerInfos[scalerName] = scalerInfo;
  152. }
  153. /// <summary>
  154. /// Flushes the Adaptive Performance scaler data for this frame. Used in conjunction with <see cref="EmitScalerDataToProfilerStream"/>.
  155. /// </summary>
  156. public static void FlushScalerDataToProfilerStream()
  157. {
  158. ScalerInfo[] arr = new ScalerInfo[scalerInfos.Count];
  159. scalerInfos.Values.CopyTo(arr, 0);
  160. Profiler.EmitFrameMetaData(kAdaptivePerformanceProfilerModuleGuid, kScalerDataTag, arr);
  161. }
  162. }