using System;
using System.Collections.Generic;
using UnityEditor.Build;
using UnityEngine;
using UnityEngine.Rendering;
namespace UnityEditor.Rendering
{
///
/// Contains a set of needed data for building.
/// This might also being called when building Asset Bundles
///
public class CoreBuildData : IDisposable
{
static CoreBuildData m_Instance = null;
///
/// Instance to the current .
///
public static CoreBuildData instance => m_Instance ??= CreateInstance();
///
/// If the target build has an SRP configured
///
public bool buildingPlayerForRenderPipeline { get; private set; } = false;
///
/// A valid type of , that the build is targeting,
///
public Type currentRenderPipelineAssetType { get; private set; } = null;
///
/// A list of , all of them of the same type.
///
public List renderPipelineAssets { get; private set; } = new();
internal Dictionary computeShaderCache { get; private set; } = new();
internal bool pipelineSupportGPUResidentDrawer { get; private set; } = false;
internal bool playerNeedGPUResidentDrawer { get; private set; } = false;
private CoreBuildData(BuildTarget buildTarget)
{
m_Instance = this;
if (!buildTarget.TryGetRenderPipelineAssets(renderPipelineAssets))
return;
buildingPlayerForRenderPipeline = true;
//We can check only the first as we don't support multiple pipeline type in player
var asset = renderPipelineAssets[0];
currentRenderPipelineAssetType = asset.GetType();
CheckGPUResidentDrawerUsage();
}
private static CoreBuildData CreateInstance()
=> new(EditorUserBuildSettings.activeBuildTarget);
private void CheckGPUResidentDrawerUsage()
{
foreach (var renderPipelineAsset in renderPipelineAssets)
{
if (renderPipelineAsset is IGPUResidentRenderPipeline gpuResidentPipelineAsset
&& gpuResidentPipelineAsset.IsGPUResidentDrawerSupportedBySRP())
{
// Record if any pipeline supports the GPU resident drawer
pipelineSupportGPUResidentDrawer = true;
// If any pipeline already has GPU resident drawer enabled, then record this and also early out
if (gpuResidentPipelineAsset.gpuResidentDrawerSettings.mode != GPUResidentDrawerMode.Disabled)
{
playerNeedGPUResidentDrawer = true;
break;
}
}
}
if (!playerNeedGPUResidentDrawer)
return;
GraphicsSettings.GetRenderPipelineSettings()
.ForEachFieldOfType(computeShader => computeShaderCache.Add(computeShader.GetInstanceID(), computeShader));
}
///
/// Dispose all the gathered data for building
///
public void Dispose()
{
renderPipelineAssets?.Clear();
computeShaderCache?.Clear();
m_Instance = null;
}
}
}