Skip to content

Commit 6d4d44e

Browse files
ncerone-unityEvergreen
authored andcommitted
[Backport 6000.5] Correctly support UITKPreview without affecting other preview
1 parent 860b79e commit 6d4d44e

9 files changed

Lines changed: 173 additions & 67 deletions

File tree

Packages/com.unity.shadergraph/Editor/Data/Graphs/GraphData.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,17 @@ public List<BlockFieldDescriptor> GetActiveBlocksForAllActiveTargets()
887887
return context.activeBlocks;
888888
}
889889

890+
public void RefreshBadgesAndPreviews()
891+
{
892+
foreach (var node in this.m_Nodes)
893+
{
894+
if (node.value != null)
895+
{
896+
node.value.Dirty(ModificationScope.Graph);
897+
}
898+
}
899+
}
900+
890901
public void UpdateActiveBlocks(List<BlockFieldDescriptor> activeBlockDescriptors)
891902
{
892903
// Set Blocks as active based on supported Block list

Packages/com.unity.shadergraph/Editor/Drawing/Views/MaterialGraphView.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ void ChangeTargetSettings()
138138
graph.AddRemoveBlocksFromActiveList(activeBlocks);
139139
}
140140

141+
graph.RefreshBadgesAndPreviews();
141142
graph.UpdateActiveBlocks(activeBlocks);
142143
this.m_PreviewManagerUpdateDelegate();
143144
this.m_InspectorUpdateDelegate();

Packages/com.unity.shadergraph/Editor/Generation/Processors/Generator.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using UnityEngine.Assertions;
1313
using Pool = UnityEngine.Pool;
1414
using Unity.Profiling;
15+
using UnityEditor.Rendering.UITK.ShaderGraph;
1516

1617
namespace UnityEditor.ShaderGraph
1718
{
@@ -192,6 +193,14 @@ Target[] GetTargetImplementations()
192193
}
193194
else
194195
{
196+
var targets = m_GraphData.activeTargets.ToList();
197+
foreach (var target in targets)
198+
{
199+
if (target.activeSubTarget is IUISubTarget)
200+
{
201+
return new Target[] { new PreviewTarget(true) };
202+
}
203+
}
195204
return new Target[] { new PreviewTarget() };
196205
}
197206
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#ifdef UITK_PREVIEW
2+
#include "UIShim.hlsl"
3+
#endif

Packages/com.unity.shadergraph/Editor/Generation/Targets/BuiltIn/ShaderLibrary/Shim/UIShimPreview.hlsl.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/com.unity.shadergraph/Editor/Generation/Targets/PreviewTarget.cs

