Unity 2021.2beta DLSS & FSR Script to switch and change between Modes and Quality

Hi everyone, I'm back !

A little gift to implement quickly FSR and/or DLSS, and change quality mode.

First in your HDRP template do that :
7300066--883789--upload_2021-7-5_19-46-25.png

Now Create 3 dropDown, like that :
First for Modes (Native, DLSS, FSR)
7300066--883819--upload_2021-7-5_19-55-52.png

FSR DropDown (UltraQuality, HighQuality, Quality, Balanced, Performance)
I had HighQuality for better choice
7300066--883816--upload_2021-7-5_19-55-21.png

DLSS DropDown (Maximum Quality, Balanced, Maximum Performance, Ultra Performance)
7300066--883822--upload_2021-7-5_19-56-31.png

For best result with Raytracing, I configure the base on my script (DLSS no need AA, and FSR need TAA Low)
7300066--883825--upload_2021-7-5_19-58-1.png

Finally, the script :

using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;

public class DynamicResolutionManager : MonoBehaviour
{
    public Camera[] DynamicResolutionCamera = new Camera[0];
    private DynamicResolutionType dynamicResolutionType = DynamicResolutionType.None;
    public ResolutionConfiguration[] Configurations = new ResolutionConfiguration[]
    {
        new ResolutionConfiguration
        {
            name = "Native",
            AntialiasingMode = HDAdditionalCameraData.AntialiasingMode.TemporalAntialiasing,
            TAAQualityLevel = HDAdditionalCameraData.TAAQualityLevel.High,
            DynamicResolutionScale = 1,
        },
        new ResolutionConfiguration
        {
            name = "Dlss",
            dLSSQuality = DLSSQuality.MaximumQuality,
            QualityMode = 2,
            AntialiasingMode = HDAdditionalCameraData.AntialiasingMode.None,
            DynamicResolutionScale = 1
        },
        new ResolutionConfiguration
        {
            name = "FSR",
            fsrQuality = FSRQuality.UltraQuality,
            AntialiasingMode = HDAdditionalCameraData.AntialiasingMode.TemporalAntialiasing,
            TAAQualityLevel = HDAdditionalCameraData.TAAQualityLevel.Low,
            DynamicResolutionScale = 1
        }
    };

    private void Start()
    {
        UpdateInfos();
    }

    public void ChangeMode(int enumVal)
    {
        dynamicResolutionType = (DynamicResolutionType)enumVal;

        switch (dynamicResolutionType)
        {
            case DynamicResolutionType.None:
                SwitchToDLSSAndNativeMode();
                break;
            case DynamicResolutionType.DLSS:
                SwitchToDLSSAndNativeMode();
                break;
            case DynamicResolutionType.FSR:
                SwitchToFSRMode();
                break;
        }
    }

    void SwitchToDLSSAndNativeMode()
    {
        ApplyResolution();
        ApplySettingsToCam();
    }
    void SwitchToFSRMode()
    {
        ApplySettingsToCam();
        Configurations[(int)dynamicResolutionType].DynamicResolutionScale = GetFSRDynamicScale(Configurations[(int)dynamicResolutionType].fsrQuality);
        ApplyResolution();
    }

