Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
HlslSemanticAnalysis.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.Globalization;
6 using System.Linq;
7 using SiliconStudio.Shaders.Grammar.Hlsl;
8 using SiliconStudio.Shaders.Ast;
9 using SiliconStudio.Shaders.Ast.Hlsl;
10 using SiliconStudio.Shaders.Parser;
11 using SiliconStudio.Shaders.Parser.Hlsl;
12 using SiliconStudio.Shaders.Properties;
13 using SiliconStudio.Shaders.Utility;
15 
16 namespace SiliconStudio.Shaders.Analysis.Hlsl
17 {
18  /// <summary>
19  /// A Type reference analysis is building type references.
20  /// </summary>
22  {
23  private static readonly Object lockInit = new Object();
24  private static bool isInitialized = false;
25  protected readonly static List<IDeclaration> defaultDeclarations = new List<IDeclaration>();
26  private readonly static Dictionary<string, TypeBase> BuiltinObjects = new Dictionary<string, TypeBase>();
27  private readonly static Dictionary<GenericInstanceKey, TypeBase> InstanciatedTypes = new Dictionary<GenericInstanceKey, TypeBase>();
28 
29  #region Constructors and Destructors
30 
31  /// <summary>
32  /// Initializes a new instance of the <see cref="HlslSemanticAnalysis"/> class.
33  /// </summary>
34  /// <param name="result">
35  /// The result.
36  /// </param>
37  protected HlslSemanticAnalysis(ParsingResult result) : base(result)
38  {
39  }
40 
41  #endregion
42 
43  #region Public Methods
44 
45  private static readonly object SwizzleTag = "MatrixSwizzleDecode";
46 
47 
48  /// <summary>
49  /// Decodes the swizzle.
50  /// </summary>
51  /// <param name="memberReference">The member reference.</param>
52  /// <param name="result">The result.</param>
53  /// <returns></returns>
54  public static List<MatrixType.Indexer> MatrixSwizzleDecode(MemberReferenceExpression memberReference, ParsingResult result = null)
55  {
56  string components = memberReference.Member.Text;
57 
58  var matrixDecl = (MatrixType)(memberReference.Target.TypeInference.TargetType);
59 
60  var span = matrixDecl.Span;
61 
62  var swizzles = (List<MatrixType.Indexer>)memberReference.GetTag(SwizzleTag);
63  if (swizzles != null)
64  return swizzles;
65 
66  swizzles = new List<MatrixType.Indexer>();
67 
68  if (components.StartsWith("_"))
69  {
70  string[] splitComponents;
71  int indexOffset = 0;
72  if (components.StartsWith("_m"))
73  {
74  splitComponents = components.Split(new[] { "_m" }, StringSplitOptions.RemoveEmptyEntries);
75  }
76  else
77  {
78  splitComponents = components.Split(new[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
79  indexOffset = 1;
80  }
81 
82  int dimension = 0;
83 
84  if (splitComponents.Length == 0 && result != null)
85  {
86  result.Error(MessageCode.ErrorMatrixInvalidMemberReference, span, components);
87  }
88 
89  foreach (var splitComponent in splitComponents)
90  {
91  if (splitComponent.Length != 2 || !IsValidIndex(span, splitComponent[0], indexOffset, indexOffset + 3) || !IsValidIndex(span, splitComponent[1], indexOffset, indexOffset + 3))
92  {
93  swizzles.Clear();
94  break;
95  }
96 
97  swizzles.Add(new MatrixType.Indexer(int.Parse(splitComponent[0].ToString()) - indexOffset, int.Parse(splitComponent[1].ToString()) - indexOffset));
98  dimension++;
99  }
100  }
101 
102  memberReference.SetTag(SwizzleTag, swizzles);
103  return swizzles;
104  }
105 
106  [Visit]
107  public void Visit(AsmExpression asmExpression)
108  {
109 
110  }
111 
112  [Visit]
113  public void Visit(Annotations annotations)
114  {
115 
116  }
117 
118  [Visit]
119  public void Visit(CastExpression castExpression)
120  {
121  Visit((Node)castExpression);
122 
123  var targetType = castExpression.Target.ResolveType();
124  castExpression.TypeInference = (TypeInference)castExpression.Target.TypeInference.Clone();
125  if (castExpression.TypeInference.TargetType == null)
126  castExpression.TypeInference.TargetType = targetType;
127  }
128 
129  protected override void Visit(MethodInvocationExpression expression)
130  {
131  var methodAsVariable = expression.Target as VariableReferenceExpression;
132 
133  // We are not parsing CompileShader methods
134  if (methodAsVariable != null)
135  {
136  switch (methodAsVariable.Name.Text)
137  {
138  case "ConstructGSWithSO":
139  case "CompileShader":
140  return;
141  }
142  }
143 
144  base.Visit(expression);
145  }
146 
147 
148  [Visit]
149  public virtual void Visit(CompileExpression compileExpression)
150  {
151  // Visit((Node)compileExpression);
152  //Warning("TypeInference on CompileExpression is not handled", compileExpression.Span);
153  }
154 
155  [Visit]
156  public virtual void Visit(StateExpression stateExpression)
157  {
158  // Visit((Node)stateExpression);
159  // Warning("TypeInference on StateExpression is not handled", stateExpression.Span);
160  }
161 
162  [Visit]
163  public virtual void Visit(Technique technique)
164  {
165  // Force to not visit a techniques
166  }
167 
168 
169  /// <summary>
170  /// Visits the specified type name.
171  /// </summary>
172  /// <param name="typeName">Name of the type.</param>
173  [Visit]
174  protected virtual void Visit(GenericType genericType)
175  {
176  Visit((Node)genericType);
177 
178  string genericName = genericType.Name.Text;
179 
180  // Get the case insenti
181  var value = TextureType.Parse(genericName);
182  if (value != null)
183  genericName = value.Name.Text;
184 
185  TypeBase typeBase;
186  if (BuiltinObjects.TryGetValue(genericName, out typeBase))
187  {
188  genericType.TypeInference.TargetType = GetGenericInstance(genericName, genericType, typeBase);
189  }
190  }
191 
192  protected override IEnumerable<IDeclaration> FindDeclarationsFromObject(TypeBase typeBase, string memberName)
193  {
194  if (typeBase is ClassType)
195  {
196  var classType = (ClassType)typeBase;
197 
198  foreach (var declaration in classType.Members.OfType<IDeclaration>().Where(node => node.Name == memberName))
199  yield return declaration;
200 
201  foreach (var baseType in classType.BaseClasses)
202  foreach (var baseDeclaration in FindDeclarationsFromObject(baseType.ResolveType(), memberName))
203  yield return baseDeclaration;
204  }
205  else if (typeBase is InterfaceType)
206  {
207  var interfaceType = (InterfaceType)typeBase;
208 
209  foreach (var declaration in interfaceType.Methods.OfType<IDeclaration>().Where(node => node.Name == memberName))
210  yield return declaration;
211  }
212  /*else if (typeBase is StructType)
213  {
214  var structType = (StructType)typeBase;
215  foreach (var declaration in structType.Fields.Where(node => node.Name == memberName))
216  yield return declaration;
217  }*/
218  }
219 
220  protected override void ProcessMethodInvocation(MethodInvocationExpression expression, string methodName, List<IDeclaration> declarations)
221  {
222  // Check for typedef method
223  if (methodName != null)
224  {
225  var varExp = expression.Target as VariableReferenceExpression;
226 
227  if (varExp != null)
228  {
229  var typeDefDeclarator = declarations.OfType<Typedef>().FirstOrDefault();
230  if (typeDefDeclarator != null)
231  {
232  varExp.TypeInference.Declaration = typeDefDeclarator;
233  varExp.TypeInference.TargetType = typeDefDeclarator.ResolveType();
234 
235  expression.TypeInference.Declaration = typeDefDeclarator;
236  expression.TypeInference.TargetType = typeDefDeclarator.ResolveType();
237  return;
238  }
239 
240  //var builtInFunction = defaultDeclarations.FirstOrDefault(x => x.Name.Text == varExp.Name.Text && TestParametersType(expression, x)) as MethodDeclaration;
241  var builtInFunction = defaultDeclarations.FirstOrDefault(x => (x as MethodDeclaration != null) && (x as MethodDeclaration).IsSameSignature(expression)) as MethodDeclaration;
242  if (builtInFunction != null)
243  {
244  varExp.TypeInference.Declaration = builtInFunction;
245  varExp.TypeInference.TargetType = builtInFunction.ReturnType.ResolveType();
246 
247  expression.TypeInference.Declaration = builtInFunction;
248  expression.TypeInference.TargetType = builtInFunction.ReturnType.ResolveType();
249  return;
250  }
251  }
252  }
253 
254  base.ProcessMethodInvocation(expression, methodName, declarations);
255  }
256 
257  [Visit]
258  protected virtual void Visit(TextureType textureType)
259  {
260  Visit((Node)textureType);
261 
262  AssociatePredefinedObjects(textureType);
263  }
264 
265  private void AssociatePredefinedObjects(TypeBase typebase)
266  {
267  // Use the returned name in order to support case insensitive names
268  TypeBase predefinedType;
269  if (typebase.TypeInference.TargetType == null && BuiltinObjects.TryGetValue(typebase.Name.Text, out predefinedType))
270  {
271  var textureType = new GenericType<TypeBase>();
272  textureType.Parameters[0] = VectorType.Float4;
273 
274  typebase.TypeInference.TargetType = GetGenericInstance(typebase.Name.Text, textureType, predefinedType);
275  }
276  }
277 
278  /// <summary>
279  /// Visits the specified type name.
280  /// </summary>
281  /// <param name="typeName">Name of the type.</param>
282  [Visit]
283  protected override TypeBase Visit(TypeName typeName)
284  {
285  Visit((Node)typeName);
286 
287  var name = typeName.Name.Text;
288 
289  // Substitute case insensitive types to case sensitive types
290  // TODO this is temporary. We need to found a better workaround.
291  TypeBase value = TextureType.Parse(name);
292  if (value != null)
293  {
294  AssociatePredefinedObjects(value);
295  return value;
296  }
297 
298  value = StreamTypeName.Parse(name);
299  if (value != null)
300  {
301  AssociatePredefinedObjects(value);
302  return value;
303  }
304 
305  value = SamplerType.Parse(name);
306  if (value != null)
307  return value;
308 
309  value = StateType.Parse(name);
310  if (value != null)
311  return value;
312 
313  // Replace shader objects
314  if (name == "VertexShader" || name == "GeometryShader" || name == "PixelShader")
315  return new ObjectType(name);
316 
317  // Else call the base
318  return base.Visit(typeName);
319  }
320 
321  private static bool IsValidIndex(SourceSpan span, char valueChar, int min, int max, ParsingResult result = null)
322  {
323  int value;
324  var isParseOk = int.TryParse(valueChar.ToString(CultureInfo.InvariantCulture), out value);
325 
326  if (!isParseOk || value < min || value > max)
327  {
328  if (result != null)
329  result.Error(MessageCode.ErrorMatrixInvalidIndex, span, valueChar, min, max);
330  return false;
331  }
332 
333  return true;
334  }
335 
336  /// <summary>
337  /// Visits the specified member reference.
338  /// </summary>
339  /// <param name="memberReference">The member reference.</param>
340  protected override void CommonVisit(MemberReferenceExpression memberReference)
341  {
342  var thisType = memberReference.Target.TypeInference.TargetType;
343 
344  if (thisType is MatrixType)
345  {
346  FindMemberTypeReference((MatrixType)thisType, memberReference);
347  }
348  else
349  {
350  base.CommonVisit(memberReference);
351  }
352  }
353 
354  /// <summary>
355  /// Finds the member type reference.
356  /// </summary>
357  /// <param name="matrixType">Type of the matrix.</param>
358  /// <param name="memberReference">The member reference.</param>
359  protected virtual void FindMemberTypeReference(MatrixType matrixType, MemberReferenceExpression memberReference)
360  {
361  var components = memberReference.Member.Text;
362  var span = memberReference.Span;
363 
364  // A matrix contains values organized in rows and columns, which can be accessed using the structure operator "." followed by one of two naming sets:
365  // The zero-based row-column position:
366  // _m00, _m01, _m02, _m03
367  // _m10, _m11, _m12, _m13
368  // _m20, _m21, _m22, _m23
369  // _m30, _m31, _m32, _m33
370  // The one-based row-column position:
371  // _11, _12, _13, _14
372  // _21, _22, _23, _24
373  // _31, _32, _33, _34
374  // _41, _42, _43, _44
375  var swizzles = MatrixSwizzleDecode(memberReference, ParsingResult);
376 
377  if (swizzles.Count > 0)
378  {
379  var itemType = matrixType.Type.ResolveType();
380  memberReference.TypeInference.TargetType = swizzles.Count == 1 ? itemType : new VectorType((ScalarType)itemType, swizzles.Count);
381  }
382  }
383 
384  #endregion
385 
386  #region Methods
387 
388  private static TypeBase GetGenericInstance(string typename, GenericType genericType, TypeBase predefinedType)
389  {
390  var key = new GenericInstanceKey(typename, genericType.Parameters);
391 
392  TypeBase instanciatedType;
393  if (!InstanciatedTypes.TryGetValue(key, out instanciatedType))
394  {
395  instanciatedType = genericType.MakeGenericInstance(predefinedType);
396  InstanciatedTypes.Add(key, instanciatedType);
397  }
398  return instanciatedType;
399  }
400 
401  protected static void InitializeBuiltins()
402  {
403  foreach (var function in Function.Functions)
404  {
405  foreach (var p in EnumerateParameters(function.Parameters[0]))
406  {
407  var returnType = function.Return(function, new[] { p });
408  var parameterTypes = function.ParamList(function, new[] { p });
409 
410  var methodDeclaration = new MethodDeclaration();
411  methodDeclaration.IsBuiltin = true;
412  methodDeclaration.Name = new Identifier(function.Name);
413  methodDeclaration.ReturnType = returnType;
414 
415  foreach (var parameterType in parameterTypes)
416  methodDeclaration.Parameters.Add( new Ast.Parameter { DeclaringMethod = methodDeclaration, Type = parameterType } );
417 
418  defaultDeclarations.Add(methodDeclaration);
419  }
420  }
421 
422  defaultDeclarations.AddRange(declaredMethods);
423 
424  foreach (var methodDeclaration in declaredMethods)
425  {
426  var newMethodDeclaration = new MethodDeclaration();
427  newMethodDeclaration.IsBuiltin = true;
428  newMethodDeclaration.Name = new Identifier(methodDeclaration.Name);
429  newMethodDeclaration.ReturnType = methodDeclaration.ReturnType;
430 
431  foreach (var parameter in methodDeclaration.Parameters)
432  {
433  var parameterType = parameter.Type;
434  if (parameterType is SamplerType)
435  {
436  parameterType = SamplerType.Sampler;
437  }
438 
439  newMethodDeclaration.Parameters.Add(new Ast.Parameter { DeclaringMethod = newMethodDeclaration, Type = parameterType });
440  }
441  defaultDeclarations.Add(newMethodDeclaration);
442  }
443 
444  // adding remaining functions that doesn't have multiple versions
445  defaultDeclarations.Add(GenericMethod("AllMemoryBarrier", TypeBase.Void));
446  defaultDeclarations.Add(GenericMethod("AllMemoryBarrierWithGroupSync", TypeBase.Void));
447  defaultDeclarations.Add(GenericMethod("D3DCOLORtoUBYTE4", VectorType.Int4, GenericParam("x", VectorType.Float4)));
448  defaultDeclarations.Add(GenericMethod("DeviceMemoryBarrier", TypeBase.Void));
449  defaultDeclarations.Add(GenericMethod("DeviceMemoryBarrierWithGroupSync", TypeBase.Void));
450  defaultDeclarations.Add(GenericMethod("GetRenderTargetSampleCount", ScalarType.UInt));
451  defaultDeclarations.Add(GenericMethod("GetRenderTargetSamplePosition", ScalarType.UInt, GenericParam("x", ScalarType.Int)));
452  defaultDeclarations.Add(GenericMethod("GroupMemoryBarrier", TypeBase.Void));
453  }
454 
455  public static List<IDeclaration> ParseBuiltin(string builtins, string fileName)
456  {
457  var builtinDeclarations = new List<IDeclaration>();
458 
459  var result = HlslParser.TryPreProcessAndParse(builtins, fileName);
460 
461  // Check that we parse builtins successfully.
462  var shader = ShaderParser.Check(result, fileName);
463 
464  foreach (var declaration in shader.Declarations)
465  {
466  var classType = declaration as ClassType;
467  if (classType != null)
468  {
469  classType.Name.Text = classType.Name.Text.Trim('_');
470  BuiltinObjects.Add(classType.Name.Text, classType);
471  classType.IsBuiltIn = true;
472  }
473  else if (declaration is IDeclaration)
474  {
475  var methodDeclaration = declaration as MethodDeclaration;
476  if (methodDeclaration != null)
477  methodDeclaration.IsBuiltin = true;
478  builtinDeclarations.Add((IDeclaration)declaration);
479  }
480  }
481 
482  var tempAnalysis = new HlslSemanticAnalysis(result);
483  tempAnalysis.Run();
484 
485  return builtinDeclarations;
486  }
487 
488  /// <summary>
489  /// Create the required declarations for hlsl parsing
490  /// </summary>
491  /// <param name="builtinDeclarations">the list of declarations</param>
492  protected void SetupHlslAnalyzer(List<IDeclaration> builtinDeclarations = null)
493  {
494  lock (lockInit)
495  {
496  if (!isInitialized)
497  {
498  // Add builtins
499  defaultDeclarations.AddRange(ParseBuiltin(Resources.HlslDeclarations, "internal_hlsl_declarations.hlsl"));
500  InitializeBuiltins();
501  isInitialized = true;
502  }
503  }
504 
505  // Add all default declarations
506  ScopeStack.Peek().AddDeclarations(defaultDeclarations);
507 
508  if (builtinDeclarations != null)
509  {
510  this.ScopeStack.Peek().AddDeclarations(builtinDeclarations);
511 
512  // Tag all method declared as user defined
513  foreach (var builtinDeclaration in builtinDeclarations)
514  {
515  var methodDeclaration = builtinDeclaration as MethodDeclaration;
516  if (methodDeclaration != null)
517  methodDeclaration.SetTag(TagBuiltinUserDefined, true);
518  }
519  }
520  }
521 
522  /// <summary>
523  /// Fill the clone context with the elements of the default declarations
524  /// </summary>
525  /// <param name="cloneContext">the CloneContext</param>
526  public static void FillCloneContext(CloneContext cloneContext)
527  {
528  if (!isInitialized)
529  {
530  // Add builtins
531  defaultDeclarations.AddRange(ParseBuiltin(Resources.HlslDeclarations, "internal_hlsl_declarations.hlsl"));
532  InitializeBuiltins();
533  isInitialized = true;
534  }
535 
536  foreach (var decl in defaultDeclarations)
537  DeepCloner.DeepCollect(decl, cloneContext);
538 
539  foreach (var bInObj in BuiltinObjects)
540  DeepCloner.DeepCollect(bInObj, cloneContext);
541 
542  UpdateCloneContext(cloneContext);
543  }
544 
545  /// <summary>
546  /// Update the clone context with the new instanciated classes
547  /// </summary>
548  /// <param name="cloneContext">the CloneContext</param>
549  public static void UpdateCloneContext(CloneContext cloneContext)
550  {
551  foreach (var instType in InstanciatedTypes)
552  {
553  if (instType.Key.GenericParameters.Any(x => x is TypeName))
554  continue;
555  DeepCloner.DeepCollect(instType.Value, cloneContext);
556  }
557  }
558 
559  public static void Run(ParsingResult toParse, List<IDeclaration> builtinDeclarations = null)
560  {
561  var analysis = new HlslSemanticAnalysis(toParse);
562  analysis.SetupHlslAnalyzer(builtinDeclarations);
563  analysis.Run();
564  }
565 
566  /// <inheritdoc/>
567  protected override IEnumerable<IDeclaration> FindDeclarations(string nameArg)
568  {
569  string name = nameArg;
570  bool isGenericLookingForBaseDeclarator = false;
571  if (name.StartsWith("__") && name.EndsWith("_base"))
572  {
573  name = name.Substring("__".Length);
574  name = name.Substring(0, name.Length - "_base".Length);
575  isGenericLookingForBaseDeclarator = true;
576  }
577 
578  // Override find declaration in order to return generic types from a generic declarator
579  foreach (var scopeDeclaration in ScopeStack)
580  {
581  foreach (var genericDecl in scopeDeclaration.FindGenerics(name))
582  {
583  yield return new GenericDeclaration(genericDecl.Name, genericDecl.Holder, genericDecl.Index, isGenericLookingForBaseDeclarator) { Span = genericDecl.Span };
584  }
585  }
586 
587  foreach (var item in base.FindDeclarations(name))
588  {
589  yield return item;
590  }
591  }
592 
593  private static MethodDeclaration[] declaredMethods = new MethodDeclaration[]
594  {
595  // -----------------------------------------
596  // tex1D functions
597  // -----------------------------------------
598 
599  // ret tex1D(s, t) : http://msdn.microsoft.com/en-us/library/windows/desktop/bb509672%28v=VS.85%29.aspx
600  GenericMethod("tex1D", VectorType.Float4, GenericParam("s", SamplerType.Sampler1D), GenericParam("t", ScalarType.Float)),
601 
602  // ret tex1D(s, t, ddx, ddy) http://msdn.microsoft.com/en-us/library/windows/desktop/ff471388%28v=VS.85%29.aspx
603  GenericMethod("tex1D", VectorType.Float4, GenericParam("s", SamplerType.Sampler1D), GenericParam("t", ScalarType.Float), GenericParam("ddx", ScalarType.Float), GenericParam("ddy", ScalarType.Float)),
604 
605  // ret tex1Dbias(s, t) : http://msdn.microsoft.com/en-us/library/windows/desktop/bb509673%28v=VS.85%29.aspx
606  GenericMethod("tex1Dbias", VectorType.Float4, GenericParam("s", SamplerType.Sampler1D), GenericParam("t", VectorType.Float4)),
607 
608  // ret tex1Dgrad(s, t, ddx, ddy) http://msdn.microsoft.com/en-us/library/windows/desktop/bb509674%28v=VS.85%29.aspx
609  GenericMethod("tex1Dgrad", VectorType.Float4, GenericParam("s", SamplerType.Sampler1D), GenericParam("t", ScalarType.Float), GenericParam("ddx", ScalarType.Float), GenericParam("ddy", ScalarType.Float)),
610 
611  // ret tex1Dlod(s, t) http://msdn.microsoft.com/en-us/library/windows/desktop/bb509675%28v=VS.85%29.aspx
612  GenericMethod("tex1Dlod", VectorType.Float4, GenericParam("s", SamplerType.Sampler1D), GenericParam("t", VectorType.Float4)),
613 
614  // ret tex1Dproj(s, t) http://msdn.microsoft.com/en-us/library/windows/desktop/bb509676%28v=VS.85%29.aspx
615  GenericMethod("tex1Dproj", VectorType.Float4, GenericParam("s", SamplerType.Sampler1D), GenericParam("t", VectorType.Float4)),
616 
617  // -----------------------------------------
618  // tex2D functions
619  // -----------------------------------------
620 
621  // ret tex2D(s, t) : http://msdn.microsoft.com/en-us/library/windows/desktop/bb509677%28v=VS.85%29.aspx
622  GenericMethod("tex2D", VectorType.Float4, GenericParam("s", SamplerType.Sampler2D), GenericParam("t", VectorType.Float2)),
623 
624  // ret tex2D(s, t, ddx, ddy) http://msdn.microsoft.com/en-us/library/windows/desktop/ff471389%28v=VS.85%29.aspx
625  GenericMethod("tex2D", VectorType.Float4, GenericParam("s", SamplerType.Sampler2D), GenericParam("t", VectorType.Float2), GenericParam("ddx", VectorType.Float2), GenericParam("ddy", VectorType.Float2)),
626 
627  // ret tex2Dbias(s, t) : http://msdn.microsoft.com/en-us/library/windows/desktop/bb509678%28v=VS.85%29.aspx
628  GenericMethod("tex2Dbias", VectorType.Float4, GenericParam("s", SamplerType.Sampler2D), GenericParam("t", VectorType.Float4)),
629 
630  // ret tex2Dgrad(s, t, ddx, ddy) http://msdn.microsoft.com/en-us/library/windows/desktop/bb509679%28v=VS.85%29.aspx
631  GenericMethod("tex2Dgrad", VectorType.Float4, GenericParam("s", SamplerType.Sampler2D), GenericParam("t", VectorType.Float2), GenericParam("ddx", VectorType.Float2), GenericParam("ddy", VectorType.Float2)),
632 
633  // ret tex2Dlod(s, t) http://msdn.microsoft.com/en-us/library/windows/desktop/bb509680%28v=VS.85%29.aspx
634  GenericMethod("tex2Dlod", VectorType.Float4, GenericParam("s", SamplerType.Sampler2D), GenericParam("t", VectorType.Float4)),
635 
636  // ret tex2Dproj(s, t) http://msdn.microsoft.com/en-us/library/windows/desktop/bb509681%28v=VS.85%29.aspx
637  GenericMethod("tex2Dproj", VectorType.Float4, GenericParam("s", SamplerType.Sampler2D), GenericParam("t", VectorType.Float4)),
638 
639  // -----------------------------------------
640  // tex3D functions
641  // -----------------------------------------
642 
643  // ret tex3D(s, t) : http://msdn.microsoft.com/en-us/library/windows/desktop/bb509682%28v=VS.85%29.aspx
644  GenericMethod("tex3D", VectorType.Float4, GenericParam("s", SamplerType.Sampler3D), GenericParam("t", VectorType.Float3)),
645 
646  // ret tex3D(s, t, ddx, ddy) http://msdn.microsoft.com/en-us/library/windows/desktop/ff471391%28v=VS.85%29.aspx
647  GenericMethod("tex3D", VectorType.Float4, GenericParam("s", SamplerType.Sampler3D), GenericParam("t", VectorType.Float3), GenericParam("ddx", VectorType.Float3), GenericParam("ddy", VectorType.Float3)),
648 
649  // ret tex3Dbias(s, t) : http://msdn.microsoft.com/en-us/library/windows/desktop/bb509683%28v=VS.85%29.aspx
650  GenericMethod("tex3Dbias", VectorType.Float4, GenericParam("s", SamplerType.Sampler3D), GenericParam("t", VectorType.Float4)),
651 
652  // ret tex3Dgrad(s, t, ddx, ddy) http://msdn.microsoft.com/en-us/library/windows/desktop/bb509684%28v=VS.85%29.aspx
653  GenericMethod("tex3Dgrad", VectorType.Float4, GenericParam("s", SamplerType.Sampler3D), GenericParam("t", VectorType.Float3), GenericParam("ddx", VectorType.Float3), GenericParam("ddy", VectorType.Float3)),
654 
655  // ret tex3Dlod(s, t) http://msdn.microsoft.com/en-us/library/windows/desktop/bb509685%28v=VS.85%29.aspx
656  GenericMethod("tex3Dlod", VectorType.Float4, GenericParam("s", SamplerType.Sampler3D), GenericParam("t", VectorType.Float4)),
657 
658  // ret tex3Dproj(s, t) http://msdn.microsoft.com/en-us/library/windows/desktop/bb509686%28v=VS.85%29.aspx
659  GenericMethod("tex3Dproj", VectorType.Float4, GenericParam("s", SamplerType.Sampler3D), GenericParam("t", VectorType.Float4)),
660 
661  // -----------------------------------------
662  // texCUBE functions
663  // -----------------------------------------
664 
665  // ret texCUBE(s, t) : http://msdn.microsoft.com/en-us/library/windows/desktop/bb509687%28v=VS.85%29.aspx
666  GenericMethod("texCUBE", VectorType.Float4, GenericParam("s", SamplerType.SamplerCube), GenericParam("t", VectorType.Float3)),
667 
668  // ret texCUBE(s, t, ddx, ddy) http://msdn.microsoft.com/en-us/library/windows/desktop/ff471392%28v=VS.85%29.aspx
669  GenericMethod("texCUBE", VectorType.Float4, GenericParam("s", SamplerType.SamplerCube), GenericParam("t", VectorType.Float3), GenericParam("ddx", VectorType.Float3), GenericParam("ddy", VectorType.Float3)),
670 
671  // ret texCUBEbias(s, t) : http://msdn.microsoft.com/en-us/library/windows/desktop/bb509688%28v=VS.85%29.aspx
672  GenericMethod("texCUBEbias", VectorType.Float4, GenericParam("s", SamplerType.SamplerCube), GenericParam("t", VectorType.Float4)),
673 
674  // ret texCUBEgrad(s, t, ddx, ddy) http://msdn.microsoft.com/en-us/library/windows/desktop/bb509689%28v=VS.85%29.aspx
675  GenericMethod("texCUBEgrad", VectorType.Float4, GenericParam("s", SamplerType.SamplerCube), GenericParam("t", VectorType.Float3), GenericParam("ddx", VectorType.Float3), GenericParam("ddy", VectorType.Float3)),
676 
677  // ret texCUBElod(s, t) http://msdn.microsoft.com/en-us/library/windows/desktop/bb509690%28v=VS.85%29.aspx
678  GenericMethod("texCUBElod", VectorType.Float4, GenericParam("s", SamplerType.SamplerCube), GenericParam("t", VectorType.Float4)),
679 
680  // ret texCUBEproj(s, t) http://msdn.microsoft.com/en-us/library/windows/desktop/bb509691%28v=VS.85%29.aspx
681  GenericMethod("texCUBEproj", VectorType.Float4, GenericParam("s", SamplerType.SamplerCube), GenericParam("t", VectorType.Float4)),
682 
683  //// tex2Dlod(s, t)
684  //GenericMethod("tex2Dlod", VectorType.Float4, GenericContraint("SamplerState", type => type == StateType.SamplerState || type == StateType.SamplerStateOld),
685  // GenericParam("s", "SamplerState"), GenericParam("t", VectorType.Float4)),
686  };
687 
688  private static List<GenericParameterConstraint> GenericContraint(string genericT1, Func<TypeBase,bool> checkT1)
689  {
690  return new List<GenericParameterConstraint>() { new GenericParameterConstraint(genericT1, checkT1) };
691  }
692 
693  private static List<GenericParameterConstraint> GenericContraint(string genericT1, Func<TypeBase,bool> checkT1, string genericT2, Func<TypeBase,bool> checkT2)
694  {
695  return new List<GenericParameterConstraint>() { new GenericParameterConstraint(genericT1, checkT1), new GenericParameterConstraint(genericT2, checkT2), };
696  }
697 
698  private static Ast.Parameter GenericParam(string paramName, string genericTypeName)
699  {
700  return new Ast.Parameter() { Name = new Identifier(paramName), Type = new GenericParameterType(genericTypeName) };
701  }
702 
703  private static Ast.Parameter GenericParam(string paramName, TypeBase type)
704  {
705  return new Ast.Parameter() { Name = new Identifier(paramName), Type = type };
706  }
707 
708  private static Ast.Parameter GenericParam(string paramName, TypeBase type, ParameterQualifier qualifier)
709  {
710  return new Ast.Parameter() { Name = new Identifier(paramName), Type = type, Qualifiers = qualifier };
711  }
712 
713  private static MethodDeclaration GenericMethod(string methodName, TypeBase returnType, params Ast.Parameter[] parameters)
714  {
715  return GenericMethod(methodName, returnType, null, parameters);
716  }
717 
718  private static MethodDeclaration GenericMethod(string methodName, TypeBase returnType, List<GenericParameterConstraint> constraints, params Ast.Parameter[] parameters)
719  {
720  var methodDeclaration = new MethodDeclaration() { Name = new Identifier(methodName), ReturnType = returnType };
721  methodDeclaration.IsBuiltin = true;
722  if (constraints != null)
723  methodDeclaration.ParameterConstraints = constraints;
724  methodDeclaration.Parameters.AddRange(parameters);
725  return methodDeclaration;
726  }
727 
728 
729  static TypeBase[] FloatTargets = new[] { ScalarType.Float, ScalarType.Double };
730  static TypeBase[] NumericTargets = new[] { ScalarType.Float, ScalarType.Double, ScalarType.Int, ScalarType.UInt };
731  static TypeBase[] NumericAndBoolTargets = new[] { ScalarType.Float, ScalarType.Double, ScalarType.Int, ScalarType.UInt, ScalarType.Bool };
732 
733  class ParamDef
734  {
735  public string Name { get; set; }
736  public Func<Function, Parameter[], TypeBase> ParamDecl { get; set; }
737  public bool Out { get; set; }
738  }
739  class Function
740  {
741  public static ParamDef Param(int index)
742  {
743  return new ParamDef { Name = "ret", ParamDecl = (f, p) => p[index].GenerateType() };
744  }
745 
746  public static ParamDef ParamVoid()
747  {
748  return new ParamDef { Name = "ret", ParamDecl = (f, p) => TypeBase.Void };
749  }
750 
751  public static ParamDef Param(string name, int index, bool outParam = false)
752  {
753  return new ParamDef { Name = name, ParamDecl = (f, p) => p[index].GenerateType(), Out = outParam };
754  }
755  public static ParamDef Param(Func<Function, Parameter[], TypeBase> paramDecl)
756  {
757  return new ParamDef { Name = "ret", ParamDecl = paramDecl };
758  }
759  public static ParamDef Param(string name, Func<Function, Parameter[], TypeBase> paramDecl)
760  {
761  return new ParamDef { Name = name, ParamDecl = paramDecl };
762  }
763  public string Name { get; set; }
764  public ParameterInfo[] Parameters { get; set; }
765  public Func<Function, Parameter[], TypeBase[]> ParamList { get; set; }
766  public Func<Function, Parameter[], TypeBase> Return { get; set; }
767  public static Function Template1(string name, Target[] targets, TypeBase[] targetTypes, ParamDef ret, params ParamDef[] args)
768  {
769  var p = new ParameterInfo { Targets = targets, TargetTypes = targetTypes, SizeFlags = SizeFlags.None };
770  return new Function { Name = name, Parameters = new[] { p }, ParamList = (f, p2) => args.Select(arg => arg.ParamDecl(f, p2)).ToArray(), Return = (f, p2) => ret.ParamDecl(f, p2) };
771  }
772  public static Function[] Functions = new[]
773  {
774  // Missing5: dst EvaluateAttribute
775  // Interlocked*
776  Template1("abs", AllTargets, NumericTargets, Param(0), Param("x", 0)),
777  Template1("acos", AllTargets, FloatTargets, Param(0), Param("x", 0)),
778  Template1("all", AllTargets, NumericAndBoolTargets, Param((f, p) => ScalarType.Bool), Param("x", 0)),
779  //Template1("AllMemoryBarrier", new[] { Target.Scalar }, new TypeBase[] { ScalarType.Float }, ParamVoid()),
780  //Template1("AllMemoryBarrierWithGroupSync", new[] { Target.Scalar }, new TypeBase[] { ScalarType.Float }, ParamVoid()),
781  Template1("any", AllTargets, NumericAndBoolTargets, Param((f, p) => ScalarType.Bool), Param("x", 0)),
782  Template1("asdouble", new[] { Target.Scalar, Target.Vector2 }, new TypeBase[] { ScalarType.UInt }, Param((f,p) => p[0].ChangeTargetType(ScalarType.Double).GenerateType()), Param("l",0), Param("h",0)),
783  Template1("asfloat", AllTargets, new TypeBase[] { ScalarType.Bool, ScalarType.Float, ScalarType.Int, ScalarType.UInt }, Param((f,p) => p[0].ChangeTargetType(ScalarType.Float).GenerateType()), Param("x", 0)),
784  Template1("asin", AllTargets, FloatTargets, Param(0), Param("x", 0)),
785  Template1("asint", AllTargets, new TypeBase[] { ScalarType.Float, ScalarType.UInt }, Param((f,p) => p[0].ChangeTargetType(ScalarType.Int).GenerateType()), Param("x", 0)),
786  Template1("asuint", AllTargets, new TypeBase[] { ScalarType.Float, ScalarType.Int }, Param((f,p) => p[0].ChangeTargetType(ScalarType.UInt).GenerateType()), Param("x", 0)),
787  Template1("asuint", new[] { Target.Scalar }, new TypeBase[] { ScalarType.UInt }, ParamVoid(), Param("x", (f,p)=> p[0].ChangeTargetType(ScalarType.Double).GenerateType()), Param("y", 0, true), Param("z", 0, true)),
788  Template1("atan", AllTargets, FloatTargets, Param(0), Param("x", 0)),
789  Template1("atan2", AllTargets, FloatTargets, Param(0), Param("y", 0), Param("x", 0)),
790  Template1("ceil", AllTargets, FloatTargets, Param(0), Param("x", 0)),
791  Template1("clamp", AllTargets, NumericTargets, Param(0), Param("x", 0), Param("min", 0), Param("max", 0)),
792  Template1("clip", AllTargets, NumericTargets, ParamVoid(), Param("x", 0)),
793  Template1("cos", AllTargets, FloatTargets, Param(0), Param("x", 0)),
794  Template1("cosh", AllTargets, FloatTargets, Param(0), Param("x", 0)),
795  Template1("countbits", new[] { Target.Scalar, Target.Vector }, new TypeBase[] { ScalarType.UInt }, Param(0), Param("x", 0)),
796  Template1("cross", new[] { Target.Vector3 }, FloatTargets, Param(0), Param("x", 0), Param("y", 0)),
797  //Template1("D3DCOLORtoUBYTE4", new[] { Target.Vector4 }, new TypeBase[] { ScalarType.Float }, Param((f,p) => VectorType.Int4), Param("x", 0)),
798  Template1("ddx", AllTargets, FloatTargets, Param(0), Param("x", 0)),
799  Template1("ddx_coarse", new[] { Target.Scalar, Target.Vector }, new TypeBase[] { ScalarType.Float }, Param(0), Param("x", 0)),
800  Template1("ddx_fine", new[] { Target.Scalar, Target.Vector }, new TypeBase[] { ScalarType.Float }, Param(0), Param("x", 0)),
801  Template1("ddy", AllTargets, FloatTargets, Param(0), Param("x", 0)),
802  Template1("ddy_coarse", new[] { Target.Scalar, Target.Vector }, new TypeBase[] { ScalarType.Float }, Param(0), Param("x", 0)),
803  Template1("ddy_fine", new[] { Target.Scalar, Target.Vector }, new TypeBase[] { ScalarType.Float }, Param(0), Param("x", 0)),
804  Template1("degrees", AllTargets, FloatTargets, Param(0), Param("x", 0)),
805  Template1("determinant", new[] { Target.Matrix }, FloatTargets, Param((f, p) => p[0].ChangeTarget(Target.Scalar).GenerateType()), Param("x", 0)),
806  Template1("distance", new[] { Target.Vector }, FloatTargets, Param((f, p) => p[0].ChangeTarget(Target.Scalar).GenerateType()), Param("x", 0), Param("y", 0)),
807  Template1("dot", new[] { Target.Vector }, NumericTargets, Param((f, p) => p[0].ChangeTarget(Target.Scalar).GenerateType()), Param("x", 0), Param("y", 0)),
808  Template1("EvaluateAttributeAtCentroid", AllTargets, NumericTargets, Param(0), Param("x", 0)),
809  Template1("EvaluateAttributeAtSample", AllTargets, NumericTargets, Param(0), Param("x", 0), Param("s", (f,p) => ScalarType.UInt)),
810  Template1("EvaluateAttributeSnapped", AllTargets, NumericTargets, Param(0), Param("x", 0), Param("s", (f,p) => VectorType.Int2)),
811  Template1("exp", AllTargets, FloatTargets, Param(0), Param("x", 0)),
812  Template1("exp2", AllTargets, FloatTargets, Param(0), Param("x", 0)),
813  Template1("f16tof32", new[] { Target.Scalar, Target.Vector }, new TypeBase[] { ScalarType.UInt }, Param((f,p) => p[0].ChangeTargetType(ScalarType.Float).GenerateType()), Param("x",0)),
814  Template1("f32tof16", new[] { Target.Scalar, Target.Vector }, new TypeBase[] { ScalarType.Float }, Param((f,p) => p[0].ChangeTargetType(ScalarType.UInt).GenerateType()), Param("x",0)),
815  Template1("faceforward", new[] { Target.Vector }, FloatTargets, Param(0), Param("n", 0), Param("i", 0), Param("ng", 0)),
816  Template1("firstbithigh", new[] { Target.Scalar, Target.Vector }, new TypeBase[] { ScalarType.Int, ScalarType.UInt }, Param(0), Param("x", 0)),
817  Template1("firstbitlow", new[] { Target.Scalar }, new TypeBase[] { ScalarType.Int }, Param(0), Param("x", 0)),
818  Template1("firstbitlow", new[] { Target.Vector2, Target.Vector3, Target.Vector4 }, new TypeBase[] { ScalarType.UInt }, Param(0), Param("x", 0)),
819  Template1("floor", AllTargets, FloatTargets, Param(0), Param("x", 0)),
820  Template1("fmod", AllTargets, FloatTargets, Param(0), Param("x", 0), Param("y", 0)),
821  Template1("frac", AllTargets, FloatTargets, Param(0), Param("x", 0)),
822  Template1("frexp", AllTargets, FloatTargets, Param(0), Param("x", 0), Param("exp", 0)),
823  Template1("fwidth", AllTargets, FloatTargets, Param(0), Param("x", 0)),
824  Template1("isfinite", AllTargets, FloatTargets, Param((f, p) => p[0].ChangeTargetType(ScalarType.Bool).GenerateType()), Param("x", 0)),
825  Template1("isinf", AllTargets, FloatTargets, Param((f, p) => p[0].ChangeTargetType(ScalarType.Bool).GenerateType()), Param("x", 0)),
826  Template1("isnan", AllTargets, FloatTargets, Param((f, p) => p[0].ChangeTargetType(ScalarType.Bool).GenerateType()), Param("x", 0)),
827  Template1("ldexp", AllTargets, FloatTargets, Param(0), Param("x", 0), Param("exp", 0)),
828  Template1("length", new[] { Target.Vector }, FloatTargets, Param((f, p) => p[0].ChangeTarget(Target.Scalar).GenerateType()), Param("x", 0)),
829  Template1("lerp", AllTargets, FloatTargets, Param(0), Param("x", 0), Param("y", 0), Param("s", 0)),
830  Template1("lit", new[] { Target.Scalar }, FloatTargets, Param((f, p) => VectorType.Float4), Param("n_dot_l", 0), Param("n_dot_h", 0), Param("m", 0)),
831  Template1("log", AllTargets, FloatTargets, Param(0), Param("x", 0)),
832  Template1("log10", AllTargets, FloatTargets, Param(0), Param("x", 0)),
833  Template1("log2", AllTargets, FloatTargets, Param(0), Param("x", 0)),
834  Template1("mad", new[] { Target.Scalar, Target.Vector }, NumericTargets, Param(0), Param("x", 0), Param("y", 0), Param("z", 0)),
835  Template1("max", AllTargets, NumericTargets, Param(0), Param("x", 0), Param("y", 0)),
836  Template1("min", AllTargets, NumericTargets, Param(0), Param("x", 0), Param("y", 0)),
837  Template1("modf", AllTargets, NumericTargets, Param(0), Param("x", 0), Param("ip", 0, outParam: true)),
838  Template1("mul", new[] { Target.Scalar }, FloatTargets, Param(0), Param("x", 0), Param("y", 0)), // Group 1
839  Template1("mul", new[] { Target.Vector }, FloatTargets, Param(0), Param("x", (f, p) => p[0].ChangeTarget(Target.Scalar).GenerateType()), Param("y", 0)), // Group 2
840  Template1("mul", new[] { Target.Matrix }, FloatTargets, Param(0), Param("x", (f, p) => p[0].ChangeTarget(Target.Scalar).GenerateType()), Param("y", 0)), // Group 3
841  Template1("mul", new[] { Target.Vector }, FloatTargets, Param(0), Param("x", 0), Param("y", (f, p) => p[0].ChangeTarget(Target.Scalar).GenerateType())), // Group 4
842  Template1("mul", new[] { Target.Matrix }, FloatTargets, Param(0), Param("x", 0), Param("y", (f, p) => p[0].ChangeTarget(Target.Scalar).GenerateType())), // Group 7
843  Template1("mul", new[] { Target.Vector }, FloatTargets, Param((f, p) => p[0].ChangeTarget(Target.Scalar).GenerateType()), Param("x", 0), Param("y", 0)), // Group 5
844  Template1("mul", new[] { Target.Matrix }, FloatTargets, Param((f, p) => p[0].ReduceFromMatrixColumn().GenerateType()), Param("x", (f, p) => p[0].ReduceFromMatrixRow().GenerateType()), Param("y", 0)), // Group 6
845  Template1("mul", new[] { Target.Matrix }, FloatTargets, Param((f, p) => p[0].ReduceFromMatrixRow().GenerateType()), Param("x", 0), Param("y", (f, p) => p[0].ReduceFromMatrixColumn().GenerateType())), // Group 8
846  Template1("mul", new[] { Target.Matrix }, FloatTargets, Param((f, p) => p[0].MakeMatrix(p[0].RowCount, 1).GenerateType()), Param("x", 0), Param("y", (f, p) => p[0].MakeMatrix(p[0].ColumnCount, 1).GenerateType())), // Group 9
847  Template1("mul", new[] { Target.Matrix }, FloatTargets, Param((f, p) => p[0].MakeMatrix(p[0].RowCount, 2).GenerateType()), Param("x", 0), Param("y", (f, p) => p[0].MakeMatrix(p[0].ColumnCount, 2).GenerateType())), // Group 9
848  Template1("mul", new[] { Target.Matrix }, FloatTargets, Param((f, p) => p[0].MakeMatrix(p[0].RowCount, 3).GenerateType()), Param("x", 0), Param("y", (f, p) => p[0].MakeMatrix(p[0].ColumnCount, 3).GenerateType())), // Group 9
849  Template1("mul", new[] { Target.Matrix }, FloatTargets, Param((f, p) => p[0].MakeMatrix(p[0].RowCount, 4).GenerateType()), Param("x", 0), Param("y", (f, p) => p[0].MakeMatrix(p[0].ColumnCount, 4).GenerateType())), // Group 9
850  Template1("normalize", AllTargets, FloatTargets, Param(0), Param("x", 0)),
851  Template1("pow", AllTargets, FloatTargets, Param(0), Param("x", 0), Param("y", 0)),
852  Template1("radians", AllTargets, FloatTargets, Param(0), Param("x", 0)),
853  Template1("rcp", AllTargets, FloatTargets, Param(0), Param("x", 0)),
854  Template1("reflect", new[] { Target.Vector }, FloatTargets, Param(0), Param("i", 0), Param("n", 0)),
855  Template1("refract", new[] { Target.Vector }, FloatTargets, Param(0), Param("i", 0), Param("n", 0), Param("index", (f, p) => p[0].ChangeTarget(Target.Scalar).GenerateType())),
856  Template1("reversebits", new[] { Target.Scalar, Target.Vector }, new TypeBase[] { ScalarType.UInt }, Param(0), Param("x", 0)),
857  Template1("round", AllTargets, FloatTargets, Param(0), Param("x", 0)),
858  Template1("rsqrt", AllTargets, FloatTargets, Param(0), Param("x", 0)),
859  Template1("saturate", AllTargets, FloatTargets, Param(0), Param("x", 0)),
860  Template1("sign", AllTargets, NumericTargets, Param((f, p) => p[0].ChangeTargetType(ScalarType.Int).GenerateType()), Param("x", 0)),
861  Template1("sin", AllTargets, FloatTargets, Param(0), Param("x", 0)),
862  Template1("sincos", AllTargets, FloatTargets, Param((f, p) => TypeBase.Void), Param("x", 0), Param("s", 0, outParam: true), Param("c", 0, outParam: true)),
863  Template1("sinh", AllTargets, FloatTargets, Param(0), Param("x", 0)),
864  Template1("smoothstep", AllTargets, FloatTargets, Param(0), Param("min", 0), Param("max", 0), Param("x", 0)),
865  Template1("sqrt", AllTargets, FloatTargets, Param(0), Param("x", 0)),
866  Template1("step", AllTargets, FloatTargets, Param(0), Param("y", 0), Param("x", 0)),
867  Template1("tan", AllTargets, FloatTargets, Param(0), Param("x", 0)),
868  Template1("tanh", AllTargets, FloatTargets, Param(0), Param("x", 0)),
869  Template1("transpose", new[] { Target.Matrix }, FloatTargets, Param((f, p) => p[0].Transpose().GenerateType()), Param("x", 0)),
870  Template1("trunc", AllTargets, FloatTargets, Param(0), Param("x", 0)),
871  };
872  }
873  static Target[] AllTargets = new[] { Target.Scalar, Target.Vector, Target.Matrix };
874  enum Target
875  {
876  Scalar = 1,
877  Vector = 2,
878  SquareMatrix = 3,
879  Matrix = 4,
880  Vector2 = 5,
881  Vector3 = 6,
882  Vector4 = 7,
883  }
884  [Flags]
885  enum SizeFlags
886  {
887  None = 0,
888  AllMatrix = 1,
889  }
890  class ParameterInfo
891  {
892  public IList<Target> Targets;
893  public IList<TypeBase> TargetTypes;
894  public SizeFlags SizeFlags;
895  }
896  class Parameter
897  {
898  public Parameter ChangeTarget(Target target)
899  {
900  return new Parameter { Target = target, TargetType = TargetType, TargetSize = TargetSize };
901  }
902  public Parameter ChangeTargetType(TypeBase targetType)
903  {
904  return new Parameter { Target = Target, TargetType = targetType, TargetSize = TargetSize };
905  }
906  public Parameter MakeMatrix(int rowCount, int columnCount)
907  {
908  return new Parameter { Target = Target.Matrix, TargetType = TargetType, TargetSize = (rowCount - 1) * 4 + columnCount - 1 };
909  }
910  public Parameter ReduceFromMatrixRow()
911  {
912  return new Parameter { Target = Target.Vector, TargetType = TargetType, TargetSize = TargetSize / 4 };
913  }
914  public Parameter ReduceFromMatrixColumn()
915  {
916  return new Parameter { Target = Target.Vector, TargetType = TargetType, TargetSize = TargetSize % 4 };
917  }
918  public Parameter Transpose()
919  {
920  int row = TargetSize / 4;
921  int column = TargetSize % 4;
922  return new Parameter { Target = Target, TargetType = TargetType, TargetSize = column * 4 + row };
923  }
924  public int RowCount { get { return (TargetSize / 4) + 1; } }
925  public int ColumnCount { get { return (TargetSize % 4) + 1; } }
926  public Target Target { get; set; }
927  public TypeBase TargetType { get; set; }
928  public int TargetSize { get; set; }
929 
930  public TypeBase GenerateType()
931  {
932  if (Target == Target.Matrix)
933  return new MatrixType((ScalarType)TargetType, RowCount, ColumnCount);
934  if (Target == Target.Vector)
935  return new VectorType((ScalarType)TargetType, ColumnCount);
936  if (Target == Target.Vector2)
937  return new VectorType((ScalarType)TargetType, 2);
938  if (Target == Target.Vector3)
939  return new VectorType((ScalarType)TargetType, 3);
940  if (Target == Target.Vector4)
941  return new VectorType((ScalarType)TargetType, 4);
942  return TargetType;
943  }
944  }
945 
946  static IEnumerable<Parameter> EnumerateParameters(ParameterInfo p)
947  {
948  for (int target = 0; target < p.Targets.Count(); ++target)
949  {
950  for (int targetType = 0; targetType < p.TargetTypes.Count(); ++targetType)
951  {
952  switch (p.Targets[target])
953  {
954  case Target.Scalar:
955  yield return new Parameter { Target = p.Targets[target], TargetType = p.TargetTypes[targetType], TargetSize = 1 };
956  break;
957  case Target.Vector:
958  case Target.SquareMatrix:
959  for (int i = 0; i < 4; ++i)
960  {
961  yield return new Parameter { Target = p.Targets[target], TargetType = p.TargetTypes[targetType], TargetSize = i };
962  }
963  break;
964  case Target.Vector2:
965  yield return new Parameter { Target = p.Targets[target], TargetType = p.TargetTypes[targetType], TargetSize = 1 };
966  break;
967  case Target.Vector3:
968  yield return new Parameter { Target = p.Targets[target], TargetType = p.TargetTypes[targetType], TargetSize = 2 };
969  break;
970  case Target.Vector4:
971  yield return new Parameter { Target = p.Targets[target], TargetType = p.TargetTypes[targetType], TargetSize = 3 };
972  break;
973  case Target.Matrix:
974  for (int i = 0; i < 16; ++i)
975  {
976  yield return new Parameter { Target = p.Targets[target], TargetType = p.TargetTypes[targetType], TargetSize = i };
977  }
978  break;
979  }
980  }
981  }
982  }
983 
984  #endregion
985  }
986 
987 
988  internal class GenericInstanceKey
989  {
990  public string GenericName;
991 
992  public List<Node> GenericParameters;
993 
994  public GenericInstanceKey(string genericName, List<Node> genericParams)
995  {
996  GenericName = genericName;
997  GenericParameters = genericParams;
998  }
999 
1000  public override bool Equals(object obj)
1001  {
1002  var genInstKey = obj as GenericInstanceKey;
1003  if (genInstKey == null)
1004  return false;
1005 
1006  if (GenericParameters.Count != genInstKey.GenericParameters.Count)
1007  return false;
1008 
1009  bool res = true;
1010  for (int i = 0; i < GenericParameters.Count; ++i)
1011  res &= GenericParameters[i] == genInstKey.GenericParameters[i];
1012 
1013  return res;
1014  }
1015 
1016  public override int GetHashCode()
1017  {
1018  return (GenericName.GetHashCode() * 397);// ^ ;
1019  }
1020  }
1021 }
Base class for all vector types
Definition: VectorType.cs:10
static readonly ScalarType Bool
Scalar bool.
Definition: ScalarType.cs:17
override IEnumerable< IDeclaration > FindDeclarationsFromObject(TypeBase typeBase, string memberName)
TypeBase TargetType
Gets or sets the type.
A Type reference analysis is building type references.
static readonly VectorType Float2
A Float2
Definition: VectorType.cs:47
static List< MatrixType.Indexer > MatrixSwizzleDecode(MemberReferenceExpression memberReference, ParsingResult result=null)
Decodes the swizzle.
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
override void Visit(MethodInvocationExpression expression)
Visits the specified method invocation expression.
static readonly SamplerType SamplerCube
A samplerCUBE.
Definition: SamplerType.cs:39
Specialized ParameterQualifier for Hlsl.
static readonly VectorType Float3
A Float3
Definition: VectorType.cs:52
static readonly ScalarType Int
Scalar int.
Definition: ScalarType.cs:37
A typeless reference.
Definition: TypeName.cs:10
TypeInference TypeInference
Gets or sets the resolved reference.
Definition: TypeBase.cs:69
string Text
Gets or sets the name.
Definition: Identifier.cs:77
A state expresion in the form: sampler {...}.
static readonly SamplerType Sampler3D
A sampler3D.
Definition: SamplerType.cs:34
A generic declaration. This is used internally to identify a generic declaration. ...
override TypeBase Visit(TypeName typeName)
Visits the specified type name.
virtual void Visit(GenericType genericType)
Visits the specified type name.
virtual void FindMemberTypeReference(MatrixType matrixType, MemberReferenceExpression memberReference)
Finds the member type reference.
static readonly VectorType Float4
A Float4
Definition: VectorType.cs:57
Abstract node.
Definition: Node.cs:15
static readonly VectorType Int4
A Int4
Definition: VectorType.cs:27
static readonly ScalarType UInt
Scalar unsigned int.
Definition: ScalarType.cs:42
virtual void Visit(CompileExpression compileExpression)
TypeInference TypeInference
Gets or sets the type reference.
Definition: Expression.cs:28
static List< IDeclaration > ParseBuiltin(string builtins, string fileName)
Flags
Enumeration of the new Assimp's flags.
static readonly ScalarType Float
Sclar float.
Definition: ScalarType.cs:27
override IEnumerable< IDeclaration > FindDeclarations(string nameArg)
Finds a list of declaration by its name. The name.A list of declaration
static void FillCloneContext(CloneContext cloneContext)
Fill the clone context with the elements of the default declarations
SiliconStudio.Shaders.Ast.ParameterQualifier ParameterQualifier
A single parameter declaration.
Definition: Parameter.cs:10
The type of the serialized type will be passed as a generic arguments of the serializer. Example: serializer of A becomes instantiated as Serializer{A}.
static readonly SamplerType Sampler1D
A sampler1D.
Definition: SamplerType.cs:24
Search for out only dependencies.
A Generic parameter for a method that provides a constraint resolver.
A member reference in the form {this}.{Name}
override void ProcessMethodInvocation(MethodInvocationExpression expression, string methodName, List< IDeclaration > declarations)
Pres the process method invocation.
List< Node > Parameters
Gets or sets the parameters.
Definition: GenericType.cs:82
Base type for all types.
Definition: TypeBase.cs:11
static readonly ScalarType Double
Scalar double.
Definition: ScalarType.cs:22
static void UpdateCloneContext(CloneContext cloneContext)
Update the clone context with the new instanciated classes
Provides a dictionary of cloned values, where the [key] is the original object and [value] the new ob...
Base class for all generic types.
Definition: GenericType.cs:14
static readonly VectorType Int2
A Int2
Definition: VectorType.cs:17
SiliconStudio.Core.Mathematics.Vector3 Vector3
Toplevel interface for a declaration.
Definition: IDeclaration.cs:8
void SetupHlslAnalyzer(List< IDeclaration > builtinDeclarations=null)
Create the required declarations for hlsl parsing
override void CommonVisit(MemberReferenceExpression memberReference)
Visits the specified member reference.
Identifier Name
Gets or sets the type name.
Definition: TypeBase.cs:77
static readonly ScalarType Void
Scalar void. TODO this is not a scalar!
Definition: TypeBase.cs:266
static readonly SamplerType Sampler2D
A sampler2D
Definition: SamplerType.cs:29
HlslSemanticAnalysis(ParsingResult result)
Initializes a new instance of the HlslSemanticAnalysis class.
A Type reference analysis is building type references.
virtual void Visit(StateExpression stateExpression)
object GetTag(object tagKey)
Gets a tag value associated to this node..
Definition: Node.cs:78
static void Run(ParsingResult toParse, List< IDeclaration > builtinDeclarations=null)