4 using SiliconStudio.Shaders.Ast;
6 namespace SiliconStudio.Shaders.Analysis
8 internal class CastHelper
16 public static bool NeedConvertForBinary(
TypeBase leftType,
TypeBase rightType)
18 return leftType != null && rightType != null && leftType != rightType
37 var leftMatrix = (MatrixType)left;
38 var rightMatrix = (MatrixType)right;
40 var leftDimension1 = leftMatrix.RowCount;
41 var leftDimension2 = leftMatrix.ColumnCount;
42 var rightDimension1 = rightMatrix.RowCount;
43 var rightDimension2 = rightMatrix.ColumnCount;
45 if (!((leftDimension1 >= rightDimension1 && leftDimension2 >= rightDimension2) || (leftDimension1 <= rightDimension1 && leftDimension2 <= rightDimension2)))
50 var type = isBooleanOperator ? ScalarType.Bool : GetBinaryImplicitScalarConversionType(leftMatrix.Type.ResolveType(), rightMatrix.Type.ResolveType());
52 return new MatrixType(type, Math.Min(leftDimension1, rightDimension1), Math.Min(leftDimension2, rightDimension2));
59 if (right is MatrixType || (!(left is MatrixType) && !(left is
VectorType)))
66 if (left is MatrixType && right is VectorType)
68 var leftMatrix = (MatrixType)left;
69 var rightVector = (VectorType)right;
71 var leftDimension1 = leftMatrix.RowCount;
72 var leftDimension2 = leftMatrix.ColumnCount;
73 var rightDimension1 = rightVector.Dimension;
76 if (leftDimension1 != 1 && leftDimension2 != 1)
81 var type = isBooleanOperator ? ScalarType.Bool : GetBinaryImplicitScalarConversionType(leftMatrix.Type.ResolveType(), rightVector.Type.ResolveType());
83 return new VectorType(type, Math.Min(Math.Max(leftDimension1, leftDimension2), rightDimension1));
87 if (left is MatrixType)
89 var leftMatrix = (MatrixType)left;
91 var leftDimension1 = leftMatrix.RowCount;
92 var leftDimension2 = leftMatrix.ColumnCount;
94 var type = isBooleanOperator ? ScalarType.Bool : GetBinaryImplicitScalarConversionType(leftMatrix.Type.ResolveType(), right);
96 return new MatrixType(type, leftDimension1, leftDimension2);
100 if (left is VectorType)
102 var leftVector = (VectorType)left;
103 var leftDimension1 = leftVector.Dimension;
105 var rightDimension1 = right is VectorType ? ((VectorType)right).Dimension : 1;
107 var rightBaseType = right is VectorType ? ((VectorType)right).Type.ResolveType() : right;
109 int dimension = Math.Min(leftDimension1, rightDimension1);
110 if (rightDimension1 == 1 || leftDimension1 == 1)
111 dimension = Math.Max(leftDimension1, rightDimension1);
113 var type = isBooleanOperator ? ScalarType.Bool : GetBinaryImplicitScalarConversionType(leftVector.Type.ResolveType(), rightBaseType);
114 if (type != null)
return new VectorType(type, dimension);
118 return (isBooleanOperator) ? ScalarType.Bool : GetBinaryImplicitScalarConversionType(left, right);
129 if ((left is VectorType || left is MatrixType) && right is
ScalarType)
130 return GetMultiplyImplicitConversionType(right, left);
132 if (left is ScalarType)
135 if (right is VectorType) { componentType = (right as VectorType).Type; }
136 else if (right is MatrixType) { componentType = (right as MatrixType).Type; }
138 if (componentType != null)
140 ScalarType resultComponentType = null;
141 if (left == ScalarType.Double || componentType == ScalarType.Double)
142 resultComponentType = ScalarType.Double;
143 else if (left == ScalarType.Float || componentType == ScalarType.Float)
144 resultComponentType = ScalarType.Float;
145 else if (left == ScalarType.Half || componentType == ScalarType.Half)
147 else if (left == ScalarType.Int || componentType == ScalarType.Int)
148 resultComponentType = ScalarType.Int;
149 else if (left == ScalarType.UInt || componentType == ScalarType.UInt)
150 resultComponentType = ScalarType.UInt;
152 if (resultComponentType != null)
154 if (right is VectorType)
155 return new VectorType(resultComponentType, (right as VectorType).Dimension);
157 if (right is MatrixType)
158 return new MatrixType(resultComponentType, (right as MatrixType).RowCount, (right as MatrixType).ColumnCount);
163 return GetBinaryImplicitConversionType(left, right,
false);
174 if (right is ScalarType)
177 if (left is VectorType) { componentType = (left as VectorType).Type; }
178 else if (left is MatrixType) { componentType = (left as MatrixType).Type; }
180 if (componentType != null)
182 ScalarType resultComponentType = null;
183 if (left == ScalarType.Double || componentType == ScalarType.Double)
184 resultComponentType = ScalarType.Double;
185 else if (left == ScalarType.Float || componentType == ScalarType.Float)
186 resultComponentType = ScalarType.Float;
187 else if (left == ScalarType.Half || componentType == ScalarType.Half)
189 else if (left == ScalarType.Int || componentType == ScalarType.Int)
190 resultComponentType = ScalarType.Int;
191 else if (left == ScalarType.UInt || componentType == ScalarType.UInt)
192 resultComponentType = ScalarType.UInt;
194 if (resultComponentType != null)
196 if (left is VectorType)
197 return new VectorType(resultComponentType, (left as VectorType).Dimension);
199 if (left is MatrixType)
200 return new MatrixType(resultComponentType, (left as MatrixType).RowCount, (left as MatrixType).ColumnCount);
205 return GetBinaryImplicitConversionType(left, right,
false);
216 public static ScalarType GetBinaryImplicitScalarConversionType(
TypeBase left,
TypeBase right)
218 if (left is ScalarType && right is ScalarType)
221 return (ScalarType)left;
223 foreach (var type
in new[] { ScalarType.Double, ScalarType.Float,
ScalarType.Half, ScalarType.UInt, ScalarType.Int, ScalarType.Bool })
225 if (left == type || right == type)
Base class for all vector types
SiliconStudio.Paradox.Games.Mathematics.Half Half
static TypeBase GetBaseType(TypeBase type)