1 /*
2 * Copyright 2021 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "src/sksl/SkSLBuiltinTypes.h"
9
10 #include "include/private/SkSLModifiers.h"
11 #include "src/sksl/SkSLErrorReporter.h"
12 #include "src/sksl/ir/SkSLExpression.h"
13 #include "src/sksl/spirv.h"
14
15 namespace SkSL {
16
17 /** Create a scalar type. */
MakeScalarType(const char * name,const char * abbrev,Type::NumberKind numberKind,int priority,bool highPrecision)18 std::unique_ptr<Type> BuiltinTypes::MakeScalarType(const char* name,
19 const char* abbrev,
20 Type::NumberKind numberKind,
21 int priority,
22 bool highPrecision) {
23 return std::unique_ptr<Type>(new Type(name, abbrev, numberKind, priority, highPrecision));
24 }
25
26 /** Create a type for literal scalars. */
MakeLiteralType(const char * name,const Type & scalarType,int priority)27 std::unique_ptr<Type> BuiltinTypes::MakeLiteralType(const char* name,
28 const Type& scalarType,
29 int priority) {
30 return std::unique_ptr<Type>(new Type(name, scalarType, priority));
31 }
32
33 /** Create a vector type. */
MakeVectorType(const char * name,const char * abbrev,const Type & componentType,int columns)34 std::unique_ptr<Type> BuiltinTypes::MakeVectorType(const char* name,
35 const char* abbrev,
36 const Type& componentType,
37 int columns) {
38 return std::unique_ptr<Type>(new Type(name, abbrev, Type::TypeKind::kVector,
39 componentType, columns));
40 }
41
42 /**
43 * Create a generic type which maps to the listed types--e.g. $genType is a generic type which
44 * can match float, float2, float3 or float4.
45 */
MakeGenericType(const char * name,std::vector<const Type * > types)46 std::unique_ptr<Type> BuiltinTypes::MakeGenericType(const char* name,
47 std::vector<const Type*> types) {
48 return std::unique_ptr<Type>(new Type(name, std::move(types)));
49 }
50
51 /** Create a matrix type. */
MakeMatrixType(const char * name,const char * abbrev,const Type & componentType,int columns,int rows)52 std::unique_ptr<Type> BuiltinTypes::MakeMatrixType(const char* name,
53 const char* abbrev,
54 const Type& componentType,
55 int columns,
56 int rows) {
57 return std::unique_ptr<Type>(new Type(name, abbrev, componentType, columns, rows));
58 }
59
60 /** Create a texture type. */
MakeTextureType(const char * name,SpvDim_ dimensions,bool isDepth,bool isArrayedTexture,bool isMultisampled,bool isSampled)61 std::unique_ptr<Type> BuiltinTypes::MakeTextureType(const char* name,
62 SpvDim_ dimensions,
63 bool isDepth,
64 bool isArrayedTexture,
65 bool isMultisampled,
66 bool isSampled) {
67 return std::unique_ptr<Type>(
68 new Type(name, dimensions, isDepth, isArrayedTexture, isMultisampled, isSampled));
69 }
70
71 /** Create a sampler type. */
MakeSamplerType(const char * name,const Type & textureType)72 std::unique_ptr<Type> BuiltinTypes::MakeSamplerType(const char* name, const Type& textureType) {
73 return std::unique_ptr<Type>(new Type(name, textureType));
74 }
75
76 /**
77 * Create a "special" type with the given name, abbreviation, and TypeKind.
78 */
MakeSpecialType(const char * name,const char * abbrev,Type::TypeKind typeKind)79 std::unique_ptr<Type> BuiltinTypes::MakeSpecialType(const char* name,
80 const char* abbrev,
81 Type::TypeKind typeKind) {
82 return std::unique_ptr<Type>(new Type(name, abbrev, typeKind));
83 }
84
85 /**
86 * Initializes the core SkSL types.
87 */
BuiltinTypes()88 BuiltinTypes::BuiltinTypes()
89 : fFloat(MakeScalarType(
90 "float", "f", Type::NumberKind::kFloat, /*priority=*/10, /*highPrecision=*/true))
91 , fFloat2(MakeVectorType("float2", "f2", *fFloat, /*columns=*/2))
92 , fFloat3(MakeVectorType("float3", "f3", *fFloat, /*columns=*/3))
93 , fFloat4(MakeVectorType("float4", "f4", *fFloat, /*columns=*/4))
94 , fHalf(MakeScalarType("half", "h", Type::NumberKind::kFloat, /*priority=*/9))
95 , fHalf2(MakeVectorType("half2", "h2", *fHalf, /*columns=*/2))
96 , fHalf3(MakeVectorType("half3", "h3", *fHalf, /*columns=*/3))
97 , fHalf4(MakeVectorType("half4", "h4", *fHalf, /*columns=*/4))
98 , fInt(MakeScalarType(
99 "int", "i", Type::NumberKind::kSigned, /*priority=*/7, /*highPrecision=*/true))
100 , fInt2(MakeVectorType("int2", "i2", *fInt, /*columns=*/2))
101 , fInt3(MakeVectorType("int3", "i3", *fInt, /*columns=*/3))
102 , fInt4(MakeVectorType("int4", "i4", *fInt, /*columns=*/4))
103 , fUInt(MakeScalarType(
104 "uint", "I", Type::NumberKind::kUnsigned, /*priority=*/6, /*highPrecision=*/true))
105 , fUInt2(MakeVectorType("uint2", "I2", *fUInt, /*columns=*/2))
106 , fUInt3(MakeVectorType("uint3", "I3", *fUInt, /*columns=*/3))
107 , fUInt4(MakeVectorType("uint4", "I4", *fUInt, /*columns=*/4))
108 , fShort(MakeScalarType("short", "s", Type::NumberKind::kSigned, /*priority=*/4))
109 , fShort2(MakeVectorType("short2", "s2", *fShort, /*columns=*/2))
110 , fShort3(MakeVectorType("short3", "s3", *fShort, /*columns=*/3))
111 , fShort4(MakeVectorType("short4", "s4", *fShort, /*columns=*/4))
112 , fUShort(MakeScalarType("ushort", "S", Type::NumberKind::kUnsigned, /*priority=*/3))
113 , fUShort2(MakeVectorType("ushort2", "S2", *fUShort, /*columns=*/2))
114 , fUShort3(MakeVectorType("ushort3", "S3", *fUShort, /*columns=*/3))
115 , fUShort4(MakeVectorType("ushort4", "S4", *fUShort, /*columns=*/4))
116 , fBool(MakeScalarType("bool", "b", Type::NumberKind::kBoolean, /*priority=*/0))
117 , fBool2(MakeVectorType("bool2", "b2", *fBool, /*columns=*/2))
118 , fBool3(MakeVectorType("bool3", "b3", *fBool, /*columns=*/3))
119 , fBool4(MakeVectorType("bool4", "b4", *fBool, /*columns=*/4))
120 , fInvalid(MakeSpecialType("<INVALID>", "O", Type::TypeKind::kOther))
121 , fVoid(MakeSpecialType("void", "v", Type::TypeKind::kVoid))
122 , fFloatLiteral(MakeLiteralType("$floatLiteral", *fFloat, /*priority=*/8))
123 , fIntLiteral(MakeLiteralType("$intLiteral", *fInt, /*priority=*/5))
124 , fFloat2x2(MakeMatrixType("float2x2", "f22", *fFloat, /*columns=*/2, /*rows=*/2))
125 , fFloat2x3(MakeMatrixType("float2x3", "f23", *fFloat, /*columns=*/2, /*rows=*/3))
126 , fFloat2x4(MakeMatrixType("float2x4", "f24", *fFloat, /*columns=*/2, /*rows=*/4))
127 , fFloat3x2(MakeMatrixType("float3x2", "f32", *fFloat, /*columns=*/3, /*rows=*/2))
128 , fFloat3x3(MakeMatrixType("float3x3", "f33", *fFloat, /*columns=*/3, /*rows=*/3))
129 , fFloat3x4(MakeMatrixType("float3x4", "f34", *fFloat, /*columns=*/3, /*rows=*/4))
130 , fFloat4x2(MakeMatrixType("float4x2", "f42", *fFloat, /*columns=*/4, /*rows=*/2))
131 , fFloat4x3(MakeMatrixType("float4x3", "f43", *fFloat, /*columns=*/4, /*rows=*/3))
132 , fFloat4x4(MakeMatrixType("float4x4", "f44", *fFloat, /*columns=*/4, /*rows=*/4))
133 , fHalf2x2(MakeMatrixType("half2x2", "h22", *fHalf, /*columns=*/2, /*rows=*/2))
134 , fHalf2x3(MakeMatrixType("half2x3", "h23", *fHalf, /*columns=*/2, /*rows=*/3))
135 , fHalf2x4(MakeMatrixType("half2x4", "h24", *fHalf, /*columns=*/2, /*rows=*/4))
136 , fHalf3x2(MakeMatrixType("half3x2", "h32", *fHalf, /*columns=*/3, /*rows=*/2))
137 , fHalf3x3(MakeMatrixType("half3x3", "h33", *fHalf, /*columns=*/3, /*rows=*/3))
138 , fHalf3x4(MakeMatrixType("half3x4", "h34", *fHalf, /*columns=*/3, /*rows=*/4))
139 , fHalf4x2(MakeMatrixType("half4x2", "h42", *fHalf, /*columns=*/4, /*rows=*/2))
140 , fHalf4x3(MakeMatrixType("half4x3", "h43", *fHalf, /*columns=*/4, /*rows=*/3))
141 , fHalf4x4(MakeMatrixType("half4x4", "h44", *fHalf, /*columns=*/4, /*rows=*/4))
142 , fTexture1D(MakeTextureType("texture1D",
143 SpvDim1D,
144 /*isDepth=*/false,
145 /*isArrayedTexture=*/false,
146 /*isMultisampled=*/false,
147 /*isSampled=*/true))
148 , fTexture2D(MakeTextureType("texture2D",
149 SpvDim2D,
150 /*isDepth=*/false,
151 /*isArrayedTexture=*/false,
152 /*isMultisampled=*/false,
153 /*isSampled=*/true))
154 , fTexture3D(MakeTextureType("texture3D",
155 SpvDim3D,
156 /*isDepth=*/false,
157 /*isArrayedTexture=*/false,
158 /*isMultisampled=*/false,
159 /*isSampled=*/true))
160 , fTextureExternalOES(MakeTextureType("textureExternalOES",
161 SpvDim2D,
162 /*isDepth=*/false,
163 /*isArrayedTexture=*/false,
164 /*isMultisampled=*/false,
165 /*isSampled=*/true))
166 , fTextureCube(MakeTextureType("textureCube",
167 SpvDimCube,
168 /*isDepth=*/false,
169 /*isArrayedTexture=*/false,
170 /*isMultisampled=*/false,
171 /*isSampled=*/true))
172 , fTexture2DRect(MakeTextureType("texture2DRect",
173 SpvDimRect,
174 /*isDepth=*/false,
175 /*isArrayedTexture=*/false,
176 /*isMultisampled=*/false,
177 /*isSampled=*/true))
178 , fITexture2D(MakeTextureType("itexture2D",
179 SpvDim2D,
180 /*isDepth=*/false,
181 /*isArrayedTexture=*/false,
182 /*isMultisampled=*/false,
183 /*isSampled=*/true))
184 , fSampler1D(MakeSamplerType("sampler1D", *fTexture1D))
185 , fSampler2D(MakeSamplerType("sampler2D", *fTexture2D))
186 , fSampler3D(MakeSamplerType("sampler3D", *fTexture3D))
187 , fSamplerExternalOES(MakeSamplerType("samplerExternalOES", *fTextureExternalOES))
188 , fSampler2DRect(MakeSamplerType("sampler2DRect", *fTexture2DRect))
189
190 , fISampler2D(MakeSamplerType("isampler2D", *fITexture2D))
191
192 , fSampler(MakeSpecialType("sampler", "ss", Type::TypeKind::kSeparateSampler))
193
194 , fSubpassInput(MakeTextureType("subpassInput",
195 SpvDimSubpassData,
196 /*isDepth=*/false,
197 /*isArrayedTexture=*/false,
198 /*isMultisampled=*/false,
199 /*isSampled=*/false))
200 , fSubpassInputMS(MakeTextureType("subpassInputMS",
201 SpvDimSubpassData,
202 /*isDepth=*/false,
203 /*isArrayedTexture=*/false,
204 /*isMultisampled=*/true,
205 /*isSampled=*/false))
206
207 , fGenType(MakeGenericType("$genType",
208 {fFloat.get(), fFloat2.get(), fFloat3.get(), fFloat4.get()}))
209 , fGenHType(MakeGenericType("$genHType",
210 {fHalf.get(), fHalf2.get(), fHalf3.get(), fHalf4.get()}))
211 , fGenIType(
212 MakeGenericType("$genIType", {fInt.get(), fInt2.get(), fInt3.get(), fInt4.get()}))
213 , fGenUType(MakeGenericType("$genUType",
214 {fUInt.get(), fUInt2.get(), fUInt3.get(), fUInt4.get()}))
215 , fGenBType(MakeGenericType("$genBType",
216 {fBool.get(), fBool2.get(), fBool3.get(), fBool4.get()}))
217 , fMat(MakeGenericType("$mat",
218 {fFloat2x2.get(), fFloat2x3.get(), fFloat2x4.get(), fFloat3x2.get(),
219 fFloat3x3.get(), fFloat3x4.get(), fFloat4x2.get(), fFloat4x3.get(),
220 fFloat4x4.get()}))
221 , fHMat(MakeGenericType(
222 "$hmat",
223 {fHalf2x2.get(), fHalf2x3.get(), fHalf2x4.get(), fHalf3x2.get(), fHalf3x3.get(),
224 fHalf3x4.get(), fHalf4x2.get(), fHalf4x3.get(), fHalf4x4.get()}))
225 , fSquareMat(MakeGenericType("$squareMat",
226 {fFloat2x2.get(), fFloat3x3.get(), fFloat4x4.get()}))
227 , fSquareHMat(
228 MakeGenericType("$squareHMat", {fHalf2x2.get(), fHalf3x3.get(), fHalf4x4.get()}))
229 , fVec(MakeGenericType("$vec",
230 {fInvalid.get(), fFloat2.get(), fFloat3.get(), fFloat4.get()}))
231 , fHVec(MakeGenericType("$hvec",
232 {fInvalid.get(), fHalf2.get(), fHalf3.get(), fHalf4.get()}))
233 , fIVec(MakeGenericType("$ivec", {fInvalid.get(), fInt2.get(), fInt3.get(), fInt4.get()}))
234 , fUVec(MakeGenericType("$uvec",
235 {fInvalid.get(), fUInt2.get(), fUInt3.get(), fUInt4.get()}))
236 , fSVec(MakeGenericType("$svec",
237 {fInvalid.get(), fShort2.get(), fShort3.get(), fShort4.get()}))
238 , fUSVec(MakeGenericType("$usvec",
239 {fInvalid.get(), fUShort2.get(), fUShort3.get(), fUShort4.get()}))
240 , fBVec(MakeGenericType("$bvec",
241 {fInvalid.get(), fBool2.get(), fBool3.get(), fBool4.get()}))
242 , fSkCaps(MakeSpecialType("$sk_Caps", "O", Type::TypeKind::kOther))
243 , fFragmentProcessor(MakeSpecialType("fragmentProcessor", "fp",
244 Type::TypeKind::kFragmentProcessor))
245 , fColorFilter(MakeSpecialType("colorFilter", "CF", Type::TypeKind::kColorFilter))
246 , fShader(MakeSpecialType("shader", "SH", Type::TypeKind::kShader)) {}
247
248 } // namespace SkSL
249