Sin descripción
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.

AdaptivePerformanceScaler.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. using UnityEngine.Scripting;
  2. #if VISUAL_SCRIPTING_ENABLED
  3. using Unity.VisualScripting;
  4. using UnityEngine.AdaptivePerformance.VisualScripting;
  5. #endif
  6. namespace UnityEngine.AdaptivePerformance
  7. {
  8. /// <summary>
  9. /// Scaler impact on visuals.
  10. /// </summary>
  11. public enum ScalerVisualImpact
  12. {
  13. /// <summary>
  14. /// Low impact on visual quality. Changes might not be very noticeable to the user.
  15. /// </summary>
  16. Low,
  17. /// <summary>
  18. /// Medium impact on visual quality. Mildly affects the visuals in the scene and might be noticeable to the user.
  19. /// </summary>
  20. Medium,
  21. /// <summary>
  22. /// High impact on visual quality. The scaler will have an easily visible effect on quality.
  23. /// </summary>
  24. High
  25. }
  26. /// <summary>
  27. /// Bottleneck flags that the scaler targets.
  28. /// </summary>
  29. [System.Flags]
  30. public enum ScalerTarget
  31. {
  32. /// <summary>
  33. /// The scaler targets the CPU and attempts to reduce the CPU load.
  34. /// </summary>
  35. CPU = 0x1,
  36. /// <summary>
  37. /// The scaler targets the GPU and attempts to reduce the GPU load.
  38. /// </summary>
  39. GPU = 0x2,
  40. /// <summary>
  41. /// The scaler targets fillrate, often at the expense of visual quality.
  42. /// </summary>
  43. FillRate = 0x4
  44. }
  45. /// <summary>
  46. /// Abstract class representing single feature that is controlled by <see cref="AdaptivePerformanceIndexer"/>.
  47. /// You control the quality through changing the levels, where 0 represents the controller not being applied and 1,2... as applied.
  48. /// As a result, a higher level represents lower visuals, but better performance.
  49. /// </summary>
  50. #if UNITY_2021_2_OR_NEWER
  51. [RequireDerived]
  52. #endif
  53. public abstract class AdaptivePerformanceScaler : ScriptableObject
  54. {
  55. private AdaptivePerformanceIndexer m_Indexer;
  56. /// <summary>
  57. /// Returns a string with the name of the scaler.
  58. /// </summary>
  59. public virtual string Name
  60. {
  61. get => m_defaultSetting.name;
  62. set
  63. {
  64. if (m_defaultSetting.name == value)
  65. return;
  66. m_defaultSetting.name = value;
  67. }
  68. }
  69. /// <summary>
  70. /// Returns `true` if this scaler is active, otherwise returns `false`.
  71. /// </summary>
  72. public virtual bool Enabled
  73. {
  74. get => m_defaultSetting.enabled;
  75. set
  76. {
  77. if (m_defaultSetting.enabled == value)
  78. return;
  79. m_defaultSetting.enabled = value;
  80. AdaptivePerformanceAnalytics.SendAdaptiveFeatureUpdateEvent(Name, m_defaultSetting.enabled);
  81. }
  82. }
  83. /// <summary>
  84. /// Returns a generic scale of this scaler in range [0,1] which is applied to the quality.
  85. /// </summary>
  86. public virtual float Scale
  87. {
  88. get => m_defaultSetting.scale;
  89. set
  90. {
  91. if (m_defaultSetting.scale == value)
  92. return;
  93. m_defaultSetting.scale = value;
  94. }
  95. }
  96. /// <summary>
  97. /// Returns the visual impact of scaler when applied.
  98. /// </summary>
  99. public virtual ScalerVisualImpact VisualImpact
  100. {
  101. get => m_defaultSetting.visualImpact;
  102. set
  103. {
  104. if (m_defaultSetting.visualImpact == value)
  105. return;
  106. m_defaultSetting.visualImpact = value;
  107. }
  108. }
  109. /// <summary>
  110. /// Returns the bottlenecks that this scaler targets.
  111. /// </summary>
  112. public virtual ScalerTarget Target
  113. {
  114. get => m_defaultSetting.target;
  115. set
  116. {
  117. if (m_defaultSetting.target == value)
  118. return;
  119. m_defaultSetting.target = value;
  120. }
  121. }
  122. /// <summary>
  123. /// Returns the maximum level of this scaler.
  124. /// </summary>
  125. public virtual int MaxLevel
  126. {
  127. get => m_defaultSetting.maxLevel;
  128. set
  129. {
  130. if (m_defaultSetting.maxLevel == value)
  131. return;
  132. m_defaultSetting.maxLevel = value;
  133. }
  134. }
  135. /// <summary>
  136. /// Returns the minimum boundary of this scaler.
  137. /// </summary>
  138. public virtual float MinBound
  139. {
  140. get => m_defaultSetting.minBound;
  141. set
  142. {
  143. if (m_defaultSetting.minBound == value)
  144. return;
  145. m_defaultSetting.minBound = value;
  146. }
  147. }
  148. /// <summary>
  149. /// Returns the maximum boundary of this scaler.
  150. /// </summary>
  151. public virtual float MaxBound
  152. {
  153. get => m_defaultSetting.maxBound;
  154. set
  155. {
  156. if (m_defaultSetting.maxBound == value)
  157. return;
  158. m_defaultSetting.maxBound = value;
  159. }
  160. }
  161. /// <summary>
  162. /// Returns the current level of scale.
  163. /// </summary>
  164. public int CurrentLevel { get; private set; }
  165. /// <summary>
  166. /// Returns `true` if the scaler can no longer be applied, otherwise returns `false`.
  167. /// </summary>
  168. public bool IsMaxLevel { get => CurrentLevel == MaxLevel; }
  169. /// <summary>
  170. /// Returns `true` if the scaler is not applied, otherwise returns `false`.
  171. /// </summary>
  172. public bool NotLeveled { get => CurrentLevel == 0; }
  173. /// <summary>
  174. /// Scaler impact on GPU so far in milliseconds.
  175. /// </summary>
  176. public int GpuImpact { get; internal set; }
  177. /// <summary>
  178. /// Scaler impact on CPU so far in milliseconds.
  179. /// </summary>
  180. public int CpuImpact { get; internal set; }
  181. int m_OverrideLevel = -1;
  182. AdaptivePerformanceScalerSettingsBase m_defaultSetting = new AdaptivePerformanceScalerSettingsBase();
  183. #if VISUAL_SCRIPTING_ENABLED
  184. AdaptivePerformanceScalerEvent m_ScalerEvent;
  185. #endif
  186. /// <summary>
  187. /// Settings reference for all scalers.
  188. /// </summary>
  189. protected IAdaptivePerformanceSettings m_Settings;
  190. /// <summary>
  191. /// Allows you to manually override the scaler level.
  192. /// If the value is -1, <see cref="AdaptivePerformanceIndexer"/> will handle levels, otherwise scaler will be forced to the value you specify.
  193. /// </summary>
  194. public int OverrideLevel
  195. {
  196. get => m_OverrideLevel;
  197. set
  198. {
  199. m_OverrideLevel = value;
  200. m_Indexer.UpdateOverrideLevel(this);
  201. }
  202. }
  203. /// <summary>
  204. /// Calculate the cost of applying this particular scaler.
  205. /// </summary>
  206. /// <returns>Cost of scaler ranges from 0 to infinity.</returns>
  207. public int CalculateCost()
  208. {
  209. var bottleneck = Holder.Instance.PerformanceStatus.PerformanceMetrics.PerformanceBottleneck;
  210. var cost = 0;
  211. switch (VisualImpact)
  212. {
  213. case ScalerVisualImpact.Low:
  214. cost += CurrentLevel * 1;
  215. break;
  216. case ScalerVisualImpact.Medium:
  217. cost += CurrentLevel * 2;
  218. break;
  219. case ScalerVisualImpact.High:
  220. cost += CurrentLevel * 3;
  221. break;
  222. }
  223. // Bottleneck should always be best priority
  224. if (bottleneck == PerformanceBottleneck.CPU && (Target & ScalerTarget.CPU) == 0)
  225. cost = 6;
  226. if (bottleneck == PerformanceBottleneck.GPU && (Target & ScalerTarget.GPU) == 0)
  227. cost = 6;
  228. if (bottleneck == PerformanceBottleneck.TargetFrameRate && (Target & ScalerTarget.FillRate) == 0)
  229. cost = 6;
  230. return cost;
  231. }
  232. /// <summary>
  233. /// Ensures settings are applied during startup.
  234. /// </summary>
  235. protected virtual void Awake()
  236. {
  237. if (Holder.Instance == null)
  238. return;
  239. m_Settings = Holder.Instance.Settings;
  240. m_Indexer = Holder.Instance.Indexer;
  241. }
  242. private void OnEnable()
  243. {
  244. if (m_Indexer == null)
  245. return;
  246. m_Indexer.AddScaler(this);
  247. AdaptivePerformanceAnalytics.RegisterFeature(Name, Enabled);
  248. AdaptivePerformanceAnalytics.SendAdaptiveFeatureUpdateEvent(Name, Enabled);
  249. OnEnabled();
  250. }
  251. private void OnDisable()
  252. {
  253. if (m_Indexer == null)
  254. return;
  255. m_Indexer.RemoveScaler(this);
  256. OnDisabled();
  257. }
  258. internal void IncreaseLevel()
  259. {
  260. if (IsMaxLevel)
  261. {
  262. Debug.LogError("Cannot increase scaler level as it is already max.");
  263. return;
  264. }
  265. CurrentLevel++;
  266. OnLevelIncrease();
  267. OnLevel();
  268. #if VISUAL_SCRIPTING_ENABLED
  269. m_ScalerEvent.Name = Name;
  270. m_ScalerEvent.Level = CurrentLevel;
  271. EventBus.Trigger(AdaptivePerformanceEventHooks.OnScalerEvent, m_ScalerEvent);
  272. #endif
  273. }
  274. internal void DecreaseLevel()
  275. {
  276. if (NotLeveled)
  277. {
  278. Debug.LogError("Cannot decrease scaler level as it is already 0.");
  279. return;
  280. }
  281. CurrentLevel--;
  282. OnLevelDecrease();
  283. OnLevel();
  284. #if VISUAL_SCRIPTING_ENABLED
  285. m_ScalerEvent.Name = Name;
  286. m_ScalerEvent.Level = CurrentLevel;
  287. EventBus.Trigger(AdaptivePerformanceEventHooks.OnScalerEvent, m_ScalerEvent);
  288. #endif
  289. }
  290. internal void Activate()
  291. {
  292. OnEnabled();
  293. }
  294. internal void Deactivate()
  295. {
  296. OnDisabled();
  297. }
  298. /// <summary>
  299. /// Any scaler with settings in <see cref="IAdaptivePerformanceSettings"/> needs to call this method and provide the scaler specific setting. Unity uses the setting arguments in the base-scaler as the default settings.
  300. /// This is also used by Scaler Profiles to apply their Settings.
  301. /// </summary>
  302. /// <param name="defaultSetting">The settings to apply to the scaler.</param>
  303. public void ApplyDefaultSetting(AdaptivePerformanceScalerSettingsBase defaultSetting)
  304. {
  305. m_defaultSetting = defaultSetting;
  306. }
  307. /// <summary>
  308. /// Checks if scale changed based on the current level and returns true if scale changed false otherwise.
  309. /// </summary>
  310. /// <returns>Returns true if scale changed false otherwise.</returns>
  311. protected bool ScaleChanged()
  312. {
  313. float oldScaleFactor = Scale;
  314. float scaleIncrement = (MaxBound - MinBound) / MaxLevel;
  315. Scale = scaleIncrement * (MaxLevel - CurrentLevel) + MinBound;
  316. return Scale != oldScaleFactor;
  317. }
  318. /// <summary>
  319. /// Callback for when the performance level is increased.
  320. /// </summary>
  321. protected virtual void OnLevelIncrease() {}
  322. /// <summary>
  323. /// Callback for when the performance level is decreased.
  324. /// </summary>
  325. protected virtual void OnLevelDecrease() {}
  326. /// <summary>
  327. /// Callback for any level change
  328. /// </summary>
  329. protected virtual void OnLevel() {}
  330. /// <summary>
  331. /// Callback when scaler gets enabled and added to the indexer
  332. /// </summary>
  333. protected virtual void OnEnabled() {}
  334. /// <summary>
  335. /// Callback when scaler gets disabled and removed from indexer
  336. /// </summary>
  337. protected virtual void OnDisabled() {}
  338. }
  339. }