|
|
|
@ -69,6 +69,22 @@ namespace FidelityFX |
|
|
|
|
|
|
|
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) |
|
|
|
{ |
|
|
|
Debug.LogError("FSR2 requires compute shader support!"); |
|
|
|
@ -78,22 +94,12 @@ namespace FidelityFX |
|
|
|
|
|
|
|
_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
|
|
|
|
Fsr2.InitializationFlags flags = 0; |
|
|
|
if (_renderCamera.allowHDR) flags |= Fsr2.InitializationFlags.EnableHighDynamicRange; |
|
|
|
if (enableFP16) flags |= Fsr2.InitializationFlags.EnableFP16Usage; |
|
|
|
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); |
|
|
|
|
|
|
|
_dispatchCommandBuffer = new CommandBuffer { name = "FSR2 Dispatch" }; |
|
|
|
@ -108,10 +114,6 @@ namespace FidelityFX |
|
|
|
_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; |
|
|
|
_prevQualityMode = qualityMode; |
|
|
|
_prevGenReactiveMask = autoGenerateReactiveMask; |
|
|
|
@ -119,9 +121,14 @@ namespace FidelityFX |
|
|
|
|
|
|
|
private void OnDisable() |
|
|
|
{ |
|
|
|
// Undo the current mipmap bias offset
|
|
|
|
float biasOffset = Fsr2.GetMipmapBiasOffset(_renderSize.x, _prevDisplaySize.x); |
|
|
|
Fsr2.GlobalCallbacks.ApplyMipmapBias(-biasOffset); |
|
|
|
|
|
|
|
// Restore the camera's original state
|
|
|
|
_renderCamera.depthTextureMode = _originalDepthTextureMode; |
|
|
|
_renderCamera.targetTexture = _originalRenderTarget; |
|
|
|
|
|
|
|
if (_opaqueInputCommandBuffer != null) |
|
|
|
{ |
|
|
|
_renderCamera.RemoveCommandBuffer(CameraEvent.BeforeForwardAlpha, _opaqueInputCommandBuffer); |
|
|
|
@ -135,9 +142,6 @@ namespace FidelityFX |
|
|
|
_dispatchCommandBuffer = null; |
|
|
|
} |
|
|
|
|
|
|
|
_renderCamera.depthTextureMode = _originalDepthTextureMode; |
|
|
|
_renderCamera.targetTexture = _originalRenderTarget; |
|
|
|
|
|
|
|
if (_context != null) |
|
|
|
{ |
|
|
|
_context.Destroy(); |
|
|
|
|