Browse Source

Set up initial skeleton for Frame Interpolation implementation

fsr3framegen
Nico de Poel 2 years ago
parent
commit
e6c714c02e
  1. 53
      Runtime/FrameInterpolation/FrameInterpolation.cs
  2. 101
      Runtime/FrameInterpolation/FrameInterpolationContext.cs
  3. 53
      Runtime/FrameInterpolation/FrameInterpolationPass.cs
  4. 10
      Runtime/FrameInterpolation/FrameInterpolationResources.cs
  5. 10
      Runtime/FrameInterpolation/FrameInterpolationShaderIDs.cs

53
Runtime/FrameInterpolation/FrameInterpolation.cs

@ -0,0 +1,53 @@
using System;
using System.Runtime.InteropServices;
namespace FidelityFX.FrameGen
{
public static class FrameInterpolation
{
public struct ContextDescription
{
}
// TODO: turn all of these into structs
public class DispatchDescription
{
}
[Flags]
public enum InitializationFlags
{
EnableDepthInverted = 1 << 0,
EnableDepthInfinite = 1 << 1,
EnableHDRColorInput = 1 << 3,
EnableDisplayResolutionMotionVectors = 1 << 4,
EnableJitterMotionVectors = 1 << 5,
EnableAsyncSupport = 1 << 6,
}
[Flags]
public enum DispatchFlags
{
DrawDebugTearLines = 1 << 0,
DrawDebugResetIndicators = 1 << 1,
DrawDebugView = 1 << 2,
}
[Serializable, StructLayout(LayoutKind.Sequential)]
internal struct Constants
{
}
[Serializable, StructLayout(LayoutKind.Sequential)]
internal struct InpaintingPyramidConstants
{
public uint mips;
public uint numWorkGroups;
public uint workGroupOffsetX;
public uint workGroupOffsetY;
}
}
}

101
Runtime/FrameInterpolation/FrameInterpolationContext.cs

@ -0,0 +1,101 @@
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Profiling;
using UnityEngine.Rendering;
namespace FidelityFX.FrameGen
{
public class FrameInterpolationContext
{
private FrameInterpolation.ContextDescription _contextDescription;
private FrameInterpolationPass _reconstructAndDilatePass;
private FrameInterpolationPass _setupPass;
private FrameInterpolationPass _reconstructPreviousDepthPass;
private FrameInterpolationPass _gameMotionVectorFieldPass;
private FrameInterpolationPass _opticalFlowVectorFieldPass;
private FrameInterpolationPass _disocclusionMaskPass;
private FrameInterpolationPass _scfiPass;
private FrameInterpolationPass _inpaintingPyramidPass;
private FrameInterpolationPass _inpaintingPass;
private FrameInterpolationPass _gameVectorFieldInpaintingPyramidPass;
private FrameInterpolationPass _debugViewPass;
private readonly FrameInterpolationResources _resources = new FrameInterpolationResources();
private ComputeBuffer _frameInterpolationConstantsBuffer;
private readonly FrameInterpolation.Constants[] _frameInterpolationConstantsArray = { new FrameInterpolation.Constants() };
private ref FrameInterpolation.Constants Constants => ref _frameInterpolationConstantsArray[0];
private ComputeBuffer _spdConstantsBuffer;
private readonly FrameInterpolation.InpaintingPyramidConstants[] _spdConstantsArray = { new FrameInterpolation.InpaintingPyramidConstants() };
private ref FrameInterpolation.InpaintingPyramidConstants SpdConstants => ref _spdConstantsArray[0];
private readonly CustomSampler _sampler = CustomSampler.Create("Frame Interpolation");
public void Create(in FrameInterpolation.ContextDescription contextDescription)
{
_contextDescription = contextDescription;
_frameInterpolationConstantsBuffer = CreateConstantBuffer<FrameInterpolation.Constants>();
_spdConstantsBuffer = CreateConstantBuffer<FrameInterpolation.InpaintingPyramidConstants>();
CreatePasses();
}
private void CreatePasses()
{
}
public void Destroy()
{
DestroyPass(ref _debugViewPass);
DestroyPass(ref _gameVectorFieldInpaintingPyramidPass);
DestroyPass(ref _inpaintingPass);
DestroyPass(ref _inpaintingPyramidPass);
DestroyPass(ref _scfiPass);
DestroyPass(ref _disocclusionMaskPass);
DestroyPass(ref _opticalFlowVectorFieldPass);
DestroyPass(ref _gameMotionVectorFieldPass);
DestroyPass(ref _reconstructPreviousDepthPass);
DestroyPass(ref _setupPass);
DestroyPass(ref _reconstructAndDilatePass);
_resources.Destroy();
DestroyConstantBuffer(ref _spdConstantsBuffer);
DestroyConstantBuffer(ref _frameInterpolationConstantsBuffer);
}
public void Dispatch(CommandBuffer commandBuffer, FrameInterpolation.DispatchDescription dispatchDescription)
{
commandBuffer.BeginSample(_sampler);
commandBuffer.EndSample(_sampler);
}
private static ComputeBuffer CreateConstantBuffer<TConstants>() where TConstants: struct
{
return new ComputeBuffer(1, Marshal.SizeOf<TConstants>(), ComputeBufferType.Constant);
}
private static void DestroyConstantBuffer(ref ComputeBuffer bufferRef)
{
if (bufferRef == null)
return;
bufferRef.Release();
bufferRef = null;
}
private static void DestroyPass(ref FrameInterpolationPass pass)
{
if (pass == null)
return;
pass.Dispose();
pass = null;
}
}
}

