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 SPIRV_DEBUG 0 |
| 22 |
| 23 static const int32_t SKSL_MAGIC = 0x0; // FIXME: we should probably register a
magic number |
| 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 SPIRV_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.fFlags & Modifiers::kOut_Flag) != 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 SPIRV_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 SPIRV_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 for (int32_t i = 0; i < (int32_t) type->fields().size(); i++) { |
| 991 size_t size = type->fields()[i].fType->size(); |
| 992 size_t alignment = type->fields()[i].fType->alignment(); |
| 993 size_t mod = offset % alignment; |
| 994 if (mod != 0) { |
| 995 offset += alignment - mod; |
| 996 } |
| 997 this->writeInstruction(SpvOpMemberName, resultId, i, type->fields()[i].f
Name.c_str(), |
| 998 fNameBuffer); |
| 999 this->writeLayout(type->fields()[i].fModifiers.fLayout, resultId, i); |
| 1000 if (type->fields()[i].fModifiers.fLayout.fBuiltin < 0) { |
| 1001 this->writeInstruction(SpvOpMemberDecorate, resultId, (SpvId) i, Spv
DecorationOffset, |
| 1002 (SpvId) offset, fDecorationBuffer); |
| 1003 } |
| 1004 if (type->fields()[i].fType->kind() == Type::kMatrix_Kind) { |
| 1005 this->writeInstruction(SpvOpMemberDecorate, resultId, (SpvId) i, Spv
DecorationColMajor, |
| 1006 fDecorationBuffer); |
| 1007 this->writeInstruction(SpvOpMemberDecorate, resultId, (SpvId) i, |
| 1008 SpvDecorationMatrixStride, |
| 1009 (SpvId) type->fields()[i].fType->stride(), fD
ecorationBuffer); |
| 1010 } |
| 1011 offset += size; |
| 1012 Type::Kind kind = type->fields()[i].fType->kind(); |
| 1013 if ((kind == Type::kArray_Kind || kind == Type::kStruct_Kind) && offset
% alignment != 0) { |
| 1014 offset += alignment - offset % alignment; |
| 1015 } |
| 1016 ASSERT(offset % alignment == 0); |
| 1017 } |
| 1018 } |
| 1019 |
| 1020 SpvId SPIRVCodeGenerator::getType(std::shared_ptr<Type> type) { |
| 1021 auto entry = fTypeMap.find(type->name()); |
| 1022 if (entry == fTypeMap.end()) { |
| 1023 SpvId result = this->nextId(); |
| 1024 switch (type->kind()) { |
| 1025 case Type::kScalar_Kind: |
| 1026 if (type == kBool_Type) { |
| 1027 this->writeInstruction(SpvOpTypeBool, result, fConstantBuffe
r); |
| 1028 } |
| 1029 else if (type == kInt_Type) { |
| 1030 this->writeInstruction(SpvOpTypeInt, result, 32, 1, fConstan
tBuffer); |
| 1031 } |
| 1032 else if (type == kUInt_Type) { |
| 1033 this->writeInstruction(SpvOpTypeInt, result, 32, 0, fConstan
tBuffer); |
| 1034 } |
| 1035 else if (type == kFloat_Type) { |
| 1036 this->writeInstruction(SpvOpTypeFloat, result, 32, fConstant
Buffer); |
| 1037 } |
| 1038 else if (type == kDouble_Type) { |
| 1039 this->writeInstruction(SpvOpTypeFloat, result, 64, fConstant
Buffer); |
| 1040 } |
| 1041 else { |
| 1042 ASSERT(false); |
| 1043 } |
| 1044 break; |
| 1045 case Type::kVector_Kind: |
| 1046 this->writeInstruction(SpvOpTypeVector, result, |
| 1047 this->getType(type->componentType()), |
| 1048 type->columns(), fConstantBuffer); |
| 1049 break; |
| 1050 case Type::kMatrix_Kind: |
| 1051 this->writeInstruction(SpvOpTypeMatrix, result, this->getType(in
dex_type(type)), |
| 1052 type->columns(), fConstantBuffer); |
| 1053 break; |
| 1054 case Type::kStruct_Kind: |
| 1055 this->writeStruct(type, result); |
| 1056 break; |
| 1057 case Type::kArray_Kind: { |
| 1058 if (type->columns() > 0) { |
| 1059 IntLiteral count(Position(), type->columns()); |
| 1060 this->writeInstruction(SpvOpTypeArray, result, |
| 1061 this->getType(type->componentType()),
|
| 1062 this->writeIntLiteral(count), fConsta
ntBuffer); |
| 1063 this->writeInstruction(SpvOpDecorate, result, SpvDecorationA
rrayStride, |
| 1064 (SpvId) type->stride(), fDecorationBu
ffer); |
| 1065 } else { |
| 1066 printf("FIXME: creating a runtime-sized array. this won't ac
tually work yet!\n"); |
| 1067 this->writeInstruction(SpvOpTypeRuntimeArray, result, |
| 1068 this->getType(type->componentType()),
fConstantBuffer); |
| 1069 } |
| 1070 break; |
| 1071 } |
| 1072 case Type::kSampler_Kind: { |
| 1073 SpvId image = this->nextId(); |
| 1074 this->writeInstruction(SpvOpTypeImage, image, this->getType(kFlo
at_Type), |
| 1075 type->dimensions(), type->isDepth(), type
->isArrayed(), |
| 1076 type->isMultisampled(), type->isSampled()
, |
| 1077 SpvImageFormatUnknown, fConstantBuffer); |
| 1078 this->writeInstruction(SpvOpTypeSampledImage, result, image, fCo
nstantBuffer); |
| 1079 break; |
| 1080 } |
| 1081 default: |
| 1082 if (type == kVoid_Type) { |
| 1083 this->writeInstruction(SpvOpTypeVoid, result, fConstantBuffe
r); |
| 1084 } else { |
| 1085 printf("invalid type: %s\n", type->description().c_str()); |
| 1086 ASSERT(false); |
| 1087 } |
| 1088 } |
| 1089 fTypeMap[type->name()] = result; |
| 1090 return result; |
| 1091 } |
| 1092 return entry->second; |
| 1093 } |
| 1094 |
| 1095 SpvId SPIRVCodeGenerator::getFunctionType(std::shared_ptr<FunctionDeclaration> f
unction) { |
| 1096 std::string key = function->fReturnType->description() + "("; |
| 1097 std::string separator = ""; |
| 1098 for (size_t i = 0; i < function->fParameters.size(); i++) { |
| 1099 key += separator; |
| 1100 separator = ", "; |
| 1101 key += function->fParameters[i]->fType->description(); |
| 1102 } |
| 1103 key += ")"; |
| 1104 auto entry = fTypeMap.find(key); |
| 1105 if (entry == fTypeMap.end()) { |
| 1106 SpvId result = this->nextId(); |
| 1107 int32_t length = 3 + (int32_t) function->fParameters.size(); |
| 1108 SpvId returnType = this->getType(function->fReturnType); |
| 1109 std::vector<SpvId> parameterTypes; |
| 1110 for (size_t i = 0; i < function->fParameters.size(); i++) { |
| 1111 // glslang seems to treat all function parameters as pointers whethe
r they need to be or |
| 1112 // not. I was initially puzzled by this until I ran bizarre failure
s with certain |
| 1113 // patterns of function calls and control constructs, as exemplified
by this minimal |
| 1114 // failure case: |
| 1115 // |
| 1116 // void sphere(float x) { |
| 1117 // } |
| 1118 // |
| 1119 // void map() { |
| 1120 // sphere(1.0); |
| 1121 // } |
| 1122 // |
| 1123 // void main() { |
| 1124 // for (int i = 0; i < 1; i++) { |
| 1125 // map(); |
| 1126 // } |
| 1127 // } |
| 1128 // |
| 1129 // As of this writing, compiling this in the "obvious" way (with sph
ere taking a float) |
| 1130 // crashes. Making it take a float* and storing the parameter in a t
emporary variable, |
| 1131 // as glslang does, fixes it. |
| 1132 // if (is_out(function->fParameters[i])) { |
| 1133 parameterTypes.push_back(this->getPointerType(function->fParamet
ers[i]->fType, |
| 1134 SpvStorageClassFun
ction)); |
| 1135 // } else { |
| 1136 // parameterTypes.push_back(this->getType(function->fParameters[i
]->fType)); |
| 1137 // } |
| 1138 } |
| 1139 this->writeOpCode(SpvOpTypeFunction, length, fConstantBuffer); |
| 1140 this->writeWord(result, fConstantBuffer); |
| 1141 this->writeWord(returnType, fConstantBuffer); |
| 1142 for (SpvId id : parameterTypes) { |
| 1143 this->writeWord(id, fConstantBuffer); |
| 1144 } |
| 1145 fTypeMap[key] = result; |
| 1146 return result; |
| 1147 } |
| 1148 return entry->second; |
| 1149 } |
| 1150 |
| 1151 SpvId SPIRVCodeGenerator::getPointerType(std::shared_ptr<Type> type, |
| 1152 SpvStorageClass_ storageClass) { |
| 1153 std::string key = type->description() + "*" + to_string(storageClass); |
| 1154 auto entry = fTypeMap.find(key); |
| 1155 if (entry == fTypeMap.end()) { |
| 1156 SpvId result = this->nextId(); |
| 1157 this->writeInstruction(SpvOpTypePointer, result, storageClass, |
| 1158 this->getType(type), fConstantBuffer); |
| 1159 fTypeMap[key] = result; |
| 1160 return result; |
| 1161 } |
| 1162 return entry->second; |
| 1163 } |
| 1164 |
| 1165 SpvId SPIRVCodeGenerator::writeExpression(Expression& expr, std::ostream& out) { |
| 1166 switch (expr.fKind) { |
| 1167 case Expression::kBinary_Kind: |
| 1168 return this->writeBinaryExpression((BinaryExpression&) expr, out); |
| 1169 case Expression::kBoolLiteral_Kind: |
| 1170 return this->writeBoolLiteral((BoolLiteral&) expr); |
| 1171 case Expression::kConstructor_Kind: |
| 1172 return this->writeConstructor((Constructor&) expr, out); |
| 1173 case Expression::kIntLiteral_Kind: |
| 1174 return this->writeIntLiteral((IntLiteral&) expr); |
| 1175 case Expression::kFieldAccess_Kind: |
| 1176 return this->writeFieldAccess(((FieldAccess&) expr), out); |
| 1177 case Expression::kFloatLiteral_Kind: |
| 1178 return this->writeFloatLiteral(((FloatLiteral&) expr)); |
| 1179 case Expression::kFunctionCall_Kind: |
| 1180 return this->writeFunctionCall((FunctionCall&) expr, out); |
| 1181 case Expression::kPrefix_Kind: |
| 1182 return this->writePrefixExpression((PrefixExpression&) expr, out); |
| 1183 case Expression::kPostfix_Kind: |
| 1184 return this->writePostfixExpression((PostfixExpression&) expr, out); |
| 1185 case Expression::kSwizzle_Kind: |
| 1186 return this->writeSwizzle((Swizzle&) expr, out); |
| 1187 case Expression::kVariableReference_Kind: |
| 1188 return this->writeVariableReference((VariableReference&) expr, out); |
| 1189 case Expression::kTernary_Kind: |
| 1190 return this->writeTernaryExpression((TernaryExpression&) expr, out); |
| 1191 case Expression::kIndex_Kind: |
| 1192 return this->writeIndexExpression((IndexExpression&) expr, out); |
| 1193 default: |
| 1194 printf("unsupported expression: %s\n", expr.description().c_str()); |
| 1195 ASSERT(false); |
| 1196 } |
| 1197 return -1; |
| 1198 } |
| 1199 |
| 1200 SpvId SPIRVCodeGenerator::writeIntrinsicCall(FunctionCall& c, std::ostream& out)
{ |
| 1201 auto intrinsic = fIntrinsicMap.find(c.fFunction->fName); |
| 1202 ASSERT(intrinsic != fIntrinsicMap.end()); |
| 1203 SpvId result = this->nextId(); |
| 1204 std::shared_ptr<Type> type = c.fParameters[0]->fType; |
| 1205 int32_t intrinsicId; |
| 1206 if (std::get<0>(intrinsic->second) == kSpecial_IntrinsicKind || is_float(typ
e)) { |
| 1207 intrinsicId = std::get<1>(intrinsic->second); |
| 1208 } else if (is_signed(type)) { |
| 1209 intrinsicId = std::get<2>(intrinsic->second); |
| 1210 } else if (is_unsigned(type)) { |
| 1211 intrinsicId = std::get<3>(intrinsic->second); |
| 1212 } else if (is_bool(type)) { |
| 1213 intrinsicId = std::get<4>(intrinsic->second); |
| 1214 } else { |
| 1215 ABORT("invalid call %s, cannot operate on '%s'\n", c.description().c_str
(), |
| 1216 type->description().c_str()); |
| 1217 } |
| 1218 switch (std::get<0>(intrinsic->second)) { |
| 1219 case kGLSL_STD_450_IntrinsicKind: { |
| 1220 std::vector<SpvId> parameters; |
| 1221 for (size_t i = 0; i < c.fParameters.size(); i++) { |
| 1222 parameters.push_back(this->writeExpression(*c.fParameters[i], ou
t)); |
| 1223 } |
| 1224 this->writeOpCode(SpvOpExtInst, 5 + (int32_t) parameters.size(), out
); |
| 1225 this->writeWord(this->getType(c.fType), out); |
| 1226 this->writeWord(result, out); |
| 1227 this->writeWord(fGLSLExtendedInstructions, out); |
| 1228 this->writeWord(intrinsicId, out); |
| 1229 for (SpvId id : parameters) { |
| 1230 this->writeWord(id, out); |
| 1231 } |
| 1232 return result; |
| 1233 } |
| 1234 case kSPIRV_IntrinsicKind: { |
| 1235 std::vector<SpvId> parameters; |
| 1236 for (size_t i = 0; i < c.fParameters.size(); i++) { |
| 1237 parameters.push_back(this->writeExpression(*c.fParameters[i], ou
t)); |
| 1238 } |
| 1239 this->writeOpCode((SpvOp_) intrinsicId, 3 + (int32_t) parameters.siz
e(), out); |
| 1240 this->writeWord(this->getType(c.fType), out); |
| 1241 this->writeWord(result, out); |
| 1242 for (SpvId id : parameters) { |
| 1243 this->writeWord(id, out); |
| 1244 } |
| 1245 return result; |
| 1246 } |
| 1247 case kSpecial_IntrinsicKind: |
| 1248 return this->writeSpecialIntrinsic(c, (SpecialIntrinsic) intrinsicId
, out); |
| 1249 default: |
| 1250 ABORT("unsupported intrinsic kind"); |
| 1251 } |
| 1252 } |
| 1253 |
| 1254 SpvId SPIRVCodeGenerator::writeSpecialIntrinsic(FunctionCall& c, SpecialIntrinsi
c kind, |
| 1255 std::ostream& out) { |
| 1256 SpvId result = this->nextId(); |
| 1257 switch (kind) { |
| 1258 case kAtan_SpecialIntrinsic: { |
| 1259 std::vector<SpvId> parameters; |
| 1260 for (size_t i = 0; i < c.fParameters.size(); i++) { |
| 1261 parameters.push_back(this->writeExpression(*c.fParameters[i], ou
t)); |
| 1262 } |
| 1263 this->writeOpCode(SpvOpExtInst, 5 + (int32_t) parameters.size(), out
); |
| 1264 this->writeWord(this->getType(c.fType), out); |
| 1265 this->writeWord(result, out); |
| 1266 this->writeWord(fGLSLExtendedInstructions, out); |
| 1267 this->writeWord(parameters.size() == 2 ? GLSLstd450Atan2 : GLSLstd45
0Atan, out); |
| 1268 for (SpvId id : parameters) { |
| 1269 this->writeWord(id, out); |
| 1270 } |
| 1271 return result; |
| 1272 } |
| 1273 case kTexture_SpecialIntrinsic: { |
| 1274 SpvId type = this->getType(c.fType); |
| 1275 SpvId sampler = this->writeExpression(*c.fParameters[0],
out); |
| 1276 SpvId uv = this->writeExpression(*c.fParameters[1], out)
; |
| 1277 if (c.fParameters.size() == 3) { |
| 1278 this->writeInstruction(SpvOpImageSampleImplicitLod, type, result
, sampler, uv, |
| 1279 SpvImageOperandsBiasMask, |
| 1280 this->writeExpression(*c.fParameters[2],
out), |
| 1281 out); |
| 1282 } else { |
| 1283 ASSERT(c.fParameters.size() == 2); |
| 1284 this->writeInstruction(SpvOpImageSampleImplicitLod, type, result
, sampler, uv, out); |
| 1285 } |
| 1286 break; |
| 1287 } |
| 1288 case kTextureProj_SpecialIntrinsic: { |
| 1289 SpvId type = this->getType(c.fType); |
| 1290 SpvId sampler = this->writeExpression(*c.fParameters[0], out); |
| 1291 SpvId uv = this->writeExpression(*c.fParameters[1], out); |
| 1292 if (c.fParameters.size() == 3) { |
| 1293 this->writeInstruction(SpvOpImageSampleProjImplicitLod, type, re
sult, sampler, uv, |
| 1294 SpvImageOperandsBiasMask, |
| 1295 this->writeExpression(*c.fParameters[2],
out), |
| 1296 out); |
| 1297 } else { |
| 1298 ASSERT(c.fParameters.size() == 2); |
| 1299 this->writeInstruction(SpvOpImageSampleProjImplicitLod, type, re
sult, sampler, uv, |
| 1300 out); |
| 1301 } |
| 1302 break; |
| 1303 } |
| 1304 case kTexture2D_SpecialIntrinsic: |
| 1305 this->writeInstruction(SpvOpImageSampleImplicitLod, |
| 1306 this->getType(c.fType), |
| 1307 result, |
| 1308 this->writeExpression(*c.fParameters[0], out)
, |
| 1309 this->writeExpression(*c.fParameters[1], out)
, |
| 1310 out); |
| 1311 break; |
| 1312 } |
| 1313 return result; |
| 1314 } |
| 1315 |
| 1316 SpvId SPIRVCodeGenerator::writeFunctionCall(FunctionCall& c, std::ostream& out)
{ |
| 1317 auto entry = fFunctionMap.find(c.fFunction); |
| 1318 if (entry == fFunctionMap.end()) { |
| 1319 return this->writeIntrinsicCall(c, out); |
| 1320 } |
| 1321 std::vector<SpvId> parameters; |
| 1322 for (size_t i = 0; i < c.fParameters.size(); i++) { |
| 1323 if (is_out(c.fFunction->fParameters[i])) { |
| 1324 parameters.push_back(this->getLValue(*c.fParameters[i], out)); |
| 1325 } else { |
| 1326 SpvId expr = this->writeExpression(*c.fParameters[i], out); |
| 1327 SpvId tmpVar = this->nextId(); |
| 1328 this->writeInstruction(SpvOpVariable, |
| 1329 this->getPointerType(c.fParameters[i]->fType,
|
| 1330 SpvStorageClassFunction)
, |
| 1331 tmpVar, |
| 1332 SpvStorageClassFunction, |
| 1333 out); |
| 1334 this->writeInstruction(SpvOpStore, tmpVar, expr, out); |
| 1335 parameters.push_back(tmpVar); |
| 1336 } |
| 1337 } |
| 1338 SpvId result = this->nextId(); |
| 1339 this->writeOpCode(SpvOpFunctionCall, 4 + (int32_t) c.fParameters.size(), out
); |
| 1340 this->writeWord(this->getType(c.fType), out); |
| 1341 this->writeWord(result, out); |
| 1342 this->writeWord(entry->second, out); |
| 1343 for (SpvId id : parameters) { |
| 1344 this->writeWord(id, out); |
| 1345 } |
| 1346 return result; |
| 1347 } |
| 1348 |
| 1349 SpvId SPIRVCodeGenerator::writeConstructor(Constructor& c, std::ostream& out) { |
| 1350 SpvId result = this->nextId(); |
| 1351 if (c.fType->kind() == Type::kVector_Kind && c.isConstant()) { |
| 1352 std::vector<SpvId> parameters; |
| 1353 for (size_t i = 0; i < c.fParameters.size(); i++) { |
| 1354 parameters.push_back(this->writeExpression(*c.fParameters[i], fConst
antBuffer)); |
| 1355 } |
| 1356 SpvId type = this->getType(c.fType); |
| 1357 if (c.fParameters.size() == 1) { |
| 1358 this->writeOpCode(SpvOpConstantComposite, 3 + c.fType->columns(), fC
onstantBuffer); |
| 1359 this->writeWord(type, fConstantBuffer); |
| 1360 this->writeWord(result, fConstantBuffer); |
| 1361 for (int i = 0; i < c.fType->columns(); i++) { |
| 1362 this->writeWord(parameters[0], fConstantBuffer); |
| 1363 } |
| 1364 } else { |
| 1365 this->writeOpCode(SpvOpConstantComposite, 3 + (int32_t) c.fParameter
s.size(), |
| 1366 fConstantBuffer); |
| 1367 this->writeWord(type, fConstantBuffer); |
| 1368 this->writeWord(result, fConstantBuffer); |
| 1369 for (SpvId id : parameters) { |
| 1370 this->writeWord(id, fConstantBuffer); |
| 1371 } |
| 1372 } |
| 1373 return result; |
| 1374 } |
| 1375 if (c.fType == kFloat_Type) { |
| 1376 ASSERT(c.fParameters.size() == 1); |
| 1377 ASSERT(c.fParameters[0]->fType->isNumber()); |
| 1378 SpvId parameter = this->writeExpression(*c.fParameters[0], out); |
| 1379 if (c.fParameters[0]->fType == kInt_Type) { |
| 1380 this->writeInstruction(SpvOpConvertSToF, this->getType(c.fType), res
ult, parameter, |
| 1381 out); |
| 1382 } else if (c.fParameters[0]->fType == kUInt_Type) { |
| 1383 this->writeInstruction(SpvOpConvertUToF, this->getType(c.fType), res
ult, parameter, |
| 1384 out); |
| 1385 } else if (c.fParameters[0]->fType == kFloat_Type) { |
| 1386 return parameter; |
| 1387 } |
| 1388 return result; |
| 1389 } |
| 1390 else if (c.fType == kInt_Type) { |
| 1391 ASSERT(c.fParameters.size() == 1); |
| 1392 ASSERT(c.fParameters[0]->fType->isNumber()); |
| 1393 SpvId parameter = this->writeExpression(*c.fParameters[0], out); |
| 1394 if (c.fParameters[0]->fType == kFloat_Type) { |
| 1395 this->writeInstruction(SpvOpConvertFToS, this->getType(c.fType), res
ult, parameter, |
| 1396 out); |
| 1397 } else if (c.fParameters[0]->fType == kUInt_Type) { |
| 1398 this->writeInstruction(SpvOpSatConvertUToS, this->getType(c.fType),
result, parameter, |
| 1399 out); |
| 1400 } else if (c.fParameters[0]->fType == kInt_Type) { |
| 1401 return parameter; |
| 1402 } |
| 1403 return result; |
| 1404 } |
| 1405 ASSERT(!c.fType->isNumber()); |
| 1406 std::vector<SpvId> parameters; |
| 1407 for (size_t i = 0; i < c.fParameters.size(); i++) { |
| 1408 parameters.push_back(this->writeExpression(*c.fParameters[i], out)); |
| 1409 } |
| 1410 int rows = c.fType->rows(); |
| 1411 int columns = c.fType->columns(); |
| 1412 if (parameters.size() == 1) { |
| 1413 if (c.fType->kind() == Type::kVector_Kind) { |
| 1414 this->writeOpCode(SpvOpCompositeConstruct, 3 + c.fType->columns(), o
ut); |
| 1415 this->writeWord(this->getType(c.fType), out); |
| 1416 this->writeWord(result, out); |
| 1417 for (int i = 0; i < c.fType->columns(); i++) { |
| 1418 this->writeWord(parameters[0], out); |
| 1419 } |
| 1420 } else { |
| 1421 ASSERT(c.fType->kind() == Type::kMatrix_Kind); |
| 1422 // FIXME this won't work for int matrices |
| 1423 FloatLiteral zero(Position(), 0); |
| 1424 SpvId zeroId = this->writeFloatLiteral(zero); |
| 1425 std::vector<SpvId> columnIds; |
| 1426 for (int column = 0; column < columns; column++) { |
| 1427 this->writeOpCode(SpvOpCompositeConstruct, 3 + c.fType->rows(), |
| 1428 out); |
| 1429 this->writeWord(this->getType(c.fType->componentType()->toCompou
nd(rows, 1)), out); |
| 1430 for (int row = 0; row < c.fType->columns(); row++) { |
| 1431 this->writeWord(row == column ? parameters[0] : zeroId, out)
; |
| 1432 } |
| 1433 } |
| 1434 this->writeOpCode(SpvOpCompositeConstruct, 3 + columns, |
| 1435 out); |
| 1436 this->writeWord(this->getType(c.fType), out); |
| 1437 for (SpvId id : columnIds) { |
| 1438 this->writeWord(id, out); |
| 1439 } |
| 1440 this->writeWord(result, out); |
| 1441 } |
| 1442 } else if (c.fType->kind() == Type::kMatrix_Kind) { |
| 1443 std::vector<SpvId> columnIds; |
| 1444 int currentCount = 0; |
| 1445 for (size_t i = 0; i < parameters.size(); i++) { |
| 1446 if (c.fParameters[i]->fType->kind() == Type::kVector_Kind) { |
| 1447 ASSERT(currentCount == 0); |
| 1448 columnIds.push_back(parameters[i]); |
| 1449 currentCount = 0; |
| 1450 } else { |
| 1451 ASSERT(c.fParameters[i]->fType->kind() == Type::kScalar_Kind); |
| 1452 if (currentCount == 0) { |
| 1453 this->writeOpCode(SpvOpCompositeConstruct, 3 + c.fType->rows
(), out); |
| 1454 this->writeWord(this->getType(c.fType->componentType()->toCo
mpound(rows, 1)), |
| 1455 out); |
| 1456 SpvId id = this->nextId(); |
| 1457 this->writeWord(id, out); |
| 1458 columnIds.push_back(id); |
| 1459 } |
| 1460 this->writeWord(parameters[i], out); |
| 1461 currentCount = (currentCount + 1) % rows; |
| 1462 } |
| 1463 } |
| 1464 ASSERT(columnIds.size() == columns) |
| 1465 this->writeOpCode(SpvOpCompositeConstruct, 3 + columns, out); |
| 1466 this->writeWord(this->getType(c.fType), out); |
| 1467 this->writeWord(result, out); |
| 1468 for (SpvId id : columnIds) { |
| 1469 this->writeWord(id, out); |
| 1470 } |
| 1471 } else { |
| 1472 this->writeOpCode(SpvOpCompositeConstruct, 3 + (int32_t) c.fParameters.s
ize(), out); |
| 1473 this->writeWord(this->getType(c.fType), out); |
| 1474 this->writeWord(result, out); |
| 1475 for (SpvId id : parameters) { |
| 1476 this->writeWord(id, out); |
| 1477 } |
| 1478 } |
| 1479 return result; |
| 1480 } |
| 1481 |
| 1482 SpvStorageClass_ get_storage_class(const Modifiers& modifiers) { |
| 1483 if (modifiers.fFlags & Modifiers::kIn_Flag) { |
| 1484 return SpvStorageClassInput; |
| 1485 } else if (modifiers.fFlags & Modifiers::kOut_Flag) { |
| 1486 return SpvStorageClassOutput; |
| 1487 } else if (modifiers.fFlags & Modifiers::kUniform_Flag) { |
| 1488 return SpvStorageClassUniform; |
| 1489 } else { |
| 1490 return SpvStorageClassFunction; |
| 1491 } |
| 1492 } |
| 1493 |
| 1494 SpvStorageClass_ get_storage_class(Expression& expr) { |
| 1495 switch (expr.fKind) { |
| 1496 case Expression::kVariableReference_Kind: |
| 1497 return get_storage_class(((VariableReference&) expr).fVariable->fMod
ifiers); |
| 1498 case Expression::kFieldAccess_Kind: |
| 1499 return get_storage_class(*((FieldAccess&) expr).fBase); |
| 1500 case Expression::kIndex_Kind: |
| 1501 return get_storage_class(*((IndexExpression&) expr).fBase); |
| 1502 default: |
| 1503 return SpvStorageClassFunction; |
| 1504 } |
| 1505 } |
| 1506 |
| 1507 SpvId SPIRVCodeGenerator::getLValue(Expression& value, std::ostream& out) { |
| 1508 switch (value.fKind) { |
| 1509 case Expression::kVariableReference_Kind: { |
| 1510 std::shared_ptr<Variable> var = ((VariableReference&) value).fVariab
le; |
| 1511 auto entry = fVariableMap.find(var); |
| 1512 ASSERT(entry != fVariableMap.end()); |
| 1513 return entry->second; |
| 1514 } |
| 1515 case Expression::kIndex_Kind: { |
| 1516 IndexExpression& index = (IndexExpression&) value; |
| 1517 SpvId base = this->getLValue(*index.fBase, out);; |
| 1518 SpvId result = this->nextId(); |
| 1519 this->writeInstruction(SpvOpAccessChain, |
| 1520 this->getPointerType(index.fType, |
| 1521 get_storage_class(*index
.fBase)), |
| 1522 result, |
| 1523 base, |
| 1524 this->writeExpression(*index.fIndex, out), |
| 1525 out); |
| 1526 return result; |
| 1527 } |
| 1528 case Expression::kFieldAccess_Kind: { |
| 1529 FieldAccess& f = (FieldAccess&) value; |
| 1530 SpvId base = this->getLValue(*f.fBase, out);; |
| 1531 IntLiteral index(Position(), f.fFieldIndex); |
| 1532 SpvId result = this->nextId(); |
| 1533 this->writeInstruction(SpvOpAccessChain, |
| 1534 this->getPointerType(f.fType, |
| 1535 get_storage_class(*f.fBa
se)), |
| 1536 result, |
| 1537 base, |
| 1538 this->writeIntLiteral(index), |
| 1539 out); |
| 1540 return result; |
| 1541 } |
| 1542 default: |
| 1543 SpvId result = this->nextId(); |
| 1544 SpvId type = this->getPointerType(value.fType, SpvStorageClassFuncti
on); |
| 1545 this->writeInstruction(SpvOpVariable, type, result, SpvStorageClassF
unction, out); |
| 1546 this->writeInstruction(SpvOpStore, result, this->writeExpression(val
ue, out), out); |
| 1547 return result; |
| 1548 } |
| 1549 } |
| 1550 |
| 1551 void SPIRVCodeGenerator::storeToLValue(Expression& lvalue, SpvId value, std::ost
ream& out) { |
| 1552 switch (lvalue.fKind) { |
| 1553 case Expression::kVariableReference_Kind: // fall through |
| 1554 case Expression::kIndex_Kind: // fall through |
| 1555 case Expression::kFieldAccess_Kind: { |
| 1556 SpvId id = this->getLValue(lvalue, out); |
| 1557 this->writeInstruction(SpvOpStore, id, value, out); |
| 1558 break; |
| 1559 } |
| 1560 case Expression::kSwizzle_Kind: { |
| 1561 Swizzle& swizzle = (Swizzle&) lvalue; |
| 1562 size_t count = swizzle.fComponents.size(); |
| 1563 SpvId base = this->getLValue(*swizzle.fBase, out); |
| 1564 if (count == 1) { |
| 1565 IntLiteral index(Position(), swizzle.fComponents[0]); |
| 1566 SpvId target = this->nextId(); |
| 1567 this->writeInstruction(SpvOpAccessChain, |
| 1568 this->getPointerType(swizzle.fType, |
| 1569 get_storage_class(*s
wizzle.fBase)), |
| 1570 target, |
| 1571 base, |
| 1572 this->writeIntLiteral(index), |
| 1573 out); |
| 1574 this->writeInstruction(SpvOpStore, target, value, out); |
| 1575 } else { |
| 1576 SpvId shuffle = this->nextId(); |
| 1577 std::shared_ptr<Type> baseType = swizzle.fBase->fType; |
| 1578 SpvId lhs = this->writeExpression(*swizzle.fBase, out); |
| 1579 this->writeOpCode(SpvOpVectorShuffle, 5 + baseType->columns(), o
ut); |
| 1580 this->writeWord(this->getType(baseType), out); |
| 1581 this->writeWord(shuffle, out); |
| 1582 this->writeWord(lhs, out); |
| 1583 this->writeWord(value, out); |
| 1584 for (int i = 0; i < baseType->columns(); i++) { |
| 1585 int offset = i; |
| 1586 for (size_t j = 0; j < swizzle.fComponents.size(); j++) { |
| 1587 if (swizzle.fComponents[j] == i) { |
| 1588 offset = (int) (j + baseType->columns()); |
| 1589 break; |
| 1590 } |
| 1591 } |
| 1592 this->writeWord(offset, out); |
| 1593 } |
| 1594 this->writeInstruction(SpvOpStore, base, shuffle, out); |
| 1595 } |
| 1596 break; |
| 1597 } |
| 1598 default: |
| 1599 printf("cannot use %s as an lvalue\n", lvalue.description().c_str())
; |
| 1600 ASSERT(false); |
| 1601 } |
| 1602 } |
| 1603 |
| 1604 SpvId SPIRVCodeGenerator::writeVariableReference(VariableReference& ref, std::os
tream& out) { |
| 1605 auto entry = fVariableMap.find(ref.fVariable); |
| 1606 ASSERT(entry != fVariableMap.end()); |
| 1607 SpvId var = entry->second; |
| 1608 SpvId result = this->nextId(); |
| 1609 this->writeInstruction(SpvOpLoad, this->getType(ref.fVariable->fType), resul
t, var, out); |
| 1610 return result; |
| 1611 } |
| 1612 |
| 1613 std::vector<SpvId> SPIRVCodeGenerator::getAccessChain(Expression& expr, std::ost
ream& out) { |
| 1614 std::vector<SpvId> chain; |
| 1615 switch (expr.fKind) { |
| 1616 case Expression::kIndex_Kind: { |
| 1617 IndexExpression& indexExpr = (IndexExpression&) expr; |
| 1618 chain = this->getAccessChain(*indexExpr.fBase, out); |
| 1619 chain.push_back(this->writeExpression(*indexExpr.fIndex, out)); |
| 1620 break; |
| 1621 } |
| 1622 case Expression::kFieldAccess_Kind: { |
| 1623 FieldAccess& fieldExpr = (FieldAccess&) expr; |
| 1624 chain = this->getAccessChain(*fieldExpr.fBase, out); |
| 1625 IntLiteral index(Position(), fieldExpr.fFieldIndex); |
| 1626 chain.push_back(this->writeIntLiteral(index)); |
| 1627 break; |
| 1628 } |
| 1629 default: |
| 1630 chain.push_back(this->getLValue(expr, out)); |
| 1631 } |
| 1632 return chain; |
| 1633 } |
| 1634 |
| 1635 SpvId SPIRVCodeGenerator::writeIndexExpression(IndexExpression& expr, std::ostre
am& out) { |
| 1636 std::vector<SpvId> chain = this->getAccessChain(expr, out); |
| 1637 SpvId member = this->nextId(); |
| 1638 this->writeOpCode(SpvOpAccessChain, (SpvId) (3 + chain.size()), out); |
| 1639 this->writeWord(this->getPointerType(expr.fType, get_storage_class(*expr.fBa
se)), out); |
| 1640 this->writeWord(member, out); |
| 1641 for (SpvId idx : chain) { |
| 1642 this->writeWord(idx, out); |
| 1643 } |
| 1644 SpvId result = this->nextId(); |
| 1645 this->writeInstruction(SpvOpLoad, this->getType(expr.fType), result, member,
out); |
| 1646 return result; |
| 1647 } |
| 1648 |
| 1649 SpvId SPIRVCodeGenerator::writeFieldAccess(FieldAccess& f, std::ostream& out) { |
| 1650 std::vector<SpvId> chain = this->getAccessChain(f, out); |
| 1651 SpvId member = this->nextId(); |
| 1652 this->writeOpCode(SpvOpAccessChain, (SpvId) (3 + chain.size()), out); |
| 1653 this->writeWord(this->getPointerType(f.fType, get_storage_class(*f.fBase)),
out); |
| 1654 this->writeWord(member, out); |
| 1655 for (SpvId idx : chain) { |
| 1656 this->writeWord(idx, out); |
| 1657 } |
| 1658 SpvId result = this->nextId(); |
| 1659 this->writeInstruction(SpvOpLoad, this->getType(f.fType), result, member, ou
t); |
| 1660 return result; |
| 1661 } |
| 1662 |
| 1663 SpvId SPIRVCodeGenerator::writeSwizzle(Swizzle& swizzle, std::ostream& out) { |
| 1664 SpvId base = this->writeExpression(*swizzle.fBase, out); |
| 1665 SpvId result = this->nextId(); |
| 1666 size_t count = swizzle.fComponents.size(); |
| 1667 if (count == 1) { |
| 1668 this->writeInstruction(SpvOpCompositeExtract, this->getType(swizzle.fTyp
e), result, base, |
| 1669 swizzle.fComponents[0], out); |
| 1670 } else { |
| 1671 this->writeOpCode(SpvOpVectorShuffle, 5 + (int32_t) count, out); |
| 1672 this->writeWord(this->getType(swizzle.fType), out); |
| 1673 this->writeWord(result, out); |
| 1674 this->writeWord(base, out); |
| 1675 this->writeWord(base, out); |
| 1676 for (int component : swizzle.fComponents) { |
| 1677 this->writeWord(component, out); |
| 1678 } |
| 1679 } |
| 1680 return result; |
| 1681 } |
| 1682 |
| 1683 SpvId SPIRVCodeGenerator::writeBinaryOperation(std::shared_ptr<Type> resultType,
|
| 1684 std::shared_ptr<Type> operandType
, SpvId lhs, |
| 1685 SpvId rhs, SpvOp_ ifFloat, SpvOp_
ifInt, |
| 1686 SpvOp_ ifUInt, SpvOp_ ifBool, std
::ostream& out) { |
| 1687 SpvId result = this->nextId(); |
| 1688 if (is_float(operandType)) { |
| 1689 this->writeInstruction(ifFloat, this->getType(resultType), result, lhs,
rhs, out); |
| 1690 } else if (is_signed(operandType)) { |
| 1691 this->writeInstruction(ifInt, this->getType(resultType), result, lhs, rh
s, out); |
| 1692 } else if (is_unsigned(operandType)) { |
| 1693 this->writeInstruction(ifUInt, this->getType(resultType), result, lhs, r
hs, out); |
| 1694 } else if (operandType == kBool_Type) { |
| 1695 this->writeInstruction(ifBool, this->getType(resultType), result, lhs, r
hs, out); |
| 1696 } else { |
| 1697 printf("invalid operandType: %s\n", operandType->description().c_str()); |
| 1698 ASSERT(false); |
| 1699 } |
| 1700 return result; |
| 1701 } |
| 1702 |
| 1703 SpvId SPIRVCodeGenerator::writeBinaryExpression(BinaryExpression& b, std::ostrea
m& out) { |
| 1704 // handle cases where we don't necessarily evaluate both LHS and RHS |
| 1705 switch (b.fOperator) { |
| 1706 case Token::EQ: { |
| 1707 SpvId rhs = this->writeExpression(*b.fRight, out); |
| 1708 this->storeToLValue(*b.fLeft, rhs, out); |
| 1709 return rhs; |
| 1710 } |
| 1711 case Token::LOGICALAND: |
| 1712 return this->writeLogicalAnd(b, out); |
| 1713 case Token::LOGICALOR: |
| 1714 return this->writeLogicalOr(b, out); |
| 1715 default: |
| 1716 break; |
| 1717 } |
| 1718 |
| 1719 // "normal" operators |
| 1720 std::shared_ptr<Type> resultType = b.fType; |
| 1721 SpvId lhs = this->writeExpression(*b.fLeft, out); |
| 1722 SpvId rhs = this->writeExpression(*b.fRight, out); |
| 1723 // component type we are operating on: float, int, uint |
| 1724 std::shared_ptr<Type> operandType; |
| 1725 // IR allows mismatched types in expressions (e.g. vec2 * float), but they n
eed special handling |
| 1726 // in SPIR-V |
| 1727 if (b.fLeft->fType != b.fRight->fType) { |
| 1728 if (b.fLeft->fType->kind() == Type::kVector_Kind && |
| 1729 b.fRight->fType->isNumber()) { |
| 1730 // promote number to vector |
| 1731 SpvId vec = this->nextId(); |
| 1732 this->writeOpCode(SpvOpCompositeConstruct, 3 + b.fType->columns(), o
ut); |
| 1733 this->writeWord(this->getType(resultType), out); |
| 1734 this->writeWord(vec, out); |
| 1735 for (int i = 0; i < resultType->columns(); i++) { |
| 1736 this->writeWord(rhs, out); |
| 1737 } |
| 1738 rhs = vec; |
| 1739 operandType = b.fRight->fType; |
| 1740 } else if (b.fRight->fType->kind() == Type::kVector_Kind && |
| 1741 b.fLeft->fType->isNumber()) { |
| 1742 // promote number to vector |
| 1743 SpvId vec = this->nextId(); |
| 1744 this->writeOpCode(SpvOpCompositeConstruct, 3 + b.fType->columns(), o
ut); |
| 1745 this->writeWord(this->getType(resultType), out); |
| 1746 this->writeWord(vec, out); |
| 1747 for (int i = 0; i < resultType->columns(); i++) { |
| 1748 this->writeWord(lhs, out); |
| 1749 } |
| 1750 lhs = vec; |
| 1751 operandType = b.fLeft->fType; |
| 1752 } else if (b.fLeft->fType->kind() == Type::kMatrix_Kind) { |
| 1753 SpvOp_ op; |
| 1754 if (b.fRight->fType->kind() == Type::kMatrix_Kind) { |
| 1755 op = SpvOpMatrixTimesMatrix; |
| 1756 } else if (b.fRight->fType->kind() == Type::kVector_Kind) { |
| 1757 op = SpvOpMatrixTimesVector; |
| 1758 } else { |
| 1759 ASSERT(b.fRight->fType->kind() == Type::kScalar_Kind); |
| 1760 op = SpvOpMatrixTimesScalar; |
| 1761 } |
| 1762 SpvId result = this->nextId(); |
| 1763 this->writeInstruction(op, this->getType(b.fType), result, lhs, rhs,
out); |
| 1764 if (b.fOperator == Token::STAREQ) { |
| 1765 this->storeToLValue(*b.fLeft, result, out); |
| 1766 } else { |
| 1767 ASSERT(b.fOperator == Token::STAR); |
| 1768 } |
| 1769 return result; |
| 1770 } else if (b.fRight->fType->kind() == Type::kMatrix_Kind) { |
| 1771 SpvId result = this->nextId(); |
| 1772 if (b.fLeft->fType->kind() == Type::kVector_Kind) { |
| 1773 this->writeInstruction(SpvOpVectorTimesMatrix, this->getType(b.f
Type), result, lhs, |
| 1774 rhs, out); |
| 1775 } else { |
| 1776 ASSERT(b.fLeft->fType->kind() == Type::kScalar_Kind); |
| 1777 this->writeInstruction(SpvOpMatrixTimesScalar, this->getType(b.f
Type), result, rhs, |
| 1778 lhs, out); |
| 1779 } |
| 1780 if (b.fOperator == Token::STAREQ) { |
| 1781 this->storeToLValue(*b.fLeft, result, out); |
| 1782 } else { |
| 1783 ASSERT(b.fOperator == Token::STAR); |
| 1784 } |
| 1785 return result; |
| 1786 } else { |
| 1787 printf("unsupported binary expression: %s\n", b.description().c_str(
)); |
| 1788 ASSERT(false); |
| 1789 } |
| 1790 } else { |
| 1791 operandType = b.fLeft->fType; |
| 1792 ASSERT(operandType == b.fRight->fType); |
| 1793 } |
| 1794 switch (b.fOperator) { |
| 1795 case Token::EQEQ: |
| 1796 ASSERT(resultType == kBool_Type); |
| 1797 return this->writeBinaryOperation(resultType, operandType, lhs, rhs,
SpvOpFOrdEqual, |
| 1798 SpvOpIEqual, SpvOpIEqual, SpvOpLog
icalEqual, out); |
| 1799 case Token::NEQ: |
| 1800 ASSERT(resultType == kBool_Type); |
| 1801 return this->writeBinaryOperation(resultType, operandType, lhs, rhs,
SpvOpFOrdNotEqual, |
| 1802 SpvOpINotEqual, SpvOpINotEqual, Sp
vOpLogicalNotEqual, |
| 1803 out); |
| 1804 case Token::GT: |
| 1805 ASSERT(resultType == kBool_Type); |
| 1806 return this->writeBinaryOperation(resultType, operandType, lhs, rhs,
|
| 1807 SpvOpFOrdGreaterThan, SpvOpSGreate
rThan, |
| 1808 SpvOpUGreaterThan, SpvOpUndef, out
); |
| 1809 case Token::LT: |
| 1810 ASSERT(resultType == kBool_Type); |
| 1811 return this->writeBinaryOperation(resultType, operandType, lhs, rhs,
SpvOpFOrdLessThan, |
| 1812 SpvOpSLessThan, SpvOpULessThan, Sp
vOpUndef, out); |
| 1813 case Token::GTEQ: |
| 1814 ASSERT(resultType == kBool_Type); |
| 1815 return this->writeBinaryOperation(resultType, operandType, lhs, rhs,
|
| 1816 SpvOpFOrdGreaterThanEqual, SpvOpSG
reaterThanEqual, |
| 1817 SpvOpUGreaterThanEqual, SpvOpUndef
, out); |
| 1818 case Token::LTEQ: |
| 1819 ASSERT(resultType == kBool_Type); |
| 1820 return this->writeBinaryOperation(resultType, operandType, lhs, rhs,
|
| 1821 SpvOpFOrdLessThanEqual, SpvOpSLess
ThanEqual, |
| 1822 SpvOpULessThanEqual, SpvOpUndef, o
ut); |
| 1823 case Token::PLUS: |
| 1824 return this->writeBinaryOperation(resultType, operandType, lhs, rhs,
SpvOpFAdd, |
| 1825 SpvOpIAdd, SpvOpIAdd, SpvOpUndef,
out); |
| 1826 case Token::MINUS: |
| 1827 return this->writeBinaryOperation(resultType, operandType, lhs, rhs,
SpvOpFSub, |
| 1828 SpvOpISub, SpvOpISub, SpvOpUndef,
out); |
| 1829 case Token::STAR: |
| 1830 if (b.fLeft->fType->kind() == Type::kMatrix_Kind && |
| 1831 b.fRight->fType->kind() == Type::kMatrix_Kind) { |
| 1832 // matrix multiply |
| 1833 SpvId result = this->nextId(); |
| 1834 this->writeInstruction(SpvOpMatrixTimesMatrix, this->getType(res
ultType), result, |
| 1835 lhs, rhs, out); |
| 1836 return result; |
| 1837 } |
| 1838 return this->writeBinaryOperation(resultType, operandType, lhs, rhs,
SpvOpFMul, |
| 1839 SpvOpIMul, SpvOpIMul, SpvOpUndef,
out); |
| 1840 case Token::SLASH: |
| 1841 return this->writeBinaryOperation(resultType, operandType, lhs, rhs,
SpvOpFDiv, |
| 1842 SpvOpSDiv, SpvOpUDiv, SpvOpUndef,
out); |
| 1843 case Token::PLUSEQ: { |
| 1844 SpvId result = this->writeBinaryOperation(resultType, operandType, l
hs, rhs, SpvOpFAdd, |
| 1845 SpvOpIAdd, SpvOpIAdd, SpvO
pUndef, out); |
| 1846 this->storeToLValue(*b.fLeft, result, out); |
| 1847 return result; |
| 1848 } |
| 1849 case Token::MINUSEQ: { |
| 1850 SpvId result = this->writeBinaryOperation(resultType, operandType, l
hs, rhs, SpvOpFSub, |
| 1851 SpvOpISub, SpvOpISub, SpvO
pUndef, out); |
| 1852 this->storeToLValue(*b.fLeft, result, out); |
| 1853 return result; |
| 1854 } |
| 1855 case Token::STAREQ: { |
| 1856 if (b.fLeft->fType->kind() == Type::kMatrix_Kind && |
| 1857 b.fRight->fType->kind() == Type::kMatrix_Kind) { |
| 1858 // matrix multiply |
| 1859 SpvId result = this->nextId(); |
| 1860 this->writeInstruction(SpvOpMatrixTimesMatrix, this->getType(res
ultType), result, |
| 1861 lhs, rhs, out); |
| 1862 this->storeToLValue(*b.fLeft, result, out); |
| 1863 return result; |
| 1864 } |
| 1865 SpvId result = this->writeBinaryOperation(resultType, operandType, l
hs, rhs, SpvOpFMul, |
| 1866 SpvOpIMul, SpvOpIMul, SpvO
pUndef, out); |
| 1867 this->storeToLValue(*b.fLeft, result, out); |
| 1868 return result; |
| 1869 } |
| 1870 case Token::SLASHEQ: { |
| 1871 SpvId result = this->writeBinaryOperation(resultType, operandType, l
hs, rhs, SpvOpFDiv, |
| 1872 SpvOpSDiv, SpvOpUDiv, SpvO
pUndef, out); |
| 1873 this->storeToLValue(*b.fLeft, result, out); |
| 1874 return result; |
| 1875 } |
| 1876 default: |
| 1877 ABORT("unsupported binary expression: %s\n", b.description().c_str()
); |
| 1878 } |
| 1879 } |
| 1880 |
| 1881 SpvId SPIRVCodeGenerator::writeLogicalAnd(BinaryExpression& a, std::ostream& out
) { |
| 1882 ASSERT(a.fOperator == Token::LOGICALAND); |
| 1883 BoolLiteral falseLiteral(Position(), false); |
| 1884 SpvId falseConstant = this->writeBoolLiteral(falseLiteral); |
| 1885 SpvId lhs = this->writeExpression(*a.fLeft, out); |
| 1886 SpvId rhsLabel = this->nextId(); |
| 1887 SpvId end = this->nextId(); |
| 1888 SpvId lhsBlock = fCurrentBlock; |
| 1889 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone
, out); |
| 1890 this->writeInstruction(SpvOpBranchConditional, lhs, rhsLabel, end, out); |
| 1891 this->writeLabel(rhsLabel, out); |
| 1892 SpvId rhs = this->writeExpression(*a.fRight, out); |
| 1893 SpvId rhsBlock = fCurrentBlock; |
| 1894 this->writeInstruction(SpvOpBranch, end, out); |
| 1895 this->writeLabel(end, out); |
| 1896 SpvId result = this->nextId(); |
| 1897 this->writeInstruction(SpvOpPhi, this->getType(kBool_Type), result, falseCon
stant, lhsBlock, |
| 1898 rhs, rhsBlock, out); |
| 1899 return result; |
| 1900 } |
| 1901 |
| 1902 SpvId SPIRVCodeGenerator::writeLogicalOr(BinaryExpression& o, std::ostream& out)
{ |
| 1903 ASSERT(o.fOperator == Token::LOGICALOR); |
| 1904 BoolLiteral trueLiteral(Position(), true); |
| 1905 SpvId trueConstant = this->writeBoolLiteral(trueLiteral); |
| 1906 SpvId lhs = this->writeExpression(*o.fLeft, out); |
| 1907 SpvId rhsLabel = this->nextId(); |
| 1908 SpvId end = this->nextId(); |
| 1909 SpvId lhsBlock = fCurrentBlock; |
| 1910 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone
, out); |
| 1911 this->writeInstruction(SpvOpBranchConditional, lhs, end, rhsLabel, out); |
| 1912 this->writeLabel(rhsLabel, out); |
| 1913 SpvId rhs = this->writeExpression(*o.fRight, out); |
| 1914 SpvId rhsBlock = fCurrentBlock; |
| 1915 this->writeInstruction(SpvOpBranch, end, out); |
| 1916 this->writeLabel(end, out); |
| 1917 SpvId result = this->nextId(); |
| 1918 this->writeInstruction(SpvOpPhi, this->getType(kBool_Type), result, trueCons
tant, lhsBlock, |
| 1919 rhs, rhsBlock, out); |
| 1920 return result; |
| 1921 } |
| 1922 |
| 1923 SpvId SPIRVCodeGenerator::writeTernaryExpression(TernaryExpression& t, std::ostr
eam& out) { |
| 1924 SpvId test = this->writeExpression(*t.fTest, out); |
| 1925 if (t.fIfTrue->isConstant() && t.fIfFalse->isConstant()) { |
| 1926 // both true and false are constants, can just use OpSelect |
| 1927 SpvId result = this->nextId(); |
| 1928 SpvId trueId = this->writeExpression(*t.fIfTrue, out); |
| 1929 SpvId falseId = this->writeExpression(*t.fIfFalse, out); |
| 1930 this->writeInstruction(SpvOpSelect, this->getType(t.fType), result, test
, trueId, falseId, |
| 1931 out); |
| 1932 return result; |
| 1933 } |
| 1934 // was originally using OpPhi to choose the result, but for some reason that
is crashing on |
| 1935 // Adreno. Switched to storing the result in a temp variable as glslang does
. |
| 1936 SpvId var = this->nextId(); |
| 1937 this->writeInstruction(SpvOpVariable, this->getPointerType(t.fType, SpvStora
geClassFunction), |
| 1938 var, SpvStorageClassFunction, out); |
| 1939 SpvId trueLabel = this->nextId(); |
| 1940 SpvId falseLabel = this->nextId(); |
| 1941 SpvId end = this->nextId(); |
| 1942 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone
, out); |
| 1943 this->writeInstruction(SpvOpBranchConditional, test, trueLabel, falseLabel,
out); |
| 1944 this->writeLabel(trueLabel, out); |
| 1945 this->writeInstruction(SpvOpStore, var, this->writeExpression(*t.fIfTrue, ou
t), out); |
| 1946 this->writeInstruction(SpvOpBranch, end, out); |
| 1947 this->writeLabel(falseLabel, out); |
| 1948 this->writeInstruction(SpvOpStore, var, this->writeExpression(*t.fIfFalse, o
ut), out); |
| 1949 this->writeInstruction(SpvOpBranch, end, out); |
| 1950 this->writeLabel(end, out); |
| 1951 SpvId result = this->nextId(); |
| 1952 this->writeInstruction(SpvOpLoad, this->getType(t.fType), result, var, out); |
| 1953 return result; |
| 1954 } |
| 1955 |
| 1956 SpvId SPIRVCodeGenerator::writePrefixExpression(PrefixExpression& p, std::ostrea
m& out) { |
| 1957 SpvId expr = this->writeExpression(*p.fOperand, out); |
| 1958 if (p.fOperator == Token::MINUS) { |
| 1959 SpvId result = this->nextId(); |
| 1960 SpvId typeId = this->getType(p.fType); |
| 1961 if (is_float(p.fType)) { |
| 1962 this->writeInstruction(SpvOpFNegate, typeId, result, expr, out); |
| 1963 } else if (is_signed(p.fType)) { |
| 1964 this->writeInstruction(SpvOpSNegate, typeId, result, expr, out); |
| 1965 } else { |
| 1966 ABORT("unsupported prefix expression %s\n", p.description().c_str())
; |
| 1967 }; |
| 1968 return result; |
| 1969 } |
| 1970 switch (p.fOperator) { |
| 1971 case Token::PLUS: |
| 1972 return expr; |
| 1973 case Token::PLUSPLUS: { |
| 1974 IntLiteral oneLiteral(Position(), 1); |
| 1975 SpvId one = this->writeExpression(oneLiteral, out); |
| 1976 SpvId result = this->writeBinaryOperation(p.fType, p.fType, expr, on
e, SpvOpFAdd, |
| 1977 SpvOpIAdd, SpvOpIAdd, SpvOpU
ndef, out); |
| 1978 this->storeToLValue(*p.fOperand, result, out); |
| 1979 return result; |
| 1980 } |
| 1981 case Token::MINUSMINUS: { |
| 1982 IntLiteral oneLiteral(Position(), 1); |
| 1983 SpvId one = this->writeExpression(oneLiteral, out); |
| 1984 SpvId result = this->writeBinaryOperation(p.fType, p.fType, expr, on
e, SpvOpFSub, |
| 1985 SpvOpISub, SpvOpISub, SpvOpU
ndef, out); |
| 1986 this->storeToLValue(*p.fOperand, result, out); |
| 1987 return result; |
| 1988 } |
| 1989 case Token::NOT: { |
| 1990 ASSERT(p.fOperand->fType == kBool_Type); |
| 1991 SpvId result = this->nextId(); |
| 1992 this->writeInstruction(SpvOpLogicalNot, this->getType(p.fOperand->fT
ype), result, |
| 1993 this->writeExpression(*p.fOperand, out), out)
; |
| 1994 return result; |
| 1995 } |
| 1996 default: |
| 1997 ABORT("unsupported prefix expression: %s\n", p.description().c_str()
); |
| 1998 } |
| 1999 } |
| 2000 |
| 2001 SpvId SPIRVCodeGenerator::writePostfixExpression(PostfixExpression& p, std::ostr
eam& out) { |
| 2002 SpvId result = this->writeExpression(*p.fOperand, out); |
| 2003 IntLiteral oneLiteral(Position(), 1); |
| 2004 SpvId one = this->writeExpression(oneLiteral, out); |
| 2005 switch (p.fOperator) { |
| 2006 case Token::PLUSPLUS: { |
| 2007 SpvId temp = this->writeBinaryOperation(p.fType, p.fType, result, on
e, SpvOpFAdd, |
| 2008 SpvOpIAdd, SpvOpIAdd, SpvOpU
ndef, out); |
| 2009 this->storeToLValue(*p.fOperand, temp, out); |
| 2010 return result; |
| 2011 } |
| 2012 case Token::MINUSMINUS: { |
| 2013 SpvId temp = this->writeBinaryOperation(p.fType, p.fType, result, on
e, SpvOpFSub, |
| 2014 SpvOpISub, SpvOpISub, SpvOpU
ndef, out); |
| 2015 this->storeToLValue(*p.fOperand, temp, out); |
| 2016 return result; |
| 2017 } |
| 2018 default: |
| 2019 ABORT("unsupported postfix expression %s\n", p.description().c_str()
); |
| 2020 } |
| 2021 } |
| 2022 |
| 2023 SpvId SPIRVCodeGenerator::writeBoolLiteral(BoolLiteral& b) { |
| 2024 if (b.fValue) { |
| 2025 if (fBoolTrue == 0) { |
| 2026 fBoolTrue = this->nextId(); |
| 2027 this->writeInstruction(SpvOpConstantTrue, this->getType(b.fType), fB
oolTrue, |
| 2028 fConstantBuffer); |
| 2029 } |
| 2030 return fBoolTrue; |
| 2031 } else { |
| 2032 if (fBoolFalse == 0) { |
| 2033 fBoolFalse = this->nextId(); |
| 2034 this->writeInstruction(SpvOpConstantFalse, this->getType(b.fType), f
BoolFalse, |
| 2035 fConstantBuffer); |
| 2036 } |
| 2037 return fBoolFalse; |
| 2038 } |
| 2039 } |
| 2040 |
| 2041 SpvId SPIRVCodeGenerator::writeIntLiteral(IntLiteral& i) { |
| 2042 if (i.fType == kInt_Type) { |
| 2043 auto entry = fIntConstants.find(i.fValue); |
| 2044 if (entry == fIntConstants.end()) { |
| 2045 SpvId result = this->nextId(); |
| 2046 this->writeInstruction(SpvOpConstant, this->getType(i.fType), result
, (SpvId) i.fValue, |
| 2047 fConstantBuffer); |
| 2048 fIntConstants[i.fValue] = result; |
| 2049 return result; |
| 2050 } |
| 2051 return entry->second; |
| 2052 } else { |
| 2053 ASSERT(i.fType == kUInt_Type); |
| 2054 auto entry = fUIntConstants.find(i.fValue); |
| 2055 if (entry == fUIntConstants.end()) { |
| 2056 SpvId result = this->nextId(); |
| 2057 this->writeInstruction(SpvOpConstant, this->getType(i.fType), result
, (SpvId) i.fValue, |
| 2058 fConstantBuffer); |
| 2059 fUIntConstants[i.fValue] = result; |
| 2060 return result; |
| 2061 } |
| 2062 return entry->second; |
| 2063 } |
| 2064 } |
| 2065 |
| 2066 SpvId SPIRVCodeGenerator::writeFloatLiteral(FloatLiteral& f) { |
| 2067 if (f.fType == kFloat_Type) { |
| 2068 float value = (float) f.fValue; |
| 2069 auto entry = fFloatConstants.find(value); |
| 2070 if (entry == fFloatConstants.end()) { |
| 2071 SpvId result = this->nextId(); |
| 2072 uint32_t bits; |
| 2073 ASSERT(sizeof(bits) == sizeof(value)); |
| 2074 memcpy(&bits, &value, sizeof(bits)); |
| 2075 this->writeInstruction(SpvOpConstant, this->getType(f.fType), result
, bits, |
| 2076 fConstantBuffer); |
| 2077 fFloatConstants[value] = result; |
| 2078 return result; |
| 2079 } |
| 2080 return entry->second; |
| 2081 } else { |
| 2082 ASSERT(f.fType == kDouble_Type); |
| 2083 auto entry = fDoubleConstants.find(f.fValue); |
| 2084 if (entry == fDoubleConstants.end()) { |
| 2085 SpvId result = this->nextId(); |
| 2086 uint64_t bits; |
| 2087 ASSERT(sizeof(bits) == sizeof(f.fValue)); |
| 2088 memcpy(&bits, &f.fValue, sizeof(bits)); |
| 2089 this->writeInstruction(SpvOpConstant, this->getType(f.fType), result
, bits & 0xffffffff, |
| 2090 bits >> 32, fConstantBuffer); |
| 2091 fDoubleConstants[f.fValue] = result; |
| 2092 return result; |
| 2093 } |
| 2094 return entry->second; |
| 2095 } |
| 2096 } |
| 2097 |
| 2098 SpvId SPIRVCodeGenerator::writeFunctionStart(std::shared_ptr<FunctionDeclaration
> f, |
| 2099 std::ostream& out) { |
| 2100 SpvId result = fFunctionMap[f]; |
| 2101 this->writeInstruction(SpvOpFunction, this->getType(f->fReturnType), result,
|
| 2102 SpvFunctionControlMaskNone, this->getFunctionType(f),
out); |
| 2103 this->writeInstruction(SpvOpName, result, f->fName.c_str(), fNameBuffer); |
| 2104 for (size_t i = 0; i < f->fParameters.size(); i++) { |
| 2105 SpvId id = this->nextId(); |
| 2106 fVariableMap[f->fParameters[i]] = id; |
| 2107 SpvId type; |
| 2108 type = this->getPointerType(f->fParameters[i]->fType, SpvStorageClassFun
ction); |
| 2109 this->writeInstruction(SpvOpFunctionParameter, type, id, out); |
| 2110 } |
| 2111 return result; |
| 2112 } |
| 2113 |
| 2114 SpvId SPIRVCodeGenerator::writeFunctionDeclaration(std::shared_ptr<FunctionDecla
ration> f, |
| 2115 std::ostream& out) { |
| 2116 printf("WARNING: writing function declaration for %s\n", f->description().c_
str()); |
| 2117 SpvId result = this->writeFunctionStart(f, out); |
| 2118 this->writeInstruction(SpvOpFunctionEnd, out); |
| 2119 return result; |
| 2120 } |
| 2121 |
| 2122 SpvId SPIRVCodeGenerator::writeFunction(FunctionDefinition& f, std::ostream& out
) { |
| 2123 SpvId result = this->writeFunctionStart(f.fDeclaration, out); |
| 2124 this->writeLabel(this->nextId(), out); |
| 2125 if (f.fDeclaration->fName == "main") { |
| 2126 out << fGlobalInitializersBuffer.str(); |
| 2127 } |
| 2128 std::stringstream bodyBuffer; |
| 2129 this->writeBlock(*f.fBody, bodyBuffer); |
| 2130 out << fVariableBuffer.str(); |
| 2131 fVariableBuffer.str(""); |
| 2132 out << bodyBuffer.str(); |
| 2133 if (fCurrentBlock) { |
| 2134 this->writeInstruction(SpvOpReturn, out); |
| 2135 } |
| 2136 this->writeInstruction(SpvOpFunctionEnd, out); |
| 2137 return result; |
| 2138 } |
| 2139 |
| 2140 void SPIRVCodeGenerator::writeLayout(const Layout& layout, SpvId target) { |
| 2141 if (layout.fLocation >= 0) { |
| 2142 this->writeInstruction(SpvOpDecorate, target, SpvDecorationLocation, lay
out.fLocation, |
| 2143 fDecorationBuffer); |
| 2144 } |
| 2145 if (layout.fBinding >= 0) { |
| 2146 this->writeInstruction(SpvOpDecorate, target, SpvDecorationBinding, layo
ut.fBinding, |
| 2147 fDecorationBuffer); |
| 2148 } |
| 2149 if (layout.fIndex >= 0) { |
| 2150 this->writeInstruction(SpvOpDecorate, target, SpvDecorationIndex, layout
.fIndex, |
| 2151 fDecorationBuffer); |
| 2152 } |
| 2153 if (layout.fSet >= 0) { |
| 2154 this->writeInstruction(SpvOpDecorate, target, SpvDecorationDescriptorSet
, layout.fSet, |
| 2155 fDecorationBuffer); |
| 2156 } |
| 2157 if (layout.fBuiltin >= 0) { |
| 2158 this->writeInstruction(SpvOpDecorate, target, SpvDecorationBuiltIn, layo
ut.fBuiltin, |
| 2159 fDecorationBuffer); |
| 2160 } |
| 2161 } |
| 2162 |
| 2163 void SPIRVCodeGenerator::writeLayout(const Layout& layout, SpvId target, int mem
ber) { |
| 2164 if (layout.fLocation >= 0) { |
| 2165 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecoratio
nLocation, |
| 2166 layout.fLocation, fDecorationBuffer); |
| 2167 } |
| 2168 if (layout.fBinding >= 0) { |
| 2169 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecoratio
nBinding, |
| 2170 layout.fBinding, fDecorationBuffer); |
| 2171 } |
| 2172 if (layout.fIndex >= 0) { |
| 2173 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecoratio
nIndex, |
| 2174 layout.fIndex, fDecorationBuffer); |
| 2175 } |
| 2176 if (layout.fSet >= 0) { |
| 2177 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecoratio
nDescriptorSet, |
| 2178 layout.fSet, fDecorationBuffer); |
| 2179 } |
| 2180 if (layout.fBuiltin >= 0) { |
| 2181 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecoratio
nBuiltIn, |
| 2182 layout.fBuiltin, fDecorationBuffer); |
| 2183 } |
| 2184 } |
| 2185 |
| 2186 SpvId SPIRVCodeGenerator::writeInterfaceBlock(InterfaceBlock& intf) { |
| 2187 SpvId type = this->getType(intf.fVariable->fType); |
| 2188 SpvId result = this->nextId(); |
| 2189 this->writeInstruction(SpvOpDecorate, type, SpvDecorationBlock, fDecorationB
uffer); |
| 2190 SpvStorageClass_ storageClass = get_storage_class(intf.fVariable->fModifiers
); |
| 2191 SpvId ptrType = this->nextId(); |
| 2192 this->writeInstruction(SpvOpTypePointer, ptrType, storageClass, type, fConst
antBuffer); |
| 2193 this->writeInstruction(SpvOpVariable, ptrType, result, storageClass, fConsta
ntBuffer); |
| 2194 this->writeLayout(intf.fVariable->fModifiers.fLayout, result); |
| 2195 fVariableMap[intf.fVariable] = result; |
| 2196 return result; |
| 2197 } |
| 2198 |
| 2199 void SPIRVCodeGenerator::writeGlobalVars(VarDeclaration& decl, std::ostream& out
) { |
| 2200 for (size_t i = 0; i < decl.fVars.size(); i++) { |
| 2201 if (!decl.fVars[i]->fIsReadFrom && !decl.fVars[i]->fIsWrittenTo) { |
| 2202 continue; |
| 2203 } |
| 2204 SpvStorageClass_ storageClass; |
| 2205 if (decl.fVars[i]->fModifiers.fFlags & Modifiers::kIn_Flag) { |
| 2206 storageClass = SpvStorageClassInput; |
| 2207 } else if (decl.fVars[i]->fModifiers.fFlags & Modifiers::kOut_Flag) { |
| 2208 storageClass = SpvStorageClassOutput; |
| 2209 } else if (decl.fVars[i]->fModifiers.fFlags & Modifiers::kUniform_Flag)
{ |
| 2210 if (decl.fVars[i]->fType->kind() == Type::kSampler_Kind) { |
| 2211 storageClass = SpvStorageClassUniformConstant; |
| 2212 } else { |
| 2213 storageClass = SpvStorageClassUniform; |
| 2214 } |
| 2215 } else { |
| 2216 storageClass = SpvStorageClassPrivate; |
| 2217 } |
| 2218 SpvId id = this->nextId(); |
| 2219 fVariableMap[decl.fVars[i]] = id; |
| 2220 SpvId type = this->getPointerType(decl.fVars[i]->fType, storageClass); |
| 2221 this->writeInstruction(SpvOpVariable, type, id, storageClass, fConstantB
uffer); |
| 2222 this->writeInstruction(SpvOpName, id, decl.fVars[i]->fName.c_str(), fNam
eBuffer); |
| 2223 if (decl.fVars[i]->fType->kind() == Type::kMatrix_Kind) { |
| 2224 this->writeInstruction(SpvOpMemberDecorate, id, (SpvId) i, SpvDecora
tionColMajor, |
| 2225 fDecorationBuffer); |
| 2226 this->writeInstruction(SpvOpMemberDecorate, id, (SpvId) i, SpvDecora
tionMatrixStride, |
| 2227 (SpvId) decl.fVars[i]->fType->stride(), fDeco
rationBuffer); |
| 2228 } |
| 2229 if (decl.fValues[i] != nullptr) { |
| 2230 ASSERT(!fCurrentBlock); |
| 2231 fCurrentBlock = -1; |
| 2232 SpvId value = this->writeExpression(*decl.fValues[i], fGlobalInitial
izersBuffer); |
| 2233 this->writeInstruction(SpvOpStore, id, value, fGlobalInitializersBuf
fer); |
| 2234 fCurrentBlock = 0; |
| 2235 } |
| 2236 this->writeLayout(decl.fVars[i]->fModifiers.fLayout, id); |
| 2237 } |
| 2238 } |
| 2239 |
| 2240 void SPIRVCodeGenerator::writeVarDeclaration(VarDeclaration& decl, std::ostream&
out) { |
| 2241 for (size_t i = 0; i < decl.fVars.size(); i++) { |
| 2242 SpvId id = this->nextId(); |
| 2243 fVariableMap[decl.fVars[i]] = id; |
| 2244 SpvId type = this->getPointerType(decl.fVars[i]->fType, SpvStorageClassF
unction); |
| 2245 this->writeInstruction(SpvOpVariable, type, id, SpvStorageClassFunction,
fVariableBuffer); |
| 2246 this->writeInstruction(SpvOpName, id, decl.fVars[i]->fName.c_str(), fNam
eBuffer); |
| 2247 if (decl.fValues[i] != nullptr) { |
| 2248 SpvId value = this->writeExpression(*decl.fValues[i], out); |
| 2249 this->writeInstruction(SpvOpStore, id, value, out); |
| 2250 } |
| 2251 } |
| 2252 } |
| 2253 |
| 2254 void SPIRVCodeGenerator::writeStatement(Statement& s, std::ostream& out) { |
| 2255 switch (s.fKind) { |
| 2256 case Statement::kBlock_Kind: |
| 2257 this->writeBlock((Block&) s, out); |
| 2258 break; |
| 2259 case Statement::kExpression_Kind: |
| 2260 this->writeExpression(*((ExpressionStatement&) s).fExpression, out); |
| 2261 break; |
| 2262 case Statement::kReturn_Kind: |
| 2263 this->writeReturnStatement((ReturnStatement&) s, out); |
| 2264 break; |
| 2265 case Statement::kVarDeclaration_Kind: |
| 2266 this->writeVarDeclaration(*((VarDeclarationStatement&) s).fDeclarati
on, out); |
| 2267 break; |
| 2268 case Statement::kIf_Kind: |
| 2269 this->writeIfStatement((IfStatement&) s, out); |
| 2270 break; |
| 2271 case Statement::kFor_Kind: |
| 2272 this->writeForStatement((ForStatement&) s, out); |
| 2273 break; |
| 2274 case Statement::kBreak_Kind: |
| 2275 this->writeInstruction(SpvOpBranch, fBreakTarget.top(), out); |
| 2276 break; |
| 2277 case Statement::kContinue_Kind: |
| 2278 this->writeInstruction(SpvOpBranch, fContinueTarget.top(), out); |
| 2279 break; |
| 2280 case Statement::kDiscard_Kind: |
| 2281 this->writeInstruction(SpvOpKill, out); |
| 2282 break; |
| 2283 default: |
| 2284 printf("unsupported statement: %s\n", s.description().c_str()); |
| 2285 ASSERT(false); |
| 2286 } |
| 2287 } |
| 2288 |
| 2289 void SPIRVCodeGenerator::writeBlock(Block& b, std::ostream& out) { |
| 2290 for (size_t i = 0; i < b.fStatements.size(); i++) { |
| 2291 this->writeStatement(*b.fStatements[i], out); |
| 2292 } |
| 2293 } |
| 2294 |
| 2295 void SPIRVCodeGenerator::writeIfStatement(IfStatement& stmt, std::ostream& out)
{ |
| 2296 SpvId test = this->writeExpression(*stmt.fTest, out); |
| 2297 SpvId ifTrue = this->nextId(); |
| 2298 SpvId ifFalse = this->nextId(); |
| 2299 if (stmt.fIfFalse) { |
| 2300 SpvId end = this->nextId(); |
| 2301 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMask
None, out); |
| 2302 this->writeInstruction(SpvOpBranchConditional, test, ifTrue, ifFalse, ou
t); |
| 2303 this->writeLabel(ifTrue, out); |
| 2304 this->writeStatement(*stmt.fIfTrue, out); |
| 2305 if (fCurrentBlock) { |
| 2306 this->writeInstruction(SpvOpBranch, end, out); |
| 2307 } |
| 2308 this->writeLabel(ifFalse, out); |
| 2309 this->writeStatement(*stmt.fIfFalse, out); |
| 2310 if (fCurrentBlock) { |
| 2311 this->writeInstruction(SpvOpBranch, end, out); |
| 2312 } |
| 2313 this->writeLabel(end, out); |
| 2314 } else { |
| 2315 this->writeInstruction(SpvOpSelectionMerge, ifFalse, SpvSelectionControl
MaskNone, out); |
| 2316 this->writeInstruction(SpvOpBranchConditional, test, ifTrue, ifFalse, ou
t); |
| 2317 this->writeLabel(ifTrue, out); |
| 2318 this->writeStatement(*stmt.fIfTrue, out); |
| 2319 if (fCurrentBlock) { |
| 2320 this->writeInstruction(SpvOpBranch, ifFalse, out); |
| 2321 } |
| 2322 this->writeLabel(ifFalse, out); |
| 2323 } |
| 2324 } |
| 2325 |
| 2326 void SPIRVCodeGenerator::writeForStatement(ForStatement& f, std::ostream& out) { |
| 2327 if (f.fInitializer) { |
| 2328 this->writeStatement(*f.fInitializer, out); |
| 2329 } |
| 2330 SpvId header = this->nextId(); |
| 2331 SpvId start = this->nextId(); |
| 2332 SpvId body = this->nextId(); |
| 2333 SpvId next = this->nextId(); |
| 2334 fContinueTarget.push(next); |
| 2335 SpvId end = this->nextId(); |
| 2336 fBreakTarget.push(end); |
| 2337 this->writeInstruction(SpvOpBranch, header, out); |
| 2338 this->writeLabel(header, out); |
| 2339 this->writeInstruction(SpvOpLoopMerge, end, next, SpvLoopControlMaskNone, ou
t); |
| 2340 this->writeInstruction(SpvOpBranch, start, out); |
| 2341 this->writeLabel(start, out); |
| 2342 SpvId test = this->writeExpression(*f.fTest, out); |
| 2343 this->writeInstruction(SpvOpBranchConditional, test, body, end, out); |
| 2344 this->writeLabel(body, out); |
| 2345 this->writeStatement(*f.fStatement, out); |
| 2346 if (fCurrentBlock) { |
| 2347 this->writeInstruction(SpvOpBranch, next, out); |
| 2348 } |
| 2349 this->writeLabel(next, out); |
| 2350 if (f.fNext) { |
| 2351 this->writeExpression(*f.fNext, out); |
| 2352 } |
| 2353 this->writeInstruction(SpvOpBranch, header, out); |
| 2354 this->writeLabel(end, out); |
| 2355 fBreakTarget.pop(); |
| 2356 fContinueTarget.pop(); |
| 2357 } |
| 2358 |
| 2359 void SPIRVCodeGenerator::writeReturnStatement(ReturnStatement& r, std::ostream&
out) { |
| 2360 if (r.fExpression) { |
| 2361 this->writeInstruction(SpvOpReturnValue, this->writeExpression(*r.fExpre
ssion, out), |
| 2362 out); |
| 2363 } else { |
| 2364 this->writeInstruction(SpvOpReturn, out); |
| 2365 } |
| 2366 } |
| 2367 |
| 2368 void SPIRVCodeGenerator::writeInstructions(Program& program, std::ostream& out)
{ |
| 2369 fGLSLExtendedInstructions = this->nextId(); |
| 2370 std::stringstream body; |
| 2371 std::vector<SpvId> interfaceVars; |
| 2372 // assign IDs to functions |
| 2373 for (size_t i = 0; i < program.fElements.size(); i++) { |
| 2374 if (program.fElements[i]->fKind == ProgramElement::kFunction_Kind) { |
| 2375 FunctionDefinition& f = (FunctionDefinition&) *program.fElements[i]; |
| 2376 fFunctionMap[f.fDeclaration] = this->nextId(); |
| 2377 } |
| 2378 } |
| 2379 for (size_t i = 0; i < program.fElements.size(); i++) { |
| 2380 if (program.fElements[i]->fKind == ProgramElement::kInterfaceBlock_Kind)
{ |
| 2381 InterfaceBlock& intf = (InterfaceBlock&) *program.fElements[i]; |
| 2382 SpvId id = this->writeInterfaceBlock(intf); |
| 2383 if ((intf.fVariable->fModifiers.fFlags & Modifiers::kIn_Flag) || |
| 2384 (intf.fVariable->fModifiers.fFlags & Modifiers::kOut_Flag)) { |
| 2385 interfaceVars.push_back(id); |
| 2386 } |
| 2387 } |
| 2388 } |
| 2389 for (size_t i = 0; i < program.fElements.size(); i++) { |
| 2390 if (program.fElements[i]->fKind == ProgramElement::kVar_Kind) { |
| 2391 this->writeGlobalVars(((VarDeclaration&) *program.fElements[i]), bod
y); |
| 2392 } |
| 2393 } |
| 2394 for (size_t i = 0; i < program.fElements.size(); i++) { |
| 2395 if (program.fElements[i]->fKind == ProgramElement::kFunction_Kind) { |
| 2396 this->writeFunction(((FunctionDefinition&) *program.fElements[i]), b
ody); |
| 2397 } |
| 2398 } |
| 2399 std::shared_ptr<FunctionDeclaration> main = nullptr; |
| 2400 for (auto entry : fFunctionMap) { |
| 2401 if (entry.first->fName == "main") { |
| 2402 main = entry.first; |
| 2403 } |
| 2404 } |
| 2405 ASSERT(main != nullptr); |
| 2406 for (auto entry : fVariableMap) { |
| 2407 std::shared_ptr<Variable> var = entry.first; |
| 2408 if (var->fStorage == Variable::kGlobal_Storage && |
| 2409 ((var->fModifiers.fFlags & Modifiers::kIn_Flag) || |
| 2410 (var->fModifiers.fFlags & Modifiers::kOut_Flag))) { |
| 2411 interfaceVars.push_back(entry.second); |
| 2412 } |
| 2413 } |
| 2414 this->writeCapabilities(out); |
| 2415 this->writeInstruction(SpvOpExtInstImport, fGLSLExtendedInstructions, "GLSL.
std.450", out); |
| 2416 this->writeInstruction(SpvOpMemoryModel, SpvAddressingModelLogical, SpvMemor
yModelGLSL450, out); |
| 2417 this->writeOpCode(SpvOpEntryPoint, (SpvId) (3 + (strlen(main->fName.c_str())
+ 4) / 4) + |
| 2418 (int32_t) interfaceVars.size(), out); |
| 2419 switch (program.fKind) { |
| 2420 case Program::kVertex_Kind: |
| 2421 this->writeWord(SpvExecutionModelVertex, out); |
| 2422 break; |
| 2423 case Program::kFragment_Kind: |
| 2424 this->writeWord(SpvExecutionModelFragment, out); |
| 2425 break; |
| 2426 } |
| 2427 this->writeWord(fFunctionMap[main], out); |
| 2428 this->writeString(main->fName.c_str(), out); |
| 2429 for (int var : interfaceVars) { |
| 2430 this->writeWord(var, out); |
| 2431 } |
| 2432 if (program.fKind == Program::kFragment_Kind) { |
| 2433 this->writeInstruction(SpvOpExecutionMode, |
| 2434 fFunctionMap[main], |
| 2435 SpvExecutionModeOriginUpperLeft, |
| 2436 out); |
| 2437 } |
| 2438 for (size_t i = 0; i < program.fElements.size(); i++) { |
| 2439 if (program.fElements[i]->fKind == ProgramElement::kExtension_Kind) { |
| 2440 this->writeInstruction(SpvOpSourceExtension, |
| 2441 ((Extension&) *program.fElements[i]).fName.c_
str(), |
| 2442 out); |
| 2443 } |
| 2444 } |
| 2445 |
| 2446 out << fNameBuffer.str(); |
| 2447 out << fDecorationBuffer.str(); |
| 2448 out << fConstantBuffer.str(); |
| 2449 out << fExternalFunctionsBuffer.str(); |
| 2450 out << body.str(); |
| 2451 } |
| 2452 |
| 2453 void SPIRVCodeGenerator::generateCode(Program& program, std::ostream& out) { |
| 2454 this->writeWord(SpvMagicNumber, out); |
| 2455 this->writeWord(SpvVersion, out); |
| 2456 this->writeWord(SKSL_MAGIC, out); |
| 2457 std::stringstream buffer; |
| 2458 this->writeInstructions(program, buffer); |
| 2459 this->writeWord(fIdCount, out); |
| 2460 this->writeWord(0, out); // reserved, always zero |
| 2461 out << buffer.str(); |
| 2462 } |
| 2463 |
| 2464 } |
OLD | NEW |