OLD | NEW |
(Empty) | |
| 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 #include "SkSLSPIRVCodeGenerator.h" |
| 9 |
| 10 #include "string.h" |
| 11 |
| 12 #include "GLSL.std.450.h" |
| 13 |
| 14 #include "ir/SkSLExpressionStatement.h" |
| 15 #include "ir/SkSLExtension.h" |
| 16 #include "ir/SkSLIndexExpression.h" |
| 17 #include "ir/SkSLVariableReference.h" |
| 18 |
| 19 namespace SkSL { |
| 20 |
| 21 #define DEBUG 0 |
| 22 |
| 23 static const int32_t SKSL_MAGIC = 0x0; // FIXME: need to register a magic numbe
r |
| 24 |
| 25 void SPIRVCodeGenerator::setupIntrinsics() { |
| 26 #define ALL_GLSL(x) std::make_tuple(kGLSL_STD_450_IntrinsicKind, GLSLstd450 ## x
, GLSLstd450 ## x, \ |
| 27 GLSLstd450 ## x, GLSLstd450 ## x) |
| 28 #define BY_TYPE_GLSL(ifFloat, ifInt, ifUInt) std::make_tuple(kGLSL_STD_450_Intri
nsicKind, \ |
| 29 GLSLstd450 ## ifFlo
at, \ |
| 30 GLSLstd450 ## ifInt
, \ |
| 31 GLSLstd450 ## ifUIn
t, \ |
| 32 SpvOpUndef) |
| 33 #define SPECIAL(x) std::make_tuple(kSpecial_IntrinsicKind, k ## x ## _SpecialInt
rinsic, \ |
| 34 k ## x ## _SpecialIntrinsic, k ## x ## _Speci
alIntrinsic, \ |
| 35 k ## x ## _SpecialIntrinsic) |
| 36 fIntrinsicMap["round"] = ALL_GLSL(Round); |
| 37 fIntrinsicMap["roundEven"] = ALL_GLSL(RoundEven); |
| 38 fIntrinsicMap["trunc"] = ALL_GLSL(Trunc); |
| 39 fIntrinsicMap["abs"] = BY_TYPE_GLSL(FAbs, SAbs, SAbs); |
| 40 fIntrinsicMap["sign"] = BY_TYPE_GLSL(FSign, SSign, SSign); |
| 41 fIntrinsicMap["floor"] = ALL_GLSL(Floor); |
| 42 fIntrinsicMap["ceil"] = ALL_GLSL(Ceil); |
| 43 fIntrinsicMap["fract"] = ALL_GLSL(Fract); |
| 44 fIntrinsicMap["radians"] = ALL_GLSL(Radians); |
| 45 fIntrinsicMap["degrees"] = ALL_GLSL(Degrees); |
| 46 fIntrinsicMap["sin"] = ALL_GLSL(Sin); |
| 47 fIntrinsicMap["cos"] = ALL_GLSL(Cos); |
| 48 fIntrinsicMap["tan"] = ALL_GLSL(Tan); |
| 49 fIntrinsicMap["asin"] = ALL_GLSL(Asin); |
| 50 fIntrinsicMap["acos"] = ALL_GLSL(Acos); |
| 51 fIntrinsicMap["atan"] = SPECIAL(Atan); |
| 52 fIntrinsicMap["sinh"] = ALL_GLSL(Sinh); |
| 53 fIntrinsicMap["cosh"] = ALL_GLSL(Cosh); |
| 54 fIntrinsicMap["tanh"] = ALL_GLSL(Tanh); |
| 55 fIntrinsicMap["asinh"] = ALL_GLSL(Asinh); |
| 56 fIntrinsicMap["acosh"] = ALL_GLSL(Acosh); |
| 57 fIntrinsicMap["atanh"] = ALL_GLSL(Atanh); |
| 58 fIntrinsicMap["pow"] = ALL_GLSL(Pow); |
| 59 fIntrinsicMap["exp"] = ALL_GLSL(Exp); |
| 60 fIntrinsicMap["log"] = ALL_GLSL(Log); |
| 61 fIntrinsicMap["exp2"] = ALL_GLSL(Exp2); |
| 62 fIntrinsicMap["log2"] = ALL_GLSL(Log2); |
| 63 fIntrinsicMap["sqrt"] = ALL_GLSL(Sqrt); |
| 64 fIntrinsicMap["inversesqrt"] = ALL_GLSL(InverseSqrt); |
| 65 fIntrinsicMap["determinant"] = ALL_GLSL(Determinant); |
| 66 fIntrinsicMap["matrixInverse"] = ALL_GLSL(MatrixInverse); |
| 67 fIntrinsicMap["mod"] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOp
FMod, SpvOpSMod, |
| 68 SpvOpUMod, SpvOpUndef); |
| 69 fIntrinsicMap["min"] = BY_TYPE_GLSL(FMin, SMin, UMin); |
| 70 fIntrinsicMap["max"] = BY_TYPE_GLSL(FMax, SMax, UMax); |
| 71 fIntrinsicMap["clamp"] = BY_TYPE_GLSL(FClamp, SClamp, UClamp); |
| 72 fIntrinsicMap["dot"] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOp
Dot, SpvOpUndef, |
| 73 SpvOpUndef, SpvOpUndef); |
| 74 fIntrinsicMap["mix"] = ALL_GLSL(FMix); |
| 75 fIntrinsicMap["step"] = ALL_GLSL(Step); |
| 76 fIntrinsicMap["smoothstep"] = ALL_GLSL(SmoothStep); |
| 77 fIntrinsicMap["fma"] = ALL_GLSL(Fma); |
| 78 fIntrinsicMap["frexp"] = ALL_GLSL(Frexp); |
| 79 fIntrinsicMap["ldexp"] = ALL_GLSL(Ldexp); |
| 80 |
| 81 #define PACK(type) fIntrinsicMap["pack" #type] = ALL_GLSL(Pack ## type); \ |
| 82 fIntrinsicMap["unpack" #type] = ALL_GLSL(Unpack ## type) |
| 83 PACK(Snorm4x8); |
| 84 PACK(Unorm4x8); |
| 85 PACK(Snorm2x16); |
| 86 PACK(Unorm2x16); |
| 87 PACK(Half2x16); |
| 88 PACK(Double2x32); |
| 89 fIntrinsicMap["length"] = ALL_GLSL(Length); |
| 90 fIntrinsicMap["distance"] = ALL_GLSL(Distance); |
| 91 fIntrinsicMap["cross"] = ALL_GLSL(Cross); |
| 92 fIntrinsicMap["normalize"] = ALL_GLSL(Normalize); |
| 93 fIntrinsicMap["faceForward"] = ALL_GLSL(FaceForward); |
| 94 fIntrinsicMap["reflect"] = ALL_GLSL(Reflect); |
| 95 fIntrinsicMap["refract"] = ALL_GLSL(Refract); |
| 96 fIntrinsicMap["findLSB"] = ALL_GLSL(FindILsb); |
| 97 fIntrinsicMap["findMSB"] = BY_TYPE_GLSL(FindSMsb, FindSMsb, FindUMsb); |
| 98 fIntrinsicMap["dFdx"] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpDP
dx, SpvOpUndef, |
| 99 SpvOpUndef, SpvOpUndef); |
| 100 fIntrinsicMap["dFdy"] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpDP
dy, SpvOpUndef, |
| 101 SpvOpUndef, SpvOpUndef); |
| 102 fIntrinsicMap["dFdy"] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpDP
dy, SpvOpUndef, |
| 103 SpvOpUndef, SpvOpUndef); |
| 104 fIntrinsicMap["texture"] = SPECIAL(Texture); |
| 105 fIntrinsicMap["texture2D"] = SPECIAL(Texture2D); |
| 106 fIntrinsicMap["textureProj"] = SPECIAL(TextureProj); |
| 107 |
| 108 fIntrinsicMap["any"] = std::make_tuple(kSPIRV_IntrinsicKind, Sp
vOpUndef, |
| 109 SpvOpUndef, SpvOpUndef,
SpvOpAny); |
| 110 fIntrinsicMap["all"] = std::make_tuple(kSPIRV_IntrinsicKind, Sp
vOpUndef, |
| 111 SpvOpUndef, SpvOpUndef,
SpvOpAll); |
| 112 fIntrinsicMap["equal"] = std::make_tuple(kSPIRV_IntrinsicKind, Sp
vOpFOrdEqual, |
| 113 SpvOpIEqual, SpvOpIEqual
, |
| 114 SpvOpLogicalEqual); |
| 115 fIntrinsicMap["notEqual"] = std::make_tuple(kSPIRV_IntrinsicKind, Sp
vOpFOrdNotEqual, |
| 116 SpvOpINotEqual, SpvOpINo
tEqual, |
| 117 SpvOpLogicalNotEqual); |
| 118 fIntrinsicMap["lessThan"] = std::make_tuple(kSPIRV_IntrinsicKind, Sp
vOpSLessThan, |
| 119 SpvOpULessThan, SpvOpFOr
dLessThan, |
| 120 SpvOpUndef); |
| 121 fIntrinsicMap["lessThanEqual"] = std::make_tuple(kSPIRV_IntrinsicKind, Sp
vOpSLessThanEqual, |
| 122 SpvOpULessThanEqual, Spv
OpFOrdLessThanEqual, |
| 123 SpvOpUndef); |
| 124 fIntrinsicMap["greaterThan"] = std::make_tuple(kSPIRV_IntrinsicKind, Sp
vOpSGreaterThan, |
| 125 SpvOpUGreaterThan, SpvOp
FOrdGreaterThan, |
| 126 SpvOpUndef); |
| 127 fIntrinsicMap["greaterThanEqual"] = std::make_tuple(kSPIRV_IntrinsicKind, |
| 128 SpvOpSGreaterThanEqual, |
| 129 SpvOpUGreaterThanEqual, |
| 130 SpvOpFOrdGreaterThanEqua
l, |
| 131 SpvOpUndef); |
| 132 |
| 133 // interpolateAt* not yet supported... |
| 134 } |
| 135 |
| 136 void SPIRVCodeGenerator::writeWord(int32_t word, std::ostream& out) { |
| 137 #if DEBUG |
| 138 out << "(" << word << ") "; |
| 139 #else |
| 140 out.write((const char*) &word, sizeof(word)); |
| 141 #endif |
| 142 } |
| 143 |
| 144 static bool is_float(std::shared_ptr<Type> type) { |
| 145 if (type->kind() == Type::kVector_Kind) { |
| 146 return is_float(type->componentType()); |
| 147 } |
| 148 return type == kFloat_Type || type == kDouble_Type; |
| 149 } |
| 150 |
| 151 static bool is_signed(std::shared_ptr<Type> type) { |
| 152 if (type->kind() == Type::kVector_Kind) { |
| 153 return is_signed(type->componentType()); |
| 154 } |
| 155 return type == kInt_Type; |
| 156 } |
| 157 |
| 158 static bool is_unsigned(std::shared_ptr<Type> type) { |
| 159 if (type->kind() == Type::kVector_Kind) { |
| 160 return is_unsigned(type->componentType()); |
| 161 } |
| 162 return type == kUInt_Type; |
| 163 } |
| 164 |
| 165 static bool is_bool(std::shared_ptr<Type> type) { |
| 166 if (type->kind() == Type::kVector_Kind) { |
| 167 return is_bool(type->componentType()); |
| 168 } |
| 169 return type == kBool_Type; |
| 170 } |
| 171 |
| 172 static bool is_out(std::shared_ptr<Variable> var) { |
| 173 return (var->fModifiers.fBits & Modifiers::kOut_Bit) != 0; |
| 174 } |
| 175 |
| 176 static std::string opcode_text(SpvOp_ opCode) { |
| 177 switch (opCode) { |
| 178 case SpvOpNop: |
| 179 return "Nop"; |
| 180 case SpvOpUndef: |
| 181 return "Undef"; |
| 182 case SpvOpSourceContinued: |
| 183 return "SourceContinued"; |
| 184 case SpvOpSource: |
| 185 return "Source"; |
| 186 case SpvOpSourceExtension: |
| 187 return "SourceExtension"; |
| 188 case SpvOpName: |
| 189 return "Name"; |
| 190 case SpvOpMemberName: |
| 191 return "MemberName"; |
| 192 case SpvOpString: |
| 193 return "String"; |
| 194 case SpvOpLine: |
| 195 return "Line"; |
| 196 case SpvOpExtension: |
| 197 return "Extension"; |
| 198 case SpvOpExtInstImport: |
| 199 return "ExtInstImport"; |
| 200 case SpvOpExtInst: |
| 201 return "ExtInst"; |
| 202 case SpvOpMemoryModel: |
| 203 return "MemoryModel"; |
| 204 case SpvOpEntryPoint: |
| 205 return "EntryPoint"; |
| 206 case SpvOpExecutionMode: |
| 207 return "ExecutionMode"; |
| 208 case SpvOpCapability: |
| 209 return "Capability"; |
| 210 case SpvOpTypeVoid: |
| 211 return "TypeVoid"; |
| 212 case SpvOpTypeBool: |
| 213 return "TypeBool"; |
| 214 case SpvOpTypeInt: |
| 215 return "TypeInt"; |
| 216 case SpvOpTypeFloat: |
| 217 return "TypeFloat"; |
| 218 case SpvOpTypeVector: |
| 219 return "TypeVector"; |
| 220 case SpvOpTypeMatrix: |
| 221 return "TypeMatrix"; |
| 222 case SpvOpTypeImage: |
| 223 return "TypeImage"; |
| 224 case SpvOpTypeSampler: |
| 225 return "TypeSampler"; |
| 226 case SpvOpTypeSampledImage: |
| 227 return "TypeSampledImage"; |
| 228 case SpvOpTypeArray: |
| 229 return "TypeArray"; |
| 230 case SpvOpTypeRuntimeArray: |
| 231 return "TypeRuntimeArray"; |
| 232 case SpvOpTypeStruct: |
| 233 return "TypeStruct"; |
| 234 case SpvOpTypeOpaque: |
| 235 return "TypeOpaque"; |
| 236 case SpvOpTypePointer: |
| 237 return "TypePointer"; |
| 238 case SpvOpTypeFunction: |
| 239 return "TypeFunction"; |
| 240 case SpvOpTypeEvent: |
| 241 return "TypeEvent"; |
| 242 case SpvOpTypeDeviceEvent: |
| 243 return "TypeDeviceEvent"; |
| 244 case SpvOpTypeReserveId: |
| 245 return "TypeReserveId"; |
| 246 case SpvOpTypeQueue: |
| 247 return "TypeQueue"; |
| 248 case SpvOpTypePipe: |
| 249 return "TypePipe"; |
| 250 case SpvOpTypeForwardPointer: |
| 251 return "TypeForwardPointer"; |
| 252 case SpvOpConstantTrue: |
| 253 return "ConstantTrue"; |
| 254 case SpvOpConstantFalse: |
| 255 return "ConstantFalse"; |
| 256 case SpvOpConstant: |
| 257 return "Constant"; |
| 258 case SpvOpConstantComposite: |
| 259 return "ConstantComposite"; |
| 260 case SpvOpConstantSampler: |
| 261 return "ConstantSampler"; |
| 262 case SpvOpConstantNull: |
| 263 return "ConstantNull"; |
| 264 case SpvOpSpecConstantTrue: |
| 265 return "SpecConstantTrue"; |
| 266 case SpvOpSpecConstantFalse: |
| 267 return "SpecConstantFalse"; |
| 268 case SpvOpSpecConstant: |
| 269 return "SpecConstant"; |
| 270 case SpvOpSpecConstantComposite: |
| 271 return "SpecConstantComposite"; |
| 272 case SpvOpSpecConstantOp: |
| 273 return "SpecConstantOp"; |
| 274 case SpvOpFunction: |
| 275 return "Function"; |
| 276 case SpvOpFunctionParameter: |
| 277 return "FunctionParameter"; |
| 278 case SpvOpFunctionEnd: |
| 279 return "FunctionEnd"; |
| 280 case SpvOpFunctionCall: |
| 281 return "FunctionCall"; |
| 282 case SpvOpVariable: |
| 283 return "Variable"; |
| 284 case SpvOpImageTexelPointer: |
| 285 return "ImageTexelPointer"; |
| 286 case SpvOpLoad: |
| 287 return "Load"; |
| 288 case SpvOpStore: |
| 289 return "Store"; |
| 290 case SpvOpCopyMemory: |
| 291 return "CopyMemory"; |
| 292 case SpvOpCopyMemorySized: |
| 293 return "CopyMemorySized"; |
| 294 case SpvOpAccessChain: |
| 295 return "AccessChain"; |
| 296 case SpvOpInBoundsAccessChain: |
| 297 return "InBoundsAccessChain"; |
| 298 case SpvOpPtrAccessChain: |
| 299 return "PtrAccessChain"; |
| 300 case SpvOpArrayLength: |
| 301 return "ArrayLength"; |
| 302 case SpvOpGenericPtrMemSemantics: |
| 303 return "GenericPtrMemSemantics"; |
| 304 case SpvOpInBoundsPtrAccessChain: |
| 305 return "InBoundsPtrAccessChain"; |
| 306 case SpvOpDecorate: |
| 307 return "Decorate"; |
| 308 case SpvOpMemberDecorate: |
| 309 return "MemberDecorate"; |
| 310 case SpvOpDecorationGroup: |
| 311 return "DecorationGroup"; |
| 312 case SpvOpGroupDecorate: |
| 313 return "GroupDecorate"; |
| 314 case SpvOpGroupMemberDecorate: |
| 315 return "GroupMemberDecorate"; |
| 316 case SpvOpVectorExtractDynamic: |
| 317 return "VectorExtractDynamic"; |
| 318 case SpvOpVectorInsertDynamic: |
| 319 return "VectorInsertDynamic"; |
| 320 case SpvOpVectorShuffle: |
| 321 return "VectorShuffle"; |
| 322 case SpvOpCompositeConstruct: |
| 323 return "CompositeConstruct"; |
| 324 case SpvOpCompositeExtract: |
| 325 return "CompositeExtract"; |
| 326 case SpvOpCompositeInsert: |
| 327 return "CompositeInsert"; |
| 328 case SpvOpCopyObject: |
| 329 return "CopyObject"; |
| 330 case SpvOpTranspose: |
| 331 return "Transpose"; |
| 332 case SpvOpSampledImage: |
| 333 return "SampledImage"; |
| 334 case SpvOpImageSampleImplicitLod: |
| 335 return "ImageSampleImplicitLod"; |
| 336 case SpvOpImageSampleExplicitLod: |
| 337 return "ImageSampleExplicitLod"; |
| 338 case SpvOpImageSampleDrefImplicitLod: |
| 339 return "ImageSampleDrefImplicitLod"; |
| 340 case SpvOpImageSampleDrefExplicitLod: |
| 341 return "ImageSampleDrefExplicitLod"; |
| 342 case SpvOpImageSampleProjImplicitLod: |
| 343 return "ImageSampleProjImplicitLod"; |
| 344 case SpvOpImageSampleProjExplicitLod: |
| 345 return "ImageSampleProjExplicitLod"; |
| 346 case SpvOpImageSampleProjDrefImplicitLod: |
| 347 return "ImageSampleProjDrefImplicitLod"; |
| 348 case SpvOpImageSampleProjDrefExplicitLod: |
| 349 return "ImageSampleProjDrefExplicitLod"; |
| 350 case SpvOpImageFetch: |
| 351 return "ImageFetch"; |
| 352 case SpvOpImageGather: |
| 353 return "ImageGather"; |
| 354 case SpvOpImageDrefGather: |
| 355 return "ImageDrefGather"; |
| 356 case SpvOpImageRead: |
| 357 return "ImageRead"; |
| 358 case SpvOpImageWrite: |
| 359 return "ImageWrite"; |
| 360 case SpvOpImage: |
| 361 return "Image"; |
| 362 case SpvOpImageQueryFormat: |
| 363 return "ImageQueryFormat"; |
| 364 case SpvOpImageQueryOrder: |
| 365 return "ImageQueryOrder"; |
| 366 case SpvOpImageQuerySizeLod: |
| 367 return "ImageQuerySizeLod"; |
| 368 case SpvOpImageQuerySize: |
| 369 return "ImageQuerySize"; |
| 370 case SpvOpImageQueryLod: |
| 371 return "ImageQueryLod"; |
| 372 case SpvOpImageQueryLevels: |
| 373 return "ImageQueryLevels"; |
| 374 case SpvOpImageQuerySamples: |
| 375 return "ImageQuerySamples"; |
| 376 case SpvOpConvertFToU: |
| 377 return "ConvertFToU"; |
| 378 case SpvOpConvertFToS: |
| 379 return "ConvertFToS"; |
| 380 case SpvOpConvertSToF: |
| 381 return "ConvertSToF"; |
| 382 case SpvOpConvertUToF: |
| 383 return "ConvertUToF"; |
| 384 case SpvOpUConvert: |
| 385 return "UConvert"; |
| 386 case SpvOpSConvert: |
| 387 return "SConvert"; |
| 388 case SpvOpFConvert: |
| 389 return "FConvert"; |
| 390 case SpvOpQuantizeToF16: |
| 391 return "QuantizeToF16"; |
| 392 case SpvOpConvertPtrToU: |
| 393 return "ConvertPtrToU"; |
| 394 case SpvOpSatConvertSToU: |
| 395 return "SatConvertSToU"; |
| 396 case SpvOpSatConvertUToS: |
| 397 return "SatConvertUToS"; |
| 398 case SpvOpConvertUToPtr: |
| 399 return "ConvertUToPtr"; |
| 400 case SpvOpPtrCastToGeneric: |
| 401 return "PtrCastToGeneric"; |
| 402 case SpvOpGenericCastToPtr: |
| 403 return "GenericCastToPtr"; |
| 404 case SpvOpGenericCastToPtrExplicit: |
| 405 return "GenericCastToPtrExplicit"; |
| 406 case SpvOpBitcast: |
| 407 return "Bitcast"; |
| 408 case SpvOpSNegate: |
| 409 return "SNegate"; |
| 410 case SpvOpFNegate: |
| 411 return "FNegate"; |
| 412 case SpvOpIAdd: |
| 413 return "IAdd"; |
| 414 case SpvOpFAdd: |
| 415 return "FAdd"; |
| 416 case SpvOpISub: |
| 417 return "ISub"; |
| 418 case SpvOpFSub: |
| 419 return "FSub"; |
| 420 case SpvOpIMul: |
| 421 return "IMul"; |
| 422 case SpvOpFMul: |
| 423 return "FMul"; |
| 424 case SpvOpUDiv: |
| 425 return "UDiv"; |
| 426 case SpvOpSDiv: |
| 427 return "SDiv"; |
| 428 case SpvOpFDiv: |
| 429 return "FDiv"; |
| 430 case SpvOpUMod: |
| 431 return "UMod"; |
| 432 case SpvOpSRem: |
| 433 return "SRem"; |
| 434 case SpvOpSMod: |
| 435 return "SMod"; |
| 436 case SpvOpFRem: |
| 437 return "FRem"; |
| 438 case SpvOpFMod: |
| 439 return "FMod"; |
| 440 case SpvOpVectorTimesScalar: |
| 441 return "VectorTimesScalar"; |
| 442 case SpvOpMatrixTimesScalar: |
| 443 return "MatrixTimesScalar"; |
| 444 case SpvOpVectorTimesMatrix: |
| 445 return "VectorTimesMatrix"; |
| 446 case SpvOpMatrixTimesVector: |
| 447 return "MatrixTimesVector"; |
| 448 case SpvOpMatrixTimesMatrix: |
| 449 return "MatrixTimesMatrix"; |
| 450 case SpvOpOuterProduct: |
| 451 return "OuterProduct"; |
| 452 case SpvOpDot: |
| 453 return "Dot"; |
| 454 case SpvOpIAddCarry: |
| 455 return "IAddCarry"; |
| 456 case SpvOpISubBorrow: |
| 457 return "ISubBorrow"; |
| 458 case SpvOpUMulExtended: |
| 459 return "UMulExtended"; |
| 460 case SpvOpSMulExtended: |
| 461 return "SMulExtended"; |
| 462 case SpvOpAny: |
| 463 return "Any"; |
| 464 case SpvOpAll: |
| 465 return "All"; |
| 466 case SpvOpIsNan: |
| 467 return "IsNan"; |
| 468 case SpvOpIsInf: |
| 469 return "IsInf"; |
| 470 case SpvOpIsFinite: |
| 471 return "IsFinite"; |
| 472 case SpvOpIsNormal: |
| 473 return "IsNormal"; |
| 474 case SpvOpSignBitSet: |
| 475 return "SignBitSet"; |
| 476 case SpvOpLessOrGreater: |
| 477 return "LessOrGreater"; |
| 478 case SpvOpOrdered: |
| 479 return "Ordered"; |
| 480 case SpvOpUnordered: |
| 481 return "Unordered"; |
| 482 case SpvOpLogicalEqual: |
| 483 return "LogicalEqual"; |
| 484 case SpvOpLogicalNotEqual: |
| 485 return "LogicalNotEqual"; |
| 486 case SpvOpLogicalOr: |
| 487 return "LogicalOr"; |
| 488 case SpvOpLogicalAnd: |
| 489 return "LogicalAnd"; |
| 490 case SpvOpLogicalNot: |
| 491 return "LogicalNot"; |
| 492 case SpvOpSelect: |
| 493 return "Select"; |
| 494 case SpvOpIEqual: |
| 495 return "IEqual"; |
| 496 case SpvOpINotEqual: |
| 497 return "INotEqual"; |
| 498 case SpvOpUGreaterThan: |
| 499 return "UGreaterThan"; |
| 500 case SpvOpSGreaterThan: |
| 501 return "SGreaterThan"; |
| 502 case SpvOpUGreaterThanEqual: |
| 503 return "UGreaterThanEqual"; |
| 504 case SpvOpSGreaterThanEqual: |
| 505 return "SGreaterThanEqual"; |
| 506 case SpvOpULessThan: |
| 507 return "ULessThan"; |
| 508 case SpvOpSLessThan: |
| 509 return "SLessThan"; |
| 510 case SpvOpULessThanEqual: |
| 511 return "ULessThanEqual"; |
| 512 case SpvOpSLessThanEqual: |
| 513 return "SLessThanEqual"; |
| 514 case SpvOpFOrdEqual: |
| 515 return "FOrdEqual"; |
| 516 case SpvOpFUnordEqual: |
| 517 return "FUnordEqual"; |
| 518 case SpvOpFOrdNotEqual: |
| 519 return "FOrdNotEqual"; |
| 520 case SpvOpFUnordNotEqual: |
| 521 return "FUnordNotEqual"; |
| 522 case SpvOpFOrdLessThan: |
| 523 return "FOrdLessThan"; |
| 524 case SpvOpFUnordLessThan: |
| 525 return "FUnordLessThan"; |
| 526 case SpvOpFOrdGreaterThan: |
| 527 return "FOrdGreaterThan"; |
| 528 case SpvOpFUnordGreaterThan: |
| 529 return "FUnordGreaterThan"; |
| 530 case SpvOpFOrdLessThanEqual: |
| 531 return "FOrdLessThanEqual"; |
| 532 case SpvOpFUnordLessThanEqual: |
| 533 return "FUnordLessThanEqual"; |
| 534 case SpvOpFOrdGreaterThanEqual: |
| 535 return "FOrdGreaterThanEqual"; |
| 536 case SpvOpFUnordGreaterThanEqual: |
| 537 return "FUnordGreaterThanEqual"; |
| 538 case SpvOpShiftRightLogical: |
| 539 return "ShiftRightLogical"; |
| 540 case SpvOpShiftRightArithmetic: |
| 541 return "ShiftRightArithmetic"; |
| 542 case SpvOpShiftLeftLogical: |
| 543 return "ShiftLeftLogical"; |
| 544 case SpvOpBitwiseOr: |
| 545 return "BitwiseOr"; |
| 546 case SpvOpBitwiseXor: |
| 547 return "BitwiseXor"; |
| 548 case SpvOpBitwiseAnd: |
| 549 return "BitwiseAnd"; |
| 550 case SpvOpNot: |
| 551 return "Not"; |
| 552 case SpvOpBitFieldInsert: |
| 553 return "BitFieldInsert"; |
| 554 case SpvOpBitFieldSExtract: |
| 555 return "BitFieldSExtract"; |
| 556 case SpvOpBitFieldUExtract: |
| 557 return "BitFieldUExtract"; |
| 558 case SpvOpBitReverse: |
| 559 return "BitReverse"; |
| 560 case SpvOpBitCount: |
| 561 return "BitCount"; |
| 562 case SpvOpDPdx: |
| 563 return "DPdx"; |
| 564 case SpvOpDPdy: |
| 565 return "DPdy"; |
| 566 case SpvOpFwidth: |
| 567 return "Fwidth"; |
| 568 case SpvOpDPdxFine: |
| 569 return "DPdxFine"; |
| 570 case SpvOpDPdyFine: |
| 571 return "DPdyFine"; |
| 572 case SpvOpFwidthFine: |
| 573 return "FwidthFine"; |
| 574 case SpvOpDPdxCoarse: |
| 575 return "DPdxCoarse"; |
| 576 case SpvOpDPdyCoarse: |
| 577 return "DPdyCoarse"; |
| 578 case SpvOpFwidthCoarse: |
| 579 return "FwidthCoarse"; |
| 580 case SpvOpEmitVertex: |
| 581 return "EmitVertex"; |
| 582 case SpvOpEndPrimitive: |
| 583 return "EndPrimitive"; |
| 584 case SpvOpEmitStreamVertex: |
| 585 return "EmitStreamVertex"; |
| 586 case SpvOpEndStreamPrimitive: |
| 587 return "EndStreamPrimitive"; |
| 588 case SpvOpControlBarrier: |
| 589 return "ControlBarrier"; |
| 590 case SpvOpMemoryBarrier: |
| 591 return "MemoryBarrier"; |
| 592 case SpvOpAtomicLoad: |
| 593 return "AtomicLoad"; |
| 594 case SpvOpAtomicStore: |
| 595 return "AtomicStore"; |
| 596 case SpvOpAtomicExchange: |
| 597 return "AtomicExchange"; |
| 598 case SpvOpAtomicCompareExchange: |
| 599 return "AtomicCompareExchange"; |
| 600 case SpvOpAtomicCompareExchangeWeak: |
| 601 return "AtomicCompareExchangeWeak"; |
| 602 case SpvOpAtomicIIncrement: |
| 603 return "AtomicIIncrement"; |
| 604 case SpvOpAtomicIDecrement: |
| 605 return "AtomicIDecrement"; |
| 606 case SpvOpAtomicIAdd: |
| 607 return "AtomicIAdd"; |
| 608 case SpvOpAtomicISub: |
| 609 return "AtomicISub"; |
| 610 case SpvOpAtomicSMin: |
| 611 return "AtomicSMin"; |
| 612 case SpvOpAtomicUMin: |
| 613 return "AtomicUMin"; |
| 614 case SpvOpAtomicSMax: |
| 615 return "AtomicSMax"; |
| 616 case SpvOpAtomicUMax: |
| 617 return "AtomicUMax"; |
| 618 case SpvOpAtomicAnd: |
| 619 return "AtomicAnd"; |
| 620 case SpvOpAtomicOr: |
| 621 return "AtomicOr"; |
| 622 case SpvOpAtomicXor: |
| 623 return "AtomicXor"; |
| 624 case SpvOpPhi: |
| 625 return "Phi"; |
| 626 case SpvOpLoopMerge: |
| 627 return "LoopMerge"; |
| 628 case SpvOpSelectionMerge: |
| 629 return "SelectionMerge"; |
| 630 case SpvOpLabel: |
| 631 return "Label"; |
| 632 case SpvOpBranch: |
| 633 return "Branch"; |
| 634 case SpvOpBranchConditional: |
| 635 return "BranchConditional"; |
| 636 case SpvOpSwitch: |
| 637 return "Switch"; |
| 638 case SpvOpKill: |
| 639 return "Kill"; |
| 640 case SpvOpReturn: |
| 641 return "Return"; |
| 642 case SpvOpReturnValue: |
| 643 return "ReturnValue"; |
| 644 case SpvOpUnreachable: |
| 645 return "Unreachable"; |
| 646 case SpvOpLifetimeStart: |
| 647 return "LifetimeStart"; |
| 648 case SpvOpLifetimeStop: |
| 649 return "LifetimeStop"; |
| 650 case SpvOpGroupAsyncCopy: |
| 651 return "GroupAsyncCopy"; |
| 652 case SpvOpGroupWaitEvents: |
| 653 return "GroupWaitEvents"; |
| 654 case SpvOpGroupAll: |
| 655 return "GroupAll"; |
| 656 case SpvOpGroupAny: |
| 657 return "GroupAny"; |
| 658 case SpvOpGroupBroadcast: |
| 659 return "GroupBroadcast"; |
| 660 case SpvOpGroupIAdd: |
| 661 return "GroupIAdd"; |
| 662 case SpvOpGroupFAdd: |
| 663 return "GroupFAdd"; |
| 664 case SpvOpGroupFMin: |
| 665 return "GroupFMin"; |
| 666 case SpvOpGroupUMin: |
| 667 return "GroupUMin"; |
| 668 case SpvOpGroupSMin: |
| 669 return "GroupSMin"; |
| 670 case SpvOpGroupFMax: |
| 671 return "GroupFMax"; |
| 672 case SpvOpGroupUMax: |
| 673 return "GroupUMax"; |
| 674 case SpvOpGroupSMax: |
| 675 return "GroupSMax"; |
| 676 case SpvOpReadPipe: |
| 677 return "ReadPipe"; |
| 678 case SpvOpWritePipe: |
| 679 return "WritePipe"; |
| 680 case SpvOpReservedReadPipe: |
| 681 return "ReservedReadPipe"; |
| 682 case SpvOpReservedWritePipe: |
| 683 return "ReservedWritePipe"; |
| 684 case SpvOpReserveReadPipePackets: |
| 685 return "ReserveReadPipePackets"; |
| 686 case SpvOpReserveWritePipePackets: |
| 687 return "ReserveWritePipePackets"; |
| 688 case SpvOpCommitReadPipe: |
| 689 return "CommitReadPipe"; |
| 690 case SpvOpCommitWritePipe: |
| 691 return "CommitWritePipe"; |
| 692 case SpvOpIsValidReserveId: |
| 693 return "IsValidReserveId"; |
| 694 case SpvOpGetNumPipePackets: |
| 695 return "GetNumPipePackets"; |
| 696 case SpvOpGetMaxPipePackets: |
| 697 return "GetMaxPipePackets"; |
| 698 case SpvOpGroupReserveReadPipePackets: |
| 699 return "GroupReserveReadPipePackets"; |
| 700 case SpvOpGroupReserveWritePipePackets: |
| 701 return "GroupReserveWritePipePackets"; |
| 702 case SpvOpGroupCommitReadPipe: |
| 703 return "GroupCommitReadPipe"; |
| 704 case SpvOpGroupCommitWritePipe: |
| 705 return "GroupCommitWritePipe"; |
| 706 case SpvOpEnqueueMarker: |
| 707 return "EnqueueMarker"; |
| 708 case SpvOpEnqueueKernel: |
| 709 return "EnqueueKernel"; |
| 710 case SpvOpGetKernelNDrangeSubGroupCount: |
| 711 return "GetKernelNDrangeSubGroupCount"; |
| 712 case SpvOpGetKernelNDrangeMaxSubGroupSize: |
| 713 return "GetKernelNDrangeMaxSubGroupSize"; |
| 714 case SpvOpGetKernelWorkGroupSize: |
| 715 return "GetKernelWorkGroupSize"; |
| 716 case SpvOpGetKernelPreferredWorkGroupSizeMultiple: |
| 717 return "GetKernelPreferredWorkGroupSizeMultiple"; |
| 718 case SpvOpRetainEvent: |
| 719 return "RetainEvent"; |
| 720 case SpvOpReleaseEvent: |
| 721 return "ReleaseEvent"; |
| 722 case SpvOpCreateUserEvent: |
| 723 return "CreateUserEvent"; |
| 724 case SpvOpIsValidEvent: |
| 725 return "IsValidEvent"; |
| 726 case SpvOpSetUserEventStatus: |
| 727 return "SetUserEventStatus"; |
| 728 case SpvOpCaptureEventProfilingInfo: |
| 729 return "CaptureEventProfilingInfo"; |
| 730 case SpvOpGetDefaultQueue: |
| 731 return "GetDefaultQueue"; |
| 732 case SpvOpBuildNDRange: |
| 733 return "BuildNDRange"; |
| 734 case SpvOpImageSparseSampleImplicitLod: |
| 735 return "ImageSparseSampleImplicitLod"; |
| 736 case SpvOpImageSparseSampleExplicitLod: |
| 737 return "ImageSparseSampleExplicitLod"; |
| 738 case SpvOpImageSparseSampleDrefImplicitLod: |
| 739 return "ImageSparseSampleDrefImplicitLod"; |
| 740 case SpvOpImageSparseSampleDrefExplicitLod: |
| 741 return "ImageSparseSampleDrefExplicitLod"; |
| 742 case SpvOpImageSparseSampleProjImplicitLod: |
| 743 return "ImageSparseSampleProjImplicitLod"; |
| 744 case SpvOpImageSparseSampleProjExplicitLod: |
| 745 return "ImageSparseSampleProjExplicitLod"; |
| 746 case SpvOpImageSparseSampleProjDrefImplicitLod: |
| 747 return "ImageSparseSampleProjDrefImplicitLod"; |
| 748 case SpvOpImageSparseSampleProjDrefExplicitLod: |
| 749 return "ImageSparseSampleProjDrefExplicitLod"; |
| 750 case SpvOpImageSparseFetch: |
| 751 return "ImageSparseFetch"; |
| 752 case SpvOpImageSparseGather: |
| 753 return "ImageSparseGather"; |
| 754 case SpvOpImageSparseDrefGather: |
| 755 return "ImageSparseDrefGather"; |
| 756 case SpvOpImageSparseTexelsResident: |
| 757 return "ImageSparseTexelsResident"; |
| 758 case SpvOpNoLine: |
| 759 return "NoLine"; |
| 760 case SpvOpAtomicFlagTestAndSet: |
| 761 return "AtomicFlagTestAndSet"; |
| 762 case SpvOpAtomicFlagClear: |
| 763 return "AtomicFlagClear"; |
| 764 case SpvOpImageSparseRead: |
| 765 return "ImageSparseRead"; |
| 766 default: |
| 767 ABORT("unsupported SPIR-V op"); |
| 768 } |
| 769 } |
| 770 |
| 771 void SPIRVCodeGenerator::writeOpCode(SpvOp_ opCode, int length, std::ostream& ou
t) { |
| 772 ASSERT(opCode != SpvOpLabel && opCode != SpvOpUndef); |
| 773 switch (opCode) { |
| 774 case SpvOpReturn: // fall through |
| 775 case SpvOpReturnValue: // fall through |
| 776 case SpvOpBranch: // fall through |
| 777 case SpvOpBranchConditional: |
| 778 if (!fCurrentBlock) { |
| 779 printf("using %s outside of a block\n", opcode_text(opCode).c_st
r()); |
| 780 ASSERT(false); |
| 781 } |
| 782 fCurrentBlock = 0; |
| 783 break; |
| 784 case SpvOpConstant: // fall through |
| 785 case SpvOpConstantTrue: // fall through |
| 786 case SpvOpConstantFalse: // fall through |
| 787 case SpvOpConstantComposite: // fall through |
| 788 case SpvOpTypeVoid: // fall through |
| 789 case SpvOpTypeInt: // fall through |
| 790 case SpvOpTypeFloat: // fall through |
| 791 case SpvOpTypeBool: // fall through |
| 792 case SpvOpTypeVector: // fall through |
| 793 case SpvOpTypeMatrix: // fall through |
| 794 case SpvOpTypeArray: // fall through |
| 795 case SpvOpTypePointer: // fall through |
| 796 case SpvOpTypeFunction: // fall through |
| 797 case SpvOpTypeRuntimeArray: // fall through |
| 798 case SpvOpTypeStruct: // fall through |
| 799 case SpvOpTypeImage: // fall through |
| 800 case SpvOpTypeSampledImage: // fall through |
| 801 case SpvOpVariable: // fall through |
| 802 case SpvOpFunction: // fall through |
| 803 case SpvOpFunctionParameter: // fall through |
| 804 case SpvOpFunctionEnd: // fall through |
| 805 case SpvOpExecutionMode: // fall through |
| 806 case SpvOpMemoryModel: // fall through |
| 807 case SpvOpCapability: // fall through |
| 808 case SpvOpExtInstImport: // fall through |
| 809 case SpvOpEntryPoint: // fall through |
| 810 case SpvOpSource: // fall through |
| 811 case SpvOpSourceExtension: // fall through |
| 812 case SpvOpName: // fall through |
| 813 case SpvOpMemberName: // fall through |
| 814 case SpvOpDecorate: // fall through |
| 815 case SpvOpMemberDecorate: |
| 816 break; |
| 817 default: |
| 818 if (!fCurrentBlock) { |
| 819 printf("using %s outside of a block\n", opcode_text(opCode).c_st
r()); |
| 820 ASSERT(false); |
| 821 } |
| 822 } |
| 823 #if DEBUG |
| 824 out << std::endl << opcode_text(opCode) << " "; |
| 825 #else |
| 826 this->writeWord((length << 16) | opCode, out); |
| 827 #endif |
| 828 } |
| 829 |
| 830 void SPIRVCodeGenerator::writeLabel(SpvId label, std::ostream& out) { |
| 831 fCurrentBlock = label; |
| 832 #if DEBUG |
| 833 out << std::endl << opcode_text(SpvOpLabel) << " "; |
| 834 #else |
| 835 this->writeWord((2 << 16) | SpvOpLabel, out); |
| 836 #endif |
| 837 this->writeWord(label, out); |
| 838 } |
| 839 |
| 840 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, std::ostream& out) { |
| 841 this->writeOpCode(opCode, 1, out); |
| 842 } |
| 843 |
| 844 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, std::ost
ream& out) { |
| 845 this->writeOpCode(opCode, 2, out); |
| 846 this->writeWord(word1, out); |
| 847 } |
| 848 |
| 849 void SPIRVCodeGenerator::writeString(const char* string, std::ostream& out) { |
| 850 size_t length = strlen(string); |
| 851 out << string; |
| 852 switch (length % 4) { |
| 853 case 1: |
| 854 out << (char) 0; |
| 855 // fall through |
| 856 case 2: |
| 857 out << (char) 0; |
| 858 // fall through |
| 859 case 3: |
| 860 out << (char) 0; |
| 861 break; |
| 862 default: |
| 863 this->writeWord(0, out); |
| 864 } |
| 865 } |
| 866 |
| 867 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, const char* string, std
::ostream& out) { |
| 868 int32_t length = (int32_t) strlen(string); |
| 869 this->writeOpCode(opCode, 1 + (length + 4) / 4, out); |
| 870 this->writeString(string, out); |
| 871 } |
| 872 |
| 873 |
| 874 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, const ch
ar* string, |
| 875 std::ostream& out) { |
| 876 int32_t length = (int32_t) strlen(string); |
| 877 this->writeOpCode(opCode, 2 + (length + 4) / 4, out); |
| 878 this->writeWord(word1, out); |
| 879 this->writeString(string, out); |
| 880 } |
| 881 |
| 882 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t
word2, |
| 883 const char* string, std::ostream& out)
{ |
| 884 int32_t length = (int32_t) strlen(string); |
| 885 this->writeOpCode(opCode, 3 + (length + 4) / 4, out); |
| 886 this->writeWord(word1, out); |
| 887 this->writeWord(word2, out); |
| 888 this->writeString(string, out); |
| 889 } |
| 890 |
| 891 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t
word2, |
| 892 std::ostream& out) { |
| 893 this->writeOpCode(opCode, 3, out); |
| 894 this->writeWord(word1, out); |
| 895 this->writeWord(word2, out); |
| 896 } |
| 897 |
| 898 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t
word2, |
| 899 int32_t word3, std::ostream& out) { |
| 900 this->writeOpCode(opCode, 4, out); |
| 901 this->writeWord(word1, out); |
| 902 this->writeWord(word2, out); |
| 903 this->writeWord(word3, out); |
| 904 } |
| 905 |
| 906 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t
word2, |
| 907 int32_t word3, int32_t word4, std::ost
ream& out) { |
| 908 this->writeOpCode(opCode, 5, out); |
| 909 this->writeWord(word1, out); |
| 910 this->writeWord(word2, out); |
| 911 this->writeWord(word3, out); |
| 912 this->writeWord(word4, out); |
| 913 } |
| 914 |
| 915 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t
word2, |
| 916 int32_t word3, int32_t word4, int32_t
word5, |
| 917 std::ostream& out) { |
| 918 this->writeOpCode(opCode, 6, out); |
| 919 this->writeWord(word1, out); |
| 920 this->writeWord(word2, out); |
| 921 this->writeWord(word3, out); |
| 922 this->writeWord(word4, out); |
| 923 this->writeWord(word5, out); |
| 924 } |
| 925 |
| 926 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t
word2, |
| 927 int32_t word3, int32_t word4, int32_t
word5, |
| 928 int32_t word6, std::ostream& out) { |
| 929 this->writeOpCode(opCode, 7, out); |
| 930 this->writeWord(word1, out); |
| 931 this->writeWord(word2, out); |
| 932 this->writeWord(word3, out); |
| 933 this->writeWord(word4, out); |
| 934 this->writeWord(word5, out); |
| 935 this->writeWord(word6, out); |
| 936 } |
| 937 |
| 938 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t
word2, |
| 939 int32_t word3, int32_t word4, int32_t
word5, |
| 940 int32_t word6, int32_t word7, std::ost
ream& out) { |
| 941 this->writeOpCode(opCode, 8, out); |
| 942 this->writeWord(word1, out); |
| 943 this->writeWord(word2, out); |
| 944 this->writeWord(word3, out); |
| 945 this->writeWord(word4, out); |
| 946 this->writeWord(word5, out); |
| 947 this->writeWord(word6, out); |
| 948 this->writeWord(word7, out); |
| 949 } |
| 950 |
| 951 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t
word2, |
| 952 int32_t word3, int32_t word4, int32_t
word5, |
| 953 int32_t word6, int32_t word7, int32_t
word8, |
| 954 std::ostream& out) { |
| 955 this->writeOpCode(opCode, 9, out); |
| 956 this->writeWord(word1, out); |
| 957 this->writeWord(word2, out); |
| 958 this->writeWord(word3, out); |
| 959 this->writeWord(word4, out); |
| 960 this->writeWord(word5, out); |
| 961 this->writeWord(word6, out); |
| 962 this->writeWord(word7, out); |
| 963 this->writeWord(word8, out); |
| 964 } |
| 965 |
| 966 void SPIRVCodeGenerator::writeCapabilities(std::ostream& out) { |
| 967 for (uint64_t i = 0, bit = 1; i <= kLast_Capability; i++, bit <<= 1) { |
| 968 if (fCapabilities & bit) { |
| 969 this->writeInstruction(SpvOpCapability, (SpvId) i, out); |
| 970 } |
| 971 } |
| 972 } |
| 973 |
| 974 SpvId SPIRVCodeGenerator::nextId() { |
| 975 return fIdCount++; |
| 976 } |
| 977 |
| 978 void SPIRVCodeGenerator::writeStruct(std::shared_ptr<Type> type, SpvId resultId)
{ |
| 979 this->writeInstruction(SpvOpName, resultId, type->name().c_str(), fNameBuffe
r); |
| 980 std::vector<SpvId> types; |
| 981 for (auto f : type->fields()) { |
| 982 types.push_back(this->getType(f.fType)); |
| 983 } |
| 984 this->writeOpCode(SpvOpTypeStruct, 2 + (int32_t) types.size(), fConstantBuff
er); |
| 985 this->writeWord(resultId, fConstantBuffer); |
| 986 for (SpvId id : types) { |
| 987 this->writeWord(id, fConstantBuffer); |
| 988 } |
| 989 size_t offset = 0; |
| 990 // SkDebugf(" writing struct %s\n", type->description().c_str()); |
| 991 for (int32_t i = 0; i < (int32_t) type->fields().size(); i++) { |
| 992 size_t size = type->fields()[i].fType->size(); |
| 993 size_t alignment = type->fields()[i].fType->alignment(); |
| 994 size_t mod = offset % alignment; |
| 995 if (mod != 0) { |
| 996 offset += alignment - mod; |
| 997 } |
| 998 // SkDebugf(" field %d (%s): size=%d, alignment=%d, offset=%d\n", |
| 999 // i, type->fields()[i].fType->description().c_str(), size, align
ment, offset); |
| 1000 this->writeInstruction(SpvOpMemberName, resultId, i, type->fields()[i].f
Name.c_str(), |
| 1001 fNameBuffer); |
| 1002 this->writeLayout(type->fields()[i].fModifiers.fLayout, resultId, i); |
| 1003 // FIXME: need to look into how alignment should be handled. |
| 1004 if (type->fields()[i].fModifiers.fLayout.fBuiltin < 0) { |
| 1005 this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorati
onOffset, offset, |
| 1006 fDecorationBuffer); |
| 1007 } |
| 1008 if (type->fields()[i].fType->kind() == Type::kMatrix_Kind) { |
| 1009 this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorati
onColMajor, |
| 1010 fDecorationBuffer); |
| 1011 this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorati
onMatrixStride, |
| 1012 type->fields()[i].fType->stride(), fDecoratio
nBuffer); |
| 1013 } |
| 1014 offset += size; |
| 1015 Type::Kind kind = type->fields()[i].fType->kind(); |
| 1016 if ((kind == Type::kArray_Kind || kind == Type::kStruct_Kind) && offset
% alignment != 0) { |
| 1017 offset += alignment - offset % alignment; |
| 1018 } |
| 1019 ASSERT(offset % alignment == 0); |
| 1020 } |
| 1021 } |
| 1022 |
| 1023 SpvId SPIRVCodeGenerator::getType(std::shared_ptr<Type> type) { |
| 1024 auto entry = fTypeMap.find(type->name()); |
| 1025 if (entry == fTypeMap.end()) { |
| 1026 SpvId result = this->nextId(); |
| 1027 switch (type->kind()) { |
| 1028 case Type::kScalar_Kind: |
| 1029 if (type == kBool_Type) { |
| 1030 this->writeInstruction(SpvOpTypeBool, result, fConstantBuffe
r); |
| 1031 } |
| 1032 else if (type == kInt_Type) { |
| 1033 this->writeInstruction(SpvOpTypeInt, result, 32, 1, fConstan
tBuffer); |
| 1034 } |
| 1035 else if (type == kUInt_Type) { |
| 1036 this->writeInstruction(SpvOpTypeInt, result, 32, 0, fConstan
tBuffer); |
| 1037 } |
| 1038 else if (type == kFloat_Type) { |
| 1039 this->writeInstruction(SpvOpTypeFloat, result, 32, fConstant
Buffer); |
| 1040 } |
| 1041 else if (type == kDouble_Type) { |
| 1042 this->writeInstruction(SpvOpTypeFloat, result, 64, fConstant
Buffer); |
| 1043 } |
| 1044 else { |
| 1045 ASSERT(false); |
| 1046 } |
| 1047 break; |
| 1048 case Type::kVector_Kind: |
| 1049 this->writeInstruction(SpvOpTypeVector, result, |
| 1050 this->getType(type->componentType()), |
| 1051 type->columns(), fConstantBuffer); |
| 1052 break; |
| 1053 case Type::kMatrix_Kind: |
| 1054 this->writeInstruction(SpvOpTypeMatrix, result, this->getType(in
dex_type(type)), |
| 1055 type->columns(), fConstantBuffer); |
| 1056 break; |
| 1057 case Type::kStruct_Kind: |
| 1058 this->writeStruct(type, result); |
| 1059 break; |
| 1060 case Type::kArray_Kind: { |
| 1061 if (type->columns() > 0) { |
| 1062 IntLiteral count(Position(), type->columns()); |
| 1063 this->writeInstruction(SpvOpTypeArray, result, |
| 1064 this->getType(type->componentType()),
|
| 1065 this->writeIntLiteral(count), fConsta
ntBuffer); |
| 1066 this->writeInstruction(SpvOpDecorate, result, SpvDecorationA
rrayStride, |
| 1067 type->stride(), fDecorationBuffer); |
| 1068 } else { |
| 1069 printf("FIXME: creating a runtime-sized array. this won't ac
tually work yet!\n"); |
| 1070 this->writeInstruction(SpvOpTypeRuntimeArray, result, |
| 1071 this->getType(type->componentType()),
fConstantBuffer); |
| 1072 } |
| 1073 break; |
| 1074 } |
| 1075 case Type::kSampler_Kind: { |
| 1076 SpvId image = this->nextId(); |
| 1077 this->writeInstruction(SpvOpTypeImage, image, this->getType(kFlo
at_Type), |
| 1078 type->dimensions(), type->isDepth(), type
->isArrayed(), |
| 1079 type->isMultisampled(), type->isSampled()
, |
| 1080 SpvImageFormatUnknown, fConstantBuffer); |
| 1081 this->writeInstruction(SpvOpTypeSampledImage, result, image, fCo
nstantBuffer); |
| 1082 break; |
| 1083 } |
| 1084 default: |
| 1085 if (type == kVoid_Type) { |
| 1086 this->writeInstruction(SpvOpTypeVoid, result, fConstantBuffe
r); |
| 1087 } else { |
| 1088 printf("invalid type: %s\n", type->description().c_str()); |
| 1089 ASSERT(false); |
| 1090 } |
| 1091 } |
| 1092 fTypeMap[type->name()] = result; |
| 1093 return result; |
| 1094 } |
| 1095 return entry->second; |
| 1096 } |
| 1097 |
| 1098 SpvId SPIRVCodeGenerator::getFunctionType(std::shared_ptr<FunctionDeclaration> f
unction) { |
| 1099 std::string key = function->fReturnType->description() + "("; |
| 1100 std::string separator = ""; |
| 1101 for (size_t i = 0; i < function->fParameters.size(); i++) { |
| 1102 key += separator; |
| 1103 separator = ", "; |
| 1104 key += function->fParameters[i]->fType->description(); |
| 1105 } |
| 1106 key += ")"; |
| 1107 auto entry = fTypeMap.find(key); |
| 1108 if (entry == fTypeMap.end()) { |
| 1109 SpvId result = this->nextId(); |
| 1110 int32_t length = 3 + (int32_t) function->fParameters.size(); |
| 1111 SpvId returnType = this->getType(function->fReturnType); |
| 1112 std::vector<SpvId> parameterTypes; |
| 1113 for (size_t i = 0; i < function->fParameters.size(); i++) { |
| 1114 // glslang seems to treat all function parameters as pointers whethe
r they need to be or |
| 1115 // not. I was initially puzzled by this until I ran bizarre failure
s with certain |
| 1116 // patterns of function calls and control constructs, as exemplified
by this minimal |
| 1117 // failure case: |
| 1118 // |
| 1119 // void sphere(float x) { |
| 1120 // } |
| 1121 // |
| 1122 // void map() { |
| 1123 // sphere(1.0); |
| 1124 // } |
| 1125 // |
| 1126 // void main() { |
| 1127 // for (int i = 0; i < 1; i++) { |
| 1128 // map(); |
| 1129 // } |
| 1130 // } |
| 1131 // |
| 1132 // As of this writing, compiling this in the "obvious" way (with sph
ere taking a float) |
| 1133 // crashes. Making it take a float* and storing the parameter in a t
emporary variable, |
| 1134 // as glslang does, fixes it. |
| 1135 // if (is_out(function->fParameters[i])) { |
| 1136 parameterTypes.push_back(this->getPointerType(function->fParamet
ers[i]->fType, |
| 1137 SpvStorageClassFun
ction)); |
| 1138 // } else { |
| 1139 // parameterTypes.push_back(this->getType(function->fParameters[i
]->fType)); |
| 1140 // } |
| 1141 } |
| 1142 this->writeOpCode(SpvOpTypeFunction, length, fConstantBuffer); |
| 1143 this->writeWord(result, fConstantBuffer); |
| 1144 this->writeWord(returnType, fConstantBuffer); |
| 1145 for (SpvId id : parameterTypes) { |
| 1146 this->writeWord(id, fConstantBuffer); |
| 1147 } |
| 1148 fTypeMap[key] = result; |
| 1149 return result; |
| 1150 } |
| 1151 return entry->second; |
| 1152 } |
| 1153 |
| 1154 SpvId SPIRVCodeGenerator::getPointerType(std::shared_ptr<Type> type, |
| 1155 SpvStorageClass_ storageClass) { |
| 1156 std::string key = type->description() + "*" + to_string(storageClass); |
| 1157 auto entry = fTypeMap.find(key); |
| 1158 if (entry == fTypeMap.end()) { |
| 1159 SpvId result = this->nextId(); |
| 1160 this->writeInstruction(SpvOpTypePointer, result, storageClass, |
| 1161 this->getType(type), fConstantBuffer); |
| 1162 fTypeMap[key] = result; |
| 1163 return result; |
| 1164 } |
| 1165 return entry->second; |
| 1166 } |
| 1167 |
| 1168 SpvId SPIRVCodeGenerator::writeExpression(Expression& expr, std::ostream& out) { |
| 1169 switch (expr.fKind) { |
| 1170 case Expression::kBinary_Kind: |
| 1171 return this->writeBinaryExpression((BinaryExpression&) expr, out); |
| 1172 case Expression::kBoolLiteral_Kind: |
| 1173 return this->writeBoolLiteral((BoolLiteral&) expr); |
| 1174 case Expression::kConstructor_Kind: |
| 1175 return this->writeConstructor((Constructor&) expr, out); |
| 1176 case Expression::kIntLiteral_Kind: |
| 1177 return this->writeIntLiteral((IntLiteral&) expr); |
| 1178 case Expression::kFieldAccess_Kind: |
| 1179 return this->writeFieldAccess(((FieldAccess&) expr), out); |
| 1180 case Expression::kFloatLiteral_Kind: |
| 1181 return this->writeFloatLiteral(((FloatLiteral&) expr)); |
| 1182 case Expression::kFunctionCall_Kind: |
| 1183 return this->writeFunctionCall((FunctionCall&) expr, out); |
| 1184 case Expression::kPrefix_Kind: |
| 1185 return this->writePrefixExpression((PrefixExpression&) expr, out); |
| 1186 case Expression::kPostfix_Kind: |
| 1187 return this->writePostfixExpression((PostfixExpression&) expr, out); |
| 1188 case Expression::kSwizzle_Kind: |
| 1189 return this->writeSwizzle((Swizzle&) expr, out); |
| 1190 case Expression::kVariableReference_Kind: |
| 1191 return this->writeVariableReference((VariableReference&) expr, out); |
| 1192 case Expression::kTernary_Kind: |
| 1193 return this->writeTernaryExpression((TernaryExpression&) expr, out); |
| 1194 case Expression::kIndex_Kind: |
| 1195 return this->writeIndexExpression((IndexExpression&) expr, out); |
| 1196 default: |
| 1197 printf("unsupported expression: %s\n", expr.description().c_str()); |
| 1198 ASSERT(false); |
| 1199 } |
| 1200 return -1; |
| 1201 } |
| 1202 |
| 1203 SpvId SPIRVCodeGenerator::writeIntrinsicCall(FunctionCall& c, std::ostream& out)
{ |
| 1204 auto intrinsic = fIntrinsicMap.find(c.fFunction->fName); |
| 1205 ASSERT(intrinsic != fIntrinsicMap.end()); |
| 1206 SpvId result = this->nextId(); |
| 1207 std::shared_ptr<Type> type = c.fParameters[0]->fType; |
| 1208 int32_t intrinsicId; |
| 1209 if (std::get<0>(intrinsic->second) == kSpecial_IntrinsicKind || is_float(typ
e)) { |
| 1210 intrinsicId = std::get<1>(intrinsic->second); |
| 1211 } else if (is_signed(type)) { |
| 1212 intrinsicId = std::get<2>(intrinsic->second); |
| 1213 } else if (is_unsigned(type)) { |
| 1214 intrinsicId = std::get<3>(intrinsic->second); |
| 1215 } else if (is_bool(type)) { |
| 1216 intrinsicId = std::get<4>(intrinsic->second); |
| 1217 } else { |
| 1218 ABORT("invalid call %s, cannot operate on '%s'\n", c.description().c_str
(), |
| 1219 type->description().c_str()); |
| 1220 } |
| 1221 switch (std::get<0>(intrinsic->second)) { |
| 1222 case kGLSL_STD_450_IntrinsicKind: { |
| 1223 std::vector<SpvId> parameters; |
| 1224 for (size_t i = 0; i < c.fParameters.size(); i++) { |
| 1225 parameters.push_back(this->writeExpression(*c.fParameters[i], ou
t)); |
| 1226 } |
| 1227 this->writeOpCode(SpvOpExtInst, 5 + (int32_t) parameters.size(), out
); |
| 1228 this->writeWord(this->getType(c.fType), out); |
| 1229 this->writeWord(result, out); |
| 1230 this->writeWord(fGLSLExtendedInstructions, out); |
| 1231 this->writeWord(intrinsicId, out); |
| 1232 for (SpvId id : parameters) { |
| 1233 this->writeWord(id, out); |
| 1234 } |
| 1235 return result; |
| 1236 } |
| 1237 case kSPIRV_IntrinsicKind: { |
| 1238 std::vector<SpvId> parameters; |
| 1239 for (size_t i = 0; i < c.fParameters.size(); i++) { |
| 1240 parameters.push_back(this->writeExpression(*c.fParameters[i], ou
t)); |
| 1241 } |
| 1242 this->writeOpCode((SpvOp_) intrinsicId, 3 + (int32_t) parameters.siz
e(), out); |
| 1243 this->writeWord(this->getType(c.fType), out); |
| 1244 this->writeWord(result, out); |
| 1245 for (SpvId id : parameters) { |
| 1246 this->writeWord(id, out); |
| 1247 } |
| 1248 return result; |
| 1249 } |
| 1250 case kSpecial_IntrinsicKind: |
| 1251 return this->writeSpecialIntrinsic(c, (SpecialIntrinsic) intrinsicId
, out); |
| 1252 default: |
| 1253 ABORT("unsupported intrinsic kind"); |
| 1254 } |
| 1255 } |
| 1256 |
| 1257 SpvId SPIRVCodeGenerator::writeSpecialIntrinsic(FunctionCall& c, SpecialIntrinsi
c kind, |
| 1258 std::ostream& out) { |
| 1259 SpvId result = this->nextId(); |
| 1260 switch (kind) { |
| 1261 case kAtan_SpecialIntrinsic: { |
| 1262 SkDebugf("ATAN2: %s\n", c.description().c_str()); |
| 1263 std::vector<SpvId> parameters; |
| 1264 for (size_t i = 0; i < c.fParameters.size(); i++) { |
| 1265 SkDebugf("ATAN2 parameter: %s\n", c.fParameters[i]->description(
).c_str()); |
| 1266 parameters.push_back(this->writeExpression(*c.fParameters[i], ou
t)); |
| 1267 } |
| 1268 this->writeOpCode(SpvOpExtInst, 5 + (int32_t) parameters.size(), out
); |
| 1269 this->writeWord(this->getType(c.fType), out); |
| 1270 this->writeWord(result, out); |
| 1271 this->writeWord(fGLSLExtendedInstructions, out); |
| 1272 this->writeWord(parameters.size() == 2 ? GLSLstd450Atan2 : GLSLstd45
0Atan, out); |
| 1273 for (SpvId id : parameters) { |
| 1274 this->writeWord(id, out); |
| 1275 } |
| 1276 return result; |
| 1277 } |
| 1278 case kTexture_SpecialIntrinsic: { |
| 1279 SpvId type = this->getType(c.fType); |
| 1280 SpvId sampler = this->writeExpression(*c.fParameters[0],
out); |
| 1281 SpvId uv = this->writeExpression(*c.fParameters[1], out)
; |
| 1282 if (c.fParameters.size() == 3) { |
| 1283 this->writeInstruction(SpvOpImageSampleImplicitLod, type, result
, sampler, uv, |
| 1284 SpvImageOperandsBiasMask, |
| 1285 this->writeExpression(*c.fParameters[2],
out), |
| 1286 out); |
| 1287 } else { |
| 1288 ASSERT(c.fParameters.size() == 2); |
| 1289 this->writeInstruction(SpvOpImageSampleImplicitLod, type, result
, sampler, uv, out); |
| 1290 } |
| 1291 break; |
| 1292 } |
| 1293 case kTextureProj_SpecialIntrinsic: { |
| 1294 SpvId type = this->getType(c.fType); |
| 1295 SpvId sampler = this->writeExpression(*c.fParameters[0], out); |
| 1296 SpvId uv = this->writeExpression(*c.fParameters[1], out); |
| 1297 if (c.fParameters.size() == 3) { |
| 1298 this->writeInstruction(SpvOpImageSampleProjImplicitLod, type, re
sult, sampler, uv, |
| 1299 SpvImageOperandsBiasMask, |
| 1300 this->writeExpression(*c.fParameters[2],
out), |
| 1301 out); |
| 1302 } else { |
| 1303 ASSERT(c.fParameters.size() == 2); |
| 1304 this->writeInstruction(SpvOpImageSampleProjImplicitLod, type, re
sult, sampler, uv, |
| 1305 out); |
| 1306 } |
| 1307 break; |
| 1308 } |
| 1309 case kTexture2D_SpecialIntrinsic: |
| 1310 this->writeInstruction(SpvOpImageSampleImplicitLod, |
| 1311 this->getType(c.fType), |
| 1312 result, |
| 1313 this->writeExpression(*c.fParameters[0], out)
, |
| 1314 this->writeExpression(*c.fParameters[1], out)
, |
| 1315 out); |
| 1316 break; |
| 1317 } |
| 1318 return result; |
| 1319 } |
| 1320 |
| 1321 SpvId SPIRVCodeGenerator::writeFunctionCall(FunctionCall& c, std::ostream& out)
{ |
| 1322 auto entry = fFunctionMap.find(c.fFunction); |
| 1323 if (entry == fFunctionMap.end()) { |
| 1324 return this->writeIntrinsicCall(c, out); |
| 1325 } |
| 1326 std::vector<SpvId> parameters; |
| 1327 for (size_t i = 0; i < c.fParameters.size(); i++) { |
| 1328 if (is_out(c.fFunction->fParameters[i])) { |
| 1329 parameters.push_back(this->getLValue(*c.fParameters[i], out)); |
| 1330 } else { |
| 1331 SpvId expr = this->writeExpression(*c.fParameters[i], out); |
| 1332 SpvId tmpVar = this->nextId(); |
| 1333 this->writeInstruction(SpvOpVariable, |
| 1334 this->getPointerType(c.fParameters[i]->fType,
|
| 1335 SpvStorageClassFunction)
, |
| 1336 tmpVar, |
| 1337 SpvStorageClassFunction, |
| 1338 out); |
| 1339 this->writeInstruction(SpvOpStore, tmpVar, expr, out); |
| 1340 parameters.push_back(tmpVar); |
| 1341 } |
| 1342 } |
| 1343 SpvId result = this->nextId(); |
| 1344 this->writeOpCode(SpvOpFunctionCall, 4 + (int32_t) c.fParameters.size(), out
); |
| 1345 this->writeWord(this->getType(c.fType), out); |
| 1346 this->writeWord(result, out); |
| 1347 this->writeWord(entry->second, out); |
| 1348 for (SpvId id : parameters) { |
| 1349 this->writeWord(id, out); |
| 1350 } |
| 1351 return result; |
| 1352 } |
| 1353 |
| 1354 SpvId SPIRVCodeGenerator::writeConstructor(Constructor& c, std::ostream& out) { |
| 1355 SpvId result = this->nextId(); |
| 1356 if (c.fType->kind() == Type::kVector_Kind && c.isConstant()) { |
| 1357 std::vector<SpvId> parameters; |
| 1358 for (size_t i = 0; i < c.fParameters.size(); i++) { |
| 1359 parameters.push_back(this->writeExpression(*c.fParameters[i], fConst
antBuffer)); |
| 1360 } |
| 1361 SpvId type = this->getType(c.fType); |
| 1362 if (c.fParameters.size() == 1) { |
| 1363 this->writeOpCode(SpvOpConstantComposite, 3 + c.fType->columns(), fC
onstantBuffer); |
| 1364 this->writeWord(type, fConstantBuffer); |
| 1365 this->writeWord(result, fConstantBuffer); |
| 1366 for (int i = 0; i < c.fType->columns(); i++) { |
| 1367 this->writeWord(parameters[0], fConstantBuffer); |
| 1368 } |
| 1369 } else { |
| 1370 this->writeOpCode(SpvOpConstantComposite, 3 + (int32_t) c.fParameter
s.size(), |
| 1371 fConstantBuffer); |
| 1372 this->writeWord(type, fConstantBuffer); |
| 1373 this->writeWord(result, fConstantBuffer); |
| 1374 for (SpvId id : parameters) { |
| 1375 this->writeWord(id, fConstantBuffer); |
| 1376 } |
| 1377 } |
| 1378 return result; |
| 1379 } |
| 1380 if (c.fType == kFloat_Type) { |
| 1381 ASSERT(c.fParameters.size() == 1); |
| 1382 ASSERT(c.fParameters[0]->fType->isNumber()); |
| 1383 SpvId parameter = this->writeExpression(*c.fParameters[0], out); |
| 1384 if (c.fParameters[0]->fType == kInt_Type) { |
| 1385 this->writeInstruction(SpvOpConvertSToF, this->getType(c.fType), res
ult, parameter, |
| 1386 out); |
| 1387 } else if (c.fParameters[0]->fType == kUInt_Type) { |
| 1388 this->writeInstruction(SpvOpConvertUToF, this->getType(c.fType), res
ult, parameter, |
| 1389 out); |
| 1390 } else if (c.fParameters[0]->fType == kFloat_Type) { |
| 1391 return parameter; |
| 1392 } |
| 1393 return result; |
| 1394 } |
| 1395 else if (c.fType == kInt_Type) { |
| 1396 ASSERT(c.fParameters.size() == 1); |
| 1397 ASSERT(c.fParameters[0]->fType->isNumber()); |
| 1398 SpvId parameter = this->writeExpression(*c.fParameters[0], out); |
| 1399 if (c.fParameters[0]->fType == kFloat_Type) { |
| 1400 this->writeInstruction(SpvOpConvertFToS, this->getType(c.fType), res
ult, parameter, |
| 1401 out); |
| 1402 } else if (c.fParameters[0]->fType == kUInt_Type) { |
| 1403 this->writeInstruction(SpvOpSatConvertUToS, this->getType(c.fType),
result, parameter, |
| 1404 out); |
| 1405 } else if (c.fParameters[0]->fType == kInt_Type) { |
| 1406 return parameter; |
| 1407 } |
| 1408 return result; |
| 1409 } |
| 1410 ASSERT(!c.fType->isNumber()); |
| 1411 std::vector<SpvId> parameters; |
| 1412 for (size_t i = 0; i < c.fParameters.size(); i++) { |
| 1413 parameters.push_back(this->writeExpression(*c.fParameters[i], out)); |
| 1414 } |
| 1415 int rows = c.fType->rows(); |
| 1416 int columns = c.fType->columns(); |
| 1417 if (parameters.size() == 1) { |
| 1418 if (c.fType->kind() == Type::kVector_Kind) { |
| 1419 this->writeOpCode(SpvOpCompositeConstruct, 3 + c.fType->columns(), o
ut); |
| 1420 this->writeWord(this->getType(c.fType), out); |
| 1421 this->writeWord(result, out); |
| 1422 for (int i = 0; i < c.fType->columns(); i++) { |
| 1423 this->writeWord(parameters[0], out); |
| 1424 } |
| 1425 } else { |
| 1426 ASSERT(c.fType->kind() == Type::kMatrix_Kind); |
| 1427 // FIXME this won't work for int matrices |
| 1428 FloatLiteral zero(Position(), 0); |
| 1429 SpvId zeroId = this->writeFloatLiteral(zero); |
| 1430 std::vector<SpvId> columnIds; |
| 1431 for (int column = 0; column < columns; column++) { |
| 1432 this->writeOpCode(SpvOpCompositeConstruct, 3 + c.fType->rows(), |
| 1433 out); |
| 1434 this->writeWord(this->getType(c.fType->componentType()->toCompou
nd(rows, 1)), out); |
| 1435 for (int row = 0; row < c.fType->columns(); row++) { |
| 1436 this->writeWord(row == column ? parameters[0] : zeroId, out)
; |
| 1437 } |
| 1438 } |
| 1439 this->writeOpCode(SpvOpCompositeConstruct, 3 + columns, |
| 1440 out); |
| 1441 this->writeWord(this->getType(c.fType), out); |
| 1442 for (SpvId id : columnIds) { |
| 1443 this->writeWord(id, out); |
| 1444 } |
| 1445 this->writeWord(result, out); |
| 1446 } |
| 1447 } else if (c.fType->kind() == Type::kMatrix_Kind) { |
| 1448 std::vector<SpvId> columnIds; |
| 1449 int currentCount = 0; |
| 1450 for (size_t i = 0; i < parameters.size(); i++) { |
| 1451 if (c.fParameters[i]->fType->kind() == Type::kVector_Kind) { |
| 1452 ASSERT(currentCount == 0); |
| 1453 columnIds.push_back(parameters[i]); |
| 1454 currentCount = 0; |
| 1455 } else { |
| 1456 ASSERT(c.fParameters[i]->fType->kind() == Type::kScalar_Kind); |
| 1457 if (currentCount == 0) { |
| 1458 this->writeOpCode(SpvOpCompositeConstruct, 3 + c.fType->rows
(), out); |
| 1459 this->writeWord(this->getType(c.fType->componentType()->toCo
mpound(rows, 1)), |
| 1460 out); |
| 1461 SpvId id = this->nextId(); |
| 1462 this->writeWord(id, out); |
| 1463 columnIds.push_back(id); |
| 1464 } |
| 1465 this->writeWord(parameters[i], out); |
| 1466 currentCount = (currentCount + 1) % rows; |
| 1467 } |
| 1468 } |
| 1469 ASSERT(columnIds.size() == columns) |
| 1470 this->writeOpCode(SpvOpCompositeConstruct, 3 + columns, out); |
| 1471 this->writeWord(this->getType(c.fType), out); |
| 1472 this->writeWord(result, out); |
| 1473 for (SpvId id : columnIds) { |
| 1474 this->writeWord(id, out); |
| 1475 } |
| 1476 } else { |
| 1477 this->writeOpCode(SpvOpCompositeConstruct, 3 + (int32_t) c.fParameters.s
ize(), out); |
| 1478 this->writeWord(this->getType(c.fType), out); |
| 1479 this->writeWord(result, out); |
| 1480 for (SpvId id : parameters) { |
| 1481 this->writeWord(id, out); |
| 1482 } |
| 1483 } |
| 1484 return result; |
| 1485 } |
| 1486 |
| 1487 SpvStorageClass_ get_storage_class(const Modifiers& modifiers) { |
| 1488 if (modifiers.fBits & Modifiers::kIn_Bit) { |
| 1489 return SpvStorageClassInput; |
| 1490 } else if (modifiers.fBits & Modifiers::kOut_Bit) { |
| 1491 return SpvStorageClassOutput; |
| 1492 } else if (modifiers.fBits & Modifiers::kUniform_Bit) { |
| 1493 return SpvStorageClassUniform; |
| 1494 } else { |
| 1495 return SpvStorageClassFunction; |
| 1496 } |
| 1497 } |
| 1498 |
| 1499 SpvStorageClass_ get_storage_class(Expression& expr) { |
| 1500 switch (expr.fKind) { |
| 1501 case Expression::kVariableReference_Kind: |
| 1502 return get_storage_class(((VariableReference&) expr).fVariable->fMod
ifiers); |
| 1503 case Expression::kFieldAccess_Kind: |
| 1504 return get_storage_class(*((FieldAccess&) expr).fBase); |
| 1505 case Expression::kIndex_Kind: |
| 1506 return get_storage_class(*((IndexExpression&) expr).fBase); |
| 1507 default: |
| 1508 SkDebugf("cannot determine storage class for %d\n", expr.fKind); |
| 1509 return SpvStorageClassFunction; |
| 1510 } |
| 1511 } |
| 1512 |
| 1513 SpvId SPIRVCodeGenerator::getLValue(Expression& value, std::ostream& out) { |
| 1514 switch (value.fKind) { |
| 1515 case Expression::kVariableReference_Kind: { |
| 1516 std::shared_ptr<Variable> var = ((VariableReference&) value).fVariab
le; |
| 1517 auto entry = fVariableMap.find(var); |
| 1518 ASSERT(entry != fVariableMap.end()); |
| 1519 return entry->second; |
| 1520 } |
| 1521 case Expression::kIndex_Kind: { |
| 1522 IndexExpression& index = (IndexExpression&) value; |
| 1523 SpvId base = this->getLValue(*index.fBase, out);; |
| 1524 SpvId result = this->nextId(); |
| 1525 this->writeInstruction(SpvOpAccessChain, |
| 1526 this->getPointerType(index.fType, |
| 1527 get_storage_class(*index
.fBase)), |
| 1528 result, |
| 1529 base, |
| 1530 this->writeExpression(*index.fIndex, out), |
| 1531 out); |
| 1532 return result; |
| 1533 } |
| 1534 case Expression::kFieldAccess_Kind: { |
| 1535 FieldAccess& f = (FieldAccess&) value; |
| 1536 SpvId base = this->getLValue(*f.fBase, out);; |
| 1537 IntLiteral index(Position(), f.fFieldIndex); |
| 1538 SpvId result = this->nextId(); |
| 1539 this->writeInstruction(SpvOpAccessChain, |
| 1540 this->getPointerType(f.fType, |
| 1541 get_storage_class(*f.fBa
se)), |
| 1542 result, |
| 1543 base, |
| 1544 this->writeIntLiteral(index), |
| 1545 out); |
| 1546 return result; |
| 1547 } |
| 1548 default: |
| 1549 SpvId result = this->nextId(); |
| 1550 SpvId type = this->getPointerType(value.fType, SpvStorageClassFuncti
on); |
| 1551 this->writeInstruction(SpvOpVariable, type, result, SpvStorageClassF
unction, out); |
| 1552 this->writeInstruction(SpvOpStore, result, this->writeExpression(val
ue, out), out); |
| 1553 return result; |
| 1554 } |
| 1555 } |
| 1556 |
| 1557 void SPIRVCodeGenerator::storeToLValue(Expression& lvalue, SpvId value, std::ost
ream& out) { |
| 1558 switch (lvalue.fKind) { |
| 1559 case Expression::kVariableReference_Kind: // fall through |
| 1560 case Expression::kIndex_Kind: // fall through |
| 1561 case Expression::kFieldAccess_Kind: { |
| 1562 SpvId id = this->getLValue(lvalue, out); |
| 1563 this->writeInstruction(SpvOpStore, id, value, out); |
| 1564 break; |
| 1565 } |
| 1566 case Expression::kSwizzle_Kind: { |
| 1567 Swizzle& swizzle = (Swizzle&) lvalue; |
| 1568 size_t count = swizzle.fComponents.size(); |
| 1569 SpvId base = this->getLValue(*swizzle.fBase, out); |
| 1570 if (count == 1) { |
| 1571 IntLiteral index(Position(), swizzle.fComponents[0]); |
| 1572 SpvId target = this->nextId(); |
| 1573 this->writeInstruction(SpvOpAccessChain, |
| 1574 this->getPointerType(swizzle.fType, |
| 1575 get_storage_class(*s
wizzle.fBase)), |
| 1576 target, |
| 1577 base, |
| 1578 this->writeIntLiteral(index), |
| 1579 out); |
| 1580 this->writeInstruction(SpvOpStore, target, value, out); |
| 1581 } else { |
| 1582 SpvId shuffle = this->nextId(); |
| 1583 std::shared_ptr<Type> baseType = swizzle.fBase->fType; |
| 1584 SpvId lhs = this->writeExpression(*swizzle.fBase, out); |
| 1585 this->writeOpCode(SpvOpVectorShuffle, 5 + baseType->columns(), o
ut); |
| 1586 this->writeWord(this->getType(baseType), out); |
| 1587 this->writeWord(shuffle, out); |
| 1588 this->writeWord(lhs, out); |
| 1589 this->writeWord(value, out); |
| 1590 for (int i = 0; i < baseType->columns(); i++) { |
| 1591 int offset = i; |
| 1592 for (size_t j = 0; j < swizzle.fComponents.size(); j++) { |
| 1593 if (swizzle.fComponents[j] == i) { |
| 1594 offset = j + baseType->columns(); |
| 1595 break; |
| 1596 } |
| 1597 } |
| 1598 this->writeWord(offset, out); |
| 1599 } |
| 1600 this->writeInstruction(SpvOpStore, base, shuffle, out); |
| 1601 } |
| 1602 break; |
| 1603 } |
| 1604 default: |
| 1605 printf("cannot use %s as an lvalue\n", lvalue.description().c_str())
; |
| 1606 ASSERT(false); |
| 1607 } |
| 1608 } |
| 1609 |
| 1610 SpvId SPIRVCodeGenerator::writeVariableReference(VariableReference& ref, std::os
tream& out) { |
| 1611 auto entry = fVariableMap.find(ref.fVariable); |
| 1612 ASSERT(entry != fVariableMap.end()); |
| 1613 SpvId var = entry->second; |
| 1614 /* if (ref.fVariable->fStorage == Variable::kParameter_Storage && !is_out(ref
.fVariable) && |
| 1615 !ref.fVariable->fIsWrittenTo) { |
| 1616 return entry->second; |
| 1617 }*/ |
| 1618 SpvId result = this->nextId(); |
| 1619 this->writeInstruction(SpvOpLoad, this->getType(ref.fVariable->fType), resul
t, var, out); |
| 1620 return result; |
| 1621 } |
| 1622 |
| 1623 std::vector<SpvId> SPIRVCodeGenerator::getAccessChain(Expression& expr, std::ost
ream& out) { |
| 1624 std::vector<SpvId> chain; |
| 1625 switch (expr.fKind) { |
| 1626 case Expression::kIndex_Kind: { |
| 1627 IndexExpression& indexExpr = (IndexExpression&) expr; |
| 1628 chain = this->getAccessChain(*indexExpr.fBase, out); |
| 1629 chain.push_back(this->writeExpression(*indexExpr.fIndex, out)); |
| 1630 break; |
| 1631 } |
| 1632 case Expression::kFieldAccess_Kind: { |
| 1633 FieldAccess& fieldExpr = (FieldAccess&) expr; |
| 1634 chain = this->getAccessChain(*fieldExpr.fBase, out); |
| 1635 IntLiteral index(Position(), fieldExpr.fFieldIndex); |
| 1636 chain.push_back(this->writeIntLiteral(index)); |
| 1637 break; |
| 1638 } |
| 1639 default: |
| 1640 chain.push_back(this->getLValue(expr, out)); |
| 1641 } |
| 1642 return chain; |
| 1643 } |
| 1644 |
| 1645 SpvId SPIRVCodeGenerator::writeIndexExpression(IndexExpression& expr, std::ostre
am& out) { |
| 1646 std::vector<SpvId> chain = this->getAccessChain(expr, out); |
| 1647 SpvId member = this->nextId(); |
| 1648 this->writeOpCode(SpvOpAccessChain, 3 + chain.size(), out); |
| 1649 this->writeWord(this->getPointerType(expr.fType, get_storage_class(*expr.fBa
se)), out); |
| 1650 this->writeWord(member, out); |
| 1651 for (SpvId idx : chain) { |
| 1652 this->writeWord(idx, out); |
| 1653 } |
| 1654 SpvId result = this->nextId(); |
| 1655 this->writeInstruction(SpvOpLoad, this->getType(expr.fType), result, member,
out); |
| 1656 return result; |
| 1657 } |
| 1658 |
| 1659 SpvId SPIRVCodeGenerator::writeFieldAccess(FieldAccess& f, std::ostream& out) { |
| 1660 std::vector<SpvId> chain = this->getAccessChain(f, out); |
| 1661 SpvId member = this->nextId(); |
| 1662 this->writeOpCode(SpvOpAccessChain, 3 + chain.size(), out); |
| 1663 this->writeWord(this->getPointerType(f.fType, get_storage_class(*f.fBase)),
out); |
| 1664 this->writeWord(member, out); |
| 1665 for (SpvId idx : chain) { |
| 1666 this->writeWord(idx, out); |
| 1667 } |
| 1668 SpvId result = this->nextId(); |
| 1669 this->writeInstruction(SpvOpLoad, this->getType(f.fType), result, member, ou
t); |
| 1670 return result; |
| 1671 } |
| 1672 |
| 1673 SpvId SPIRVCodeGenerator::writeSwizzle(Swizzle& swizzle, std::ostream& out) { |
| 1674 SpvId base = this->writeExpression(*swizzle.fBase, out); |
| 1675 SpvId result = this->nextId(); |
| 1676 size_t count = swizzle.fComponents.size(); |
| 1677 if (count == 1) { |
| 1678 this->writeInstruction(SpvOpCompositeExtract, this->getType(swizzle.fTyp
e), result, base, |
| 1679 swizzle.fComponents[0], out); |
| 1680 } else { |
| 1681 this->writeOpCode(SpvOpVectorShuffle, 5 + (int32_t) count, out); |
| 1682 this->writeWord(this->getType(swizzle.fType), out); |
| 1683 this->writeWord(result, out); |
| 1684 this->writeWord(base, out); |
| 1685 this->writeWord(base, out); |
| 1686 for (int component : swizzle.fComponents) { |
| 1687 this->writeWord(component, out); |
| 1688 } |
| 1689 } |
| 1690 return result; |
| 1691 } |
| 1692 |
| 1693 SpvId SPIRVCodeGenerator::writeBinaryOperation(std::shared_ptr<Type> resultType,
|
| 1694 std::shared_ptr<Type> operandType
, SpvId lhs, |
| 1695 SpvId rhs, SpvOp_ ifFloat, SpvOp_
ifInt, |
| 1696 SpvOp_ ifUInt, SpvOp_ ifBool, std
::ostream& out) { |
| 1697 SpvId result = this->nextId(); |
| 1698 if (is_float(operandType)) { |
| 1699 this->writeInstruction(ifFloat, this->getType(resultType), result, lhs,
rhs, out); |
| 1700 } else if (is_signed(operandType)) { |
| 1701 this->writeInstruction(ifInt, this->getType(resultType), result, lhs, rh
s, out); |
| 1702 } else if (is_unsigned(operandType)) { |
| 1703 this->writeInstruction(ifUInt, this->getType(resultType), result, lhs, r
hs, out); |
| 1704 } else if (operandType == kBool_Type) { |
| 1705 this->writeInstruction(ifBool, this->getType(resultType), result, lhs, r
hs, out); |
| 1706 } else { |
| 1707 printf("invalid operandType: %s\n", operandType->description().c_str()); |
| 1708 ASSERT(false); |
| 1709 } |
| 1710 return result; |
| 1711 } |
| 1712 |
| 1713 SpvId SPIRVCodeGenerator::writeBinaryExpression(BinaryExpression& b, std::ostrea
m& out) { |
| 1714 // handle cases where we don't necessarily evaluate both LHS and RHS |
| 1715 switch (b.fOperator) { |
| 1716 case Token::EQ: { |
| 1717 SpvId rhs = this->writeExpression(*b.fRight, out); |
| 1718 this->storeToLValue(*b.fLeft, rhs, out); |
| 1719 return rhs; |
| 1720 } |
| 1721 case Token::LOGICALAND: |
| 1722 return this->writeLogicalAnd(b, out); |
| 1723 case Token::LOGICALOR: |
| 1724 return this->writeLogicalOr(b, out); |
| 1725 default: |
| 1726 break; |
| 1727 } |
| 1728 |
| 1729 // "normal" operators |
| 1730 std::shared_ptr<Type> resultType = b.fType; |
| 1731 SpvId lhs = this->writeExpression(*b.fLeft, out); |
| 1732 SpvId rhs = this->writeExpression(*b.fRight, out); |
| 1733 // component type we are operating on: float, int, uint |
| 1734 std::shared_ptr<Type> operandType; |
| 1735 // IR allows mismatched types in expressions (e.g. vec2 * float), but they n
eed special handling |
| 1736 // in SPIR-V |
| 1737 if (b.fLeft->fType != b.fRight->fType) { |
| 1738 if (b.fLeft->fType->kind() == Type::kVector_Kind && |
| 1739 b.fRight->fType->isNumber()) { |
| 1740 // promote number to vector |
| 1741 SpvId vec = this->nextId(); |
| 1742 this->writeOpCode(SpvOpCompositeConstruct, 3 + b.fType->columns(), o
ut); |
| 1743 this->writeWord(this->getType(resultType), out); |
| 1744 this->writeWord(vec, out); |
| 1745 for (int i = 0; i < resultType->columns(); i++) { |
| 1746 this->writeWord(rhs, out); |
| 1747 } |
| 1748 rhs = vec; |
| 1749 operandType = b.fRight->fType; |
| 1750 } else if (b.fRight->fType->kind() == Type::kVector_Kind && |
| 1751 b.fLeft->fType->isNumber()) { |
| 1752 // promote number to vector |
| 1753 SpvId vec = this->nextId(); |
| 1754 this->writeOpCode(SpvOpCompositeConstruct, 3 + b.fType->columns(), o
ut); |
| 1755 this->writeWord(this->getType(resultType), out); |
| 1756 this->writeWord(vec, out); |
| 1757 for (int i = 0; i < resultType->columns(); i++) { |
| 1758 this->writeWord(lhs, out); |
| 1759 } |
| 1760 lhs = vec; |
| 1761 operandType = b.fLeft->fType; |
| 1762 } else if (b.fLeft->fType->kind() == Type::kMatrix_Kind) { |
| 1763 SpvOp_ op; |
| 1764 if (b.fRight->fType->kind() == Type::kMatrix_Kind) { |
| 1765 op = SpvOpMatrixTimesMatrix; |
| 1766 } else if (b.fRight->fType->kind() == Type::kVector_Kind) { |
| 1767 op = SpvOpMatrixTimesVector; |
| 1768 } else { |
| 1769 ASSERT(b.fRight->fType->kind() == Type::kScalar_Kind); |
| 1770 op = SpvOpMatrixTimesScalar; |
| 1771 } |
| 1772 SpvId result = this->nextId(); |
| 1773 this->writeInstruction(op, this->getType(b.fType), result, lhs, rhs,
out); |
| 1774 if (b.fOperator == Token::STAREQ) { |
| 1775 this->storeToLValue(*b.fLeft, result, out); |
| 1776 } else { |
| 1777 ASSERT(b.fOperator == Token::STAR); |
| 1778 } |
| 1779 return result; |
| 1780 } else if (b.fRight->fType->kind() == Type::kMatrix_Kind) { |
| 1781 SpvId result = this->nextId(); |
| 1782 if (b.fLeft->fType->kind() == Type::kVector_Kind) { |
| 1783 this->writeInstruction(SpvOpVectorTimesMatrix, this->getType(b.f
Type), result, lhs, |
| 1784 rhs, out); |
| 1785 } else { |
| 1786 ASSERT(b.fLeft->fType->kind() == Type::kScalar_Kind); |
| 1787 this->writeInstruction(SpvOpMatrixTimesScalar, this->getType(b.f
Type), result, rhs, |
| 1788 lhs, out); |
| 1789 } |
| 1790 if (b.fOperator == Token::STAREQ) { |
| 1791 this->storeToLValue(*b.fLeft, result, out); |
| 1792 } else { |
| 1793 ASSERT(b.fOperator == Token::STAR); |
| 1794 } |
| 1795 return result; |
| 1796 } else { |
| 1797 printf("unsupported binary expression: %s\n", b.description().c_str(
)); |
| 1798 ASSERT(false); |
| 1799 } |
| 1800 } else { |
| 1801 operandType = b.fLeft->fType; |
| 1802 ASSERT(operandType == b.fRight->fType); |
| 1803 } |
| 1804 switch (b.fOperator) { |
| 1805 case Token::EQEQ: |
| 1806 ASSERT(resultType == kBool_Type); |
| 1807 return this->writeBinaryOperation(resultType, operandType, lhs, rhs,
SpvOpFOrdEqual, |
| 1808 SpvOpIEqual, SpvOpIEqual, SpvOpLog
icalEqual, out); |
| 1809 case Token::NEQ: |
| 1810 ASSERT(resultType == kBool_Type); |
| 1811 return this->writeBinaryOperation(resultType, operandType, lhs, rhs,
SpvOpFOrdNotEqual, |
| 1812 SpvOpINotEqual, SpvOpINotEqual, Sp
vOpLogicalNotEqual, |
| 1813 out); |
| 1814 case Token::GT: |
| 1815 ASSERT(resultType == kBool_Type); |
| 1816 return this->writeBinaryOperation(resultType, operandType, lhs, rhs,
|
| 1817 SpvOpFOrdGreaterThan, SpvOpSGreate
rThan, |
| 1818 SpvOpUGreaterThan, SpvOpUndef, out
); |
| 1819 case Token::LT: |
| 1820 ASSERT(resultType == kBool_Type); |
| 1821 return this->writeBinaryOperation(resultType, operandType, lhs, rhs,
SpvOpFOrdLessThan, |
| 1822 SpvOpSLessThan, SpvOpULessThan, Sp
vOpUndef, out); |
| 1823 case Token::GTEQ: |
| 1824 ASSERT(resultType == kBool_Type); |
| 1825 return this->writeBinaryOperation(resultType, operandType, lhs, rhs,
|
| 1826 SpvOpFOrdGreaterThanEqual, SpvOpSG
reaterThanEqual, |
| 1827 SpvOpUGreaterThanEqual, SpvOpUndef
, out); |
| 1828 case Token::LTEQ: |
| 1829 ASSERT(resultType == kBool_Type); |
| 1830 return this->writeBinaryOperation(resultType, operandType, lhs, rhs,
|
| 1831 SpvOpFOrdLessThanEqual, SpvOpSLess
ThanEqual, |
| 1832 SpvOpULessThanEqual, SpvOpUndef, o
ut); |
| 1833 case Token::PLUS: |
| 1834 return this->writeBinaryOperation(resultType, operandType, lhs, rhs,
SpvOpFAdd, |
| 1835 SpvOpIAdd, SpvOpIAdd, SpvOpUndef,
out); |
| 1836 case Token::MINUS: |
| 1837 return this->writeBinaryOperation(resultType, operandType, lhs, rhs,
SpvOpFSub, |
| 1838 SpvOpISub, SpvOpISub, SpvOpUndef,
out); |
| 1839 case Token::STAR: |
| 1840 if (b.fLeft->fType->kind() == Type::kMatrix_Kind && |
| 1841 b.fRight->fType->kind() == Type::kMatrix_Kind) { |
| 1842 // matrix multiply |
| 1843 SpvId result = this->nextId(); |
| 1844 this->writeInstruction(SpvOpMatrixTimesMatrix, this->getType(res
ultType), result, |
| 1845 lhs, rhs, out); |
| 1846 return result; |
| 1847 } |
| 1848 return this->writeBinaryOperation(resultType, operandType, lhs, rhs,
SpvOpFMul, |
| 1849 SpvOpIMul, SpvOpIMul, SpvOpUndef,
out); |
| 1850 case Token::SLASH: |
| 1851 return this->writeBinaryOperation(resultType, operandType, lhs, rhs,
SpvOpFDiv, |
| 1852 SpvOpSDiv, SpvOpUDiv, SpvOpUndef,
out); |
| 1853 case Token::PLUSEQ: { |
| 1854 SpvId result = this->writeBinaryOperation(resultType, operandType, l
hs, rhs, SpvOpFAdd, |
| 1855 SpvOpIAdd, SpvOpIAdd, SpvO
pUndef, out); |
| 1856 this->storeToLValue(*b.fLeft, result, out); |
| 1857 return result; |
| 1858 } |
| 1859 case Token::MINUSEQ: { |
| 1860 SpvId result = this->writeBinaryOperation(resultType, operandType, l
hs, rhs, SpvOpFSub, |
| 1861 SpvOpISub, SpvOpISub, SpvO
pUndef, out); |
| 1862 this->storeToLValue(*b.fLeft, result, out); |
| 1863 return result; |
| 1864 } |
| 1865 case Token::STAREQ: { |
| 1866 if (b.fLeft->fType->kind() == Type::kMatrix_Kind && |
| 1867 b.fRight->fType->kind() == Type::kMatrix_Kind) { |
| 1868 // matrix multiply |
| 1869 SpvId result = this->nextId(); |
| 1870 this->writeInstruction(SpvOpMatrixTimesMatrix, this->getType(res
ultType), result, |
| 1871 lhs, rhs, out); |
| 1872 this->storeToLValue(*b.fLeft, result, out); |
| 1873 return result; |
| 1874 } |
| 1875 SpvId result = this->writeBinaryOperation(resultType, operandType, l
hs, rhs, SpvOpFMul, |
| 1876 SpvOpIMul, SpvOpIMul, SpvO
pUndef, out); |
| 1877 this->storeToLValue(*b.fLeft, result, out); |
| 1878 return result; |
| 1879 } |
| 1880 case Token::SLASHEQ: { |
| 1881 SpvId result = this->writeBinaryOperation(resultType, operandType, l
hs, rhs, SpvOpFDiv, |
| 1882 SpvOpSDiv, SpvOpUDiv, SpvO
pUndef, out); |
| 1883 this->storeToLValue(*b.fLeft, result, out); |
| 1884 return result; |
| 1885 } |
| 1886 default: |
| 1887 ABORT("unsupported binary expression: %s\n", b.description().c_str()
); |
| 1888 } |
| 1889 } |
| 1890 |
| 1891 SpvId SPIRVCodeGenerator::writeLogicalAnd(BinaryExpression& a, std::ostream& out
) { |
| 1892 ASSERT(a.fOperator == Token::LOGICALAND); |
| 1893 BoolLiteral falseLiteral(Position(), false); |
| 1894 SpvId falseConstant = this->writeBoolLiteral(falseLiteral); |
| 1895 SpvId lhs = this->writeExpression(*a.fLeft, out); |
| 1896 SpvId rhsLabel = this->nextId(); |
| 1897 SpvId end = this->nextId(); |
| 1898 SpvId lhsBlock = fCurrentBlock; |
| 1899 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone
, out); |
| 1900 this->writeInstruction(SpvOpBranchConditional, lhs, rhsLabel, end, out); |
| 1901 this->writeLabel(rhsLabel, out); |
| 1902 SpvId rhs = this->writeExpression(*a.fRight, out); |
| 1903 SpvId rhsBlock = fCurrentBlock; |
| 1904 this->writeInstruction(SpvOpBranch, end, out); |
| 1905 this->writeLabel(end, out); |
| 1906 SpvId result = this->nextId(); |
| 1907 this->writeInstruction(SpvOpPhi, this->getType(kBool_Type), result, falseCon
stant, lhsBlock, |
| 1908 rhs, rhsBlock, out); |
| 1909 return result; |
| 1910 } |
| 1911 |
| 1912 SpvId SPIRVCodeGenerator::writeLogicalOr(BinaryExpression& o, std::ostream& out)
{ |
| 1913 ASSERT(o.fOperator == Token::LOGICALOR); |
| 1914 BoolLiteral trueLiteral(Position(), true); |
| 1915 SpvId trueConstant = this->writeBoolLiteral(trueLiteral); |
| 1916 SpvId lhs = this->writeExpression(*o.fLeft, out); |
| 1917 SpvId rhsLabel = this->nextId(); |
| 1918 SpvId end = this->nextId(); |
| 1919 SpvId lhsBlock = fCurrentBlock; |
| 1920 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone
, out); |
| 1921 this->writeInstruction(SpvOpBranchConditional, lhs, end, rhsLabel, out); |
| 1922 this->writeLabel(rhsLabel, out); |
| 1923 SpvId rhs = this->writeExpression(*o.fRight, out); |
| 1924 SpvId rhsBlock = fCurrentBlock; |
| 1925 this->writeInstruction(SpvOpBranch, end, out); |
| 1926 this->writeLabel(end, out); |
| 1927 SpvId result = this->nextId(); |
| 1928 this->writeInstruction(SpvOpPhi, this->getType(kBool_Type), result, trueCons
tant, lhsBlock, |
| 1929 rhs, rhsBlock, out); |
| 1930 return result; |
| 1931 } |
| 1932 |
| 1933 SpvId SPIRVCodeGenerator::writeTernaryExpression(TernaryExpression& t, std::ostr
eam& out) { |
| 1934 SpvId test = this->writeExpression(*t.fTest, out); |
| 1935 if (t.fIfTrue->isConstant() && t.fIfFalse->isConstant()) { |
| 1936 // both true and false are constants, can just use OpSelect |
| 1937 SpvId result = this->nextId(); |
| 1938 SpvId trueId = this->writeExpression(*t.fIfTrue, out); |
| 1939 SpvId falseId = this->writeExpression(*t.fIfFalse, out); |
| 1940 this->writeInstruction(SpvOpSelect, this->getType(t.fType), result, test
, trueId, falseId, |
| 1941 out); |
| 1942 return result; |
| 1943 } |
| 1944 // was originally using OpPhi to choose the result, but for some reason that
is crashing on |
| 1945 // Adreno. Switched to storing the result in a temp variable as glslang does
. |
| 1946 SpvId var = this->nextId(); |
| 1947 this->writeInstruction(SpvOpVariable, this->getPointerType(t.fType, SpvStora
geClassFunction), |
| 1948 var, SpvStorageClassFunction, out); |
| 1949 SpvId trueLabel = this->nextId(); |
| 1950 SpvId falseLabel = this->nextId(); |
| 1951 SpvId end = this->nextId(); |
| 1952 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone
, out); |
| 1953 this->writeInstruction(SpvOpBranchConditional, test, trueLabel, falseLabel,
out); |
| 1954 this->writeLabel(trueLabel, out); |
| 1955 this->writeInstruction(SpvOpStore, var, this->writeExpression(*t.fIfTrue, ou
t), out); |
| 1956 this->writeInstruction(SpvOpBranch, end, out); |
| 1957 this->writeLabel(falseLabel, out); |
| 1958 this->writeInstruction(SpvOpStore, var, this->writeExpression(*t.fIfFalse, o
ut), out); |
| 1959 this->writeInstruction(SpvOpBranch, end, out); |
| 1960 this->writeLabel(end, out); |
| 1961 SpvId result = this->nextId(); |
| 1962 this->writeInstruction(SpvOpLoad, this->getType(t.fType), result, var, out); |
| 1963 return result; |
| 1964 } |
| 1965 |
| 1966 SpvId SPIRVCodeGenerator::writePrefixExpression(PrefixExpression& p, std::ostrea
m& out) { |
| 1967 SpvId expr = this->writeExpression(*p.fOperand, out); |
| 1968 if (p.fOperator == Token::MINUS) { |
| 1969 SpvId result = this->nextId(); |
| 1970 SpvId typeId = this->getType(p.fType); |
| 1971 if (is_float(p.fType)) { |
| 1972 this->writeInstruction(SpvOpFNegate, typeId, result, expr, out); |
| 1973 } else if (is_signed(p.fType)) { |
| 1974 this->writeInstruction(SpvOpSNegate, typeId, result, expr, out); |
| 1975 } else { |
| 1976 ABORT("unsupported prefix expression %s\n", p.description().c_str())
; |
| 1977 }; |
| 1978 return result; |
| 1979 } |
| 1980 switch (p.fOperator) { |
| 1981 case Token::PLUS: |
| 1982 return expr; |
| 1983 case Token::PLUSPLUS: { |
| 1984 IntLiteral oneLiteral(Position(), 1); |
| 1985 SpvId one = this->writeExpression(oneLiteral, out); |
| 1986 SpvId result = this->writeBinaryOperation(p.fType, p.fType, expr, on
e, SpvOpFAdd, |
| 1987 SpvOpIAdd, SpvOpIAdd, SpvOpU
ndef, out); |
| 1988 this->storeToLValue(*p.fOperand, result, out); |
| 1989 return result; |
| 1990 } |
| 1991 case Token::MINUSMINUS: { |
| 1992 IntLiteral oneLiteral(Position(), 1); |
| 1993 SpvId one = this->writeExpression(oneLiteral, out); |
| 1994 SpvId result = this->writeBinaryOperation(p.fType, p.fType, expr, on
e, SpvOpFSub, |
| 1995 SpvOpISub, SpvOpISub, SpvOpU
ndef, out); |
| 1996 this->storeToLValue(*p.fOperand, result, out); |
| 1997 return result; |
| 1998 } |
| 1999 case Token::NOT: { |
| 2000 ASSERT(p.fOperand->fType == kBool_Type); |
| 2001 SpvId result = this->nextId(); |
| 2002 this->writeInstruction(SpvOpLogicalNot, this->getType(p.fOperand->fT
ype), result, |
| 2003 this->writeExpression(*p.fOperand, out), out)
; |
| 2004 return result; |
| 2005 } |
| 2006 default: |
| 2007 ABORT("unsupported prefix expression: %s\n", p.description().c_str()
); |
| 2008 } |
| 2009 } |
| 2010 |
| 2011 SpvId SPIRVCodeGenerator::writePostfixExpression(PostfixExpression& p, std::ostr
eam& out) { |
| 2012 SpvId result = this->writeExpression(*p.fOperand, out); |
| 2013 IntLiteral oneLiteral(Position(), 1); |
| 2014 SpvId one = this->writeExpression(oneLiteral, out); |
| 2015 switch (p.fOperator) { |
| 2016 case Token::PLUSPLUS: { |
| 2017 SpvId temp = this->writeBinaryOperation(p.fType, p.fType, result, on
e, SpvOpFAdd, |
| 2018 SpvOpIAdd, SpvOpIAdd, SpvOpU
ndef, out); |
| 2019 this->storeToLValue(*p.fOperand, temp, out); |
| 2020 return result; |
| 2021 } |
| 2022 case Token::MINUSMINUS: { |
| 2023 SpvId temp = this->writeBinaryOperation(p.fType, p.fType, result, on
e, SpvOpFSub, |
| 2024 SpvOpISub, SpvOpISub, SpvOpU
ndef, out); |
| 2025 this->storeToLValue(*p.fOperand, temp, out); |
| 2026 return result; |
| 2027 } |
| 2028 default: |
| 2029 ABORT("unsupported postfix expression %s\n", p.description().c_str()
); |
| 2030 } |
| 2031 } |
| 2032 |
| 2033 SpvId SPIRVCodeGenerator::writeBoolLiteral(BoolLiteral& b) { |
| 2034 if (b.fValue) { |
| 2035 if (fBoolTrue == 0) { |
| 2036 fBoolTrue = this->nextId(); |
| 2037 this->writeInstruction(SpvOpConstantTrue, this->getType(b.fType), fB
oolTrue, |
| 2038 fConstantBuffer); |
| 2039 } |
| 2040 return fBoolTrue; |
| 2041 } else { |
| 2042 if (fBoolFalse == 0) { |
| 2043 fBoolFalse = this->nextId(); |
| 2044 this->writeInstruction(SpvOpConstantFalse, this->getType(b.fType), f
BoolFalse, |
| 2045 fConstantBuffer); |
| 2046 } |
| 2047 return fBoolFalse; |
| 2048 } |
| 2049 } |
| 2050 |
| 2051 SpvId SPIRVCodeGenerator::writeIntLiteral(IntLiteral& i) { |
| 2052 if (i.fType == kInt_Type) { |
| 2053 auto entry = fIntConstants.find(i.fValue); |
| 2054 if (entry == fIntConstants.end()) { |
| 2055 SpvId result = this->nextId(); |
| 2056 this->writeInstruction(SpvOpConstant, this->getType(i.fType), result
, (SpvId) i.fValue, |
| 2057 fConstantBuffer); |
| 2058 fIntConstants[i.fValue] = result; |
| 2059 return result; |
| 2060 } |
| 2061 return entry->second; |
| 2062 } else { |
| 2063 ASSERT(i.fType == kUInt_Type); |
| 2064 auto entry = fUIntConstants.find(i.fValue); |
| 2065 if (entry == fUIntConstants.end()) { |
| 2066 SpvId result = this->nextId(); |
| 2067 this->writeInstruction(SpvOpConstant, this->getType(i.fType), result
, (SpvId) i.fValue, |
| 2068 fConstantBuffer); |
| 2069 fUIntConstants[i.fValue] = result; |
| 2070 return result; |
| 2071 } |
| 2072 return entry->second; |
| 2073 } |
| 2074 } |
| 2075 |
| 2076 SpvId SPIRVCodeGenerator::writeFloatLiteral(FloatLiteral& f) { |
| 2077 if (f.fType == kFloat_Type) { |
| 2078 float value = (float) f.fValue; |
| 2079 auto entry = fFloatConstants.find(value); |
| 2080 if (entry == fFloatConstants.end()) { |
| 2081 SpvId result = this->nextId(); |
| 2082 uint32_t bits; |
| 2083 ASSERT(sizeof(bits) == sizeof(value)); |
| 2084 memcpy(&bits, &value, sizeof(bits)); |
| 2085 this->writeInstruction(SpvOpConstant, this->getType(f.fType), result
, bits, |
| 2086 fConstantBuffer); |
| 2087 fFloatConstants[value] = result; |
| 2088 return result; |
| 2089 } |
| 2090 return entry->second; |
| 2091 } else { |
| 2092 ASSERT(f.fType == kDouble_Type); |
| 2093 auto entry = fDoubleConstants.find(f.fValue); |
| 2094 if (entry == fDoubleConstants.end()) { |
| 2095 SpvId result = this->nextId(); |
| 2096 uint64_t bits; |
| 2097 ASSERT(sizeof(bits) == sizeof(f.fValue)); |
| 2098 memcpy(&bits, &f.fValue, sizeof(bits)); |
| 2099 this->writeInstruction(SpvOpConstant, this->getType(f.fType), result
, bits & 0xffffffff, |
| 2100 bits >> 32, fConstantBuffer); |
| 2101 fDoubleConstants[f.fValue] = result; |
| 2102 return result; |
| 2103 } |
| 2104 return entry->second; |
| 2105 } |
| 2106 } |
| 2107 |
| 2108 SpvId SPIRVCodeGenerator::writeFunctionStart(std::shared_ptr<FunctionDeclaration
> f, |
| 2109 std::ostream& out) { |
| 2110 SpvId result = fFunctionMap[f]; |
| 2111 this->writeInstruction(SpvOpFunction, this->getType(f->fReturnType), result,
|
| 2112 SpvFunctionControlMaskNone, this->getFunctionType(f),
out); |
| 2113 this->writeInstruction(SpvOpName, result, f->fName.c_str(), fNameBuffer); |
| 2114 for (size_t i = 0; i < f->fParameters.size(); i++) { |
| 2115 SpvId id = this->nextId(); |
| 2116 fVariableMap[f->fParameters[i]] = id; |
| 2117 SpvId type; |
| 2118 // if (is_out(f->fParameters[i])) { |
| 2119 type = this->getPointerType(f->fParameters[i]->fType, SpvStorageClas
sFunction); |
| 2120 this->writeInstruction(SpvOpFunctionParameter, type, id, out); |
| 2121 /* } else { |
| 2122 type = this->getType(f->fParameters[i]->fType); |
| 2123 if (f->fParameters[i]->fIsWrittenTo) { |
| 2124 SpvId temp = this->nextId(); |
| 2125 this->writeInstruction(SpvOpFunctionParameter, type, temp, out); |
| 2126 SpvId ptr = this->getPointerType(f->fParameters[i]->fType, SpvSt
orageClassFunction); |
| 2127 ASSERT(!fCurrentBlock); |
| 2128 fCurrentBlock = -1; |
| 2129 this->writeInstruction(SpvOpVariable, ptr, id, SpvStorageClassFu
nction, |
| 2130 fVariableBuffer); |
| 2131 this->writeInstruction(SpvOpStore, id, temp, fVariableBuffer); |
| 2132 fCurrentBlock = 0; |
| 2133 } else { |
| 2134 this->writeInstruction(SpvOpFunctionParameter, type, id, out); |
| 2135 } |
| 2136 }*/ |
| 2137 } |
| 2138 return result; |
| 2139 } |
| 2140 |
| 2141 SpvId SPIRVCodeGenerator::writeFunctionDeclaration(std::shared_ptr<FunctionDecla
ration> f, |
| 2142 std::ostream& out) { |
| 2143 printf("WARNING: writing function declaration for %s\n", f->description().c_
str()); |
| 2144 SpvId result = this->writeFunctionStart(f, out); |
| 2145 this->writeInstruction(SpvOpFunctionEnd, out); |
| 2146 return result; |
| 2147 } |
| 2148 |
| 2149 SpvId SPIRVCodeGenerator::writeFunction(FunctionDefinition& f, std::ostream& out
) { |
| 2150 SpvId result = this->writeFunctionStart(f.fDeclaration, out); |
| 2151 this->writeLabel(this->nextId(), out); |
| 2152 if (f.fDeclaration->fName == "main") { |
| 2153 out << fGlobalInitializersBuffer.str(); |
| 2154 } |
| 2155 std::stringstream bodyBuffer; |
| 2156 this->writeBlock(*f.fBody, bodyBuffer); |
| 2157 out << fVariableBuffer.str(); |
| 2158 fVariableBuffer.str(""); |
| 2159 out << bodyBuffer.str(); |
| 2160 if (fCurrentBlock) { |
| 2161 this->writeInstruction(SpvOpReturn, out); |
| 2162 } |
| 2163 this->writeInstruction(SpvOpFunctionEnd, out); |
| 2164 return result; |
| 2165 } |
| 2166 |
| 2167 void SPIRVCodeGenerator::writeLayout(const Layout& layout, SpvId target) { |
| 2168 if (layout.fLocation >= 0) { |
| 2169 this->writeInstruction(SpvOpDecorate, target, SpvDecorationLocation, lay
out.fLocation, |
| 2170 fDecorationBuffer); |
| 2171 } |
| 2172 if (layout.fBinding >= 0) { |
| 2173 this->writeInstruction(SpvOpDecorate, target, SpvDecorationBinding, layo
ut.fBinding, |
| 2174 fDecorationBuffer); |
| 2175 } |
| 2176 if (layout.fIndex >= 0) { |
| 2177 this->writeInstruction(SpvOpDecorate, target, SpvDecorationIndex, layout
.fIndex, |
| 2178 fDecorationBuffer); |
| 2179 } |
| 2180 if (layout.fSet >= 0) { |
| 2181 this->writeInstruction(SpvOpDecorate, target, SpvDecorationDescriptorSet
, layout.fSet, |
| 2182 fDecorationBuffer); |
| 2183 } |
| 2184 if (layout.fBuiltin >= 0) { |
| 2185 this->writeInstruction(SpvOpDecorate, target, SpvDecorationBuiltIn, layo
ut.fBuiltin, |
| 2186 fDecorationBuffer); |
| 2187 } |
| 2188 } |
| 2189 |
| 2190 void SPIRVCodeGenerator::writeLayout(const Layout& layout, SpvId target, int mem
ber) { |
| 2191 if (layout.fLocation >= 0) { |
| 2192 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecoratio
nLocation, |
| 2193 layout.fLocation, fDecorationBuffer); |
| 2194 } |
| 2195 if (layout.fBinding >= 0) { |
| 2196 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecoratio
nBinding, |
| 2197 layout.fBinding, fDecorationBuffer); |
| 2198 } |
| 2199 if (layout.fIndex >= 0) { |
| 2200 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecoratio
nIndex, |
| 2201 layout.fIndex, fDecorationBuffer); |
| 2202 } |
| 2203 if (layout.fSet >= 0) { |
| 2204 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecoratio
nDescriptorSet, |
| 2205 layout.fSet, fDecorationBuffer); |
| 2206 } |
| 2207 if (layout.fBuiltin >= 0) { |
| 2208 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecoratio
nBuiltIn, |
| 2209 layout.fBuiltin, fDecorationBuffer); |
| 2210 } |
| 2211 } |
| 2212 |
| 2213 SpvId SPIRVCodeGenerator::writeInterfaceBlock(InterfaceBlock& intf) { |
| 2214 SpvId type = this->getType(intf.fVariable->fType); |
| 2215 SpvId result = this->nextId(); |
| 2216 this->writeInstruction(SpvOpDecorate, type, SpvDecorationBlock, fDecorationB
uffer); |
| 2217 SpvStorageClass_ storageClass = get_storage_class(intf.fVariable->fModifiers
); |
| 2218 SpvId ptrType = this->nextId(); |
| 2219 this->writeInstruction(SpvOpTypePointer, ptrType, storageClass, type, fConst
antBuffer); |
| 2220 this->writeInstruction(SpvOpVariable, ptrType, result, storageClass, fConsta
ntBuffer); |
| 2221 this->writeLayout(intf.fVariable->fModifiers.fLayout, result); |
| 2222 fVariableMap[intf.fVariable] = result; |
| 2223 return result; |
| 2224 } |
| 2225 |
| 2226 void SPIRVCodeGenerator::writeGlobalVars(VarDeclaration& decl, std::ostream& out
) { |
| 2227 for (size_t i = 0; i < decl.fVars.size(); i++) { |
| 2228 if (!decl.fVars[i]->fIsReadFrom && !decl.fVars[i]->fIsWrittenTo) { |
| 2229 continue; |
| 2230 } |
| 2231 SpvStorageClass_ storageClass; |
| 2232 if (decl.fVars[i]->fModifiers.fBits & Modifiers::kIn_Bit) { |
| 2233 storageClass = SpvStorageClassInput; |
| 2234 } else if (decl.fVars[i]->fModifiers.fBits & Modifiers::kOut_Bit) { |
| 2235 storageClass = SpvStorageClassOutput; |
| 2236 } else if (decl.fVars[i]->fModifiers.fBits & Modifiers::kUniform_Bit) { |
| 2237 if (decl.fVars[i]->fType->kind() == Type::kSampler_Kind) { |
| 2238 storageClass = SpvStorageClassUniformConstant; |
| 2239 } else { |
| 2240 storageClass = SpvStorageClassUniform; |
| 2241 } |
| 2242 } else { |
| 2243 storageClass = SpvStorageClassPrivate; |
| 2244 } |
| 2245 SpvId id = this->nextId(); |
| 2246 fVariableMap[decl.fVars[i]] = id; |
| 2247 SpvId type = this->getPointerType(decl.fVars[i]->fType, storageClass); |
| 2248 this->writeInstruction(SpvOpVariable, type, id, storageClass, fConstantB
uffer); |
| 2249 this->writeInstruction(SpvOpName, id, decl.fVars[i]->fName.c_str(), fNam
eBuffer); |
| 2250 if (decl.fVars[i]->fType->kind() == Type::kMatrix_Kind) { |
| 2251 this->writeInstruction(SpvOpMemberDecorate, id, i, SpvDecorationColM
ajor, |
| 2252 fDecorationBuffer); |
| 2253 this->writeInstruction(SpvOpMemberDecorate, id, i, SpvDecorationMatr
ixStride, |
| 2254 decl.fVars[i]->fType->stride(), fDecorationBu
ffer); |
| 2255 } |
| 2256 if (decl.fValues[i] != nullptr) { |
| 2257 ASSERT(!fCurrentBlock); |
| 2258 fCurrentBlock = -1; |
| 2259 SpvId value = this->writeExpression(*decl.fValues[i], fGlobalInitial
izersBuffer); |
| 2260 this->writeInstruction(SpvOpStore, id, value, fGlobalInitializersBuf
fer); |
| 2261 fCurrentBlock = 0; |
| 2262 } |
| 2263 this->writeLayout(decl.fVars[i]->fModifiers.fLayout, id); |
| 2264 } |
| 2265 } |
| 2266 |
| 2267 void SPIRVCodeGenerator::writeVarDeclaration(VarDeclaration& decl, std::ostream&
out) { |
| 2268 for (size_t i = 0; i < decl.fVars.size(); i++) { |
| 2269 SpvId id = this->nextId(); |
| 2270 fVariableMap[decl.fVars[i]] = id; |
| 2271 SpvId type = this->getPointerType(decl.fVars[i]->fType, SpvStorageClassF
unction); |
| 2272 this->writeInstruction(SpvOpVariable, type, id, SpvStorageClassFunction,
fVariableBuffer); |
| 2273 this->writeInstruction(SpvOpName, id, decl.fVars[i]->fName.c_str(), fNam
eBuffer); |
| 2274 if (decl.fValues[i] != nullptr) { |
| 2275 SpvId value = this->writeExpression(*decl.fValues[i], out); |
| 2276 this->writeInstruction(SpvOpStore, id, value, out); |
| 2277 } |
| 2278 } |
| 2279 } |
| 2280 |
| 2281 void SPIRVCodeGenerator::writeStatement(Statement& s, std::ostream& out) { |
| 2282 switch (s.fKind) { |
| 2283 case Statement::kBlock_Kind: |
| 2284 this->writeBlock((Block&) s, out); |
| 2285 break; |
| 2286 case Statement::kExpression_Kind: |
| 2287 this->writeExpression(*((ExpressionStatement&) s).fExpression, out); |
| 2288 break; |
| 2289 case Statement::kReturn_Kind: |
| 2290 this->writeReturnStatement((ReturnStatement&) s, out); |
| 2291 break; |
| 2292 case Statement::kVarDeclaration_Kind: |
| 2293 this->writeVarDeclaration(*((VarDeclarationStatement&) s).fDeclarati
on, out); |
| 2294 break; |
| 2295 case Statement::kIf_Kind: |
| 2296 this->writeIfStatement((IfStatement&) s, out); |
| 2297 break; |
| 2298 case Statement::kFor_Kind: |
| 2299 this->writeForStatement((ForStatement&) s, out); |
| 2300 break; |
| 2301 case Statement::kBreak_Kind: |
| 2302 this->writeInstruction(SpvOpBranch, fBreakTarget.top(), out); |
| 2303 break; |
| 2304 case Statement::kContinue_Kind: |
| 2305 this->writeInstruction(SpvOpBranch, fContinueTarget.top(), out); |
| 2306 break; |
| 2307 case Statement::kDiscard_Kind: |
| 2308 this->writeInstruction(SpvOpKill, out); |
| 2309 break; |
| 2310 default: |
| 2311 printf("unsupported statement: %s\n", s.description().c_str()); |
| 2312 ASSERT(false); |
| 2313 } |
| 2314 } |
| 2315 |
| 2316 void SPIRVCodeGenerator::writeBlock(Block& b, std::ostream& out) { |
| 2317 for (size_t i = 0; i < b.fStatements.size(); i++) { |
| 2318 this->writeStatement(*b.fStatements[i], out); |
| 2319 } |
| 2320 } |
| 2321 |
| 2322 void SPIRVCodeGenerator::writeIfStatement(IfStatement& stmt, std::ostream& out)
{ |
| 2323 SpvId test = this->writeExpression(*stmt.fTest, out); |
| 2324 SpvId ifTrue = this->nextId(); |
| 2325 SpvId ifFalse = this->nextId(); |
| 2326 if (stmt.fIfFalse) { |
| 2327 SpvId end = this->nextId(); |
| 2328 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMask
None, out); |
| 2329 this->writeInstruction(SpvOpBranchConditional, test, ifTrue, ifFalse, ou
t); |
| 2330 this->writeLabel(ifTrue, out); |
| 2331 this->writeStatement(*stmt.fIfTrue, out); |
| 2332 if (fCurrentBlock) { |
| 2333 this->writeInstruction(SpvOpBranch, end, out); |
| 2334 } |
| 2335 this->writeLabel(ifFalse, out); |
| 2336 this->writeStatement(*stmt.fIfFalse, out); |
| 2337 if (fCurrentBlock) { |
| 2338 this->writeInstruction(SpvOpBranch, end, out); |
| 2339 } |
| 2340 this->writeLabel(end, out); |
| 2341 } else { |
| 2342 this->writeInstruction(SpvOpSelectionMerge, ifFalse, SpvSelectionControl
MaskNone, out); |
| 2343 this->writeInstruction(SpvOpBranchConditional, test, ifTrue, ifFalse, ou
t); |
| 2344 this->writeLabel(ifTrue, out); |
| 2345 this->writeStatement(*stmt.fIfTrue, out); |
| 2346 if (fCurrentBlock) { |
| 2347 this->writeInstruction(SpvOpBranch, ifFalse, out); |
| 2348 } |
| 2349 this->writeLabel(ifFalse, out); |
| 2350 } |
| 2351 } |
| 2352 |
| 2353 void SPIRVCodeGenerator::writeForStatement(ForStatement& f, std::ostream& out) { |
| 2354 if (f.fInitializer) { |
| 2355 this->writeStatement(*f.fInitializer, out); |
| 2356 } |
| 2357 SpvId header = this->nextId(); |
| 2358 SpvId start = this->nextId(); |
| 2359 SpvId body = this->nextId(); |
| 2360 SpvId next = this->nextId(); |
| 2361 fContinueTarget.push(next); |
| 2362 SpvId end = this->nextId(); |
| 2363 fBreakTarget.push(end); |
| 2364 this->writeInstruction(SpvOpBranch, header, out); |
| 2365 this->writeLabel(header, out); |
| 2366 this->writeInstruction(SpvOpLoopMerge, end, next, SpvLoopControlMaskNone, ou
t); |
| 2367 this->writeInstruction(SpvOpBranch, start, out); |
| 2368 this->writeLabel(start, out); |
| 2369 SpvId test = this->writeExpression(*f.fTest, out); |
| 2370 this->writeInstruction(SpvOpBranchConditional, test, body, end, out); |
| 2371 this->writeLabel(body, out); |
| 2372 this->writeStatement(*f.fStatement, out); |
| 2373 if (fCurrentBlock) { |
| 2374 this->writeInstruction(SpvOpBranch, next, out); |
| 2375 } |
| 2376 this->writeLabel(next, out); |
| 2377 if (f.fNext) { |
| 2378 this->writeExpression(*f.fNext, out); |
| 2379 } |
| 2380 this->writeInstruction(SpvOpBranch, header, out); |
| 2381 this->writeLabel(end, out); |
| 2382 fBreakTarget.pop(); |
| 2383 fContinueTarget.pop(); |
| 2384 } |
| 2385 |
| 2386 void SPIRVCodeGenerator::writeReturnStatement(ReturnStatement& r, std::ostream&
out) { |
| 2387 if (r.fExpression) { |
| 2388 this->writeInstruction(SpvOpReturnValue, this->writeExpression(*r.fExpre
ssion, out), |
| 2389 out); |
| 2390 } else { |
| 2391 this->writeInstruction(SpvOpReturn, out); |
| 2392 } |
| 2393 } |
| 2394 |
| 2395 void SPIRVCodeGenerator::writeInstructions(Program& program, std::ostream& out)
{ |
| 2396 fGLSLExtendedInstructions = this->nextId(); |
| 2397 std::stringstream body; |
| 2398 std::vector<SpvId> interfaceVars; |
| 2399 // assign IDs to functions |
| 2400 for (size_t i = 0; i < program.fElements.size(); i++) { |
| 2401 if (program.fElements[i]->fKind == ProgramElement::kFunction_Kind) { |
| 2402 FunctionDefinition& f = (FunctionDefinition&) *program.fElements[i]; |
| 2403 fFunctionMap[f.fDeclaration] = this->nextId(); |
| 2404 } |
| 2405 } |
| 2406 for (size_t i = 0; i < program.fElements.size(); i++) { |
| 2407 if (program.fElements[i]->fKind == ProgramElement::kInterfaceBlock_Kind)
{ |
| 2408 InterfaceBlock& intf = (InterfaceBlock&) *program.fElements[i]; |
| 2409 SpvId id = this->writeInterfaceBlock(intf); |
| 2410 if ((intf.fVariable->fModifiers.fBits & Modifiers::kIn_Bit) || |
| 2411 (intf.fVariable->fModifiers.fBits & Modifiers::kOut_Bit)) { |
| 2412 interfaceVars.push_back(id); |
| 2413 } |
| 2414 } |
| 2415 } |
| 2416 for (size_t i = 0; i < program.fElements.size(); i++) { |
| 2417 if (program.fElements[i]->fKind == ProgramElement::kVar_Kind) { |
| 2418 this->writeGlobalVars(((VarDeclaration&) *program.fElements[i]), bod
y); |
| 2419 } |
| 2420 } |
| 2421 for (size_t i = 0; i < program.fElements.size(); i++) { |
| 2422 if (program.fElements[i]->fKind == ProgramElement::kFunction_Kind) { |
| 2423 this->writeFunction(((FunctionDefinition&) *program.fElements[i]), b
ody); |
| 2424 } |
| 2425 } |
| 2426 std::shared_ptr<FunctionDeclaration> main = nullptr; |
| 2427 for (auto entry : fFunctionMap) { |
| 2428 if (entry.first->fName == "main") { |
| 2429 main = entry.first; |
| 2430 } |
| 2431 } |
| 2432 ASSERT(main != nullptr); |
| 2433 for (auto entry : fVariableMap) { |
| 2434 std::shared_ptr<Variable> var = entry.first; |
| 2435 if (var->fStorage == Variable::kGlobal_Storage && |
| 2436 ((var->fModifiers.fBits & Modifiers::kIn_Bit) || |
| 2437 (var->fModifiers.fBits & Modifiers::kOut_Bit))) { |
| 2438 interfaceVars.push_back(entry.second); |
| 2439 } |
| 2440 } |
| 2441 this->writeCapabilities(out); |
| 2442 this->writeInstruction(SpvOpExtInstImport, fGLSLExtendedInstructions, "GLSL.
std.450", out); |
| 2443 this->writeInstruction(SpvOpMemoryModel, SpvAddressingModelLogical, SpvMemor
yModelGLSL450, out); |
| 2444 this->writeOpCode(SpvOpEntryPoint, 3 + (strlen(main->fName.c_str()) + 4) / 4
+ |
| 2445 (int32_t) interfaceVars.size(), out); |
| 2446 switch (program.fKind) { |
| 2447 case Program::kVertex_Kind: |
| 2448 this->writeWord(SpvExecutionModelVertex, out); |
| 2449 break; |
| 2450 case Program::kFragment_Kind: |
| 2451 this->writeWord(SpvExecutionModelFragment, out); |
| 2452 break; |
| 2453 } |
| 2454 this->writeWord(fFunctionMap[main], out); |
| 2455 this->writeString(main->fName.c_str(), out); |
| 2456 for (int var : interfaceVars) { |
| 2457 this->writeWord(var, out); |
| 2458 } |
| 2459 if (program.fKind == Program::kFragment_Kind) { |
| 2460 this->writeInstruction(SpvOpExecutionMode, |
| 2461 fFunctionMap[main], |
| 2462 SpvExecutionModeOriginUpperLeft, |
| 2463 out); |
| 2464 } |
| 2465 for (size_t i = 0; i < program.fElements.size(); i++) { |
| 2466 if (program.fElements[i]->fKind == ProgramElement::kExtension_Kind) { |
| 2467 this->writeInstruction(SpvOpSourceExtension, |
| 2468 ((Extension&) *program.fElements[i]).fName.c_
str(), |
| 2469 out); |
| 2470 } |
| 2471 } |
| 2472 |
| 2473 out << fNameBuffer.str(); |
| 2474 out << fDecorationBuffer.str(); |
| 2475 out << fConstantBuffer.str(); |
| 2476 out << fExternalFunctionsBuffer.str(); |
| 2477 out << body.str(); |
| 2478 } |
| 2479 |
| 2480 void SPIRVCodeGenerator::generateCode(Program& program, std::ostream& out) { |
| 2481 this->writeWord(SpvMagicNumber, out); |
| 2482 this->writeWord(SpvVersion, out); |
| 2483 this->writeWord(SKSL_MAGIC, out); |
| 2484 std::stringstream buffer; |
| 2485 this->writeInstructions(program, buffer); |
| 2486 this->writeWord(fIdCount, out); |
| 2487 this->writeWord(0, out); // reserved, always zero |
| 2488 out << buffer.str(); |
| 2489 } |
| 2490 |
| 2491 } |
OLD | NEW |