Skip to content

Commit

Permalink
Improving DRS cpu load, and not recreating DLSS state every change of…
Browse files Browse the repository at this point in the history
… resolution

Changelog
  • Loading branch information
kecho committed Jul 26, 2021
1 parent 94f598a commit e874a3a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 44 deletions.
1 change: 1 addition & 0 deletions com.unity.render-pipelines.high-definition/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixed the APV UI loosing focus when the helpbox about baking appears in the probe volume.
- Fixed enabling a lensflare in playmode.
- Fixed white flashes when history is reset due to changes on type of upsampler.
- Fixed CPU performance on DLSS, avoiding to recreate state whenever a target can fall into the safe min/max resolution specified by the system.

### Changed
- Changed Window/Render Pipeline/HD Render Pipeline Wizard to Window/Rendering/HDRP Wizard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -902,8 +902,9 @@ internal void Update(FrameSettings currentFrameSettings, HDRenderPipeline hdrp,
if (canDoDynamicResolution)
{
Vector2Int scaledSize = DynamicResolutionHandler.instance.GetScaledSize(new Vector2Int(actualWidth, actualHeight));
actualWidth = scaledSize.x;
actualHeight = scaledSize.y;
//ensure that the scale is within and we dont round up an extra pixel
actualWidth = Math.Min(scaledSize.x, actualWidth);
actualHeight = Math.Min(scaledSize.y, actualHeight);
globalMipBias += DynamicResolutionHandler.instance.CalculateMipBias(scaledSize, nonScaledViewport, IsDLSSEnabled());
lowResScale = DynamicResolutionHandler.instance.GetLowResMultiplier(lowResScale);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ void ConfigureKeywords(bool enableBakeShadowMask, HDCamera hdCamera, CommandBuff
bool useOptimalSettings = hdCam.deepLearningSuperSamplingUseCustomAttributes
? hdCam.deepLearningSuperSamplingUseOptimalSettings
: m_Asset.currentPlatformRenderPipelineSettings.dynamicResolutionSettings.DLSSUseOptimalSettings;
m_DLSSPass.SetupAutomaticDRSScaling(useOptimalSettings, camera, xrPass, ref outDrsSettings);
m_DLSSPass.SetupDRSScaling(useOptimalSettings, camera, xrPass, ref outDrsSettings);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ public void BeginFrame(HDCamera hdCamera)
#endif
}

public void SetupAutomaticDRSScaling(bool enabled, Camera camera, XRPass xrPass, ref GlobalDynamicResolutionSettings dynamicResolutionSettings)
public void SetupDRSScaling(bool enableAutomaticSettings, Camera camera, XRPass xrPass, ref GlobalDynamicResolutionSettings dynamicResolutionSettings)
{
#if ENABLE_NVIDIA && ENABLE_NVIDIA_MODULE
InternalNVIDIASetupAutomaticDRSScaling(enabled, camera, xrPass, ref dynamicResolutionSettings);
InternalNVIDIASetupDRSScaling(enableAutomaticSettings, camera, xrPass, ref dynamicResolutionSettings);
#endif
}

Expand Down Expand Up @@ -276,6 +276,11 @@ private struct OptimalSettingsRequest
public NVIDIA.DLSSQuality quality;
public Rect viewport;
public NVIDIA.OptimalDLSSSettingsData optimalSettings;
public bool CanFit(DLSSPass.Resolution rect)
{
return rect.width >= optimalSettings.minWidth && rect.height >= optimalSettings.minHeight
&& rect.width <= optimalSettings.maxWidth && rect.height <= optimalSettings.maxHeight;
}
}

private static bool IsOptimalSettingsValid(in NVIDIA.OptimalDLSSSettingsData optimalSettings)
Expand All @@ -294,11 +299,12 @@ private class ViewState
private NVIDIA.GraphicsDevice m_Device;
private DlssViewData m_Data = new DlssViewData();
private bool m_UsingOptimalSettings = false;
private bool m_OptimalSettingsRequested = false;
private bool m_UseAutomaticSettings = false;
private DLSSPass.Resolution m_BackbufferRes;
private OptimalSettingsRequest m_OptimalSettingsRequest = new OptimalSettingsRequest();

public NVIDIA.DLSSContext DLSSContext { get { return m_DlssContext; } }
public bool RequestedOptimalSettings { get { return m_OptimalSettingsRequested; } }
public bool useAutomaticSettings { get { return m_UseAutomaticSettings; } }
public OptimalSettingsRequest OptimalSettingsRequestData { get { return m_OptimalSettingsRequest; } }

public ViewState(NVIDIA.GraphicsDevice device)
Expand All @@ -307,22 +313,22 @@ public ViewState(NVIDIA.GraphicsDevice device)
m_DlssContext = null;
}

