Browse Source

Moved all create/destroy methods for various types of resources to a new common base class

framework
Nico de Poel 2 years ago
parent
commit
6219d6159d
  1. 80
      Runtime/Common/FfxResourcesBase.cs
  2. 11
      Runtime/Common/FfxResourcesBase.cs.meta
  3. 42
      Runtime/FSR2/Fsr2Resources.cs
  4. 42
      Runtime/FSR3/Fsr3UpscalerResources.cs

80
Runtime/Common/FfxResourcesBase.cs

@ -0,0 +1,80 @@
using System;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
namespace FidelityFX
{
internal abstract class FfxResourcesBase
{
protected static ComputeBuffer CreateBuffer<TElem>(string name, int count)
{
return new ComputeBuffer(count, Marshal.SizeOf<TElem>());
}
protected static RenderTexture CreateResource(string name, in Vector2Int size, GraphicsFormat format)
{
var rt = new RenderTexture(size.x, size.y, 0, format) { name = name, enableRandomWrite = true };
rt.Create();
return rt;
}
protected static RenderTexture CreateResourceMips(string name, in Vector2Int size, GraphicsFormat format)
{
int mipCount = 1 + Mathf.FloorToInt(Mathf.Log(Math.Max(size.x, size.y), 2.0f));
var rt = new RenderTexture(size.x, size.y, 0, format, mipCount) { name = name, enableRandomWrite = true, useMipMap = true, autoGenerateMips = false };
rt.Create();
return rt;
}
protected static void CreateDoubleBufferedResource(RenderTexture[] resource, string name, in Vector2Int size, GraphicsFormat format, int numElements = 2)
{
numElements = Math.Min(resource.Length, numElements);
for (int i = 0; i < numElements; ++i)
{
resource[i] = new RenderTexture(size.x, size.y, 0, format) { name = name + (i + 1), enableRandomWrite = true };
resource[i].Create();
}
}
protected static void DestroyResource(ref Texture2D resource)
{
if (resource == null)
return;
#if UNITY_EDITOR
if (Application.isPlaying && !UnityEditor.EditorApplication.isPaused)
UnityEngine.Object.Destroy(resource);
else
UnityEngine.Object.DestroyImmediate(resource);
#else
UnityEngine.Object.Destroy(resource);
#endif
resource = null;
}
protected static void DestroyResource(ref RenderTexture resource)
{
if (resource == null)
return;
resource.Release();
resource = null;
}
protected static void DestroyResource(ref ComputeBuffer resource)
{
if (resource == null)
return;
resource.Release();
resource = null;
}
protected static void DestroyResource(RenderTexture[] resource)
{
for (int i = 0; i < resource.Length; ++i)
DestroyResource(ref resource[i]);
}
}
}

11
Runtime/Common/FfxResourcesBase.cs.meta

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2a46b97ac72c45545a80a896462e1112
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

42
Runtime/FSR2/Fsr2Resources.cs

@ -29,7 +29,7 @@ namespace FidelityFX.FSR2
/// Helper class for bundling and managing persistent resources required by the FSR2 process.
/// This includes lookup tables, default fallback resources and double-buffered resources that get swapped between frames.
/// </summary>
internal class Fsr2Resources
internal class Fsr2Resources: FfxResourcesBase
{
public Texture2D DefaultExposure;
public Texture2D DefaultReactive;
@ -169,15 +169,6 @@ namespace FidelityFX.FSR2
commandBuffer.ReleaseTemporaryRT(Fsr2ShaderIDs.UavNewLocks);
}
private static void CreateDoubleBufferedResource(RenderTexture[] resource, string name, Vector2Int size, GraphicsFormat format)
{
for (int i = 0; i < 2; ++i)
{
resource[i] = new RenderTexture(size.x, size.y, 0, format) { name = name + (i + 1), enableRandomWrite = true };
resource[i].Create();
}
}
public void Destroy()
{
DestroyTcrAutogenResources();
@ -201,37 +192,6 @@ namespace FidelityFX.FSR2
DestroyResource(ref AutoComposition);
DestroyResource(ref AutoReactive);
}
private static void DestroyResource(ref Texture2D resource)
{
if (resource == null)
return;
#if UNITY_EDITOR
if (Application.isPlaying && !UnityEditor.EditorApplication.isPaused)
UnityEngine.Object.Destroy(resource);
else
UnityEngine.Object.DestroyImmediate(resource);
#else
UnityEngine.Object.Destroy(resource);
#endif
resource = null;
}
private static void DestroyResource(ref RenderTexture resource)
{
if (resource == null)
return;
resource.Release();
resource = null;
}
private static void DestroyResource(RenderTexture[] resource)
{
for (int i = 0; i < resource.Length; ++i)
DestroyResource(ref resource[i]);
}
private const int MaximumBiasTextureWidth = 16;
private const int MaximumBiasTextureHeight = 16;

42
Runtime/FSR3/Fsr3UpscalerResources.cs

@ -29,7 +29,7 @@ namespace FidelityFX.FSR3
/// Helper class for bundling and managing persistent resources required by the FSR3 Upscaler process.
/// This includes lookup tables, default fallback resources and double-buffered resources that get swapped between frames.
/// </summary>
internal class Fsr3UpscalerResources
internal class Fsr3UpscalerResources: FfxResourcesBase
{
public Texture2D LanczosLut;
public Texture2D DefaultExposure;
@ -173,15 +173,6 @@ namespace FidelityFX.FSR3
commandBuffer.ReleaseTemporaryRT(Fsr3ShaderIDs.UavIntermediate);
}
private static void CreateDoubleBufferedResource(RenderTexture[] resource, string name, Vector2Int size, GraphicsFormat format)
{
for (int i = 0; i < 2; ++i)
{
resource[i] = new RenderTexture(size.x, size.y, 0, format) { name = name + (i + 1), enableRandomWrite = true };
resource[i].Create();
}
}
public void Destroy()
{
DestroyTcrAutogenResources();
@ -210,36 +201,5 @@ namespace FidelityFX.FSR3
DestroyResource(ref AutoComposition);
DestroyResource(ref AutoReactive);
}
private static void DestroyResource(ref Texture2D resource)
{
if (resource == null)
return;
#if UNITY_EDITOR
if (Application.isPlaying && !UnityEditor.EditorApplication.isPaused)
UnityEngine.Object.Destroy(resource);
else
UnityEngine.Object.DestroyImmediate(resource);
#else
UnityEngine.Object.Destroy(resource);
#endif
resource = null;
}
private static void DestroyResource(ref RenderTexture resource)
{
if (resource == null)
return;
resource.Release();
resource = null;
}
private static void DestroyResource(RenderTexture[] resource)
{
for (int i = 0; i < resource.Length; ++i)
DestroyResource(ref resource[i]);
}
}
}
Loading…
Cancel
Save