No Description
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.

AbstractShaderProperty.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace UnityEditor.ShaderGraph.Internal
  5. {
  6. [Serializable]
  7. public abstract class AbstractShaderProperty : ShaderInput
  8. {
  9. public abstract PropertyType propertyType { get; }
  10. internal override ConcreteSlotValueType concreteShaderValueType => propertyType.ToConcreteShaderValueType();
  11. // user selected precision setting
  12. [SerializeField]
  13. Precision m_Precision = Precision.Inherit;
  14. [Obsolete("AbstractShaderProperty.gpuInstanced is no longer used")]
  15. public bool gpuInstanced
  16. {
  17. get { return false; }
  18. set { }
  19. }
  20. internal virtual string GetHLSLVariableName(bool isSubgraphProperty, GenerationMode mode)
  21. {
  22. if (mode == GenerationMode.VFX)
  23. {
  24. // Per-element exposed properties are provided by the properties structure filled by VFX.
  25. if (overrideHLSLDeclaration)
  26. return $"PROP.{referenceName}";
  27. // For un-exposed global properties, just read from the cbuffer.
  28. else
  29. return referenceName;
  30. }
  31. return referenceName;
  32. }
  33. internal string GetConnectionStateHLSLVariableName()
  34. {
  35. return GetConnectionStateVariableName(referenceName + "_" + objectId);
  36. }
  37. // NOTE: this does not tell you the HLSLDeclaration of the entire property...
  38. // instead, it tells you what the DEFAULT HLSL Declaration would be, IF the property makes use of the default
  39. // to check ACTUAL HLSL Declaration types, enumerate the HLSL Properties and check their HLSLDeclarations...
  40. internal virtual HLSLDeclaration GetDefaultHLSLDeclaration()
  41. {
  42. if (overrideHLSLDeclaration)
  43. return hlslDeclarationOverride;
  44. // default Behavior switches between UnityPerMaterial and Global based on Exposed checkbox
  45. if (generatePropertyBlock)
  46. return HLSLDeclaration.UnityPerMaterial;
  47. else
  48. return HLSLDeclaration.Global;
  49. }
  50. // by default we disallow UI from choosing "DoNotDeclare"
  51. // it needs a bit more UI support to disable property node output slots before we make it public
  52. internal virtual bool AllowHLSLDeclaration(HLSLDeclaration decl) => (decl != HLSLDeclaration.DoNotDeclare);
  53. [SerializeField]
  54. internal bool overrideHLSLDeclaration = false;
  55. [SerializeField]
  56. internal HLSLDeclaration hlslDeclarationOverride;
  57. override internal bool isExposed => base.isExposed && shouldForceExposed;
  58. internal bool shouldForceExposed => (hlslDeclarationOverride == HLSLDeclaration.HybridPerInstance || GetDefaultHLSLDeclaration() == HLSLDeclaration.UnityPerMaterial) && isExposable;
  59. internal Precision precision
  60. {
  61. get => m_Precision;
  62. set => m_Precision = value;
  63. }
  64. ConcretePrecision m_ConcretePrecision = ConcretePrecision.Single;
  65. public ConcretePrecision concretePrecision => m_ConcretePrecision;
  66. internal void SetupConcretePrecision(ConcretePrecision defaultPrecision)
  67. {
  68. m_ConcretePrecision = precision.ToConcrete(defaultPrecision, defaultPrecision);
  69. }
  70. [SerializeField]
  71. bool m_Hidden = false;
  72. public bool hidden
  73. {
  74. get => m_Hidden;
  75. set => m_Hidden = value;
  76. }
  77. internal string hideTagString => hidden || (shouldForceExposed && !isExposed) ? "[HideInInspector]" : "";
  78. // reference names are the HLSL declaration name / property block ref name
  79. internal virtual void GetPropertyReferenceNames(List<string> result)
  80. {
  81. result.Add(referenceName);
  82. }
  83. // display names are used as the UI name in the property block / show up in the Material Inspector
  84. internal virtual void GetPropertyDisplayNames(List<string> result)
  85. {
  86. result.Add(displayName);
  87. }
  88. // the simple interface for simple properties
  89. internal virtual string GetPropertyBlockString()
  90. {
  91. return string.Empty;
  92. }
  93. internal bool shouldGeneratePropertyBlock => (generatePropertyBlock || shouldForceExposed)
  94. && GetDefaultHLSLDeclaration() != HLSLDeclaration.Global;
  95. // the more complex interface for complex properties (defaulted for simple properties)
  96. internal virtual void AppendPropertyBlockStrings(ShaderStringBuilder builder)
  97. {
  98. builder.AppendLine(GetPropertyBlockString());
  99. }
  100. internal abstract void ForeachHLSLProperty(Action<HLSLProperty> action);
  101. internal virtual string GetPropertyAsArgumentStringForVFX(string precisionString)
  102. {
  103. return GetPropertyAsArgumentString(precisionString);
  104. }
  105. internal abstract string GetPropertyAsArgumentString(string precisionString);
  106. internal abstract AbstractMaterialNode ToConcreteNode();
  107. internal abstract PreviewProperty GetPreviewMaterialProperty();
  108. public virtual string GetPropertyTypeString()
  109. {
  110. var typeString = propertyType.ToString();
  111. if (sgVersion < latestVersion)
  112. typeString = $"{typeString} (Legacy v{sgVersion})";
  113. return typeString;
  114. }
  115. }
  116. [Serializable]
  117. public abstract class AbstractShaderProperty<T> : AbstractShaderProperty
  118. {
  119. [SerializeField]
  120. T m_Value;
  121. public virtual T value
  122. {
  123. get => m_Value;
  124. set => m_Value = value;
  125. }
  126. }
  127. // class for extracting deprecated data from older versions of AbstractShaderProperty
  128. class LegacyShaderPropertyData
  129. {
  130. // indicates user wishes to support the HYBRID renderer GPU instanced path
  131. [SerializeField]
  132. public bool m_GPUInstanced = false;
  133. // converts the old m_GPUInstanced data into the new override HLSLDeclaration system.
  134. public static void UpgradeToHLSLDeclarationOverride(string json, AbstractShaderProperty property)
  135. {
  136. // this maintains the old behavior for versioned properties:
  137. // old exposed GPUInstanced properties are declared hybrid (becomes override in new system)
  138. // old unexposed GPUInstanced properties are declared global (becomes override in new system)
  139. // old exposed properties are declared UnityPerMaterial (default behavior, no override necessary)
  140. // old unexposed properties are declared Global (default behavior, no override necessary)
  141. // moving forward, users can use the overrides directly to control what it does
  142. var legacyShaderPropertyData = new LegacyShaderPropertyData();
  143. JsonUtility.FromJsonOverwrite(json, legacyShaderPropertyData);
  144. if (legacyShaderPropertyData.m_GPUInstanced)
  145. {
  146. property.overrideHLSLDeclaration = true;
  147. if (property.generatePropertyBlock)
  148. property.hlslDeclarationOverride = HLSLDeclaration.HybridPerInstance;
  149. else
  150. property.hlslDeclarationOverride = HLSLDeclaration.Global;
  151. }
  152. }
  153. }
  154. public enum HLSLType
  155. {
  156. // value types
  157. _float,
  158. _float2,
  159. _float3,
  160. _float4,
  161. _matrix4x4,
  162. // object types
  163. FirstObjectType,
  164. _Texture2D = FirstObjectType,
  165. _Texture3D,
  166. _TextureCube,
  167. _Texture2DArray,
  168. _SamplerState,
  169. // custom type
  170. _CUSTOM
  171. }
  172. // describes the different ways we can generate HLSL declarations
  173. [Flags]
  174. internal enum HLSLDeclaration
  175. {
  176. DoNotDeclare, // NOT declared in HLSL
  177. Global, // declared in the global scope, mainly for use with state coming from Shader.SetGlobal*()
  178. UnityPerMaterial, // declared in the UnityPerMaterial cbuffer, populated by Material or MaterialPropertyBlock
  179. HybridPerInstance, // declared using HybridRenderer path (v1 or v2) to get DOTS GPU instancing
  180. }
  181. internal struct HLSLProperty
  182. {
  183. public string name;
  184. public HLSLType type;
  185. public ConcretePrecision precision;
  186. public HLSLDeclaration declaration;
  187. public Action<ShaderStringBuilder> customDeclaration;
  188. public HLSLProperty(HLSLType type, string name, HLSLDeclaration declaration, ConcretePrecision precision = ConcretePrecision.Single)
  189. {
  190. this.type = type;
  191. this.name = name;
  192. this.declaration = declaration;
  193. this.precision = precision;
  194. this.customDeclaration = null;
  195. }
  196. public bool ValueEquals(HLSLProperty other)
  197. {
  198. if ((name != other.name) ||
  199. (type != other.type) ||
  200. (precision != other.precision) ||
  201. (declaration != other.declaration) ||
  202. ((customDeclaration == null) != (other.customDeclaration == null)))
  203. {
  204. return false;
  205. }
  206. else if (customDeclaration != null)
  207. {
  208. var ssb = new ShaderStringBuilder();
  209. var ssbother = new ShaderStringBuilder();
  210. customDeclaration(ssb);
  211. other.customDeclaration(ssbother);
  212. if (ssb.ToCodeBlock() != ssbother.ToCodeBlock())
  213. return false;
  214. }
  215. return true;
  216. }
  217. static string[,] kValueTypeStrings = new string[(int)HLSLType.FirstObjectType, 2]
  218. {
  219. {"float", "half"},
  220. {"float2", "half2"},
  221. {"float3", "half3"},
  222. {"float4", "half4"},
  223. {"float4x4", "half4x4"}
  224. };
  225. static string[] kObjectTypeStrings = new string[(int)HLSLType._CUSTOM - (int)HLSLType.FirstObjectType]
  226. {
  227. "TEXTURE2D",
  228. "TEXTURE3D",
  229. "TEXTURECUBE",
  230. "TEXTURE2D_ARRAY",
  231. "SAMPLER",
  232. };
  233. public bool IsObjectType()
  234. {
  235. return type == HLSLType._SamplerState ||
  236. type == HLSLType._Texture2D ||
  237. type == HLSLType._Texture3D ||
  238. type == HLSLType._TextureCube ||
  239. type == HLSLType._Texture2DArray ||
  240. type == HLSLType._CUSTOM;
  241. }
  242. public string GetValueTypeString()
  243. {
  244. if (type < HLSLType.FirstObjectType)
  245. return kValueTypeStrings[(int)type, (int)precision];
  246. return null;
  247. }
  248. public void AppendTo(ShaderStringBuilder ssb, Func<string, string> nameModifier = null)
  249. {
  250. var mName = nameModifier?.Invoke(name) ?? name;
  251. if (type < HLSLType.FirstObjectType)
  252. {
  253. ssb.Append(kValueTypeStrings[(int)type, (int)precision]);
  254. ssb.Append(" ");
  255. ssb.Append(mName);
  256. ssb.Append(";");
  257. }
  258. else if (type < HLSLType._CUSTOM)
  259. {
  260. ssb.Append(kObjectTypeStrings[type - HLSLType.FirstObjectType]);
  261. ssb.Append("(");
  262. ssb.Append(mName);
  263. ssb.Append(");");
  264. }
  265. else
  266. {
  267. customDeclaration(ssb);
  268. }
  269. //ssb.Append(" // ");
  270. //ssb.Append(declaration.ToString());
  271. ssb.AppendNewLine();
  272. }
  273. }
  274. }