Browse Source

Made SGSR2 upscaler plugin abstract and split off 3-pass CS version into its own separate class

sgsr2_fs
Nico de Poel 1 year ago
parent
commit
d4c1d3f56e
  1. 5
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling.cs
  2. 113
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler.cs
  3. 104
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler_3PassCS.cs
  4. 3
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler_3PassCS.cs.meta

5
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling.cs

@ -16,7 +16,8 @@ namespace UnityEngine.Rendering.PostProcessing
[InspectorName("FidelityFX Super Resolution 2.2 (FSR2)")] FSR2, [InspectorName("FidelityFX Super Resolution 2.2 (FSR2)")] FSR2,
[InspectorName("FidelityFX Super Resolution 3.1 (FSR3)")] FSR3, [InspectorName("FidelityFX Super Resolution 3.1 (FSR3)")] FSR3,
//[InspectorName("Arm Accuracy Super Resolution (ASR)")] ASR, //[InspectorName("Arm Accuracy Super Resolution (ASR)")] ASR,
[InspectorName("Snapdragon Game Super Resolution 2 (SGSR2)")] SGSR2,
[InspectorName("Snapdragon Game Super Resolution 2 (SGSR2) 2-Pass Compute")] SGSR2_2PassCS,
[InspectorName("Snapdragon Game Super Resolution 2 (SGSR2) 3-Pass Compute")] SGSR2_3PassCS,
[InspectorName("PlayStation Spectral Super Resolution (PSSR)")] PSSR, [InspectorName("PlayStation Spectral Super Resolution (PSSR)")] PSSR,
} }
@ -180,7 +181,7 @@ namespace UnityEngine.Rendering.PostProcessing
{ {
UpscalerType.FSR2 when FSR2Upscaler.IsSupported => new FSR2Upscaler(), UpscalerType.FSR2 when FSR2Upscaler.IsSupported => new FSR2Upscaler(),
UpscalerType.FSR3 when FSR3Upscaler.IsSupported => new FSR3Upscaler(), UpscalerType.FSR3 when FSR3Upscaler.IsSupported => new FSR3Upscaler(),
UpscalerType.SGSR2 when SGSR2Upscaler.IsSupported => new SGSR2Upscaler(),
UpscalerType.SGSR2_3PassCS when SGSR2Upscaler.IsSupported => new SGSR2Upscaler_3PassCS(),
_ => new FSR2Upscaler(), // Fallback for when the selected upscaler is not supported on the current hardware _ => new FSR2Upscaler(), // Fallback for when the selected upscaler is not supported on the current hardware
}; };

113
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler.cs

