Browse Source

A bit of reorganization in how shaders and buffers are created and destroyed.

Also using Unity's system info to decide on inverted depth or not.
mac-autoexp
Nico de Poel 3 years ago
parent
commit
a576f94e95
  1. 6
      Assets/Scripts/Fsr2.cs
  2. 5
      Assets/Scripts/Fsr2Callbacks.cs
  3. 71
      Assets/Scripts/Fsr2Context.cs

6
Assets/Scripts/Fsr2.cs

@ -1,5 +1,6 @@
using System; using System;
using UnityEngine; using UnityEngine;
using UnityEngine.Rendering;
namespace FidelityFX namespace FidelityFX
{ {
@ -63,7 +64,10 @@ namespace FidelityFX
/// </summary> /// </summary>
public static Fsr2Context CreateContext(InitializationFlags flags = 0) public static Fsr2Context CreateContext(InitializationFlags flags = 0)
{ {
// flags |= InitializationFlags.EnableDepthInverted; // Depends on the runtime platform
if (SystemInfo.usesReversedZBuffer)
flags |= InitializationFlags.EnableDepthInverted;
else
flags &= ~InitializationFlags.EnableDepthInverted;
var contextDescription = new ContextDescription var contextDescription = new ContextDescription
{ {

5
Assets/Scripts/Fsr2Callbacks.cs

@ -14,6 +14,11 @@ namespace FidelityFX
return Resources.Load<ComputeShader>(name); return Resources.Load<ComputeShader>(name);
} }
public virtual void UnloadComputeShader(ComputeShader shader)
{
Resources.UnloadAsset(shader);
}
public virtual void ApplyMipmapBias(float biasOffset) public virtual void ApplyMipmapBias(float biasOffset)
{ {
foreach (var texture in Resources.FindObjectsOfTypeAll<Texture2D>()) foreach (var texture in Resources.FindObjectsOfTypeAll<Texture2D>())

71
Assets/Scripts/Fsr2Context.cs

@ -32,9 +32,9 @@ namespace FidelityFX
{ {
_contextDescription = contextDescription; _contextDescription = contextDescription;
_fsr2ConstantsBuffer = new ComputeBuffer(1, Marshal.SizeOf<Fsr2Constants>(), ComputeBufferType.Constant);
_spdConstantsBuffer = new ComputeBuffer(1, Marshal.SizeOf<SpdConstants>(), ComputeBufferType.Constant);
_rcasConstantsBuffer = new ComputeBuffer(1, Marshal.SizeOf<RcasConstants>(), ComputeBufferType.Constant);
_fsr2ConstantsBuffer = CreateConstantBuffer<Fsr2Constants>();
_spdConstantsBuffer = CreateConstantBuffer<SpdConstants>();
_rcasConstantsBuffer = CreateConstantBuffer<RcasConstants>();
// Set defaults // Set defaults
_fsr2ConstantsArray[0].displaySize = _contextDescription.DisplaySize; _fsr2ConstantsArray[0].displaySize = _contextDescription.DisplaySize;
@ -71,12 +71,6 @@ namespace FidelityFX
LoadComputeShader("FSR2/ffx_fsr2_tcr_autogen_pass", ref _tcrAutogenShader); LoadComputeShader("FSR2/ffx_fsr2_tcr_autogen_pass", ref _tcrAutogenShader);
} }
private void LoadComputeShader(string name, ref ComputeShader shaderRef)
{
if (shaderRef == null)
shaderRef = _contextDescription.Callbacks.LoadComputeShader(name);
}
public void Dispatch(Fsr2.DispatchDescription dispatchDescription) public void Dispatch(Fsr2.DispatchDescription dispatchDescription)
{ {
_fsr2ConstantsArray[0].preExposure = dispatchDescription.PreExposure; _fsr2ConstantsArray[0].preExposure = dispatchDescription.PreExposure;
@ -110,23 +104,19 @@ namespace FidelityFX
public void Destroy() public void Destroy()
{ {
if (_rcasConstantsBuffer != null)
{
_rcasConstantsBuffer.Release();
_rcasConstantsBuffer = null;
}
if (_spdConstantsBuffer != null)
{
_spdConstantsBuffer.Release();
_spdConstantsBuffer = null;
}
if (_fsr2ConstantsBuffer != null)
{
_fsr2ConstantsBuffer.Release();
_fsr2ConstantsBuffer = null;
}
DestroyConstantBuffer(ref _rcasConstantsBuffer);
DestroyConstantBuffer(ref _spdConstantsBuffer);
DestroyConstantBuffer(ref _fsr2ConstantsBuffer);
DestroyComputeShader(ref _tcrAutogenShader);
DestroyComputeShader(ref _generateReactiveShader);
DestroyComputeShader(ref _accumulateShader);
DestroyComputeShader(ref _lockShader);
DestroyComputeShader(ref _reconstructPreviousDepthShader);
DestroyComputeShader(ref _depthClipShader);
DestroyComputeShader(ref _prepareInputColorShader);
DestroyComputeShader(ref _rcasShader);
DestroyComputeShader(ref _computeLuminancePyramidShader);
} }
[Serializable, StructLayout(LayoutKind.Sequential)] [Serializable, StructLayout(LayoutKind.Sequential)]
@ -203,5 +193,34 @@ namespace FidelityFX
new(1064229695u, 997604214u), new(1064229695u, 997604214u),
new(1065353216u, 1006648320), new(1065353216u, 1006648320),
}; };
private static ComputeBuffer CreateConstantBuffer<TConstants>() where TConstants: struct
{
return new ComputeBuffer(1, Marshal.SizeOf<TConstants>(), ComputeBufferType.Constant);
}
private void LoadComputeShader(string name, ref ComputeShader shaderRef)
{
if (shaderRef == null)
shaderRef = _contextDescription.Callbacks.LoadComputeShader(name);
}
private static void DestroyConstantBuffer(ref ComputeBuffer bufferRef)
{
if (bufferRef == null)
return;
bufferRef.Release();
bufferRef = null;
}
private void DestroyComputeShader(ref ComputeShader shaderRef)
{
if (shaderRef == null)
return;
_contextDescription.Callbacks.UnloadComputeShader(shaderRef);
shaderRef = null;
}
} }
} }
Loading…
Cancel
Save