Skip to content

Commit b2e73bb

Browse files
committed
Move Xbox structs to dds.h
1 parent 23bad7e commit b2e73bb

File tree

3 files changed

+76
-71
lines changed

3 files changed

+76
-71
lines changed

Auxiliary/DirectXTexXboxDDS.cpp

+22-50
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,6 @@ using namespace Xbox;
2323

2424
namespace
2525
{
26-
const DDS_PIXELFORMAT DDSPF_XBOX =
27-
{ sizeof(DDS_PIXELFORMAT), DDS_FOURCC, MAKEFOURCC('X','B','O','X'), 0, 0, 0, 0, 0 };
28-
29-
#pragma pack(push,1)
30-
31-
struct DDS_HEADER_XBOX
32-
// Must match structure in XboxDDSTextureLoader module
33-
{
34-
DXGI_FORMAT dxgiFormat;
35-
uint32_t resourceDimension;
36-
uint32_t miscFlag; // see DDS_RESOURCE_MISC_FLAG
37-
uint32_t arraySize;
38-
uint32_t miscFlags2; // see DDS_MISC_FLAGS2
39-
uint32_t tileMode; // see XG_TILE_MODE / XG_SWIZZLE_MODE
40-
uint32_t baseAlignment;
41-
uint32_t dataSize;
42-
uint32_t xdkVer; // matching _XDK_VER / _GXDK_VER
43-
};
44-
45-
#pragma pack(pop)
46-
47-
constexpr uint32_t XBOX_TILEMODE_SCARLETT = 0x1000000;
48-
49-
static_assert(sizeof(DDS_HEADER_XBOX) == 36, "DDS XBOX Header size mismatch");
50-
static_assert(sizeof(DDS_HEADER_XBOX) >= sizeof(DDS_HEADER_DXT10), "DDS XBOX Header should be larger than DX10 header");
51-
52-
constexpr size_t XBOX_HEADER_SIZE = sizeof(uint32_t) + sizeof(DDS_HEADER) + sizeof(DDS_HEADER_XBOX);
53-
5426
//-------------------------------------------------------------------------------------
5527
// Decodes DDS header using XBOX extended header (variant of DX10 header)
5628
//-------------------------------------------------------------------------------------
@@ -86,7 +58,7 @@ namespace
8658
*ddPixelFormat = {};
8759
}
8860

89-
if (size < (sizeof(DDS_HEADER) + sizeof(uint32_t)))
61+
if (size < DDS_MIN_HEADER_SIZE)
9062
{
9163
return HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
9264
}
@@ -120,13 +92,13 @@ namespace
12092
}
12193

12294
// Buffer must be big enough for both headers and magic value
123-
if (size < XBOX_HEADER_SIZE)
95+
if (size < DDS_XBOX_HEADER_SIZE)
12496
{
12597
return E_FAIL;
12698
}
12799

128100
auto xboxext = reinterpret_cast<const DDS_HEADER_XBOX*>(
129-
reinterpret_cast<const uint8_t*>(pSource) + sizeof(uint32_t) + sizeof(DDS_HEADER));
101+
reinterpret_cast<const uint8_t*>(pSource) + DDS_MIN_HEADER_SIZE);
130102

