FSR2 tests in Unity based on built-in render pipeline
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

249 lines
13 KiB

using System;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
namespace FidelityFX
{
internal abstract class Fsr2Pipeline: IDisposable
{
internal const int ShadingChangeMipLevel = 4; // Corresponds to FFX_FSR2_SHADING_CHANGE_MIP_LEVEL define
protected readonly Fsr2.ContextDescription ContextDescription;
protected readonly ComputeBuffer Constants;
protected ComputeShader ComputeShader;
protected int KernelIndex;
// Shader resource views, i.e. read-only bindings
protected static readonly int SrvInputColor = Shader.PropertyToID("r_input_color_jittered");
protected static readonly int SrvOpaqueOnly = Shader.PropertyToID("r_input_opaque_only");
protected static readonly int SrvInputMotionVectors = Shader.PropertyToID("r_input_motion_vectors");
protected static readonly int SrvInputDepth = Shader.PropertyToID("r_input_depth");
protected static readonly int SrvInputExposure = Shader.PropertyToID("r_input_exposure");
protected static readonly int SrvRcasInput = Shader.PropertyToID("r_rcas_input");
// Unordered access views, i.e. random read/write bindings
protected static readonly int UavUpscaledOutput = Shader.PropertyToID("rw_upscaled_output");
protected static readonly int UavExposureMipLumaChange = Shader.PropertyToID("rw_img_mip_shading_change");
protected static readonly int UavExposureMip5 = Shader.PropertyToID("rw_img_mip_5");
protected static readonly int UavAutoExposure = Shader.PropertyToID("rw_auto_exposure");
protected static readonly int UavSpdAtomicCount = Shader.PropertyToID("rw_spd_global_atomic");
// Constant buffer bindings
protected static readonly int CbFsr2 = Shader.PropertyToID("cbFSR2");
protected static readonly int CbSpd = Shader.PropertyToID("cbSPD");
protected static readonly int CbRcas = Shader.PropertyToID("cbRCAS");
protected static readonly int CbGenReactive = Shader.PropertyToID("cbGenerateReactive");
protected Fsr2Pipeline(Fsr2.ContextDescription contextDescription, ComputeBuffer constants)
{
ContextDescription = contextDescription;
Constants = constants;
}
public virtual void Dispose()
{
UnloadComputeShader();
}
public abstract void ScheduleDispatch(CommandBuffer commandBuffer, Fsr2.DispatchDescription dispatchParams, int dispatchX, int dispatchY);
public static void RegisterResources(CommandBuffer commandBuffer, Fsr2.ContextDescription contextDescription, Fsr2.DispatchDescription dispatchParams)
{
Vector2Int displaySize = contextDescription.DisplaySize;
Vector2Int maxRenderSize = contextDescription.MaxRenderSize;
// Set up shared aliasable resources, i.e. temporary render textures
// These do not need to persist between frames, but they do need to be available between passes
// Resource FSR2_SpdAtomicCounter: FFX_RESOURCE_USAGE_UAV, FFX_SURFACE_FORMAT_R32_UINT, FFX_RESOURCE_FLAGS_ALIASABLE
commandBuffer.GetTemporaryRT(UavSpdAtomicCount, 1, 1, 0, FilterMode.Point, GraphicsFormat.R32_UInt, 1, true);
// Resource FSR2_ExposureMips: FFX_RESOURCE_USAGE_UAV, FFX_SURFACE_FORMAT_R16_FLOAT, FFX_RESOURCE_FLAGS_ALIASABLE, has mipmap chain
const int lumaMip = ShadingChangeMipLevel + 1;
commandBuffer.GetTemporaryRT(UavExposureMipLumaChange, maxRenderSize.x >> lumaMip, maxRenderSize.y >> lumaMip, 0, FilterMode.Point, GraphicsFormat.R16_SFloat, 1, true);
commandBuffer.GetTemporaryRT(UavExposureMip5, maxRenderSize.x >> 6, maxRenderSize.y >> 6, 0, FilterMode.Point, GraphicsFormat.R16_SFloat, 1, true);
}
public static void UnregisterResources(CommandBuffer commandBuffer)
{
// Release all of the aliasable resources used this frame
commandBuffer.ReleaseTemporaryRT(UavSpdAtomicCount);
commandBuffer.ReleaseTemporaryRT(UavExposureMipLumaChange);
commandBuffer.ReleaseTemporaryRT(UavExposureMip5);
}
protected void LoadComputeShader(string name)
{
LoadComputeShader(name, ContextDescription.Flags, ref ComputeShader, out KernelIndex);
}
private void LoadComputeShader(string name, Fsr2.InitializationFlags flags, ref ComputeShader shaderRef, out int kernelIndex)
{
if (shaderRef == null)
shaderRef = ContextDescription.Callbacks.LoadComputeShader(name);
kernelIndex = shaderRef.FindKernel("CS");
bool useLut = (SystemInfo.computeSubGroupSize == 64);
// This mirrors the permutation rules from the CreatePipeline* functions
if ((flags & Fsr2.InitializationFlags.EnableHighDynamicRange) != 0) shaderRef.EnableKeyword("FFX_FSR2_OPTION_HDR_COLOR_INPUT");
if ((flags & Fsr2.InitializationFlags.EnableDisplayResolutionMotionVectors) == 0) shaderRef.EnableKeyword("FFX_FSR2_OPTION_LOW_RESOLUTION_MOTION_VECTORS");
if ((flags & Fsr2.InitializationFlags.EnableMotionVectorsJitterCancellation) != 0) shaderRef.EnableKeyword("FFX_FSR2_OPTION_JITTERED_MOTION_VECTORS");
if ((flags & Fsr2.InitializationFlags.EnableDepthInverted) != 0) shaderRef.EnableKeyword("FFX_FSR2_OPTION_INVERTED_DEPTH");
if (useLut) shaderRef.EnableKeyword("FFX_FSR2_OPTION_REPROJECT_USE_LANCZOS_TYPE");
// TODO: enable FFX_HALF if FP16 is supported (except RCAS)
}
private void UnloadComputeShader()
{
UnloadComputeShader(ref ComputeShader);
}
private void UnloadComputeShader(ref ComputeShader shaderRef)
{
if (shaderRef == null)
return;
ContextDescription.Callbacks.UnloadComputeShader(shaderRef);
shaderRef = null;
}
}
internal class Fsr2ComputeLuminancePyramidPipeline : Fsr2Pipeline
{
private readonly ComputeBuffer _spdConstants;
private readonly RenderTexture _autoExposure;
public Fsr2ComputeLuminancePyramidPipeline(Fsr2.ContextDescription contextDescription, ComputeBuffer constants, ComputeBuffer spdConstants, RenderTexture autoExposure)
: base(contextDescription, constants)
{
_spdConstants = spdConstants;
_autoExposure = autoExposure;
LoadComputeShader("FSR2/ffx_fsr2_compute_luminance_pyramid_pass");
}
public override void ScheduleDispatch(CommandBuffer commandBuffer, Fsr2.DispatchDescription dispatchParams, int dispatchX, int dispatchY)
{
// Problems to solve:
// - How do resources (render textures) relate to SRV/UAV bindings? How are those tied together?
// - What about the SRV/UAVs that are not related to any resources? Where are those filled in?
// - How do we clear the resources that need to be cleared at dispatch? (SetBufferData)
// - Shouldn't we use a ComputeBuffer for resources that are one-dimensional and clearly not image data? e.g. SPD atomic counter & Lanczos LUT data
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, SrvInputColor, dispatchParams.Input);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, UavAutoExposure, _autoExposure);
commandBuffer.SetComputeConstantBufferParam(ComputeShader, CbFsr2, Constants, 0, Marshal.SizeOf<Fsr2.Fsr2Constants>());
commandBuffer.SetComputeConstantBufferParam(ComputeShader, CbSpd, _spdConstants, 0, Marshal.SizeOf<Fsr2.SpdConstants>());
commandBuffer.DispatchCompute(ComputeShader, KernelIndex, dispatchX, dispatchY, 1);
}
}
internal class Fsr2ReconstructPreviousDepthPipeline : Fsr2Pipeline
{
public Fsr2ReconstructPreviousDepthPipeline(Fsr2.ContextDescription contextDescription, ComputeBuffer constants)
: base(contextDescription, constants)
{
LoadComputeShader("FSR2/ffx_fsr2_reconstruct_previous_depth_pass");
}
public override void ScheduleDispatch(CommandBuffer commandBuffer, Fsr2.DispatchDescription dispatchParams, int dispatchX, int dispatchY)
{
/*
#define FSR2_BIND_SRV_INPUT_MOTION_VECTORS 0
#define FSR2_BIND_SRV_INPUT_DEPTH 1
#define FSR2_BIND_SRV_INPUT_COLOR 2
#define FSR2_BIND_SRV_INPUT_EXPOSURE 3
#define FSR2_BIND_UAV_RECONSTRUCTED_PREV_NEAREST_DEPTH 0
#define FSR2_BIND_UAV_DILATED_MOTION_VECTORS 1
#define FSR2_BIND_UAV_DILATED_DEPTH 2
#define FSR2_BIND_UAV_LOCK_INPUT_LUMA 3
#define FSR2_BIND_CB_FSR2 0
*/
commandBuffer.SetComputeConstantBufferParam(ComputeShader, CbFsr2, Constants, 0, Marshal.SizeOf<Fsr2.Fsr2Constants>());
commandBuffer.DispatchCompute(ComputeShader, KernelIndex, dispatchX, dispatchY, 1);
}
}
internal class Fsr2AccumulatePipeline : Fsr2Pipeline
{
public Fsr2AccumulatePipeline(Fsr2.ContextDescription contextDescription, ComputeBuffer constants)
: base(contextDescription, constants)
{
LoadComputeShader("FSR2/ffx_fsr2_accumulate_pass");
}
public override void ScheduleDispatch(CommandBuffer commandBuffer, Fsr2.DispatchDescription dispatchParams, int dispatchX, int dispatchY)
{
//throw new NotImplementedException();
}
}
internal class Fsr2AccumulateSharpenPipeline : Fsr2AccumulatePipeline
{
private readonly ComputeShader _shaderCopy;
public Fsr2AccumulateSharpenPipeline(Fsr2.ContextDescription contextDescription, ComputeBuffer constants)
: base(contextDescription, constants)
{
// Simply loading the accumulate_pass compute shader will give us the same instance as the non-sharpen pipeline
// So we have to clone the shader instance and set the extra keyword on the new copy
_shaderCopy = UnityEngine.Object.Instantiate(ComputeShader);
foreach (var keyword in ComputeShader.enabledKeywords)
{
_shaderCopy.EnableKeyword(keyword.name);
}
_shaderCopy.EnableKeyword("FFX_FSR2_OPTION_APPLY_SHARPENING");
}
public override void ScheduleDispatch(CommandBuffer commandBuffer, Fsr2.DispatchDescription dispatchParams, int dispatchX, int dispatchY)
{
// Temporarily swap around the shaders so that the dispatch will bind and execute the correct one
ComputeShader tmp = ComputeShader;
ComputeShader = _shaderCopy;
base.ScheduleDispatch(commandBuffer, dispatchParams, dispatchX, dispatchY);
ComputeShader = tmp;
}
public override void Dispose()
{
// Since we instantiated this copy, we have to destroy it instead of unloading the shader resource
UnityEngine.Object.Destroy(_shaderCopy);
base.Dispose();
}
}
internal class Fsr2RcasPipeline : Fsr2Pipeline
{
private readonly ComputeBuffer _rcasConstants;
public Fsr2RcasPipeline(Fsr2.ContextDescription contextDescription, ComputeBuffer constants, ComputeBuffer rcasConstants)
: base(contextDescription, constants)
{
_rcasConstants = rcasConstants;
LoadComputeShader("FSR2/ffx_fsr2_rcas_pass");
}
public override void ScheduleDispatch(CommandBuffer commandBuffer, Fsr2.DispatchDescription dispatchParams, int dispatchX, int dispatchY)
{
// Run the RCAS sharpening filter on the upscaled image
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, SrvInputExposure, dispatchParams.Exposure);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, SrvRcasInput, dispatchParams.Input);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, UavUpscaledOutput, dispatchParams.Output);
commandBuffer.SetComputeConstantBufferParam(ComputeShader, CbFsr2, Constants, 0, Marshal.SizeOf<Fsr2.Fsr2Constants>());
commandBuffer.SetComputeConstantBufferParam(ComputeShader, CbRcas, _rcasConstants, 0, Marshal.SizeOf<Fsr2.RcasConstants>());
commandBuffer.DispatchCompute(ComputeShader, KernelIndex, dispatchX, dispatchY, 1);
}
}
}