ShaderStringBuilder.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. namespace UnityEditor.ShaderGraph
  8. {
  9. struct ShaderStringMapping
  10. {
  11. public AbstractMaterialNode node { get; set; }
  12. // public List<AbstractMaterialNode> nodes { get; set; }
  13. public int startIndex { get; set; }
  14. public int count { get; set; }
  15. }
  16. class ShaderStringBuilder : IDisposable
  17. {
  18. enum ScopeType
  19. {
  20. Indent,
  21. Block,
  22. BlockSemicolon
  23. }
  24. StringBuilder m_StringBuilder;
  25. Stack<ScopeType> m_ScopeStack;
  26. int m_IndentationLevel;
  27. ShaderStringMapping m_CurrentMapping;
  28. List<ShaderStringMapping> m_Mappings;
  29. const string k_IndentationString = " ";
  30. internal AbstractMaterialNode currentNode
  31. {
  32. get { return m_CurrentMapping.node; }
  33. set
  34. {
  35. m_CurrentMapping.count = m_StringBuilder.Length - m_CurrentMapping.startIndex;
  36. if (m_CurrentMapping.count > 0)
  37. m_Mappings.Add(m_CurrentMapping);
  38. m_CurrentMapping.node = value;
  39. m_CurrentMapping.startIndex = m_StringBuilder.Length;
  40. m_CurrentMapping.count = 0;
  41. }
  42. }
  43. internal List<ShaderStringMapping> mappings
  44. {
  45. get { return m_Mappings; }
  46. }
  47. public ShaderStringBuilder()
  48. {
  49. m_StringBuilder = new StringBuilder();
  50. m_ScopeStack = new Stack<ScopeType>();
  51. m_Mappings = new List<ShaderStringMapping>();
  52. m_CurrentMapping = new ShaderStringMapping();
  53. }
  54. public ShaderStringBuilder(int indentationLevel)
  55. : this()
  56. {
  57. IncreaseIndent(indentationLevel);
  58. }
  59. public void AppendNewLine()
  60. {
  61. m_StringBuilder.AppendLine();
  62. }
  63. public void AppendLine(string value)
  64. {
  65. if (!string.IsNullOrEmpty(value))
  66. {
  67. AppendIndentation();
  68. m_StringBuilder.Append(value);
  69. }
  70. AppendNewLine();
  71. }
  72. [StringFormatMethod("formatString")]
  73. public void AppendLine(string formatString, params object[] args)
  74. {
  75. AppendIndentation();
  76. m_StringBuilder.AppendFormat(CultureInfo.InvariantCulture,formatString, args);
  77. AppendNewLine();
  78. }
  79. public void AppendLines(string lines)
  80. {
  81. if (string.IsNullOrEmpty(lines))
  82. return;
  83. var splitLines = lines.Split('\n');
  84. var lineCount = splitLines.Length;
  85. var lastLine = splitLines[lineCount - 1];
  86. if (string.IsNullOrEmpty(lastLine) || lastLine == "\r")
  87. lineCount--;
  88. for (var i = 0; i < lineCount; i++)
  89. AppendLine(splitLines[i].Trim('\r'));
  90. }
  91. public void Append(string value)
  92. {
  93. m_StringBuilder.Append(value);
  94. }
  95. public void Append(string value, int start, int count)
  96. {
  97. m_StringBuilder.Append(value, start, count);
  98. }
  99. [StringFormatMethod("formatString")]
  100. public void Append(string formatString, params object[] args)
  101. {
  102. m_StringBuilder.AppendFormat(formatString, args);
  103. }
  104. public void AppendSpaces(int count)
  105. {
  106. m_StringBuilder.Append(' ', count);
  107. }
  108. public void AppendIndentation()
  109. {
  110. for (var i = 0; i < m_IndentationLevel; i++)
  111. m_StringBuilder.Append(k_IndentationString);
  112. }
  113. public IDisposable IndentScope()
  114. {
  115. m_ScopeStack.Push(ScopeType.Indent);
  116. IncreaseIndent();
  117. return this;
  118. }
  119. public IDisposable BlockScope()
  120. {
  121. AppendLine("{");
  122. IncreaseIndent();
  123. m_ScopeStack.Push(ScopeType.Block);
  124. return this;
  125. }
  126. public IDisposable BlockSemicolonScope()
  127. {
  128. AppendLine("{");
  129. IncreaseIndent();
  130. m_ScopeStack.Push(ScopeType.BlockSemicolon);
  131. return this;
  132. }
  133. public void IncreaseIndent()
  134. {
  135. m_IndentationLevel++;
  136. }
  137. public void IncreaseIndent(int level)
  138. {
  139. for (var i = 0; i < level; i++)
  140. IncreaseIndent();
  141. }
  142. public void DecreaseIndent()
  143. {
  144. m_IndentationLevel--;
  145. }
  146. public void DecreaseIndent(int level)
  147. {
  148. for (var i = 0; i < level; i++)
  149. DecreaseIndent();
  150. }
  151. public void Dispose()
  152. {
  153. if(m_ScopeStack.Count == 0)
  154. return;
  155. switch (m_ScopeStack.Pop())
  156. {
  157. case ScopeType.Indent:
  158. DecreaseIndent();
  159. break;
  160. case ScopeType.Block:
  161. DecreaseIndent();
  162. AppendLine("}");
  163. break;
  164. case ScopeType.BlockSemicolon:
  165. DecreaseIndent();
  166. AppendLine("};");
  167. break;
  168. }
  169. }
  170. public void Concat(ShaderStringBuilder other)
  171. {
  172. // First re-add all the mappings from `other`, such that their mappings are transformed.
  173. foreach (var mapping in other.m_Mappings)
  174. {
  175. currentNode = mapping.node;
  176. // Use `AppendLines` to indent according to the current indentation.
  177. AppendLines(other.ToString(mapping.startIndex, mapping.count));
  178. }
  179. currentNode = other.currentNode;
  180. AppendLines(other.ToString(other.m_CurrentMapping.startIndex, other.length - other.m_CurrentMapping.startIndex));
  181. }
  182. public void ReplaceInCurrentMapping(string oldValue, string newValue)
  183. {
  184. int start = m_CurrentMapping.startIndex;
  185. int end = m_StringBuilder.Length - start;
  186. m_StringBuilder.Replace(oldValue, newValue, start, end );
  187. }
  188. public string ToCodeBlack()
  189. {
  190. // Remove new line
  191. if(m_StringBuilder.Length > 0)
  192. m_StringBuilder.Length = m_StringBuilder.Length - 1;
  193. // Set indentations
  194. m_StringBuilder.Replace(Environment.NewLine, Environment.NewLine + k_IndentationString);
  195. return m_StringBuilder.ToString();
  196. }
  197. public override string ToString()
  198. {
  199. return m_StringBuilder.ToString();
  200. }
  201. public string ToString(out ShaderSourceMap sourceMap)
  202. {
  203. m_CurrentMapping.count = m_StringBuilder.Length - m_CurrentMapping.startIndex;
  204. if (m_CurrentMapping.count > 0)
  205. m_Mappings.Add(m_CurrentMapping);
  206. var source = m_StringBuilder.ToString();
  207. sourceMap = new ShaderSourceMap(source, m_Mappings);
  208. m_Mappings.RemoveAt(m_Mappings.Count - 1);
  209. m_CurrentMapping.count = 0;
  210. return source;
  211. }
  212. public string ToString(int startIndex, int length)
  213. {
  214. return m_StringBuilder.ToString(startIndex, length);
  215. }
  216. internal void Clear()
  217. {
  218. m_StringBuilder.Length = 0;
  219. }
  220. internal int length
  221. {
  222. get { return m_StringBuilder.Length; }
  223. set { m_StringBuilder.Length = value; }
  224. }
  225. }
  226. }