diff --git a/com.unity.render-pipelines.high-definition/Editor/BuildProcessors/HDRPPreprocessShaders.cs b/com.unity.render-pipelines.high-definition/Editor/BuildProcessors/HDRPPreprocessShaders.cs index b82ee87e056..61aaeeb6a6f 100644 --- a/com.unity.render-pipelines.high-definition/Editor/BuildProcessors/HDRPPreprocessShaders.cs +++ b/com.unity.render-pipelines.high-definition/Editor/BuildProcessors/HDRPPreprocessShaders.cs @@ -7,6 +7,7 @@ using UnityEngine.Rendering.HighDefinition; using System.Diagnostics; using Debug = UnityEngine.Debug; +using System.Reflection; namespace UnityEditor.Rendering.HighDefinition { @@ -297,6 +298,11 @@ public void OnProcessComputeShader(ComputeShader shader, string kernelName, ILis if (HDRenderPipeline.currentAsset == null) return; + // Discard any compute shader use for raytracing if none of the RP asset required it + ComputeShader unused; + if (!ShaderBuildPreprocessor.playerNeedRaytracing && ShaderBuildPreprocessor.computeShaderCache.TryGetValue(shader.GetInstanceID(), out unused)) + return; + var exportLog = ShaderBuildPreprocessor.hdrpAssets.Count > 0 && ShaderBuildPreprocessor.hdrpAssets.Any(hdrpAsset => hdrpAsset.shaderVariantLogLevel != ShaderVariantLogLevel.Disabled); @@ -535,23 +541,73 @@ public void OnProcessShader(Shader shader, ShaderSnippetData snippet, IList _hdrpAssets; + private static Dictionary s_ComputeShaderCache; + private static bool s_PlayerNeedRaytracing; public static List hdrpAssets { get { - if (_hdrpAssets == null || _hdrpAssets.Count == 0) GetAllValidHDRPAssets(); + if (_hdrpAssets == null || _hdrpAssets.Count == 0) + GetAllValidHDRPAssets(); return _hdrpAssets; } } + public static Dictionary computeShaderCache + { + get + { + if (s_ComputeShaderCache == null) + BuilRaytracingComputeList(); + return s_ComputeShaderCache; + } + } + + public static bool playerNeedRaytracing + { + get + { + return s_PlayerNeedRaytracing; + } + } + + public static void BuilRaytracingComputeList() + { + if (s_ComputeShaderCache != null) + s_ComputeShaderCache.Clear(); + else + s_ComputeShaderCache = new Dictionary(); + + if (HDRenderPipeline.defaultAsset == null) + return; + + if (HDRenderPipeline.defaultAsset.renderPipelineRayTracingResources == null) + return; + + foreach (var fieldInfo in HDRenderPipeline.defaultAsset.renderPipelineRayTracingResources.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)) + { + ComputeShader computeshader; + computeshader = fieldInfo.GetValue(HDRenderPipeline.defaultAsset.renderPipelineRayTracingResources) as ComputeShader; + + if (computeshader != null) + { + s_ComputeShaderCache.Add(computeshader.GetInstanceID(), computeshader); + } + } + } + static void GetAllValidHDRPAssets() { + s_PlayerNeedRaytracing = false; + if (HDRenderPipeline.currentAsset == null) return; - if (_hdrpAssets != null) _hdrpAssets.Clear(); - else _hdrpAssets = new List(); + if (_hdrpAssets != null) + _hdrpAssets.Clear(); + else + _hdrpAssets = new List(); using (ListPool.Get(out var tmpAssets)) { @@ -642,6 +698,15 @@ static void GetAllValidHDRPAssets() Debug.LogWarning("There is no HDRP Asset provided in GraphicsSettings. Build time can be extremely long without it."); } } + else + { + // Take the opportunity to know if we need raytracing at runtime + foreach (var hdrpAsset in _hdrpAssets) + { + if (hdrpAsset.currentPlatformRenderPipelineSettings.supportRayTracing) + s_PlayerNeedRaytracing = true; + } + } /* Debug.Log(string.Format("{0} HDRP assets in build:{1}", diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs index 0907569c3f1..629c7c22114 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs @@ -612,7 +612,20 @@ void UpgradeResourcesInAssetIfNeeded(HDRenderPipelineAsset asset) ResourceReloader.ReloadAllNullIn(asset.renderPipelineResources, HDUtils.GetHDRenderPipelinePath()); #endif - if (GatherRayTracingSupport(asset.currentPlatformRenderPipelineSettings)) + bool requiresRayTracingResources = false; + // Make sure to include ray-tracing resources if at least one of the defaultAsset or quality levels needs it + if (defaultAsset.currentPlatformRenderPipelineSettings.supportRayTracing) + requiresRayTracingResources = true; + + int qualityLevelCount = QualitySettings.names.Length; + for (int i = 0; i < qualityLevelCount && !requiresRayTracingResources; ++i) + { + var hdrpAsset = QualitySettings.GetRenderPipelineAssetAt(i) as HDRenderPipelineAsset; + if (hdrpAsset != null && hdrpAsset.currentPlatformRenderPipelineSettings.supportRayTracing) + requiresRayTracingResources = true; + } + + if (requiresRayTracingResources) { if (asset.renderPipelineRayTracingResources == null) asset.renderPipelineRayTracingResources