diff --git a/Runtime/CameraFrameSettings.cs b/Runtime/CameraFrameSettings.cs
index 2c04909..44859b9 100644
--- a/Runtime/CameraFrameSettings.cs
+++ b/Runtime/CameraFrameSettings.cs
@@ -52,6 +52,20 @@ public static void Enable(HDAdditionalCameraData camera, FrameSettingsField fiel
camera.renderingPathCustomFrameSettings.SetEnabled(field, true);
}
+ ///
+ /// Switches a field the caller holds through on or off: for a setting the camera has to
+ /// keep either way while the caller runs (ray tracing, which is on by default where the asset traces).
+ ///
+ public static void Set(HDAdditionalCameraData camera, FrameSettingsField field, bool enabled)
+ {
+ if (camera == null || !Cameras.TryGetValue(camera, out var saved) || !saved.Fields.ContainsKey(field)) {
+ return;
+ }
+ camera.customRenderingSettings = true;
+ camera.renderingPathCustomFrameSettingsOverrideMask.mask[(uint)field] = true;
+ camera.renderingPathCustomFrameSettings.SetEnabled(field, enabled);
+ }
+
/// Gives up one of the field. A destroyed camera has nothing to restore.
public static void Release(HDAdditionalCameraData camera, FrameSettingsField field)
{
diff --git a/Runtime/HdrpGraphicsApplier.cs b/Runtime/HdrpGraphicsApplier.cs
index d669d4d..a67c7f6 100644
--- a/Runtime/HdrpGraphicsApplier.cs
+++ b/Runtime/HdrpGraphicsApplier.cs
@@ -25,7 +25,7 @@ namespace VisualPinball.Engine.Unity.Hdrp
{
///
/// Applies the host app's to HDRP at runtime: camera anti-aliasing,
- /// ball reflections, the shadows of the lights around the table and the live Volume effects (SSR/RT
+ /// ball reflections, the shadows of the lights around the table, ray traced or not, and the live Volume effects (SSR/RT
/// reflections and their quality, SSAO, SSGI/RT-GI, recursive ray tracing, Bloom).
/// Registered before any scene loads, the same pattern as HdrpMaterialResolver. Engine-level
/// settings (resolution, vsync, quality level) are applied host-side and don't pass through here.
@@ -49,8 +49,13 @@ private static void Register()
VpeGraphics.DepthOfFieldHandler = SetDepthOfField;
VpeGraphics.ReleaseRenderBuffersHandler = ReleaseRenderBuffers;
VpeGraphics.EffectSupport = IsEffectSupported;
+ VpeGraphics.TierEffectSupport = IsEffectSupportedAt;
VpeGraphics.EffectiveSettingsSource = DescribeEffective;
_shadows = VpeShadows.High;
+ _rayTracedShadows = false;
+ _tracing = false;
+ // without a domain reload, the last play session's holds are still counted
+ ReleaseCameraRayTracing();
ShadowLights.Clear();
Originals.Clear();
#if ENABLE_NVIDIA && ENABLE_NVIDIA_MODULE
@@ -76,14 +81,20 @@ private static string DescribeEffective(Camera cam)
var blur = stack.GetComponent();
string On(bool on) => on ? "on" : "off";
var shadowLights = 0;
+ var tracedLights = 0; // of them, the ones whose shadows are ray traced
foreach (var light in Object.FindObjectsByType(FindObjectsInactive.Exclude)) {
if (light.enabled && light.shadows != LightShadows.None) {
shadowLights++;
+ if (frame.IsEnabled(FrameSettingsField.RayTracing) && frame.IsEnabled(FrameSettingsField.ScreenSpaceShadows)
+ && light.TryGetComponent(out var hdLight) && hdLight.useRayTracedShadows
+ && (light.type != LightType.Directional || hdLight.useScreenSpaceShadows)) {
+ tracedLights++;
+ }
}
}
return string.Format(System.Globalization.CultureInfo.InvariantCulture,
"asset {0}; aa {1}; bloom {2:0.###} (frame {3}); ao {4:0.###} (frame {5}); ssr {6}/transparent {7} quality {8} (frame {9}); ssgi {10} (frame {11}); "
- + "shadows {12} light(s), {13} cascade(s) to {14:0.##} m (frame {15}); motion blur {16:0.##} (frame {17}); scale {18:0.###}",
+ + "shadows {12} light(s), {19} ray traced, {13} cascade(s) to {14:0.##} m (frame {15}); motion blur {16:0.##} (frame {17}); scale {18:0.###}",
GraphicsSettings.currentRenderPipeline != null ? GraphicsSettings.currentRenderPipeline.name : "none",
cam.TryGetComponent(out var data) ? data.antialiasing.ToString() : "?",
bloom.intensity.value, On(frame.IsEnabled(FrameSettingsField.Bloom)),
@@ -92,7 +103,7 @@ private static string DescribeEffective(Camera cam)
On(gi.enable.value), On(frame.IsEnabled(FrameSettingsField.SSGI)),
shadowLights, shadows.cascadeShadowSplitCount.value, shadows.maxShadowDistance.value, On(frame.IsEnabled(FrameSettingsField.ShadowMaps)),
blur.intensity.value, On(frame.IsEnabled(FrameSettingsField.MotionBlur)),
- RenderScale())
+ RenderScale(), tracedLights)
+ DescribeCost(frame, ssr, gi, ao, bloom)
+ DescribeDlss(data);
}
@@ -138,9 +149,16 @@ private static string DescribeCost(FrameSettings frame, ScreenSpaceReflection ss
// A pipeline asset can leave an effect out (the Performant tier has no SSR and no SSGI): its volume
// component and frame setting then do nothing.
- private static bool IsEffectSupported(VpeEffect effect)
+ private static bool IsEffectSupported(VpeEffect effect) => IsEffectSupported(GraphicsSettings.currentRenderPipeline, effect);
+
+ // The same for a tier that isn't the active one: its asset says what it renders without a switch to it.
+ private static bool IsEffectSupportedAt(int qualityLevel, VpeEffect effect)
+ => qualityLevel >= 0 && qualityLevel < QualitySettings.names.Length
+ && IsEffectSupported(QualitySettings.GetRenderPipelineAssetAt(qualityLevel) ?? GraphicsSettings.defaultRenderPipeline, effect);
+
+ private static bool IsEffectSupported(RenderPipelineAsset pipeline, VpeEffect effect)
{
- if (GraphicsSettings.currentRenderPipeline is not HDRenderPipelineAsset asset) {
+ if (pipeline is not HDRenderPipelineAsset asset) {
return false;
}
var settings = asset.currentPlatformRenderPipelineSettings;
@@ -148,6 +166,9 @@ private static bool IsEffectSupported(VpeEffect effect)
VpeEffect.ScreenSpaceReflections => settings.supportSSR,
VpeEffect.AmbientOcclusion => settings.supportSSAO,
VpeEffect.ScreenSpaceGlobalIllumination => settings.supportSSGI,
+ // traced shadows are drawn into the screen-space shadow buffer, which the asset has to have as well
+ VpeEffect.RayTracedShadows => SystemInfo.supportsRayTracing && settings.supportRayTracing
+ && settings.hdShadowInitParams.supportScreenSpaceShadows,
_ => true,
};
}
@@ -190,6 +211,7 @@ public void Apply(VpeGraphicsSettings settings)
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);
@@ -216,6 +238,61 @@ private static void ApplyCameraMotionBlur(Camera cam, bool on)
}
}
+ // With ray tracing in its frame settings, a camera builds the acceleration structure once a light or a
+ // volume asks for rays, and leaves out every light that isn't included for ray tracing. So the camera
+ // only traces while a setting wants it: with all of them off, it renders as on an asset without ray
+ // tracing. Traced shadows are drawn into the screen-space shadow buffer, which the camera needs too.
+ // Both go through CameraFrameSettings, which other components (autofocus, ball reflections) share: one
+ // of them giving the camera its own settings back would otherwise drop these too.
+ private static void ApplyCameraRayTracing(Camera cam, bool on)
+ {
+ HDAdditionalCameraData hd = null;
+ if (cam != null) {
+ cam.TryGetComponent(out hd);
+ }
+ // nothing to keep off where the asset doesn't trace
+ if (!IsRayTracingAvailable()) {
+ hd = null;
+ }
+ // by reference: a destroyed camera equals null, and still has to be released
+ if (!ReferenceEquals(_rayTracingCamera, hd)) {
+ ReleaseCameraRayTracing();
+ if (hd == null) {
+ return;
+ }
+ CameraFrameSettings.Enable(hd, FrameSettingsField.RayTracing);
+ _rayTracingCamera = hd;
+ }
+ if (hd == null) {
+ return;
+ }
+ CameraFrameSettings.Set(hd, FrameSettingsField.RayTracing, on);
+ if (on != _screenSpaceShadowsHeld) {
+ if (on) {
+ CameraFrameSettings.Enable(hd, FrameSettingsField.ScreenSpaceShadows);
+ } else {
+ CameraFrameSettings.Release(hd, FrameSettingsField.ScreenSpaceShadows);
+ }
+ _screenSpaceShadowsHeld = on;
+ }
+ }
+
+ private static void ReleaseCameraRayTracing()
+ {
+ if (!ReferenceEquals(_rayTracingCamera, null)) {
+ if (_screenSpaceShadowsHeld) {
+ CameraFrameSettings.Release(_rayTracingCamera, FrameSettingsField.ScreenSpaceShadows);
+ }
+ CameraFrameSettings.Release(_rayTracingCamera, FrameSettingsField.RayTracing);
+ }
+ _rayTracingCamera = null;
+ _screenSpaceShadowsHeld = false;
+ }
+
+ // the camera whose ray tracing frame setting the applier holds, and whether it holds its screen-space shadows
+ private static HDAdditionalCameraData _rayTracingCamera;
+ private static bool _screenSpaceShadowsHeld;
+
// The component lives on the camera that renders the balls; it's only added once it's wanted.
private static void ApplyBallReflections(Camera cam, bool on)
{
@@ -605,8 +682,18 @@ private static void ApplyVolume(VpeGraphicsSettings s)
ReleaseOrphanedProfiles();
ForgetOrphanedOriginals();
_shadows = (VpeShadows)Mathf.Clamp(s.Shadows, 0, 2);
- ApplyLightShadows();
+ // ray-traced effects only where the active pipeline asset can trace
+ var rayTracing = IsRayTracingAvailable();
+ _rayTracedShadows = s.RayTracedShadows && _shadows != VpeShadows.Off && IsEffectSupported(VpeEffect.RayTracedShadows);
var volumes = FindGlobalVolumes(Camera.main);
+ // the other ray-traced effects run on a volume; traced shadows are the lights' own
+ var rtReflections = s.RayTracedReflections && rayTracing && volumes.Count > 0;
+ var rtGlobalIllumination = s.RayTracedGlobalIllumination && rayTracing && volumes.Count > 0;
+ var recursive = s.RayTracedRecursiveRendering && rayTracing && volumes.Count > 0;
+ // what needs the light cluster; traced shadows don't
+ var tracing = rtReflections || rtGlobalIllumination || recursive;
+ _tracing = _rayTracedShadows || tracing;
+ ApplyLightShadows();
if (volumes.Count == 0) {
// no volume runs the recursive rendering, so no glass may stay flagged for it (it wouldn't draw)
ApplyRecursiveMaterials(false);
@@ -619,13 +706,6 @@ private static void ApplyVolume(VpeGraphicsSettings s)
Logger.Info("Volumes the settings go to: {Volumes:l}; default profile {Default:l}, quality profile {Quality:l}",
string.Join("; ", volumes.ConvertAll(DescribeVolume)), DescribeProfile(VolumeManager.instance.globalDefaultProfile, true), DescribeProfile(VolumeManager.instance.qualityDefaultProfile, true));
- // ray-traced effects only where the active pipeline asset (the Ray Tracing quality level) can trace
- var rayTracing = IsRayTracingAvailable();
- var rtReflections = s.RayTracedReflections && rayTracing;
- var rtGlobalIllumination = s.RayTracedGlobalIllumination && rayTracing;
- var recursive = s.RayTracedRecursiveRendering && rayTracing;
- var tracing = rtReflections || rtGlobalIllumination || recursive;
-
// a table's profile may lack the component a ray-traced effect runs on
var primaryProfile = ProfileOf(primary);
if (tracing && !volumes.Exists(v => ProfileOf(v).Has())) {
@@ -704,11 +784,11 @@ private static void ApplyVolume(VpeGraphicsSettings s)
} else {
Force(ao, ao.intensity, 0f);
}
- // the player build strips the visibility rays ray-traced AO needs (RayTracingPassStripper)
+ // no setting asks for ray-traced occlusion, whatever a table's profile says
ao.rayTracing.Override(false);
}
if (profile.TryGet(out var sss)) {
- // same for ray-traced subsurface scattering
+ // the player build strips the passes of ray-traced subsurface scattering (RayTracingPassStripper)
sss.rayTracing.Override(false);
}
if (profile.TryGet(out var pathTracing)) {
@@ -742,23 +822,10 @@ private static void ApplyVolume(VpeGraphicsSettings s)
FittedClusters.Clear();
}
FitToPlayfield();
- if (rayTracing) {
- foreach (var light in Object.FindObjectsByType(FindObjectsInactive.Include)) {
- // ray-traced shadows and contact shadows trace the visibility rays the build strips
- light.useRayTracedShadows = false;
- light.rayTraceContactShadow = false;
- // A lamp lights what's around it in the reflections too. The bulb models used to leave their
- // light out of ray tracing, and packages exported with them still do.
- if (light.GetComponentInParent(true) != null) {
- light.includeForRayTracing = true;
- }
- }
- }
-
- Logger.Info("Ray tracing {State} (GPU support: {Gpu}, pipeline asset: {Asset}, volumes: {Volumes}); reflections {Reflections}, GI {Gi}, recursive {Recursive}.",
+ Logger.Info("Ray tracing {State} (GPU support: {Gpu}, pipeline asset: {Asset}, volumes: {Volumes}); shadows {Shadows}, reflections {Reflections}, GI {Gi}, recursive {Recursive}.",
rayTracing ? "available" : "unavailable", SystemInfo.supportsRayTracing,
GraphicsSettings.currentRenderPipeline != null ? GraphicsSettings.currentRenderPipeline.name : "none",
- string.Join(", ", volumes.ConvertAll(v => v.name)), rtReflections, rtGlobalIllumination, recursive);
+ string.Join(", ", volumes.ConvertAll(v => v.name)), _rayTracedShadows, rtReflections, rtGlobalIllumination, recursive);
}
// HDRP's ray tracing light cluster is a fixed 64x32x64 grid around the camera, as wide as the LightCluster
@@ -832,6 +899,9 @@ private static float ShadowDistance(Vector3 cameraPosition, PlayfieldComponent p
// they were set up with is kept, for High to give back.
private const int LowShadowResolution = 256;
private static VpeShadows _shadows = VpeShadows.High;
+ // whether the directional light traces its shadows, and whether anything traces at all
+ private static bool _rayTracedShadows;
+ private static bool _tracing;
private static readonly Dictionary ShadowLights = new();
private static void ApplyLightShadows()
@@ -861,6 +931,51 @@ internal static void ApplyLightShadows(HDAdditionalLightData hd)
if (hd == null || !hd.TryGetComponent(out var light)) {
return;
}
+ ApplyShadowMaps(hd, light);
+ ApplyRayTracedShadows(hd, light);
+ }
+
+ // How many rays a pixel sends towards the light, and how far the denoiser looks, in pixels. A traced
+ // light costs both per pixel: on an RTX 4090 at 3840 x 2160, 0.8 ms per ray, and 1.3 ms to denoise at
+ // 8 pixels (2.2 ms at HDRP's 16). The table stands still under a camera that does too, so the
+ // denoiser's history holds: one ray with half the reach gives the same still picture as four with
+ // all of it, at well under half the time.
+ private const int ShadowRays = 1;
+ private const int ShadowFilterSize = 8;
+
+ // Only the directional light traces its shadows: it is the one whose shadows show most, and every
+ // traced light costs the same again (the room's four together took a 4K frame from 6 to 16 ms). The
+ // spots keep their shadow maps. Whether a light traces is the setting's to say, not the table's: a
+ // light a package brought with traced shadows would otherwise trace on its own. The shadow is as soft
+ // as the light's angular diameter makes it, and coloured by what is transparent on the way, so clear
+ // plastics and ramps still cast one. A directional light only traces into the screen-space shadow
+ // buffer, and would be rasterized into it when it doesn't trace. A camera that traces also leaves out
+ // the lights that aren't included for ray tracing, in what it rasterizes too: the bulb models used to
+ // leave their light out, and packages exported with them still do, so every light is included: no
+ // lamp may go dark with the setting.
+ private static void ApplyRayTracedShadows(HDAdditionalLightData hd, Light light)
+ {
+ var directional = light.type == LightType.Directional;
+ var trace = _rayTracedShadows && directional && light.shadows != LightShadows.None;
+ hd.useRayTracedShadows = trace;
+ hd.rayTraceContactShadow = false;
+ if (directional) {
+ hd.useScreenSpaceShadows = trace;
+ }
+ if (_tracing) {
+ hd.includeForRayTracing = true;
+ }
+ if (!trace) {
+ return;
+ }
+ hd.numRayTracingSamples = ShadowRays;
+ hd.filterTracedShadow = true;
+ hd.filterSizeTraced = ShadowFilterSize;
+ hd.colorShadow = true;
+ }
+
+ private static void ApplyShadowMaps(HDAdditionalLightData hd, Light light)
+ {
if (!ShadowLights.TryGetValue(light, out var original)) {
if (light.shadows == LightShadows.None || _shadows == VpeShadows.High) {
return; // casts none, or keeps what it has