Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using UnityEngine.Rendering.HighDefinition;
using System.Diagnostics;
using Debug = UnityEngine.Debug;
using System.Reflection;

namespace UnityEditor.Rendering.HighDefinition
{
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -535,23 +541,73 @@ public void OnProcessShader(Shader shader, ShaderSnippetData snippet, IList<Shad
class ShaderBuildPreprocessor : IPreprocessBuildWithReport
{
private static List<HDRenderPipelineAsset> _hdrpAssets;
private static Dictionary<int, ComputeShader> s_ComputeShaderCache;
private static bool s_PlayerNeedRaytracing;

public static List<HDRenderPipelineAsset> hdrpAssets
{
get
{
if (_hdrpAssets == null || _hdrpAssets.Count == 0) GetAllValidHDRPAssets();
if (_hdrpAssets == null || _hdrpAssets.Count == 0)
GetAllValidHDRPAssets();
return _hdrpAssets;
}
}

public static Dictionary<int, ComputeShader> 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<int, ComputeShader>();

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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not need BindingFlags.Static. There will never have static field in resources as they are not serialized.

{
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<HDRenderPipelineAsset>();
if (_hdrpAssets != null)
_hdrpAssets.Clear();
else
_hdrpAssets = new List<HDRenderPipelineAsset>();

using (ListPool<HDRenderPipelineAsset>.Get(out var tmpAssets))
{
Expand Down Expand Up @@ -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}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment on lines +615 to +618
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be simplified as
bool requiresRayTracingResources = defaultAsset.currentPlatformRenderPipelineSettings.supportRayTracing;

Does former GatherRayTracingSupport also check if hardware is compatible? Should we check it here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, will make the change on master. We must not check hardware support as you can build with a GPU not supporting it


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
Expand Down