暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AdaptiveResolution.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using UnityEngine.Rendering;
  2. namespace UnityEngine.AdaptivePerformance
  3. {
  4. /// <summary>
  5. /// A scaler used by <see cref="AdaptivePerformanceIndexer"/> to adjust the resolution of all render targets that allow dynamic resolution.
  6. /// If a device or graphics API doesn't support a dynamic resolution, it will use the rendering pipeline's render scale multiplier as a fallback.
  7. /// </summary>
  8. public class AdaptiveResolution : AdaptivePerformanceScaler
  9. {
  10. static int instanceCount = 0;
  11. /// <summary>
  12. /// Ensures settings are applied during startup.
  13. /// </summary>
  14. protected override void Awake()
  15. {
  16. base.Awake();
  17. if (m_Settings == null)
  18. return;
  19. ApplyDefaultSetting(m_Settings.scalerSettings.AdaptiveResolution);
  20. }
  21. /// <summary>
  22. /// Callback when scaler gets disabled and removed from indexer
  23. /// </summary>
  24. protected override void OnDisabled()
  25. {
  26. OnDestroy();
  27. }
  28. /// <summary>
  29. /// Callback when scaler gets enabled and added to the indexer
  30. /// </summary>
  31. protected override void OnEnabled() {}
  32. void OnValidate()
  33. {
  34. if (MaxLevel < 1)
  35. MaxLevel = 1;
  36. MaxBound = Mathf.Clamp(MaxBound, 0.25f, 1.0f);
  37. MinBound = Mathf.Clamp(MinBound, 0.25f, MaxBound);
  38. }
  39. // TODO: expose dynamicResolution capability through SystemInfo
  40. private bool IsDynamicResolutionSupported()
  41. {
  42. #if UNITY_XBOXONE || UNITY_PS4 || UNITY_SWITCH || UNITY_IOS || UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  43. return true;
  44. #elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_TVOS // metal only
  45. return SystemInfo.graphicsDeviceType == GraphicsDeviceType.Metal;
  46. #elif UNITY_ANDROID
  47. return SystemInfo.graphicsDeviceType == GraphicsDeviceType.Vulkan;
  48. #elif UNITY_WSA
  49. return SystemInfo.graphicsDeviceType == GraphicsDeviceType.Direct3D12;
  50. #else
  51. return false;
  52. #endif
  53. }
  54. private void Start()
  55. {
  56. ++instanceCount;
  57. if (instanceCount > 1)
  58. Debug.LogWarning("Multiple Adaptive Resolution scalers created. They will interfere with each other.");
  59. if (!IsDynamicResolutionSupported())
  60. Debug.Log(string.Format("Dynamic resolution is not supported. Will be using fallback to Render Scale Multiplier."));
  61. }
  62. private void OnDestroy()
  63. {
  64. --instanceCount;
  65. if (Scale == 1.0f)
  66. return;
  67. APLog.Debug("Restoring dynamic resolution scale factor to 1.0");
  68. if (IsDynamicResolutionSupported())
  69. ScalableBufferManager.ResizeBuffers(1.0f, 1.0f);
  70. else
  71. AdaptivePerformanceRenderSettings.RenderScaleMultiplier = 1.0f;
  72. }
  73. /// <summary>
  74. /// Callback for any level change
  75. /// </summary>
  76. protected override void OnLevel()
  77. {
  78. var scaleChange = ScaleChanged();
  79. // if Gfx API does not support Dynamic resolution, currentCamera.allowDynamicResolution will be false
  80. if (IsDynamicResolutionSupported())
  81. {
  82. if (scaleChange)
  83. ScalableBufferManager.ResizeBuffers(Scale, Scale);
  84. int rezWidth = (int)Mathf.Ceil(ScalableBufferManager.widthScaleFactor * Screen.currentResolution.width);
  85. int rezHeight = (int)Mathf.Ceil(ScalableBufferManager.heightScaleFactor * Screen.currentResolution.height);
  86. APLog.Debug(
  87. $"Adaptive Resolution Scale: {Scale:F3} Resolution: {rezWidth}x{rezHeight} ScaleFactor: {ScalableBufferManager.widthScaleFactor:F3}x{ScalableBufferManager.heightScaleFactor:F3} Level:{CurrentLevel}/{MaxLevel}");
  88. }
  89. else
  90. {
  91. AdaptivePerformanceRenderSettings.RenderScaleMultiplier = Scale;
  92. APLog.Debug($"Dynamic resolution is not supported. Using fallback to Render Scale Multiplier : {Scale:F3}");
  93. // TODO: warn if unsupported render pipeline is used
  94. //Debug.Log("You might not use a supported Render Pipeline. Currently only Universal Render Pipeline and Built-in are supported by Adaptive Resolution.");
  95. }
  96. }
  97. }
  98. }