Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Arm Limited |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "spirv_parser.hpp" |
| 18 | #include <assert.h> |
| 19 | |
| 20 | using namespace std; |
| 21 | using namespace spv; |
| 22 | |
| 23 | namespace spirv_cross |
| 24 | { |
| 25 | Parser::Parser(std::vector<uint32_t> spirv) |
| 26 | { |
| 27 | ir.spirv = move(spirv); |
| 28 | } |
| 29 | |
| 30 | Parser::Parser(const uint32_t *spirv_data, size_t word_count) |
| 31 | { |
| 32 | ir.spirv = vector<uint32_t>(spirv_data, spirv_data + word_count); |
| 33 | } |
| 34 | |
| 35 | static inline uint32_t swap_endian(uint32_t v) |
| 36 | { |
| 37 | return ((v >> 24) & 0x000000ffu) | ((v >> 8) & 0x0000ff00u) | ((v << 8) & 0x00ff0000u) | ((v << 24) & 0xff000000u); |
| 38 | } |
| 39 | |
| 40 | static bool is_valid_spirv_version(uint32_t version) |
| 41 | { |
| 42 | switch (version) |
| 43 | { |
| 44 | // Allow v99 since it tends to just work. |
| 45 | case 99: |
| 46 | case 0x10000: // SPIR-V 1.0 |
| 47 | case 0x10100: // SPIR-V 1.1 |
| 48 | case 0x10200: // SPIR-V 1.2 |
| 49 | case 0x10300: // SPIR-V 1.3 |
| 50 | return true; |
| 51 | |
| 52 | default: |
| 53 | return false; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void Parser::parse() |
| 58 | { |
| 59 | auto &spirv = ir.spirv; |
| 60 | |
| 61 | auto len = spirv.size(); |
| 62 | if (len < 5) |
| 63 | SPIRV_CROSS_THROW("SPIRV file too small."); |
| 64 | |
| 65 | auto s = spirv.data(); |
| 66 | |
| 67 | // Endian-swap if we need to. |
| 68 | if (s[0] == swap_endian(MagicNumber)) |
| 69 | transform(begin(spirv), end(spirv), begin(spirv), [](uint32_t c) { return swap_endian(c); }); |
| 70 | |
| 71 | if (s[0] != MagicNumber || !is_valid_spirv_version(s[1])) |
| 72 | SPIRV_CROSS_THROW("Invalid SPIRV format."); |
| 73 | |
| 74 | uint32_t bound = s[3]; |
| 75 | ir.set_id_bounds(bound); |
| 76 | |
| 77 | uint32_t offset = 5; |
| 78 | |
| 79 | vector<Instruction> instructions; |
| 80 | while (offset < len) |
| 81 | { |
| 82 | Instruction instr = {}; |
| 83 | instr.op = spirv[offset] & 0xffff; |
| 84 | instr.count = (spirv[offset] >> 16) & 0xffff; |
| 85 | |
| 86 | if (instr.count == 0) |
| 87 | SPIRV_CROSS_THROW("SPIR-V instructions cannot consume 0 words. Invalid SPIR-V file."); |
| 88 | |
| 89 | instr.offset = offset + 1; |
| 90 | instr.length = instr.count - 1; |
| 91 | |
| 92 | offset += instr.count; |
| 93 | |
| 94 | if (offset > spirv.size()) |
| 95 | SPIRV_CROSS_THROW("SPIR-V instruction goes out of bounds."); |
| 96 | |
| 97 | instructions.push_back(instr); |
| 98 | } |
| 99 | |
| 100 | for (auto &i : instructions) |
| 101 | parse(i); |
| 102 | |
| 103 | if (current_function) |
| 104 | SPIRV_CROSS_THROW("Function was not terminated."); |
| 105 | if (current_block) |
| 106 | SPIRV_CROSS_THROW("Block was not terminated."); |
| 107 | } |
| 108 | |
| 109 | const uint32_t *Parser::stream(const Instruction &instr) const |
| 110 | { |
| 111 | // If we're not going to use any arguments, just return nullptr. |
| 112 | // We want to avoid case where we return an out of range pointer |
| 113 | // that trips debug assertions on some platforms. |
| 114 | if (!instr.length) |
| 115 | return nullptr; |
| 116 | |
| 117 | if (instr.offset + instr.length > ir.spirv.size()) |
| 118 | SPIRV_CROSS_THROW("Compiler::stream() out of range."); |
| 119 | return &ir.spirv[instr.offset]; |
| 120 | } |
| 121 | |
| 122 | static string extract_string(const vector<uint32_t> &spirv, uint32_t offset) |
| 123 | { |
| 124 | string ret; |
| 125 | for (uint32_t i = offset; i < spirv.size(); i++) |
| 126 | { |
| 127 | uint32_t w = spirv[i]; |
| 128 | |
| 129 | for (uint32_t j = 0; j < 4; j++, w >>= 8) |
| 130 | { |
| 131 | char c = w & 0xff; |
| 132 | if (c == '\0') |
| 133 | return ret; |
| 134 | ret += c; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | SPIRV_CROSS_THROW("String was not terminated before EOF"); |
| 139 | } |
| 140 | |
| 141 | void Parser::parse(const Instruction &instruction) |
| 142 | { |
| 143 | auto *ops = stream(instruction); |
| 144 | auto op = static_cast<Op>(instruction.op); |
| 145 | uint32_t length = instruction.length; |
| 146 | |
| 147 | switch (op) |
| 148 | { |
| 149 | case OpMemoryModel: |
lifpan | 00a765e | 2018-11-15 09:04:36 +0800 | [diff] [blame^] | 150 | case OpSourceContinued: |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 151 | case OpSourceExtension: |
| 152 | case OpNop: |
| 153 | case OpLine: |
| 154 | case OpNoLine: |
| 155 | case OpString: |
lifpan | 9161096 | 2018-11-13 14:28:38 +0800 | [diff] [blame] | 156 | case OpModuleProcessed: |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 157 | break; |
| 158 | |
| 159 | case OpSource: |
| 160 | { |
| 161 | auto lang = static_cast<SourceLanguage>(ops[0]); |
| 162 | switch (lang) |
| 163 | { |
| 164 | case SourceLanguageESSL: |
| 165 | ir.source.es = true; |
| 166 | ir.source.version = ops[1]; |
| 167 | ir.source.known = true; |
| 168 | ir.source.hlsl = false; |
| 169 | break; |
| 170 | |
| 171 | case SourceLanguageGLSL: |
| 172 | ir.source.es = false; |
| 173 | ir.source.version = ops[1]; |
| 174 | ir.source.known = true; |
| 175 | ir.source.hlsl = false; |
| 176 | break; |
| 177 | |
| 178 | case SourceLanguageHLSL: |
| 179 | // For purposes of cross-compiling, this is GLSL 450. |
| 180 | ir.source.es = false; |
| 181 | ir.source.version = 450; |
| 182 | ir.source.known = true; |
| 183 | ir.source.hlsl = true; |
| 184 | break; |
| 185 | |
| 186 | default: |
| 187 | ir.source.known = false; |
| 188 | break; |
| 189 | } |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | case OpUndef: |
| 194 | { |
| 195 | uint32_t result_type = ops[0]; |
| 196 | uint32_t id = ops[1]; |
| 197 | set<SPIRUndef>(id, result_type); |
| 198 | break; |
| 199 | } |
| 200 | |
| 201 | case OpCapability: |
| 202 | { |
| 203 | uint32_t cap = ops[0]; |
| 204 | if (cap == CapabilityKernel) |
| 205 | SPIRV_CROSS_THROW("Kernel capability not supported."); |
| 206 | |
| 207 | ir.declared_capabilities.push_back(static_cast<Capability>(ops[0])); |
| 208 | break; |
| 209 | } |
| 210 | |
| 211 | case OpExtension: |
| 212 | { |
| 213 | auto ext = extract_string(ir.spirv, instruction.offset); |
| 214 | ir.declared_extensions.push_back(move(ext)); |
| 215 | break; |
| 216 | } |
| 217 | |
| 218 | case OpExtInstImport: |
| 219 | { |
| 220 | uint32_t id = ops[0]; |
| 221 | auto ext = extract_string(ir.spirv, instruction.offset + 1); |
| 222 | if (ext == "GLSL.std.450") |
| 223 | set<SPIRExtension>(id, SPIRExtension::GLSL); |
| 224 | else if (ext == "SPV_AMD_shader_ballot") |
| 225 | set<SPIRExtension>(id, SPIRExtension::SPV_AMD_shader_ballot); |
| 226 | else if (ext == "SPV_AMD_shader_explicit_vertex_parameter") |
| 227 | set<SPIRExtension>(id, SPIRExtension::SPV_AMD_shader_explicit_vertex_parameter); |
| 228 | else if (ext == "SPV_AMD_shader_trinary_minmax") |
| 229 | set<SPIRExtension>(id, SPIRExtension::SPV_AMD_shader_trinary_minmax); |
| 230 | else if (ext == "SPV_AMD_gcn_shader") |
| 231 | set<SPIRExtension>(id, SPIRExtension::SPV_AMD_gcn_shader); |
| 232 | else |
| 233 | set<SPIRExtension>(id, SPIRExtension::Unsupported); |
| 234 | |
| 235 | // Other SPIR-V extensions which have ExtInstrs are currently not supported. |
| 236 | |
| 237 | break; |
| 238 | } |
| 239 | |
| 240 | case OpEntryPoint: |
| 241 | { |
| 242 | auto itr = |
| 243 | ir.entry_points.insert(make_pair(ops[1], SPIREntryPoint(ops[1], static_cast<ExecutionModel>(ops[0]), |
| 244 | extract_string(ir.spirv, instruction.offset + 2)))); |
| 245 | auto &e = itr.first->second; |
| 246 | |
| 247 | // Strings need nul-terminator and consume the whole word. |
| 248 | uint32_t strlen_words = uint32_t((e.name.size() + 1 + 3) >> 2); |
| 249 | e.interface_variables.insert(end(e.interface_variables), ops + strlen_words + 2, ops + instruction.length); |
| 250 | |
| 251 | // Set the name of the entry point in case OpName is not provided later. |
| 252 | ir.set_name(ops[1], e.name); |
| 253 | |
| 254 | // If we don't have an entry, make the first one our "default". |
| 255 | if (!ir.default_entry_point) |
| 256 | ir.default_entry_point = ops[1]; |
| 257 | break; |
| 258 | } |
| 259 | |
| 260 | case OpExecutionMode: |
| 261 | { |
| 262 | auto &execution = ir.entry_points[ops[0]]; |
| 263 | auto mode = static_cast<ExecutionMode>(ops[1]); |
| 264 | execution.flags.set(mode); |
| 265 | |
| 266 | switch (mode) |
| 267 | { |
| 268 | case ExecutionModeInvocations: |
| 269 | execution.invocations = ops[2]; |
| 270 | break; |
| 271 | |
| 272 | case ExecutionModeLocalSize: |
| 273 | execution.workgroup_size.x = ops[2]; |
| 274 | execution.workgroup_size.y = ops[3]; |
| 275 | execution.workgroup_size.z = ops[4]; |
| 276 | break; |
| 277 | |
| 278 | case ExecutionModeOutputVertices: |
| 279 | execution.output_vertices = ops[2]; |
| 280 | break; |
| 281 | |
| 282 | default: |
| 283 | break; |
| 284 | } |
| 285 | break; |
| 286 | } |
| 287 | |
| 288 | case OpName: |
| 289 | { |
| 290 | uint32_t id = ops[0]; |
| 291 | ir.set_name(id, extract_string(ir.spirv, instruction.offset + 1)); |
| 292 | break; |
| 293 | } |
| 294 | |
| 295 | case OpMemberName: |
| 296 | { |
| 297 | uint32_t id = ops[0]; |
| 298 | uint32_t member = ops[1]; |
| 299 | ir.set_member_name(id, member, extract_string(ir.spirv, instruction.offset + 2)); |
| 300 | break; |
| 301 | } |
| 302 | |
| 303 | case OpDecorate: |
| 304 | case OpDecorateId: |
| 305 | { |
| 306 | uint32_t id = ops[0]; |
| 307 | |
| 308 | auto decoration = static_cast<Decoration>(ops[1]); |
| 309 | if (length >= 3) |
| 310 | { |
| 311 | ir.meta[id].decoration_word_offset[decoration] = uint32_t(&ops[2] - ir.spirv.data()); |
| 312 | ir.set_decoration(id, decoration, ops[2]); |
| 313 | } |
| 314 | else |
| 315 | ir.set_decoration(id, decoration); |
| 316 | |
| 317 | break; |
| 318 | } |
| 319 | |
| 320 | case OpDecorateStringGOOGLE: |
| 321 | { |
| 322 | uint32_t id = ops[0]; |
| 323 | auto decoration = static_cast<Decoration>(ops[1]); |
| 324 | ir.set_decoration_string(id, decoration, extract_string(ir.spirv, instruction.offset + 2)); |
| 325 | break; |
| 326 | } |
| 327 | |
| 328 | case OpMemberDecorate: |
| 329 | { |
| 330 | uint32_t id = ops[0]; |
| 331 | uint32_t member = ops[1]; |
| 332 | auto decoration = static_cast<Decoration>(ops[2]); |
| 333 | if (length >= 4) |
| 334 | ir.set_member_decoration(id, member, decoration, ops[3]); |
| 335 | else |
| 336 | ir.set_member_decoration(id, member, decoration); |
| 337 | break; |
| 338 | } |
| 339 | |
| 340 | case OpMemberDecorateStringGOOGLE: |
| 341 | { |
| 342 | uint32_t id = ops[0]; |
| 343 | uint32_t member = ops[1]; |
| 344 | auto decoration = static_cast<Decoration>(ops[2]); |
| 345 | ir.set_member_decoration_string(id, member, decoration, extract_string(ir.spirv, instruction.offset + 3)); |
| 346 | break; |
| 347 | } |
| 348 | |
| 349 | // Build up basic types. |
| 350 | case OpTypeVoid: |
| 351 | { |
| 352 | uint32_t id = ops[0]; |
| 353 | auto &type = set<SPIRType>(id); |
| 354 | type.basetype = SPIRType::Void; |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | case OpTypeBool: |
| 359 | { |
| 360 | uint32_t id = ops[0]; |
| 361 | auto &type = set<SPIRType>(id); |
| 362 | type.basetype = SPIRType::Boolean; |
| 363 | type.width = 1; |
| 364 | break; |
| 365 | } |
| 366 | |
| 367 | case OpTypeFloat: |
| 368 | { |
| 369 | uint32_t id = ops[0]; |
| 370 | uint32_t width = ops[1]; |
| 371 | auto &type = set<SPIRType>(id); |
| 372 | if (width == 64) |
| 373 | type.basetype = SPIRType::Double; |
| 374 | else if (width == 32) |
| 375 | type.basetype = SPIRType::Float; |
| 376 | else if (width == 16) |
| 377 | type.basetype = SPIRType::Half; |
| 378 | else |
| 379 | SPIRV_CROSS_THROW("Unrecognized bit-width of floating point type."); |
| 380 | type.width = width; |
| 381 | break; |
| 382 | } |
| 383 | |
| 384 | case OpTypeInt: |
| 385 | { |
| 386 | uint32_t id = ops[0]; |
| 387 | uint32_t width = ops[1]; |
Chip Davis | 117ccf4 | 2018-11-01 17:20:07 -0500 | [diff] [blame] | 388 | bool signedness = ops[2]; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 389 | auto &type = set<SPIRType>(id); |
Chip Davis | 117ccf4 | 2018-11-01 17:20:07 -0500 | [diff] [blame] | 390 | switch (width) |
| 391 | { |
| 392 | case 64: |
| 393 | type.basetype = signedness ? SPIRType::Int64 : SPIRType::UInt64; |
| 394 | break; |
| 395 | case 32: |
| 396 | type.basetype = signedness ? SPIRType::Int : SPIRType::UInt; |
| 397 | break; |
| 398 | case 16: |
| 399 | type.basetype = signedness ? SPIRType::Short : SPIRType::UShort; |
| 400 | break; |
| 401 | case 8: |
| 402 | type.basetype = signedness ? SPIRType::SByte : SPIRType::UByte; |
| 403 | break; |
| 404 | default: |
| 405 | SPIRV_CROSS_THROW("Unrecognized bit-width of integral type."); |
| 406 | } |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 407 | type.width = width; |
| 408 | break; |
| 409 | } |
| 410 | |
| 411 | // Build composite types by "inheriting". |
| 412 | // NOTE: The self member is also copied! For pointers and array modifiers this is a good thing |
| 413 | // since we can refer to decorations on pointee classes which is needed for UBO/SSBO, I/O blocks in geometry/tess etc. |
| 414 | case OpTypeVector: |
| 415 | { |
| 416 | uint32_t id = ops[0]; |
| 417 | uint32_t vecsize = ops[2]; |
| 418 | |
| 419 | auto &base = get<SPIRType>(ops[1]); |
| 420 | auto &vecbase = set<SPIRType>(id); |
| 421 | |
| 422 | vecbase = base; |
| 423 | vecbase.vecsize = vecsize; |
| 424 | vecbase.self = id; |
| 425 | vecbase.parent_type = ops[1]; |
| 426 | break; |
| 427 | } |
| 428 | |
| 429 | case OpTypeMatrix: |
| 430 | { |
| 431 | uint32_t id = ops[0]; |
| 432 | uint32_t colcount = ops[2]; |
| 433 | |
| 434 | auto &base = get<SPIRType>(ops[1]); |
| 435 | auto &matrixbase = set<SPIRType>(id); |
| 436 | |
| 437 | matrixbase = base; |
| 438 | matrixbase.columns = colcount; |
| 439 | matrixbase.self = id; |
| 440 | matrixbase.parent_type = ops[1]; |
| 441 | break; |
| 442 | } |
| 443 | |
| 444 | case OpTypeArray: |
| 445 | { |
| 446 | uint32_t id = ops[0]; |
| 447 | auto &arraybase = set<SPIRType>(id); |
| 448 | |
| 449 | uint32_t tid = ops[1]; |
| 450 | auto &base = get<SPIRType>(tid); |
| 451 | |
| 452 | arraybase = base; |
| 453 | arraybase.parent_type = tid; |
| 454 | |
| 455 | uint32_t cid = ops[2]; |
| 456 | ir.mark_used_as_array_length(cid); |
| 457 | auto *c = maybe_get<SPIRConstant>(cid); |
| 458 | bool literal = c && !c->specialization; |
| 459 | |
| 460 | arraybase.array_size_literal.push_back(literal); |
| 461 | arraybase.array.push_back(literal ? c->scalar() : cid); |
| 462 | // Do NOT set arraybase.self! |
| 463 | break; |
| 464 | } |
| 465 | |
| 466 | case OpTypeRuntimeArray: |
| 467 | { |
| 468 | uint32_t id = ops[0]; |
| 469 | |
| 470 | auto &base = get<SPIRType>(ops[1]); |
| 471 | auto &arraybase = set<SPIRType>(id); |
| 472 | |
| 473 | arraybase = base; |
| 474 | arraybase.array.push_back(0); |
| 475 | arraybase.array_size_literal.push_back(true); |
| 476 | arraybase.parent_type = ops[1]; |
| 477 | // Do NOT set arraybase.self! |
| 478 | break; |
| 479 | } |
| 480 | |
| 481 | case OpTypeImage: |
| 482 | { |
| 483 | uint32_t id = ops[0]; |
| 484 | auto &type = set<SPIRType>(id); |
| 485 | type.basetype = SPIRType::Image; |
| 486 | type.image.type = ops[1]; |
| 487 | type.image.dim = static_cast<Dim>(ops[2]); |
| 488 | type.image.depth = ops[3] == 1; |
| 489 | type.image.arrayed = ops[4] != 0; |
| 490 | type.image.ms = ops[5] != 0; |
| 491 | type.image.sampled = ops[6]; |
| 492 | type.image.format = static_cast<ImageFormat>(ops[7]); |
| 493 | type.image.access = (length >= 9) ? static_cast<AccessQualifier>(ops[8]) : AccessQualifierMax; |
| 494 | |
| 495 | if (type.image.sampled == 0) |
| 496 | SPIRV_CROSS_THROW("OpTypeImage Sampled parameter must not be zero."); |
| 497 | |
| 498 | break; |
| 499 | } |
| 500 | |
| 501 | case OpTypeSampledImage: |
| 502 | { |
| 503 | uint32_t id = ops[0]; |
| 504 | uint32_t imagetype = ops[1]; |
| 505 | auto &type = set<SPIRType>(id); |
| 506 | type = get<SPIRType>(imagetype); |
| 507 | type.basetype = SPIRType::SampledImage; |
| 508 | type.self = id; |
| 509 | break; |
| 510 | } |
| 511 | |
| 512 | case OpTypeSampler: |
| 513 | { |
| 514 | uint32_t id = ops[0]; |
| 515 | auto &type = set<SPIRType>(id); |
| 516 | type.basetype = SPIRType::Sampler; |
| 517 | break; |
| 518 | } |
| 519 | |
| 520 | case OpTypePointer: |
| 521 | { |
| 522 | uint32_t id = ops[0]; |
| 523 | |
| 524 | auto &base = get<SPIRType>(ops[2]); |
| 525 | auto &ptrbase = set<SPIRType>(id); |
| 526 | |
| 527 | ptrbase = base; |
| 528 | if (ptrbase.pointer) |
| 529 | SPIRV_CROSS_THROW("Cannot make pointer-to-pointer type."); |
| 530 | ptrbase.pointer = true; |
| 531 | ptrbase.storage = static_cast<StorageClass>(ops[1]); |
| 532 | |
| 533 | if (ptrbase.storage == StorageClassAtomicCounter) |
| 534 | ptrbase.basetype = SPIRType::AtomicCounter; |
| 535 | |
| 536 | ptrbase.parent_type = ops[2]; |
| 537 | |
| 538 | // Do NOT set ptrbase.self! |
| 539 | break; |
| 540 | } |
| 541 | |
| 542 | case OpTypeStruct: |
| 543 | { |
| 544 | uint32_t id = ops[0]; |
| 545 | auto &type = set<SPIRType>(id); |
| 546 | type.basetype = SPIRType::Struct; |
| 547 | for (uint32_t i = 1; i < length; i++) |
| 548 | type.member_types.push_back(ops[i]); |
| 549 | |
| 550 | // Check if we have seen this struct type before, with just different |
| 551 | // decorations. |
| 552 | // |
| 553 | // Add workaround for issue #17 as well by looking at OpName for the struct |
| 554 | // types, which we shouldn't normally do. |
| 555 | // We should not normally have to consider type aliases like this to begin with |
| 556 | // however ... glslang issues #304, #307 cover this. |
| 557 | |
| 558 | // For stripped names, never consider struct type aliasing. |
| 559 | // We risk declaring the same struct multiple times, but type-punning is not allowed |
| 560 | // so this is safe. |
| 561 | bool consider_aliasing = !ir.get_name(type.self).empty(); |
| 562 | if (consider_aliasing) |
| 563 | { |
| 564 | for (auto &other : global_struct_cache) |
| 565 | { |
| 566 | if (ir.get_name(type.self) == ir.get_name(other) && |
| 567 | types_are_logically_equivalent(type, get<SPIRType>(other))) |
| 568 | { |
| 569 | type.type_alias = other; |
| 570 | break; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | if (type.type_alias == 0) |
| 575 | global_struct_cache.push_back(id); |
| 576 | } |
| 577 | break; |
| 578 | } |
| 579 | |
| 580 | case OpTypeFunction: |
| 581 | { |
| 582 | uint32_t id = ops[0]; |
| 583 | uint32_t ret = ops[1]; |
| 584 | |
| 585 | auto &func = set<SPIRFunctionPrototype>(id, ret); |
| 586 | for (uint32_t i = 2; i < length; i++) |
| 587 | func.parameter_types.push_back(ops[i]); |
| 588 | break; |
| 589 | } |
| 590 | |
| 591 | // Variable declaration |
| 592 | // All variables are essentially pointers with a storage qualifier. |
| 593 | case OpVariable: |
| 594 | { |
| 595 | uint32_t type = ops[0]; |
| 596 | uint32_t id = ops[1]; |
| 597 | auto storage = static_cast<StorageClass>(ops[2]); |
| 598 | uint32_t initializer = length == 4 ? ops[3] : 0; |
| 599 | |
| 600 | if (storage == StorageClassFunction) |
| 601 | { |
| 602 | if (!current_function) |
| 603 | SPIRV_CROSS_THROW("No function currently in scope"); |
| 604 | current_function->add_local_variable(id); |
| 605 | } |
| 606 | |
| 607 | set<SPIRVariable>(id, type, storage, initializer); |
| 608 | |
| 609 | // hlsl based shaders don't have those decorations. force them and then reset when reading/writing images |
| 610 | auto &ttype = get<SPIRType>(type); |
| 611 | if (ttype.basetype == SPIRType::BaseType::Image) |
| 612 | { |
| 613 | ir.set_decoration(id, DecorationNonWritable); |
| 614 | ir.set_decoration(id, DecorationNonReadable); |
| 615 | } |
| 616 | |
| 617 | break; |
| 618 | } |
| 619 | |
| 620 | // OpPhi |
| 621 | // OpPhi is a fairly magical opcode. |
| 622 | // It selects temporary variables based on which parent block we *came from*. |
| 623 | // In high-level languages we can "de-SSA" by creating a function local, and flush out temporaries to this function-local |
| 624 | // variable to emulate SSA Phi. |
| 625 | case OpPhi: |
| 626 | { |
| 627 | if (!current_function) |
| 628 | SPIRV_CROSS_THROW("No function currently in scope"); |
| 629 | if (!current_block) |
| 630 | SPIRV_CROSS_THROW("No block currently in scope"); |
| 631 | |
| 632 | uint32_t result_type = ops[0]; |
| 633 | uint32_t id = ops[1]; |
| 634 | |
| 635 | // Instead of a temporary, create a new function-wide temporary with this ID instead. |
| 636 | auto &var = set<SPIRVariable>(id, result_type, spv::StorageClassFunction); |
| 637 | var.phi_variable = true; |
| 638 | |
| 639 | current_function->add_local_variable(id); |
| 640 | |
| 641 | for (uint32_t i = 2; i + 2 <= length; i += 2) |
| 642 | current_block->phi_variables.push_back({ ops[i], ops[i + 1], id }); |
| 643 | break; |
| 644 | } |
| 645 | |
| 646 | // Constants |
| 647 | case OpSpecConstant: |
| 648 | case OpConstant: |
| 649 | { |
| 650 | uint32_t id = ops[1]; |
| 651 | auto &type = get<SPIRType>(ops[0]); |
| 652 | |
| 653 | if (type.width > 32) |
| 654 | set<SPIRConstant>(id, ops[0], ops[2] | (uint64_t(ops[3]) << 32), op == OpSpecConstant); |
| 655 | else |
| 656 | set<SPIRConstant>(id, ops[0], ops[2], op == OpSpecConstant); |
| 657 | break; |
| 658 | } |
| 659 | |
| 660 | case OpSpecConstantFalse: |
| 661 | case OpConstantFalse: |
| 662 | { |
| 663 | uint32_t id = ops[1]; |
| 664 | set<SPIRConstant>(id, ops[0], uint32_t(0), op == OpSpecConstantFalse); |
| 665 | break; |
| 666 | } |
| 667 | |
| 668 | case OpSpecConstantTrue: |
| 669 | case OpConstantTrue: |
| 670 | { |
| 671 | uint32_t id = ops[1]; |
| 672 | set<SPIRConstant>(id, ops[0], uint32_t(1), op == OpSpecConstantTrue); |
| 673 | break; |
| 674 | } |
| 675 | |
| 676 | case OpConstantNull: |
| 677 | { |
| 678 | uint32_t id = ops[1]; |
| 679 | uint32_t type = ops[0]; |
| 680 | make_constant_null(id, type); |
| 681 | break; |
| 682 | } |
| 683 | |
| 684 | case OpSpecConstantComposite: |
| 685 | case OpConstantComposite: |
| 686 | { |
| 687 | uint32_t id = ops[1]; |
| 688 | uint32_t type = ops[0]; |
| 689 | |
| 690 | auto &ctype = get<SPIRType>(type); |
| 691 | |
| 692 | // We can have constants which are structs and arrays. |
| 693 | // In this case, our SPIRConstant will be a list of other SPIRConstant ids which we |
| 694 | // can refer to. |
| 695 | if (ctype.basetype == SPIRType::Struct || !ctype.array.empty()) |
| 696 | { |
| 697 | set<SPIRConstant>(id, type, ops + 2, length - 2, op == OpSpecConstantComposite); |
| 698 | } |
| 699 | else |
| 700 | { |
| 701 | uint32_t elements = length - 2; |
| 702 | if (elements > 4) |
| 703 | SPIRV_CROSS_THROW("OpConstantComposite only supports 1, 2, 3 and 4 elements."); |
| 704 | |
| 705 | SPIRConstant remapped_constant_ops[4]; |
| 706 | const SPIRConstant *c[4]; |
| 707 | for (uint32_t i = 0; i < elements; i++) |
| 708 | { |
| 709 | // Specialization constants operations can also be part of this. |
| 710 | // We do not know their value, so any attempt to query SPIRConstant later |
| 711 | // will fail. We can only propagate the ID of the expression and use to_expression on it. |
| 712 | auto *constant_op = maybe_get<SPIRConstantOp>(ops[2 + i]); |
| 713 | if (constant_op) |
| 714 | { |
| 715 | if (op == OpConstantComposite) |
| 716 | SPIRV_CROSS_THROW("Specialization constant operation used in OpConstantComposite."); |
| 717 | |
| 718 | remapped_constant_ops[i].make_null(get<SPIRType>(constant_op->basetype)); |
| 719 | remapped_constant_ops[i].self = constant_op->self; |
| 720 | remapped_constant_ops[i].constant_type = constant_op->basetype; |
| 721 | remapped_constant_ops[i].specialization = true; |
| 722 | c[i] = &remapped_constant_ops[i]; |
| 723 | } |
| 724 | else |
| 725 | c[i] = &get<SPIRConstant>(ops[2 + i]); |
| 726 | } |
| 727 | set<SPIRConstant>(id, type, c, elements, op == OpSpecConstantComposite); |
| 728 | } |
| 729 | break; |
| 730 | } |
| 731 | |
| 732 | // Functions |
| 733 | case OpFunction: |
| 734 | { |
| 735 | uint32_t res = ops[0]; |
| 736 | uint32_t id = ops[1]; |
| 737 | // Control |
| 738 | uint32_t type = ops[3]; |
| 739 | |
| 740 | if (current_function) |
| 741 | SPIRV_CROSS_THROW("Must end a function before starting a new one!"); |
| 742 | |
| 743 | current_function = &set<SPIRFunction>(id, res, type); |
| 744 | break; |
| 745 | } |
| 746 | |
| 747 | case OpFunctionParameter: |
| 748 | { |
| 749 | uint32_t type = ops[0]; |
| 750 | uint32_t id = ops[1]; |
| 751 | |
| 752 | if (!current_function) |
| 753 | SPIRV_CROSS_THROW("Must be in a function!"); |
| 754 | |
| 755 | current_function->add_parameter(type, id); |
| 756 | set<SPIRVariable>(id, type, StorageClassFunction); |
| 757 | break; |
| 758 | } |
| 759 | |
| 760 | case OpFunctionEnd: |
| 761 | { |
| 762 | if (current_block) |
| 763 | { |
| 764 | // Very specific error message, but seems to come up quite often. |
| 765 | SPIRV_CROSS_THROW( |
| 766 | "Cannot end a function before ending the current block.\n" |
| 767 | "Likely cause: If this SPIR-V was created from glslang HLSL, make sure the entry point is valid."); |
| 768 | } |
| 769 | current_function = nullptr; |
| 770 | break; |
| 771 | } |
| 772 | |
| 773 | // Blocks |
| 774 | case OpLabel: |
| 775 | { |
| 776 | // OpLabel always starts a block. |
| 777 | if (!current_function) |
| 778 | SPIRV_CROSS_THROW("Blocks cannot exist outside functions!"); |
| 779 | |
| 780 | uint32_t id = ops[0]; |
| 781 | |
| 782 | current_function->blocks.push_back(id); |
| 783 | if (!current_function->entry_block) |
| 784 | current_function->entry_block = id; |
| 785 | |
| 786 | if (current_block) |
| 787 | SPIRV_CROSS_THROW("Cannot start a block before ending the current block."); |
| 788 | |
| 789 | current_block = &set<SPIRBlock>(id); |
| 790 | break; |
| 791 | } |
| 792 | |
| 793 | // Branch instructions end blocks. |
| 794 | case OpBranch: |
| 795 | { |
| 796 | if (!current_block) |
| 797 | SPIRV_CROSS_THROW("Trying to end a non-existing block."); |
| 798 | |
| 799 | uint32_t target = ops[0]; |
| 800 | current_block->terminator = SPIRBlock::Direct; |
| 801 | current_block->next_block = target; |
| 802 | current_block = nullptr; |
| 803 | break; |
| 804 | } |
| 805 | |
| 806 | case OpBranchConditional: |
| 807 | { |
| 808 | if (!current_block) |
| 809 | SPIRV_CROSS_THROW("Trying to end a non-existing block."); |
| 810 | |
| 811 | current_block->condition = ops[0]; |
| 812 | current_block->true_block = ops[1]; |
| 813 | current_block->false_block = ops[2]; |
| 814 | |
| 815 | current_block->terminator = SPIRBlock::Select; |
| 816 | current_block = nullptr; |
| 817 | break; |
| 818 | } |
| 819 | |
| 820 | case OpSwitch: |
| 821 | { |
| 822 | if (!current_block) |
| 823 | SPIRV_CROSS_THROW("Trying to end a non-existing block."); |
| 824 | |
| 825 | if (current_block->merge == SPIRBlock::MergeNone) |
| 826 | SPIRV_CROSS_THROW("Switch statement is not structured"); |
| 827 | |
| 828 | current_block->terminator = SPIRBlock::MultiSelect; |
| 829 | |
| 830 | current_block->condition = ops[0]; |
| 831 | current_block->default_block = ops[1]; |
| 832 | |
| 833 | for (uint32_t i = 2; i + 2 <= length; i += 2) |
| 834 | current_block->cases.push_back({ ops[i], ops[i + 1] }); |
| 835 | |
| 836 | // If we jump to next block, make it break instead since we're inside a switch case block at that point. |
| 837 | ir.block_meta[current_block->next_block] |= ParsedIR::BLOCK_META_MULTISELECT_MERGE_BIT; |
| 838 | |
| 839 | current_block = nullptr; |
| 840 | break; |
| 841 | } |
| 842 | |
| 843 | case OpKill: |
| 844 | { |
| 845 | if (!current_block) |
| 846 | SPIRV_CROSS_THROW("Trying to end a non-existing block."); |
| 847 | current_block->terminator = SPIRBlock::Kill; |
| 848 | current_block = nullptr; |
| 849 | break; |
| 850 | } |
| 851 | |
| 852 | case OpReturn: |
| 853 | { |
| 854 | if (!current_block) |
| 855 | SPIRV_CROSS_THROW("Trying to end a non-existing block."); |
| 856 | current_block->terminator = SPIRBlock::Return; |
| 857 | current_block = nullptr; |
| 858 | break; |
| 859 | } |
| 860 | |
| 861 | case OpReturnValue: |
| 862 | { |
| 863 | if (!current_block) |
| 864 | SPIRV_CROSS_THROW("Trying to end a non-existing block."); |
| 865 | current_block->terminator = SPIRBlock::Return; |
| 866 | current_block->return_value = ops[0]; |
| 867 | current_block = nullptr; |
| 868 | break; |
| 869 | } |
| 870 | |
| 871 | case OpUnreachable: |
| 872 | { |
| 873 | if (!current_block) |
| 874 | SPIRV_CROSS_THROW("Trying to end a non-existing block."); |
| 875 | current_block->terminator = SPIRBlock::Unreachable; |
| 876 | current_block = nullptr; |
| 877 | break; |
| 878 | } |
| 879 | |
| 880 | case OpSelectionMerge: |
| 881 | { |
| 882 | if (!current_block) |
| 883 | SPIRV_CROSS_THROW("Trying to modify a non-existing block."); |
| 884 | |
| 885 | current_block->next_block = ops[0]; |
| 886 | current_block->merge = SPIRBlock::MergeSelection; |
| 887 | ir.block_meta[current_block->next_block] |= ParsedIR::BLOCK_META_SELECTION_MERGE_BIT; |
| 888 | |
| 889 | if (length >= 2) |
| 890 | { |
| 891 | if (ops[1] & SelectionControlFlattenMask) |
| 892 | current_block->hint = SPIRBlock::HintFlatten; |
| 893 | else if (ops[1] & SelectionControlDontFlattenMask) |
| 894 | current_block->hint = SPIRBlock::HintDontFlatten; |
| 895 | } |
| 896 | break; |
| 897 | } |
| 898 | |
| 899 | case OpLoopMerge: |
| 900 | { |
| 901 | if (!current_block) |
| 902 | SPIRV_CROSS_THROW("Trying to modify a non-existing block."); |
| 903 | |
| 904 | current_block->merge_block = ops[0]; |
| 905 | current_block->continue_block = ops[1]; |
| 906 | current_block->merge = SPIRBlock::MergeLoop; |
| 907 | |
| 908 | ir.block_meta[current_block->self] |= ParsedIR::BLOCK_META_LOOP_HEADER_BIT; |
| 909 | ir.block_meta[current_block->merge_block] |= ParsedIR::BLOCK_META_LOOP_MERGE_BIT; |
| 910 | |
| 911 | ir.continue_block_to_loop_header[current_block->continue_block] = current_block->self; |
| 912 | |
| 913 | // Don't add loop headers to continue blocks, |
| 914 | // which would make it impossible branch into the loop header since |
| 915 | // they are treated as continues. |
| 916 | if (current_block->continue_block != current_block->self) |
| 917 | ir.block_meta[current_block->continue_block] |= ParsedIR::BLOCK_META_CONTINUE_BIT; |
| 918 | |
| 919 | if (length >= 3) |
| 920 | { |
| 921 | if (ops[2] & LoopControlUnrollMask) |
| 922 | current_block->hint = SPIRBlock::HintUnroll; |
| 923 | else if (ops[2] & LoopControlDontUnrollMask) |
| 924 | current_block->hint = SPIRBlock::HintDontUnroll; |
| 925 | } |
| 926 | break; |
| 927 | } |
| 928 | |
| 929 | case OpSpecConstantOp: |
| 930 | { |
| 931 | if (length < 3) |
| 932 | SPIRV_CROSS_THROW("OpSpecConstantOp not enough arguments."); |
| 933 | |
| 934 | uint32_t result_type = ops[0]; |
| 935 | uint32_t id = ops[1]; |
| 936 | auto spec_op = static_cast<Op>(ops[2]); |
| 937 | |
| 938 | set<SPIRConstantOp>(id, result_type, spec_op, ops + 3, length - 3); |
| 939 | break; |
| 940 | } |
| 941 | |
| 942 | // Actual opcodes. |
| 943 | default: |
| 944 | { |
| 945 | if (!current_block) |
| 946 | SPIRV_CROSS_THROW("Currently no block to insert opcode."); |
| 947 | |
| 948 | current_block->ops.push_back(instruction); |
| 949 | break; |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | bool Parser::types_are_logically_equivalent(const SPIRType &a, const SPIRType &b) const |
| 955 | { |
| 956 | if (a.basetype != b.basetype) |
| 957 | return false; |
| 958 | if (a.width != b.width) |
| 959 | return false; |
| 960 | if (a.vecsize != b.vecsize) |
| 961 | return false; |
| 962 | if (a.columns != b.columns) |
| 963 | return false; |
| 964 | if (a.array.size() != b.array.size()) |
| 965 | return false; |
| 966 | |
| 967 | size_t array_count = a.array.size(); |
| 968 | if (array_count && memcmp(a.array.data(), b.array.data(), array_count * sizeof(uint32_t)) != 0) |
| 969 | return false; |
| 970 | |
| 971 | if (a.basetype == SPIRType::Image || a.basetype == SPIRType::SampledImage) |
| 972 | { |
| 973 | if (memcmp(&a.image, &b.image, sizeof(SPIRType::Image)) != 0) |
| 974 | return false; |
| 975 | } |
| 976 | |
| 977 | if (a.member_types.size() != b.member_types.size()) |
| 978 | return false; |
| 979 | |
| 980 | size_t member_types = a.member_types.size(); |
| 981 | for (size_t i = 0; i < member_types; i++) |
| 982 | { |
| 983 | if (!types_are_logically_equivalent(get<SPIRType>(a.member_types[i]), get<SPIRType>(b.member_types[i]))) |
| 984 | return false; |
| 985 | } |
| 986 | |
| 987 | return true; |
| 988 | } |
| 989 | |
| 990 | bool Parser::variable_storage_is_aliased(const SPIRVariable &v) const |
| 991 | { |
| 992 | auto &type = get<SPIRType>(v.basetype); |
| 993 | bool ssbo = v.storage == StorageClassStorageBuffer || |
| 994 | ir.meta[type.self].decoration.decoration_flags.get(DecorationBufferBlock); |
| 995 | bool image = type.basetype == SPIRType::Image; |
| 996 | bool counter = type.basetype == SPIRType::AtomicCounter; |
| 997 | |
| 998 | bool is_restrict; |
| 999 | if (ssbo) |
| 1000 | is_restrict = ir.get_buffer_block_flags(v).get(DecorationRestrict); |
| 1001 | else |
| 1002 | is_restrict = ir.has_decoration(v.self, DecorationRestrict); |
| 1003 | |
| 1004 | return !is_restrict && (ssbo || image || counter); |
| 1005 | } |
| 1006 | |
| 1007 | void Parser::make_constant_null(uint32_t id, uint32_t type) |
| 1008 | { |
| 1009 | auto &constant_type = get<SPIRType>(type); |
| 1010 | |
| 1011 | if (!constant_type.array.empty()) |
| 1012 | { |
| 1013 | assert(constant_type.parent_type); |
| 1014 | uint32_t parent_id = ir.increase_bound_by(1); |
| 1015 | make_constant_null(parent_id, constant_type.parent_type); |
| 1016 | |
| 1017 | if (!constant_type.array_size_literal.back()) |
| 1018 | SPIRV_CROSS_THROW("Array size of OpConstantNull must be a literal."); |
| 1019 | |
| 1020 | vector<uint32_t> elements(constant_type.array.back()); |
| 1021 | for (uint32_t i = 0; i < constant_type.array.back(); i++) |
| 1022 | elements[i] = parent_id; |
| 1023 | set<SPIRConstant>(id, type, elements.data(), uint32_t(elements.size()), false); |
| 1024 | } |
| 1025 | else if (!constant_type.member_types.empty()) |
| 1026 | { |
| 1027 | uint32_t member_ids = ir.increase_bound_by(uint32_t(constant_type.member_types.size())); |
| 1028 | vector<uint32_t> elements(constant_type.member_types.size()); |
| 1029 | for (uint32_t i = 0; i < constant_type.member_types.size(); i++) |
| 1030 | { |
| 1031 | make_constant_null(member_ids + i, constant_type.member_types[i]); |
| 1032 | elements[i] = member_ids + i; |
| 1033 | } |
| 1034 | set<SPIRConstant>(id, type, elements.data(), uint32_t(elements.size()), false); |
| 1035 | } |
| 1036 | else |
| 1037 | { |
| 1038 | auto &constant = set<SPIRConstant>(id, type); |
| 1039 | constant.make_null(constant_type); |
| 1040 | } |
| 1041 | } |
| 1042 | |
Chip Davis | 117ccf4 | 2018-11-01 17:20:07 -0500 | [diff] [blame] | 1043 | } // namespace spirv_cross |