-
Notifications
You must be signed in to change notification settings - Fork 868
Expand file tree
/
Copy pathNodeUtils.cs
More file actions
1020 lines (930 loc) · 35.1 KB
/
NodeUtils.cs
File metadata and controls
1020 lines (930 loc) · 35.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using UnityEditor.ShaderGraph;
using UnityEngine;
using UnityEngine.Pool;
using UnityEngine.Rendering.ShaderGraph;
namespace UnityEditor.Graphing
{
class SlotConfigurationException : Exception
{
public SlotConfigurationException(string message)
: base(message)
{ }
}
static class NodeUtils
{
static string NodeDocSuffix = "-Node";
public static void SlotConfigurationExceptionIfBadConfiguration(AbstractMaterialNode node, IEnumerable<int> expectedInputSlots, IEnumerable<int> expectedOutputSlots)
{
var missingSlots = new List<int>();
var inputSlots = expectedInputSlots as IList<int> ?? expectedInputSlots.ToList();
missingSlots.AddRange(inputSlots.Except(node.GetInputSlots<MaterialSlot>().Select(x => x.id)));
var outputSlots = expectedOutputSlots as IList<int> ?? expectedOutputSlots.ToList();
missingSlots.AddRange(outputSlots.Except(node.GetOutputSlots<MaterialSlot>().Select(x => x.id)));
if (missingSlots.Count == 0)
return;
var toPrint = missingSlots.Select(x => x.ToString());
throw new SlotConfigurationException(string.Format("Missing slots {0} on node {1}", string.Join(", ", toPrint.ToArray()), node));
}
public static IEnumerable<IEdge> GetAllEdges(AbstractMaterialNode node)
{
var result = new List<IEdge>();
var validSlots = ListPool<MaterialSlot>.Get();
validSlots.AddRange(node.GetInputSlots<MaterialSlot>());
for (int index = 0; index < validSlots.Count; index++)
{
var inputSlot = validSlots[index];
result.AddRange(node.owner.GetEdges(inputSlot.slotReference));
}
validSlots.Clear();
validSlots.AddRange(node.GetOutputSlots<MaterialSlot>());
for (int index = 0; index < validSlots.Count; index++)
{
var outputSlot = validSlots[index];
result.AddRange(node.owner.GetEdges(outputSlot.slotReference));
}
ListPool<MaterialSlot>.Release(validSlots);
return result;
}
public static string GetDuplicateSafeNameForSlot(AbstractMaterialNode node, int slotId, string name)
{
List<MaterialSlot> slots = new List<MaterialSlot>();
node.GetSlots(slots);
name = name.Trim();
return GraphUtil.SanitizeName(slots.Where(p => p.id != slotId).Select(p => p.RawDisplayName()), "{0} ({1})", name);
}
// CollectNodesNodeFeedsInto looks at the current node and calculates
// which child nodes it depends on for it's calculation.
// Results are returned depth first so by processing each node in
// order you can generate a valid code block.
public enum IncludeSelf
{
Include,
Exclude
}
public static SlotReference DepthFirstCollectRedirectNodeFromNode(RedirectNodeData node)
{
var inputSlot = node.FindSlot<MaterialSlot>(RedirectNodeData.kInputSlotID);
foreach (var edge in node.owner.GetEdges(inputSlot.slotReference))
{
// get the input details
var outputSlotRef = edge.outputSlot;
var inputNode = outputSlotRef.node;
// If this is a redirect node we continue to look for the top one
if (inputNode is RedirectNodeData redirectNode)
{
return DepthFirstCollectRedirectNodeFromNode(redirectNode);
}
return outputSlotRef;
}
// If no edges it is the first redirect node without an edge going into it and we should return the slot ref
return node.GetSlotReference(RedirectNodeData.kInputSlotID);
}
public static void DepthFirstCollectNodesFromNode(List<AbstractMaterialNode> nodeList, AbstractMaterialNode node,
IncludeSelf includeSelf = IncludeSelf.Include, List<KeyValuePair<ShaderKeyword, int>> keywordPermutation = null, bool ignoreActiveState = false)
{
// no where to start
if (node == null)
return;
// already added this node
if (nodeList.Contains(node))
return;
IEnumerable<int> ids;
// If this node is a keyword node and we have an active keyword permutation
// The only valid port id is the port that corresponds to that keywords value in the active permutation
if (node is KeywordNode keywordNode && keywordPermutation != null)
{
var valueInPermutation = keywordPermutation.FirstOrDefault(x => x.Key == keywordNode.keyword);
ids = new int[] { keywordNode.GetSlotIdForPermutation(valueInPermutation) };
}
else
{
ids = node.GetInputSlots<MaterialSlot>().Select(x => x.id);
}
foreach (var slot in ids)
{
foreach (var edge in node.owner.GetEdges(node.GetSlotReference(slot)))
{
var outputNode = edge.outputSlot.node;
if (outputNode != null)
DepthFirstCollectNodesFromNode(nodeList, outputNode, keywordPermutation: keywordPermutation, ignoreActiveState: ignoreActiveState);
}
}
if (includeSelf == IncludeSelf.Include && (node.isActive || ignoreActiveState))
nodeList.Add(node);
}
internal static List<AbstractMaterialNode> GetParentNodes(AbstractMaterialNode node)
{
List<AbstractMaterialNode> nodeList = new List<AbstractMaterialNode>();
var ids = node.GetInputSlots<MaterialSlot>().Select(x => x.id);
foreach (var slot in ids)
{
if (node.owner == null)
break;
foreach (var edge in node.owner.GetEdges(node.FindSlot<MaterialSlot>(slot).slotReference))
{
var outputNode = ((Edge)edge).outputSlot.node;
if (outputNode != null)
{
nodeList.Add(outputNode);
}
}
}
return nodeList;
}
private static bool ActiveLeafExists(AbstractMaterialNode node)
{
//if our active state has been explicitly set to a value use it
switch (node.activeState)
{
case AbstractMaterialNode.ActiveState.Implicit:
break;
case AbstractMaterialNode.ActiveState.ExplicitInactive:
return false;
case AbstractMaterialNode.ActiveState.ExplicitActive:
return true;
}
List<AbstractMaterialNode> parentNodes = GetParentNodes(node);
//at this point we know we are not explicitly set to a state,
//so there is no reason to be inactive
if (parentNodes.Count == 0)
{
return true;
}
bool output = false;
foreach (var parent in parentNodes)
{
output |= ActiveLeafExists(parent);
if (output)
{
break;
}
}
return output;
}
private static List<AbstractMaterialNode> GetChildNodes(AbstractMaterialNode node)
{
List<AbstractMaterialNode> nodeList = new List<AbstractMaterialNode>();
var ids = node.GetOutputSlots<MaterialSlot>().Select(x => x.id);
foreach (var slot in ids)
{
foreach (var edge in node.owner.GetEdges(node.FindSlot<MaterialSlot>(slot).slotReference))
{
var inputNode = ((Edge)edge).inputSlot.node;
if (inputNode != null)
{
nodeList.Add(inputNode);
}
}
}
return nodeList;
}
private static bool ActiveRootExists(AbstractMaterialNode node)
{
//if our active state has been explicitly set to a value use it
switch (node.activeState)
{
case AbstractMaterialNode.ActiveState.Implicit:
break;
case AbstractMaterialNode.ActiveState.ExplicitInactive:
return false;
case AbstractMaterialNode.ActiveState.ExplicitActive:
return true;
}
List<AbstractMaterialNode> childNodes = GetChildNodes(node);
//at this point we know we are not explicitly set to a state,
//so there is no reason to be inactive
if (childNodes.Count == 0)
{
return true;
}
bool output = false;
foreach (var child in childNodes)
{
output |= ActiveRootExists(child);
if (output)
{
break;
}
}
return output;
}
private static void ActiveTreeExists(AbstractMaterialNode node, out bool activeLeaf, out bool activeRoot, out bool activeTree)
{
activeLeaf = ActiveLeafExists(node);
activeRoot = ActiveRootExists(node);
activeTree = activeRoot && activeLeaf;
}
//First pass check if node is now active after a change, so just check if there is a valid "tree" : a valid upstream input path,
// and a valid downstream output path, or "leaf" and "root". If this changes the node's active state, then anything connected may
// change as well, so update the "forrest" or all connectected trees of this nodes leaves.
// NOTE: I cannot think if there is any case where the entirety of the connected graph would need to change, but if there are bugs
// on certain nodes farther away from the node not updating correctly, a possible solution may be to get the entirety of the connected
// graph instead of just what I have declared as the "local" connected graph
public static void ReevaluateActivityOfConnectedNodes(AbstractMaterialNode node, PooledHashSet<AbstractMaterialNode> changedNodes = null)
{
List<AbstractMaterialNode> forest = GetForest(node);
ReevaluateActivityOfNodeList(forest, changedNodes);
}
public static void ReevaluateActivityOfNodeList(IEnumerable<AbstractMaterialNode> nodes, PooledHashSet<AbstractMaterialNode> changedNodes = null)
{
bool getChangedNodes = changedNodes != null;
foreach (AbstractMaterialNode n in nodes)
{
if (n.activeState != AbstractMaterialNode.ActiveState.Implicit)
continue;
ActiveTreeExists(n, out _, out _, out bool at);
if (n.isActive != at && getChangedNodes)
{
changedNodes.Add(n);
}
n.SetActive(at, false);
}
}
//Go to the leaves of the node, then get all trees with those leaves
private static List<AbstractMaterialNode> GetForest(AbstractMaterialNode node)
{
List<AbstractMaterialNode> leaves = GetLeaves(node);
List<AbstractMaterialNode> forrest = new List<AbstractMaterialNode>();
foreach (var leaf in leaves)
{
if (!forrest.Contains(leaf))
{
forrest.Add(leaf);
}
foreach (var child in GetChildNodesRecursive(leaf))
{
if (!forrest.Contains(child))
{
forrest.Add(child);
}
}
}
return forrest;
}
private static List<AbstractMaterialNode> GetChildNodesRecursive(AbstractMaterialNode node)
{
List<AbstractMaterialNode> output = new List<AbstractMaterialNode>() { node };
List<AbstractMaterialNode> children = GetChildNodes(node);
foreach (var child in children)
{
if (!output.Contains(child))
{
output.Add(child);
}
foreach (var descendent in GetChildNodesRecursive(child))
{
if (!output.Contains(descendent))
{
output.Add(descendent);
}
}
}
return output;
}
private static List<AbstractMaterialNode> GetLeaves(AbstractMaterialNode node)
{
List<AbstractMaterialNode> parents = GetParentNodes(node);
List<AbstractMaterialNode> output = new List<AbstractMaterialNode>();
if (parents.Count == 0)
{
output.Add(node);
}
else
{
foreach (var parent in parents)
{
foreach (var leaf in GetLeaves(parent))
{
if (!output.Contains(leaf))
{
output.Add(leaf);
}
}
}
}
return output;
}
public static void GetDownsteamNodesForNode(List<AbstractMaterialNode> nodeList, AbstractMaterialNode node)
{
// no where to start
if (node == null)
return;
// Recursively traverse downstream from the original node
// Traverse down each edge and continue on any connected downstream nodes
// Only nodes with no nodes further downstream are added to node list
bool hasDownstream = false;
var ids = node.GetOutputSlots<MaterialSlot>().Select(x => x.id);
foreach (var slot in ids)
{
foreach (var edge in node.owner.GetEdges(node.FindSlot<MaterialSlot>(slot).slotReference))
{
var inputNode = ((Edge)edge).inputSlot.node;
if (inputNode != null)
{
hasDownstream = true;
GetDownsteamNodesForNode(nodeList, inputNode);
}
}
}
// No more nodes downstream from here
if (!hasDownstream)
nodeList.Add(node);
}
public static void CollectNodeSet(HashSet<AbstractMaterialNode> nodeSet, MaterialSlot slot)
{
var node = slot.owner;
var graph = node.owner;
foreach (var edge in graph.GetEdges(node.GetSlotReference(slot.id)))
{
var outputNode = edge.outputSlot.node;
if (outputNode != null)
{
CollectNodeSet(nodeSet, outputNode);
}
}
}
public static void CollectNodeSet(HashSet<AbstractMaterialNode> nodeSet, AbstractMaterialNode node)
{
if (!nodeSet.Add(node))
{
return;
}
using (ListPool<MaterialSlot>.Get(out var slots))
{
node.GetInputSlots(slots);
foreach (var slot in slots)
{
CollectNodeSet(nodeSet, slot);
}
}
}
public static void CollectNodesNodeFeedsInto(List<AbstractMaterialNode> nodeList, AbstractMaterialNode node, IncludeSelf includeSelf = IncludeSelf.Include)
{
if (node == null)
return;
if (nodeList.Contains(node))
return;
foreach (var slot in node.GetOutputSlots<MaterialSlot>())
{
foreach (var edge in node.owner.GetEdges(slot.slotReference))
{
var inputNode = edge.inputSlot.node;
CollectNodesNodeFeedsInto(nodeList, inputNode);
}
}
if (includeSelf == IncludeSelf.Include)
nodeList.Add(node);
}
public static string GetDocumentationString(string pageName)
{
return Documentation.GetPageLink(pageName.Replace(" ", "-") + NodeDocSuffix);
}
static Stack<MaterialSlot> s_SlotStack = new Stack<MaterialSlot>();
public static ShaderStage GetEffectiveShaderStage(MaterialSlot initialSlot, bool goingBackwards)
{
var graph = initialSlot.owner.owner;
s_SlotStack.Clear();
s_SlotStack.Push(initialSlot);
while (s_SlotStack.Count > 0)
{
var slot = s_SlotStack.Pop();
ShaderStage stage;
if (slot.stageCapability.TryGetShaderStage(out stage))
return stage;
if (goingBackwards && slot.isInputSlot)
{
foreach (var edge in graph.GetEdges(slot.slotReference))
{
var node = edge.outputSlot.node;
s_SlotStack.Push(node.FindOutputSlot<MaterialSlot>(edge.outputSlot.slotId));
}
}
else if (!goingBackwards && slot.isOutputSlot)
{
foreach (var edge in graph.GetEdges(slot.slotReference))
{
var node = edge.inputSlot.node;
s_SlotStack.Push(node.FindInputSlot<MaterialSlot>(edge.inputSlot.slotId));
}
}
else
{
var ownerSlots = Enumerable.Empty<MaterialSlot>();
if (goingBackwards && slot.isOutputSlot)
ownerSlots = slot.owner.GetInputSlots<MaterialSlot>(slot);
else if (!goingBackwards && slot.isInputSlot)
ownerSlots = slot.owner.GetOutputSlots<MaterialSlot>(slot);
foreach (var ownerSlot in ownerSlots)
s_SlotStack.Push(ownerSlot);
}
}
// We default to fragment shader stage if all connected nodes were compatible with both.
return ShaderStage.Fragment;
}
public static ShaderStageCapability GetEffectiveShaderStageCapability(MaterialSlot initialSlot, bool goingBackwards)
{
var graph = initialSlot.owner.owner;
s_SlotStack.Clear();
s_SlotStack.Push(initialSlot);
ShaderStageCapability capabilities = ShaderStageCapability.All;
while (s_SlotStack.Count > 0)
{
var slot = s_SlotStack.Pop();
// Clear any stages from the total capabilities that this slot doesn't support (e.g. if this is vertex, clear pixel)
capabilities &= slot.stageCapability;
// Can early out if we know nothing is compatible, otherwise we have to keep checking everything we can reach.
if (capabilities == ShaderStageCapability.None)
return capabilities;
if (goingBackwards && slot.isInputSlot)
{
foreach (var edge in graph.GetEdges(slot.slotReference))
{
var node = edge.outputSlot.node;
s_SlotStack.Push(node.FindOutputSlot<MaterialSlot>(edge.outputSlot.slotId));
}
}
else if (!goingBackwards && slot.isOutputSlot)
{
foreach (var edge in graph.GetEdges(slot.slotReference))
{
var node = edge.inputSlot.node;
s_SlotStack.Push(node.FindInputSlot<MaterialSlot>(edge.inputSlot.slotId));
}
}
else
{
var ownerSlots = Enumerable.Empty<MaterialSlot>();
if (goingBackwards && slot.isOutputSlot)
ownerSlots = slot.owner.GetInputSlots<MaterialSlot>(slot);
else if (!goingBackwards && slot.isInputSlot)
ownerSlots = slot.owner.GetOutputSlots<MaterialSlot>(slot);
foreach (var ownerSlot in ownerSlots)
s_SlotStack.Push(ownerSlot);
}
}
return capabilities;
}
public static string GetSlotDimension(ConcreteSlotValueType slotValue)
{
switch (slotValue)
{
case ConcreteSlotValueType.Vector1:
return String.Empty;
case ConcreteSlotValueType.Vector2:
return "2";
case ConcreteSlotValueType.Vector3:
return "3";
case ConcreteSlotValueType.Vector4:
return "4";
case ConcreteSlotValueType.Matrix2:
return "2x2";
case ConcreteSlotValueType.Matrix3:
return "3x3";
case ConcreteSlotValueType.Matrix4:
return "4x4";
case ConcreteSlotValueType.PropertyConnectionState:
return String.Empty;
default:
return "Error";
}
}
// NOTE: there are several bugs here.. we should use ConvertToValidHLSLIdentifier() instead
public static string GetHLSLSafeName(string input)
{
char[] arr = input.ToCharArray();
arr = Array.FindAll<char>(arr, (c => (Char.IsLetterOrDigit(c))));
var safeName = new string(arr);
if (safeName.Length > 1 && char.IsDigit(safeName[0]))
{
safeName = $"var{safeName}";
}
return safeName;
}
static readonly string[] k_HLSLNumericKeywords =
{
"float",
"half", // not technically in HLSL spec, but prob should be
"real", // Unity thing, but included here
"int",
"uint",
"bool",
"min10float",
"min16float",
"min12int",
"min16int",
"min16uint"
};
static readonly string[] k_HLSLNumericKeywordSuffixes =
{
"",
"1", "2", "3", "4",
"1x1", "1x2", "1x3", "1x4",
"2x1", "2x2", "2x3", "2x4",
"3x1", "3x2", "3x3", "3x4",
"4x1", "4x2", "4x3", "4x4"
};
static HashSet<string> m_ShaderLabKeywords = new HashSet<string>()
{
// these should all be lowercase, as shaderlab keywords are case insensitive
"properties",
"range",
"bind",
"bindchannels",
"tags",
"lod",
"shader",
"subshader",
"category",
"fallback",
"dependency",
"customeditor",
"rect",
"any",
"float",
"color",
"int",
"integer",
"vector",
"matrix",
"2d",
"cube",
"3d",
"2darray",
"cubearray",
"name",
"settexture",
"true",
"false",
"on",
"off",
"separatespecular",
"offset",
"zwrite",
"zclip",
"conservative",
"ztest",
"alphatest",
"fog",
"stencil",
"colormask",
"alphatomask",
"cull",
"front",
"material",
"ambient",
"diffuse",
"specular",
"emission",
"shininess",
"blend",
"blendop",
"colormaterial",
"lighting",
"pass",
"grabpass",
"usepass",
"gpuprogramid",
"add",
"sub",
"revsub",
"min",
"max",
"logicalclear",
"logicalset",
"logicalcopy",
"logicalcopyinverted",
"logicalnoop",
"logicalinvert",
"logicaland",
"logicalnand",
"logicalor",
"logicalnor",
"logicalxor",
"logicalequiv",
"logicalandreverse",
"logicalandinverted",
"logicalorreverse",
"logicalorinverted",
"multiply",
"screen",
"overlay",
"darken",
"lighten",
"colordodge",
"colorburn",
"hardlight",
"softlight",
"difference",
"exclusion",
"hslhue",
"hslsaturation",
"hslcolor",
"hslluminosity",
"zero",
"one",
"dstcolor",
"srccolor",
"oneminusdstcolor",
"srcalpha",
"oneminussrccolor",
"dstalpha",
"oneminusdstalpha",
"srcalphasaturate",
"oneminussrcalpha",
"constantcolor",
"oneminusconstantcolor",
"constantalpha",
"oneminusconstantalpha",
};
static HashSet<string> m_HLSLKeywords = new HashSet<string>()
{
"AppendStructuredBuffer",
"asm",
"asm_fragment",
"auto",
"BlendState",
"break",
"Buffer",
"ByteAddressBuffer",
"case",
"catch",
"cbuffer",
"centroid",
"char",
"class",
"column_major",
"compile",
"compile_fragment",
"CompileShader",
"const",
"const_cast",
"continue",
"ComputeShader",
"ConsumeStructuredBuffer",
"default",
"delete",
"DepthStencilState",
"DepthStencilView",
"discard",
"do",
"double",
"DomainShader",
"dynamic_cast",
"dword",
"else",
"enum",
"explicit",
"export",
"extern",
"false",
"for",
"friend",
"fxgroup",
"GeometryShader",
"goto",
"groupshared",
"half",
"Hullshader",
"if",
"in",
"inline",
"inout",
"InputPatch",
"interface",
"line",
"lineadj",
"linear",
"LineStream",
"long",
"matrix",
"mutable",
"namespace",
"new",
"nointerpolation",
"noperspective",
"NULL",
"operator",
"out",
"OutputPatch",
"packoffset",
"pass",
"pixelfragment",
"PixelShader",
"point",
"PointStream",
"precise",
"private",
"protected",
"public",
"RasterizerState",
"reinterpret_cast",
"RenderTargetView",
"return",
"register",
"row_major",
"RWBuffer",
"RWByteAddressBuffer",
"RWStructuredBuffer",
"RWTexture1D",
"RWTexture1DArray",
"RWTexture2D",
"RWTexture2DArray",
"RWTexture3D",
"sample",
"sampler",
"SamplerState",
"SamplerComparisonState",
"shared",
"short",
"signed",
"sizeof",
"snorm",
"stateblock",
"stateblock_state",
"static",
"static_cast",
"string",
"struct",
"switch",
"StructuredBuffer",
"tbuffer",
"technique",
"technique10",
"technique11",
"template",
"texture",
"Texture1D",
"Texture1DArray",
"Texture2D",
"Texture2DArray",
"Texture2DMS",
"Texture2DMSArray",
"Texture3D",
"TextureCube",
"TextureCubeArray",
"this",
"throw",
"true",
"try",
"typedef",
"typename",
"triangle",
"triangleadj",
"TriangleStream",
"uniform",
"unorm",
"union",
"unsigned",
"using",
"vector",
"vertexfragment",
"VertexShader",
"virtual",
"void",
"volatile",
"while"
};
static HashSet<string> m_ShaderGraphKeywords = new HashSet<string>()
{
"Gradient",
"UnitySamplerState",
"UnityTexture2D",
"UnityTexture2DArray",
"UnityTexture3D",
"UnityTextureCube"
};
static bool m_HLSLKeywordDictionaryBuilt = false;
public static bool IsHLSLKeyword(string id)
{
if (!m_HLSLKeywordDictionaryBuilt)
{
foreach (var numericKeyword in k_HLSLNumericKeywords)
foreach (var suffix in k_HLSLNumericKeywordSuffixes)
m_HLSLKeywords.Add(numericKeyword + suffix);
m_HLSLKeywordDictionaryBuilt = true;
}
bool isHLSLKeyword = m_HLSLKeywords.Contains(id);
return isHLSLKeyword;
}
public static bool IsShaderLabKeyWord(string id)
{
bool isShaderLabKeyword = m_ShaderLabKeywords.Contains(id.ToLower());
return isShaderLabKeyword;
}
public static bool IsShaderGraphKeyWord(string id)
{
bool isShaderGraphKeyword = m_ShaderGraphKeywords.Contains(id);
return isShaderGraphKeyword;
}
public static string ConvertToValidHLSLIdentifier(string originalId, Func<string, bool> isDisallowedIdentifier = null)
{
// Converts " 1 var * q-30 ( 0 ) (1) " to "_1_var_q_30_0_1"
if (originalId == null)
originalId = "";
StringBuilder hlslId = new StringBuilder(originalId.Length);
bool lastInvalid = false;
for (int i = 0; i < originalId.Length; i++)
{
char c = originalId[i];
bool isLetter = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
bool isUnderscore = (c == '_');
bool isDigit = (c >= '0' && c <= '9');
bool validChar = isLetter || isUnderscore || isDigit;
if (!validChar)
{
// when we see an invalid character, we just record that we saw it and go to the next character
// this way we combine multiple invalid characters, and trailing ones just get dropped
lastInvalid = true;
}
else
{
// whenever we hit a valid character
// if the last character was invalid, append an underscore
// unless we're at the beginning of the string
if (lastInvalid && (hlslId.Length > 0))
hlslId.Append("_");
// HLSL ids can't start with a digit, prepend an underscore to prevent this
if (isDigit && (hlslId.Length == 0))
hlslId.Append("_");
hlslId.Append(c);
lastInvalid = false;
}
}
// empty strings not allowed -- append an underscore if it's empty
if (hlslId.Length <= 0)
hlslId.Append("_");
var result = hlslId.ToString();
while (IsHLSLKeyword(result))
result = "_" + result;
if (isDisallowedIdentifier != null)
while (isDisallowedIdentifier(result))
result = "_" + result;
return result;
}
private static string GetDisplaySafeName(string input)
{
//strip valid display characters from slot name
//current valid characters are whitespace and ( ) _ separators
StringBuilder cleanName = new StringBuilder();
foreach (var c in input)
{
if (c != ' ' && c != '(' && c != ')' && c != '_')
cleanName.Append(c);
}
return cleanName.ToString();
}
public static bool ValidateSlotName(string inName, out string errorMessage)
{
//check for invalid characters between display safe and hlsl safe name
if (GetDisplaySafeName(inName) != GetHLSLSafeName(inName) && GetDisplaySafeName(inName) != ConvertToValidHLSLIdentifier(inName))
{
errorMessage = "Slot name(s) found invalid character(s). Valid characters: A-Z, a-z, 0-9, _ ( ) ";
return true;
}
//if clean, return null and false
errorMessage = null;
return false;
}
public static string FloatToShaderValue(float value)
{
if (Single.IsPositiveInfinity(value))
{
return "1.#INF";
}
if (Single.IsNegativeInfinity(value))
{
return "-1.#INF";
}
if (Single.IsNaN(value))
{
return "NAN";
}
return value.ToString(CultureInfo.InvariantCulture);
}
// A number large enough to become Infinity (~FLOAT_MAX_VALUE * 10) + explanatory comment
private const string k_ShaderLabInfinityAlternatrive = "3402823500000000000000000000000000000000 /* Infinity */";
// ShaderLab doesn't support Scientific Notion nor Infinity. To stop from generating a broken shader we do this.