53
Runtime/FrameInterpolation/FrameInterpolationPass.cs

@ -0,0 +1,53 @@
using System;
using UnityEngine;
using UnityEngine.Profiling;
using UnityEngine.Rendering;
namespace FidelityFX.FrameGen
{
internal abstract class FrameInterpolationPass: IDisposable
{
protected readonly FrameInterpolation.ContextDescription ContextDescription;
protected readonly FrameInterpolationResources Resources;
protected readonly ComputeBuffer Constants;
protected ComputeShader ComputeShader;
protected int KernelIndex;
private CustomSampler _sampler;
protected FrameInterpolationPass(FrameInterpolation.ContextDescription contextDescription, FrameInterpolationResources resources, ComputeBuffer constants)
{
ContextDescription = contextDescription;
Resources = resources;
Constants = constants;
}
public virtual void Dispose()
{
}
public void ScheduleDispatch(CommandBuffer commandBuffer, OpticalFlow.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY, int dispatchZ = 1)
{
commandBuffer.BeginSample(_sampler);
DoScheduleDispatch(commandBuffer, dispatchParams, frameIndex, dispatchX, dispatchY, dispatchZ);
commandBuffer.EndSample(_sampler);
}
protected abstract void DoScheduleDispatch(CommandBuffer commandBuffer, OpticalFlow.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY, int dispatchZ);
protected void InitComputeShader(string passName, ComputeShader shader)
{
if (shader == null)
{
throw new MissingReferenceException($"Shader for Frame Interpolation pass '{passName}' could not be loaded! Please ensure it is included in the project correctly.");
}
ComputeShader = shader;
KernelIndex = ComputeShader.FindKernel("CS");
_sampler = CustomSampler.Create(passName);
// TODO: shader variants
}
}
}

10
Runtime/FrameInterpolation/FrameInterpolationResources.cs

@ -0,0 +1,10 @@
namespace FidelityFX.FrameGen
{
internal class FrameInterpolationResources
{
public void Destroy()
{
}
}
}

10
Runtime/FrameInterpolation/FrameInterpolationShaderIDs.cs

@ -0,0 +1,10 @@
using UnityEngine;
namespace FidelityFX.FrameGen
{
public static class FrameInterpolationShaderIDs
{
public static readonly int SrvInputDepth = Shader.PropertyToID("r_input_depth");
public static readonly int SrvInputMotionVectors = Shader.PropertyToID("r_input_motion_vectors");
}
}
Loading…
Cancel
Save