From 3e31950d56ea6328936d1f9c8ceaaf318cf52e68 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Mon, 13 Mar 2023 17:45:09 +0100 Subject: [PATCH] Changed Fsr2Callbacks into an interface + base implementation --- Assets/Scripts/Internal/Fsr2.cs | 4 +-- Assets/Scripts/Internal/Fsr2Callbacks.cs | 41 ++++++++++++++++++------ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/Assets/Scripts/Internal/Fsr2.cs b/Assets/Scripts/Internal/Fsr2.cs index 0a935b5..92f7565 100644 --- a/Assets/Scripts/Internal/Fsr2.cs +++ b/Assets/Scripts/Internal/Fsr2.cs @@ -11,7 +11,7 @@ namespace FidelityFX public static class Fsr2 { // Allow overriding of certain Unity resource management behaviors by the programmer - public static Fsr2Callbacks GlobalCallbacks { get; set; } = new Fsr2Callbacks(); + public static IFsr2Callbacks GlobalCallbacks { get; set; } = new Fsr2CallbacksBase(); /// /// Creates a new FSR2 context with standard parameters that are appropriate for the current platform. @@ -135,7 +135,7 @@ namespace FidelityFX public InitializationFlags Flags; public Vector2Int MaxRenderSize; public Vector2Int DisplaySize; - public Fsr2Callbacks Callbacks; + public IFsr2Callbacks Callbacks; } /// diff --git a/Assets/Scripts/Internal/Fsr2Callbacks.cs b/Assets/Scripts/Internal/Fsr2Callbacks.cs index 5fd5a3a..a2bc44f 100644 --- a/Assets/Scripts/Internal/Fsr2Callbacks.cs +++ b/Assets/Scripts/Internal/Fsr2Callbacks.cs @@ -6,13 +6,42 @@ namespace FidelityFX /// A collection of callbacks required by the FSR2 process. /// This allows some customization by the game dev on how to integrate FSR2 into their own game setup. /// - public class Fsr2Callbacks + public interface IFsr2Callbacks { + Shader LoadShader(string name); + void UnloadShader(Shader shader); + ComputeShader LoadComputeShader(string name); + void UnloadComputeShader(ComputeShader shader); + + /// + /// Apply a mipmap bias to in-game textures to prevent them from becoming blurry as the internal rendering resolution lowers. + /// This will need to be customized on a per-game basis, as there is no clear universal way to determine what are "in-game" textures. + /// The default implementation will simply apply a mipmap bias to all 2D textures, which will include things like UI textures and which might miss things like terrain texture arrays. + /// + /// Depending on how your game organizes its assets, you will want to create a filter that more specifically selects the textures that need to have this mipmap bias applied. + /// You may also want to store the bias offset value and apply it to any assets that are loaded in on demand. + /// + void ApplyMipmapBias(float biasOffset); + } + + /// + /// Default implementation of IFsr2Callbacks using simple Resources calls. + /// These are fine for testing but a proper game will want to extend and override these methods. + /// + public class Fsr2CallbacksBase: IFsr2Callbacks + { + protected float CurrentBiasOffset = 0; + public virtual Shader LoadShader(string name) { return Resources.Load(name); } + public virtual void UnloadShader(Shader shader) + { + Resources.UnloadAsset(shader); + } + public virtual ComputeShader LoadComputeShader(string name) { return Resources.Load(name); @@ -23,16 +52,10 @@ namespace FidelityFX Resources.UnloadAsset(shader); } - /// - /// Apply a mipmap bias to in-game textures to prevent them from becoming blurry as the internal rendering resolution lowers. - /// This will need to be customized on a per-game basis, as there is no clear universal way to determine what are "in-game" textures. - /// The default implementation will simply apply a mipmap bias to all 2D textures, which will include things like UI textures and which might miss things like terrain texture arrays. - /// - /// Depending on how your game organizes its assets, you will want to create a filter that more specifically selects the textures that need to have this mipmap bias applied. - /// You may also want to store the bias offset value and apply it to any assets that are loaded in on demand. - /// public virtual void ApplyMipmapBias(float biasOffset) { + CurrentBiasOffset += biasOffset; + foreach (var texture in Resources.FindObjectsOfTypeAll()) { texture.mipMapBias += biasOffset;