1 /* 2 * Copyright 2016 Google Inc. 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 #ifndef SKSL_CONTEXT 9 #define SKSL_CONTEXT 10 11 #include <memory> 12 13 #include "src/sksl/ir/SkSLExpression.h" 14 #include "src/sksl/ir/SkSLType.h" 15 16 namespace SkSL { 17 18 /** 19 * Contains compiler-wide objects, which currently means the core types. 20 */ 21 class Context { 22 public: Context()23 Context() 24 : fInvalid_Type(new Type("<INVALID>")) 25 , fVoid_Type(new Type("void")) 26 , fNull_Type(new Type("null")) 27 , fFloatLiteral_Type(new Type("$floatLiteral", Type::NumberKind::kFloat, 3)) 28 , fIntLiteral_Type(new Type("$intLiteral", Type::NumberKind::kSigned, 1)) 29 , fFloat_Type(new Type("float", Type::NumberKind::kFloat, 5, true)) 30 , fFloat2_Type(new Type("float2", *fFloat_Type, 2)) 31 , fFloat3_Type(new Type("float3", *fFloat_Type, 3)) 32 , fFloat4_Type(new Type("float4", *fFloat_Type, 4)) 33 , fHalf_Type(new Type("half", Type::NumberKind::kFloat, 4)) 34 , fHalf2_Type(new Type("half2", *fHalf_Type, 2)) 35 , fHalf3_Type(new Type("half3", *fHalf_Type, 3)) 36 , fHalf4_Type(new Type("half4", *fHalf_Type, 4)) 37 , fUInt_Type(new Type("uint", Type::NumberKind::kUnsigned, 2, true)) 38 , fUInt2_Type(new Type("uint2", *fUInt_Type, 2)) 39 , fUInt3_Type(new Type("uint3", *fUInt_Type, 3)) 40 , fUInt4_Type(new Type("uint4", *fUInt_Type, 4)) 41 , fInt_Type(new Type("int", Type::NumberKind::kSigned, 2, true)) 42 , fInt2_Type(new Type("int2", *fInt_Type, 2)) 43 , fInt3_Type(new Type("int3", *fInt_Type, 3)) 44 , fInt4_Type(new Type("int4", *fInt_Type, 4)) 45 , fUShort_Type(new Type("ushort", Type::NumberKind::kUnsigned, 0)) 46 , fUShort2_Type(new Type("ushort2", *fUShort_Type, 2)) 47 , fUShort3_Type(new Type("ushort3", *fUShort_Type, 3)) 48 , fUShort4_Type(new Type("ushort4", *fUShort_Type, 4)) 49 , fShort_Type(new Type("short", Type::NumberKind::kSigned, 0)) 50 , fShort2_Type(new Type("short2", *fShort_Type, 2)) 51 , fShort3_Type(new Type("short3", *fShort_Type, 3)) 52 , fShort4_Type(new Type("short4", *fShort_Type, 4)) 53 , fUByte_Type(new Type("ubyte", Type::NumberKind::kUnsigned, 0)) 54 , fUByte2_Type(new Type("ubyte2", *fUByte_Type, 2)) 55 , fUByte3_Type(new Type("ubyte3", *fUByte_Type, 3)) 56 , fUByte4_Type(new Type("ubyte4", *fUByte_Type, 4)) 57 , fByte_Type(new Type("byte", Type::NumberKind::kSigned, 0)) 58 , fByte2_Type(new Type("byte2", *fByte_Type, 2)) 59 , fByte3_Type(new Type("byte3", *fByte_Type, 3)) 60 , fByte4_Type(new Type("byte4", *fByte_Type, 4)) 61 , fBool_Type(new Type("bool", Type::NumberKind::kNonnumeric, -1)) 62 , fBool2_Type(new Type("bool2", *fBool_Type, 2)) 63 , fBool3_Type(new Type("bool3", *fBool_Type, 3)) 64 , fBool4_Type(new Type("bool4", *fBool_Type, 4)) 65 , fFloat2x2_Type(new Type("float2x2", *fFloat_Type, 2, 2)) 66 , fFloat2x3_Type(new Type("float2x3", *fFloat_Type, 2, 3)) 67 , fFloat2x4_Type(new Type("float2x4", *fFloat_Type, 2, 4)) 68 , fFloat3x2_Type(new Type("float3x2", *fFloat_Type, 3, 2)) 69 , fFloat3x3_Type(new Type("float3x3", *fFloat_Type, 3, 3)) 70 , fFloat3x4_Type(new Type("float3x4", *fFloat_Type, 3, 4)) 71 , fFloat4x2_Type(new Type("float4x2", *fFloat_Type, 4, 2)) 72 , fFloat4x3_Type(new Type("float4x3", *fFloat_Type, 4, 3)) 73 , fFloat4x4_Type(new Type("float4x4", *fFloat_Type, 4, 4)) 74 , fHalf2x2_Type(new Type("half2x2", *fHalf_Type, 2, 2)) 75 , fHalf2x3_Type(new Type("half2x3", *fHalf_Type, 2, 3)) 76 , fHalf2x4_Type(new Type("half2x4", *fHalf_Type, 2, 4)) 77 , fHalf3x2_Type(new Type("half3x2", *fHalf_Type, 3, 2)) 78 , fHalf3x3_Type(new Type("half3x3", *fHalf_Type, 3, 3)) 79 , fHalf3x4_Type(new Type("half3x4", *fHalf_Type, 3, 4)) 80 , fHalf4x2_Type(new Type("half4x2", *fHalf_Type, 4, 2)) 81 , fHalf4x3_Type(new Type("half4x3", *fHalf_Type, 4, 3)) 82 , fHalf4x4_Type(new Type("half4x4", *fHalf_Type, 4, 4)) 83 , fTexture1D_Type(new Type("texture1D", SpvDim1D, false, false, false, true)) 84 , fTexture2D_Type(new Type("texture2D", SpvDim2D, false, false, false, true)) 85 , fTexture3D_Type(new Type("texture3D", SpvDim3D, false, false, false, true)) 86 , fTextureExternalOES_Type(new Type("textureExternalOES", SpvDim2D, false, false, false, true)) 87 , fTextureCube_Type(new Type("textureCube", SpvDimCube, false, false, false, true)) 88 , fTexture2DRect_Type(new Type("texture2DRect", SpvDimRect, false, false, false, true)) 89 , fTextureBuffer_Type(new Type("textureBuffer", SpvDimBuffer, false, false, false, true)) 90 , fITexture2D_Type(new Type("itexture2D", SpvDim2D, false, false, false, true)) 91 , fSampler1D_Type(new Type("sampler1D", *fTexture1D_Type)) 92 , fSampler2D_Type(new Type("sampler2D", *fTexture2D_Type)) 93 , fSampler3D_Type(new Type("sampler3D", *fTexture3D_Type)) 94 , fSamplerExternalOES_Type(new Type("samplerExternalOES", *fTextureExternalOES_Type)) 95 , fSamplerCube_Type(new Type("samplerCube", *fTextureCube_Type)) 96 , fSampler2DRect_Type(new Type("sampler2DRect", *fTexture2DRect_Type)) 97 , fSampler1DArray_Type(new Type("sampler1DArray")) 98 , fSampler2DArray_Type(new Type("sampler2DArray")) 99 , fSamplerCubeArray_Type(new Type("samplerCubeArray")) 100 , fSamplerBuffer_Type(new Type("samplerBuffer", *fTextureBuffer_Type)) 101 , fSampler2DMS_Type(new Type("sampler2DMS")) 102 , fSampler2DMSArray_Type(new Type("sampler2DMSArray")) 103 , fSampler1DShadow_Type(new Type("sampler1DShadow")) 104 , fSampler2DShadow_Type(new Type("sampler2DShadow")) 105 , fSamplerCubeShadow_Type(new Type("samplerCubeShadow")) 106 , fSampler2DRectShadow_Type(new Type("sampler2DRectShadow")) 107 , fSampler1DArrayShadow_Type(new Type("sampler1DArrayShadow")) 108 , fSampler2DArrayShadow_Type(new Type("sampler2DArrayShadow")) 109 , fSamplerCubeArrayShadow_Type(new Type("samplerCubeArrayShadow")) 110 111 // Related to below FIXME, gsampler*s don't currently expand to cover integer case. 112 , fISampler2D_Type(new Type("isampler2D", *fITexture2D_Type)) 113 114 , fSampler_Type(new Type("sampler", Type::TypeKind::kSeparateSampler)) 115 // FIXME express these as "gimage2D" that expand to image2D, iimage2D, and uimage2D. 116 , fImage2D_Type(new Type("image2D", SpvDim2D, false, false, false, true)) 117 , fIImage2D_Type(new Type("iimage2D", SpvDim2D, false, false, false, true)) 118 119 // FIXME express these as "gsubpassInput" that expand to subpassInput, isubpassInput, 120 // and usubpassInput. 121 , fSubpassInput_Type(new Type("subpassInput", SpvDimSubpassData, false, false, 122 false, false)) 123 , fSubpassInputMS_Type(new Type("subpassInputMS", SpvDimSubpassData, false, false, 124 true, false)) 125 126 // FIXME figure out what we're supposed to do with the gsampler et al. types) 127 , fGSampler1D_Type(new Type("$gsampler1D", static_type(*fSampler1D_Type))) 128 , fGSampler2D_Type(new Type("$gsampler2D", static_type(*fSampler2D_Type))) 129 , fGSampler3D_Type(new Type("$gsampler3D", static_type(*fSampler3D_Type))) 130 , fGSamplerCube_Type(new Type("$gsamplerCube", static_type(*fSamplerCube_Type))) 131 , fGSampler2DRect_Type(new Type("$gsampler2DRect", static_type(*fSampler2DRect_Type))) 132 , fGSampler1DArray_Type(new Type("$gsampler1DArray", 133 static_type(*fSampler1DArray_Type))) 134 , fGSampler2DArray_Type(new Type("$gsampler2DArray", 135 static_type(*fSampler2DArray_Type))) 136 , fGSamplerCubeArray_Type(new Type("$gsamplerCubeArray", 137 static_type(*fSamplerCubeArray_Type))) 138 , fGSamplerBuffer_Type(new Type("$gsamplerBuffer", static_type(*fSamplerBuffer_Type))) 139 , fGSampler2DMS_Type(new Type("$gsampler2DMS", static_type(*fSampler2DMS_Type))) 140 , fGSampler2DMSArray_Type(new Type("$gsampler2DMSArray", 141 static_type(*fSampler2DMSArray_Type))) 142 , fGSampler2DArrayShadow_Type(new Type("$gsampler2DArrayShadow", 143 static_type(*fSampler2DArrayShadow_Type))) 144 , fGSamplerCubeArrayShadow_Type(new Type("$gsamplerCubeArrayShadow", 145 static_type(*fSamplerCubeArrayShadow_Type))) 146 , fGenType_Type(new Type("$genType", { fFloat_Type.get(), fFloat2_Type.get(), 147 fFloat3_Type.get(), fFloat4_Type.get() })) 148 , fGenHType_Type(new Type("$genHType", { fHalf_Type.get(), fHalf2_Type.get(), 149 fHalf3_Type.get(), fHalf4_Type.get() })) 150 , fGenIType_Type(new Type("$genIType", { fInt_Type.get(), fInt2_Type.get(), 151 fInt3_Type.get(), fInt4_Type.get() })) 152 , fGenUType_Type(new Type("$genUType", { fUInt_Type.get(), fUInt2_Type.get(), 153 fUInt3_Type.get(), fUInt4_Type.get() })) 154 , fGenBType_Type(new Type("$genBType", { fBool_Type.get(), fBool2_Type.get(), 155 fBool3_Type.get(), fBool4_Type.get() })) 156 , fMat_Type(new Type("$mat", { fFloat2x2_Type.get(), fFloat2x3_Type.get(), 157 fFloat2x4_Type.get(), fFloat3x2_Type.get(), 158 fFloat3x3_Type.get(), fFloat3x4_Type.get(), 159 fFloat4x2_Type.get(), fFloat4x3_Type.get(), 160 fFloat4x4_Type.get(), fHalf2x2_Type.get(), 161 fHalf2x3_Type.get(), fHalf2x4_Type.get(), 162 fHalf3x2_Type.get(), fHalf3x3_Type.get(), 163 fHalf3x4_Type.get(), fHalf4x2_Type.get(), 164 fHalf4x3_Type.get(), fHalf4x4_Type.get() })) 165 , fVec_Type(new Type("$vec", { fInvalid_Type.get(), fFloat2_Type.get(), 166 fFloat3_Type.get(), fFloat4_Type.get() })) 167 , fGVec_Type(new Type("$gvec")) 168 , fGVec2_Type(new Type("$gfloat2")) 169 , fGVec3_Type(new Type("$gfloat3")) 170 , fGVec4_Type(new Type("$gfloat4", static_type(*fFloat4_Type))) 171 , fHVec_Type(new Type("$hvec", { fInvalid_Type.get(), fHalf2_Type.get(), 172 fHalf3_Type.get(), fHalf4_Type.get() })) 173 , fIVec_Type(new Type("$ivec", { fInvalid_Type.get(), fInt2_Type.get(), 174 fInt3_Type.get(), fInt4_Type.get() })) 175 , fUVec_Type(new Type("$uvec", { fInvalid_Type.get(), fUInt2_Type.get(), 176 fUInt3_Type.get(), fUInt4_Type.get() })) 177 , fSVec_Type(new Type("$svec", { fInvalid_Type.get(), fShort2_Type.get(), 178 fShort3_Type.get(), fShort4_Type.get() })) 179 , fUSVec_Type(new Type("$usvec", { fInvalid_Type.get(), fUShort2_Type.get(), 180 fUShort3_Type.get(), fUShort4_Type.get() })) 181 , fByteVec_Type(new Type("$bytevec", { fInvalid_Type.get(), fByte2_Type.get(), 182 fByte3_Type.get(), fByte4_Type.get() })) 183 , fUByteVec_Type(new Type("$ubytevec", { fInvalid_Type.get(), fUByte2_Type.get(), 184 fUByte3_Type.get(), fUByte4_Type.get() })) 185 , fBVec_Type(new Type("$bvec", { fInvalid_Type.get(), fBool2_Type.get(), 186 fBool3_Type.get(), fBool4_Type.get() })) 187 , fSkCaps_Type(new Type("$sk_Caps")) 188 , fFragmentProcessor_Type(fp_type(fInt_Type.get(), fBool_Type.get())) 189 , fDefined_Expression(new Defined(fInvalid_Type.get())) {} 190 static_type(const Type & t)191 static std::vector<const Type*> static_type(const Type& t) { 192 return { &t, &t, &t, &t }; 193 } 194 195 const std::unique_ptr<Type> fInvalid_Type; 196 const std::unique_ptr<Type> fVoid_Type; 197 const std::unique_ptr<Type> fNull_Type; 198 const std::unique_ptr<Type> fFloatLiteral_Type; 199 const std::unique_ptr<Type> fIntLiteral_Type; 200 201 const std::unique_ptr<Type> fFloat_Type; 202 const std::unique_ptr<Type> fFloat2_Type; 203 const std::unique_ptr<Type> fFloat3_Type; 204 const std::unique_ptr<Type> fFloat4_Type; 205 206 const std::unique_ptr<Type> fHalf_Type; 207 const std::unique_ptr<Type> fHalf2_Type; 208 const std::unique_ptr<Type> fHalf3_Type; 209 const std::unique_ptr<Type> fHalf4_Type; 210 211 const std::unique_ptr<Type> fUInt_Type; 212 const std::unique_ptr<Type> fUInt2_Type; 213 const std::unique_ptr<Type> fUInt3_Type; 214 const std::unique_ptr<Type> fUInt4_Type; 215 216 const std::unique_ptr<Type> fInt_Type; 217 const std::unique_ptr<Type> fInt2_Type; 218 const std::unique_ptr<Type> fInt3_Type; 219 const std::unique_ptr<Type> fInt4_Type; 220 221 const std::unique_ptr<Type> fUShort_Type; 222 const std::unique_ptr<Type> fUShort2_Type; 223 const std::unique_ptr<Type> fUShort3_Type; 224 const std::unique_ptr<Type> fUShort4_Type; 225 226 const std::unique_ptr<Type> fShort_Type; 227 const std::unique_ptr<Type> fShort2_Type; 228 const std::unique_ptr<Type> fShort3_Type; 229 const std::unique_ptr<Type> fShort4_Type; 230 231 const std::unique_ptr<Type> fUByte_Type; 232 const std::unique_ptr<Type> fUByte2_Type; 233 const std::unique_ptr<Type> fUByte3_Type; 234 const std::unique_ptr<Type> fUByte4_Type; 235 236 const std::unique_ptr<Type> fByte_Type; 237 const std::unique_ptr<Type> fByte2_Type; 238 const std::unique_ptr<Type> fByte3_Type; 239 const std::unique_ptr<Type> fByte4_Type; 240 241 const std::unique_ptr<Type> fBool_Type; 242 const std::unique_ptr<Type> fBool2_Type; 243 const std::unique_ptr<Type> fBool3_Type; 244 const std::unique_ptr<Type> fBool4_Type; 245 246 const std::unique_ptr<Type> fFloat2x2_Type; 247 const std::unique_ptr<Type> fFloat2x3_Type; 248 const std::unique_ptr<Type> fFloat2x4_Type; 249 const std::unique_ptr<Type> fFloat3x2_Type; 250 const std::unique_ptr<Type> fFloat3x3_Type; 251 const std::unique_ptr<Type> fFloat3x4_Type; 252 const std::unique_ptr<Type> fFloat4x2_Type; 253 const std::unique_ptr<Type> fFloat4x3_Type; 254 const std::unique_ptr<Type> fFloat4x4_Type; 255 256 const std::unique_ptr<Type> fHalf2x2_Type; 257 const std::unique_ptr<Type> fHalf2x3_Type; 258 const std::unique_ptr<Type> fHalf2x4_Type; 259 const std::unique_ptr<Type> fHalf3x2_Type; 260 const std::unique_ptr<Type> fHalf3x3_Type; 261 const std::unique_ptr<Type> fHalf3x4_Type; 262 const std::unique_ptr<Type> fHalf4x2_Type; 263 const std::unique_ptr<Type> fHalf4x3_Type; 264 const std::unique_ptr<Type> fHalf4x4_Type; 265 266 const std::unique_ptr<Type> fTexture1D_Type; 267 const std::unique_ptr<Type> fTexture2D_Type; 268 const std::unique_ptr<Type> fTexture3D_Type; 269 const std::unique_ptr<Type> fTextureExternalOES_Type; 270 const std::unique_ptr<Type> fTextureCube_Type; 271 const std::unique_ptr<Type> fTexture2DRect_Type; 272 const std::unique_ptr<Type> fTextureBuffer_Type; 273 const std::unique_ptr<Type> fITexture2D_Type; 274 275 const std::unique_ptr<Type> fSampler1D_Type; 276 const std::unique_ptr<Type> fSampler2D_Type; 277 const std::unique_ptr<Type> fSampler3D_Type; 278 const std::unique_ptr<Type> fSamplerExternalOES_Type; 279 const std::unique_ptr<Type> fSamplerCube_Type; 280 const std::unique_ptr<Type> fSampler2DRect_Type; 281 const std::unique_ptr<Type> fSampler1DArray_Type; 282 const std::unique_ptr<Type> fSampler2DArray_Type; 283 const std::unique_ptr<Type> fSamplerCubeArray_Type; 284 const std::unique_ptr<Type> fSamplerBuffer_Type; 285 const std::unique_ptr<Type> fSampler2DMS_Type; 286 const std::unique_ptr<Type> fSampler2DMSArray_Type; 287 const std::unique_ptr<Type> fSampler1DShadow_Type; 288 const std::unique_ptr<Type> fSampler2DShadow_Type; 289 const std::unique_ptr<Type> fSamplerCubeShadow_Type; 290 const std::unique_ptr<Type> fSampler2DRectShadow_Type; 291 const std::unique_ptr<Type> fSampler1DArrayShadow_Type; 292 const std::unique_ptr<Type> fSampler2DArrayShadow_Type; 293 const std::unique_ptr<Type> fSamplerCubeArrayShadow_Type; 294 295 const std::unique_ptr<Type> fISampler2D_Type; 296 const std::unique_ptr<Type> fSampler_Type; 297 298 const std::unique_ptr<Type> fImage2D_Type; 299 const std::unique_ptr<Type> fIImage2D_Type; 300 301 const std::unique_ptr<Type> fSubpassInput_Type; 302 const std::unique_ptr<Type> fSubpassInputMS_Type; 303 304 const std::unique_ptr<Type> fGSampler1D_Type; 305 const std::unique_ptr<Type> fGSampler2D_Type; 306 const std::unique_ptr<Type> fGSampler3D_Type; 307 const std::unique_ptr<Type> fGSamplerCube_Type; 308 const std::unique_ptr<Type> fGSampler2DRect_Type; 309 const std::unique_ptr<Type> fGSampler1DArray_Type; 310 const std::unique_ptr<Type> fGSampler2DArray_Type; 311 const std::unique_ptr<Type> fGSamplerCubeArray_Type; 312 const std::unique_ptr<Type> fGSamplerBuffer_Type; 313 const std::unique_ptr<Type> fGSampler2DMS_Type; 314 const std::unique_ptr<Type> fGSampler2DMSArray_Type; 315 const std::unique_ptr<Type> fGSampler2DArrayShadow_Type; 316 const std::unique_ptr<Type> fGSamplerCubeArrayShadow_Type; 317 318 const std::unique_ptr<Type> fGenType_Type; 319 const std::unique_ptr<Type> fGenHType_Type; 320 const std::unique_ptr<Type> fGenIType_Type; 321 const std::unique_ptr<Type> fGenUType_Type; 322 const std::unique_ptr<Type> fGenBType_Type; 323 324 const std::unique_ptr<Type> fMat_Type; 325 326 const std::unique_ptr<Type> fVec_Type; 327 328 const std::unique_ptr<Type> fGVec_Type; 329 const std::unique_ptr<Type> fGVec2_Type; 330 const std::unique_ptr<Type> fGVec3_Type; 331 const std::unique_ptr<Type> fGVec4_Type; 332 const std::unique_ptr<Type> fHVec_Type; 333 const std::unique_ptr<Type> fDVec_Type; 334 const std::unique_ptr<Type> fIVec_Type; 335 const std::unique_ptr<Type> fUVec_Type; 336 const std::unique_ptr<Type> fSVec_Type; 337 const std::unique_ptr<Type> fUSVec_Type; 338 const std::unique_ptr<Type> fByteVec_Type; 339 const std::unique_ptr<Type> fUByteVec_Type; 340 341 const std::unique_ptr<Type> fBVec_Type; 342 343 const std::unique_ptr<Type> fSkCaps_Type; 344 const std::unique_ptr<Type> fFragmentProcessor_Type; 345 346 // sentinel expression used to mark that a variable has a value during dataflow analysis (when 347 // it could have several different values, or the analyzer is otherwise unable to assign it a 348 // specific expression) 349 const std::unique_ptr<Expression> fDefined_Expression; 350 351 private: 352 class Defined final : public Expression { 353 public: 354 static constexpr Kind kExpressionKind = Kind::kDefined; 355 Defined(const Type * type)356 Defined(const Type* type) 357 : INHERITED(-1, kExpressionKind, type) {} 358 hasProperty(Property property)359 bool hasProperty(Property property) const override { 360 return false; 361 } 362 description()363 String description() const override { 364 return "<defined>"; 365 } 366 clone()367 std::unique_ptr<Expression> clone() const override { 368 return std::unique_ptr<Expression>(new Defined(&this->type())); 369 } 370 371 using INHERITED = Expression; 372 }; 373 fp_type(const Type * intType,const Type * boolType)374 static std::unique_ptr<Type> fp_type(const Type* intType, const Type* boolType) { 375 // Build fields for FragmentProcessors, which should parallel the 376 // C++ API for GrFragmentProcessor. 377 Modifiers mods(Layout(), Modifiers::kConst_Flag); 378 std::vector<Type::Field> fields = { 379 Type::Field(mods, "numTextureSamplers", intType), 380 Type::Field(mods, "numChildProcessors", intType), 381 Type::Field(mods, "usesLocalCoords", boolType), 382 Type::Field(mods, "compatibleWithCoverageAsAlpha", boolType), 383 Type::Field(mods, "preservesOpaqueInput", boolType), 384 Type::Field(mods, "hasConstantOutputForConstantInput", boolType) 385 }; 386 return std::make_unique<Type>("fragmentProcessor", fields); 387 } 388 }; 389 390 } // namespace SkSL 391 392 #endif 393