using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine.Rendering; #if UNITY_RENDER_PIPELINES_UNIVERSAL using UnityEngine.Rendering.Universal; #endif // UNITY_RENDER_PIPELINES_UNIVERSAL #if UNITY_EDITOR using UnityEditor; #endif namespace UnityEngine.XR.OpenXR.Features { /// /// This enables the use of Automatic Viewport Dynamic Resolution in OpenXR. /// #if UNITY_EDITOR && UNITY_6000_3_OR_NEWER && UNITY_RENDER_PIPELINES_UNIVERSAL [UnityEditor.XR.OpenXR.Features.OpenXRFeature( UiName = k_UIName, Desc = @"Automatic Viewport Dynamic Resolution", Company = "Unity", DocumentationLink = "", OpenxrExtensionStrings = k_OpenXRRequestedExtensions, Version = "1.0.0", BuildTargetGroups = new[] { BuildTargetGroup.Android }, FeatureId = k_DynamicResolutionFeatureId)] #endif public class AutomaticDynamicResolutionFeature : OpenXRFeature { /// /// The minimum possible value of the resolution scalars /// public const float minResolutionScalarLimit = 0.05f; /// /// The maximum possible value of the resolution scalars. /// public const float maxResolutionScalarLimit = 2.0f; /// /// The UI name that shows on the XR Plug-in Management panel. /// internal const string k_UIName = "Automatic Viewport Dynamic Resolution"; /// /// The required OpenXR extensions. /// internal const string k_OpenXRRequestedExtensions = "XR_META_recommended_layer_resolution XR_ANDROID_recommended_resolution"; /// /// The feature ID string. This is used to give the feature a well known ID for reference. /// internal const string k_DynamicResolutionFeatureId = "com.unity.openxr.feature.ViewportDynamicResolution"; /// /// The initial value of the minimum resolution scalar /// internal const float k_InitialMinResolutionScalar = 0.05f; /// /// The initial value of the maximum resolution scalar /// internal const float k_InitialMaxResolutionScalar = 1.0f; /// /// Stores the current minimum resolution scalar. /// [SerializeField] [Tooltip("Select the minimum resolution scalar.")] internal float m_MinResolutionScalar = k_InitialMinResolutionScalar; /// /// Stores the current maximum resolution scalar /// [SerializeField] [Tooltip("Select the maximum resolution scalar.")] internal float m_MaxResolutionScalar = k_InitialMaxResolutionScalar; /// /// Stores the current minimum resolution scalar. It is bounded from below /// by , and from above by the current /// value of . /// /// /// Only the getter is public. You can change this value in: /// "Project Settings -> XR Plug-in Management -> OpenXR -> /// Automatic Viewport Dynamic Resolution" /// public float minResolutionScalar { get => m_MinResolutionScalar; private set { if (OpenXRLoaderBase.Instance != null) { if (value > m_MaxResolutionScalar) m_MinResolutionScalar = m_MaxResolutionScalar; else m_MinResolutionScalar = value; } } } /// /// Stores the current maximum resolution scalar. It is bounded from below /// by the current value of , and from above /// by . /// /// /// Only the getter is public. You can change this value in: /// "Project Settings -> XR Plug-in Management -> OpenXR -> /// Automatic Viewport Dynamic Resolution" /// public float maxResolutionScalar { get => m_MaxResolutionScalar; private set { if (OpenXRLoaderBase.Instance != null) { if (value < m_MinResolutionScalar) m_MaxResolutionScalar = m_MinResolutionScalar; else m_MaxResolutionScalar = value; } } } /// /// Are we currently using the suggested resolution provided by the extension(s), /// or are we disregarding it and not updating resolution automatically. /// Default is . /// /// /// Call to change this value. /// Only the getter is public. /// public static bool usingSuggestedResolutionScale { get; private set; } = true; /// protected internal override bool OnInstanceCreate(ulong instance) { if (!enabled || !Internal_IsAutomaticDynamicResolutionScalingSupported()) return false; Internal_SetUsingSuggestedResolutionScale(usingSuggestedResolutionScale); Internal_SetMinMaxScalerResolution(minResolutionScalar, maxResolutionScalar); var displaySubsystem = GetFirstDisplaySubsystem(); if (displaySubsystem != null) displaySubsystem.scaleOfAllRenderTargets = maxResolutionScalar; #if UNITY_RENDER_PIPELINES_UNIVERSAL RenderPipelineAsset currentRenderPipelineAsset = GraphicsSettings.currentRenderPipeline; UniversalRenderPipelineAsset urpAsset = currentRenderPipelineAsset as UniversalRenderPipelineAsset; if (urpAsset != null) urpAsset.renderScale = maxResolutionScalar; #endif // UNITY_RENDER_PIPELINES_UNIVERSAL return base.OnInstanceCreate(instance); } /// /// Determines if automatic dynamic resolution scaling is supported. /// /// /// Returns if automatic dynamic resolution scaling /// is supported, and returns if it is not. /// public static bool IsAutomaticDynamicResolutionScalingSupported() { return Internal_IsAutomaticDynamicResolutionScalingSupported(); } /// /// Set whether or not we are using the suggested resolution provided by /// the extensions, rather than disregarding it and not updating resolution /// automatically. Default is . /// /// /// The flag whether or not to use the suggested resolution and update the /// resolution automatically. /// public static void SetUsingSuggestedResolutionScale(bool usingSuggestedScale) { usingSuggestedResolutionScale = usingSuggestedScale; Internal_SetUsingSuggestedResolutionScale(usingSuggestedScale); } /// /// A helper to return the first available XRDisplaySubsystem. /// static XRDisplaySubsystem GetFirstDisplaySubsystem() { List displays = new List(); SubsystemManager.GetSubsystems(displays); if (displays.Count == 0) return null; return displays[0]; } #if UNITY_EDITOR [CustomEditor(typeof(AutomaticDynamicResolutionFeature))] internal class DynamicResolutionSettingsEditor : Editor { // variables to store the state of the min resolution scalar slider private float prevMinResolutionScalar; private SerializedProperty minResolutionScalar; static GUIContent s_MinResolutionLabel = EditorGUIUtility.TrTextContent("Min Resolution Scalar"); // variables to store the state of the max resolution scalar slider private float prevMaxResolutionScalar; private SerializedProperty maxResolutionScalar; static GUIContent s_MaxResolutionLabel = EditorGUIUtility.TrTextContent("Max Resolution Scalar"); void OnEnable() { minResolutionScalar = serializedObject.FindProperty("m_MinResolutionScalar"); prevMinResolutionScalar = minResolutionScalar.floatValue; maxResolutionScalar = serializedObject.FindProperty("m_MaxResolutionScalar"); prevMaxResolutionScalar = maxResolutionScalar.floatValue; } public override void OnInspectorGUI() { serializedObject.Update(); EditorGUILayout.Slider( minResolutionScalar, minResolutionScalarLimit, maxResolutionScalarLimit, s_MinResolutionLabel); EditorGUILayout.Slider( maxResolutionScalar, minResolutionScalarLimit, maxResolutionScalarLimit, s_MaxResolutionLabel); // If min slider is adjusted, prevent min slider value from going above max slider value. // Or, if max slider is adjusted, prevent max slider value from going below min slider value. if (minResolutionScalar.floatValue != prevMinResolutionScalar && minResolutionScalar.floatValue > maxResolutionScalar.floatValue) { minResolutionScalar.floatValue = maxResolutionScalar.floatValue; } else if (maxResolutionScalar.floatValue != prevMaxResolutionScalar && maxResolutionScalar.floatValue < minResolutionScalar.floatValue) { maxResolutionScalar.floatValue = minResolutionScalar.floatValue; } prevMinResolutionScalar = minResolutionScalar.floatValue; prevMaxResolutionScalar = maxResolutionScalar.floatValue; serializedObject.ApplyModifiedProperties(); } } #endif // Import functions from the UnityOpenXR dll private const string k_Library = "UnityOpenXR"; [DllImport(k_Library, EntryPoint = "DynamicResolution_IsAutomaticDynamicResolutionScalingSupported")] [return: MarshalAs(UnmanagedType.U1)] private static extern bool Internal_IsAutomaticDynamicResolutionScalingSupported(); [DllImport(k_Library, EntryPoint = "DynamicResolution_SetMinMaxScalerResolution")] private static extern void Internal_SetMinMaxScalerResolution(float min, float max); [DllImport(k_Library, EntryPoint = "DynamicResolution_SetUsingSuggestedResolutionScale")] private static extern void Internal_SetUsingSuggestedResolutionScale([MarshalAs(UnmanagedType.I1)] bool usingSuggestedScale); } }