Browse Source

Reshufled some code so that if the initialization process fails for some reason, we don't leave the scene behind in a messed up state.

mac-autoexp
Nico de Poel 3 years ago
parent
commit
1c030377c0
  1. 38
      Assets/Scripts/Fsr2ImageEffect.cs

38
Assets/Scripts/Fsr2ImageEffect.cs

@ -69,6 +69,22 @@ namespace FidelityFX
private void OnEnable() private void OnEnable()
{ {
// Set up the original camera to output all of the required FSR2 input resources at the desired resolution
_renderCamera = GetComponent<Camera>();
_originalRenderTarget = _renderCamera.targetTexture;
_originalDepthTextureMode = _renderCamera.depthTextureMode;
_renderCamera.targetTexture = null; // Clear the camera's target texture so we can fully control how the output gets written
_renderCamera.depthTextureMode = _originalDepthTextureMode | DepthTextureMode.Depth | DepthTextureMode.MotionVectors;
// Determine the desired rendering and display resolutions
_displaySize = new Vector2Int(_renderCamera.pixelWidth, _renderCamera.pixelHeight);
Fsr2.GetRenderResolutionFromQualityMode(out var renderWidth, out var renderHeight, _displaySize.x, _displaySize.y, qualityMode);
_renderSize = new Vector2Int(renderWidth, renderHeight);
// Apply a mipmap bias so that textures retain their sharpness
float biasOffset = Fsr2.GetMipmapBiasOffset(_renderSize.x, _displaySize.x);
Fsr2.GlobalCallbacks.ApplyMipmapBias(biasOffset);
if (!SystemInfo.supportsComputeShaders) if (!SystemInfo.supportsComputeShaders)
{ {
Debug.LogError("FSR2 requires compute shader support!"); Debug.LogError("FSR2 requires compute shader support!");
@ -78,22 +94,12 @@ namespace FidelityFX
_helper = GetComponent<Fsr2ImageEffectHelper>(); _helper = GetComponent<Fsr2ImageEffectHelper>();
// Set up the original camera to output all of the required FSR2 input resources at the desired resolution
_renderCamera = GetComponent<Camera>();
_originalRenderTarget = _renderCamera.targetTexture;
_originalDepthTextureMode = _renderCamera.depthTextureMode;
_renderCamera.targetTexture = null; // Clear the camera's target texture so we can fully control how the output gets written
_renderCamera.depthTextureMode = _originalDepthTextureMode | DepthTextureMode.Depth | DepthTextureMode.MotionVectors;
// Initialize FSR2 context // Initialize FSR2 context
Fsr2.InitializationFlags flags = 0; Fsr2.InitializationFlags flags = 0;
if (_renderCamera.allowHDR) flags |= Fsr2.InitializationFlags.EnableHighDynamicRange; if (_renderCamera.allowHDR) flags |= Fsr2.InitializationFlags.EnableHighDynamicRange;
if (enableFP16) flags |= Fsr2.InitializationFlags.EnableFP16Usage; if (enableFP16) flags |= Fsr2.InitializationFlags.EnableFP16Usage;
if (enableAutoExposure) flags |= Fsr2.InitializationFlags.EnableAutoExposure; if (enableAutoExposure) flags |= Fsr2.InitializationFlags.EnableAutoExposure;
_displaySize = new Vector2Int(_renderCamera.pixelWidth, _renderCamera.pixelHeight);
Fsr2.GetRenderResolutionFromQualityMode(out var renderWidth, out var renderHeight, _displaySize.x, _displaySize.y, qualityMode);
_renderSize = new Vector2Int(renderWidth, renderHeight);
_context = Fsr2.CreateContext(_displaySize, _renderSize, flags); _context = Fsr2.CreateContext(_displaySize, _renderSize, flags);
_dispatchCommandBuffer = new CommandBuffer { name = "FSR2 Dispatch" }; _dispatchCommandBuffer = new CommandBuffer { name = "FSR2 Dispatch" };
@ -108,10 +114,6 @@ namespace FidelityFX
_renderCamera.AddCommandBuffer(CameraEvent.BeforeForwardAlpha, _opaqueInputCommandBuffer); _renderCamera.AddCommandBuffer(CameraEvent.BeforeForwardAlpha, _opaqueInputCommandBuffer);
} }
// Apply a mipmap bias so that textures retain their sharpness
float biasOffset = Fsr2.GetMipmapBiasOffset(_renderSize.x, _displaySize.x);
Fsr2.GlobalCallbacks.ApplyMipmapBias(biasOffset);
_prevDisplaySize = _displaySize; _prevDisplaySize = _displaySize;
_prevQualityMode = qualityMode; _prevQualityMode = qualityMode;
_prevGenReactiveMask = autoGenerateReactiveMask; _prevGenReactiveMask = autoGenerateReactiveMask;
@ -119,9 +121,14 @@ namespace FidelityFX
private void OnDisable() private void OnDisable()
{ {
// Undo the current mipmap bias offset
float biasOffset = Fsr2.GetMipmapBiasOffset(_renderSize.x, _prevDisplaySize.x); float biasOffset = Fsr2.GetMipmapBiasOffset(_renderSize.x, _prevDisplaySize.x);
Fsr2.GlobalCallbacks.ApplyMipmapBias(-biasOffset); Fsr2.GlobalCallbacks.ApplyMipmapBias(-biasOffset);
// Restore the camera's original state
_renderCamera.depthTextureMode = _originalDepthTextureMode;
_renderCamera.targetTexture = _originalRenderTarget;
if (_opaqueInputCommandBuffer != null) if (_opaqueInputCommandBuffer != null)
{ {
_renderCamera.RemoveCommandBuffer(CameraEvent.BeforeForwardAlpha, _opaqueInputCommandBuffer); _renderCamera.RemoveCommandBuffer(CameraEvent.BeforeForwardAlpha, _opaqueInputCommandBuffer);
@ -135,9 +142,6 @@ namespace FidelityFX
_dispatchCommandBuffer = null; _dispatchCommandBuffer = null;
} }
_renderCamera.depthTextureMode = _originalDepthTextureMode;
_renderCamera.targetTexture = _originalRenderTarget;
if (_context != null) if (_context != null)
{ {
_context.Destroy(); _context.Destroy();

Loading…
Cancel
Save