Lines changed: 81 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@ sealed class PreviewTarget : Target
1010
{
1111
static readonly GUID kSourceCodeGuid = new GUID("7464b9fcde08e5645a16b9b8ae1e573c"); // PreviewTarget.cs
1212

13-
public PreviewTarget()
13+
private bool isUITKPreview = false;
14+
15+
public PreviewTarget() : this(false)
16+
{
17+
}
18+
19+
public PreviewTarget(bool isUITKPreview)
1420
{
1521
displayName = "Preview";
1622
isHidden = true;
23+
this.isUITKPreview = isUITKPreview;
1724
}
1825

1926
public override bool IsActive() => false;
@@ -22,7 +29,7 @@ public PreviewTarget()
2229
public override void Setup(ref TargetSetupContext context)
2330
{
2431
context.AddAssetDependency(kSourceCodeGuid, AssetCollection.Flags.SourceDependency);
25-
context.AddSubShader(SubShaders.Preview);
32+
context.AddSubShader(SubShaders.GetPreview(isUITKPreview));
2633
}
2734

2835
public override void GetFields(ref TargetFieldContext context)
@@ -41,66 +48,77 @@ public override void GetPropertiesGUI(ref TargetPropertyGUIContext context, Acti
4148

4249
static class SubShaders
4350
{
44-
public static SubShaderDescriptor Preview = new SubShaderDescriptor()
51+
public static SubShaderDescriptor GetPreview(bool isUITKPreview)
4552
{
46-
renderQueue = "Geometry",
47-
renderType = "Opaque",
48-
generatesPreview = true,
49-
passes = new PassCollection { Passes.Preview },
50-
};
53+
var preview = new SubShaderDescriptor()
54+
{
55+
renderQueue = "Geometry",
56+
renderType = "Opaque",
57+
generatesPreview = true,
58+
passes = new PassCollection { Passes.GetPreview(isUITKPreview) },
59+
};
60+
61+
return preview;
62+
}
5163
}
5264

5365
static class Passes
5466
{
55-
public static PassDescriptor Preview = new PassDescriptor()
67+
public static PassDescriptor GetPreview(bool isUITKPreview)
5668
{
57-
// Definition
58-
referenceName = "SHADERPASS_PREVIEW",
59-
useInPreview = true,
69+
var pass = new PassDescriptor()
70+
{
71+
// Definition
72+
referenceName = "SHADERPASS_PREVIEW",
73+
useInPreview = true,
6074

61-
// Templates
62-
passTemplatePath = GenerationUtils.GetDefaultTemplatePath("PassMesh.template"),
63-
sharedTemplateDirectories = GenerationUtils.GetDefaultSharedTemplateDirectories(),
75+
// Templates
76+
passTemplatePath = GenerationUtils.GetDefaultTemplatePath("PassMesh.template"),
77+
sharedTemplateDirectories = GenerationUtils.GetDefaultSharedTemplateDirectories(),
6478

65-
// Collections
66-
structs = new StructCollection
67-
{
68-
{ Structs.Attributes },
69-
{ StructDescriptors.PreviewVaryings },
70-
{ Structs.SurfaceDescriptionInputs },
71-
{ Structs.VertexDescriptionInputs },
72-
},
73-
fieldDependencies = FieldDependencies.Default,
74-
pragmas = new PragmaCollection
75-
{
76-
{ Pragma.Vertex("vert") },
77-
{ Pragma.Fragment("frag") },
78-
},
79-
defines = new DefineCollection
80-
{
81-
{ KeywordDescriptors.PreviewKeyword, 1 },
82-
},
83-
includes = new IncludeCollection
84-
{
85-
// Pre-graph
86-
{ "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl", IncludeLocation.Pregraph },
87-
{ "Packages/com.unity.render-pipelines.core/ShaderLibrary/Packing.hlsl", IncludeLocation.Pregraph },
88-
{ "Packages/com.unity.render-pipelines.core/ShaderLibrary/TextureStack.hlsl", IncludeLocation.Pregraph }, // TODO: put this on a conditional
89-
{ "Packages/com.unity.render-pipelines.core/ShaderLibrary/NormalSurfaceGradient.hlsl", IncludeLocation.Pregraph },
90-
{ "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl", IncludeLocation.Pregraph },
91-
{ "Packages/com.unity.render-pipelines.core/ShaderLibrary/Texture.hlsl", IncludeLocation.Pregraph },
92-
{ "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl", IncludeLocation.Pregraph },
93-
{ "Packages/com.unity.render-pipelines.core/ShaderLibrary/EntityLighting.hlsl", IncludeLocation.Pregraph },
94-
{ "Packages/com.unity.shadergraph/ShaderGraphLibrary/ShaderVariables.hlsl", IncludeLocation.Pregraph },
95-
{ "Packages/com.unity.shadergraph/ShaderGraphLibrary/ShaderVariablesFunctions.hlsl", IncludeLocation.Pregraph },
96-
{ "Packages/com.unity.shadergraph/ShaderGraphLibrary/Functions.hlsl", IncludeLocation.Pregraph },
97-
{ "Packages/com.unity.shadergraph/Editor/Generation/Targets/BuiltIn/ShaderLibrary/Shim/UIShim.hlsl", IncludeLocation.Pregraph },
98-
99-
// Post-graph
100-
{ "Packages/com.unity.shadergraph/ShaderGraphLibrary/PreviewVaryings.hlsl", IncludeLocation.Postgraph },
101-
{ "Packages/com.unity.shadergraph/ShaderGraphLibrary/PreviewPass.hlsl", IncludeLocation.Postgraph },
102-
}
103-
};
79+
// Collections
80+
structs = new StructCollection
81+
{
82+
{ Structs.Attributes },
83+
{ StructDescriptors.PreviewVaryings },
84+
{ Structs.SurfaceDescriptionInputs },
85+
{ Structs.VertexDescriptionInputs },
86+
},
87+
fieldDependencies = FieldDependencies.Default,
88+
pragmas = new PragmaCollection
89+
{
90+
{ Pragma.Vertex("vert") },
91+
{ Pragma.Fragment("frag") },
92+
},
93+
defines = new DefineCollection
94+
{
95+
{ KeywordDescriptors.PreviewKeyword, 1 },
96+
{ KeywordDescriptors.UITKPreviewKeyword, isUITKPreview ? 1 : 0 }
97+
},
98+
includes = new IncludeCollection
99+
{
100+
// Pre-graph
101+
{ "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl", IncludeLocation.Pregraph },
102+
{ "Packages/com.unity.render-pipelines.core/ShaderLibrary/Packing.hlsl", IncludeLocation.Pregraph },
103+
{ "Packages/com.unity.render-pipelines.core/ShaderLibrary/TextureStack.hlsl", IncludeLocation.Pregraph }, // TODO: put this on a conditional
104+
{ "Packages/com.unity.render-pipelines.core/ShaderLibrary/NormalSurfaceGradient.hlsl", IncludeLocation.Pregraph },
105+
{ "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl", IncludeLocation.Pregraph },
106+
{ "Packages/com.unity.render-pipelines.core/ShaderLibrary/Texture.hlsl", IncludeLocation.Pregraph },
107+
{ "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl", IncludeLocation.Pregraph },
108+
{ "Packages/com.unity.render-pipelines.core/ShaderLibrary/EntityLighting.hlsl", IncludeLocation.Pregraph },
109+
{ "Packages/com.unity.shadergraph/ShaderGraphLibrary/ShaderVariables.hlsl", IncludeLocation.Pregraph },
110+
{ "Packages/com.unity.shadergraph/ShaderGraphLibrary/ShaderVariablesFunctions.hlsl", IncludeLocation.Pregraph },
111+
{ "Packages/com.unity.shadergraph/ShaderGraphLibrary/Functions.hlsl", IncludeLocation.Pregraph },
112+
{ "Packages/com.unity.shadergraph/Editor/Generation/Targets/BuiltIn/ShaderLibrary/Shim/UIShimPreview.hlsl", IncludeLocation.Pregraph },
113+
114+
// Post-graph
115+
{ "Packages/com.unity.shadergraph/ShaderGraphLibrary/PreviewVaryings.hlsl", IncludeLocation.Postgraph },
116+
{ "Packages/com.unity.shadergraph/ShaderGraphLibrary/PreviewPass.hlsl", IncludeLocation.Postgraph },
117+
}
118+
};
119+
120+
return pass;
121+
}
104122
}
105123