    void ApplySettingsToCam()
    {
        if (DynamicResolutionCamera.Length == 0 || DynamicResolutionCamera[0] == null)
            return;

        ResolutionConfiguration mode = Configurations[(int)dynamicResolutionType];

        foreach (Camera cam in DynamicResolutionCamera)
        {
            if(cam != null)
            {
                HDAdditionalCameraData HDCam = cam.gameObject.GetComponent<HDAdditionalCameraData>();
                HDCam.antialiasing = mode.AntialiasingMode;
                HDCam.TAAQuality = mode.TAAQualityLevel;

                switch (dynamicResolutionType)
                {
                    case DynamicResolutionType.None:
                        HDCam.allowDeepLearningSuperSampling = false;
                        HDCam.allowDynamicResolution = false;
                        break;
                    case DynamicResolutionType.DLSS:
                        HDCam.allowDynamicResolution = true;
                        HDCam.allowDeepLearningSuperSampling = true;
                        HDCam.deepLearningSuperSamplingUseCustomQualitySettings = true;
                        HDCam.deepLearningSuperSamplingQuality = mode.QualityMode;
                        break;
                    case DynamicResolutionType.FSR:
                        HDCam.allowDynamicResolution = true;
                        HDCam.allowDeepLearningSuperSampling = false;
                        break;
                }
            }
        }
    }
    public void ChangeDLSSQuality(int enumQualityVal)
    {
        DLSSQuality dLSSQuality = (DLSSQuality)enumQualityVal;
        uint QVal = 2;
        switch (dLSSQuality)
        {
            case DLSSQuality.MaximumPerformance:
                QVal = 0;
                break;
            case DLSSQuality.Balanced:
                QVal = 1;
                break;
            case DLSSQuality.MaximumQuality:
                QVal = 2;
                break;
            case DLSSQuality.UltraPerformance:
                QVal = 3;
                break;
        }
        Configurations[(int)dynamicResolutionType].QualityMode = QVal;
        ApplySettingsToCam();
    }

    public void ChangeFSRQuality(int enumQualityVal)
    {
        FSRQuality Q = (FSRQuality)enumQualityVal;
        Configurations[(int)dynamicResolutionType].fsrQuality = Q;
        Configurations[(int)dynamicResolutionType].DynamicResolutionScale = GetFSRDynamicScale(Q);
        ApplyResolution();
        UpdateInfos();//To Remove
    }

    float GetFSRDynamicScale(FSRQuality quality)
    {
        float newScale = 1;

        switch (quality)
        {
            case FSRQuality.UltraQuality:
                newScale = .76953125F;
                break;
            case FSRQuality.HighQuality:
                newScale = .73F;
                break;
            case FSRQuality.Quality:
                newScale = .66640625F;
                break;
            case FSRQuality.Balanced:
                newScale = .58828125F;
                break;
            case FSRQuality.Performance:
                newScale = .5f;
                break;
        }
        return newScale;
    }

    void ApplyResolution()
    {
        DynamicResolutionHandler.SetDynamicResScaler(SetDynamicResolutionScale, DynamicResScalePolicyType.ReturnsMinMaxLerpFactor);
    }

    float SetDynamicResolutionScale()
    {
        return Configurations[(int)dynamicResolutionType].DynamicResolutionScale;
    }

    //Debug Change Quality Mode
    void UpdateInfos()
    {
        float m_Scale = Configurations[(int)dynamicResolutionType].DynamicResolutionScale;
        int rezWidth = Mathf.RoundToInt(m_Scale * Screen.currentResolution.width);
        int rezHeight = Mathf.RoundToInt(m_Scale * Screen.currentResolution.height);
        Debug.Log(string.Format("Scale: {0:F3}x{1:F3}\nResolution: {2}x{3}\n",
            m_Scale,
            m_Scale,
            rezWidth,
            rezHeight));
    }
}

public class ResolutionConfiguration
{
    public string name;
    public DLSSQuality dLSSQuality;
    public uint QualityMode;
    public FSRQuality fsrQuality;
    public float DynamicResolutionScale;
    public HDAdditionalCameraData.AntialiasingMode AntialiasingMode;
    public HDAdditionalCameraData.TAAQualityLevel TAAQualityLevel;
}

public enum DynamicResolutionType
{
    None,
    DLSS,
    FSR
}

public enum DLSSQuality
{
    MaximumQuality,
    Balanced,
    MaximumPerformance,
    UltraPerformance
}
public enum FSRQuality
{
    UltraQuality,
    HighQuality,
    Quality,
    Balanced,
    Performance
}

Enjoy !

8 Likes

I love you, this is the exact problem I've tried to solve an hour ago.

Your welcome :wink:

Wow, this is great stuff. However, seems that with current version, one must set "Use Optimal Settings" for DLSS to kick in. Seems that it does not render at lower resolution without this setting.

Thank you, thought the way I did it worked but either way this seems much better. Will make sure to give credit. Thank you.