131103
metadata.arraySize = xboxext->arraySize;
132104
if (metadata.arraySize == 0)
@@ -263,7 +235,7 @@ HRESULT Xbox::EncodeDDSHeader(
263235
if (!pDestination)
264236
return E_INVALIDARG;
265237

266-
if (maxsize < XBOX_HEADER_SIZE)
238+
if (maxsize < DDS_XBOX_HEADER_SIZE)
267239
return E_NOT_SUFFICIENT_BUFFER;
268240

269241
*reinterpret_cast<uint32_t*>(pDestination) = DDS_MAGIC;
@@ -492,16 +464,16 @@ HRESULT Xbox::GetMetadataFromDDSFileEx(
492464
}
493465

494466
// Need at least enough data to fill the standard header and magic number to be a valid DDS
495-
if (fileInfo.EndOfFile.LowPart < (sizeof(DDS_HEADER) + sizeof(uint32_t)))
467+
if (fileInfo.EndOfFile.LowPart < DDS_MIN_HEADER_SIZE)
496468
{
497469
return E_FAIL;
498470
}
499471

500472
// Read the header in (including extended header if present)
501-
uint8_t header[XBOX_HEADER_SIZE] = {};
473+
uint8_t header[DDS_XBOX_HEADER_SIZE] = {};
502474

503475
DWORD bytesRead = 0;
504-
if (!ReadFile(hFile.get(), header, XBOX_HEADER_SIZE, &bytesRead, nullptr))
476+
if (!ReadFile(hFile.get(), header, DDS_XBOX_HEADER_SIZE, &bytesRead, nullptr))
505477
{
506478
return HRESULT_FROM_WIN32(GetLastError());
507479
}
@@ -567,13 +539,13 @@ HRESULT Xbox::LoadFromDDSMemoryEx(
567539
return E_FAIL;
568540
}
569541

570-
if (size <= XBOX_HEADER_SIZE)
542+
if (size <= DDS_XBOX_HEADER_SIZE)
571543
{
572544
return E_FAIL;
573545
}
574546

575547
// Copy tiled data
576-
const size_t remaining = size - XBOX_HEADER_SIZE;
548+
const size_t remaining = size - DDS_XBOX_HEADER_SIZE;
577549

578550
if (remaining < dataSize)
579551
{
@@ -586,7 +558,7 @@ HRESULT Xbox::LoadFromDDSMemoryEx(
586558

587559
assert(xbox.GetPointer() != nullptr);
588560

589-
memcpy(xbox.GetPointer(), reinterpret_cast<const uint8_t*>(pSource) + XBOX_HEADER_SIZE, dataSize);
561+
memcpy(xbox.GetPointer(), reinterpret_cast<const uint8_t*>(pSource) + DDS_XBOX_HEADER_SIZE, dataSize);
590562

591563
if (metadata)
592564
memcpy(metadata, &mdata, sizeof(TexMetadata));
@@ -645,16 +617,16 @@ HRESULT Xbox::LoadFromDDSFileEx(
645617
}
646618

647619
// Need at least enough data to fill the standard header and magic number to be a valid DDS
648-
if (fileInfo.EndOfFile.LowPart < (sizeof(DDS_HEADER) + sizeof(uint32_t)))
620+
if (fileInfo.EndOfFile.LowPart < DDS_MIN_HEADER_SIZE)
649621
{
650622
return E_FAIL;
651623
}
652624

653625
// Read the header in (including extended header if present)
654-
uint8_t header[XBOX_HEADER_SIZE] = {};
626+
uint8_t header[DDS_XBOX_HEADER_SIZE] = {};
655627

656628
DWORD bytesRead = 0;
657-
if (!ReadFile(hFile.get(), header, XBOX_HEADER_SIZE, &bytesRead, nullptr))
629+
if (!ReadFile(hFile.get(), header, DDS_XBOX_HEADER_SIZE, &bytesRead, nullptr))
658630
{
659631
return HRESULT_FROM_WIN32(GetLastError());
660632
}
@@ -678,7 +650,7 @@ HRESULT Xbox::LoadFromDDSFileEx(
678650
}
679651

680652
// Read tiled data
681-
const DWORD remaining = fileInfo.EndOfFile.LowPart - XBOX_HEADER_SIZE;
653+
const DWORD remaining = fileInfo.EndOfFile.LowPart - DDS_XBOX_HEADER_SIZE;
682654
if (remaining == 0)
683655
return E_FAIL;
684656

@@ -717,24 +689,24 @@ HRESULT Xbox::SaveToDDSMemory(const XboxImage& xbox, Blob& blob)
717689

718690
blob.Release();
719691

720-
HRESULT hr = blob.Initialize(XBOX_HEADER_SIZE + xbox.GetSize());
692+
HRESULT hr = blob.Initialize(DDS_XBOX_HEADER_SIZE + xbox.GetSize());
721693
if (FAILED(hr))
722694
return hr;
723695

724696
// Copy header
725697
auto pDestination = reinterpret_cast<uint8_t*>(blob.GetBufferPointer());
726698
assert(pDestination);
727699

728-
hr = EncodeDDSHeader(xbox, pDestination, XBOX_HEADER_SIZE);
700+
hr = EncodeDDSHeader(xbox, pDestination, DDS_XBOX_HEADER_SIZE);
729701
if (FAILED(hr))
730702
{
731703
blob.Release();
732704
return hr;
733705
}
734706

735707
// Copy tiled data
736-
const size_t remaining = blob.GetBufferSize() - XBOX_HEADER_SIZE;
737-
pDestination += XBOX_HEADER_SIZE;
708+
const size_t remaining = blob.GetBufferSize() - DDS_XBOX_HEADER_SIZE;
709+
pDestination += DDS_XBOX_HEADER_SIZE;
738710

739711
if (!remaining)
740712
{
@@ -764,8 +736,8 @@ HRESULT Xbox::SaveToDDSFile(const XboxImage& xbox, const wchar_t* szFile)
764736
return E_INVALIDARG;
765737

766738
// Create DDS Header
767-
uint8_t header[XBOX_HEADER_SIZE] = {};
768-
HRESULT hr = EncodeDDSHeader(xbox, header, XBOX_HEADER_SIZE);
739+
uint8_t header[DDS_XBOX_HEADER_SIZE] = {};
740+
HRESULT hr = EncodeDDSHeader(xbox, header, DDS_XBOX_HEADER_SIZE);
769741
if (FAILED(hr))
770742
return hr;
771743

@@ -783,12 +755,12 @@ HRESULT Xbox::SaveToDDSFile(const XboxImage& xbox, const wchar_t* szFile)
783755
}
784756

785757
DWORD bytesWritten;
786-
if (!WriteFile(hFile.get(), header, static_cast<DWORD>(XBOX_HEADER_SIZE), &bytesWritten, nullptr))
758+
if (!WriteFile(hFile.get(), header, static_cast<DWORD>(DDS_XBOX_HEADER_SIZE), &bytesWritten, nullptr))
787759
{
788760
return HRESULT_FROM_WIN32(GetLastError());
789761
}
790762

791-
if (bytesWritten != XBOX_HEADER_SIZE)
763+
if (bytesWritten != DDS_XBOX_HEADER_SIZE)
792764
{
793765
return E_FAIL;
794766
}

DirectXTex/DDS.h

+35
Original file line numberDiff line numberDiff line change
@@ -288,4 +288,39 @@ namespace DirectX
288288
static_assert(sizeof(DDS_HEADER) == 124, "DDS Header size mismatch");
289289
static_assert(sizeof(DDS_HEADER_DXT10) == 20, "DDS DX10 Extended Header size mismatch");
290290

291+
constexpr size_t DDS_MIN_HEADER_SIZE = sizeof(uint32_t) + sizeof(DDS_HEADER);
292+
constexpr size_t DDS_DX10_HEADER_SIZE = sizeof(uint32_t) + sizeof(DDS_HEADER) + sizeof(DDS_HEADER_DXT10);
293+
static_assert(DDS_DX10_HEADER_SIZE > DDS_MIN_HEADER_SIZE, "DDS DX10 Header should be larger than standard header");
294+
295+
} // namespace
296+
297+
namespace Xbox
298+
{
299+
DDSGLOBALCONST DirectX::DDS_PIXELFORMAT DDSPF_XBOX =
300+
{ sizeof(DirectX::DDS_PIXELFORMAT), DDS_FOURCC, MAKEFOURCC('X','B','O','X'), 0, 0, 0, 0, 0 };
301+
302+
#pragma pack(push,1)
303+
304+
struct DDS_HEADER_XBOX
305+
// Must match structure in XboxDDSTextureLoader module
306+
{
307+
DXGI_FORMAT dxgiFormat;
308+
uint32_t resourceDimension;
309+
uint32_t miscFlag; // see DDS_RESOURCE_MISC_FLAG
310+
uint32_t arraySize;
311+
uint32_t miscFlags2; // see DDS_MISC_FLAGS2
312+
uint32_t tileMode; // see XG_TILE_MODE / XG_SWIZZLE_MODE
313+
uint32_t baseAlignment;
314+
uint32_t dataSize;
315+
uint32_t xdkVer; // matching _XDK_VER / _GXDK_VER
316+
};
317+
318+
#pragma pack(pop)
319+
320+
static_assert(sizeof(DDS_HEADER_XBOX) == 36, "DDS XBOX Header size mismatch");
321+
static_assert(sizeof(DDS_HEADER_XBOX) > sizeof(DirectX::DDS_HEADER_DXT10), "DDS XBOX Header should be larger than DX10 header");
322+
323+
constexpr size_t DDS_XBOX_HEADER_SIZE = sizeof(uint32_t) + sizeof(DirectX::DDS_HEADER) + sizeof(DDS_HEADER_XBOX);
324+
325+
constexpr uint32_t XBOX_TILEMODE_SCARLETT = 0x1000000;
291326
} // namespace

DirectXTex/DirectXTexDDS.cpp

+19-21
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ static_assert(static_cast<int>(TEX_DIMENSION_TEXTURE3D) == static_cast<int>(DDS_
2222

2323
namespace
2424
{
25-
constexpr size_t MAX_HEADER_SIZE = sizeof(uint32_t) + sizeof(DDS_HEADER) + sizeof(DDS_HEADER_DXT10);
26-
2725
//-------------------------------------------------------------------------------------
2826
// Legacy format mapping table (used for DDS files without 'DX10' extended header)
2927
//-------------------------------------------------------------------------------------
@@ -326,7 +324,7 @@ namespace
326324
*ddPixelFormat = {};
327325
}
328326

329-
if (size < (sizeof(DDS_HEADER) + sizeof(uint32_t)))
327+
if (size < DDS_MIN_HEADER_SIZE)
330328
{
331329
return HRESULT_E_INVALID_DATA;
332330
}
@@ -386,12 +384,12 @@ namespace
386384
}
387385

388386
// Buffer must be big enough for both headers and magic value
389-
if (size < (sizeof(DDS_HEADER) + sizeof(uint32_t) + sizeof(DDS_HEADER_DXT10)))
387+
if (size < DDS_DX10_HEADER_SIZE)
390388
{
391389
return E_FAIL;
392390
}
393391

394-
auto d3d10ext = reinterpret_cast<const DDS_HEADER_DXT10*>(static_cast<const uint8_t*>(pSource) + sizeof(uint32_t) + sizeof(DDS_HEADER));
392+
auto d3d10ext = reinterpret_cast<const DDS_HEADER_DXT10*>(static_cast<const uint8_t*>(pSource) + DDS_MIN_HEADER_SIZE);
395393
convFlags |= CONV_FLAGS_DX10;
396394

397395
metadata.arraySize = d3d10ext->arraySize;
@@ -826,7 +824,7 @@ HRESULT DirectX::EncodeDDSHeader(
826824
}
827825
}
828826

829-
required = sizeof(uint32_t) + sizeof(DDS_HEADER);
827+
required = DDS_MIN_HEADER_SIZE;
830828

831829
if (ddpf.size == 0)
832830
{
@@ -1776,24 +1774,24 @@ HRESULT DirectX::GetMetadataFromDDSFileEx(
17761774
#endif
17771775

17781776
// Need at least enough data to fill the standard header and magic number to be a valid DDS
1779-
if (len < (sizeof(DDS_HEADER) + sizeof(uint32_t)))
1777+
if (len < DDS_MIN_HEADER_SIZE)
17801778
{
17811779
return E_FAIL;
17821780
}
17831781

17841782
// Read the header in (including extended header if present)
1785-
uint8_t header[MAX_HEADER_SIZE] = {};
1783+
uint8_t header[DDS_DX10_HEADER_SIZE] = {};
17861784

17871785
#ifdef _WIN32
17881786
DWORD bytesRead = 0;
1789-
if (!ReadFile(hFile.get(), header, MAX_HEADER_SIZE, &bytesRead, nullptr))
1787+
if (!ReadFile(hFile.get(), header, DDS_DX10_HEADER_SIZE, &bytesRead, nullptr))
17901788
{
17911789
return HRESULT_FROM_WIN32(GetLastError());
17921790
}
17931791

17941792
auto const headerLen = static_cast<size_t>(bytesRead);
17951793
#else
1796-
auto const headerLen = std::min<size_t>(len, MAX_HEADER_SIZE);
1794+
auto const headerLen = std::min<size_t>(len, DDS_DX10_HEADER_SIZE);
17971795

17981796
inFile.read(reinterpret_cast<char*>(header), headerLen);
17991797
if (!inFile)
@@ -1839,7 +1837,7 @@ HRESULT DirectX::LoadFromDDSMemoryEx(
18391837
if (FAILED(hr))
18401838
return hr;
18411839

1842-
size_t offset = sizeof(uint32_t) + sizeof(DDS_HEADER);
1840+
size_t offset = DDS_MIN_HEADER_SIZE;
18431841
if (convFlags & CONV_FLAGS_DX10)
18441842
offset += sizeof(DDS_HEADER_DXT10);
18451843

@@ -1960,24 +1958,24 @@ HRESULT DirectX::LoadFromDDSFileEx(
19601958
#endif
19611959

19621960
// Need at least enough data to fill the standard header and magic number to be a valid DDS
1963-
if (len < (sizeof(DDS_HEADER) + sizeof(uint32_t)))
1961+
if (len < DDS_MIN_HEADER_SIZE)
19641962
{
19651963
return E_FAIL;
19661964
}
19671965

19681966
// Read the header in (including extended header if present)
1969-
uint8_t header[MAX_HEADER_SIZE] = {};
1967+
uint8_t header[DDS_DX10_HEADER_SIZE] = {};
19701968

19711969
#ifdef _WIN32
19721970
DWORD bytesRead = 0;
1973-
if (!ReadFile(hFile.get(), header, MAX_HEADER_SIZE, &bytesRead, nullptr))
1971+
if (!ReadFile(hFile.get(), header, DDS_DX10_HEADER_SIZE, &bytesRead, nullptr))
19741972
{
19751973
return HRESULT_FROM_WIN32(GetLastError());
19761974
}
19771975

19781976
auto const headerLen = static_cast<size_t>(bytesRead);
19791977
#else
1980-
auto const headerLen = std::min<size_t>(len, MAX_HEADER_SIZE);
1978+
auto const headerLen = std::min<size_t>(len, DDS_DX10_HEADER_SIZE);
19811979

19821980
inFile.read(reinterpret_cast<char*>(header), headerLen);
19831981
if (!inFile)
@@ -1990,24 +1988,24 @@ HRESULT DirectX::LoadFromDDSFileEx(
19901988
if (FAILED(hr))
19911989
return hr;
19921990

1993-
size_t offset = MAX_HEADER_SIZE;
1991+
size_t offset = DDS_DX10_HEADER_SIZE;
19941992

19951993
if (!(convFlags & CONV_FLAGS_DX10))
19961994
{
19971995
#ifdef _WIN32
19981996
// Must reset file position since we read more than the standard header above
1999-
const LARGE_INTEGER filePos = { { sizeof(uint32_t) + sizeof(DDS_HEADER), 0 } };
1997+
const LARGE_INTEGER filePos = { { DDS_MIN_HEADER_SIZE, 0 } };
20001998
if (!SetFilePointerEx(hFile.get(), filePos, nullptr, FILE_BEGIN))
20011999
{
20022000
return HRESULT_FROM_WIN32(GetLastError());
20032001
}
20042002
#else
2005-
inFile.seekg(sizeof(uint32_t) + sizeof(DDS_HEADER), std::ios::beg);
2003+
inFile.seekg(DDS_MIN_HEADER_SIZE, std::ios::beg);
20062004
if (!inFile)
20072005
return E_FAIL;
20082006
#endif
20092007

2010-
offset = sizeof(uint32_t) + sizeof(DDS_HEADER);
2008+
offset = DDS_MIN_HEADER_SIZE;
20112009
}
20122010

20132011
std::unique_ptr<uint32_t[]> pal8;
@@ -2393,9 +2391,9 @@ HRESULT DirectX::SaveToDDSFile(
23932391
return E_INVALIDARG;
23942392

23952393
// Create DDS Header
2396-
uint8_t header[MAX_HEADER_SIZE];
2394+
uint8_t header[DDS_DX10_HEADER_SIZE];
23972395
size_t required;
2398-
HRESULT hr = EncodeDDSHeader(metadata, flags, header, MAX_HEADER_SIZE, required);
2396+
HRESULT hr = EncodeDDSHeader(metadata, flags, header, DDS_DX10_HEADER_SIZE, required);
23992397
if (FAILED(hr))
24002398
return hr;
24012399

0 commit comments

Comments
 (0)