@ -4,19 +4,19 @@ using UnityEngine.Experimental.Rendering;
namespace UnityEngine.Rendering.PostProcessing namespace UnityEngine.Rendering.PostProcessing
{ {
internal class SGSR2Upscaler: Upscaler
internal abstract class SGSR2Upscaler: Upscaler
{ {
public static bool IsSupported => SystemInfo.supportsComputeShaders; public static bool IsSupported => SystemInfo.supportsComputeShaders;
private RenderTexture _colorLuma;
private RenderTexture _motionDepthAlpha;
private RenderTexture _motionDepthClipAlpha;
private readonly RenderTexture[] _lumaHistory = new RenderTexture[2];
private readonly RenderTexture[] _upscaleHistory = new RenderTexture[2];
protected RenderTexture _colorLuma;
protected RenderTexture _motionDepthAlpha;
protected RenderTexture _motionDepthClipAlpha;
protected readonly RenderTexture[] _lumaHistory = new RenderTexture[2];
protected readonly RenderTexture[] _upscaleHistory = new RenderTexture[2];
private readonly ConstantsBuffer<SGSR2.Params> _paramsBuffer = new();
protected readonly ConstantsBuffer<SGSR2.Params> _paramsBuffer = new();
private uint _frameCount = 0;
protected uint _frameCount = 0;
public override void CreateContext(PostProcessRenderContext context, Upscaling config) public override void CreateContext(PostProcessRenderContext context, Upscaling config)
{ {
@ -41,102 +41,5 @@ namespace UnityEngine.Rendering.PostProcessing
DestroyRenderTexture(ref _motionDepthAlpha); DestroyRenderTexture(ref _motionDepthAlpha);
DestroyRenderTexture(ref _colorLuma); DestroyRenderTexture(ref _colorLuma);
} }
public override void Render(PostProcessRenderContext context, Upscaling config)
{
var cmd = context.command;
cmd.BeginSample("SGSR2");
ref var parms = ref _paramsBuffer.Value;
parms.renderSize = config.GetScaledRenderSize(context.camera);
parms.displaySize = config.UpscaleSize;
parms.renderSizeRcp = new Vector2(1.0f / parms.renderSize.x, 1.0f / parms.renderSize.y);
parms.displaySizeRcp = new Vector2(1.0f / parms.displaySize.x, 1.0f / parms.displaySize.y);
parms.jitterOffset = config.JitterOffset;
parms.clipToPrevClip = Matrix4x4.identity; // TODO: clipToPrevClip => (previous_view_proj * inv_vp)
parms.preExposure = config.preExposure;
parms.cameraFovAngleHor = Mathf.Tan(context.camera.fieldOfView * Mathf.Deg2Rad * 0.5f) * (float)parms.renderSize.x / parms.renderSize.y;
parms.cameraNear = context.camera.nearClipPlane;
parms.minLerpContribution = 0f;
parms.bSameCamera = 0u;
parms.reset = config.Reset ? 1u : 0u;
_paramsBuffer.UpdateBufferData(cmd);
if (_frameCount == 0 || config.Reset)
{
cmd.SetRenderTarget(_lumaHistory[0]);
cmd.ClearRenderTarget(false, true, Color.clear);
cmd.SetRenderTarget(_lumaHistory[1]);
cmd.ClearRenderTarget(false, true, Color.clear);
cmd.SetRenderTarget(_upscaleHistory[0]);
cmd.ClearRenderTarget(false, true, Color.clear);
cmd.SetRenderTarget(_upscaleHistory[1]);
cmd.ClearRenderTarget(false, true, Color.clear);
}
Convert(cmd, context, config);
Activate(cmd, context);
Upscale(cmd, context);
cmd.EndSample("SGSR2");
_frameCount++;
}
private void Convert(CommandBuffer cmd, PostProcessRenderContext context, Upscaling config)
{
var shader = context.resources.computeShaders.sgsr2Upscaler.threePassCompute.convert;
int kernelIndex = shader.FindKernel("CS");
cmd.SetComputeConstantBufferParam(shader, "Params", _paramsBuffer, 0, Marshal.SizeOf<SGSR2.Params>());
cmd.SetComputeTextureParam(shader, kernelIndex, "InputOpaqueColor", config.ColorOpaqueOnly);
cmd.SetComputeTextureParam(shader, kernelIndex, "InputColor", context.source);
cmd.SetComputeTextureParam(shader, kernelIndex, "InputDepth", BuiltinRenderTextureType.CameraTarget, 0, RenderTextureSubElement.Depth);
cmd.SetComputeTextureParam(shader, kernelIndex, "InputVelocity", BuiltinRenderTextureType.MotionVectors);
cmd.SetComputeTextureParam(shader, kernelIndex, "MotionDepthAlphaBuffer", _motionDepthAlpha);
cmd.SetComputeTextureParam(shader, kernelIndex, "YCoCgColor", _colorLuma);
const int threadGroupWorkRegionDim = 8;
int dispatchSrcX = (_paramsBuffer.Value.renderSize.x + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim;
int dispatchSrcY = (_paramsBuffer.Value.renderSize.y + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim;
cmd.DispatchCompute(shader, kernelIndex, dispatchSrcX, dispatchSrcY, 1);
}
private void Activate(CommandBuffer cmd, PostProcessRenderContext context)
{
var shader = context.resources.computeShaders.sgsr2Upscaler.threePassCompute.activate;
int kernelIndex = shader.FindKernel("CS");
uint frameIndex = _frameCount % 2;
cmd.SetComputeConstantBufferParam(shader, "Params", _paramsBuffer, 0, Marshal.SizeOf<SGSR2.Params>());
cmd.SetComputeTextureParam(shader, kernelIndex, "PrevLumaHistory", _lumaHistory[frameIndex ^ 1]);
cmd.SetComputeTextureParam(shader, kernelIndex, "MotionDepthAlphaBuffer", _motionDepthAlpha);
cmd.SetComputeTextureParam(shader, kernelIndex, "YCoCgColor", _colorLuma);
cmd.SetComputeTextureParam(shader, kernelIndex, "MotionDepthClipAlphaBuffer", _motionDepthClipAlpha);
cmd.SetComputeTextureParam(shader, kernelIndex, "LumaHistory", _lumaHistory[frameIndex]);
const int threadGroupWorkRegionDim = 8;
int dispatchSrcX = (_paramsBuffer.Value.renderSize.x + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim;
int dispatchSrcY = (_paramsBuffer.Value.renderSize.y + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim;
cmd.DispatchCompute(shader, kernelIndex, dispatchSrcX, dispatchSrcY, 1);
}
private void Upscale(CommandBuffer cmd, PostProcessRenderContext context)
{
var shader = context.resources.computeShaders.sgsr2Upscaler.threePassCompute.upscale;
int kernelIndex = shader.FindKernel("CS");
uint frameIndex = _frameCount % 2;
cmd.SetComputeConstantBufferParam(shader, "Params", _paramsBuffer, 0, Marshal.SizeOf<SGSR2.Params>());
cmd.SetComputeTextureParam(shader, kernelIndex, "PrevHistoryOutput", _upscaleHistory[frameIndex ^ 1]);
cmd.SetComputeTextureParam(shader, kernelIndex, "MotionDepthClipAlphaBuffer", _motionDepthClipAlpha);
cmd.SetComputeTextureParam(shader, kernelIndex, "YCoCgColor", _colorLuma);
cmd.SetComputeTextureParam(shader, kernelIndex, "SceneColorOutput", context.destination);
cmd.SetComputeTextureParam(shader, kernelIndex, "HistoryOutput", _upscaleHistory[frameIndex]);
const int threadGroupWorkRegionDim = 8;
int dispatchDstX = (_paramsBuffer.Value.displaySize.x + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim;
int dispatchDstY = (_paramsBuffer.Value.displaySize.y + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim;
cmd.DispatchCompute(shader, kernelIndex, dispatchDstX, dispatchDstY, 1);
}
} }
} }

104
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler_3PassCS.cs

@ -0,0 +1,104 @@
using System.Runtime.InteropServices;
namespace UnityEngine.Rendering.PostProcessing
{
internal class SGSR2Upscaler_3PassCS: SGSR2Upscaler
{
public override void Render(PostProcessRenderContext context, Upscaling config)
{
var cmd = context.command;
cmd.BeginSample("SGSR2 3-Pass CS");
ref var parms = ref _paramsBuffer.Value;
parms.renderSize = config.GetScaledRenderSize(context.camera);
parms.displaySize = config.UpscaleSize;
parms.renderSizeRcp = new Vector2(1.0f / parms.renderSize.x, 1.0f / parms.renderSize.y);
parms.displaySizeRcp = new Vector2(1.0f / parms.displaySize.x, 1.0f / parms.displaySize.y);
parms.jitterOffset = config.JitterOffset;
parms.clipToPrevClip = Matrix4x4.identity; // TODO: clipToPrevClip => (previous_view_proj * inv_vp)
parms.preExposure = config.preExposure;
parms.cameraFovAngleHor = Mathf.Tan(context.camera.fieldOfView * Mathf.Deg2Rad * 0.5f) * (float)parms.renderSize.x / parms.renderSize.y;
parms.cameraNear = context.camera.nearClipPlane;
parms.minLerpContribution = 0f;
parms.bSameCamera = 0u;
parms.reset = config.Reset ? 1u : 0u;
_paramsBuffer.UpdateBufferData(cmd);
if (_frameCount == 0 || config.Reset)
{
cmd.SetRenderTarget(_lumaHistory[0]);
cmd.ClearRenderTarget(false, true, Color.clear);
cmd.SetRenderTarget(_lumaHistory[1]);
cmd.ClearRenderTarget(false, true, Color.clear);
cmd.SetRenderTarget(_upscaleHistory[0]);
cmd.ClearRenderTarget(false, true, Color.clear);
cmd.SetRenderTarget(_upscaleHistory[1]);
cmd.ClearRenderTarget(false, true, Color.clear);
}
Convert(cmd, context, config);
Activate(cmd, context);
Upscale(cmd, context);
cmd.EndSample("SGSR2 3-Pass CS");
_frameCount++;
}
private void Convert(CommandBuffer cmd, PostProcessRenderContext context, Upscaling config)
{
var shader = context.resources.computeShaders.sgsr2Upscaler.threePassCompute.convert;
int kernelIndex = shader.FindKernel("CS");
cmd.SetComputeConstantBufferParam(shader, "Params", _paramsBuffer, 0, Marshal.SizeOf<SGSR2.Params>());
cmd.SetComputeTextureParam(shader, kernelIndex, "InputOpaqueColor", config.ColorOpaqueOnly);
cmd.SetComputeTextureParam(shader, kernelIndex, "InputColor", context.source);
cmd.SetComputeTextureParam(shader, kernelIndex, "InputDepth", BuiltinRenderTextureType.CameraTarget, 0, RenderTextureSubElement.Depth);
cmd.SetComputeTextureParam(shader, kernelIndex, "InputVelocity", BuiltinRenderTextureType.MotionVectors);
cmd.SetComputeTextureParam(shader, kernelIndex, "MotionDepthAlphaBuffer", _motionDepthAlpha);
cmd.SetComputeTextureParam(shader, kernelIndex, "YCoCgColor", _colorLuma);
const int threadGroupWorkRegionDim = 8;
int dispatchSrcX = (_paramsBuffer.Value.renderSize.x + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim;
int dispatchSrcY = (_paramsBuffer.Value.renderSize.y + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim;
cmd.DispatchCompute(shader, kernelIndex, dispatchSrcX, dispatchSrcY, 1);
}
private void Activate(CommandBuffer cmd, PostProcessRenderContext context)
{
var shader = context.resources.computeShaders.sgsr2Upscaler.threePassCompute.activate;
int kernelIndex = shader.FindKernel("CS");
uint frameIndex = _frameCount % 2;
cmd.SetComputeConstantBufferParam(shader, "Params", _paramsBuffer, 0, Marshal.SizeOf<SGSR2.Params>());
cmd.SetComputeTextureParam(shader, kernelIndex, "PrevLumaHistory", _lumaHistory[frameIndex ^ 1]);
cmd.SetComputeTextureParam(shader, kernelIndex, "MotionDepthAlphaBuffer", _motionDepthAlpha);
cmd.SetComputeTextureParam(shader, kernelIndex, "YCoCgColor", _colorLuma);
cmd.SetComputeTextureParam(shader, kernelIndex, "MotionDepthClipAlphaBuffer", _motionDepthClipAlpha);
cmd.SetComputeTextureParam(shader, kernelIndex, "LumaHistory", _lumaHistory[frameIndex]);
const int threadGroupWorkRegionDim = 8;
int dispatchSrcX = (_paramsBuffer.Value.renderSize.x + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim;
int dispatchSrcY = (_paramsBuffer.Value.renderSize.y + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim;
cmd.DispatchCompute(shader, kernelIndex, dispatchSrcX, dispatchSrcY, 1);
}
private void Upscale(CommandBuffer cmd, PostProcessRenderContext context)
{
var shader = context.resources.computeShaders.sgsr2Upscaler.threePassCompute.upscale;
int kernelIndex = shader.FindKernel("CS");
uint frameIndex = _frameCount % 2;
cmd.SetComputeConstantBufferParam(shader, "Params", _paramsBuffer, 0, Marshal.SizeOf<SGSR2.Params>());
cmd.SetComputeTextureParam(shader, kernelIndex, "PrevHistoryOutput", _upscaleHistory[frameIndex ^ 1]);
cmd.SetComputeTextureParam(shader, kernelIndex, "MotionDepthClipAlphaBuffer", _motionDepthClipAlpha);
cmd.SetComputeTextureParam(shader, kernelIndex, "YCoCgColor", _colorLuma);
cmd.SetComputeTextureParam(shader, kernelIndex, "SceneColorOutput", context.destination);
cmd.SetComputeTextureParam(shader, kernelIndex, "HistoryOutput", _upscaleHistory[frameIndex]);
const int threadGroupWorkRegionDim = 8;
int dispatchDstX = (_paramsBuffer.Value.displaySize.x + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim;
int dispatchDstY = (_paramsBuffer.Value.displaySize.y + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim;
cmd.DispatchCompute(shader, kernelIndex, dispatchDstX, dispatchDstY, 1);
}
}
}

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

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 78943b31437146f29ec0a7d8d67eb5cc
timeCreated: 1734733770
Loading…
Cancel
Save