Browse Source

Apply jitter offset to dispatch parameters

mac-autoexp
Nico de Poel 3 years ago
parent
commit
d607b1bf5a
  1. 2
      Assets/Scripts/Fsr2Context.cs
  2. 23
      Assets/Scripts/Fsr2Controller.cs
  3. 9
      Assets/Scripts/SubsampleTest.cs

2
Assets/Scripts/Fsr2Context.cs

@ -98,7 +98,7 @@ namespace FidelityFX
_defaultExposureResource.SetPixel(0, 0, Color.black); _defaultExposureResource.SetPixel(0, 0, Color.black);
_defaultExposureResource.Apply(); _defaultExposureResource.Apply();
// Resource FSR2_DefaultReactiviyMask: FFX_RESOURCE_USAGE_READ_ONLY, FFX_SURFACE_FORMAT_R8_UNORM, FFX_RESOURCE_FLAGS_NONE
// Resource FSR2_DefaultReactivityMask: FFX_RESOURCE_USAGE_READ_ONLY, FFX_SURFACE_FORMAT_R8_UNORM, FFX_RESOURCE_FLAGS_NONE
_defaultReactiveResource = new Texture2D(1, 1, GraphicsFormat.R8_UNorm, TextureCreationFlags.None) { name = "FSR2_DefaultReactivityMask" }; _defaultReactiveResource = new Texture2D(1, 1, GraphicsFormat.R8_UNorm, TextureCreationFlags.None) { name = "FSR2_DefaultReactivityMask" };
_defaultReactiveResource.SetPixel(0, 0, Color.black); _defaultReactiveResource.SetPixel(0, 0, Color.black);
_defaultReactiveResource.Apply(); _defaultReactiveResource.Apply();

23
Assets/Scripts/Fsr2Controller.cs

@ -110,17 +110,23 @@ public class Fsr2Controller : MonoBehaviour
RenderPipelineManager.endContextRendering -= OnEndContextRendering; RenderPipelineManager.endContextRendering -= OnEndContextRendering;
} }
public void UpdateInputResources(RenderTexture src)
public void OnRenderGameCamera(RenderTexture renderTarget, Vector2 jitterOffset)
{ {
// I hate having to allocate extra RTs just to duplicate already existing Unity render buffers, but AFAIK there is no way to directly address these buffers individually from code // I hate having to allocate extra RTs just to duplicate already existing Unity render buffers, but AFAIK there is no way to directly address these buffers individually from code
_colorRT = RenderTexture.GetTemporary(src.width, src.height, 0, RenderTextureFormat.ARGBHalf);
_depthRT = RenderTexture.GetTemporary(src.width, src.height, 0, RenderTextureFormat.RFloat);
_motionVectorsRT = RenderTexture.GetTemporary(src.width, src.height, 0, RenderTextureFormat.RGHalf);
_colorRT = RenderTexture.GetTemporary(renderTarget.width, renderTarget.height, 0, RenderTextureFormat.ARGBHalf);
_depthRT = RenderTexture.GetTemporary(renderTarget.width, renderTarget.height, 0, RenderTextureFormat.RFloat);
_motionVectorsRT = RenderTexture.GetTemporary(renderTarget.width, renderTarget.height, 0, RenderTextureFormat.RGHalf);
// TODO: might be able to combine color + depth into a single RT and separate them out using RenderTextureSubElement // TODO: might be able to combine color + depth into a single RT and separate them out using RenderTextureSubElement
Graphics.Blit(src, _colorRT);
Graphics.Blit(src, _depthRT, CopyDepthMaterial);
Graphics.Blit(src, _motionVectorsRT, CopyMotionVectorsMaterial);
// TODO: we can copy to all RTs at the same time using a multi-target blit, requiring only a single shader + material
Graphics.Blit(renderTarget, _colorRT);
Graphics.Blit(renderTarget, _depthRT, CopyDepthMaterial);
Graphics.Blit(renderTarget, _motionVectorsRT, CopyMotionVectorsMaterial);
_dispatchDescription.Color = _colorRT;
_dispatchDescription.Depth = _depthRT;
_dispatchDescription.MotionVectors = _motionVectorsRT;
_dispatchDescription.JitterOffset = jitterOffset;
} }
// For scriptable rendering pipeline // For scriptable rendering pipeline
@ -136,9 +142,6 @@ public class Fsr2Controller : MonoBehaviour
if (_colorRT == null || _depthRT == null || _motionVectorsRT == null) if (_colorRT == null || _depthRT == null || _motionVectorsRT == null)
return; return;
_dispatchDescription.Color = _colorRT;
_dispatchDescription.Depth = _depthRT;
_dispatchDescription.MotionVectors = _motionVectorsRT;
_dispatchDescription.Output = _outputRT; _dispatchDescription.Output = _outputRT;
_dispatchDescription.Exposure = null; _dispatchDescription.Exposure = null;
_dispatchDescription.Reactive = null; _dispatchDescription.Reactive = null;

