설명 없음
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.

ShaderStringBuilder.cs 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using JetBrains.Annotations;
  5. using UnityEditor.Graphing;
  6. using System.Globalization;
  7. using UnityEngine.Profiling;
  8. namespace UnityEditor.ShaderGraph
  9. {
  10. struct ShaderStringMapping
  11. {
  12. public AbstractMaterialNode node { get; set; }
  13. // public List<AbstractMaterialNode> nodes { get; set; }
  14. public int startIndex { get; set; }
  15. public int count { get; set; }
  16. }
  17. class ShaderStringBuilder : IDisposable
  18. {
  19. enum ScopeType
  20. {
  21. Indent,
  22. Block,
  23. BlockSemicolon
  24. }
  25. StringBuilder m_StringBuilder;
  26. Stack<ScopeType> m_ScopeStack;
  27. int m_IndentationLevel;
  28. ShaderStringMapping m_CurrentMapping;
  29. List<ShaderStringMapping> m_Mappings;
  30. bool m_HumanReadable;
  31. const string k_IndentationString = " ";
  32. const string k_NewLineString = "\n";
  33. internal AbstractMaterialNode currentNode
  34. {
  35. get { return m_CurrentMapping.node; }
  36. set
  37. {
  38. m_CurrentMapping.count = m_StringBuilder.Length - m_CurrentMapping.startIndex;
  39. if (m_CurrentMapping.count > 0)
  40. m_Mappings.Add(m_CurrentMapping);
  41. m_CurrentMapping.node = value;
  42. m_CurrentMapping.startIndex = m_StringBuilder.Length;
  43. m_CurrentMapping.count = 0;
  44. }
  45. }
  46. internal List<ShaderStringMapping> mappings
  47. {
  48. get { return m_Mappings; }
  49. }
  50. public ShaderStringBuilder(int indentationLevel = 0, int stringBuilderSize = 8192, bool humanReadable = false)
  51. {
  52. IncreaseIndent(indentationLevel);
  53. m_StringBuilder = new StringBuilder(stringBuilderSize);
  54. m_ScopeStack = new Stack<ScopeType>();
  55. m_Mappings = new List<ShaderStringMapping>();
  56. m_CurrentMapping = new ShaderStringMapping();
  57. m_HumanReadable = humanReadable;
  58. }
  59. public void AppendNewLine()
  60. {
  61. m_StringBuilder.Append(k_NewLineString);
  62. }
  63. private void AppendLine(string value, int startIndex, int count)
  64. {
  65. if (value.Length > 0)
  66. {
  67. TryAppendIndentation();
  68. m_StringBuilder.Append(value, startIndex, count);
  69. }
  70. AppendNewLine();
  71. }
  72. public void AppendLine(string value)
  73. {
  74. if (!string.IsNullOrEmpty(value))
  75. {
  76. TryAppendIndentation();
  77. m_StringBuilder.Append(value);
  78. }
  79. AppendNewLine();
  80. }
  81. public void AddLine(string v0) { TryAppendIndentation(); Append(v0); AppendNewLine(); }
  82. public void AddLine(string v0, string v1) { TryAppendIndentation(); Append(v0); Append(v1); AppendNewLine(); }
  83. public void AddLine(string v0, string v1, string v2) { TryAppendIndentation(); Append(v0); Append(v1); Append(v2); AppendNewLine(); }
  84. public void AddLine(string v0, string v1, string v2, string v3) { TryAppendIndentation(); Append(v0); Append(v1); Append(v2); Append(v3); AppendNewLine(); }
  85. public void AddLine(string v0, string v1, string v2, string v3, string v4) { TryAppendIndentation(); Append(v0); Append(v1); Append(v2); Append(v3); Append(v4); AppendNewLine(); }
  86. public void AddLine(string v0, string v1, string v2, string v3, string v4, string v5) { TryAppendIndentation(); Append(v0); Append(v1); Append(v2); Append(v3); Append(v4); Append(v5); AppendNewLine(); }
  87. public void AddLine(string v0, string v1, string v2, string v3, string v4, string v5, string v6) { TryAppendIndentation(); Append(v0); Append(v1); Append(v2); Append(v3); Append(v4); Append(v5); Append(v6); AppendNewLine(); }
  88. public void AddLine(string v0, string v1, string v2, string v3, string v4, string v5, string v6, string v7) { TryAppendIndentation(); Append(v0); Append(v1); Append(v2); Append(v3); Append(v4); Append(v5); Append(v6); Append(v7); AppendNewLine(); }
  89. [StringFormatMethod("formatString")]
  90. public void AppendLine(string formatString, params object[] args)
  91. {
  92. TryAppendIndentation();
  93. m_StringBuilder.AppendFormat(CultureInfo.InvariantCulture, formatString, args);
  94. AppendNewLine();
  95. }
  96. static readonly char[] LineSeparators = new[] { '\n', '\r'};
  97. public void AppendLines(string lines)
  98. {
  99. if (string.IsNullOrEmpty(lines))
  100. return;
  101. int startSearchIndex = 0;
  102. int indexOfNextBreak = lines.IndexOfAny(LineSeparators);
  103. while (indexOfNextBreak >= 0)
  104. {
  105. AppendLine(lines, startSearchIndex, indexOfNextBreak - startSearchIndex);
  106. startSearchIndex = indexOfNextBreak + 1;
  107. indexOfNextBreak = lines.IndexOfAny(LineSeparators, startSearchIndex);
  108. }
  109. if (startSearchIndex < lines.Length)
  110. {
  111. AppendLine(lines, startSearchIndex, lines.Length - startSearchIndex);
  112. }
  113. }
  114. public void Append(string value)
  115. {
  116. m_StringBuilder.Append(value);
  117. }
  118. public void Append(string value, int start, int count)
  119. {
  120. m_StringBuilder.Append(value, start, count);
  121. }
  122. [StringFormatMethod("formatString")]
  123. public void Append(string formatString, params object[] args)
  124. {
  125. m_StringBuilder.AppendFormat(formatString, args);
  126. }
  127. public void AppendSpaces(int count)
  128. {
  129. m_StringBuilder.Append(' ', count);
  130. }
  131. public void TryAppendIndentation()
  132. {
  133. if (m_HumanReadable)
  134. {
  135. for (var i = 0; i < m_IndentationLevel; i++)
  136. m_StringBuilder.Append(k_IndentationString);
  137. }
  138. }
  139. public IDisposable IndentScope()
  140. {
  141. m_ScopeStack.Push(ScopeType.Indent);
  142. IncreaseIndent();
  143. return this;
  144. }
  145. public IDisposable BlockScope()
  146. {
  147. AppendLine("{");
  148. IncreaseIndent();
  149. m_ScopeStack.Push(ScopeType.Block);
  150. return this;
  151. }
  152. public IDisposable BlockSemicolonScope()
  153. {
  154. AppendLine("{");
  155. IncreaseIndent();
  156. m_ScopeStack.Push(ScopeType.BlockSemicolon);
  157. return this;
  158. }
  159. public void IncreaseIndent()
  160. {
  161. m_IndentationLevel++;
  162. }
  163. public void IncreaseIndent(int level)
  164. {
  165. for (var i = 0; i < level; i++)
  166. IncreaseIndent();
  167. }
  168. public void DecreaseIndent()
  169. {
  170. m_IndentationLevel--;
  171. }
  172. public void DecreaseIndent(int level)
  173. {
  174. for (var i = 0; i < level; i++)
  175. DecreaseIndent();
  176. }
  177. public void Dispose()
  178. {
  179. if (m_ScopeStack.Count == 0)
  180. return;
  181. switch (m_ScopeStack.Pop())
  182. {
  183. case ScopeType.Indent:
  184. DecreaseIndent();
  185. break;
  186. case ScopeType.Block:
  187. DecreaseIndent();
  188. AppendLine("}");
  189. break;
  190. case ScopeType.BlockSemicolon:
  191. DecreaseIndent();
  192. AppendLine("};");
  193. break;
  194. }
  195. }
  196. public void Concat(ShaderStringBuilder other)
  197. {
  198. // First re-add all the mappings from `other`, such that their mappings are transformed.
  199. foreach (var mapping in other.m_Mappings)
  200. {
  201. currentNode = mapping.node;
  202. // Use `AppendLines` to indent according to the current indentation.
  203. if (m_HumanReadable)
  204. {
  205. AppendLines(other.ToString(mapping.startIndex, mapping.count));
  206. }
  207. else
  208. {
  209. Append(other.ToString(mapping.startIndex, mapping.count));
  210. }
  211. }
  212. currentNode = other.currentNode;
  213. if (m_HumanReadable)
  214. {
  215. AppendLines(other.ToString(other.m_CurrentMapping.startIndex, other.length - other.m_CurrentMapping.startIndex));
  216. }
  217. else
  218. {
  219. Append(other.ToString(other.m_CurrentMapping.startIndex, other.length - other.m_CurrentMapping.startIndex));
  220. }
  221. }
  222. public void ReplaceInCurrentMapping(string oldValue, string newValue)
  223. {
  224. Profiler.BeginSample("ReplaceInCurrentMapping");
  225. int start = m_CurrentMapping.startIndex;
  226. int end = m_StringBuilder.Length - start;
  227. m_StringBuilder.Replace(oldValue, newValue, start, end);
  228. Profiler.EndSample();
  229. }
  230. public void Replace(string oldValue, string newValue, int start, int end)
  231. {
  232. m_StringBuilder.Replace(oldValue, newValue, start, end);
  233. }
  234. public string ToCodeBlock()
  235. {
  236. // Remove new line
  237. if (m_StringBuilder.Length > 0)
  238. m_StringBuilder.Length = m_StringBuilder.Length - 1;
  239. if (m_HumanReadable)
  240. {
  241. // Set indentations
  242. m_StringBuilder.Replace(Environment.NewLine, Environment.NewLine + k_IndentationString);
  243. }
  244. return m_StringBuilder.ToString();
  245. }
  246. public override string ToString()
  247. {
  248. return m_StringBuilder.ToString();
  249. }
  250. public string ToString(int startIndex, int length)
  251. {
  252. return m_StringBuilder.ToString(startIndex, length);
  253. }
  254. internal void Clear()
  255. {
  256. m_StringBuilder.Length = 0;
  257. }
  258. internal int length
  259. {
  260. get { return m_StringBuilder.Length; }
  261. set { m_StringBuilder.Length = value; }
  262. }
  263. }
  264. }