MCPcopy Create free account
hub / github.com/NVIDIA-RTX/Donut / LoadDDSTextureFromMemory

Function LoadDDSTextureFromMemory

src/engine/DDSFile.cpp:567–753  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

565 }
566
567 bool LoadDDSTextureFromMemory(TextureData& textureInfo)
568 {
569 if (textureInfo.data->size() < sizeof(uint32_t) + sizeof(DDS_HEADER))
570 {
571 return false;
572 }
573
574 auto dwMagicNumber = *reinterpret_cast<const uint32_t*>(textureInfo.data->data());
575 if (dwMagicNumber != DDS_MAGIC)
576 {
577 return false;
578 }
579
580 auto header = reinterpret_cast<const DDS_HEADER*>(static_cast<const char*>(textureInfo.data->data()) + sizeof(uint32_t));
581
582 // Verify header to validate DDS file
583 if (header->size != sizeof(DDS_HEADER) ||
584 header->ddspf.size != sizeof(DDS_PIXELFORMAT))
585 {
586 return false;
587 }
588
589 // Check for DX10 extension
590 bool bDXT10Header = false;
591 if ((header->ddspf.flags & DDS_FOURCC) &&
592 (MAKEFOURCC('D', 'X', '1', '0') == header->ddspf.fourCC))
593 {
594 // Must be long enough for both headers and magic value
595 if (textureInfo.data->size() < (sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10)))
596 {
597 return false;
598 }
599
600 bDXT10Header = true;
601 }
602
603 ptrdiff_t dataOffset = sizeof(uint32_t)
604 + sizeof(DDS_HEADER)
605 + (bDXT10Header ? sizeof(DDS_HEADER_DXT10) : 0);
606
607 textureInfo.width = header->width;
608 textureInfo.height = header->height;
609 textureInfo.mipLevels = header->mipMapCount ? header->mipMapCount : 1;
610 textureInfo.depth = 1;
611 textureInfo.arraySize = 1;
612 textureInfo.alphaMode = GetAlphaMode(header);
613
614 if ((header->ddspf.flags & DDS_FOURCC) &&
615 (MAKEFOURCC('D', 'X', '1', '0') == header->ddspf.fourCC))
616 {
617 auto d3d10ext = reinterpret_cast<const DDS_HEADER_DXT10*>(reinterpret_cast<const char*>(header) + sizeof(DDS_HEADER));
618
619 if (d3d10ext->arraySize == 0)
620 {
621 return false;
622 }
623
624 for (const FormatMapping& mapping : g_FormatMappings)

Callers 2

FillTextureDataMethod · 0.85
CreateDDSTextureInternalFunction · 0.85

Calls 5

GetAlphaModeFunction · 0.85
ConvertDDSFormatFunction · 0.85
FillTextureInfoOffsetsFunction · 0.85
sizeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected