Browse Source

Respect output target texture's dimensions when determining render size and rescaling camera viewport.

mac-autoexp
Nico de Poel 3 years ago
parent
commit
644349b16b
  1. 27
      Assets/Scripts/Fsr2ImageEffect.cs

27
Assets/Scripts/Fsr2ImageEffect.cs

@ -103,7 +103,7 @@ namespace FidelityFX
_renderCamera.depthTextureMode = _originalDepthTextureMode | DepthTextureMode.Depth | DepthTextureMode.MotionVectors; _renderCamera.depthTextureMode = _originalDepthTextureMode | DepthTextureMode.Depth | DepthTextureMode.MotionVectors;
// Determine the desired rendering and display resolutions // Determine the desired rendering and display resolutions
_displaySize = new Vector2Int(_renderCamera.pixelWidth, _renderCamera.pixelHeight);
_displaySize = GetDisplaySize();
Fsr2.GetRenderResolutionFromQualityMode(out var renderWidth, out var renderHeight, _displaySize.x, _displaySize.y, qualityMode); Fsr2.GetRenderResolutionFromQualityMode(out var renderWidth, out var renderHeight, _displaySize.x, _displaySize.y, qualityMode);
_renderSize = new Vector2Int(renderWidth, renderHeight); _renderSize = new Vector2Int(renderWidth, renderHeight);
@ -185,7 +185,8 @@ namespace FidelityFX
private void Update() private void Update()
{ {
if (_renderCamera.pixelWidth != _prevDisplaySize.x || _renderCamera.pixelHeight != _prevDisplaySize.y || qualityMode != _prevQualityMode)
var displaySize = GetDisplaySize();
if (displaySize.x != _prevDisplaySize.x || displaySize.y != _prevDisplaySize.y || qualityMode != _prevQualityMode)
{ {
// Force all resources to be destroyed and recreated with the new settings // Force all resources to be destroyed and recreated with the new settings
OnDisable(); OnDisable();
@ -219,8 +220,8 @@ namespace FidelityFX
if (_helper == null || !_helper.enabled) if (_helper == null || !_helper.enabled)
{ {
// Render to a smaller portion of the screen by manipulating the camera's viewport rect // Render to a smaller portion of the screen by manipulating the camera's viewport rect
_renderCamera.aspect = (_renderCamera.pixelWidth * _originalRect.width) / (_renderCamera.pixelHeight * _originalRect.height);
_renderCamera.rect = new Rect(0, 0, _originalRect.width * _renderSize.x / _displaySize.x, _originalRect.height * _renderSize.y / _displaySize.y);
_renderCamera.aspect = (_displaySize.x * _originalRect.width) / (_displaySize.y * _originalRect.height);
_renderCamera.rect = new Rect(0, 0, _originalRect.width * _renderSize.x / _renderCamera.pixelWidth, _originalRect.height * _renderSize.y / _renderCamera.pixelHeight);
} }
// Set up the parameters to auto-generate a reactive mask // Set up the parameters to auto-generate a reactive mask
@ -322,13 +323,12 @@ namespace FidelityFX
_context.Dispatch(_dispatchDescription, _dispatchCommandBuffer); _context.Dispatch(_dispatchDescription, _dispatchCommandBuffer);
// Output the upscaled image // Output the upscaled image
if (_originalRenderTarget != null || outputColorDepth != null)
var outputTexture = GetOutputTexture();
if (outputTexture != null)
{ {
var targetTexture = outputColorDepth != null ? outputColorDepth : _originalRenderTarget;
// Output to the camera target texture, passing through depth and motion vectors // Output to the camera target texture, passing through depth and motion vectors
_dispatchCommandBuffer.SetGlobalTexture("_DepthTex", BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Depth); _dispatchCommandBuffer.SetGlobalTexture("_DepthTex", BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Depth);
_dispatchCommandBuffer.Blit(Fsr2ShaderIDs.UavUpscaledOutput, targetTexture, _copyWithDepthMaterial);
_dispatchCommandBuffer.Blit(Fsr2ShaderIDs.UavUpscaledOutput, outputTexture, _copyWithDepthMaterial);
if (outputMotionVectors != null) if (outputMotionVectors != null)
_dispatchCommandBuffer.Blit(BuiltinRenderTextureType.MotionVectors, outputMotionVectors); _dispatchCommandBuffer.Blit(BuiltinRenderTextureType.MotionVectors, outputMotionVectors);
} }
@ -350,5 +350,16 @@ namespace FidelityFX
// Shut up the Unity warning about not writing to the destination texture // Shut up the Unity warning about not writing to the destination texture
RenderTexture.active = dest; RenderTexture.active = dest;
} }
private RenderTexture GetOutputTexture() => outputColorDepth != null ? outputColorDepth : _originalRenderTarget;
private Vector2Int GetDisplaySize()
{
var outputTexture = GetOutputTexture();
if (outputTexture != null)
return new Vector2Int(outputTexture.width, outputTexture.height);
return new Vector2Int(_renderCamera.pixelWidth, _renderCamera.pixelHeight);
}
} }
} }
Loading…
Cancel
Save