4 using System.Collections.Concurrent;
5 using System.Collections.Generic;
8 using SiliconStudio.Assets;
9 using SiliconStudio.Core;
10 using SiliconStudio.Core.Mathematics;
11 using SiliconStudio.Core.Serialization.Contents;
12 using SiliconStudio.Paradox.Assets.Effect;
13 using SiliconStudio.Paradox.Effects;
14 using SiliconStudio.Paradox.Effects.Data;
15 using SiliconStudio.Paradox.Graphics;
16 using SiliconStudio.Paradox.Shaders.Parser.Ast;
17 using SiliconStudio.Shaders.Ast;
19 namespace SiliconStudio.
Paradox.Assets.Materials.Nodes
21 [ContentSerializer(typeof(DataContentSerializer<MaterialShaderClassNode>))]
22 [DataContract(
"MaterialShaderClassNode")]
25 #region Public properties
39 return mixinReference;
43 mixinReference = value;
63 public Dictionary<string, IMaterialNode> CompositionNodes {
get; set; }
76 #region Private members
86 #region Constructor & public methods
92 CompositionNodes =
new Dictionary<string, IMaterialNode>();
99 foreach (var composition
in CompositionNodes)
101 if (composition.Value != null)
103 KeyValuePair<string, IMaterialNode> composition1 = composition;
104 yield
return new MaterialNodeEntry(composition.Value, node => CompositionNodes[composition1.Key] = node);
109 if (materialContext != null && materialContext.ExploreGenerics)
111 foreach (var gen
in Generics)
115 var foundNode = materialContext.Material.FindNode(((NodeParameterTexture)gen.Value).Reference);
116 if (foundNode != null)
126 public void PrepareNode(ConcurrentDictionary<string, string> projectShaders)
128 if (!MixinReference.HasLocation())
134 var newCompositionNodes =
new Dictionary<string, IMaterialNode>();
137 var localMixinName = Path.GetFileNameWithoutExtension(MixinReference.Location);
141 if (projectShaders.TryGetValue(localMixinName, out source))
143 shader = MaterialNodeClassLoader.GetLoader().ParseShader(source);
147 shader = MaterialNodeClassLoader.GetLoader().GetShader(localMixinName);
153 var acceptLinkedVariable =
true;
155 foreach (var
generic in shader.ShaderGenerics)
157 if (
generic.Type.Name.Text ==
"float4")
158 AddKey<Vector4>(generic.Name.Text, newGenerics);
159 else if (
generic.Type.Name.Text ==
"float3")
160 AddKey<Vector3>(generic.Name.Text, newGenerics);
161 else if (
generic.Type.Name.Text ==
"float2")
162 AddKey<Vector2>(generic.Name.Text, newGenerics);
163 else if (
generic.Type.Name.Text ==
"float")
164 AddKey<float>(generic.Name.Text, newGenerics);
165 else if (
generic.Type.Name.Text ==
"int")
166 AddKey<int>(generic.Name.Text, newGenerics);
167 else if (
generic.Type.Name.Text ==
"Texture2D")
168 AddKey<Texture2D>(generic.Name.Text, newGenerics);
169 else if (
generic.Type.Name.Text ==
"SamplerState")
170 AddKey<SamplerState>(generic.Name.Text, newGenerics);
172 AddKey<string>(generic.Name.Text, newGenerics);
175 acceptLinkedVariable =
false;
178 foreach (var member
in shader.Members.OfType<
Variable>())
181 if (member.Type is
TypeName && (member.Type.TypeInference == null || member.Type.TypeInference.TargetType == null))
184 if (member.Type.Name.Text ==
"ComputeColor")
186 if (CompositionNodes.ContainsKey(member.Name.Text))
187 newCompositionNodes.Add(member.Name.Text, CompositionNodes[member.Name.Text]);
189 newCompositionNodes.Add(member.Name.Text, null);
195 string linkName = null;
196 var isStage = member.Qualifiers.Contains(ParadoxStorageQualifier.Stage);
197 var isStream = member.Qualifiers.Contains(ParadoxStorageQualifier.Stream);
198 foreach (var annotation
in member.Attributes.OfType<SiliconStudio.Shaders.Ast.Hlsl.AttributeDeclaration>())
200 if (annotation.Name ==
"Color")
202 if (acceptLinkedVariable && annotation.Name ==
"Link" && annotation.Parameters.Count > 0)
203 linkName = (string)annotation.Parameters[0].Value;
206 if (!isStream && (isStage || !
string.IsNullOrEmpty(linkName)))
208 if (linkName == null)
209 linkName = localMixinName +
"." + member.Name.Text;
211 var memberType = member.Type.ResolveType();
214 AddMember<Color4>(linkName, newMembers);
218 AddMember<float>(linkName, newMembers);
222 AddMember<double>(linkName, newMembers);
226 AddMember<int>(linkName, newMembers);
230 AddMember<uint>(linkName, newMembers);
234 AddMember<bool>(linkName, newMembers);
238 switch (((VectorType)memberType).Dimension)
241 AddMember<Vector2>(linkName, newMembers);
244 AddMember<Vector3>(linkName, newMembers);
247 AddMember<Vector4>(linkName, newMembers);
251 else if (member.Type.Name.Text ==
"Texture2D")
255 else if (member.Type.Name.Text ==
"SamplerState")
263 Generics = newGenerics;
264 CompositionNodes = newCompositionNodes;
265 Members = newMembers;
272 if (MixinReference != null)
274 foreach (var keyValue
in Members)
276 var expectedType = keyValue.Value.GetType();
277 if (expectedType == typeof(
Color4))
279 AddToCollection<Color4>(keyValue.Key, (
Color4)keyValue.Value, collection);
281 else if (expectedType == typeof(
float))
283 AddToCollection<float>(keyValue.Key, (float)keyValue.Value, collection);
285 else if (expectedType == typeof(
double))
287 AddToCollection<double>(keyValue.Key, (double)keyValue.Value, collection);
289 else if (expectedType == typeof(
int))
291 AddToCollection<int>(keyValue.Key, (int)keyValue.Value, collection);
293 else if (expectedType == typeof(uint))
295 AddToCollection<uint>(keyValue.Key, (uint)keyValue.Value, collection);
297 else if (expectedType == typeof(
bool))
299 AddToCollection<bool>(keyValue.Key, (bool)keyValue.Value, collection);
301 else if (expectedType == typeof(
Vector2))
303 AddToCollection<Vector2>(keyValue.Key, (
Vector2)keyValue.Value, collection);
305 else if (expectedType == typeof(
Vector3))
307 AddToCollection<Vector3>(keyValue.Key, (
Vector3)keyValue.Value, collection);
309 else if (expectedType == typeof(
Vector4))
311 AddToCollection<Vector4>(keyValue.Key, (
Vector4)keyValue.Value, collection);
316 if (matContext != null)
318 var textureNode = matContext.Material.FindNode(((
NodeParameterTexture)keyValue.Value).Reference);
319 if (textureNode != null)
340 #region Private methods
350 var pk = GetTypedParameterKey<T>(linkName);
353 Type expectedType = null;
355 if (pk.PropertyType == typeof(Graphics.Texture))
360 else if (pk.PropertyType == typeof(Graphics.SamplerState))
367 expectedType = pk.PropertyType;
368 defaultValue = pk.DefaultMetadataT.DefaultValue;
371 if (Members.ContainsKey(pk))
373 var value = Members[pk];
374 if (value.GetType() == expectedType)
375 members.Add(pk, value);
378 members.Add(pk, defaultValue);
388 private void AddKey<T>(
string keyName, GenericDictionary generics)
390 INodeParameter nodeParameter;
391 var typeT = typeof(T);
392 if (typeT == typeof(
string))
393 nodeParameter =
new NodeParameter();
395 nodeParameter =
new NodeParameterTexture();
396 else if (typeT == typeof(
float))
397 nodeParameter =
new NodeParameterFloat();
398 else if (typeT == typeof(
int))
399 nodeParameter =
new NodeParameterInt();
400 else if (typeT == typeof(
Vector2))
401 nodeParameter =
new NodeParameterFloat2();
402 else if (typeT == typeof(
Vector3))
403 nodeParameter =
new NodeParameterFloat3();
404 else if (typeT == typeof(
Vector4))
405 nodeParameter =
new NodeParameterFloat4();
407 nodeParameter =
new NodeParameterSampler();
409 throw new Exception(
"Unsupported generic format");
411 if (Generics.ContainsKey(keyName))
413 var gen = Generics[keyName];
414 if (gen == null || gen.GetType() != nodeParameter.GetType())
415 generics[keyName] = nodeParameter;
417 generics[keyName] = gen;
420 generics.Add(keyName, nodeParameter);
434 collection.Set(pk, value);
439 #region Private static methods
447 private static ParameterKey<T> GetTypedParameterKey<T>(
string linkName)
449 var pk = ParameterKeys.FindByName(linkName);
452 if (pk.PropertyType == typeof(T))
453 return (ParameterKey<T>)pk;
Base class for all vector types
static readonly ScalarType Bool
Scalar bool.
Key of an effect parameter.
Base implementation for IMaterialNode.
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
static readonly ScalarType Int
Scalar int.
Represents a two dimensional mathematical vector.
A custom dictionary to keep track of the order the elements were inserted.
void PrepareNode(ConcurrentDictionary< string, string > projectShaders)
Load the shader and extract the information.
An entry to a nested IMaterialNode
Key of an gereric effect parameter.
Represents a three dimensional mathematical vector.
Data type for SiliconStudio.Paradox.Effects.ParameterCollection.
Represents a color in the form of rgba.
static readonly ScalarType Half
Scalar half.
static readonly ScalarType UInt
Scalar unsigned int.
static readonly ScalarType Float
Sclar float.
override string ToString()
Represents a four dimensional mathematical vector.
A Texture 2D frontend to SharpDX.Direct3D11.Texture2D.
static readonly ScalarType Double
Scalar double.
override IEnumerable< MaterialNodeEntry > GetChildren(object context=null)
Gets the children. The context to get the children.The list of children.
SiliconStudio.Core.Mathematics.Vector3 Vector3
ParameterCollectionData GetParameters(object context)
MaterialShaderClassNode()
Base class for texture resources.