9
Assets/Scripts/SubsampleTest.cs

@ -19,6 +19,7 @@ public class SubsampleTest : MonoBehaviour
private bool enableJitter; private bool enableJitter;
private Fsr2Controller _fsr2Controller; private Fsr2Controller _fsr2Controller;
private Vector2 _jitterOffset;
private void OnEnable() private void OnEnable()
{ {
@ -99,10 +100,10 @@ public class SubsampleTest : MonoBehaviour
// Perform custom jittering of the camera's projection matrix according to FSR2's instructions // Perform custom jittering of the camera's projection matrix according to FSR2's instructions
// Unity already does jittering behind the scenes for certain post-effects, so can we perhaps integrate with that? // Unity already does jittering behind the scenes for certain post-effects, so can we perhaps integrate with that?
int jitterPhaseCount = Fsr2.GetJitterPhaseCount(gameCamera.targetTexture.width, Screen.width); int jitterPhaseCount = Fsr2.GetJitterPhaseCount(gameCamera.targetTexture.width, Screen.width);
Fsr2.GetJitterOffset(out float jitterX, out float jitterY, Time.frameCount, jitterPhaseCount);
Fsr2.GetJitterOffset(out _jitterOffset.x, out _jitterOffset.y, Time.frameCount, jitterPhaseCount);
jitterX = 2.0f * jitterX / gameCamera.targetTexture.width;
jitterY = -2.0f * jitterY / gameCamera.targetTexture.height;
float jitterX = 2.0f * _jitterOffset.x / gameCamera.targetTexture.width;
float jitterY = -2.0f * _jitterOffset.y / gameCamera.targetTexture.height;
var jitterTranslationMatrix = Matrix4x4.Translate(new Vector3(jitterX, jitterY, 0)); var jitterTranslationMatrix = Matrix4x4.Translate(new Vector3(jitterX, jitterY, 0));
gameCamera.projectionMatrix = jitterTranslationMatrix * gameCamera.nonJitteredProjectionMatrix; gameCamera.projectionMatrix = jitterTranslationMatrix * gameCamera.nonJitteredProjectionMatrix;
@ -112,7 +113,7 @@ public class SubsampleTest : MonoBehaviour
private void OnRenderImage(RenderTexture src, RenderTexture dest) private void OnRenderImage(RenderTexture src, RenderTexture dest)
{ {
// We don't actually output any image from this camera; instead we pass its render buffers on to the FSR2 system // We don't actually output any image from this camera; instead we pass its render buffers on to the FSR2 system
_fsr2Controller.UpdateInputResources(src);
_fsr2Controller.OnRenderGameCamera(src, _jitterOffset);
// Shut up Unity warning about not writing anything to the destination texture // Shut up Unity warning about not writing anything to the destination texture
Graphics.SetRenderTarget(dest); Graphics.SetRenderTarget(dest);

Loading…
Cancel
Save