public void RequestUseOptimalSetting(NVIDIA.DLSSQuality quality, Rect viewport, in NVIDIA.OptimalDLSSSettingsData optimalSettings)
public void RequestUseAutomaticSettings(bool useAutomaticSettings, NVIDIA.DLSSQuality quality, Rect viewport, in NVIDIA.OptimalDLSSSettingsData optimalSettings)
{
m_OptimalSettingsRequested = true;
m_UseAutomaticSettings = useAutomaticSettings;
m_OptimalSettingsRequest.quality = quality;
m_OptimalSettingsRequest.viewport = viewport;
m_OptimalSettingsRequest.optimalSettings = optimalSettings;
}

public void ClearOptimalSettings()
public void ClearAutomaticSettings()
{
m_OptimalSettingsRequested = false;
m_UseAutomaticSettings = false;
}

private bool ShouldUseOptimalSettings()
private bool ShouldUseAutomaticSettings()
{
if (!m_OptimalSettingsRequested || m_DlssContext == null)
if (!m_UseAutomaticSettings || m_DlssContext == null)
return false;

return m_DlssContext.initData.quality == m_OptimalSettingsRequest.quality
Expand All @@ -333,18 +339,20 @@ private bool ShouldUseOptimalSettings()

public void UpdateViewState(
in DlssViewData viewData,
/*uint backbufferWidth, uint backbufferHeight,*/
CommandBuffer cmdBuffer)
{
bool shouldUseOptimalSettings = ShouldUseOptimalSettings();
bool shouldUseOptimalSettings = ShouldUseAutomaticSettings();
bool isNew = false;
if (viewData.outputRes != m_Data.outputRes ||
(viewData.inputRes != m_Data.inputRes && !m_UsingOptimalSettings) ||
(viewData.CanFitInput(m_Data.inputRes) && m_UsingOptimalSettings) ||
(viewData.inputRes.width > m_BackbufferRes.width || viewData.inputRes.height > m_BackbufferRes.height) ||
(viewData.inputRes != m_BackbufferRes && !m_OptimalSettingsRequest.CanFit(viewData.inputRes)) ||
viewData.perfQuality != m_Data.perfQuality ||
m_DlssContext == null ||
shouldUseOptimalSettings != m_UsingOptimalSettings)
{
isNew = true;
m_BackbufferRes = viewData.inputRes;

if (m_DlssContext != null)
{
Expand All @@ -357,8 +365,8 @@ private bool ShouldUseOptimalSettings()
settings.SetFlag(NVIDIA.DLSSFeatureFlags.MVLowRes, true);
settings.SetFlag(NVIDIA.DLSSFeatureFlags.DepthInverted, true);
settings.SetFlag(NVIDIA.DLSSFeatureFlags.DoSharpening, true);
settings.inputRTWidth = viewData.inputRes.width;
settings.inputRTHeight = viewData.inputRes.height;
settings.inputRTWidth = m_BackbufferRes.width;
settings.inputRTHeight = m_BackbufferRes.height;
settings.outputRTWidth = viewData.outputRes.width;
settings.outputRTHeight = viewData.outputRes.height;
settings.quality = viewData.perfQuality;
Expand Down Expand Up @@ -435,12 +443,12 @@ public bool IsAlive()
return m_CamReference.TryGetTarget(out _);
}

public void ClearOptimalSettings()
public void ClearAutomaticSettings()
{
if (m_Views == null)
return;
foreach (var v in m_Views)
v.ClearOptimalSettings();
v.ClearAutomaticSettings();
}

public void SubmitCommands(
Expand Down Expand Up @@ -560,7 +568,7 @@ private float ScaleFn()
return 100.0f;

var viewState = cameraState.ViewStates[0];
if (!viewState.RequestedOptimalSettings)
if (!viewState.useAutomaticSettings)
return 100.0f;

var optimalSettings = viewState.OptimalSettingsRequestData.optimalSettings;
Expand All @@ -570,7 +578,7 @@ private float ScaleFn()
return Mathf.Min(suggestedPercentageX, suggestedPercentageY) * 100.0f;
}

private void InternalNVIDIASetupAutomaticDRSScaling(bool enabled, Camera camera, XRPass xrPass, ref GlobalDynamicResolutionSettings dynamicResolutionSettings)
private void InternalNVIDIASetupDRSScaling(bool enableAutomaticSettings, Camera camera, XRPass xrPass, ref GlobalDynamicResolutionSettings dynamicResolutionSettings)
{
if (m_Device == null)
return;
Expand All @@ -586,38 +594,33 @@ private void InternalNVIDIASetupAutomaticDRSScaling(bool enabled, Camera camera,
if (cameraState.ViewStates[0].DLSSContext == null)
return;

if (!enabled)
{
cameraState.ClearOptimalSettings();
}
else
var usedQuality = cameraState.ViewStates[0].DLSSContext.initData.quality;
Rect finalViewport = xrPass != null && xrPass.enabled ? xrPass.GetViewport() : new Rect(camera.pixelRect.x, camera.pixelRect.y, camera.pixelWidth, camera.pixelHeight);
NVIDIA.OptimalDLSSSettingsData optimalSettings = new NVIDIA.OptimalDLSSSettingsData();
m_Device.GetOptimalSettings((uint)finalViewport.width, (uint)finalViewport.height, usedQuality, out optimalSettings);

foreach (var view in cameraState.ViewStates)
{
var usedQuality = cameraState.ViewStates[0].DLSSContext.initData.quality;
Rect finalViewport = xrPass != null && xrPass.enabled ? xrPass.GetViewport() : new Rect(camera.pixelRect.x, camera.pixelRect.y, camera.pixelWidth, camera.pixelHeight);
NVIDIA.OptimalDLSSSettingsData optimalSettings = new NVIDIA.OptimalDLSSSettingsData();
if (view == null)
continue;

if (!m_Device.GetOptimalSettings((uint)finalViewport.width, (uint)finalViewport.height, usedQuality, out optimalSettings))
{
cameraState.ClearOptimalSettings();
return;
}
view.RequestUseAutomaticSettings(enableAutomaticSettings, usedQuality, finalViewport, optimalSettings);
}

if (IsOptimalSettingsValid(optimalSettings))
if (enableAutomaticSettings)
{
if (IsOptimalSettingsValid(optimalSettings) && enableAutomaticSettings)
{
dynamicResolutionSettings.maxPercentage = Mathf.Min((float)optimalSettings.maxWidth / finalViewport.width, (float)optimalSettings.maxHeight / finalViewport.height) * 100.0f;
dynamicResolutionSettings.minPercentage = Mathf.Max((float)optimalSettings.minWidth / finalViewport.width, (float)optimalSettings.minHeight / finalViewport.height) * 100.0f;
m_SelectedCameraKey = cameraKey;
DynamicResolutionHandler.SetSystemDynamicResScaler(ScaleFn, DynamicResScalePolicyType.ReturnsPercentage);
DynamicResolutionHandler.SetActiveDynamicScalerSlot(DynamicResScalerSlot.System);
}

foreach (var view in cameraState.ViewStates)
{
if (view == null)
continue;

view.RequestUseOptimalSetting(usedQuality, finalViewport, optimalSettings);
}
}
else
{
cameraState.ClearAutomaticSettings();
}
}

Expand Down Expand Up @@ -672,6 +675,7 @@ private void InternalNVIDIARender(in DLSSPass.Parameters parameters, DLSSPass.Ca

dlssViewData.inputRes = new Resolution() { width = (uint)parameters.hdCamera.actualWidth, height = (uint)parameters.hdCamera.actualHeight };
dlssViewData.outputRes = new Resolution() { width = (uint)DynamicResolutionHandler.instance.finalViewport.x, height = (uint)DynamicResolutionHandler.instance.finalViewport.y };

dlssViewData.jitterX = -parameters.hdCamera.taaJitter.x;
dlssViewData.jitterY = -parameters.hdCamera.taaJitter.y;
dlssViewData.reset = parameters.hdCamera.isFirstFrame;
Expand Down

0 comments on commit e874a3a

Please sign in to comment.