VFXShaderWriter.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Globalization;
  6. using UnityEngine;
  7. using UnityEngine.VFX;
  8. using System.Reflection;
  9. using System.Runtime.InteropServices;
  10. using System.Collections.ObjectModel;
  11. namespace UnityEditor.VFX
  12. {
  13. static class VFXCodeGeneratorHelper
  14. {
  15. private static readonly char[] kAlpha = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
  16. public static string GeneratePrefix(uint index)
  17. {
  18. if (index == 0u) return "a";
  19. var prefix = "";
  20. while (index != 0u)
  21. {
  22. prefix = kAlpha[index % kAlpha.Length] + prefix;
  23. index /= (uint)kAlpha.Length;
  24. }
  25. return prefix;
  26. }
  27. }
  28. class VFXShaderWriter
  29. {
  30. public VFXShaderWriter()
  31. { }
  32. public VFXShaderWriter(string initialValue)
  33. {
  34. builder.Append(initialValue);
  35. }
  36. public static string GetValueString(VFXValueType type, object value)
  37. {
  38. var format = "";
  39. switch (type)
  40. {
  41. case VFXValueType.Boolean:
  42. case VFXValueType.Int32:
  43. case VFXValueType.Uint32:
  44. case VFXValueType.Float:
  45. format = "({0}){1}";
  46. break;
  47. case VFXValueType.Float2:
  48. case VFXValueType.Float3:
  49. case VFXValueType.Float4:
  50. case VFXValueType.Matrix4x4:
  51. format = "{0}{1}";
  52. break;
  53. default: throw new Exception("GetValueString missing type: " + type);
  54. }
  55. // special cases of ToString
  56. switch (type)
  57. {
  58. case VFXValueType.Boolean:
  59. value = value.ToString().ToLower();
  60. break;
  61. case VFXValueType.Float:
  62. value = FormatFloat((float)value);
  63. break;
  64. case VFXValueType.Float2:
  65. value = $"({FormatFloat(((Vector2)value).x)}, {FormatFloat(((Vector2)value).y)})";
  66. break;
  67. case VFXValueType.Float3:
  68. value = $"({FormatFloat(((Vector3)value).x)}, {FormatFloat(((Vector3)value).y)}, {FormatFloat(((Vector3)value).z)})";
  69. break;
  70. case VFXValueType.Float4:
  71. value = $"({FormatFloat(((Vector4)value).x)}, {FormatFloat(((Vector4)value).y)}, {FormatFloat(((Vector4)value).z)}, {FormatFloat(((Vector4)value).w)})";
  72. break;
  73. case VFXValueType.Matrix4x4:
  74. {
  75. var matrix = ((Matrix4x4)value).transpose;
  76. value = "(";
  77. for (int i = 0; i < 16; ++i)
  78. value += string.Format(CultureInfo.InvariantCulture, i == 15 ? "{0}" : "{0},", FormatFloat(matrix[i]));
  79. value += ")";
  80. }
  81. break;
  82. }
  83. return string.Format(CultureInfo.InvariantCulture, format, VFXExpression.TypeToCode(type), value);
  84. }
  85. private static string FormatFloat(float f)
  86. {
  87. if (float.IsInfinity(f))
  88. return f > 0.0f ? "VFX_INFINITY" : "-VFX_INFINITY";
  89. else if (float.IsNaN(f))
  90. return "VFX_NAN";
  91. else
  92. return f.ToString("G9", CultureInfo.InvariantCulture);
  93. }
  94. public static string GetMultilineWithPrefix(string str, string linePrefix)
  95. {
  96. if (linePrefix.Length == 0)
  97. return str;
  98. if (str.Length == 0)
  99. return linePrefix;
  100. string[] delim = { System.Environment.NewLine, "\n" };
  101. var lines = str.Split(delim, System.StringSplitOptions.None);
  102. var dst = new StringBuilder(linePrefix.Length * lines.Length + str.Length);
  103. foreach (var line in lines)
  104. {
  105. dst.Append(linePrefix);
  106. dst.Append(line);
  107. dst.Append('\n');
  108. }
  109. return dst.ToString(0, dst.Length - 1); // Remove the last line terminator
  110. }
  111. public void WriteFormat(string str, object arg0) { m_Builder.AppendFormat(str, arg0); }
  112. public void WriteFormat(string str, object arg0, object arg1) { m_Builder.AppendFormat(str, arg0, arg1); }
  113. public void WriteFormat(string str, object arg0, object arg1, object arg2) { m_Builder.AppendFormat(str, arg0, arg1, arg2); }
  114. public void WriteLineFormat(string str, object arg0) { WriteFormat(str, arg0); WriteLine(); }
  115. public void WriteLineFormat(string str, object arg0, object arg1) { WriteFormat(str, arg0, arg1); WriteLine(); }
  116. public void WriteLineFormat(string str, object arg0, object arg1, object arg2) { WriteFormat(str, arg0, arg1, arg2); WriteLine(); }
  117. // Generic builder method
  118. public void Write<T>(T t)
  119. {
  120. m_Builder.Append(t);
  121. }
  122. // Optimize version to append substring and avoid useless allocation
  123. public void Write(String s, int start, int length)
  124. {
  125. m_Builder.Append(s, start, length);
  126. }
  127. public void WriteLine<T>(T t)
  128. {
  129. Write(t);
  130. WriteLine();
  131. }
  132. public void WriteLine()
  133. {
  134. m_Builder.Append('\n');
  135. WriteIndent();
  136. }
  137. public void EnterScope()
  138. {
  139. WriteLine('{');
  140. Indent();
  141. }
  142. public void ExitScope()
  143. {
  144. Deindent();
  145. WriteLine('}');
  146. }
  147. public void ExitScopeStruct()
  148. {
  149. Deindent();
  150. WriteLine("};");
  151. }
  152. public void ReplaceMultilineWithIndent(string tag, string src)
  153. {
  154. var str = m_Builder.ToString();
  155. int startIndex = 0;
  156. while (true)
  157. {
  158. int index = str.IndexOf(tag, startIndex);
  159. if (index == -1)
  160. break;
  161. var lastPrefixIndex = index;
  162. while (index > 0 && (str[index] == ' ' || str[index] == '\t'))
  163. --index;
  164. var prefix = str.Substring(index, lastPrefixIndex - index);
  165. var formattedStr = GetMultilineWithPrefix(src, prefix).Substring(prefix.Length);
  166. m_Builder.Replace(tag, formattedStr, lastPrefixIndex, tag.Length);
  167. startIndex = index;
  168. }
  169. }
  170. public void WriteMultilineWithIndent<T>(T str)
  171. {
  172. if (m_Indent == 0)
  173. Write(str);
  174. else
  175. {
  176. var indentStr = new StringBuilder(m_Indent * kIndentStr.Length);
  177. for (int i = 0; i < m_Indent; ++i)
  178. indentStr.Append(kIndentStr);
  179. WriteMultilineWithPrefix(str, indentStr.ToString());
  180. }
  181. }
  182. public void WriteMultilineWithPrefix<T>(T str, string linePrefix)
  183. {
  184. if (linePrefix.Length == 0)
  185. Write(str);
  186. else
  187. {
  188. var res = GetMultilineWithPrefix(str.ToString(), linePrefix);
  189. WriteLine(res.Substring(linePrefix.Length)); // Remove first line length;
  190. }
  191. }
  192. public override string ToString()
  193. {
  194. return m_Builder.ToString();
  195. }
  196. private int WritePadding(int alignment, int offset, ref int index)
  197. {
  198. int padding = (alignment - (offset % alignment)) % alignment;
  199. if (padding != 0)
  200. WriteLineFormat("uint{0} PADDING_{1};", padding == 1 ? "" : padding.ToString(), index++);
  201. return padding;
  202. }
  203. private static bool IsBufferBuiltinType(Type type)
  204. {
  205. return VFXExpression.IsUniform(VFXExpression.GetVFXValueTypeFromType(type));
  206. }
  207. static string GetStructureName(Type type)
  208. {
  209. if (IsBufferBuiltinType(type))
  210. return VFXExpression.TypeToCode(VFXExpression.GetVFXValueTypeFromType(type));
  211. else
  212. return type.Name;
  213. }
  214. static void GenerateStructureCode(Type type, VFXShaderWriter structureDeclaration, HashSet<Type> alreadyGeneratedStructure)
  215. {
  216. if (IsBufferBuiltinType(type))
  217. return; // No structure to generate, it is a builtin type
  218. if (alreadyGeneratedStructure.Contains(type))
  219. return;
  220. var structureName = GetStructureName(type);
  221. var prerequisite = new VFXShaderWriter();
  222. var currentStructure = new VFXShaderWriter();
  223. currentStructure.WriteLineFormat("struct {0}", structureName);
  224. currentStructure.WriteLine("{");
  225. currentStructure.Indent();
  226. foreach (var field in VFXLibrary.GetFieldFromType(type))
  227. {
  228. string typeName;
  229. if (field.valueType == VFXValueType.None)
  230. {
  231. typeName = GetStructureName(field.type);
  232. GenerateStructureCode(field.type, prerequisite, alreadyGeneratedStructure);
  233. }
  234. else
  235. typeName = VFXExpression.TypeToCode(field.valueType);
  236. currentStructure.WriteLineFormat("{0} {1};", typeName, field.name);
  237. }
  238. currentStructure.Deindent();
  239. currentStructure.WriteLine("};");
  240. prerequisite.WriteLine(currentStructure.ToString());
  241. structureDeclaration.Write(prerequisite.ToString());
  242. alreadyGeneratedStructure.Add(type);
  243. }
  244. private void WriteBufferTypeDeclaration(Type type, HashSet<Type> alreadyGeneratedStructure)
  245. {
  246. GenerateStructureCode(type, this, alreadyGeneratedStructure);
  247. var structureName = GetStructureName(type);
  248. var expectedStride = Marshal.SizeOf(type);
  249. WriteLineFormat("{0} SampleStructuredBuffer(StructuredBuffer<{0}> buffer, uint index, uint actualStride, uint actualCount)", structureName);
  250. {
  251. WriteLine("{");
  252. Indent();
  253. WriteLineFormat("{0} read = ({0})0;", structureName);
  254. WriteLine("[branch]");
  255. WriteLineFormat("if (actualStride == (uint){0} && index < actualCount)", expectedStride);
  256. {
  257. Indent();
  258. WriteLine("read = buffer[index];");
  259. Deindent();
  260. }
  261. WriteLineFormat("return read;", structureName);
  262. Deindent();
  263. WriteLine("}");
  264. }
  265. }
  266. public void WriteBufferTypeDeclaration(IEnumerable<Type> types)
  267. {
  268. var alreadyGeneratedStructure = new HashSet<Type>();
  269. foreach (var type in types)
  270. WriteBufferTypeDeclaration(type, alreadyGeneratedStructure);
  271. }
  272. public void WriteBuffer(VFXUniformMapper mapper, ReadOnlyDictionary<VFXExpression, Type> usageGraphicsBuffer)
  273. {
  274. foreach (var buffer in mapper.buffers)
  275. {
  276. var name = mapper.GetName(buffer);
  277. if (buffer.valueType == VFXValueType.Buffer && usageGraphicsBuffer.TryGetValue(buffer, out var type))
  278. {
  279. if (type == null)
  280. throw new NullReferenceException("Unexpected null type in graphicsBuffer usage");
  281. var structureName = GetStructureName(type);
  282. WriteLineFormat("StructuredBuffer<{0}> {1};", structureName, name);
  283. }
  284. else
  285. {
  286. WriteLineFormat("{0} {1};", VFXExpression.TypeToCode(buffer.valueType), name);
  287. }
  288. }
  289. }
  290. public void WriteTexture(VFXUniformMapper mapper, IEnumerable<string> skipNames = null)
  291. {
  292. foreach (var texture in mapper.textures)
  293. {
  294. var names = mapper.GetNames(texture);
  295. // TODO At the moment issue all names sharing the same texture as different texture slots. This is not optimized as it required more texture binding than necessary
  296. for (int i = 0; i < names.Count; ++i)
  297. {
  298. if (skipNames != null && skipNames.Contains(names[i]))
  299. continue;
  300. WriteLineFormat("{0} {1};", VFXExpression.TypeToCode(texture.valueType), names[i]);
  301. if (VFXExpression.IsTexture(texture.valueType)) //Mesh doesn't require a sampler or texel helper
  302. {
  303. WriteLineFormat("SamplerState sampler{0};", names[i]);
  304. WriteLineFormat("float4 {0}_TexelSize;", names[i]); // TODO This is not very good to add a uniform for each texture that is hardly ever used
  305. }
  306. WriteLine();
  307. }
  308. }
  309. }
  310. public void WriteEventBuffers(string baseName, int count)
  311. {
  312. for (int i = 0; i < count; ++i)
  313. {
  314. var prefix = VFXCodeGeneratorHelper.GeneratePrefix((uint)i);
  315. WriteLineFormat("AppendStructuredBuffer<uint> {0}_{1};", baseName, prefix);
  316. }
  317. }
  318. public void WriteCBuffer(VFXUniformMapper mapper, string bufferName)
  319. {
  320. var uniformValues = mapper.uniforms
  321. .Where(e => !e.IsAny(VFXExpression.Flags.Constant | VFXExpression.Flags.InvalidOnCPU)) // Filter out constant expressions
  322. .OrderByDescending(e => VFXValue.TypeToSize(e.valueType));
  323. var uniformBlocks = new List<List<VFXExpression>>();
  324. foreach (var value in uniformValues)
  325. {
  326. var block = uniformBlocks.FirstOrDefault(b => b.Sum(e => VFXValue.TypeToSize(e.valueType)) + VFXValue.TypeToSize(value.valueType) <= 4);
  327. if (block != null)
  328. block.Add(value);
  329. else
  330. uniformBlocks.Add(new List<VFXExpression>() { value });
  331. }
  332. if (uniformBlocks.Count > 0)
  333. {
  334. WriteLineFormat("CBUFFER_START({0})", bufferName);
  335. Indent();
  336. int paddingIndex = 0;
  337. foreach (var block in uniformBlocks)
  338. {
  339. int currentSize = 0;
  340. foreach (var value in block)
  341. {
  342. string type = VFXExpression.TypeToUniformCode(value.valueType);
  343. string name = mapper.GetName(value);
  344. if (name.StartsWith("unity_")) //Reserved unity variable name (could be filled manually see : VFXCameraUpdate)
  345. continue;
  346. currentSize += VFXExpression.TypeToSize(value.valueType);
  347. WriteLineFormat("{0} {1};", type, name);
  348. }
  349. WritePadding(4, currentSize, ref paddingIndex);
  350. }
  351. Deindent();
  352. WriteLine("CBUFFER_END");
  353. }
  354. }
  355. public void WriteAttributeStruct(IEnumerable<VFXAttribute> attributes, string name)
  356. {
  357. WriteLineFormat("struct {0}", name);
  358. WriteLine("{");
  359. Indent();
  360. foreach (var attribute in attributes)
  361. WriteLineFormat("{0} {1};", VFXExpression.TypeToCode(attribute.type), attribute.name);
  362. Deindent();
  363. WriteLine("};");
  364. }
  365. private string AggregateParameters(List<string> parameters)
  366. {
  367. return parameters.Count == 0 ? "" : parameters.Aggregate((a, b) => a + ", " + b);
  368. }
  369. private static string GetFunctionParameterType(VFXValueType type)
  370. {
  371. switch (type)
  372. {
  373. case VFXValueType.Texture2D: return "VFXSampler2D";
  374. case VFXValueType.Texture2DArray: return "VFXSampler2DArray";
  375. case VFXValueType.Texture3D: return "VFXSampler3D";
  376. case VFXValueType.TextureCube: return "VFXSamplerCube";
  377. case VFXValueType.TextureCubeArray: return "VFXSamplerCubeArray";
  378. case VFXValueType.CameraBuffer: return "VFXSamplerCameraBuffer";
  379. default:
  380. return VFXExpression.TypeToCode(type);
  381. }
  382. }
  383. private static string GetFunctionParameterName(VFXExpression expression, Dictionary<VFXExpression, string> names)
  384. {
  385. var expressionName = names[expression];
  386. switch (expression.valueType)
  387. {
  388. case VFXValueType.Texture2D:
  389. case VFXValueType.Texture2DArray:
  390. case VFXValueType.Texture3D:
  391. case VFXValueType.TextureCube:
  392. case VFXValueType.TextureCubeArray: return string.Format("GetVFXSampler({0}, {1})", expressionName, ("sampler" + expressionName));
  393. case VFXValueType.CameraBuffer: return string.Format("GetVFXSampler({0}, {1})", expressionName, ("sampler" + expressionName));
  394. default:
  395. return expressionName;
  396. }
  397. }
  398. private static string GetInputModifier(VFXAttributeMode mode)
  399. {
  400. if ((mode & VFXAttributeMode.Write) != 0)
  401. return "inout ";
  402. return string.Empty;
  403. }
  404. public struct FunctionParameter
  405. {
  406. public string name;
  407. public VFXExpression expression;
  408. public VFXAttributeMode mode;
  409. }
  410. public void WriteBlockFunction(VFXExpressionMapper mapper, string functionName, string source, IEnumerable<FunctionParameter> parameters, string commentMethod)
  411. {
  412. var parametersCode = new List<string>();
  413. foreach (var parameter in parameters)
  414. {
  415. var inputModifier = GetInputModifier(parameter.mode);
  416. var parameterType = GetFunctionParameterType(parameter.expression.valueType);
  417. parametersCode.Add(string.Format("{0}{1} {2}", inputModifier, parameterType, parameter.name));
  418. }
  419. WriteFormat("void {0}({1})", functionName, AggregateParameters(parametersCode));
  420. if (!string.IsNullOrEmpty(commentMethod))
  421. {
  422. WriteFormat(" /*{0}*/", commentMethod);
  423. }
  424. WriteLine();
  425. EnterScope();
  426. if (source != null)
  427. WriteMultilineWithIndent(source);
  428. ExitScope();
  429. }
  430. public void WriteCallFunction(string functionName, IEnumerable<FunctionParameter> parameters, VFXExpressionMapper mapper, Dictionary<VFXExpression, string> variableNames)
  431. {
  432. var parametersCode = new List<string>();
  433. foreach (var parameter in parameters)
  434. {
  435. var inputModifier = GetInputModifier(parameter.mode);
  436. parametersCode.Add(string.Format("{1}{0}", GetFunctionParameterName(parameter.expression, variableNames), string.IsNullOrEmpty(inputModifier) ? string.Empty : string.Format(" /*{0}*/", inputModifier)));
  437. }
  438. WriteLineFormat("{0}({1});", functionName, AggregateParameters(parametersCode));
  439. }
  440. public void WriteAssignement(VFXValueType type, string variableName, string value)
  441. {
  442. var format = value == "0" ? "{1} = ({0}){2};" : "{1} = {2};";
  443. WriteFormat(format, VFXExpression.TypeToCode(type), variableName, value);
  444. }
  445. public void WriteVariable(VFXValueType type, string variableName, string value)
  446. {
  447. if (!VFXExpression.IsTypeValidOnGPU(type))
  448. throw new ArgumentException(string.Format("Invalid GPU Type: {0}", type));
  449. WriteFormat("{0} ", VFXExpression.TypeToCode(type));
  450. WriteAssignement(type, variableName, value);
  451. }
  452. public void WriteDeclaration(VFXValueType type, string variableName)
  453. {
  454. if (!VFXExpression.IsTypeValidOnGPU(type))
  455. throw new ArgumentException(string.Format("Invalid GPU Type: {0}", type));
  456. WriteFormat("{0} {1};\n", VFXExpression.TypeToCode(type), variableName);
  457. }
  458. public void WriteDeclaration(VFXValueType type, string variableName, string semantic)
  459. {
  460. if (!VFXExpression.IsTypeValidOnGPU(type))
  461. throw new ArgumentException(string.Format("Invalid GPU Type: {0}", type));
  462. WriteFormat("VFX_OPTIONAL_INTERPOLATION {0} {1} : {2};\n", VFXExpression.TypeToCode(type), variableName, semantic);
  463. }
  464. public void WriteVariable(VFXExpression exp, Dictionary<VFXExpression, string> variableNames)
  465. {
  466. if (!variableNames.ContainsKey(exp))
  467. {
  468. string entry;
  469. if (exp.Is(VFXExpression.Flags.Constant))
  470. entry = exp.GetCodeString(null); // Patch constant directly
  471. else
  472. {
  473. foreach (var parent in exp.parents)
  474. WriteVariable(parent, variableNames);
  475. // Generate a new variable name
  476. entry = "tmp_" + VFXCodeGeneratorHelper.GeneratePrefix((uint)variableNames.Count());
  477. string value = exp.GetCodeString(exp.parents.Select(p => variableNames[p]).ToArray());
  478. WriteVariable(exp.valueType, entry, value);
  479. WriteLine();
  480. }
  481. variableNames[exp] = entry;
  482. }
  483. }
  484. public StringBuilder builder { get { return m_Builder; } }
  485. // Private stuff
  486. private void Indent()
  487. {
  488. ++m_Indent;
  489. Write(kIndentStr);
  490. }
  491. private void Deindent()
  492. {
  493. if (m_Indent == 0)
  494. throw new InvalidOperationException("Cannot de-indent as current indentation is 0");
  495. --m_Indent;
  496. m_Builder.Remove(m_Builder.Length - kIndentStr.Length, kIndentStr.Length); // remove last indent
  497. }
  498. private void WriteIndent()
  499. {
  500. for (int i = 0; i < m_Indent; ++i)
  501. m_Builder.Append(kIndentStr);
  502. }
  503. private StringBuilder m_Builder = new StringBuilder();
  504. private int m_Indent = 0;
  505. private const string kIndentStr = " ";
  506. }
  507. }