106124
static class StructDescriptors
@@ -144,6 +162,15 @@ static class KeywordDescriptors
144162
scope = KeywordScope.Global,
145163
stages = KeywordShaderStage.All,
146164
};
165+
public static KeywordDescriptor UITKPreviewKeyword = new KeywordDescriptor()
166+
{
167+
displayName = "UITK Preview",
168+
referenceName = "UITK_PREVIEW",
169+
type = KeywordType.Boolean,
170+
definition = KeywordDefinition.MultiCompile,
171+
scope = KeywordScope.Global,
172+
stages = KeywordShaderStage.All,
173+
};
147174
}
148175
}
149176
}

Packages/com.unity.shadergraph/Editor/Generation/Targets/UITK/UISubTarget.cs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ public string GetValidatorKey()
218218
return "UISubTarget";
219219
}
220220

221+
const string kUVErrorMessageNode = "UI Material does not support UV1-7. Consider using 'UV0'.";
222+
const string kUVErrorMessageSubGraph = "UI Material does not support UV1-7. Consider using 'UV0' in the subgraph.";
223+
221224
public INodeValidationExtension.Status GetValidationStatus(AbstractMaterialNode node, out string msg)
222225
{
223226
// Make sure node is in our graph first
@@ -227,6 +230,12 @@ public INodeValidationExtension.Status GetValidationStatus(AbstractMaterialNode
227230
return INodeValidationExtension.Status.None;
228231
}
229232

233+
// Clear all Warning/Error message from other providers.
234+
// The message from the graph (when loading the graph) will not be removed
235+
// since it's not the same provider as the UISubTarget. It then stays present
236+
// even if the UV0 is selected.
237+
node.owner.messageManager.ClearNodeFromOtherProvider(this, new[] { node });
238+
230239
foreach (var item in node.owner.activeTargets)
231240
{
232241
if (item.prefersUITKPreview)
@@ -235,16 +244,6 @@ public INodeValidationExtension.Status GetValidationStatus(AbstractMaterialNode
235244
{
236245
return INodeValidationExtension.Status.Warning;
237246
}
238-
239-
UVNode uvNode = node as UVNode;
240-
if (uvNode != null)
241-
{
242-
if (uvNode.uvChannel != UnityEditor.ShaderGraph.Internal.UVChannel.UV0)
243-
{
244-
msg = "UI Material does not support UV1-7. Consider using 'UV0'.";
245-
return INodeValidationExtension.Status.Warning;
246-
}
247-
}
248247
}
249248
}
250249

@@ -261,7 +260,17 @@ private bool ValidateUV(AbstractMaterialNode node, out string warningMessage)
261260
{
262261
if (uvSlot.channel != UnityEditor.ShaderGraph.Internal.UVChannel.UV0)
263262
{
264-
warningMessage = "UI Material does not support UV1-7. Consider using 'UV0'.";
263+
warningMessage = kUVErrorMessageNode;
264+
return true;
265+
}
266+
}
267+
268+
UVNode uvNode = node as UVNode;
269+
if (uvNode != null)
270+
{
271+
if (uvNode.uvChannel != UnityEditor.ShaderGraph.Internal.UVChannel.UV0)
272+
{
273+
warningMessage = kUVErrorMessageNode;
265274
return true;
266275
}
267276
}
@@ -283,7 +292,7 @@ public override bool ValidateNodeCompatibility(AbstractMaterialNode node, out st
283292
{
284293
if (uvSlot.channel != UnityEditor.ShaderGraph.Internal.UVChannel.UV0)
285294
{
286-
warningMessage = "UI Material does not support UV1-7. Consider using 'UV0'.";
295+
warningMessage = kUVErrorMessageNode;
287296
return true;
288297
}
289298
}
@@ -303,7 +312,7 @@ public override bool ValidateNodeCompatibility(AbstractMaterialNode node, out st
303312
{
304313
if (item != UnityEditor.ShaderGraph.Internal.UVChannel.UV0)
305314
{
306-
warningMessage = "UI Material does not support UV1-7. Consider using 'UV0' in the subgraph.";
315+
warningMessage = kUVErrorMessageSubGraph;
307316
return true;
308317
}
309318
}

Packages/com.unity.shadergraph/Editor/Util/MessageManager.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,24 @@ public void ClearNodesFromProvider(object messageProvider, IEnumerable<AbstractM
124124
}
125125
}
126126

127+
public void ClearNodeFromOtherProvider(object messageProvider, IEnumerable<AbstractMaterialNode> nodes)
128+
{
129+
foreach (var key in m_Messages.Keys)
130+
{
131+
if (key != messageProvider)
132+
{
133+
foreach (var node in nodes)
134+
{
135+
if (m_Messages[key].TryGetValue(node.objectId, out var messages))
136+
{
137+
nodeMessagesChanged |= messages.Count > 0;
138+
messages.Clear();
139+
}
140+
}
141+
}
142+
}
143+
}
144+
127145
public void ClearAll()
128146
{
129147
m_Messages.Clear();

Packages/com.unity.shadergraph/Tests/Editor/UnitTests/MessageManagerTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,27 @@ public void ReportAnyErrors_EmptyManager_ErrorOneProvider()
320320
var ret = m_EmptyMgr.HasSeverity();
321321
Assert.IsTrue(ret);
322322
}
323+
324+
[Test]
325+
public void ClearNodesFromOtherProvider()
326+
{
327+
m_ComplexMgr.ClearNodeFromOtherProvider(p0, new List<AbstractMaterialNode> { node1 });
328+
329+
// Verify node1 is still in provider0
330+
Assert.IsTrue(m_ComplexMgr.Messages.ContainsKey(p0));
331+
Assert.IsTrue(m_ComplexMgr.Messages[p0].ContainsKey(node1.objectId));
332+
Assert.AreEqual(1, m_ComplexMgr.Messages[p0][node1.objectId].Count);
333+
Assert.AreEqual(e2, m_ComplexMgr.Messages[p0][node1.objectId][0]);
334+
335+
// Verify node1 is cleared from provider1
336+
Assert.IsTrue(m_ComplexMgr.Messages.ContainsKey(p1));
337+
Assert.IsTrue(m_ComplexMgr.Messages[p1].ContainsKey(node1.objectId));
338+
Assert.AreEqual(0, m_ComplexMgr.Messages[p1][node1.objectId].Count);
339+
340+
// Verify other nodes in provider1 are unchanged
341+
Assert.AreEqual(1, m_ComplexMgr.Messages[p1][node0.objectId].Count);
342+
Assert.AreEqual(1, m_ComplexMgr.Messages[p1][node2.objectId].Count);
343+
}
323344
}
324345
}
325346

0 commit comments

Comments
 (0)