Browse Source

Made all RenderTargetView fields non-nullable, and introduced a special Empty value for optional fields.

This removes the need for a lot of HasValue checks, especially since all fields will eventually get a value anyway, making those checks rather redundant. This also makes it clearer that all SRVs will get bound with a valid input texture, even if the input is optional.
master
Nico de Poel 2 years ago
parent
commit
47cba76dee
  1. 17
      Assets/Scripts/Core/Fsr2.cs
  2. 33
      Assets/Scripts/Core/Fsr2Context.cs
  3. 73
      Assets/Scripts/Core/Fsr2Pipeline.cs
  4. 6
      Assets/Scripts/Fsr2ImageEffect.cs

17
Assets/Scripts/Core/Fsr2.cs

@ -159,8 +159,10 @@ namespace FidelityFX
/// <summary>
/// A structure wrapping all of the necessary information to bind a specific buffer or attachment of a render target to a compute shader.
/// </summary>
public struct RenderTargetView
public readonly struct RenderTargetView
{
public static readonly RenderTargetView Empty = new RenderTargetView(default(RenderTargetIdentifier));
public RenderTargetView(in RenderTargetIdentifier renderTarget, RenderTextureSubElement subElement = RenderTextureSubElement.Default, int mipLevel = 0)
{
RenderTarget = renderTarget;
@ -168,23 +170,24 @@ namespace FidelityFX
MipLevel = mipLevel;
}
public bool IsEmpty => RenderTarget.Equals(default(RenderTargetIdentifier));
public readonly RenderTargetIdentifier RenderTarget;
public readonly RenderTextureSubElement SubElement;
public readonly int MipLevel;
}
/// <summary>
/// The input and output resources are all optional. If they are null, the Fsr2Context won't try to bind them to any shaders.
/// This allows for customized and more efficient resource management outside of Fsr2Context, tailored to the specific scenario.
/// A structure encapsulating the parameters for dispatching the various passes of FidelityFX Super Resolution 2.
/// </summary>
public class DispatchDescription
{
public RenderTargetView Color;
public RenderTargetView Depth;
public RenderTargetView MotionVectors;
public RenderTargetView? Exposure;
public RenderTargetView? Reactive;
public RenderTargetView? TransparencyAndComposition;
public RenderTargetView Exposure; // optional
public RenderTargetView Reactive; // optional
public RenderTargetView TransparencyAndComposition; // optional
public RenderTargetView Output;
public Vector2 JitterOffset;
public Vector2 MotionVectorScale;
@ -202,7 +205,7 @@ namespace FidelityFX
// EXPERIMENTAL reactive mask generation parameters
public bool EnableAutoReactive;
public RenderTargetView? ColorOpaqueOnly;
public RenderTargetView ColorOpaqueOnly;
public float AutoTcThreshold = 0.05f;
public float AutoTcScale = 1.0f;
public float AutoReactiveScale = 5.0f;

33
Assets/Scripts/Core/Fsr2Context.cs

@ -159,7 +159,7 @@ namespace FidelityFX
// If auto exposure is enabled use the auto exposure SRV, otherwise what the app sends
if ((_contextDescription.Flags & Fsr2.InitializationFlags.EnableAutoExposure) != 0)
dispatchParams.Exposure = new Fsr2.RenderTargetView(_resources.AutoExposure);
else if (dispatchParams.Exposure == null)
else if (dispatchParams.Exposure.IsEmpty)
dispatchParams.Exposure = new Fsr2.RenderTargetView(_resources.DefaultExposure);
if (dispatchParams.EnableAutoReactive)
@ -169,7 +169,10 @@ namespace FidelityFX
_resources.CreateTcrAutogenResources(_contextDescription);
if (resetAccumulation)
commandBuffer.Blit(_resources.PrevPreAlpha[frameIndex ^ 1], (dispatchParams.ColorOpaqueOnly ?? new Fsr2.RenderTargetView(Fsr2ShaderIDs.SrvOpaqueOnly)).RenderTarget);
{
RenderTargetIdentifier opaqueOnly = dispatchParams.ColorOpaqueOnly.IsEmpty ? Fsr2ShaderIDs.SrvOpaqueOnly : dispatchParams.ColorOpaqueOnly.RenderTarget;
commandBuffer.Blit(_resources.PrevPreAlpha[frameIndex ^ 1], opaqueOnly);
}
}
else if (_resources.AutoReactive != null)
{
@ -177,8 +180,8 @@ namespace FidelityFX
_resources.DestroyTcrAutogenResources();
}
if (dispatchParams.Reactive == null) dispatchParams.Reactive = new Fsr2.RenderTargetView(_resources.DefaultReactive);
if (dispatchParams.TransparencyAndComposition == null) dispatchParams.TransparencyAndComposition = new Fsr2.RenderTargetView(_resources.DefaultReactive);
if (dispatchParams.Reactive.IsEmpty) dispatchParams.Reactive = new Fsr2.RenderTargetView(_resources.DefaultReactive);
if (dispatchParams.TransparencyAndComposition.IsEmpty) dispatchParams.TransparencyAndComposition = new Fsr2.RenderTargetView(_resources.DefaultReactive);
Fsr2Resources.CreateAliasableResources(commandBuffer, _contextDescription, dispatchParams);
SetupConstants(dispatchParams, resetAccumulation);
@ -433,7 +436,27 @@ namespace FidelityFX
private void DebugCheckDispatch(Fsr2.DispatchDescription dispatchParams)
{
if (dispatchParams.Exposure.HasValue && (_contextDescription.Flags & Fsr2.InitializationFlags.EnableAutoExposure) != 0)
if (dispatchParams.Color.IsEmpty)
{
Debug.LogError("Color resource is null");
}
if (dispatchParams.Depth.IsEmpty)
{
Debug.LogError("Depth resource is null");
}
if (dispatchParams.MotionVectors.IsEmpty)
{
Debug.LogError("MotionVectors resource is null");
}
if (dispatchParams.Output.IsEmpty)
{
Debug.LogError("Output resource is null");
}
if (!dispatchParams.Exposure.IsEmpty && (_contextDescription.Flags & Fsr2.InitializationFlags.EnableAutoExposure) != 0)
{
Debug.LogWarning("Exposure resource provided, however auto exposure flag is present");
}

73
Assets/Scripts/Core/Fsr2Pipeline.cs

@ -157,16 +157,12 @@ namespace FidelityFX
ref var color = ref dispatchParams.Color;
ref var depth = ref dispatchParams.Depth;
ref var motionVectors = ref dispatchParams.MotionVectors;
ref var exposure = ref dispatchParams.Exposure;
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputColor, color.RenderTarget, color.MipLevel, color.SubElement);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputDepth, depth.RenderTarget, depth.MipLevel, depth.SubElement);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputMotionVectors, motionVectors.RenderTarget, motionVectors.MipLevel, motionVectors.SubElement);
if (dispatchParams.Exposure.HasValue)
{
var exposure = dispatchParams.Exposure.Value;
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputExposure, exposure.RenderTarget, exposure.MipLevel, exposure.SubElement);
}
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputExposure, exposure.RenderTarget, exposure.MipLevel, exposure.SubElement);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.UavDilatedMotionVectors, Resources.DilatedMotionVectors[frameIndex]);
@ -189,28 +185,16 @@ namespace FidelityFX
ref var color = ref dispatchParams.Color;
ref var depth = ref dispatchParams.Depth;
ref var motionVectors = ref dispatchParams.MotionVectors;
ref var exposure = ref dispatchParams.Exposure;
ref var reactive = ref dispatchParams.Reactive;
ref var tac = ref dispatchParams.TransparencyAndComposition;
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputColor, color.RenderTarget, color.MipLevel, color.SubElement);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputDepth, depth.RenderTarget, depth.MipLevel, depth.SubElement);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputMotionVectors, motionVectors.RenderTarget, motionVectors.MipLevel, motionVectors.SubElement);
if (dispatchParams.Exposure.HasValue)
{
var exposure = dispatchParams.Exposure.Value;
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputExposure, exposure.RenderTarget, exposure.MipLevel, exposure.SubElement);
}
if (dispatchParams.Reactive.HasValue)
{
var reactive = dispatchParams.Reactive.Value;
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvReactiveMask, reactive.RenderTarget, reactive.MipLevel, reactive.SubElement);
}
if (dispatchParams.TransparencyAndComposition.HasValue)
{
var tac = dispatchParams.TransparencyAndComposition.Value;
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvTransparencyAndCompositionMask, tac.RenderTarget, tac.MipLevel, tac.SubElement);
}
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputExposure, exposure.RenderTarget, exposure.MipLevel, exposure.SubElement);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvReactiveMask, reactive.RenderTarget, reactive.MipLevel, reactive.SubElement);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvTransparencyAndCompositionMask, tac.RenderTarget, tac.MipLevel, tac.SubElement);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvReconstructedPrevNearestDepth, Fsr2ShaderIDs.UavReconstructedPrevNearestDepth);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvDilatedMotionVectors, Resources.DilatedMotionVectors[frameIndex]);
@ -284,11 +268,8 @@ namespace FidelityFX
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputMotionVectors, motionVectors.RenderTarget, motionVectors.MipLevel, motionVectors.SubElement);
}
if (dispatchParams.Exposure.HasValue)
{
var exposure = dispatchParams.Exposure.Value;
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputExposure, exposure.RenderTarget, exposure.MipLevel, exposure.SubElement);
}
ref var exposure = ref dispatchParams.Exposure;
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputExposure, exposure.RenderTarget, exposure.MipLevel, exposure.SubElement);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvDilatedReactiveMasks, Fsr2ShaderIDs.UavDilatedReactiveMasks);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInternalUpscaled, Resources.InternalUpscaled[frameIndex ^ 1]);
@ -327,12 +308,8 @@ namespace FidelityFX
public override void ScheduleDispatch(CommandBuffer commandBuffer, Fsr2.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY)
{
if (dispatchParams.Exposure.HasValue)
{
var exposure = dispatchParams.Exposure.Value;
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputExposure, exposure.RenderTarget, exposure.MipLevel, exposure.SubElement);
}
ref var exposure = ref dispatchParams.Exposure;
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputExposure, exposure.RenderTarget, exposure.MipLevel, exposure.SubElement);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvRcasInput, Resources.InternalUpscaled[frameIndex]);
ref var output = ref dispatchParams.Output;
@ -391,31 +368,19 @@ namespace FidelityFX
public override void ScheduleDispatch(CommandBuffer commandBuffer, Fsr2.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY)
{
if (dispatchParams.ColorOpaqueOnly.HasValue)
{
var opaqueOnly = dispatchParams.ColorOpaqueOnly.Value;
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvOpaqueOnly, opaqueOnly.RenderTarget, opaqueOnly.MipLevel, opaqueOnly.SubElement);
}
ref var color = ref dispatchParams.Color;
ref var motionVectors = ref dispatchParams.MotionVectors;
ref var opaqueOnly = ref dispatchParams.ColorOpaqueOnly;
ref var reactive = ref dispatchParams.Reactive;
ref var tac = ref dispatchParams.TransparencyAndComposition;
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvOpaqueOnly, opaqueOnly.RenderTarget, opaqueOnly.MipLevel, opaqueOnly.SubElement);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputColor, color.RenderTarget, color.MipLevel, color.SubElement);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputMotionVectors, motionVectors.RenderTarget, motionVectors.MipLevel, motionVectors.SubElement);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvPrevColorPreAlpha, Resources.PrevPreAlpha[frameIndex ^ 1]);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvPrevColorPostAlpha, Resources.PrevPostAlpha[frameIndex ^ 1]);
if (dispatchParams.Reactive.HasValue)
{
var reactive = dispatchParams.Reactive.Value;
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvReactiveMask, reactive.RenderTarget, reactive.MipLevel, reactive.SubElement);
}
if (dispatchParams.TransparencyAndComposition.HasValue)
{
var tac = dispatchParams.TransparencyAndComposition.Value;
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvTransparencyAndCompositionMask, tac.RenderTarget, tac.MipLevel, tac.SubElement);
}
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvReactiveMask, reactive.RenderTarget, reactive.MipLevel, reactive.SubElement);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvTransparencyAndCompositionMask, tac.RenderTarget, tac.MipLevel, tac.SubElement);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.UavAutoReactive, Resources.AutoReactive);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.UavAutoComposition, Resources.AutoComposition);

6
Assets/Scripts/Fsr2ImageEffect.cs

@ -328,9 +328,9 @@ namespace FidelityFX
_dispatchDescription.Color = new Fsr2.RenderTargetView(BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Color);
_dispatchDescription.Depth = new Fsr2.RenderTargetView(BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Depth);
_dispatchDescription.MotionVectors = new Fsr2.RenderTargetView(BuiltinRenderTextureType.MotionVectors);
_dispatchDescription.Exposure = null;
_dispatchDescription.Reactive = null;
_dispatchDescription.TransparencyAndComposition = null;
_dispatchDescription.Exposure = Fsr2.RenderTargetView.Empty;
_dispatchDescription.Reactive = Fsr2.RenderTargetView.Empty;
_dispatchDescription.TransparencyAndComposition = Fsr2.RenderTargetView.Empty;
if (!enableAutoExposure && exposure != null) _dispatchDescription.Exposure = new Fsr2.RenderTargetView(exposure);
if (reactiveMask != null) _dispatchDescription.Reactive = new Fsr2.RenderTargetView(reactiveMask);

Loading…
Cancel
Save