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.
 
 
 
 

238 lines
12 KiB

using System;
using System.Runtime.InteropServices;
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;
protected CustomSampler Sampler;
protected bool AsyncSupported => (ContextDescription.flags & FrameInterpolation.InitializationFlags.EnableAsyncSupport) == FrameInterpolation.InitializationFlags.EnableAsyncSupport;
protected FrameInterpolationPass(FrameInterpolation.ContextDescription contextDescription, FrameInterpolationResources resources, ComputeBuffer constants)
{
ContextDescription = contextDescription;
Resources = resources;
Constants = constants;
}
public virtual void Dispose()
{
}
public void ScheduleDispatch(CommandBuffer commandBuffer, FrameInterpolation.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, FrameInterpolation.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY, int dispatchZ);
protected void InitComputeShader(string passName, ComputeShader shader)
{
InitComputeShader(passName, shader, ContextDescription.flags);
}
private void InitComputeShader(string passName, ComputeShader shader, FrameInterpolation.InitializationFlags flags)
{
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);
if ((flags & FrameInterpolation.InitializationFlags.EnableDisplayResolutionMotionVectors) == 0) ComputeShader.EnableKeyword("FFX_FRAMEINTERPOLATION_OPTION_LOW_RES_MOTION_VECTORS");
if ((flags & FrameInterpolation.InitializationFlags.EnableJitterMotionVectors) != 0) ComputeShader.EnableKeyword("FFX_FRAMEINTERPOLATION_OPTION_JITTERED_MOTION_VECTORS");
if ((flags & FrameInterpolation.InitializationFlags.EnableDepthInverted) != 0) ComputeShader.EnableKeyword("FFX_FRAMEINTERPOLATION_OPTION_INVERTED_DEPTH");
}
}
internal class FrameInterpolationReconstructAndDilatePass : FrameInterpolationPass
{
public FrameInterpolationReconstructAndDilatePass(FrameInterpolation.ContextDescription contextDescription, FrameInterpolationResources resources, ComputeBuffer constants)
: base(contextDescription, resources, constants)
{
InitComputeShader("Reconstruct and Dilate", contextDescription.shaders.reconstructAndDilate);
}
protected override void DoScheduleDispatch(CommandBuffer commandBuffer, FrameInterpolation.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY, int dispatchZ)
{
}
public void ScheduleDispatch(CommandBuffer commandBuffer, FrameInterpolation.PrepareDescription prepareParams, int frameIndex, int dispatchX, int dispatchY, int dispatchZ)
{
int doubleBufferId = AsyncSupported ? frameIndex : 0;
commandBuffer.BeginSample(Sampler);
ref var mvs = ref prepareParams.motionVectors;
ref var depth = ref prepareParams.depth;
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, FrameInterpolationShaderIDs.SrvInputMotionVectors, mvs.RenderTarget, mvs.MipLevel, mvs.SubElement);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, FrameInterpolationShaderIDs.SrvInputDepth, depth.RenderTarget, depth.MipLevel, depth.SubElement);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, FrameInterpolationShaderIDs.UavReconstructedDepthPreviousFrame, Resources.ReconstructedDepth[doubleBufferId]);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, FrameInterpolationShaderIDs.UavDilatedMotionVectors, Resources.DilatedMotionVectors[doubleBufferId]);
commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, FrameInterpolationShaderIDs.UavDilatedDepth, Resources.DilatedDepth[doubleBufferId]);
commandBuffer.SetComputeConstantBufferParam(ComputeShader, FrameInterpolationShaderIDs.CbFrameInterpolation, Constants, 0, Marshal.SizeOf<FrameInterpolation.Constants>());
commandBuffer.DispatchCompute(ComputeShader, KernelIndex, dispatchX, dispatchY, dispatchZ);
commandBuffer.EndSample(Sampler);
}
}
internal class FrameInterpolationSetupPass : FrameInterpolationPass
{
public FrameInterpolationSetupPass(FrameInterpolation.ContextDescription contextDescription, FrameInterpolationResources resources, ComputeBuffer constants)
: base(contextDescription, resources, constants)
{
InitComputeShader("Setup", contextDescription.shaders.setup);
}
protected override void DoScheduleDispatch(CommandBuffer commandBuffer, FrameInterpolation.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY, int dispatchZ)
{
throw new NotImplementedException();
}
}
internal class FrameInterpolationReconstructPreviousDepthPass : FrameInterpolationPass
{
public FrameInterpolationReconstructPreviousDepthPass(FrameInterpolation.ContextDescription contextDescription, FrameInterpolationResources resources, ComputeBuffer constants)
: base(contextDescription, resources, constants)
{
InitComputeShader("Reconstruct Previous Depth", contextDescription.shaders.reconstructPreviousDepth);
}
protected override void DoScheduleDispatch(CommandBuffer commandBuffer, FrameInterpolation.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY, int dispatchZ)
{
throw new NotImplementedException();
}
}
internal class FrameInterpolationGameMotionVectorFieldPass : FrameInterpolationPass
{
public FrameInterpolationGameMotionVectorFieldPass(FrameInterpolation.ContextDescription contextDescription, FrameInterpolationResources resources, ComputeBuffer constants)
: base(contextDescription, resources, constants)
{
InitComputeShader("Game Motion Vector Field", contextDescription.shaders.gameMotionVectorField);
}
protected override void DoScheduleDispatch(CommandBuffer commandBuffer, FrameInterpolation.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY, int dispatchZ)
{
throw new NotImplementedException();
}
}
internal class FrameInterpolationOpticalFlowVectorFieldPass : FrameInterpolationPass
{
public FrameInterpolationOpticalFlowVectorFieldPass(FrameInterpolation.ContextDescription contextDescription, FrameInterpolationResources resources, ComputeBuffer constants)
: base(contextDescription, resources, constants)
{
InitComputeShader("Optical Flow Vector Field", contextDescription.shaders.opticalFlowVectorField);
}
protected override void DoScheduleDispatch(CommandBuffer commandBuffer, FrameInterpolation.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY, int dispatchZ)
{
throw new NotImplementedException();
}
}
internal class FrameInterpolationDisocclusionMaskPass : FrameInterpolationPass
{
public FrameInterpolationDisocclusionMaskPass(FrameInterpolation.ContextDescription contextDescription, FrameInterpolationResources resources, ComputeBuffer constants)
: base(contextDescription, resources, constants)
{
InitComputeShader("Disocclusion Mask", contextDescription.shaders.disocclusionMask);
}
protected override void DoScheduleDispatch(CommandBuffer commandBuffer, FrameInterpolation.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY, int dispatchZ)
{
throw new NotImplementedException();
}
}
internal class FrameInterpolationInterpolationPass : FrameInterpolationPass
{
public FrameInterpolationInterpolationPass(FrameInterpolation.ContextDescription contextDescription, FrameInterpolationResources resources, ComputeBuffer constants)
: base(contextDescription, resources, constants)
{
InitComputeShader("Interpolation", contextDescription.shaders.interpolation);
}
protected override void DoScheduleDispatch(CommandBuffer commandBuffer, FrameInterpolation.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY, int dispatchZ)
{
throw new NotImplementedException();
}
}
internal class FrameInterpolationInpaintingPyramidPass : FrameInterpolationPass
{
public FrameInterpolationInpaintingPyramidPass(FrameInterpolation.ContextDescription contextDescription, FrameInterpolationResources resources, ComputeBuffer constants)
: base(contextDescription, resources, constants)
{
InitComputeShader("Inpainting Pyramid", contextDescription.shaders.inpaintingPyramid);
}
protected override void DoScheduleDispatch(CommandBuffer commandBuffer, FrameInterpolation.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY, int dispatchZ)
{
throw new NotImplementedException();
}
}
internal class FrameInterpolationInpaintingPass : FrameInterpolationPass
{
public FrameInterpolationInpaintingPass(FrameInterpolation.ContextDescription contextDescription, FrameInterpolationResources resources, ComputeBuffer constants)
: base(contextDescription, resources, constants)
{
InitComputeShader("Inpainting", contextDescription.shaders.inpainting);
}
protected override void DoScheduleDispatch(CommandBuffer commandBuffer, FrameInterpolation.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY, int dispatchZ)
{
throw new NotImplementedException();
}
}
internal class FrameInterpolationGameVectorFieldInpaintingPyramidPass : FrameInterpolationPass
{
public FrameInterpolationGameVectorFieldInpaintingPyramidPass(FrameInterpolation.ContextDescription contextDescription, FrameInterpolationResources resources, ComputeBuffer constants)
: base(contextDescription, resources, constants)
{
InitComputeShader("Game Vector Field Inpainting Pyramid", contextDescription.shaders.gameVectorFieldInpaintingPyramid);
}
protected override void DoScheduleDispatch(CommandBuffer commandBuffer, FrameInterpolation.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY, int dispatchZ)
{
throw new NotImplementedException();
}
}
internal class FrameInterpolationDebugViewPass : FrameInterpolationPass
{
public FrameInterpolationDebugViewPass(FrameInterpolation.ContextDescription contextDescription, FrameInterpolationResources resources, ComputeBuffer constants)
: base(contextDescription, resources, constants)
{
InitComputeShader("Debug View", contextDescription.shaders.debugView);
}
protected override void DoScheduleDispatch(CommandBuffer commandBuffer, FrameInterpolation.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY, int dispatchZ)
{
throw new NotImplementedException();
}
}
}