Browse Source

Made all the ASR shader keywords global and encapsulated all of the keyword management into a separate class, which will enable and disable keywords globally based on the initialization flags and dispatch parameters.

asr-console
Nico de Poel 11 months ago
parent
commit
d5bbadaa77
  1. 4
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrContext.cs
  2. 124
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrKeywords.cs
  3. 3
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrKeywords.cs.meta
  4. 77
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrPass.cs
  5. 8
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Shaders/ffxm_fsr2_compute_luminance_pyramid_pass.compute
  6. 8
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Shaders/ffxm_fsr2_lock_pass.compute

4
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrContext.cs

@ -65,6 +65,8 @@ namespace ArmASR
private readonly Asr.GenerateReactiveConstants[] _generateReactiveConstantsArray = { new Asr.GenerateReactiveConstants() };
private ref Asr.GenerateReactiveConstants GenReactiveConsts => ref _generateReactiveConstantsArray[0];
private AsrKeywords _keywords = new();
private bool _firstExecution;
private Vector2 _previousJitterOffset;
private int _resourceFrameIndex;
@ -142,6 +144,8 @@ namespace ArmASR
if (dispatchParams.UseTextureArrays)
commandBuffer.EnableShaderKeyword("UNITY_FFXM_TEXTURE2D_X_ARRAY");
_keywords.ApplyKeywords(commandBuffer, _contextDescription.Flags, dispatchParams);
if (_firstExecution)
{
commandBuffer.SetRenderTarget(_resources.LockStatus[0]);

124
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrKeywords.cs

@ -0,0 +1,124 @@
using UnityEngine;
using UnityEngine.Rendering;
namespace ArmASR
{
public class AsrKeywords
{
private static readonly string OptionHalfPrecision = "FFXM_HALF";
private static readonly string OptionHdrColorInput = "FFXM_FSR2_OPTION_HDR_COLOR_INPUT";
private static readonly string OptionLowResolutionMotionVectors = "FFXM_FSR2_OPTION_LOW_RESOLUTION_MOTION_VECTORS";
private static readonly string OptionJitteredMotionVectors = "FFXM_FSR2_OPTION_JITTERED_MOTION_VECTORS";
private static readonly string OptionInvertedDepth = "FFXM_FSR2_OPTION_INVERTED_DEPTH";
private static readonly string OptionReprojectUseLut = "FFXM_FSR2_OPTION_REPROJECT_USE_LANCZOS_TYPE";
private static readonly string OptionApplySharpening = "FFXM_FSR2_OPTION_APPLY_SHARPENING";
#if UNITY_2021_2_OR_NEWER
private readonly GlobalKeyword _halfPrecisionKeyword;
private readonly GlobalKeyword _hdrColorInputKeyword;
private readonly GlobalKeyword _lowResMotionVectorsKeyword;
private readonly GlobalKeyword _jitteredMotionVectorsKeyword;
private readonly GlobalKeyword _invertedDepthKeyword;
private readonly GlobalKeyword _reprojectUseLutKeyword;
private readonly GlobalKeyword _applySharpeningKeyword;
#endif
public AsrKeywords()
{
#if UNITY_2021_2_OR_NEWER
_halfPrecisionKeyword = new GlobalKeyword(OptionHalfPrecision);
_hdrColorInputKeyword = new GlobalKeyword(OptionHdrColorInput);
_lowResMotionVectorsKeyword = new GlobalKeyword(OptionLowResolutionMotionVectors);
_jitteredMotionVectorsKeyword = new GlobalKeyword(OptionJitteredMotionVectors);
_invertedDepthKeyword = new GlobalKeyword(OptionInvertedDepth);
_reprojectUseLutKeyword = new GlobalKeyword(OptionReprojectUseLut);
_applySharpeningKeyword = new GlobalKeyword(OptionApplySharpening);
#endif
}
public void ApplyKeywords(CommandBuffer commandBuffer, Asr.InitializationFlags initFlags, in Asr.DispatchDescription dispatchParams)
{
bool useLut = false;
#if UNITY_2022_1_OR_NEWER // This will also work in 2020.3.43+ and 2021.3.14+
if (SystemInfo.computeSubGroupSize == 64)
{
useLut = true;
}
#endif
// This matches the permutation rules from the CreatePipeline* functions
#if UNITY_2021_2_OR_NEWER
if ((initFlags & Asr.InitializationFlags.EnableFP16Usage) != 0)
commandBuffer.EnableKeyword(_halfPrecisionKeyword);
else
commandBuffer.DisableKeyword(_halfPrecisionKeyword);
if ((initFlags & Asr.InitializationFlags.EnableHighDynamicRange) != 0)
commandBuffer.EnableKeyword(_hdrColorInputKeyword);
else
commandBuffer.DisableKeyword(_hdrColorInputKeyword);
if ((initFlags & Asr.InitializationFlags.EnableDisplayResolutionMotionVectors) == 0)
commandBuffer.EnableKeyword(_lowResMotionVectorsKeyword);
else
commandBuffer.DisableKeyword(_lowResMotionVectorsKeyword);
if ((initFlags & Asr.InitializationFlags.EnableMotionVectorsJitterCancellation) != 0)
commandBuffer.EnableKeyword(_jitteredMotionVectorsKeyword);
else
commandBuffer.DisableKeyword(_jitteredMotionVectorsKeyword);
if ((initFlags & Asr.InitializationFlags.EnableDepthInverted) != 0)
commandBuffer.EnableKeyword(_invertedDepthKeyword);
else
commandBuffer.DisableKeyword(_invertedDepthKeyword);
if (useLut)
commandBuffer.EnableKeyword(_reprojectUseLutKeyword);
else
commandBuffer.DisableKeyword(_reprojectUseLutKeyword);
if (dispatchParams.EnableSharpening)
commandBuffer.EnableKeyword(_applySharpeningKeyword);
else
commandBuffer.DisableKeyword(_applySharpeningKeyword);
#else
if ((initFlags & Asr.InitializationFlags.EnableFP16Usage) != 0)
commandBuffer.EnableShaderKeyword(OptionHalfPrecision);
else
commandBuffer.DisableShaderKeyword(OptionHalfPrecision);
if ((initFlags & Asr.InitializationFlags.EnableHighDynamicRange) != 0)
commandBuffer.EnableShaderKeyword(OptionHdrColorInput);
else
commandBuffer.DisableShaderKeyword(OptionHdrColorInput);
if ((initFlags & Asr.InitializationFlags.EnableDisplayResolutionMotionVectors) == 0)
commandBuffer.EnableShaderKeyword(OptionLowResolutionMotionVectors);
else
commandBuffer.DisableShaderKeyword(OptionLowResolutionMotionVectors);
if ((initFlags & Asr.InitializationFlags.EnableMotionVectorsJitterCancellation) != 0)
commandBuffer.EnableShaderKeyword(OptionJitteredMotionVectors);
else
commandBuffer.DisableShaderKeyword(OptionJitteredMotionVectors);
if ((initFlags & Asr.InitializationFlags.EnableDepthInverted) != 0)
commandBuffer.EnableShaderKeyword(OptionInvertedDepth);
else
commandBuffer.DisableShaderKeyword(OptionInvertedDepth);
if (useLut)
commandBuffer.EnableShaderKeyword(OptionReprojectUseLut);
else
commandBuffer.DisableShaderKeyword(OptionReprojectUseLut);
if (dispatchParams.EnableSharpening)
commandBuffer.EnableShaderKeyword(OptionApplySharpening);
else
commandBuffer.DisableShaderKeyword(OptionApplySharpening);
#endif
}
}
}

3
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrKeywords.cs.meta

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a2636bdd1878444fb3d3475610d379df
timeCreated: 1742641520

77
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrPass.cs

@ -46,7 +46,6 @@ namespace ArmASR
protected Material FragmentMaterial;
protected int FragmentPass;
protected MaterialPropertyBlock FragmentProperties;
protected readonly List<LocalKeyword> FragmentKeywords = new();
private CustomSampler _sampler;
@ -64,8 +63,6 @@ namespace ArmASR
Asr.DestroyObject(FragmentMaterial);
FragmentMaterial = null;
}
FragmentKeywords.Clear();
}
public void ScheduleDispatch(CommandBuffer commandBuffer, Asr.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY)
@ -78,11 +75,6 @@ namespace ArmASR
protected abstract void DoScheduleDispatch(CommandBuffer commandBuffer, Asr.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY);
protected void InitComputeShader(string passName, ComputeShader shader)
{
InitComputeShader(passName, shader, ContextDescription.Flags);
}
private void InitComputeShader(string passName, ComputeShader shader, Asr.InitializationFlags flags)
{
if (shader == null)
{
@ -92,19 +84,9 @@ namespace ArmASR
ComputeShader = shader;
KernelIndex = ComputeShader.FindKernel("main");
_sampler = CustomSampler.Create(passName);
foreach (string keyword in GetKeywords(flags))
{
ComputeShader.EnableKeyword(keyword);
}
}
protected void InitFragmentShader(string passName, Shader shader, int passNumber)
{
InitFragmentShader(passName, shader, passNumber, ContextDescription.Flags);
}
private void InitFragmentShader(string passName, Shader shader, int passNumber, Asr.InitializationFlags flags)
{
if (shader == null)
{
@ -113,42 +95,22 @@ namespace ArmASR
FragmentMaterial = new Material(shader);
FragmentPass = passNumber;
_sampler = CustomSampler.Create(passName);
FragmentProperties = new MaterialPropertyBlock();
foreach (string keyword in GetKeywords(flags))
{
// TODO: also want to include keywords that should be disabled (false)
// TODO: might be better to just determine all the keywords once up front and set them globally, probably don't even need to manage them here but in the AsrContext instead
// NOTE: be mindful of UNITY_2021_2_OR_NEWER (Local & GlobalKeyword were introduced there)
FragmentKeywords.Add(new LocalKeyword(shader, keyword));
}
_sampler = CustomSampler.Create(passName);
}
protected void BlitFragment(CommandBuffer commandBuffer)
protected void BlitFragment(CommandBuffer commandBuffer, params RenderTargetIdentifier[] renderTargets)
{
if (renderTargets == null || renderTargets.Length == 0)
commandBuffer.SetRenderTarget(BuiltinRenderTextureType.None);
else if (renderTargets.Length == 1)
commandBuffer.SetRenderTarget(renderTargets[0]);
else
commandBuffer.SetRenderTarget(renderTargets, BuiltinRenderTextureType.None);
commandBuffer.DrawProcedural(Matrix4x4.identity, FragmentMaterial, FragmentPass, MeshTopology.Triangles, 3, 1, FragmentProperties);
}
private static IEnumerable<string> GetKeywords(Asr.InitializationFlags flags)
{
bool useLut = false;
#if UNITY_2022_1_OR_NEWER // This will also work in 2020.3.43+ and 2021.3.14+
if (SystemInfo.computeSubGroupSize == 64)
{
useLut = true;
}
#endif
// This matches the permutation rules from the CreatePipeline* functions
if ((flags & Asr.InitializationFlags.EnableHighDynamicRange) != 0) yield return "FFXM_FSR2_OPTION_HDR_COLOR_INPUT";
if ((flags & Asr.InitializationFlags.EnableDisplayResolutionMotionVectors) == 0) yield return "FFXM_FSR2_OPTION_LOW_RESOLUTION_MOTION_VECTORS";
if ((flags & Asr.InitializationFlags.EnableMotionVectorsJitterCancellation) != 0) yield return "FFXM_FSR2_OPTION_JITTERED_MOTION_VECTORS";
if ((flags & Asr.InitializationFlags.EnableDepthInverted) != 0) yield return "FFXM_FSR2_OPTION_INVERTED_DEPTH";
if (useLut) yield return "FFXM_FSR2_OPTION_REPROJECT_USE_LANCZOS_TYPE";
if ((flags & Asr.InitializationFlags.EnableFP16Usage) != 0) yield return "FFXM_HALF";
}
[Conditional("ENABLE_PROFILER")]
protected void BeginSample(CommandBuffer cmd)
{
@ -259,35 +221,14 @@ namespace ArmASR
internal class AsrAccumulatePass : AsrPass
{
private const string SharpeningKeyword = "FFXM_FSR2_OPTION_APPLY_SHARPENING";
#if UNITY_2021_2_OR_NEWER
private readonly LocalKeyword _sharpeningKeyword;
#endif
public AsrAccumulatePass(Asr.ContextDescription contextDescription, AsrResources resources, ComputeBuffer constants)
: base(contextDescription, resources, constants)
{
InitFragmentShader("Reproject & Accumulate", contextDescription.Shaders.fragmentShader, 3);
#if UNITY_2021_2_OR_NEWER
_sharpeningKeyword = new LocalKeyword(ComputeShader, SharpeningKeyword); // TODO: dynamically enable this on MaterialPropertyBlock
#endif
}
protected override void DoScheduleDispatch(CommandBuffer commandBuffer, Asr.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY)
{
#if UNITY_2021_2_OR_NEWER
if (dispatchParams.EnableSharpening)
commandBuffer.EnableKeyword(ComputeShader, _sharpeningKeyword);
else
commandBuffer.DisableKeyword(ComputeShader, _sharpeningKeyword);
#else
if (dispatchParams.EnableSharpening)
commandBuffer.EnableShaderKeyword(SharpeningKeyword);
else
commandBuffer.DisableShaderKeyword(SharpeningKeyword);
#endif
if ((ContextDescription.Flags & Asr.InitializationFlags.EnableDisplayResolutionMotionVectors) == 0)
{
commandBuffer.SetGlobalTexture(AsrShaderIDs.SrvDilatedMotionVectors, Resources.DilatedMotionVectors[frameIndex]);

8
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Shaders/ffxm_fsr2_compute_luminance_pyramid_pass.compute

@ -1,9 +1,9 @@
#pragma kernel main
#pragma multi_compile_local __ FFXM_HALF
#pragma multi_compile_local __ FFXM_FSR2_OPTION_LOW_RESOLUTION_MOTION_VECTORS
#pragma multi_compile_local __ FFXM_FSR2_OPTION_JITTERED_MOTION_VECTORS
#pragma multi_compile_local __ FFXM_FSR2_OPTION_INVERTED_DEPTH
#pragma multi_compile __ FFXM_HALF
#pragma multi_compile __ FFXM_FSR2_OPTION_LOW_RESOLUTION_MOTION_VECTORS
#pragma multi_compile __ FFXM_FSR2_OPTION_JITTERED_MOTION_VECTORS
#pragma multi_compile __ FFXM_FSR2_OPTION_INVERTED_DEPTH
#pragma multi_compile __ UNITY_FFXM_TEXTURE2D_X_ARRAY

8
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Shaders/ffxm_fsr2_lock_pass.compute

@ -1,9 +1,9 @@
#pragma kernel main
#pragma multi_compile_local __ FFXM_HALF
#pragma multi_compile_local __ FFXM_FSR2_OPTION_LOW_RESOLUTION_MOTION_VECTORS
#pragma multi_compile_local __ FFXM_FSR2_OPTION_JITTERED_MOTION_VECTORS
#pragma multi_compile_local __ FFXM_FSR2_OPTION_INVERTED_DEPTH
#pragma multi_compile __ FFXM_HALF
#pragma multi_compile __ FFXM_FSR2_OPTION_LOW_RESOLUTION_MOTION_VECTORS
#pragma multi_compile __ FFXM_FSR2_OPTION_JITTERED_MOTION_VECTORS
#pragma multi_compile __ FFXM_FSR2_OPTION_INVERTED_DEPTH
#pragma multi_compile __ UNITY_FFXM_TEXTURE2D_X_ARRAY

Loading…
Cancel
Save