Skip to content

Commit 09affb7

Browse files
authored
Fix DX12 Capture transition state handling for MSAA (#532)
1 parent 1262c4b commit 09affb7

7 files changed

+349
-291
lines changed

DDSTextureLoader/DDSTextureLoader11.cpp

+86-78
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <algorithm>
2020
#include <cassert>
21+
#include <cstdint>
2122
#include <memory>
2223
#include <new>
2324

@@ -53,82 +54,90 @@ using namespace DirectX;
5354
//
5455
// See DDS.h in the 'Texconv' sample and the 'DirectXTex' library
5556
//--------------------------------------------------------------------------------------
56-
#pragma pack(push,1)
57+
namespace
58+
{
59+
#pragma pack(push,1)
5760

58-
constexpr uint32_t DDS_MAGIC = 0x20534444; // "DDS "
61+
constexpr uint32_t DDS_MAGIC = 0x20534444; // "DDS "
5962

60-
struct DDS_PIXELFORMAT
61-
{
62-
uint32_t size;
63-
uint32_t flags;
64-
uint32_t fourCC;
65-
uint32_t RGBBitCount;
66-
uint32_t RBitMask;
67-
uint32_t GBitMask;
68-
uint32_t BBitMask;
69-
uint32_t ABitMask;
70-
};
71-
72-
#define DDS_FOURCC 0x00000004 // DDPF_FOURCC
73-
#define DDS_RGB 0x00000040 // DDPF_RGB
74-
#define DDS_LUMINANCE 0x00020000 // DDPF_LUMINANCE
75-
#define DDS_ALPHA 0x00000002 // DDPF_ALPHA
76-
#define DDS_BUMPDUDV 0x00080000 // DDPF_BUMPDUDV
77-
78-
#define DDS_HEADER_FLAGS_VOLUME 0x00800000 // DDSD_DEPTH
79-
80-
#define DDS_HEIGHT 0x00000002 // DDSD_HEIGHT
81-
82-
#define DDS_CUBEMAP_POSITIVEX 0x00000600 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEX
83-
#define DDS_CUBEMAP_NEGATIVEX 0x00000a00 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEX
84-
#define DDS_CUBEMAP_POSITIVEY 0x00001200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEY
85-
#define DDS_CUBEMAP_NEGATIVEY 0x00002200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEY
86-
#define DDS_CUBEMAP_POSITIVEZ 0x00004200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEZ
87-
#define DDS_CUBEMAP_NEGATIVEZ 0x00008200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEZ
88-
89-
#define DDS_CUBEMAP_ALLFACES ( DDS_CUBEMAP_POSITIVEX | DDS_CUBEMAP_NEGATIVEX |\
90-
DDS_CUBEMAP_POSITIVEY | DDS_CUBEMAP_NEGATIVEY |\
91-
DDS_CUBEMAP_POSITIVEZ | DDS_CUBEMAP_NEGATIVEZ )
92-
93-
#define DDS_CUBEMAP 0x00000200 // DDSCAPS2_CUBEMAP
94-
95-
enum DDS_MISC_FLAGS2
96-
{
97-
DDS_MISC_FLAGS2_ALPHA_MODE_MASK = 0x7L,
98-
};
63+
struct DDS_PIXELFORMAT
64+
{
65+
uint32_t size;
66+
uint32_t flags;
67+
uint32_t fourCC;
68+
uint32_t RGBBitCount;
69+
uint32_t RBitMask;
70+
uint32_t GBitMask;
71+
uint32_t BBitMask;
72+
uint32_t ABitMask;
73+
};
74+
75+
#define DDS_FOURCC 0x00000004 // DDPF_FOURCC
76+
#define DDS_RGB 0x00000040 // DDPF_RGB
77+
#define DDS_LUMINANCE 0x00020000 // DDPF_LUMINANCE
78+
#define DDS_ALPHA 0x00000002 // DDPF_ALPHA
79+
#define DDS_BUMPDUDV 0x00080000 // DDPF_BUMPDUDV
80+
81+
#define DDS_HEADER_FLAGS_VOLUME 0x00800000 // DDSD_DEPTH
82+
83+
#define DDS_HEIGHT 0x00000002 // DDSD_HEIGHT
84+
85+
#define DDS_CUBEMAP_POSITIVEX 0x00000600 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEX
86+
#define DDS_CUBEMAP_NEGATIVEX 0x00000a00 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEX
87+
#define DDS_CUBEMAP_POSITIVEY 0x00001200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEY
88+
#define DDS_CUBEMAP_NEGATIVEY 0x00002200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEY
89+
#define DDS_CUBEMAP_POSITIVEZ 0x00004200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEZ
90+
#define DDS_CUBEMAP_NEGATIVEZ 0x00008200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEZ
91+
92+
#define DDS_CUBEMAP_ALLFACES ( DDS_CUBEMAP_POSITIVEX | DDS_CUBEMAP_NEGATIVEX |\
93+
DDS_CUBEMAP_POSITIVEY | DDS_CUBEMAP_NEGATIVEY |\
94+
DDS_CUBEMAP_POSITIVEZ | DDS_CUBEMAP_NEGATIVEZ )
95+
96+
#define DDS_CUBEMAP 0x00000200 // DDSCAPS2_CUBEMAP
97+
98+
enum DDS_MISC_FLAGS2
99+
{
100+
DDS_MISC_FLAGS2_ALPHA_MODE_MASK = 0x7L,
101+
};
99102

