Reads image information from the DDS header data contained in pHeaderData.
| 410 | |
| 411 | // Reads image information from the DDS header data contained in pHeaderData. |
| 412 | void readDDSHeader(ImportData& data, const void* pHeaderData, size_t& headerSize, bool loadAsSrgb) |
| 413 | { |
| 414 | // Check magic number |
| 415 | auto magic = *static_cast<const uint32_t*>(pHeaderData); |
| 416 | if (magic != DDS_MAGIC) |
| 417 | { |
| 418 | FALCOR_THROW("Unexpected magic number for a DDS file."); |
| 419 | } |
| 420 | |
| 421 | // Check size fields for both the DDS_HEADER and DDS_PIXELFORMAT structs |
| 422 | auto pHeader = reinterpret_cast<const DDS_HEADER*>(static_cast<const uint8_t*>(pHeaderData) + sizeof(uint32_t)); |
| 423 | if (pHeader->size != sizeof(DDS_HEADER) || pHeader->ddspf.size != sizeof(DDS_PIXELFORMAT)) |
| 424 | { |
| 425 | FALCOR_THROW("DDS header size mismatch."); |
| 426 | } |
| 427 | |
| 428 | // Check for the presence of the extended DX10 header and fill in ImportData fields with their corresponding values |
| 429 | data.mipLevels = (pHeader->mipMapCount == 0) ? 1 : pHeader->mipMapCount; |
| 430 | auto pixelFormat = pHeader->ddspf; |
| 431 | auto fourCC = pixelFormat.fourCC; |
| 432 | if (fourCC == MAKEFOURCC('D', 'X', '1', '0')) |
| 433 | { |
| 434 | // DX10 header extension is present |
| 435 | data.hasDX10Header = true; |
| 436 | if (headerSize != sizeof(uint32_t) + sizeof(DDS_HEADER) + sizeof(DDS_HEADER_DXT10)) |
| 437 | { |
| 438 | FALCOR_THROW("DX10 header extension size mismatch."); |
| 439 | } |
| 440 | auto pDX10Header = |
| 441 | reinterpret_cast<const DDS_HEADER_DXT10*>(static_cast<const uint8_t*>(pHeaderData) + sizeof(uint32_t) + sizeof(DDS_HEADER)); |
| 442 | data.arraySize = pDX10Header->arraySize; |
| 443 | if (data.arraySize == 0) |
| 444 | { |
| 445 | FALCOR_THROW("Array size cannot be zero."); |
| 446 | } |
| 447 | data.format = getResourceFormat(pDX10Header->dxgiFormat); |
| 448 | switch (pDX10Header->resourceDimension) |
| 449 | { |
| 450 | case DDS_DIMENSION_TEXTURE1D: |
| 451 | data.width = pHeader->width; |
| 452 | data.height = 1; |
| 453 | data.depth = 1; |
| 454 | data.type = Resource::Type::Texture1D; |
| 455 | break; |
| 456 | case DDS_DIMENSION_TEXTURE2D: |
| 457 | data.width = pHeader->width; |
| 458 | data.height = pHeader->height; |
| 459 | data.depth = 1; |
| 460 | if ((pDX10Header->miscFlag & DDS_RESOURCE_MISC_TEXTURECUBE) != 0) |
| 461 | { |
| 462 | data.type = Resource::Type::TextureCube; |
| 463 | data.arraySize *= 6; |
| 464 | } |
| 465 | else |
| 466 | { |
| 467 | data.type = Resource::Type::Texture2D; |
| 468 | } |
| 469 | break; |
no test coverage detected