From 2ecc2d54b90c583e69f36d6d60a2e4548111d5b5 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Wed, 6 Nov 2024 14:26:42 +0100 Subject: [PATCH] - Use non-jittered camera projection matrix - Double checked jitter offset values - Use prepared (i.e. copied) motion vector texture --- .../Runtime/Effects/Upscaling/PSSRUpscaler.cs | 14 +++++++------- .../Shaders/Builtins/PrepareInputs.compute | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/PSSRUpscaler.cs b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/PSSRUpscaler.cs index 276b28b..d54a765 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/PSSRUpscaler.cs +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/PSSRUpscaler.cs @@ -14,6 +14,7 @@ namespace UnityEngine.Rendering.PostProcessing private RenderTexture _inputColor; private RenderTexture _inputDepth; + private RenderTexture _inputMotionVectors; private RenderTexture _outputColor; private Texture2D _outputIntermediate; @@ -39,6 +40,7 @@ namespace UnityEngine.Rendering.PostProcessing CreateRenderTexture(ref _inputColor, "PSSR Input Color", config.MaxRenderSize, context.sourceFormat, true); CreateRenderTexture(ref _inputDepth, "PSSR Input Depth", config.MaxRenderSize, GraphicsFormat.R32_SFloat, true); + CreateRenderTexture(ref _inputMotionVectors, "PSSR Input Motion Vectors", config.MaxRenderSize, GraphicsFormat.R16G16_SFloat, true); CreateRenderTexture(ref _outputColor, "PSSR Output Color", config.UpscaleSize, RenderTextureFormat.RGB111110Float); if (PSSRPlugin.CreatePssrContext(ref initParams, out IntPtr outputColorTexturePtr) >= 0 && outputColorTexturePtr != IntPtr.Zero) @@ -83,8 +85,6 @@ namespace UnityEngine.Rendering.PostProcessing } } - private static readonly int CameraMotionVectorsTexture = Shader.PropertyToID("_CameraMotionVectorsTexture"); - public override void Render(PostProcessRenderContext context, Upscaling config) { var cmd = context.command; @@ -97,11 +97,11 @@ namespace UnityEngine.Rendering.PostProcessing cmd.BeginSample("PSSR"); // TODO: if PSSR needs a copy of the previous depth and motion vectors anyway, why not just double-buffer those resources ourselves and make good use of these otherwise dumb copies? - PrepareInputs(cmd, context, config, _inputColor, _inputDepth); + PrepareInputs(cmd, context, config, _inputColor, _inputDepth, _inputMotionVectors); _dispatchParams.color = ToNativePtr(_inputColor); _dispatchParams.depth = ToNativePtr(_inputDepth); - _dispatchParams.motionVectors = ToNativePtr(Shader.GetGlobalTexture(CameraMotionVectorsTexture)); + _dispatchParams.motionVectors = ToNativePtr(_inputMotionVectors); _dispatchParams.exposure = ToNativePtr(config.exposureSource switch { Upscaling.ExposureSource.Manual when config.exposure != null => config.exposure, @@ -114,7 +114,7 @@ namespace UnityEngine.Rendering.PostProcessing var scaledRenderSize = config.GetScaledRenderSize(context.camera); _dispatchParams.renderWidth = (uint)scaledRenderSize.x; _dispatchParams.renderHeight = (uint)scaledRenderSize.y; - _dispatchParams.jitter = config.JitterOffset; // TODO: may need to scale this (should be jitter in render pixels apparently) + _dispatchParams.jitter = config.JitterOffset; _dispatchParams.motionVectorScale = new Vector2(-scaledRenderSize.x, -scaledRenderSize.y); _dispatchParams.FromCamera(context.camera); _dispatchParams.preExposure = config.preExposure; @@ -178,7 +178,7 @@ namespace UnityEngine.Rendering.PostProcessing public Vector2 jitter; public Vector2 motionVectorScale; - public Matrix4x4 camProjection; + public Matrix4x4 camProjectionNoJitter; public Vector3 camForward; public Vector3 camUp; public Vector3 camRight; @@ -194,7 +194,7 @@ namespace UnityEngine.Rendering.PostProcessing public void FromCamera(Camera cam) { - camProjection = cam.projectionMatrix; + camProjectionNoJitter = cam.nonJitteredProjectionMatrix; camForward = cam.transform.forward; camUp = cam.transform.up; camRight = cam.transform.right; diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Shaders/Builtins/PrepareInputs.compute b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Shaders/Builtins/PrepareInputs.compute index a04b0fb..bfed7e0 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Shaders/Builtins/PrepareInputs.compute +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Shaders/Builtins/PrepareInputs.compute @@ -2,11 +2,11 @@ Texture2D InColor: register(t0); Texture2D InDepth: register(t1); -//Texture2D InMotionVectors: register(t2); +Texture2D InMotionVectors: register(t2); RWTexture2D OutColor: register(u0); RWTexture2D OutDepth: register(u1); -//RWTexture2D OutMotionVectors: register(u2); +RWTexture2D OutMotionVectors: register(u2); cbuffer Params: register(b0) { @@ -23,5 +23,5 @@ void CS(uint2 GroupId: SV_GroupID, uint2 GroupThreadId: SV_GroupThreadID) if (prepareColor) OutColor[InputPos] = InColor[InputPos]; if (prepareDepth) OutDepth[InputPos] = InDepth[InputPos]; - //if (prepareMotionVectors) OutMotionVectors[InputPos] = InMotionVectors[InputPos]; + if (prepareMotionVectors) OutMotionVectors[InputPos] = InMotionVectors[InputPos]; }