100-
struct DDS_HEADER
101-
{
102-
uint32_t size;
103-
uint32_t flags;
104-
uint32_t height;
105-
uint32_t width;
106-
uint32_t pitchOrLinearSize;
107-
uint32_t depth; // only if DDS_HEADER_FLAGS_VOLUME is set in flags
108-
uint32_t mipMapCount;
109-
uint32_t reserved1[11];
110-
DDS_PIXELFORMAT ddspf;
111-
uint32_t caps;
112-
uint32_t caps2;
113-
uint32_t caps3;
114-
uint32_t caps4;
115-
uint32_t reserved2;
116-
};
117-
118-
struct DDS_HEADER_DXT10
119-
{
120-
DXGI_FORMAT dxgiFormat;
121-
uint32_t resourceDimension;
122-
uint32_t miscFlag; // see D3D11_RESOURCE_MISC_FLAG
123-
uint32_t arraySize;
124-
uint32_t miscFlags2;
125-
};
103+
struct DDS_HEADER
104+
{
105+
uint32_t size;
106+
uint32_t flags;
107+
uint32_t height;
108+
uint32_t width;
109+
uint32_t pitchOrLinearSize;
110+
uint32_t depth; // only if DDS_HEADER_FLAGS_VOLUME is set in flags
111+
uint32_t mipMapCount;
112+
uint32_t reserved1[11];
113+
DDS_PIXELFORMAT ddspf;
114+
uint32_t caps;
115+
uint32_t caps2;
116+
uint32_t caps3;
117+
uint32_t caps4;
118+
uint32_t reserved2;
119+
};
120+
121+
struct DDS_HEADER_DXT10
122+
{
123+
DXGI_FORMAT dxgiFormat;
124+
uint32_t resourceDimension;
125+
uint32_t miscFlag; // see D3D11_RESOURCE_MISC_FLAG
126+
uint32_t arraySize;
127+
uint32_t miscFlags2;
128+
};
126129

127-
#pragma pack(pop)
130+
#pragma pack(pop)
128131

129-
//--------------------------------------------------------------------------------------
130-
namespace
131-
{
132+
static_assert(sizeof(DDS_PIXELFORMAT) == 32, "DDS pixel format size mismatch");
133+
static_assert(sizeof(DDS_HEADER) == 124, "DDS Header size mismatch");
134+
static_assert(sizeof(DDS_HEADER_DXT10) == 20, "DDS DX10 Extended Header size mismatch");
135+
136+
constexpr size_t DDS_MIN_HEADER_SIZE = sizeof(uint32_t) + sizeof(DDS_HEADER);
137+
constexpr size_t DDS_DX10_HEADER_SIZE = sizeof(uint32_t) + sizeof(DDS_HEADER) + sizeof(DDS_HEADER_DXT10);
138+
static_assert(DDS_DX10_HEADER_SIZE > DDS_MIN_HEADER_SIZE, "DDS DX10 Header should be larger than standard header");
139+
140+
//--------------------------------------------------------------------------------------
132141
struct handle_closer { void operator()(HANDLE h) noexcept { if (h) CloseHandle(h); } };
133142

134143
using ScopedHandle = std::unique_ptr<void, handle_closer>;
@@ -168,7 +177,7 @@ namespace
168177
return E_FAIL;
169178
}
170179

171-
if (ddsDataSize < (sizeof(uint32_t) + sizeof(DDS_HEADER)))
180+
if (ddsDataSize < DDS_MIN_HEADER_SIZE)
172181
{
173182
return E_FAIL;
174183
}
@@ -195,7 +204,7 @@ namespace
195204
(MAKEFOURCC('D', 'X', '1', '0') == hdr->ddspf.fourCC))
196205
{
197206
// Must be long enough for both headers and magic value
198-
if (ddsDataSize < (sizeof(uint32_t) + sizeof(DDS_HEADER) + sizeof(DDS_HEADER_DXT10)))
207+
if (ddsDataSize < DDS_DX10_HEADER_SIZE)
199208
{
200209
return E_FAIL;
201210
}
@@ -205,8 +214,7 @@ namespace
205214

206215
// setup the pointers in the process request
207216
*header = hdr;
208-
auto offset = sizeof(uint32_t)
209-
+ sizeof(DDS_HEADER)
217+
auto offset = DDS_MIN_HEADER_SIZE
210218
+ (bDXT10Header ? sizeof(DDS_HEADER_DXT10) : 0u);
211219
*bitData = ddsData + offset;
212220
*bitSize = ddsDataSize - offset;
@@ -264,7 +272,7 @@ namespace
264272
}
265273

266274
// Need at least enough data to fill the header and magic number to be a valid DDS
267-
if (fileInfo.EndOfFile.LowPart < (sizeof(uint32_t) + sizeof(DDS_HEADER)))
275+
if (fileInfo.EndOfFile.LowPart < DDS_MIN_HEADER_SIZE)
268276
{
269277
return E_FAIL;
270278
}
@@ -319,7 +327,7 @@ namespace
319327
(MAKEFOURCC('D', 'X', '1', '0') == hdr->ddspf.fourCC))
320328
{
321329
// Must be long enough for both headers and magic value
322-
if (fileInfo.EndOfFile.LowPart < (sizeof(uint32_t) + sizeof(DDS_HEADER) + sizeof(DDS_HEADER_DXT10)))
330+
if (fileInfo.EndOfFile.LowPart < DDS_DX10_HEADER_SIZE)
323331
{
324332
ddsData.reset();
325333
return E_FAIL;
@@ -330,7 +338,7 @@ namespace
330338

331339
// setup the pointers in the process request
332340
*header = hdr;
333-
auto offset = sizeof(uint32_t) + sizeof(DDS_HEADER)
341+
auto offset = DDS_MIN_HEADER_SIZE
334342
+ (bDXT10Header ? sizeof(DDS_HEADER_DXT10) : 0u);
335343
*bitData = ddsData.get() + offset;
336344
*bitSize = fileInfo.EndOfFile.LowPart - offset;

0 commit comments

Comments
 (0)