diff --git a/Runtime/HdrpGraphicsApplier.cs b/Runtime/HdrpGraphicsApplier.cs index a67c7f6..67ab53a 100644 --- a/Runtime/HdrpGraphicsApplier.cs +++ b/Runtime/HdrpGraphicsApplier.cs @@ -43,6 +43,9 @@ private static void Register() DynamicResolutionHandler.SetDynamicResScaler(() => _stpPercentage, DynamicResScalePolicyType.ReturnsPercentage); _fsr2PerformanceCrashes = SystemInfo.graphicsDeviceVendorID == 0x10DE && SystemInfo.graphicsDeviceName.IndexOf("RTX", System.StringComparison.OrdinalIgnoreCase) < 0; + Fsr2Held.Clear(); + RenderPipelineManager.beginContextRendering -= HoldFsr2OnEmptyOutputs; + RenderPipelineManager.beginContextRendering += HoldFsr2OnEmptyOutputs; VpeGraphics.RenderScaleSource = RenderScale; VpeGraphics.BallReflectionsSupported = true; VpeGraphics.MotionBlurSupported = true; @@ -207,14 +210,18 @@ public void Apply(VpeGraphicsSettings settings) if (_upscalerQuality != quality) { Logger.Info("{Upscaler} doesn't run in {Quality} mode on the {Gpu:l}, {Instead} instead.", _upscaler, quality, SystemInfo.graphicsDeviceName, _upscalerQuality); } - ApplyCameraAntialiasing(Camera.main); + if (!IsUpscalerSupported(_upscaler)) { + Logger.Info("{Upscaler} doesn't run on the {Gpu:l}, so nothing upscales.", _upscaler, SystemInfo.graphicsDeviceName); + } + // what the camera ends up with: the pipeline asset and the cabinet projection can rule the upscaler out too + var upscaling = ApplyCameraAntialiasing(Camera.main); ApplyBallReflections(Camera.main, settings.BallReflections); ApplyCameraMotionBlur(Camera.main, settings.MotionBlur != (int)VpeMotionBlur.Off); ApplyVolume(settings); ApplyCameraRayTracing(Camera.main, _tracing); HdrpShadowCache.Refresh(); Logger.Info("Upscaling {Upscaler} {Quality} (DLSS detected: {Dlss}, FSR 2 detected: {Fsr2})", - _upscaler, _upscalerQuality, HDDynamicResolutionPlatformCapabilities.DLSSDetected, HDDynamicResolutionPlatformCapabilities.FSR2Detected); + upscaling, _upscalerQuality, HDDynamicResolutionPlatformCapabilities.DLSSDetected, HDDynamicResolutionPlatformCapabilities.FSR2Detected); } // HDRP only blurs where the camera's frame settings allow it, whatever the volume says, and a camera with @@ -354,27 +361,54 @@ private static void SetDepthOfField(bool on) private static bool IsUpscalerQualitySupported(VpeUpscaler upscaler, VpeUpscalerQuality quality) => !(upscaler == VpeUpscaler.Fsr2 && quality == VpeUpscalerQuality.Performance && _fsr2PerformanceCrashes); + // AMD's plugin throws on the graphics thread, unhandled, when FSR 2 runs on an output without pixels, on + // any GPU: a window sized down to nothing, like the one a start that moves to its playfield monitor can + // have for its first frames. DLSS and STP take it. HDRP decides per camera, after the context begins to + // render, whether FSR 2 runs; on a camera whose output is that small, it doesn't, until it's bigger. + private const int MinFsr2Output = 32; + private static readonly HashSet Fsr2Held = new(); + + private static void HoldFsr2OnEmptyOutputs(ScriptableRenderContext context, List cameras) + { + foreach (var cam in cameras) { + if (cam == null || !cam.TryGetComponent(out var hd)) { + continue; + } + var empty = cam.pixelWidth < MinFsr2Output || cam.pixelHeight < MinFsr2Output; + if (empty && hd.allowFidelityFX2SuperResolution) { + hd.allowFidelityFX2SuperResolution = false; + Fsr2Held.Add(hd); + Logger.Info("FSR 2 waits on {Camera:l}: its output is {Width}x{Height}.", cam.name, cam.pixelWidth, cam.pixelHeight); + } else if (!empty && Fsr2Held.Remove(hd)) { + hd.allowFidelityFX2SuperResolution = true; + Logger.Info("FSR 2 runs again on {Camera:l}, at {Width}x{Height}.", cam.name, cam.pixelWidth, cam.pixelHeight); + } + } + } + /// /// The chosen anti-aliasing, or what the camera's cabinet projection allows instead. Without a choice - /// from the host, stands in for it. + /// from the host, stands in for it. Returns the upscaler the camera runs. /// - internal static void ApplyCameraAntialiasing(Camera cam, HDAdditionalCameraData.AntialiasingMode? unconfigured = null) + internal static VpeUpscaler ApplyCameraAntialiasing(Camera cam, HDAdditionalCameraData.AntialiasingMode? unconfigured = null) { if (cam == null) { - return; + return VpeUpscaler.Off; } var hd = cam.GetComponent(); if (hd == null) { - return; + return VpeUpscaler.Off; } var requested = _antiAliasing >= 0 ? (VpeAntiAliasing)_antiAliasing : ToVpe(unconfigured ?? hd.antialiasing); // DLSS and FSR 2 do the anti-aliasing themselves; HDRP would still run FXAA after them (it jitters // regardless). STP is HDRP's TAA upsampling, and only runs where the camera has TAA. - hd.antialiasing = ApplyCameraUpscaler(cam, hd) switch { + var upscaler = ApplyCameraUpscaler(cam, hd); + hd.antialiasing = upscaler switch { VpeUpscaler.Stp => HDAdditionalCameraData.AntialiasingMode.TemporalAntialiasing, VpeUpscaler.Off => ToHdrp(CabinetProjection.EffectiveAntiAliasing(cam, requested)), _ => HDAdditionalCameraData.AntialiasingMode.None, }; + return upscaler; } // The upscalers are temporal like TAA: where the cabinet projection rules out TAA, they're off too. @@ -398,6 +432,8 @@ private static VpeUpscaler ApplyCameraUpscaler(Camera cam, HDAdditionalCameraDat hd.deepLearningSuperSamplingUseCustomAttributes = true; hd.deepLearningSuperSamplingUseOptimalSettings = true; + // a new choice; the next frame holds it back again while the output is too small + Fsr2Held.Remove(hd); hd.allowFidelityFX2SuperResolution = upscaler == VpeUpscaler.Fsr2; hd.fidelityFX2SuperResolutionUseCustomQualitySettings = true; hd.fidelityFX2SuperResolutionQuality = (uint)_upscalerQuality; // AMD FSR2Quality has the same order