No Description
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.

ProfilerCounterValue.cs 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.InteropServices;
  5. using Unity.Collections.LowLevel.Unsafe;
  6. using Unity.Profiling.LowLevel;
  7. using Unity.Profiling.LowLevel.Unsafe;
  8. namespace Unity.Profiling
  9. {
  10. /// <summary>
  11. /// Reports a value of an integral or floating point type to the Unity Profiler.
  12. /// </summary>
  13. /// <typeparam name="T">int, uint, long, ulong, float or double type.</typeparam>
  14. #if ENABLE_PROFILER
  15. [StructLayout(LayoutKind.Sequential)]
  16. #else
  17. [StructLayout(LayoutKind.Sequential, Size = 1)]
  18. #endif
  19. public readonly struct ProfilerCounterValue<T> where T : unmanaged
  20. {
  21. #if ENABLE_PROFILER
  22. [NativeDisableUnsafePtrRestriction]
  23. [NonSerialized]
  24. readonly unsafe T* m_Value;
  25. #endif
  26. /// <summary>
  27. /// Constructs a **ProfilerCounter** that belongs to the generic ProfilerCategory.Scripts category. It is reported at the end of CPU frame to the Unity Profiler.
  28. /// </summary>
  29. /// <param name="name">Name of ProfilerCounter.</param>
  30. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  31. public ProfilerCounterValue(string name)
  32. {
  33. #if ENABLE_PROFILER
  34. byte dataType = ProfilerUtility.GetProfilerMarkerDataType<T>();
  35. unsafe
  36. {
  37. m_Value = (T*)ProfilerUnsafeUtility.CreateCounterValue(out var counterPtr, name, ProfilerUnsafeUtility.CategoryScripts, MarkerFlags.Default, dataType, (byte)ProfilerMarkerDataUnit.Undefined, UnsafeUtility.SizeOf<T>(), ProfilerCounterOptions.FlushOnEndOfFrame);
  38. }
  39. #endif
  40. }
  41. /// <summary>
  42. /// Constructs a **ProfilerCounter** that belongs to the generic ProfilerCategory.Scripts category. It is reported at the end of CPU frame to the Unity Profiler.
  43. /// </summary>
  44. /// <param name="name">Name of ProfilerCounter.</param>
  45. /// <param name="dataUnit">Value unit type.</param>
  46. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  47. public ProfilerCounterValue(string name, ProfilerMarkerDataUnit dataUnit)
  48. {
  49. #if ENABLE_PROFILER
  50. byte dataType = ProfilerUtility.GetProfilerMarkerDataType<T>();
  51. unsafe
  52. {
  53. m_Value = (T*)ProfilerUnsafeUtility.CreateCounterValue(out var counterPtr, name, ProfilerUnsafeUtility.CategoryScripts, MarkerFlags.Default, dataType, (byte)dataUnit, UnsafeUtility.SizeOf<T>(), ProfilerCounterOptions.FlushOnEndOfFrame);
  54. }
  55. #endif
  56. }
  57. /// <summary>
  58. /// Constructs a **ProfilerCounter** that belongs to generic ProfilerCategory.Scripts category.
  59. /// </summary>
  60. /// <param name="name">Name of ProfilerCounter.</param>
  61. /// <param name="dataUnit">Value unit type.</param>
  62. /// <param name="counterOptions">ProfilerCounter options.</param>
  63. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  64. public ProfilerCounterValue(string name, ProfilerMarkerDataUnit dataUnit, ProfilerCounterOptions counterOptions)
  65. {
  66. #if ENABLE_PROFILER
  67. byte dataType = ProfilerUtility.GetProfilerMarkerDataType<T>();
  68. unsafe
  69. {
  70. m_Value = (T*)ProfilerUnsafeUtility.CreateCounterValue(out var counterPtr, name, ProfilerUnsafeUtility.CategoryScripts, MarkerFlags.Default, dataType, (byte)dataUnit, UnsafeUtility.SizeOf<T>(), counterOptions);
  71. }
  72. #endif
  73. }
  74. /// <summary>
  75. /// Constructs a **ProfilerCounter** that is reported at the end of CPU frame to the Unity Profiler.
  76. /// </summary>
  77. /// <param name="category">Profiler category.</param>
  78. /// <param name="name">Name of ProfilerCounter.</param>
  79. /// <param name="dataUnit">Value unit type.</param>
  80. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  81. public ProfilerCounterValue(ProfilerCategory category, string name, ProfilerMarkerDataUnit dataUnit)
  82. {
  83. #if ENABLE_PROFILER
  84. byte dataType = ProfilerUtility.GetProfilerMarkerDataType<T>();
  85. unsafe
  86. {
  87. m_Value = (T*)ProfilerUnsafeUtility.CreateCounterValue(out var counterPtr, name, category, MarkerFlags.Default, dataType, (byte)dataUnit, UnsafeUtility.SizeOf<T>(), ProfilerCounterOptions.FlushOnEndOfFrame);
  88. }
  89. #endif
  90. }
  91. /// <summary>
  92. /// Constructs a **ProfilerCounter**.
  93. /// </summary>
  94. /// <param name="category">Profiler category.</param>
  95. /// <param name="name">Name of ProfilerCounter.</param>
  96. /// <param name="dataUnit">Value unit type.</param>
  97. /// <param name="counterOptions">ProfilerCounter options.</param>
  98. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  99. public ProfilerCounterValue(ProfilerCategory category, string name, ProfilerMarkerDataUnit dataUnit, ProfilerCounterOptions counterOptions)
  100. {
  101. #if ENABLE_PROFILER
  102. byte dataType = ProfilerUtility.GetProfilerMarkerDataType<T>();
  103. unsafe
  104. {
  105. m_Value = (T*)ProfilerUnsafeUtility.CreateCounterValue(out var counterPtr, name, category, MarkerFlags.Default, dataType, (byte)dataUnit, UnsafeUtility.SizeOf<T>(), counterOptions);
  106. }
  107. #endif
  108. }
  109. /// <summary>
  110. /// Gets or sets value of the ProfilerCounter.
  111. /// </summary>
  112. /// <remarks>Returns 0 and is not implemented in Release Players.</remarks>
  113. public T Value
  114. {
  115. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  116. get
  117. {
  118. #if ENABLE_PROFILER
  119. unsafe
  120. {
  121. return *m_Value;
  122. }
  123. #else
  124. return default;
  125. #endif
  126. }
  127. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  128. set
  129. {
  130. #if ENABLE_PROFILER
  131. unsafe
  132. {
  133. *m_Value = value;
  134. }
  135. #endif
  136. }
  137. }
  138. /// <summary>
  139. /// Sends the value to Unity Profiler immediately.
  140. /// </summary>
  141. [Conditional("ENABLE_PROFILER")]
  142. public void Sample()
  143. {
  144. #if ENABLE_PROFILER
  145. unsafe
  146. {
  147. ProfilerUnsafeUtility.FlushCounterValue(m_Value);
  148. }
  149. #endif
  150. }
  151. }
  152. }