From 5345aa80c13b3b57ff3d4080090a60df73db1f47 Mon Sep 17 00:00:00 2001 From: sebastienlagarde Date: Wed, 21 Apr 2021 19:19:16 +0200 Subject: [PATCH 1/3] draft --- .../BuildProcessors/HDRPPreprocessShaders.cs | 43 ++++++++++++++++++- .../RenderPipeline/HDRenderPipeline.cs | 22 ++++------ 2 files changed, 50 insertions(+), 15 deletions(-) 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..88f20538aa9 100644 --- a/com.unity.render-pipelines.high-definition/Editor/BuildProcessors/HDRPPreprocessShaders.cs +++ b/com.unity.render-pipelines.high-definition/Editor/BuildProcessors/HDRPPreprocessShaders.cs @@ -297,6 +297,10 @@ public void OnProcessComputeShader(ComputeShader shader, string kernelName, ILis if (HDRenderPipeline.currentAsset == null) return; + // Discard any compute use for raytracing only if not required + if (!s_PlayerNeedRaytracing && IsRaytracingResources()) + return; + var exportLog = ShaderBuildPreprocessor.hdrpAssets.Count > 0 && ShaderBuildPreprocessor.hdrpAssets.Any(hdrpAsset => hdrpAsset.shaderVariantLogLevel != ShaderVariantLogLevel.Disabled); @@ -535,6 +539,8 @@ public void OnProcessShader(Shader shader, ShaderSnippetData snippet, IList _hdrpAssets; + private static bool s_PlayerNeedRaytracing; + private static List _RaytracingComputeResources; public static List hdrpAssets { @@ -545,13 +551,37 @@ public static List hdrpAssets } } + public static bool BuilRaytracingComputeList(System.Object container, string basePath) + { + if (HDRenderPipeline.defaultAsset == null) + return; + + if (IsNull(HDRenderPipeline.defaultAsset.renderPipelineRayTracingResources)) + return; + + var changed = false; + foreach (var fieldInfo in container.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)) + { + //Recurse on sub-containers + if (IsReloadGroup(fieldInfo)) + { + changed |= FixGroupIfNeeded(container, fieldInfo); + changed |= ReloadAllNullIn(fieldInfo.GetValue(container), basePath); + } + } + } + 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 +672,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..6876e56b024 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs @@ -612,22 +612,18 @@ void UpgradeResourcesInAssetIfNeeded(HDRenderPipelineAsset asset) ResourceReloader.ReloadAllNullIn(asset.renderPipelineResources, HDUtils.GetHDRenderPipelinePath()); #endif - if (GatherRayTracingSupport(asset.currentPlatformRenderPipelineSettings)) - { - if (asset.renderPipelineRayTracingResources == null) - asset.renderPipelineRayTracingResources - = UnityEditor.AssetDatabase.LoadAssetAtPath(HDUtils.GetHDRenderPipelinePath() + "Runtime/RenderPipelineResources/HDRenderPipelineRayTracingResources.asset"); + // We always allocate raytracing resources as there is no way currently to know if they will be used or not. + // When we have multiple RP Asset, some could have the raytracing enabled, the other not. + // To do thing properly we should strip the resources inside the stripper based on the platform and all the + // Render pipeline available there. + if (asset.renderPipelineRayTracingResources == null) + asset.renderPipelineRayTracingResources + = UnityEditor.AssetDatabase.LoadAssetAtPath(HDUtils.GetHDRenderPipelinePath() + "Runtime/RenderPipelineResources/HDRenderPipelineRayTracingResources.asset"); #if UNITY_EDITOR_LINUX // Temp hack to be able to make linux test run. To clarify - ResourceReloader.TryReloadAllNullIn(asset.renderPipelineRayTracingResources, HDUtils.GetHDRenderPipelinePath()); + ResourceReloader.TryReloadAllNullIn(asset.renderPipelineRayTracingResources, HDUtils.GetHDRenderPipelinePath()); #else - ResourceReloader.ReloadAllNullIn(asset.renderPipelineRayTracingResources, HDUtils.GetHDRenderPipelinePath()); + ResourceReloader.ReloadAllNullIn(asset.renderPipelineRayTracingResources, HDUtils.GetHDRenderPipelinePath()); #endif - } - else - { - // If ray tracing is not enabled we do not want to have ray tracing resources referenced - asset.renderPipelineRayTracingResources = null; - } var editorResourcesPath = HDUtils.GetHDRenderPipelinePath() + "Editor/RenderPipelineResources/HDRenderPipelineEditorResources.asset"; if (asset.renderPipelineEditorResources == null) From 790fc7a516308367c4c18f34c1ed66d1a7d52f18 Mon Sep 17 00:00:00 2001 From: sebastienlagarde Date: Wed, 21 Apr 2021 23:34:40 +0200 Subject: [PATCH 2/3] Fix build DXR --- .../BuildProcessors/HDRPPreprocessShaders.cs | 50 ++++++++++++++----- 1 file changed, 38 insertions(+), 12 deletions(-) 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 88f20538aa9..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,8 +298,9 @@ public void OnProcessComputeShader(ComputeShader shader, string kernelName, ILis if (HDRenderPipeline.currentAsset == null) return; - // Discard any compute use for raytracing only if not required - if (!s_PlayerNeedRaytracing && IsRaytracingResources()) + // 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 @@ -539,34 +541,58 @@ public void OnProcessShader(Shader shader, ShaderSnippetData snippet, IList _hdrpAssets; + private static Dictionary s_ComputeShaderCache; private static bool s_PlayerNeedRaytracing; - private static List _RaytracingComputeResources; public static List hdrpAssets { get { - if (_hdrpAssets == null || _hdrpAssets.Count == 0) GetAllValidHDRPAssets(); + if (_hdrpAssets == null || _hdrpAssets.Count == 0) + GetAllValidHDRPAssets(); return _hdrpAssets; } } - public static bool BuilRaytracingComputeList(System.Object container, string basePath) + 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 (IsNull(HDRenderPipeline.defaultAsset.renderPipelineRayTracingResources)) + if (HDRenderPipeline.defaultAsset.renderPipelineRayTracingResources == null) return; - var changed = false; - foreach (var fieldInfo in container.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)) + foreach (var fieldInfo in HDRenderPipeline.defaultAsset.renderPipelineRayTracingResources.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)) { - //Recurse on sub-containers - if (IsReloadGroup(fieldInfo)) + ComputeShader computeshader; + computeshader = fieldInfo.GetValue(HDRenderPipeline.defaultAsset.renderPipelineRayTracingResources) as ComputeShader; + + if (computeshader != null) { - changed |= FixGroupIfNeeded(container, fieldInfo); - changed |= ReloadAllNullIn(fieldInfo.GetValue(container), basePath); + s_ComputeShaderCache.Add(computeshader.GetInstanceID(), computeshader); } } } From 622e689e2df7fbd59c3b6c5647689add3fcee695 Mon Sep 17 00:00:00 2001 From: sebastienlagarde Date: Thu, 22 Apr 2021 00:40:59 +0200 Subject: [PATCH 3/3] Match behavior with the one in master --- .../RenderPipeline/HDRenderPipeline.cs | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 deletions(-) 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 6876e56b024..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,18 +612,35 @@ void UpgradeResourcesInAssetIfNeeded(HDRenderPipelineAsset asset) ResourceReloader.ReloadAllNullIn(asset.renderPipelineResources, HDUtils.GetHDRenderPipelinePath()); #endif - // We always allocate raytracing resources as there is no way currently to know if they will be used or not. - // When we have multiple RP Asset, some could have the raytracing enabled, the other not. - // To do thing properly we should strip the resources inside the stripper based on the platform and all the - // Render pipeline available there. - if (asset.renderPipelineRayTracingResources == null) - asset.renderPipelineRayTracingResources - = UnityEditor.AssetDatabase.LoadAssetAtPath(HDUtils.GetHDRenderPipelinePath() + "Runtime/RenderPipelineResources/HDRenderPipelineRayTracingResources.asset"); + 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 + = UnityEditor.AssetDatabase.LoadAssetAtPath(HDUtils.GetHDRenderPipelinePath() + "Runtime/RenderPipelineResources/HDRenderPipelineRayTracingResources.asset"); #if UNITY_EDITOR_LINUX // Temp hack to be able to make linux test run. To clarify - ResourceReloader.TryReloadAllNullIn(asset.renderPipelineRayTracingResources, HDUtils.GetHDRenderPipelinePath()); + ResourceReloader.TryReloadAllNullIn(asset.renderPipelineRayTracingResources, HDUtils.GetHDRenderPipelinePath()); #else - ResourceReloader.ReloadAllNullIn(asset.renderPipelineRayTracingResources, HDUtils.GetHDRenderPipelinePath()); + ResourceReloader.ReloadAllNullIn(asset.renderPipelineRayTracingResources, HDUtils.GetHDRenderPipelinePath()); #endif + } + else + { + // If ray tracing is not enabled we do not want to have ray tracing resources referenced + asset.renderPipelineRayTracingResources = null; + } var editorResourcesPath = HDUtils.GetHDRenderPipelinePath() + "Editor/RenderPipelineResources/HDRenderPipelineEditorResources.asset"; if (asset.renderPipelineEditorResources == null)