Browse Source

Simplified a bunch of code and removed some unnecessary bits. Mipmap bias is applied more safely now. Context gets reset if auto exposure bool changes.

mac-autoexp
Nico de Poel 3 years ago
parent
commit
4f0edf6049
  1. 65
      Assets/Scripts/Fsr2ImageEffect.cs

65
Assets/Scripts/Fsr2ImageEffect.cs

@ -111,6 +111,7 @@ namespace FidelityFX
private readonly Fsr2.GenerateReactiveDescription _genReactiveDescription = new Fsr2.GenerateReactiveDescription();
private Fsr2ImageEffectHelper _helper;
private float _appliedBiasOffset;
private Camera _renderCamera;
private RenderTexture _originalRenderTarget;
@ -119,8 +120,7 @@ namespace FidelityFX
private Fsr2.QualityMode _prevQualityMode;
private Vector2Int _prevDisplaySize;
private bool _prevGenReactiveMask;
private bool _prevGenTcrMasks;
private bool _prevAutoExposure;
private CommandBuffer _dispatchCommandBuffer;
private CommandBuffer _opaqueInputCommandBuffer;
@ -141,13 +141,6 @@ namespace FidelityFX
_displaySize = GetDisplaySize();
Fsr2.GetRenderResolutionFromQualityMode(out var renderWidth, out var renderHeight, _displaySize.x, _displaySize.y, qualityMode);
_renderSize = new Vector2Int(renderWidth, renderHeight);
// Apply a mipmap bias so that textures retain their sharpness
float biasOffset = Fsr2.GetMipmapBiasOffset(_renderSize.x, _displaySize.x);
if (!float.IsNaN(biasOffset))
{
Callbacks.ApplyMipmapBias(biasOffset);
}
if (!SystemInfo.supportsComputeShaders)
{
@ -175,27 +168,34 @@ namespace FidelityFX
_dispatchCommandBuffer = new CommandBuffer { name = "FSR2 Dispatch" };
_opaqueInputCommandBuffer = new CommandBuffer { name = "FSR2 Opaque Input" };
if (autoGenerateReactiveMask || autoGenerateTransparencyAndComposition)
{
_renderCamera.AddCommandBuffer(CameraEvent.BeforeForwardAlpha, _opaqueInputCommandBuffer);
}
_renderCamera.AddCommandBuffer(CameraEvent.BeforeForwardAlpha, _opaqueInputCommandBuffer);
_copyWithDepthMaterial = new Material(Shader.Find("Hidden/BlitCopyWithDepth"));
_prevDisplaySize = _displaySize;
_prevQualityMode = qualityMode;
_prevGenReactiveMask = autoGenerateReactiveMask;
_prevGenTcrMasks = autoGenerateTransparencyAndComposition;
_prevAutoExposure = enableAutoExposure;
// Apply a mipmap bias so that textures retain their sharpness
float biasOffset = Fsr2.GetMipmapBiasOffset(_renderSize.x, _displaySize.x);
if (!float.IsNaN(biasOffset) && !float.IsInfinity(biasOffset))
{
Callbacks.ApplyMipmapBias(biasOffset);
_appliedBiasOffset = biasOffset;
}
else
{
_appliedBiasOffset = 0f;
}
}
private void OnDisable()
{
// Undo the current mipmap bias offset
float biasOffset = Fsr2.GetMipmapBiasOffset(_renderSize.x, _prevDisplaySize.x);
if (!float.IsNaN(biasOffset))
if (!float.IsNaN(_appliedBiasOffset) && !float.IsInfinity(_appliedBiasOffset) && _appliedBiasOffset != 0f)
{
Callbacks.ApplyMipmapBias(-biasOffset);
Callbacks.ApplyMipmapBias(-_appliedBiasOffset);
_appliedBiasOffset = 0f;
}
// Restore the camera's original state
@ -231,23 +231,12 @@ namespace FidelityFX
private void Update()
{
var displaySize = GetDisplaySize();
if (displaySize.x != _prevDisplaySize.x || displaySize.y != _prevDisplaySize.y || qualityMode != _prevQualityMode)
if (displaySize.x != _prevDisplaySize.x || displaySize.y != _prevDisplaySize.y || qualityMode != _prevQualityMode || enableAutoExposure != _prevAutoExposure)
{
// Force all resources to be destroyed and recreated with the new settings
OnDisable();
OnEnable();
}
if ((autoGenerateReactiveMask || autoGenerateTransparencyAndComposition) != (_prevGenReactiveMask || _prevGenTcrMasks))
{
if (autoGenerateReactiveMask || autoGenerateTransparencyAndComposition)
_renderCamera.AddCommandBuffer(CameraEvent.BeforeForwardAlpha, _opaqueInputCommandBuffer);
else
_renderCamera.RemoveCommandBuffer(CameraEvent.BeforeForwardAlpha, _opaqueInputCommandBuffer);
_prevGenReactiveMask = autoGenerateReactiveMask;
_prevGenTcrMasks = autoGenerateTransparencyAndComposition;
}
}
public void Reset()
@ -369,7 +358,6 @@ namespace FidelityFX
{
_dispatchCommandBuffer.GetTemporaryRT(Fsr2ShaderIDs.UavAutoReactive, _renderSize.x, _renderSize.y, 0, default, GraphicsFormat.R8_UNorm, 1, true);
_context.GenerateReactiveMask(_genReactiveDescription, _dispatchCommandBuffer);
_dispatchDescription.Reactive = Fsr2ShaderIDs.UavAutoReactive;
}
@ -394,19 +382,12 @@ namespace FidelityFX
}
_dispatchCommandBuffer.ReleaseTemporaryRT(Fsr2ShaderIDs.UavUpscaledOutput);
if (autoGenerateReactiveMask)
{
_dispatchCommandBuffer.ReleaseTemporaryRT(Fsr2ShaderIDs.UavAutoReactive);
}
_dispatchCommandBuffer.ReleaseTemporaryRT(Fsr2ShaderIDs.UavAutoReactive);
Graphics.ExecuteCommandBuffer(_dispatchCommandBuffer);
if (_colorOpaqueOnly != null)
{
RenderTexture.ReleaseTemporary(_colorOpaqueOnly);
_colorOpaqueOnly = null;
}
RenderTexture.ReleaseTemporary(_colorOpaqueOnly);
_colorOpaqueOnly = null;
// Shut up the Unity warning about not writing to the destination texture
RenderTexture.active = dest;

Loading…
Cancel
Save