A game about forced loneliness, made by TACStudios
1using System;
2using System.Linq;
3using System.Collections.Generic;
4
5namespace UnityEditor.ShaderGraph
6{
7 [GenerationAPI]
8 internal class TargetSetupContext
9 {
10 public List<SubShaderDescriptor> subShaders { get; private set; }
11
12 public KernelCollection kernels { get; private set; }
13 public AssetCollection assetCollection { get; private set; }
14
15 // these are data that are now stored in the subshaders.
16 // but for backwards compatibility with the existing Targets,
17 // we store the values provided directly by the Target,
18 // and apply them to all of the subShaders provided by the Target (that don't have their own setting)
19 // the Targets are free to switch to specifying these values per SubShaderDescriptor instead,
20 // if they want to specify different values for each subshader.
21 private List<ShaderCustomEditor> customEditorForRenderPipelines;
22 private string defaultShaderGUI;
23
24 // assetCollection is used to gather asset dependencies
25 public TargetSetupContext(AssetCollection assetCollection = null)
26 {
27 subShaders = new List<SubShaderDescriptor>();
28 kernels = new KernelCollection();
29 this.assetCollection = assetCollection;
30 }
31
32 public void SetupFinalize()
33 {
34 // copy custom editors to each subshader, if they don't have their own specification
35 if (subShaders == null)
36 return;
37
38 for (int i = 0; i < subShaders.Count; i++)
39 {
40 var subShader = subShaders[i];
41
42 if ((subShader.shaderCustomEditors == null) && (customEditorForRenderPipelines != null))
43 subShader.shaderCustomEditors = new List<ShaderCustomEditor>(customEditorForRenderPipelines);
44
45 if (subShader.shaderCustomEditor == null)
46 subShader.shaderCustomEditor = defaultShaderGUI;
47
48 subShaders[i] = subShader; // yay C# structs
49 }
50 }
51
52 public void AddSubShader(SubShaderDescriptor subShader)
53 {
54 subShaders.Add(subShader);
55 }
56
57 public void AddKernel(KernelDescriptor kernel)
58 {
59 kernels.Add(kernel);
60 }
61
62 public void AddAssetDependency(GUID guid, AssetCollection.Flags flags)
63 {
64 assetCollection?.AddAssetDependency(guid, flags);
65 }
66
67 public void SetDefaultShaderGUI(string defaultShaderGUI)
68 {
69 this.defaultShaderGUI = defaultShaderGUI;
70 }
71
72 public void AddCustomEditorForRenderPipeline(string shaderGUI, Type renderPipelineAssetType)
73 => AddCustomEditorForRenderPipeline(shaderGUI, renderPipelineAssetType.FullName);
74
75 public void AddCustomEditorForRenderPipeline(string shaderGUI, string renderPipelineAssetTypeFullName)
76 {
77 if (customEditorForRenderPipelines == null)
78 customEditorForRenderPipelines = new List<ShaderCustomEditor>();
79
80 customEditorForRenderPipelines.Add(
81 new ShaderCustomEditor()
82 {
83 shaderGUI = shaderGUI,
84 renderPipelineAssetType = renderPipelineAssetTypeFullName
85 });
86 }
87
88 public bool HasCustomEditorForRenderPipeline<RPT>()
89 => HasCustomEditorForRenderPipeline(typeof(RPT).FullName);
90
91 public bool HasCustomEditorForRenderPipeline(Type renderPipelineAssetType)
92 => HasCustomEditorForRenderPipeline(renderPipelineAssetType.FullName);
93
94 public bool HasCustomEditorForRenderPipeline(string renderPipelineAssetTypeFullName)
95 => customEditorForRenderPipelines?.Any(c => c.renderPipelineAssetType == renderPipelineAssetTypeFullName) ?? false;
96 }
97}