Skip to content

Commit b49fb82

Browse files
committed
added support for deduplicating shader tags, closes #72
1 parent 63d10bd commit b49fb82

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

Diff for: Packages/sh.orels.shaders.generator/Editor/ShaderDefinitionImporter.cs

+39-1
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,9 @@ private List<ShaderBlock> OptimizeBlocks(List<ShaderBlock> sourceBlocks)
664664
var block = collapsedBlocks[i];
665665
switch (block.Name)
666666
{
667+
case "%ShaderTags":
668+
block.Contents = DeDuplicateByParser(block.Contents, DeDupeType.Tags);
669+
continue;
667670
case "%Properties":
668671
collapsedBlocks[i].Contents = DeDuplicateByParser(block.Contents, DeDupeType.Properties);
669672
continue;
@@ -691,7 +694,8 @@ private List<ShaderBlock> OptimizeBlocks(List<ShaderBlock> sourceBlocks)
691694

692695
private enum DeDupeType
693696
{
694-
Properties
697+
Properties,
698+
Tags,
695699
}
696700

697701
private List<string> DeDuplicateByParser(List<string> source, DeDupeType type)
@@ -721,6 +725,40 @@ private List<string> DeDuplicateByParser(List<string> source, DeDupeType type)
721725
}
722726
break;
723727
}
728+
case DeDupeType.Tags:
729+
{
730+
var dedupedTags = new Dictionary<string, string>();
731+
var dedupedTagsString = new StringBuilder();
732+
combined = $"Tags {{{combined}}}";
733+
var tokens = ShaderLabLexer.Lex(combined, null, null, false, out _);
734+
var nodes = ShaderLabParser.ParseShaderLabCommands(tokens, ShaderAnalyzers.SLConfig, out _);
735+
foreach (var node in nodes)
736+
{
737+
if (node is not ShaderLabCommandTagsNode tags) continue;
738+
foreach (var tag in tags.Tags)
739+
{
740+
tag.Deconstruct(out var tagKey, out var tagValue);
741+
if (keySet.Contains(tagKey))
742+
{
743+
if (debugBuild)
744+
{
745+
Debug.LogWarning("Found duplicate tag, updating: " + tagKey + " to " + tagValue);
746+
}
747+
dedupedTags[tagKey] = tagValue;
748+
continue;
749+
}
750+
keySet.Add(tagKey);
751+
dedupedTags.Add(tagKey, tagValue);
752+
}
753+
}
754+
755+
foreach (var (key, value) in dedupedTags)
756+
{
757+
dedupedTagsString.Append($"\"{key}\" = \"{value}\" ");
758+
}
759+
deduped.Add(dedupedTagsString.ToString());
760+
break;
761+
}
724762
}
725763
return deduped;
726764
}

0 commit comments

Comments
 (0)