暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShaderSpliceUtil.cs 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using UnityEditor.ShaderGraph.Internal;
  7. using UnityEngine.Profiling;
  8. namespace UnityEditor.ShaderGraph
  9. {
  10. static class ShaderSpliceUtil
  11. {
  12. private static char[] channelNames =
  13. { 'x', 'y', 'z', 'w' };
  14. private static char[] whitespace =
  15. { ' ', '\t', '\r', '\n', '\f'};
  16. public static string GetChannelSwizzle(int firstChannel, int channelCount)
  17. {
  18. System.Text.StringBuilder result = new System.Text.StringBuilder();
  19. int lastChannel = System.Math.Min(firstChannel + channelCount - 1, 4);
  20. for (int index = firstChannel; index <= lastChannel; index++)
  21. {
  22. result.Append(channelNames[index]);
  23. }
  24. return result.ToString();
  25. }
  26. // returns the offset of the first non-whitespace character, in the range [start, end] inclusive ... will return end if none found
  27. private static int SkipWhitespace(string str, int start, int end)
  28. {
  29. int index = start;
  30. while (index < end)
  31. {
  32. char c = str[index];
  33. if (!whitespace.Contains(c))
  34. {
  35. break;
  36. }
  37. index++;
  38. }
  39. return index;
  40. }
  41. public class TemplatePreprocessor
  42. {
  43. // inputs
  44. ActiveFields activeFields;
  45. Dictionary<string, string> namedFragments;
  46. string[] templatePaths;
  47. bool isDebug;
  48. // intermediates
  49. HashSet<string> includedFiles;
  50. // outputs
  51. ShaderStringBuilder result;
  52. AssetCollection assetCollection;
  53. public TemplatePreprocessor(ActiveFields activeFields, Dictionary<string, string> namedFragments, bool isDebug, string[] templatePaths, AssetCollection assetCollection, bool humanReadable, ShaderStringBuilder outShaderCodeResult = null)
  54. {
  55. this.activeFields = activeFields;
  56. this.namedFragments = namedFragments;
  57. this.isDebug = isDebug;
  58. this.templatePaths = templatePaths;
  59. this.assetCollection = assetCollection;
  60. this.result = outShaderCodeResult ?? new ShaderStringBuilder(humanReadable: humanReadable);
  61. includedFiles = new HashSet<string>();
  62. }
  63. public ShaderStringBuilder GetShaderCode()
  64. {
  65. return result;
  66. }
  67. public void ProcessTemplateFile(string filePath)
  68. {
  69. if (File.Exists(filePath) &&
  70. !includedFiles.Contains(filePath))
  71. {
  72. includedFiles.Add(filePath);
  73. if (assetCollection != null)
  74. {
  75. GUID guid = AssetDatabase.GUIDFromAssetPath(filePath);
  76. if (!guid.Empty())
  77. assetCollection.AddAssetDependency(guid, AssetCollection.Flags.SourceDependency);
  78. }
  79. string[] templateLines = File.ReadAllLines(filePath);
  80. foreach (string line in templateLines)
  81. {
  82. ProcessTemplateLine(line, 0, line.Length);
  83. }
  84. }
  85. }
  86. private struct Token
  87. {
  88. public string s;
  89. public int start;
  90. public int end;
  91. public Token(string s, int start, int end)
  92. {
  93. this.s = s;
  94. this.start = start;
  95. this.end = end;
  96. }
  97. public static Token Invalid()
  98. {
  99. return new Token(null, 0, 0);
  100. }
  101. public bool IsValid()
  102. {
  103. return (s != null);
  104. }
  105. public bool Is(string other)
  106. {
  107. int len = end - start;
  108. return (other.Length == len) && (0 == string.CompareOrdinal(s, start, other, 0, len));
  109. }
  110. public string GetString()
  111. {
  112. int len = end - start;
  113. if (len > 0)
  114. {
  115. return s.Substring(start, end - start);
  116. }
  117. return null;
  118. }
  119. }
  120. public void ProcessTemplateLine(string line, int start, int end)
  121. {
  122. bool appendEndln = true;
  123. int cur = start;
  124. while (cur < end)
  125. {
  126. // find an escape code '$'
  127. int dollar = line.IndexOf('$', cur, end - cur);
  128. if (dollar < 0)
  129. {
  130. // no escape code found in the remaining code -- just append the rest verbatim
  131. AppendSubstring(line, cur, true, end, false);
  132. break;
  133. }
  134. else
  135. {
  136. // found $ escape sequence
  137. Token command = ParseIdentifier(line, dollar + 1, end);
  138. if (!command.IsValid())
  139. {
  140. Error("ERROR: $ must be followed by a command string (if, splice, or include)", line, dollar + 1);
  141. break;
  142. }
  143. else
  144. {
  145. if (command.Is("include"))
  146. {
  147. ProcessIncludeCommand(command, end);
  148. appendEndln = false;
  149. break; // include command always ignores the rest of the line, error or not
  150. }
  151. else if (command.Is("splice"))
  152. {
  153. if (!ProcessSpliceCommand(command, end, ref cur))
  154. {
  155. // error, skip the rest of the line
  156. break;
  157. }
  158. }
  159. else
  160. {
  161. // let's see if it is a predicate
  162. Token predicate = ParseUntil(line, dollar + 1, end, ':');
  163. if (!predicate.IsValid())
  164. {
  165. Error("ERROR: unrecognized command: " + command.GetString(), line, command.start);
  166. break;
  167. }
  168. else
  169. {
  170. if (!ProcessPredicate(predicate, end, ref cur, ref appendEndln))
  171. {
  172. break; // skip the rest of the line
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. if (appendEndln)
  180. {
  181. result.AppendNewLine();
  182. }
  183. }
  184. private void ProcessIncludeCommand(Token includeCommand, int lineEnd)
  185. {
  186. if (Expect(includeCommand.s, includeCommand.end, '('))
  187. {
  188. Token param = ParseString(includeCommand.s, includeCommand.end + 1, lineEnd);
  189. if (!param.IsValid())
  190. {
  191. Error("ERROR: $include expected a string file path parameter", includeCommand.s, includeCommand.end + 1);
  192. }
  193. else
  194. {
  195. bool found = false;
  196. string includeLocation = null;
  197. // Use reverse order in the array, higher number element have higher priority in case $include exist in several directories
  198. for (int i = templatePaths.Length - 1; i >= 0; i--)
  199. {
  200. string templatePath = templatePaths[i];
  201. includeLocation = Path.Combine(templatePath, param.GetString());
  202. if (File.Exists(includeLocation))
  203. {
  204. found = true;
  205. break;
  206. }
  207. }
  208. if (!found)
  209. {
  210. string errorStr = "ERROR: $include cannot find file : " + param.GetString() + ". Looked into:\n";
  211. foreach (string templatePath in templatePaths)
  212. {
  213. errorStr += "// " + templatePath + "\n";
  214. }
  215. Error(errorStr, includeCommand.s, param.start);
  216. }
  217. else
  218. {
  219. int endIndex = result.length;
  220. using (var temp = new ShaderStringBuilder(humanReadable: true))
  221. {
  222. // Wrap in debug mode
  223. if (isDebug)
  224. {
  225. result.AppendLine("//-------------------------------------------------------------------------------------");
  226. result.AppendLine("// TEMPLATE INCLUDE : " + param.GetString());
  227. result.AppendLine("//-------------------------------------------------------------------------------------");
  228. result.AppendNewLine();
  229. }
  230. // Recursively process templates
  231. ProcessTemplateFile(includeLocation);
  232. // Wrap in debug mode
  233. if (isDebug)
  234. {
  235. result.AppendNewLine();
  236. result.AppendLine("//-------------------------------------------------------------------------------------");
  237. result.AppendLine("// END TEMPLATE INCLUDE : " + param.GetString());
  238. result.AppendLine("//-------------------------------------------------------------------------------------");
  239. }
  240. result.AppendNewLine();
  241. // Required to enforce indentation rules
  242. // Append lines from this include into temporary StringBuilder
  243. // Reduce result length to remove this include
  244. temp.AppendLines(result.ToString(endIndex, result.length - endIndex));
  245. result.length = endIndex;
  246. result.AppendLines(temp.ToCodeBlock());
  247. }
  248. }
  249. }
  250. }
  251. }
  252. private bool ProcessSpliceCommand(Token spliceCommand, int lineEnd, ref int cur)
  253. {
  254. if (!Expect(spliceCommand.s, spliceCommand.end, '('))
  255. {
  256. return false;
  257. }
  258. else
  259. {
  260. Token param = ParseUntil(spliceCommand.s, spliceCommand.end + 1, lineEnd, ')');
  261. if (!param.IsValid())
  262. {
  263. Error("ERROR: splice command is missing a ')'", spliceCommand.s, spliceCommand.start);
  264. return false;
  265. }
  266. else
  267. {
  268. // append everything before the beginning of the escape sequence
  269. AppendSubstring(spliceCommand.s, cur, true, spliceCommand.start - 1, false);
  270. // find the named fragment
  271. string name = param.GetString(); // unfortunately this allocates a new string
  272. string fragment;
  273. if ((namedFragments != null) && namedFragments.TryGetValue(name, out fragment))
  274. {
  275. // splice the fragment
  276. result.Append(fragment);
  277. }
  278. else
  279. {
  280. // no named fragment found
  281. result.Append("/* WARNING: $splice Could not find named fragment '{0}' */", name);
  282. }
  283. // advance to just after the ')' and continue parsing
  284. cur = param.end + 1;
  285. }
  286. }
  287. return true;
  288. }
  289. private bool ContainsCommand(string source, int begin, int end)
  290. {
  291. for (int i = begin; i < end && i < source.Length; ++i)
  292. {
  293. if (source[i] == '$')
  294. return true;
  295. }
  296. return false;
  297. }
  298. private bool ProcessPredicate(Token predicate, int endLine, ref int cur, ref bool appendEndln)
  299. {
  300. // eval if(param)
  301. var fieldName = predicate.GetString();
  302. var nonwhitespace = SkipWhitespace(predicate.s, predicate.end + 1, endLine);
  303. if (!fieldName.StartsWith("features", StringComparison.Ordinal) && activeFields.permutationCount > 0)
  304. {
  305. var passedPermutations = activeFields.allPermutations.instances.Where(i => i.Contains(fieldName)).ToList();
  306. if (passedPermutations.Count > 0)
  307. {
  308. var ifdefs = KeywordUtil.GetKeywordPermutationSetConditional(
  309. passedPermutations.Select(i => i.permutationIndex).ToList()
  310. );
  311. result.AppendLine(ifdefs);
  312. //Append the rest of the line
  313. var content = predicate.s;
  314. if (ContainsCommand(content, nonwhitespace, endLine))
  315. {
  316. content = content.Substring(nonwhitespace, endLine - nonwhitespace);
  317. ProcessTemplateLine(content, 0, content.Length);
  318. }
  319. else
  320. {
  321. AppendSubstring(predicate.s, nonwhitespace, true, endLine, false);
  322. }
  323. result.AppendNewLine();
  324. result.AppendLine("#endif");
  325. return false;
  326. }
  327. else
  328. {
  329. appendEndln = false; //if line isn't active, remove whitespace
  330. }
  331. return false;
  332. }
  333. else
  334. {
  335. // eval if(param)
  336. bool contains = activeFields.baseInstance.Contains(fieldName);
  337. if (contains)
  338. {
  339. // predicate is active
  340. // append everything before the beginning of the escape sequence
  341. AppendSubstring(predicate.s, cur, true, predicate.start - 1, false);
  342. // continue parsing the rest of the line, starting with the first nonwhitespace character
  343. cur = nonwhitespace;
  344. return true;
  345. }
  346. else
  347. {
  348. // predicate is not active
  349. if (isDebug)
  350. {
  351. // append everything before the beginning of the escape sequence
  352. AppendSubstring(predicate.s, cur, true, predicate.start - 1, false);
  353. // append the rest of the line, commented out
  354. result.Append("// ");
  355. AppendSubstring(predicate.s, nonwhitespace, true, endLine, false);
  356. }
  357. else
  358. {
  359. // don't append anything
  360. appendEndln = false;
  361. }
  362. return false;
  363. }
  364. }
  365. }
  366. private static bool IsLetter(char c)
  367. {
  368. return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
  369. }
  370. private static bool IsLetterOrDigit(char c)
  371. {
  372. return IsLetter(c) || Char.IsDigit(c);
  373. }
  374. private Token ParseIdentifier(string code, int start, int end)
  375. {
  376. if (start < end)
  377. {
  378. char c = code[start];
  379. if (IsLetter(c) || (c == '_'))
  380. {
  381. int cur = start + 1;
  382. while (cur < end)
  383. {
  384. c = code[cur];
  385. if (!(IsLetterOrDigit(c) || (c == '_')))
  386. break;
  387. cur++;
  388. }
  389. return new Token(code, start, cur);
  390. }
  391. }
  392. return Token.Invalid();
  393. }
  394. private Token ParseString(string line, int start, int end)
  395. {
  396. if (Expect(line, start, '"'))
  397. {
  398. return ParseUntil(line, start + 1, end, '"');
  399. }
  400. return Token.Invalid();
  401. }
  402. private Token ParseUntil(string line, int start, int end, char endChar)
  403. {
  404. int cur = start;
  405. while (cur < end)
  406. {
  407. if (line[cur] == endChar)
  408. {
  409. return new Token(line, start, cur);
  410. }
  411. cur++;
  412. }
  413. return Token.Invalid();
  414. }
  415. private bool Expect(string line, int location, char expected)
  416. {
  417. if ((location < line.Length) && (line[location] == expected))
  418. {
  419. return true;
  420. }
  421. Error("Expected '" + expected + "'", line, location);
  422. return false;
  423. }
  424. private void Error(string error, string line, int location)
  425. {
  426. // append the line for context
  427. result.Append("\n");
  428. result.Append("// ");
  429. AppendSubstring(line, 0, true, line.Length, false);
  430. result.Append("\n");
  431. // append the location marker, and error description
  432. result.Append("// ");
  433. result.AppendSpaces(location);
  434. result.Append("^ ");
  435. result.Append(error);
  436. result.Append("\n");
  437. }
  438. // an easier to use version of substring Append() -- explicit inclusion on each end, and checks for positive length
  439. private void AppendSubstring(string str, int start, bool includeStart, int end, bool includeEnd)
  440. {
  441. if (!includeStart)
  442. {
  443. start++;
  444. }
  445. if (!includeEnd)
  446. {
  447. end--;
  448. }
  449. int count = end - start + 1;
  450. if (count > 0)
  451. {
  452. result.Append(str, start, count);
  453. }
  454. }
  455. }
  456. }
  457. }