SCShader.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. using UnityEngine;
  2. using System.Text;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace ShaderControl {
  6. public enum PragmaType {
  7. Unknown,
  8. MultiCompileGlobal,
  9. MultiCompileLocal,
  10. FeatureGlobal,
  11. FeatureLocal
  12. }
  13. public class SCShader {
  14. public string fullName = "";
  15. public string name = "";
  16. public string path = "";
  17. public int GUID;
  18. public List<SCShaderPass> passes = new List<SCShaderPass>();
  19. public List<SCKeyword> keywords = new List<SCKeyword>();
  20. public List<SCMaterial> materials = new List<SCMaterial>();
  21. public int totalVariantCount, actualBuildVariantCount;
  22. public int keywordEnabledCount;
  23. public bool foldout;
  24. public bool showMaterials;
  25. public bool pendingChanges;
  26. public bool editedByShaderControl;
  27. public bool hasBackup;
  28. public bool isReadOnly;
  29. public bool isShaderGraph;
  30. public int shaderGraphVersion = 1;
  31. public void Add(SCShaderPass pass) {
  32. passes.Add(pass);
  33. UpdateKeywords();
  34. SortKeywords();
  35. }
  36. public void AddKeywordsByName(string[] names) {
  37. bool changes = false;
  38. for (int k = 0; k < names.Length; k++) {
  39. string kwName = names[k];
  40. if (string.IsNullOrEmpty(kwName)) continue;
  41. // is it already included?
  42. int kwCount = keywords.Count;
  43. bool repeated = false;
  44. for (int j = 0; j < kwCount; j++) {
  45. if (keywords[j].name.Equals(kwName)) {
  46. repeated = true;
  47. break;
  48. }
  49. }
  50. if (repeated) continue;
  51. SCKeyword keyword = new SCKeyword(kwName);
  52. keywords.Add(keyword);
  53. changes = true;
  54. }
  55. if (changes) {
  56. SortKeywords();
  57. }
  58. }
  59. void SortKeywords() {
  60. keywords.Sort(delegate (SCKeyword k1, SCKeyword k2) { return k1.name.CompareTo(k2.name); });
  61. }
  62. public void RemoveKeyword(string name) {
  63. for (int k = 0; k < keywords.Count; k++) {
  64. SCKeyword keyword = keywords[k];
  65. if (keyword.name.Equals(name)) {
  66. if (keyword.enabled) {
  67. keywordEnabledCount--;
  68. }
  69. keywords.Remove(keyword);
  70. return;
  71. }
  72. }
  73. }
  74. public void EnableKeywords() {
  75. keywords.ForEach((SCKeyword keyword) => keyword.enabled = true);
  76. }
  77. public List<string> enabledKeywords {
  78. get {
  79. List<string> kk = new List<string>(keywords.Count);
  80. keywords.ForEach(kw => { if (kw.enabled) kk.Add(kw.name); });
  81. return kk;
  82. }
  83. }
  84. public bool hasSource {
  85. get { return path.Length > 0; }
  86. }
  87. void UpdateKeywords() {
  88. passes.ForEach((SCShaderPass pass) => {
  89. for (int l = 0; l < pass.keywordLines.Count; l++) {
  90. SCKeywordLine line = pass.keywordLines[l];
  91. for (int k = 0; k < line.keywords.Count; k++) {
  92. SCKeyword keyword = line.keywords[k];
  93. if (!keywords.Contains(keyword)) {
  94. keywords.Add(keyword);
  95. }
  96. }
  97. }
  98. });
  99. }
  100. public void UpdateVariantCount() {
  101. totalVariantCount = 0;
  102. actualBuildVariantCount = 0;
  103. passes.ForEach((SCShaderPass pass) => {
  104. int matCount = materials.Count;
  105. int passCount = 1;
  106. int passBuildCount = 1;
  107. int passKeywordLinesCount = pass.keywordLines.Count;
  108. for (int l = 0; l < passKeywordLinesCount; l++) {
  109. SCKeywordLine line = pass.keywordLines[l];
  110. int kLineEnabledCount = line.hasUnderscoreVariant ? 1 : 0;
  111. int kLineCount = kLineEnabledCount;
  112. int lineKeywordsCount = line.keywords.Count;
  113. for (int k = 0; k < lineKeywordsCount; k++) {
  114. SCKeyword keyword = line.keywords[k];
  115. // if this is a shader feature, first check if there's at least one material using it
  116. if (line.pragmaType == PragmaType.FeatureGlobal || line.pragmaType == PragmaType.FeatureLocal) {
  117. bool materialUsesKeyword = false;
  118. for (int m = 0; m < matCount; m++) {
  119. if (materials[m].ContainsKeyword(keyword.name)) {
  120. materialUsesKeyword = true;
  121. break;
  122. }
  123. }
  124. if (!materialUsesKeyword) continue;
  125. }
  126. kLineCount++;
  127. if (keyword.enabled) {
  128. kLineEnabledCount++;
  129. }
  130. }
  131. if (kLineEnabledCount > 0) {
  132. passBuildCount *= kLineEnabledCount;
  133. }
  134. passCount *= kLineCount;
  135. }
  136. totalVariantCount += passCount;
  137. actualBuildVariantCount += passBuildCount;
  138. });
  139. keywordEnabledCount = 0;
  140. int keywordCount = keywords.Count;
  141. for (int k = 0; k < keywordCount; k++) {
  142. if (keywords[k].enabled)
  143. keywordEnabledCount++;
  144. }
  145. }
  146. public SCKeyword GetKeyword(string name) {
  147. int kCount = keywords.Count;
  148. for (int k = 0; k < kCount; k++) {
  149. SCKeyword keyword = keywords[k];
  150. if (keyword.name.Equals(name))
  151. return keyword;
  152. }
  153. return new SCKeyword(name);
  154. }
  155. public static string GetSimpleName(string longName) {
  156. int k = longName.LastIndexOf("/");
  157. if (k >= 0) {
  158. return longName.Substring(k + 1);
  159. }
  160. return longName;
  161. }
  162. }
  163. public class SCShaderPass {
  164. public int pass;
  165. public List<SCKeywordLine> keywordLines = new List<SCKeywordLine>();
  166. public int keywordCount;
  167. public void Add(SCKeywordLine keywordLine) {
  168. keywordLines.Add(keywordLine);
  169. UpdateKeywordCount();
  170. }
  171. public void Add(List<SCKeywordLine> keywordLines) {
  172. this.keywordLines.AddRange(keywordLines);
  173. UpdateKeywordCount();
  174. }
  175. void UpdateKeywordCount() {
  176. keywordCount = 0;
  177. keywordLines.ForEach((SCKeywordLine obj) => keywordCount += obj.keywordCount);
  178. }
  179. public void Clear() {
  180. keywordCount = 0;
  181. keywordLines.Clear();
  182. }
  183. }
  184. public class SCKeywordLine {
  185. public const string PRAGMA_MULTICOMPILE_GLOBAL = "#pragma multi_compile ";
  186. public const string PRAGMA_MULTICOMPILE_LOCAL = "#pragma multi_compile_local ";
  187. public const string PRAGMA_FEATURE_GLOBAL = "#pragma shader_feature ";
  188. public const string PRAGMA_FEATURE_LOCAL = "#pragma shader_feature_local ";
  189. public List<SCKeyword> keywords = new List<SCKeyword>();
  190. public bool hasUnderscoreVariant;
  191. public PragmaType pragmaType;
  192. public string GetPragma() {
  193. switch (pragmaType) {
  194. case PragmaType.MultiCompileGlobal: return PRAGMA_MULTICOMPILE_GLOBAL;
  195. case PragmaType.MultiCompileLocal: return PRAGMA_MULTICOMPILE_LOCAL;
  196. case PragmaType.FeatureGlobal: return PRAGMA_FEATURE_GLOBAL;
  197. case PragmaType.FeatureLocal: return PRAGMA_FEATURE_LOCAL;
  198. }
  199. return "";
  200. }
  201. public SCKeyword GetKeyword(string name) {
  202. int kc = keywords.Count;
  203. for (int k = 0; k < kc; k++) {
  204. SCKeyword keyword = keywords[k];
  205. if (keyword.name.Equals(name)) {
  206. return keyword;
  207. }
  208. }
  209. return null;
  210. }
  211. public void Add(SCKeyword keyword, int lineNumber = 0) {
  212. if (GetKeyword(keyword.name) != null)
  213. return;
  214. // ignore underscore keywords
  215. bool goodKeyword = false;
  216. for (int k = 0; k < keyword.name.Length; k++) {
  217. if (keyword.name[k] != '_') {
  218. goodKeyword = true;
  219. break;
  220. }
  221. }
  222. keyword.isMultiCompile = pragmaType == PragmaType.MultiCompileGlobal || pragmaType == PragmaType.MultiCompileLocal;
  223. if (goodKeyword) {
  224. keyword.isGlobal = pragmaType != PragmaType.FeatureLocal && pragmaType != PragmaType.MultiCompileLocal && pragmaType != PragmaType.Unknown;
  225. keyword.verboseName = keyword.name + " (";
  226. if (!string.IsNullOrEmpty(keyword.shaderGraphName)) {
  227. keyword.verboseName += keyword.shaderGraphName + ", ";
  228. }
  229. keyword.verboseName += keyword.isMultiCompile ? "multi_compile" : "shader_feature";
  230. keyword.verboseName += keyword.isGlobal ? ", global)" : ", local)";
  231. keyword.lineNumber = lineNumber;
  232. keywords.Add(keyword);
  233. } else {
  234. keyword.isUnderscoreKeyword = true;
  235. hasUnderscoreVariant = true;
  236. }
  237. }
  238. public void DisableKeywords() {
  239. keywords.ForEach((SCKeyword obj) => obj.enabled = false);
  240. }
  241. public void Clear() {
  242. keywords.Clear();
  243. }
  244. public int keywordCount {
  245. get {
  246. return keywords.Count;
  247. }
  248. }
  249. public int keywordsEnabledCount {
  250. get {
  251. int kCount = keywords.Count;
  252. int enabledCount = 0;
  253. for (int k = 0; k < kCount; k++) {
  254. if (keywords[k].enabled)
  255. enabledCount++;
  256. }
  257. return enabledCount;
  258. }
  259. }
  260. public override string ToString() {
  261. StringBuilder sb = new StringBuilder();
  262. for (int k = 0; k < keywords.Count; k++) {
  263. if (k > 0)
  264. sb.Append(" ");
  265. sb.Append(keywords[k].name);
  266. }
  267. return sb.ToString();
  268. }
  269. }
  270. public class SCKeyword {
  271. public string name, verboseName;
  272. public string shaderGraphName;
  273. public string shaderGraphObjectId;
  274. public bool enabled;
  275. public bool isUnderscoreKeyword;
  276. public bool isGlobal = true;
  277. public bool isMultiCompile = true;
  278. public bool canBeConvertedToLocal;
  279. public bool canBeModified;
  280. public int lineNumber;
  281. public SCKeyword(string name, string shaderGraphName = null, string shaderGraphObjectId = null) {
  282. this.name = name;
  283. this.verboseName = name;
  284. this.shaderGraphName = shaderGraphName;
  285. this.shaderGraphObjectId = shaderGraphObjectId;
  286. enabled = true;
  287. }
  288. public override bool Equals(object obj) {
  289. if (obj is SCKeyword) {
  290. SCKeyword other = (SCKeyword)obj;
  291. return name.Equals(other.name);
  292. }
  293. return false;
  294. }
  295. public override int GetHashCode() {
  296. return name.GetHashCode();
  297. }
  298. public override string ToString() {
  299. return name;
  300. }
  301. }
  302. }