UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
DDSFile.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3#include "Containers/Array.h"
5#include "CoreTypes.h"
6#include "ImageCore.h"
7#include "Math/Color.h"
8
9// This is supposed to be, as close as possible, the complete list of DXGI formats
10// per those docs - the ODDFMTs we don't support for doing anything other than name, but we keep
11// them here so we can stay in sync with the docs more easily.
12//
13// we support nope out on some of the more exotic formats entirely where they're unlikely
14// to be useful for any assets
15//
16// fields: name, ID, bytes per unit (unit=1 texel for RGB, 1 block for BCN)
17#define UE_DXGI_FORMAT_LIST \
18 RGBFMT(UNKNOWN, 0, 0) \
19 RGBFMT(R32G32B32A32_TYPELESS, 1, 16) \
20 RGBFMT(R32G32B32A32_FLOAT, 2, 16) \
21 RGBFMT(R32G32B32A32_UINT, 3, 16) \
22 RGBFMT(R32G32B32A32_SINT, 4, 16) \
23 RGBFMT(R32G32B32_TYPELESS, 5, 12) \
24 RGBFMT(R32G32B32_FLOAT, 6, 12) \
25 RGBFMT(R32G32B32_UINT, 7, 12) \
26 RGBFMT(R32G32B32_SINT, 8, 12) \
27 RGBFMT(R16G16B16A16_TYPELESS, 9, 8) \
28 RGBFMT(R16G16B16A16_FLOAT, 10, 8) \
29 RGBFMT(R16G16B16A16_UNORM, 11, 8) \
30 RGBFMT(R16G16B16A16_UINT, 12, 8) \
31 RGBFMT(R16G16B16A16_SNORM, 13, 8) \
32 RGBFMT(R16G16B16A16_SINT, 14, 8) \
33 RGBFMT(R32G32_TYPELESS, 15, 8) \
34 RGBFMT(R32G32_FLOAT, 16, 8) \
35 RGBFMT(R32G32_UINT, 17, 8) \
36 RGBFMT(R32G32_SINT, 18, 8) \
37 RGBFMT(R32G8X24_TYPELESS, 19, 8) \
38 RGBFMT(D32_FLOAT_S8X24_UINT, 20, 8) \
39 RGBFMT(R32_FLOAT_X8X24_TYPELESS, 21, 8) \
40 RGBFMT(X32_TYPELESS_G8X24_UINT, 22, 8) \
41 RGBFMT(R10G10B10A2_TYPELESS, 23, 4) \
42 RGBFMT(R10G10B10A2_UNORM, 24, 4) \
43 RGBFMT(R10G10B10A2_UINT, 25, 4) \
44 RGBFMT(R11G11B10_FLOAT, 26, 4) \
45 RGBFMT(R8G8B8A8_TYPELESS, 27, 4) \
46 RGBFMT(R8G8B8A8_UNORM, 28, 4) \
47 RGBFMT(R8G8B8A8_UNORM_SRGB, 29, 4) \
48 RGBFMT(R8G8B8A8_UINT, 30, 4) \
49 RGBFMT(R8G8B8A8_SNORM, 31, 4) \
50 RGBFMT(R8G8B8A8_SINT, 32, 4) \
51 RGBFMT(R16G16_TYPELESS, 33, 4) \
52 RGBFMT(R16G16_FLOAT, 34, 4) \
53 RGBFMT(R16G16_UNORM, 35, 4) \
54 RGBFMT(R16G16_UINT, 36, 4) \
55 RGBFMT(R16G16_SNORM, 37, 4) \
56 RGBFMT(R16G16_SINT, 38, 4) \
57 RGBFMT(R32_TYPELESS, 39, 4) \
58 RGBFMT(D32_FLOAT, 40, 4) \
59 RGBFMT(R32_FLOAT, 41, 4) \
60 RGBFMT(R32_UINT, 42, 4) \
61 RGBFMT(R32_SINT, 43, 4) \
62 RGBFMT(R24G8_TYPELESS, 44, 4) \
63 RGBFMT(D24_UNORM_S8_UINT, 45, 4) \
64 RGBFMT(R24_UNORM_X8_TYPELESS, 46, 4) \
65 RGBFMT(X24_TYPELESS_G8_UINT, 47, 4) \
66 RGBFMT(R8G8_TYPELESS, 48, 2) \
67 RGBFMT(R8G8_UNORM, 49, 2) \
68 RGBFMT(R8G8_UINT, 50, 2) \
69 RGBFMT(R8G8_SNORM, 51, 2) \
70 RGBFMT(R8G8_SINT, 52, 2) \
71 RGBFMT(R16_TYPELESS, 53, 2) \
72 RGBFMT(R16_FLOAT, 54, 2) \
73 RGBFMT(D16_UNORM, 55, 2) \
74 RGBFMT(R16_UNORM, 56, 2) \
75 RGBFMT(R16_UINT, 57, 2) \
76 RGBFMT(R16_SNORM, 58, 2) \
77 RGBFMT(R16_SINT, 59, 2) \
78 RGBFMT(R8_TYPELESS, 60, 1) \
79 RGBFMT(R8_UNORM, 61, 1) \
80 RGBFMT(R8_UINT, 62, 1) \
81 RGBFMT(R8_SNORM, 63, 1) \
82 RGBFMT(R8_SINT, 64, 1) \
83 RGBFMT(A8_UNORM, 65, 1) \
84 ODDFMT(R1_UNORM, 66) \
85 RGBFMT(R9G9B9E5_SHAREDEXP, 67, 4) \
86 ODDFMT(R8G8_B8G8_UNORM, 68) \
87 ODDFMT(G8R8_G8B8_UNORM, 69) \
88 BCNFMT(BC1_TYPELESS, 70, 8) \
89 BCNFMT(BC1_UNORM, 71, 8) \
90 BCNFMT(BC1_UNORM_SRGB, 72, 8) \
91 BCNFMT(BC2_TYPELESS, 73, 16) \
92 BCNFMT(BC2_UNORM, 74, 16) \
93 BCNFMT(BC2_UNORM_SRGB, 75, 16) \
94 BCNFMT(BC3_TYPELESS, 76, 16) \
95 BCNFMT(BC3_UNORM, 77, 16) \
96 BCNFMT(BC3_UNORM_SRGB, 78, 16) \
97 BCNFMT(BC4_TYPELESS, 79, 8) \
98 BCNFMT(BC4_UNORM, 80, 8) \
99 BCNFMT(BC4_SNORM, 81, 8) \
100 BCNFMT(BC5_TYPELESS, 82, 16) \
101 BCNFMT(BC5_UNORM, 83, 16) \
102 BCNFMT(BC5_SNORM, 84, 16) \
103 RGBFMT(B5G6R5_UNORM, 85, 2) \
104 RGBFMT(B5G5R5A1_UNORM, 86, 2) \
105 RGBFMT(B8G8R8A8_UNORM, 87, 4) \
106 RGBFMT(B8G8R8X8_UNORM, 88, 4) \
107 RGBFMT(R10G10B10_XR_BIAS_A2_UNORM, 89, 4) \
108 RGBFMT(B8G8R8A8_TYPELESS, 90, 4) \
109 RGBFMT(B8G8R8A8_UNORM_SRGB, 91, 4) \
110 RGBFMT(B8G8R8X8_TYPELESS, 92, 4) \
111 RGBFMT(B8G8R8X8_UNORM_SRGB, 93, 4) \
112 BCNFMT(BC6H_TYPELESS, 94, 16) \
113 BCNFMT(BC6H_UF16, 95, 16) \
114 BCNFMT(BC6H_SF16, 96, 16) \
115 BCNFMT(BC7_TYPELESS, 97, 16) \
116 BCNFMT(BC7_UNORM, 98, 16) \
117 BCNFMT(BC7_UNORM_SRGB, 99, 16) \
118 ODDFMT(AYUV, 100) \
119 ODDFMT(Y410, 101) \
120 ODDFMT(Y416, 102) \
121 ODDFMT(NV12, 103) \
122 ODDFMT(P010, 104) \
123 ODDFMT(P016, 105) \
124 ODDFMT(_420_OPAQUE, 106) \
125 ODDFMT(YUY2, 107) \
126 ODDFMT(Y210, 108) \
127 ODDFMT(Y216, 109) \
128 ODDFMT(NV11, 110) \
129 ODDFMT(AI44, 111) \
130 ODDFMT(IA44, 112) \
131 ODDFMT(P8, 113) \
132 ODDFMT(A8P8, 114) \
133 RGBFMT(B4G4R4A4_UNORM, 115,2) \
134 ODDFMT(P208, 130) \
135 ODDFMT(V208, 131) \
136 ODDFMT(V408, 132) \
137 RGBFMT(B8G8R8, 1001,3) \
138 RGBFMT(R8G8B8, 1002,3) \
139 RGBFMT(R8G8B8X8, 1003,4) \
140 /* end */
141
142namespace UE { namespace DDS
143{
144 // Complete list of formats
145 enum class EDXGIFormat
146 {
147 #define RGBFMT(name,id,bypu) name = id,
148 #define BCNFMT(name,id,bypu) name = id,
149 #define ODDFMT(name,id) name = id,
151 #undef RGBFMT
152 #undef BCNFMT
153 #undef ODDFMT
154 };
155
156 // What version of DDS file to write
158 {
159 Auto, // Write as D3D9-version DDS if possible without losing metadata like sRGB status, D3D10 otherwise
160 D3D9, // Try really hard to write a D3D9-format .DDS file (the more widely supported version) even when doing so loses some information
161 D3D10, // Always write a D3D10-format .DDS file
162 };
163
164 // Mip data read modes
166 {
167 Full,
170 };
171
172 // Error codes
173 enum class EDDSError
174 {
175 OK, // No error
176 Ok = OK,
177 OutOfMemory, // Out of memory/allocation failure while reading
178 NotADds, // No valid DDS header found
180 BadResourceDimension, // Resource not 1D, 2D or 3D
181 BadPixelFormat, // Malformed or unsupported pixel format
182 BadImageDimension, // Image dimensions (width, height, depth, array size) outside supported range
183 BadMipmapCount, // Invalid mipmap count for given image dimensions
184 BadCubemap, // Cubemap has non-square faces, non-1 depth, or unsupported array count
185 IoError, // General I/O error
186 };
187
188 // Channel ordering for 8-bit formats
189 enum class EChannelOrder
190 {
191 RGBA,
192 BGRA
193 };
194
195 // One of these for each mip of a DDS
196 struct FDDSMip
197 {
199 // Note: pixels in DDS data must be densely packed, they exist for ease of addressing
200 // but you can't represent strided data in this form.
201 int64 RowStride; // Bytes in one row of a 2d slice of the mip. equal to SliceStride for 1D textures.
202 int64 SliceStride; // Bytes in one 2d slice of the mip, equal to DataStride for 2D textures.
204 uint8* Data; // just raw bytes; interpretation as per DXGIFormat in parent FDDSFile
205 };
206
207 // Metadata structure for a DDS file, with access pointers for the raw texture data (i.e.
208 // unconverted from the DXGI format).
209 //
210 // Can be used for loading or saving a dds file.
211 //
212 // For saving, general structure is:
213 // {
214 // dds = FDDSFile::CreateEmpty[2D](...)
215 // copy data for each mip/array:
216 // for (mips)
217 // memcpy(dds->mips[idx].data, mip_data, mip_size);
218 //
219 // Ar = IFileManager::Get().CreateFileWriter(filename);
220 //
221 // dds->Serialize(Ar);
222 // delete dds;
223 //
224 // Ar->Close()
225 // delete Ar;
226 // }
227 struct FDDSFile
228 {
229 int32 Dimension=0; // 1, 2 or 3.
234
235 // Array size here uses the conventions as in the D3D runtime, which means
236 // an array of N cubemaps has an array size of 6N (one element per face).
238 EDXGIFormat DXGIFormat=EDXGIFormat::UNKNOWN;
240
241 // Mips are ordered starting from mip 0 (full-size texture) decreasing in size;
242 // for arrays, we store the full mip chain for one array element before advancing
243 // to the next array element, and have (ArraySize * MipCount) mips total.
245
246 // When we allocate the mips ourselves, we keep all storage together in a single
247 // allocation, namely this one, and the mips point into it.
248 // With CREATE_FLAG_NO_MIP_STORAGE_ALLOC, we don't own the underlying memory and
249 // this array is unused.
251
252 // Write DDS-format data to memory. This produces a memory image of a DDS file.
253 //
254 // Automatic format selection rules used are:
255 // - D3D9 DDS does not support 1D or array textures directly, so these always write as D3D10
256 // - The pixel format must be a "common" D3D9 format (more obscure formats can be used for D3D9
257 // writing when InFormatVersion is D3D9, but "Auto" sticks with a restricted subset)
258 // - For 8-bit pixel formats, the format must be non-sRGB.
259 //
260 // The latter is somewhat arbitrary. D3D9 DDS files don't store whether pixels for a texture are meant
261 // to be interpreted as sRGB or not. This reader treats the resulting formats as non-sRGB UNORM,
262 // so the writer does the same for symmetry.
264
265 // 20 mips means 512k x 512k pixels max, should be sufficient for now.
266 // It's 512k not 1M because the final 1x1 pixel mip counts; i.e. a mip chain
267 // from 1048576x1048576 down to 1x1 has 21 mips!
268 static constexpr uint32 MAX_MIPS_SUPPORTED = 20;
269
270 // Create an empty DDS structure (for writing DDS files typically)
271 //
272 // InDimension is [1,3] (1D,2D,3D)
273 // InWidth/InHeight are for the top mip. Cubemaps must be square.
274 // InDepth is only used for 3D textures, must be 1 otherwise.
275 // InMipCount <= MAX_MIPS_SUPPORTED.
276 // InArraySize number of textures provided - must be multiple of 6 for cubemaps (CREATE_FLAG_CUBEMAP)
277 //
278 // Note for texture arrays the mip data pointers are expecting the entire mip chain
279 // for one texture before moving to the next
280 //
281 // On error, returns nullptr. If a non-null OutError is supplied, error information
282 // is written there.
284
285 // Convenience version of the above to create a basic 2D texture with mip chain
287
288 // Sanity-check that all members make sense and return an error code.
290
291 // Check if this DDS is usable in Unreal Texture types:
292 IMAGECORE_API bool IsValidTexture2D() const;
293 IMAGECORE_API bool IsValidTextureCube() const; // or cube array
296
297 UE_DEPRECATED(5.6, "This function is now deprecated, please use CreateFromDDSInMemory with EDDSReadMipMode instead.")
299
300 // Used for loading from a DDS file image in memory.
301 //
302 // On error, returns nullptr. If a non-null OutError is supplied, error information
303 // is written there.
305
306 // is this buffer a DDS ?
308
309 // For 8-bit UNorm formats, both RGBA and BGRA channel order variants exist and
310 // are popular. This function does the trivial conversion between the two, updating
311 // both the DXGI Format and the pixel data to do so. When the channel order is already
312 // correct or the pixel format is not one of the few formats that exist in both RGBA
313 // and BGRA variants, this function does nothing.
315
316 // convert X alpha to A and fill 255
318
319 // Blit pixels from DDS mip to Image
320 // return false if no pixel format conversion is supported
321 // ToImage is not allocated, must be pre-allocated and sized correctly
322 IMAGECORE_API bool GetMipImage(const FImageView & ToImage, int MipIndex) const;
323
324 // Fill DDS mip from Image
325 // Image must be of format DXGIFormatFromRawFormat
326 IMAGECORE_API void FillMip(const FImageView & FromImage, int MipIndex);
327
328 // Bit flags
331 static constexpr uint32 CREATE_FLAG_NO_MIP_STORAGE_ALLOC = 2; // Mip storage wasn't allocated by us and isn't owned by us, just pointing elsewhere.
332 static constexpr uint32 CREATE_FLAG_WAS_D3D9 = 4; // Loaded from D3D9-format .DDS file, so whether 8-bit/channel formats are sRGB or not is largely guesswork
333 };
334
335 // Returns the name of a DXGI format
337
338 // Returns whether a given pixel format is sRGB
341
342 // Return the corresponding non-sRGB version of a pixel format if there is one
344
345 // Return the corresponding sRGB version of a pixel format if there is one
347
349
351
352 // note these sizes include the 4-byte magic word, unlike the older DDSLoader functions
355
356} }
OODEFFUNC typedef const char int const char * fmt
Definition oodle2.h:678
EGammaSpace
Definition Color.h:32
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::int64 int64
A 64-bit signed integer.
Definition Platform.h:1127
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Array.h:670
Definition ImageCore.h:51
ERawImageFormat::Type DXGIFormatGetClosestRawFormat(EDXGIFormat fmt, bool *pIsExactMatch)
Definition DDSFile.cpp:1350
EDDSReadMipMode
Definition DDSFile.h:166
EDXGIFormat DXGIFormatRemoveSRGB(EDXGIFormat fmt)
Definition DDSFile.cpp:171
int64 GetDDSHeaderMinimalSize()
Definition DDSFile.cpp:752
const TCHAR * DXGIFormatGetName(EDXGIFormat fmt)
Definition DDSFile.cpp:91
EChannelOrder
Definition DDSFile.h:190
int64 GetDDSHeaderMaximalSize()
Definition DDSFile.cpp:747
EDXGIFormat
Definition DDSFile.h:146
EDDSFormatVersion
Definition DDSFile.h:158
bool DXGIFormatIsSRGB(EDXGIFormat Format)
Definition DDSFile.cpp:159
EDXGIFormat DXGIFormatFromRawFormat(ERawImageFormat::Type RawFormat, EGammaSpace GammaSpace)
Definition DDSFile.cpp:1367
bool DXGIFormatHasLinearAndSRGBForm(EDXGIFormat Format)
Definition DDSFile.cpp:165
EDXGIFormat DXGIFormatAddSRGB(EDXGIFormat fmt)
Definition DDSFile.cpp:184
EDDSError
Definition DDSFile.h:174
Definition AdvancedWidgetsModule.cpp:13
Definition ImageCore.h:264
Definition DDSFile.h:228
uint32 Depth
Definition DDSFile.h:232
uint32 MipCount
Definition DDSFile.h:233
IMAGECORE_API bool IsValidTextureVolume() const
Definition DDSFile.cpp:570
uint32 Height
Definition DDSFile.h:231
static IMAGECORE_API FDDSFile * CreateEmpty(int32 InDimension, uint32 InWidth, uint32 InHeight, uint32 InDepth, uint32 InMipCount, uint32 InArraySize, EDXGIFormat InFormat, uint32 InCreateFlags, EDDSError *OutError=nullptr)
Definition DDSFile.cpp:616
static constexpr uint32 CREATE_FLAG_WAS_D3D9
Definition DDSFile.h:332
TArray64< uint8 > MipRawData
Definition DDSFile.h:250
uint32 CreateFlags
Definition DDSFile.h:239
EDXGIFormat DXGIFormat
Definition DDSFile.h:238
IMAGECORE_API bool GetMipImage(const FImageView &ToImage, int MipIndex) const
Definition DDSFile.cpp:1175
static IMAGECORE_API bool IsADDS(const uint8 *InDDS, int64 InDDSSize)
Definition DDSFile.cpp:845
TArray< FDDSMip > Mips
Definition DDSFile.h:244
static IMAGECORE_API FDDSFile * CreateFromDDSInMemory(const uint8 *InDDS, int64 InDDSSize, EDDSError *OutError, bool bHeaderOnly)
Definition DDSFile.cpp:758
uint32 ArraySize
Definition DDSFile.h:237
IMAGECORE_API bool IsValidTextureArray() const
Definition DDSFile.cpp:546
static constexpr uint32 CREATE_FLAG_CUBEMAP
Definition DDSFile.h:330
static constexpr uint32 MAX_MIPS_SUPPORTED
Definition DDSFile.h:268
int32 Dimension
Definition DDSFile.h:229
IMAGECORE_API void FillMip(const FImageView &FromImage, int MipIndex)
Definition DDSFile.cpp:1162
static constexpr uint32 CREATE_FLAG_NONE
Definition DDSFile.h:329
IMAGECORE_API void ConvertRGBXtoRGBA()
Definition DDSFile.cpp:1085
static constexpr uint32 CREATE_FLAG_NO_MIP_STORAGE_ALLOC
Definition DDSFile.h:331
IMAGECORE_API EDDSError Validate() const
Definition DDSFile.cpp:393
static IMAGECORE_API FDDSFile * CreateEmpty2D(uint32 InWidth, uint32 InHeight, uint32 InMipCount, EDXGIFormat InFormat, uint32 InCreateFlags, EDDSError *OutError=nullptr)
Definition DDSFile.cpp:662
IMAGECORE_API bool IsValidTextureCube() const
Definition DDSFile.cpp:531
uint32 Width
Definition DDSFile.h:230
IMAGECORE_API void ConvertChannelOrder(EChannelOrder InTargetOrder)
Definition DDSFile.cpp:1123
IMAGECORE_API EDDSError WriteDDS(TArray64< uint8 > &OutDDS, EDDSFormatVersion InFormatVersion=EDDSFormatVersion::D3D10)
Definition DDSFile.cpp:890
IMAGECORE_API bool IsValidTexture2D() const
Definition DDSFile.cpp:513
Definition DDSFile.h:197
uint32 Width
Definition DDSFile.h:198
uint32 Depth
Definition DDSFile.h:198
uint32 Height
Definition DDSFile.h:198
int64 SliceStride
Definition DDSFile.h:202
int64 RowStride
Definition DDSFile.h:201
int64 DataSize
Definition DDSFile.h:203
uint8 * Data
Definition DDSFile.h:204