Skip to content

Commit 159dfa8

Browse files
rjonaitislukaschod
andauthored
[8.x.x] backport #375 Add adaptive performance as optional dependency (#513)
* Adding adaptive performance as optional dependency * Update adaptive performance package version and defines * Add enable flag for adaptive performance in URP asset * Add Adaptive performance reference to URP Editor asmdef * Revert "Add Adaptive performance reference to URP Editor asmdef" This reverts commit 28e2f21. * Enable adaptive performance by default if available * Add adaptive performance documentation * Update changelog.md Co-authored-by: Lukas Chodosevicius <[email protected]>
1 parent 8190ab0 commit 159dfa8

File tree

7 files changed

+147
-2
lines changed

7 files changed

+147
-2
lines changed

com.unity.render-pipelines.universal/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
### Added
10+
- Added option to enable/disable Adaptive Performance when it's package is available.
11+
912
### Changed
1013
- The 2D Renderer now supports camera stacking.
1114

com.unity.render-pipelines.universal/Documentation~/universalrp-asset.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ In the URP, you can configure settings for:
1616
- [__Shadows__](#shadows)
1717
- [__Post-processing__](#post-processing)
1818
- [__Advanced__](#advanced)
19+
- [__Adaptive Performance__](#adaptive-performance)
1920

2021

2122

@@ -100,3 +101,13 @@ This section allows you to fine-tune less commonly changed settings, which impac
100101
| __Mixed Lighting__ | Enable [Mixed Lighting](https://docs.unity3d.com/Manual/LightMode-Mixed.html), to tell the pipeline to include mixed lighting shader variants in the build. |
101102
| __Debug Level__ | Set the level of debug information that the render pipeline generates. The values are:<br />**Disabled**: Debugging is disabled. This is the default.<br />**Profiling**: Makes the render pipeline provide detailed information tags, which you can see in the FrameDebugger. |
102103
| __Shader Variant Log Level__ | Set the level of information about Shader Stripping and Shader Variants you want to display when Unity finishes a build. Values are:<br /> **Disabled**: Unity doesn’t log anything.<br />**Only Universal**: Unity logs information for all of the [URP Shaders](shaders-in-universalrp.md).<br />**All**: Unity logs information for all Shaders in your build.<br /> You can see the information in Console panel when your build has finished. |
104+
105+
106+
107+
### Adaptive Performance
108+
109+
This section appears if Adaptive Performance package is installed. It allows to change settings how Adaptive performance and render pipeline interact.
110+
111+
| __Property__ | __Description__ |
112+
| ----------------------- | ------------------------------------------------------------ |
113+
| __Use adaptive performance__ | Allows Adaptive Performance to adjust rendering quality during runtime. |

com.unity.render-pipelines.universal/Editor/Unity.RenderPipelines.Universal.Editor.asmdef

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,11 @@
1717
"precompiledReferences": [],
1818
"autoReferenced": true,
1919
"defineConstraints": [],
20-
"versionDefines": []
20+
"versionDefines": [
21+
{
22+
"name": "com.unity.adaptiveperformance",
23+
"expression": "2.0.0-preview.7",
24+
"define": "ADAPTIVE_PERFORMANCE_2_0_0_OR_NEWER"
25+
}
26+
]
2127
}

com.unity.render-pipelines.universal/Editor/UniversalRenderPipelineAssetEditor.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ internal class Styles
1818
public static GUIContent shadowSettingsText = EditorGUIUtility.TrTextContent("Shadows");
1919
public static GUIContent postProcessingSettingsText = EditorGUIUtility.TrTextContent("Post-processing");
2020
public static GUIContent advancedSettingsText = EditorGUIUtility.TrTextContent("Advanced");
21+
public static GUIContent adaptivePerformanceText = EditorGUIUtility.TrTextContent("Adaptive Performance");
2122

2223
// General
2324
public static GUIContent rendererHeaderText = EditorGUIUtility.TrTextContent("Renderer List", "Lists all the renderers available to this Render Pipeline Asset.");
@@ -68,6 +69,9 @@ internal class Styles
6869
public static GUIContent debugLevel = EditorGUIUtility.TrTextContent("Debug Level", "Controls the level of debug information generated by the render pipeline. When Profiling is selected, the pipeline provides detailed profiling tags.");
6970
public static GUIContent shaderVariantLogLevel = EditorGUIUtility.TrTextContent("Shader Variant Log Level", "Controls the level logging in of shader variants information is outputted when a build is performed. Information will appear in the Unity console when the build finishes.");
7071

72+
// Adaptive performance settings
73+
public static GUIContent useAdaptivePerformance = EditorGUIUtility.TrTextContent("Use adaptive performance", "Allows Adaptive Performance to adjust rendering quality during runtime");
74+
7175
// Renderer List Messages
7276
public static GUIContent rendererListDefaultMessage =
7377
EditorGUIUtility.TrTextContent("Cannot remove Default Renderer",
@@ -90,6 +94,7 @@ internal class Styles
9094
SavedBool m_ShadowSettingsFoldout;
9195
SavedBool m_PostProcessingSettingsFoldout;
9296
SavedBool m_AdvancedSettingsFoldout;
97+
SavedBool m_AdaptivePerformanceFoldout;
9398

9499
SerializedProperty m_RendererDataProp;
95100
SerializedProperty m_DefaultRendererProp;
@@ -133,6 +138,8 @@ internal class Styles
133138
SerializedProperty m_ColorGradingMode;
134139
SerializedProperty m_ColorGradingLutSize;
135140

141+
SerializedProperty m_UseAdaptivePerformance;
142+
136143
public override void OnInspectorGUI()
137144
{
138145
serializedObject.Update();
@@ -143,6 +150,9 @@ public override void OnInspectorGUI()
143150
DrawShadowSettings();
144151
DrawPostProcessingSettings();
145152
DrawAdvancedSettings();
153+
#if ADAPTIVE_PERFORMANCE_2_0_0_OR_NEWER
154+
DrawAdaptivePerformance();
155+
#endif
146156

147157
serializedObject.ApplyModifiedProperties();
148158
}
@@ -155,6 +165,7 @@ void OnEnable()
155165
m_ShadowSettingsFoldout = new SavedBool($"{target.GetType()}.ShadowSettingsFoldout", false);
156166
m_PostProcessingSettingsFoldout = new SavedBool($"{target.GetType()}.PostProcessingSettingsFoldout", false);
157167
m_AdvancedSettingsFoldout = new SavedBool($"{target.GetType()}.AdvancedSettingsFoldout", false);
168+
m_AdaptivePerformanceFoldout = new SavedBool($"{target.GetType()}.AdaptivePerformanceFoldout", false);
158169

159170
m_RendererDataProp = serializedObject.FindProperty("m_RendererDataList");
160171
m_DefaultRendererProp = serializedObject.FindProperty("m_DefaultRendererIndex");
@@ -198,6 +209,8 @@ void OnEnable()
198209
m_ColorGradingMode = serializedObject.FindProperty("m_ColorGradingMode");
199210
m_ColorGradingLutSize = serializedObject.FindProperty("m_ColorGradingLutSize");
200211

212+
m_UseAdaptivePerformance = serializedObject.FindProperty("m_UseAdaptivePerformance");
213+
201214
selectedLightRenderingMode = (LightRenderingMode)m_AdditionalLightsRenderingModeProp.intValue;
202215
}
203216

@@ -381,6 +394,20 @@ void DrawAdvancedSettings()
381394
EditorGUILayout.EndFoldoutHeaderGroup();
382395
}
383396

397+
void DrawAdaptivePerformance()
398+
{
399+
m_AdaptivePerformanceFoldout.value = EditorGUILayout.BeginFoldoutHeaderGroup(m_AdaptivePerformanceFoldout.value, Styles.adaptivePerformanceText);
400+
if (m_AdaptivePerformanceFoldout.value)
401+
{
402+
EditorGUI.indentLevel++;
403+
EditorGUILayout.PropertyField(m_UseAdaptivePerformance, Styles.useAdaptivePerformance);
404+
EditorGUI.indentLevel--;
405+
EditorGUILayout.Space();
406+
EditorGUILayout.Space();
407+
}
408+
EditorGUILayout.EndFoldoutHeaderGroup();
409+
}
410+
384411
void DrawRendererListLayout(ReorderableList list, SerializedProperty prop)
385412
{
386413
list.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>

com.unity.render-pipelines.universal/Runtime/Data/UniversalRenderPipelineAsset.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ public class UniversalRenderPipelineAsset : RenderPipelineAsset, ISerializationC
155155
[SerializeField] bool m_MixedLightingSupported = true;
156156
[SerializeField] PipelineDebugLevel m_DebugLevel = PipelineDebugLevel.Disabled;
157157

158+
// Adaptive performance settings
159+
[SerializeField] bool m_UseAdaptivePerformance = true;
160+
158161
// Post-processing settings
159162
[SerializeField] ColorGradingMode m_ColorGradingMode = ColorGradingMode.LowDynamicRange;
160163
[SerializeField] int m_ColorGradingLutSize = 32;
@@ -618,6 +621,16 @@ public int colorGradingLutSize
618621
set { m_ColorGradingLutSize = Mathf.Clamp(value, k_MinLutSize, k_MaxLutSize); }
619622
}
620623

624+
/// <summary>
625+
/// Set to true to allow Adaptive performance to modify graphics quality settings during runtime.
626+
/// Only applicable when Adaptive performance package is available.
627+
/// </summary>
628+
public bool useAdaptivePerformance
629+
{
630+
get { return m_UseAdaptivePerformance; }
631+
set { m_UseAdaptivePerformance = value; }
632+
}
633+
621634
public override Material defaultMaterial
622635
{
623636
get { return GetMaterial(DefaultMaterialType.Standard); }

com.unity.render-pipelines.universal/Runtime/Unity.RenderPipelines.Universal.Runtime.asmdef

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"references": [
44
"GUID:d60799ab2a985554ea1a39cd38695018",
55
"GUID:df380645f10b7bc4b97d4f5eb6303d95",
6-
"GUID:ab67fb10353d84448ac887a7367cbda8"
6+
"GUID:ab67fb10353d84448ac887a7367cbda8",
7+
"GUID:7dbf32976982c98448af054f2512cb79"
78
],
89
"includePlatforms": [],
910
"excludePlatforms": [],
@@ -18,6 +19,11 @@
1819
"expression": "0.0.1",
1920
"define": "VISUAL_EFFECT_GRAPH_0_0_1_OR_NEWER"
2021
},
22+
{
23+
"name": "com.unity.adaptiveperformance",
24+
"expression": "2.0.0-preview.7",
25+
"define": "ADAPTIVE_PERFORMANCE_2_0_0_OR_NEWER"
26+
},
2127
{
2228
"name": "com.unity.modules.vr",
2329
"expression": "1.0.0",

com.unity.render-pipelines.universal/Runtime/UniversalRenderPipeline.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ public static void RenderSingleCamera(ScriptableRenderContext context, Camera ca
185185
}
186186

187187
InitializeCameraData(camera, additionalCameraData, true, out var cameraData);
188+
#if ADAPTIVE_PERFORMANCE_2_0_0_OR_NEWER
189+
if (asset.useAdaptivePerformance)
190+
ApplyAdaptivePerformance(ref cameraData);
191+
#endif
188192
RenderSingleCamera(context, cameraData, cameraData.postProcessEnabled);
189193
}
190194

@@ -231,6 +235,11 @@ static void RenderSingleCamera(ScriptableRenderContext context, CameraData camer
231235
var cullResults = context.Cull(ref cullingParameters);
232236
InitializeRenderingData(asset, ref cameraData, ref cullResults, anyPostProcessingEnabled, out var renderingData);
233237

238+
#if ADAPTIVE_PERFORMANCE_2_0_0_OR_NEWER
239+
if (asset.useAdaptivePerformance)
240+
ApplyAdaptivePerformance(ref renderingData);
241+
#endif
242+
234243
renderer.Setup(context, ref renderingData);
235244
renderer.Execute(context, ref renderingData);
236245
}
@@ -321,6 +330,10 @@ static void RenderCameraStack(ScriptableRenderContext context, Camera baseCamera
321330
#endif
322331
UpdateVolumeFramework(baseCamera, baseCameraAdditionalData);
323332
InitializeCameraData(baseCamera, baseCameraAdditionalData, !isStackedRendering, out var baseCameraData);
333+
#if ADAPTIVE_PERFORMANCE_2_0_0_OR_NEWER
334+
if (asset.useAdaptivePerformance)
335+
ApplyAdaptivePerformance(ref baseCameraData);
336+
#endif
324337
RenderSingleCamera(context, baseCameraData, anyPostProcessingEnabled);
325338
EndCameraRendering(context, baseCamera);
326339

@@ -808,5 +821,71 @@ static void SetupPerFrameShaderConstants()
808821
// Used when subtractive mode is selected
809822
Shader.SetGlobalVector(PerFrameBuffer._SubtractiveShadowColor, CoreUtils.ConvertSRGBToActiveColorSpace(RenderSettings.subtractiveShadowColor));
810823
}
824+
825+
#if ADAPTIVE_PERFORMANCE_2_0_0_OR_NEWER
826+
static void ApplyAdaptivePerformance(ref CameraData cameraData)
827+
{
828+
var noFrontToBackOpaqueFlags = SortingCriteria.SortingLayer | SortingCriteria.RenderQueue | SortingCriteria.OptimizeStateChanges | SortingCriteria.CanvasOrder;
829+
if (AdaptivePerformance.AdaptivePerformanceRenderSettings.SkipFrontToBackSorting)
830+
cameraData.defaultOpaqueSortFlags = noFrontToBackOpaqueFlags;
831+
832+
var MaxShadowDistanceMultiplier = AdaptivePerformance.AdaptivePerformanceRenderSettings.MaxShadowDistanceMultiplier;
833+
cameraData.maxShadowDistance *= MaxShadowDistanceMultiplier;
834+
835+
var RenderScaleMultiplier = AdaptivePerformance.AdaptivePerformanceRenderSettings.RenderScaleMultiplier;
836+
cameraData.renderScale *= RenderScaleMultiplier;
837+
838+
// TODO
839+
if (!cameraData.isStereoEnabled)
840+
{
841+
cameraData.cameraTargetDescriptor.width = (int)(cameraData.camera.pixelWidth * cameraData.renderScale);
842+
cameraData.cameraTargetDescriptor.height = (int)(cameraData.camera.pixelHeight * cameraData.renderScale);
843+
}
844+
845+
var antialiasingQualityIndex = (int)cameraData.antialiasingQuality - AdaptivePerformance.AdaptivePerformanceRenderSettings.AntiAliasingQualityBias;
846+
if (antialiasingQualityIndex < 0)
847+
cameraData.antialiasing = AntialiasingMode.None;
848+
cameraData.antialiasingQuality = (AntialiasingQuality)Mathf.Clamp(antialiasingQualityIndex, (int)AntialiasingQuality.Low, (int)AntialiasingQuality.High);
849+
}
850+
static void ApplyAdaptivePerformance(ref RenderingData renderingData)
851+
{
852+
if (AdaptivePerformance.AdaptivePerformanceRenderSettings.SkipDynamicBatching)
853+
renderingData.supportsDynamicBatching = false;
854+
855+
var MainLightShadowmapResultionMultiplier = AdaptivePerformance.AdaptivePerformanceRenderSettings.MainLightShadowmapResultionMultiplier;
856+
renderingData.shadowData.mainLightShadowmapWidth = (int)(renderingData.shadowData.mainLightShadowmapWidth * MainLightShadowmapResultionMultiplier);
857+
renderingData.shadowData.mainLightShadowmapHeight = (int)(renderingData.shadowData.mainLightShadowmapHeight * MainLightShadowmapResultionMultiplier);
858+
859+
var MainLightShadowCascadesCountBias = AdaptivePerformance.AdaptivePerformanceRenderSettings.MainLightShadowCascadesCountBias;
860+
renderingData.shadowData.mainLightShadowCascadesCount = Mathf.Clamp(renderingData.shadowData.mainLightShadowCascadesCount - MainLightShadowCascadesCountBias, 0, 4);
861+
862+
var shadowQualityIndex = AdaptivePerformance.AdaptivePerformanceRenderSettings.ShadowQualityBias;
863+
for (int i = 0; i < shadowQualityIndex; i++)
864+
{
865+
if (renderingData.shadowData.supportsSoftShadows)
866+
{
867+
renderingData.shadowData.supportsSoftShadows = false;
868+
continue;
869+
}
870+
871+
if (renderingData.shadowData.supportsAdditionalLightShadows)
872+
{
873+
renderingData.shadowData.supportsAdditionalLightShadows = false;
874+
continue;
875+
}
876+
877+
if (renderingData.shadowData.supportsMainLightShadows)
878+
{
879+
renderingData.shadowData.supportsMainLightShadows = false;
880+
continue;
881+
}
882+
883+
break;
884+
}
885+
886+
if (AdaptivePerformance.AdaptivePerformanceRenderSettings.LutBias >= 1 && renderingData.postProcessingData.lutSize == 32)
887+
renderingData.postProcessingData.lutSize = 16;
888+
}
889+
#endif
811890
}
812891
}

0 commit comments

Comments
 (0)