4 using System.Collections.Generic;
5 using System.Globalization;
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;
16 namespace SiliconStudio.Shaders.Analysis.Hlsl
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>();
29 #region Constructors and Destructors
43 #region Public Methods
45 private static readonly
object SwizzleTag =
"MatrixSwizzleDecode";
56 string components = memberReference.Member.Text;
60 var span = matrixDecl.Span;
68 if (components.StartsWith(
"_"))
70 string[] splitComponents;
72 if (components.StartsWith(
"_m"))
74 splitComponents = components.Split(
new[] {
"_m" }, StringSplitOptions.RemoveEmptyEntries);
78 splitComponents = components.Split(
new[] {
"_" }, StringSplitOptions.RemoveEmptyEntries);
84 if (splitComponents.Length == 0 && result != null)
86 result.Error(MessageCode.ErrorMatrixInvalidMemberReference, span, components);
89 foreach (var splitComponent
in splitComponents)
91 if (splitComponent.Length != 2 || !IsValidIndex(span, splitComponent[0], indexOffset, indexOffset + 3) || !IsValidIndex(span, splitComponent[1], indexOffset, indexOffset + 3))
97 swizzles.Add(
new MatrixType.Indexer(int.Parse(splitComponent[0].ToString()) - indexOffset, int.Parse(splitComponent[1].ToString()) - indexOffset));
102 memberReference.SetTag(SwizzleTag, swizzles);
121 Visit((
Node)castExpression);
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;
134 if (methodAsVariable != null)
136 switch (methodAsVariable.Name.Text)
138 case "ConstructGSWithSO":
139 case "CompileShader":
144 base.Visit(expression);
176 Visit((
Node)genericType);
178 string genericName = genericType.Name.Text;
181 var value = TextureType.Parse(genericName);
183 genericName = value.Name.Text;
186 if (BuiltinObjects.TryGetValue(genericName, out typeBase))
188 genericType.TypeInference.TargetType = GetGenericInstance(genericName, genericType, typeBase);
196 var classType = (ClassType)typeBase;
198 foreach (var declaration
in classType.Members.OfType<
IDeclaration>().Where(node => node.Name == memberName))
199 yield
return declaration;
201 foreach (var baseType
in classType.BaseClasses)
202 foreach (var baseDeclaration
in FindDeclarationsFromObject(baseType.ResolveType(), memberName))
203 yield
return baseDeclaration;
207 var interfaceType = (InterfaceType)typeBase;
209 foreach (var declaration
in interfaceType.Methods.OfType<
IDeclaration>().Where(node => node.Name == memberName))
210 yield
return declaration;
223 if (methodName != null)
229 var typeDefDeclarator = declarations.OfType<
Typedef>().FirstOrDefault();
230 if (typeDefDeclarator != null)
232 varExp.TypeInference.Declaration = typeDefDeclarator;
233 varExp.TypeInference.TargetType = typeDefDeclarator.ResolveType();
235 expression.TypeInference.Declaration = typeDefDeclarator;
236 expression.TypeInference.TargetType = typeDefDeclarator.ResolveType();
241 var builtInFunction = defaultDeclarations.FirstOrDefault(x => (x as
MethodDeclaration != null) && (x as
MethodDeclaration).IsSameSignature(expression)) as MethodDeclaration;
242 if (builtInFunction != null)
244 varExp.TypeInference.Declaration = builtInFunction;
245 varExp.TypeInference.TargetType = builtInFunction.ReturnType.ResolveType();
247 expression.TypeInference.Declaration = builtInFunction;
248 expression.TypeInference.TargetType = builtInFunction.ReturnType.ResolveType();
254 base.ProcessMethodInvocation(expression, methodName, declarations);
260 Visit((
Node)textureType);
262 AssociatePredefinedObjects(textureType);
265 private void AssociatePredefinedObjects(
TypeBase typebase)
272 textureType.Parameters[0] = VectorType.Float4;
274 typebase.TypeInference.TargetType = GetGenericInstance(typebase.
Name.
Text, textureType, predefinedType);
285 Visit((
Node)typeName);
287 var name = typeName.Name.Text;
291 TypeBase value = TextureType.Parse(name);
294 AssociatePredefinedObjects(value);
298 value = StreamTypeName.Parse(name);
301 AssociatePredefinedObjects(value);
305 value = SamplerType.Parse(name);
309 value = StateType.Parse(name);
314 if (name ==
"VertexShader" || name ==
"GeometryShader" || name ==
"PixelShader")
318 return base.Visit(typeName);
321 private static bool IsValidIndex(
SourceSpan span,
char valueChar,
int min,
int max,
ParsingResult result = null)
324 var isParseOk = int.TryParse(valueChar.ToString(CultureInfo.InvariantCulture), out value);
326 if (!isParseOk || value < min || value > max)
329 result.Error(MessageCode.ErrorMatrixInvalidIndex, span, valueChar, min, max);
342 var thisType = memberReference.Target.TypeInference.TargetType;
346 FindMemberTypeReference((MatrixType)thisType, memberReference);
350 base.CommonVisit(memberReference);
361 var components = memberReference.Member.Text;
362 var span = memberReference.Span;
375 var swizzles = MatrixSwizzleDecode(memberReference,
ParsingResult);
377 if (swizzles.Count > 0)
379 var itemType = matrixType.Type.ResolveType();
380 memberReference.TypeInference.TargetType = swizzles.Count == 1 ? itemType :
new VectorType((
ScalarType)itemType, swizzles.Count);
390 var key =
new GenericInstanceKey(
typename, genericType.
Parameters);
393 if (!InstanciatedTypes.TryGetValue(key, out instanciatedType))
395 instanciatedType = genericType.MakeGenericInstance(predefinedType);
396 InstanciatedTypes.Add(key, instanciatedType);
398 return instanciatedType;
403 foreach (var
function in Function.Functions)
405 foreach (var p
in EnumerateParameters(
function.Parameters[0]))
407 var returnType = function.Return(
function,
new[] { p });
408 var parameterTypes = function.ParamList(
function,
new[] { p });
411 methodDeclaration.IsBuiltin =
true;
412 methodDeclaration.Name =
new Identifier(
function.Name);
413 methodDeclaration.ReturnType = returnType;
415 foreach (var parameterType
in parameterTypes)
416 methodDeclaration.Parameters.Add(
new Ast.Parameter { DeclaringMethod = methodDeclaration, Type = parameterType } );
418 defaultDeclarations.Add(methodDeclaration);
422 defaultDeclarations.AddRange(declaredMethods);
424 foreach (var methodDeclaration
in declaredMethods)
427 newMethodDeclaration.IsBuiltin =
true;
428 newMethodDeclaration.Name =
new Identifier(methodDeclaration.Name);
429 newMethodDeclaration.ReturnType = methodDeclaration.ReturnType;
431 foreach (var parameter
in methodDeclaration.Parameters)
433 var parameterType = parameter.Type;
436 parameterType = SamplerType.Sampler;
439 newMethodDeclaration.Parameters.Add(
new Ast.Parameter { DeclaringMethod = newMethodDeclaration, Type = parameterType });
441 defaultDeclarations.Add(newMethodDeclaration);
445 defaultDeclarations.Add(GenericMethod(
"AllMemoryBarrier",
TypeBase.
Void));
446 defaultDeclarations.Add(GenericMethod(
"AllMemoryBarrierWithGroupSync",
TypeBase.
Void));
448 defaultDeclarations.Add(GenericMethod(
"DeviceMemoryBarrier",
TypeBase.
Void));
449 defaultDeclarations.Add(GenericMethod(
"DeviceMemoryBarrierWithGroupSync",
TypeBase.
Void));
450 defaultDeclarations.Add(GenericMethod(
"GetRenderTargetSampleCount",
ScalarType.
UInt));
452 defaultDeclarations.Add(GenericMethod(
"GroupMemoryBarrier",
TypeBase.
Void));
455 public static List<IDeclaration>
ParseBuiltin(
string builtins,
string fileName)
457 var builtinDeclarations =
new List<IDeclaration>();
459 var result = HlslParser.TryPreProcessAndParse(builtins, fileName);
462 var shader = ShaderParser.Check(result, fileName);
464 foreach (var declaration
in shader.Declarations)
466 var classType = declaration as
ClassType;
467 if (classType != null)
469 classType.Name.Text = classType.Name.Text.Trim(
'_');
470 BuiltinObjects.Add(classType.Name.Text, classType);
471 classType.IsBuiltIn =
true;
476 if (methodDeclaration != null)
477 methodDeclaration.IsBuiltin =
true;
478 builtinDeclarations.Add((IDeclaration)declaration);
485 return builtinDeclarations;
499 defaultDeclarations.AddRange(ParseBuiltin(Resources.HlslDeclarations,
"internal_hlsl_declarations.hlsl"));
500 InitializeBuiltins();
501 isInitialized =
true;
506 ScopeStack.Peek().AddDeclarations(defaultDeclarations);
508 if (builtinDeclarations != null)
510 this.ScopeStack.Peek().AddDeclarations(builtinDeclarations);
513 foreach (var builtinDeclaration
in builtinDeclarations)
516 if (methodDeclaration != null)
517 methodDeclaration.SetTag(TagBuiltinUserDefined,
true);
531 defaultDeclarations.AddRange(ParseBuiltin(Resources.HlslDeclarations,
"internal_hlsl_declarations.hlsl"));
532 InitializeBuiltins();
533 isInitialized =
true;
536 foreach (var decl
in defaultDeclarations)
537 DeepCloner.DeepCollect(decl, cloneContext);
539 foreach (var bInObj
in BuiltinObjects)
540 DeepCloner.DeepCollect(bInObj, cloneContext);
542 UpdateCloneContext(cloneContext);
551 foreach (var instType
in InstanciatedTypes)
553 if (instType.Key.GenericParameters.Any(x => x is
TypeName))
555 DeepCloner.DeepCollect(instType.Value, cloneContext);
559 public static void Run(
ParsingResult toParse, List<IDeclaration> builtinDeclarations = null)
562 analysis.SetupHlslAnalyzer(builtinDeclarations);
569 string name = nameArg;
570 bool isGenericLookingForBaseDeclarator =
false;
571 if (name.StartsWith(
"__") && name.EndsWith(
"_base"))
573 name = name.Substring(
"__".Length);
574 name = name.Substring(0, name.Length -
"_base".Length);
575 isGenericLookingForBaseDeclarator =
true;
579 foreach (var scopeDeclaration
in ScopeStack)
581 foreach (var genericDecl
in scopeDeclaration.FindGenerics(name))
583 yield
return new GenericDeclaration(genericDecl.Name, genericDecl.Holder, genericDecl.Index, isGenericLookingForBaseDeclarator) { Span = genericDecl.Span };
587 foreach (var item
in base.FindDeclarations(name))
603 GenericMethod(
"tex1D",
VectorType.
Float4, GenericParam(
"s",
SamplerType.
Sampler1D), GenericParam(
"t",
ScalarType.
Float), GenericParam(
"ddx",
ScalarType.
Float), GenericParam(
"ddy",
ScalarType.
Float)),
609 GenericMethod(
"tex1Dgrad",
VectorType.
Float4, GenericParam(
"s",
SamplerType.
Sampler1D), GenericParam(
"t",
ScalarType.
Float), GenericParam(
"ddx",
ScalarType.
Float), GenericParam(
"ddy",
ScalarType.
Float)),
625 GenericMethod(
"tex2D",
VectorType.
Float4, GenericParam(
"s",
SamplerType.
Sampler2D), GenericParam(
"t",
VectorType.
Float2), GenericParam(
"ddx",
VectorType.
Float2), GenericParam(
"ddy",
VectorType.
Float2)),
631 GenericMethod(
"tex2Dgrad",
VectorType.
Float4, GenericParam(
"s",
SamplerType.
Sampler2D), GenericParam(
"t",
VectorType.
Float2), GenericParam(
"ddx",
VectorType.
Float2), GenericParam(
"ddy",
VectorType.
Float2)),
647 GenericMethod(
"tex3D",
VectorType.
Float4, GenericParam(
"s",
SamplerType.
Sampler3D), GenericParam(
"t",
VectorType.
Float3), GenericParam(
"ddx",
VectorType.
Float3), GenericParam(
"ddy",
VectorType.
Float3)),
653 GenericMethod(
"tex3Dgrad",
VectorType.
Float4, GenericParam(
"s",
SamplerType.
Sampler3D), GenericParam(
"t",
VectorType.
Float3), GenericParam(
"ddx",
VectorType.
Float3), GenericParam(
"ddy",
VectorType.
Float3)),
669 GenericMethod(
"texCUBE",
VectorType.
Float4, GenericParam(
"s",
SamplerType.
SamplerCube), GenericParam(
"t",
VectorType.
Float3), GenericParam(
"ddx",
VectorType.
Float3), GenericParam(
"ddy",
VectorType.
Float3)),
675 GenericMethod(
"texCUBEgrad",
VectorType.
Float4, GenericParam(
"s",
SamplerType.
SamplerCube), GenericParam(
"t",
VectorType.
Float3), GenericParam(
"ddx",
VectorType.
Float3), GenericParam(
"ddy",
VectorType.
Float3)),
688 private static List<GenericParameterConstraint> GenericContraint(
string genericT1, Func<TypeBase,bool> checkT1)
693 private static List<GenericParameterConstraint> GenericContraint(
string genericT1, Func<TypeBase,bool> checkT1,
string genericT2, Func<TypeBase,bool> checkT2)
698 private static Ast.Parameter GenericParam(
string paramName,
string genericTypeName)
703 private static Ast.Parameter GenericParam(
string paramName,
TypeBase type)
705 return new Ast.Parameter() { Name =
new Identifier(paramName),
Type = type };
710 return new Ast.Parameter() { Name =
new Identifier(paramName),
Type = type, Qualifiers = qualifier };
713 private static MethodDeclaration GenericMethod(
string methodName,
TypeBase returnType, params Ast.Parameter[] parameters)
715 return GenericMethod(methodName, returnType, null, parameters);
718 private static MethodDeclaration GenericMethod(
string methodName,
TypeBase returnType, List<GenericParameterConstraint> constraints, params Ast.Parameter[] parameters)
721 methodDeclaration.IsBuiltin =
true;
722 if (constraints != null)
723 methodDeclaration.ParameterConstraints = constraints;
724 methodDeclaration.Parameters.AddRange(parameters);
725 return methodDeclaration;
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 };
735 public string Name {
get; set; }
736 public Func<Function, Parameter[], TypeBase> ParamDecl {
get; set; }
737 public bool Out {
get; set; }
741 public static ParamDef Param(
int index)
743 return new ParamDef { Name =
"ret", ParamDecl = (f, p) => p[index].GenerateType() };
746 public static ParamDef ParamVoid()
748 return new ParamDef { Name =
"ret", ParamDecl = (f, p) =>
TypeBase.
Void };
751 public static ParamDef Param(
string name,
int index,
bool outParam =
false)
753 return new ParamDef { Name = name, ParamDecl = (f, p) => p[index].GenerateType(),
Out = outParam };
757 return new ParamDef { Name =
"ret", ParamDecl = paramDecl };
759 public static ParamDef Param(
string name, Func<Function,
Parameter[],
TypeBase> paramDecl)
761 return new ParamDef { Name = name, ParamDecl = paramDecl };
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)
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) };
772 public static Function[] Functions =
new[]
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)),
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)),
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)),
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)),
839 Template1(
"mul",
new[] { Target.Vector }, FloatTargets, Param(0), Param(
"x", (f, p) => p[0].ChangeTarget(
Target.Scalar).GenerateType()), Param(
"y", 0)),
840 Template1(
"mul",
new[] { Target.Matrix }, FloatTargets, Param(0), Param(
"x", (f, p) => p[0].ChangeTarget(
Target.Scalar).GenerateType()), Param(
"y", 0)),
841 Template1(
"mul",
new[] { Target.Vector }, FloatTargets, Param(0), Param(
"x", 0), Param(
"y", (f, p) => p[0].ChangeTarget(
Target.Scalar).GenerateType())),
842 Template1(
"mul",
new[] { Target.Matrix }, FloatTargets, Param(0), Param(
"x", 0), Param(
"y", (f, p) => p[0].ChangeTarget(
Target.Scalar).GenerateType())),
843 Template1(
"mul",
new[] { Target.Vector }, FloatTargets, Param((f, p) => p[0].ChangeTarget(
Target.Scalar).GenerateType()), Param(
"x", 0), Param(
"y", 0)),
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)),
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())),
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())),
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())),
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())),
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())),
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)),
873 static Target[] AllTargets =
new[] { Target.Scalar, Target.Vector, Target.Matrix };
892 public IList<Target> Targets;
893 public IList<TypeBase> TargetTypes;
894 public SizeFlags SizeFlags;
898 public Parameter ChangeTarget(Target target)
900 return new Parameter {
Target = target, TargetType = TargetType, TargetSize = TargetSize };
906 public Parameter MakeMatrix(
int rowCount,
int columnCount)
908 return new Parameter {
Target = Target.Matrix, TargetType = TargetType, TargetSize = (rowCount - 1) * 4 + columnCount - 1 };
912 return new Parameter {
Target = Target.Vector, TargetType = TargetType, TargetSize = TargetSize / 4 };
914 public Parameter ReduceFromMatrixColumn()
916 return new Parameter {
Target = Target.Vector, TargetType = TargetType, TargetSize = TargetSize % 4 };
920 int row = TargetSize / 4;
921 int column = TargetSize % 4;
924 public int RowCount {
get {
return (TargetSize / 4) + 1; } }
925 public int ColumnCount {
get {
return (TargetSize % 4) + 1; } }
927 public TypeBase TargetType {
get; set; }
928 public int TargetSize {
get; set; }
932 if (Target ==
Target.Matrix)
934 if (Target ==
Target.Vector)
936 if (Target ==
Target.Vector2)
938 if (Target ==
Target.Vector3)
940 if (Target ==
Target.Vector4)
948 for (
int target = 0; target < p.Targets.Count(); ++target)
950 for (
int targetType = 0; targetType < p.TargetTypes.Count(); ++targetType)
952 switch (p.Targets[target])
955 yield
return new Parameter {
Target = p.Targets[target], TargetType = p.TargetTypes[targetType], TargetSize = 1 };
958 case Target.SquareMatrix:
959 for (
int i = 0; i < 4; ++i)
961 yield
return new Parameter {
Target = p.Targets[target], TargetType = p.TargetTypes[targetType], TargetSize = i };
965 yield
return new Parameter {
Target = p.Targets[target], TargetType = p.TargetTypes[targetType], TargetSize = 1 };
968 yield
return new Parameter {
Target = p.Targets[target], TargetType = p.TargetTypes[targetType], TargetSize = 2 };
971 yield
return new Parameter {
Target = p.Targets[target], TargetType = p.TargetTypes[targetType], TargetSize = 3 };
974 for (
int i = 0; i < 16; ++i)
976 yield
return new Parameter {
Target = p.Targets[target], TargetType = p.TargetTypes[targetType], TargetSize = i };
988 internal class GenericInstanceKey
990 public string GenericName;
992 public List<Node> GenericParameters;
994 public GenericInstanceKey(
string genericName, List<Node> genericParams)
996 GenericName = genericName;
997 GenericParameters = genericParams;
1000 public override bool Equals(
object obj)
1002 var genInstKey = obj as GenericInstanceKey;
1003 if (genInstKey == null)
1006 if (GenericParameters.Count != genInstKey.GenericParameters.Count)
1010 for (
int i = 0; i < GenericParameters.Count; ++i)
1011 res &= GenericParameters[i] == genInstKey.GenericParameters[i];
1016 public override int GetHashCode()
1018 return (GenericName.GetHashCode() * 397);
Base class for all vector types
virtual void Visit(Technique technique)
static readonly ScalarType Bool
Scalar bool.
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
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.
Specialized ParameterQualifier for Hlsl.
static readonly VectorType Float3
A Float3
static readonly ScalarType Int
Scalar int.
TypeInference TypeInference
Gets or sets the resolved reference.
string Text
Gets or sets the name.
A state expresion in the form: sampler {...}.
static readonly SamplerType Sampler3D
A sampler3D.
A generic declaration. This is used internally to identify a generic declaration. ...
override TypeBase Visit(TypeName typeName)
Visits the specified type name.
void Visit(AsmExpression asmExpression)
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
static readonly VectorType Int4
A Int4
static readonly ScalarType UInt
Scalar unsigned int.
virtual void Visit(CompileExpression compileExpression)
TypeInference TypeInference
Gets or sets the type reference.
static List< IDeclaration > ParseBuiltin(string builtins, string fileName)
Flags
Enumeration of the new Assimp's flags.
static readonly ScalarType Float
Sclar float.
virtual void Visit(TextureType textureType)
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.
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}.
void Visit(CastExpression castExpression)
static readonly SamplerType Sampler1D
A sampler1D.
Search for out only dependencies.
A Generic parameter for a method that provides a constraint resolver.
A member reference in the form {this}.{Name}
static void InitializeBuiltins()
override void ProcessMethodInvocation(MethodInvocationExpression expression, string methodName, List< IDeclaration > declarations)
Pres the process method invocation.
Defines a generic parameter type.
List< Node > Parameters
Gets or sets the parameters.
static readonly ScalarType Double
Scalar double.
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.
A reference to a variable.
static readonly VectorType Int2
A Int2
SiliconStudio.Core.Mathematics.Vector3 Vector3
Toplevel interface for a declaration.
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.
static readonly ScalarType Void
Scalar void. TODO this is not a scalar!
Expression Target
Gets or sets the this.
static readonly SamplerType Sampler2D
A sampler2D
HlslSemanticAnalysis(ParsingResult result)
Initializes a new instance of the HlslSemanticAnalysis class.
A Type reference analysis is building type references.
virtual void Visit(StateExpression stateExpression)
void Visit(Annotations annotations)
object GetTag(object tagKey)
Gets a tag value associated to this node..
static void Run(ParsingResult toParse, List< IDeclaration > builtinDeclarations=null)