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