Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AdaptivePerformanceProfilerStats.cs 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. /// GUID for the Adaptive Performance Profile Module definition.
  69. /// </summary>
  70. public static readonly Guid kAdaptivePerformanceProfilerModuleGuid = new Guid("42c5aeb7-fb77-4172-a384-34063f1bd332");
  71. /// <summary>
  72. /// The Scaler data tag defines a tag for the scalers to send them via the emit frame data function.
  73. /// </summary>
  74. public static readonly int kScalerDataTag = 0;
  75. static Dictionary<string, ScalerInfo> scalerInfos = new Dictionary<string, ScalerInfo>();
  76. /// <summary>
  77. /// ScalerInfo is a struct used to collect and send scaler info to the profile collectively.
  78. /// </summary>
  79. [StructLayout(LayoutKind.Sequential)]
  80. public unsafe struct ScalerInfo
  81. {
  82. /// <summary>
  83. /// The name of the scaler. 320 characters max.
  84. /// </summary>
  85. public fixed byte scalerName[320];
  86. /// <summary>
  87. /// If the scaler is currently enabled.
  88. /// </summary>
  89. public uint enabled;
  90. /// <summary>
  91. /// The override state of the scaler.
  92. /// </summary>
  93. public int overrideLevel;
  94. /// <summary>
  95. /// The current level of the scaler.
  96. /// </summary>
  97. public int currentLevel;
  98. /// <summary>
  99. /// The maximum level of the scaler.
  100. /// </summary>
  101. public int maxLevel;
  102. /// <summary>
  103. /// The actual scale of the scaler.
  104. /// </summary>
  105. public float scale;
  106. /// <summary>
  107. /// State if the scaler is currently applied.
  108. /// </summary>
  109. public uint applied;
  110. }
  111. /// <summary>
  112. /// 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"/>.
  113. /// </summary>
  114. /// <param name="scalerName"> The name of the scaler. 320 characters max. </param>
  115. /// <param name="enabled"> If the scaler is currently enabled.</param>
  116. /// <param name="overrideLevel">The override state of the scaler.</param>
  117. /// <param name="currentLevel">The current level of the scaler.</param>
  118. /// <param name="scale">The actual scale of the scaler.</param>
  119. /// <param name="applied">If the scaler is currently applied.</param>
  120. /// <param name="maxLevel">The maximum level of the scaler.</param>
  121. [Conditional("ENABLE_PROFILER")]
  122. public static void EmitScalerDataToProfilerStream(string scalerName, bool enabled, int overrideLevel, int currentLevel, float scale, bool applied, int maxLevel)
  123. {
  124. if (!Profiler.enabled || scalerName.Length == 0)
  125. return;
  126. ScalerInfo scalerInfo;
  127. bool existingInfo = scalerInfos.TryGetValue(scalerName, out scalerInfo);
  128. if (!existingInfo)
  129. scalerInfo = new ScalerInfo();
  130. byte[] scalerNameBytes = Encoding.ASCII.GetBytes(scalerName);
  131. scalerInfo.enabled = (uint)(enabled ? 1 : 0);
  132. scalerInfo.overrideLevel = overrideLevel;
  133. scalerInfo.currentLevel = currentLevel;
  134. scalerInfo.scale = scale;
  135. scalerInfo.maxLevel = maxLevel;
  136. scalerInfo.applied = (uint)(applied ? 1 : 0);
  137. unsafe
  138. {
  139. fixed(byte* pSource = scalerNameBytes)
  140. {
  141. UnsafeUtility.MemCpy(scalerInfo.scalerName, pSource, scalerNameBytes.Length);
  142. }
  143. }
  144. if (!existingInfo)
  145. scalerInfos.Add(scalerName, scalerInfo);
  146. else
  147. scalerInfos[scalerName] = scalerInfo;
  148. }
  149. /// <summary>
  150. /// Flushes the Adaptive Performance scaler data for this frame. Used in conjunction with <see cref="EmitScalerDataToProfilerStream"/>.
  151. /// </summary>
  152. public static void FlushScalerDataToProfilerStream()
  153. {
  154. ScalerInfo[] arr = new ScalerInfo[scalerInfos.Count];
  155. scalerInfos.Values.CopyTo(arr, 0);
  156. Profiler.EmitFrameMetaData(kAdaptivePerformanceProfilerModuleGuid, kScalerDataTag, arr);
  157. }
  158. }