@ -8,7 +8,7 @@ namespace FidelityFX
{
internal abstract class Fsr2Pipeline : IDisposable
{
internal const int ShadingChangeMipLevel = 4 ; // Corresponds to FFX_FSR2_SHADING_CHANGE_MIP_LEVEL define
internal const int ShadingChangeMipLevel = 4 ; // This matches the FFX_FSR2_SHADING_CHANGE_MIP_LEVEL define
protected readonly Fsr2 . ContextDescription ContextDescription ;
protected readonly ComputeBuffer Constants ;
@ -30,6 +30,10 @@ namespace FidelityFX
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" ) ;
protected static readonly int UavReconstructedPrevNearestDepth = Shader . PropertyToID ( "rw_reconstructed_previous_nearest_depth" ) ;
protected static readonly int UavDilatedMotionVectors = Shader . PropertyToID ( "rw_dilated_motion_vectors" ) ;
protected static readonly int UavDilatedDepth = Shader . PropertyToID ( "rw_dilatedDepth" ) ;
protected static readonly int UavLockInputLuma = Shader . PropertyToID ( "rw_lock_input_luma" ) ;
// Constant buffer bindings
protected static readonly int CbFsr2 = Shader . PropertyToID ( "cbFSR2" ) ;
@ -48,7 +52,7 @@ namespace FidelityFX
UnloadComputeShader ( ) ;
}
public abstract void ScheduleDispatch ( CommandBuffer commandBuffer , Fsr2 . DispatchDescription dispatchParams , int dispatchX , int dispatchY ) ;
public abstract void ScheduleDispatch ( CommandBuffer commandBuffer , Fsr2 . DispatchDescription dispatchParams , int frameIndex , int dispatchX , int dispatchY ) ;
public static void RegisterResources ( CommandBuffer commandBuffer , Fsr2 . ContextDescription contextDescription , Fsr2 . DispatchDescription dispatchParams )
{
@ -65,6 +69,15 @@ namespace FidelityFX
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 ) ;
// FSR2_ReconstructedPrevNearestDepth: FFX_RESOURCE_USAGE_UAV, FFX_SURFACE_FORMAT_R32_UINT, FFX_RESOURCE_FLAGS_ALIASABLE
commandBuffer . GetTemporaryRT ( UavReconstructedPrevNearestDepth , maxRenderSize . x , maxRenderSize . y , 0 , FilterMode . Point , GraphicsFormat . R32_UInt , 1 , true ) ;
// FSR2_DilatedDepth: FFX_RESOURCE_USAGE_RENDERTARGET | FFX_RESOURCE_USAGE_UAV, FFX_SURFACE_FORMAT_R32_FLOAT, FFX_RESOURCE_FLAGS_ALIASABLE
commandBuffer . GetTemporaryRT ( UavDilatedDepth , maxRenderSize . x , maxRenderSize . y , 0 , FilterMode . Point , GraphicsFormat . R32_SFloat , 1 , true ) ;
// FSR2_LockInputLuma: FFX_RESOURCE_USAGE_UAV, FFX_SURFACE_FORMAT_R16_FLOAT, FFX_RESOURCE_FLAGS_ALIASABLE
commandBuffer . GetTemporaryRT ( UavLockInputLuma , displaySize . x , displaySize . y , 0 , FilterMode . Point , GraphicsFormat . R16_SFloat , 1 , true ) ;
}
public static void UnregisterResources ( CommandBuffer commandBuffer )
@ -73,6 +86,9 @@ namespace FidelityFX
commandBuffer . ReleaseTemporaryRT ( UavSpdAtomicCount ) ;
commandBuffer . ReleaseTemporaryRT ( UavExposureMipLumaChange ) ;
commandBuffer . ReleaseTemporaryRT ( UavExposureMip5 ) ;
commandBuffer . ReleaseTemporaryRT ( UavReconstructedPrevNearestDepth ) ;
commandBuffer . ReleaseTemporaryRT ( UavDilatedDepth ) ;
commandBuffer . ReleaseTemporaryRT ( UavLockInputLuma ) ;
}
protected void LoadComputeShader ( string name )
@ -127,7 +143,7 @@ namespace FidelityFX
LoadComputeShader ( "FSR2/ffx_fsr2_compute_luminance_pyramid_pass" ) ;
}
public override void ScheduleDispatch ( CommandBuffer commandBuffer , Fsr2 . DispatchDescription dispatchParams , int dispatchX , int dispatchY )
public override void ScheduleDispatch ( CommandBuffer commandBuffer , Fsr2 . DispatchDescription dispatchParams , int frameIndex , int dispatchX , int dispatchY )
{
// Problems to solve:
// - How do resources (render textures) relate to SRV/UAV bindings? How are those tied together?
@ -135,7 +151,7 @@ namespace FidelityFX
// - 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 , SrvInputColor , new RenderTargetIdentifier ( BuiltinRenderTextureType . CurrentActive ) ) ;
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 > ( ) ) ;
@ -146,27 +162,24 @@ namespace FidelityFX
internal class Fsr2ReconstructPreviousDepthPipeline : Fsr2Pipeline
{
public Fsr2ReconstructPreviousDepthPipeline ( Fsr2 . ContextDescription contextDescription , ComputeBuffer constants )
private readonly RenderTexture [ ] _dilatedMotionVectors ;
public Fsr2ReconstructPreviousDepthPipeline ( Fsr2 . ContextDescription contextDescription , ComputeBuffer constants , RenderTexture [ ] dilatedMotionVectors )
: base ( contextDescription , constants )
{
_dilatedMotionVectors = dilatedMotionVectors ;
LoadComputeShader ( "FSR2/ffx_fsr2_reconstruct_previous_depth_pass" ) ;
}
public override void ScheduleDispatch ( CommandBuffer commandBuffer , Fsr2 . DispatchDescription dispatchParams , int dispatchX , int dispatchY )
public override void ScheduleDispatch ( CommandBuffer commandBuffer , Fsr2 . DispatchDescription dispatchParams , int frameIndex , 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 . SetComputeTextureParam ( ComputeShader , KernelIndex , SrvInputMotionVectors , new RenderTargetIdentifier ( BuiltinRenderTextureType . MotionVectors ) ) ;
commandBuffer . SetComputeTextureParam ( ComputeShader , KernelIndex , SrvInputDepth , new RenderTargetIdentifier ( BuiltinRenderTextureType . Depth ) ) ;
commandBuffer . SetComputeTextureParam ( ComputeShader , KernelIndex , SrvInputColor , new RenderTargetIdentifier ( BuiltinRenderTextureType . CurrentActive ) ) ;
commandBuffer . SetComputeTextureParam ( ComputeShader , KernelIndex , SrvInputExposure , dispatchParams . Exposure ) ;
commandBuffer . SetComputeTextureParam ( ComputeShader , KernelIndex , UavDilatedMotionVectors , _dilatedMotionVectors [ frameIndex ] ) ;
commandBuffer . SetComputeConstantBufferParam ( ComputeShader , CbFsr2 , Constants , 0 , Marshal . SizeOf < Fsr2 . Fsr2Constants > ( ) ) ;
@ -182,7 +195,7 @@ namespace FidelityFX
LoadComputeShader ( "FSR2/ffx_fsr2_accumulate_pass" ) ;
}
public override void ScheduleDispatch ( CommandBuffer commandBuffer , Fsr2 . DispatchDescription dispatchParams , int dispatchX , int dispatchY )
public override void ScheduleDispatch ( CommandBuffer commandBuffer , Fsr2 . DispatchDescription dispatchParams , int frameIndex , int dispatchX , int dispatchY )
{
//throw new NotImplementedException();
}
@ -205,12 +218,12 @@ namespace FidelityFX
_shaderCopy . EnableKeyword ( "FFX_FSR2_OPTION_APPLY_SHARPENING" ) ;
}
public override void ScheduleDispatch ( CommandBuffer commandBuffer , Fsr2 . DispatchDescription dispatchParams , int dispatchX , int dispatchY )
public override void ScheduleDispatch ( CommandBuffer commandBuffer , Fsr2 . DispatchDescription dispatchParams , int frameIndex , 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 ) ;
base . ScheduleDispatch ( commandBuffer , dispatchParams , frameIndex , dispatchX , dispatchY ) ;
ComputeShader = tmp ;
}
@ -234,7 +247,7 @@ namespace FidelityFX
LoadComputeShader ( "FSR2/ffx_fsr2_rcas_pass" ) ;
}
public override void ScheduleDispatch ( CommandBuffer commandBuffer , Fsr2 . DispatchDescription dispatchParams , int dispatchX , int dispatchY )
public override void ScheduleDispatch ( CommandBuffer commandBuffer , Fsr2 . DispatchDescription dispatchParams , int frameIndex , int dispatchX , int dispatchY )
{
// Run the RCAS sharpening filter on the upscaled image
commandBuffer . SetComputeTextureParam ( ComputeShader , KernelIndex , SrvInputExposure , dispatchParams . Exposure ) ;