| 1 | // Copyright 2023 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/Assets/CustomTextureData.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <cstddef> |
| 8 | #include <cstdint> |
| 9 | #include <cstring> |
| 10 | #include <functional> |
| 11 | |
| 12 | #include "Common/Align.h" |
| 13 | #include "Common/IOFile.h" |
| 14 | #include "Common/Image.h" |
| 15 | #include "Common/Logging/Log.h" |
| 16 | #include "VideoCommon/VideoConfig.h" |
| 17 | |
| 18 | namespace |
| 19 | { |
| 20 | // From https://raw.githubusercontent.com/Microsoft/DirectXTex/master/DirectXTex/DDS.h |
| 21 | // |
| 22 | // This header defines constants and structures that are useful when parsing |
| 23 | // DDS files. DDS files were originally designed to use several structures |
| 24 | // and constants that are native to DirectDraw and are defined in ddraw.h, |
| 25 | // such as DDSURFACEDESC2 and DDSCAPS2. This file defines similar |
| 26 | // (compatible) constants and structures so that one can use DDS files |
| 27 | // without needing to include ddraw.h. |
| 28 | // |
| 29 | // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF |
| 30 | // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO |
| 31 | // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A |
| 32 | // PARTICULAR PURPOSE. |
| 33 | // |
| 34 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 35 | // |
| 36 | // http://go.microsoft.com/fwlink/?LinkId=248926 |
| 37 | |
| 38 | #pragma pack(push, 1) |
| 39 | |
| 40 | const uint32_t DDS_MAGIC = 0x20534444; // "DDS " |
| 41 | |
| 42 | struct DDS_PIXELFORMAT |
| 43 | { |
| 44 | uint32_t dwSize; |
| 45 | uint32_t dwFlags; |
| 46 | uint32_t dwFourCC; |
| 47 | uint32_t dwRGBBitCount; |
| 48 | uint32_t dwRBitMask; |
| 49 | uint32_t dwGBitMask; |
| 50 | uint32_t dwBBitMask; |
| 51 | uint32_t dwABitMask; |
| 52 | }; |
| 53 | |
| 54 | #define DDS_FOURCC 0x00000004 // DDPF_FOURCC |
| 55 | #define DDS_RGB 0x00000040 // DDPF_RGB |
| 56 | #define DDS_RGBA 0x00000041 // DDPF_RGB | DDPF_ALPHAPIXELS |
| 57 | #define DDS_LUMINANCE 0x00020000 // DDPF_LUMINANCE |
| 58 | #define DDS_LUMINANCEA 0x00020001 // DDPF_LUMINANCE | DDPF_ALPHAPIXELS |
| 59 | #define DDS_ALPHA 0x00000002 // DDPF_ALPHA |
| 60 | #define DDS_PAL8 0x00000020 // DDPF_PALETTEINDEXED8 |
| 61 | #define DDS_PAL8A 0x00000021 // DDPF_PALETTEINDEXED8 | DDPF_ALPHAPIXELS |
| 62 | #define DDS_BUMPDUDV 0x00080000 // DDPF_BUMPDUDV |
| 63 | |
| 64 | #define DDS_CUBEMAP_POSITIVEX 0x00000600 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEX |
| 65 | #define DDS_CUBEMAP_NEGATIVEX 0x00000a00 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEX |
| 66 | #define DDS_CUBEMAP_POSITIVEY 0x00001200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEY |
| 67 | #define DDS_CUBEMAP_NEGATIVEY 0x00002200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEY |
| 68 | #define DDS_CUBEMAP_POSITIVEZ 0x00004200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEZ |
| 69 | #define DDS_CUBEMAP_NEGATIVEZ 0x00008200 // DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEZ |
| 70 | |
| 71 | #define DDS_CUBEMAP_ALLFACES \ |
| 72 | (DDS_CUBEMAP_POSITIVEX | DDS_CUBEMAP_NEGATIVEX | DDS_CUBEMAP_POSITIVEY | DDS_CUBEMAP_NEGATIVEY | \ |
| 73 | DDS_CUBEMAP_POSITIVEZ | DDS_CUBEMAP_NEGATIVEZ) |
| 74 | |
| 75 | #define DDS_CUBEMAP 0x00000200 // DDSCAPS2_CUBEMAP |
| 76 | |
| 77 | #ifndef MAKEFOURCC |
| 78 | #define MAKEFOURCC(ch0, ch1, ch2, ch3) \ |
| 79 | ((uint32_t)(uint8_t)(ch0) | ((uint32_t)(uint8_t)(ch1) << 8) | ((uint32_t)(uint8_t)(ch2) << 16) | \ |
| 80 | ((uint32_t)(uint8_t)(ch3) << 24)) |
| 81 | #endif /* defined(MAKEFOURCC) */ |
| 82 | |
| 83 | #define DDS_HEADER_FLAGS_TEXTURE \ |
| 84 | 0x00001007 // DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT |
| 85 | #define DDS_HEADER_FLAGS_MIPMAP 0x00020000 // DDSD_MIPMAPCOUNT |
| 86 | #define DDS_HEADER_FLAGS_VOLUME 0x00800000 // DDSD_DEPTH |
| 87 | #define DDS_HEADER_FLAGS_PITCH 0x00000008 // DDSD_PITCH |
| 88 | #define DDS_HEADER_FLAGS_LINEARSIZE 0x00080000 // DDSD_LINEARSIZE |
| 89 | |
| 90 | // Subset here matches D3D10_RESOURCE_DIMENSION and D3D11_RESOURCE_DIMENSION |
| 91 | enum DDS_RESOURCE_DIMENSION |
| 92 | { |
| 93 | DDS_DIMENSION_TEXTURE1D = 2, |
| 94 | DDS_DIMENSION_TEXTURE2D = 3, |
| 95 | DDS_DIMENSION_TEXTURE3D = 4, |
| 96 | }; |
| 97 | |
| 98 | struct DDS_HEADER |
| 99 | { |
| 100 | uint32_t dwSize; |
| 101 | uint32_t dwFlags; |
| 102 | uint32_t dwHeight; |
| 103 | uint32_t dwWidth; |
| 104 | uint32_t dwPitchOrLinearSize; |
| 105 | uint32_t dwDepth; // only if DDS_HEADER_FLAGS_VOLUME is set in dwFlags |
| 106 | uint32_t dwMipMapCount; |
| 107 | uint32_t dwReserved1[11]; |
| 108 | DDS_PIXELFORMAT ddspf; |
| 109 | uint32_t dwCaps; |
| 110 | uint32_t dwCaps2; |
| 111 | uint32_t dwCaps3; |
| 112 | uint32_t dwCaps4; |
| 113 | uint32_t dwReserved2; |
| 114 | }; |
| 115 | |
| 116 | struct DDS_HEADER_DXT10 |
| 117 | { |
| 118 | uint32_t dxgiFormat; |
| 119 | uint32_t resourceDimension; |
| 120 | uint32_t miscFlag; // see DDS_RESOURCE_MISC_FLAG |
| 121 | uint32_t arraySize; |
| 122 | uint32_t miscFlags2; // see DDS_MISC_FLAGS2 |
| 123 | }; |
| 124 | |
| 125 | #pragma pack(pop) |
| 126 | |
| 127 | static_assert(sizeof(DDS_HEADER) == 124, "DDS Header size mismatch"); |
| 128 | static_assert(sizeof(DDS_HEADER_DXT10) == 20, "DDS DX10 Extended Header size mismatch"); |
| 129 | |
| 130 | constexpr DDS_PIXELFORMAT DDSPF_A8R8G8B8 = { |
| 131 | sizeof(DDS_PIXELFORMAT), DDS_RGBA, 0, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000}; |
| 132 | constexpr DDS_PIXELFORMAT DDSPF_X8R8G8B8 = { |
| 133 | sizeof(DDS_PIXELFORMAT), DDS_RGB, 0, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000}; |
| 134 | constexpr DDS_PIXELFORMAT DDSPF_A8B8G8R8 = { |
| 135 | sizeof(DDS_PIXELFORMAT), DDS_RGBA, 0, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000}; |
| 136 | constexpr DDS_PIXELFORMAT DDSPF_X8B8G8R8 = { |
| 137 | sizeof(DDS_PIXELFORMAT), DDS_RGB, 0, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000}; |
| 138 | constexpr DDS_PIXELFORMAT DDSPF_R8G8B8 = { |
| 139 | sizeof(DDS_PIXELFORMAT), DDS_RGB, 0, 24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000}; |
| 140 | |
| 141 | // End of Microsoft code from DDS.h. |
| 142 | |
| 143 | static constexpr bool DDSPixelFormatMatches(const DDS_PIXELFORMAT& pf1, const DDS_PIXELFORMAT& pf2) |
| 144 | { |
| 145 | return std::tie(pf1.dwSize, pf1.dwFlags, pf1.dwFourCC, pf1.dwRGBBitCount, pf1.dwRBitMask, |
| 146 | pf1.dwGBitMask, pf1.dwGBitMask, pf1.dwBBitMask, pf1.dwABitMask) == |
| 147 | std::tie(pf2.dwSize, pf2.dwFlags, pf2.dwFourCC, pf2.dwRGBBitCount, pf2.dwRBitMask, |
| 148 | pf2.dwGBitMask, pf2.dwGBitMask, pf2.dwBBitMask, pf2.dwABitMask); |
| 149 | } |
| 150 | |
| 151 | struct DDSLoadInfo |
| 152 | { |
| 153 | u32 block_size = 1; |
| 154 | u32 bytes_per_block = 4; |
| 155 | u32 width = 0; |
| 156 | u32 height = 0; |
| 157 | u32 mip_count = 0; |
| 158 | u32 array_size = 0; |
| 159 | AbstractTextureFormat format = AbstractTextureFormat::RGBA8; |
| 160 | size_t first_mip_offset = 0; |
| 161 | size_t first_mip_size = 0; |
| 162 | u32 first_mip_row_length = 0; |
| 163 | |
| 164 | std::function<void(VideoCommon::CustomTextureData::ArraySlice::Level*)> conversion_function; |
| 165 | }; |
| 166 | |
| 167 | static constexpr u32 GetBlockCount(u32 extent, u32 block_size) |
| 168 | { |
| 169 | return std::max(Common::AlignUp(extent, block_size) / block_size, 1u); |
| 170 | } |
| 171 | |
| 172 | static u32 CalculateMipCount(u32 width, u32 height) |
| 173 | { |
| 174 | u32 mip_width = width; |
| 175 | u32 mip_height = height; |
| 176 | u32 mip_count = 1; |
| 177 | while (mip_width > 1 || mip_height > 1) |
| 178 | { |
| 179 | mip_width = std::max(mip_width / 2, 1u); |
| 180 | mip_height = std::max(mip_height / 2, 1u); |
| 181 | mip_count++; |
| 182 | } |
| 183 | |
| 184 | return mip_count; |
| 185 | } |
| 186 | |
| 187 | static void ConvertTexture_X8B8G8R8(VideoCommon::CustomTextureData::ArraySlice::Level* level) |
| 188 | { |
| 189 | u8* data_ptr = level->data.data(); |
| 190 | for (u32 row = 0; row < level->height; row++) |
| 191 | { |
| 192 | for (u32 x = 0; x < level->row_length; x++) |
| 193 | { |
| 194 | // Set alpha channel to full intensity. |
| 195 | data_ptr[3] = 0xFF; |
| 196 | data_ptr += sizeof(u32); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | static void ConvertTexture_A8R8G8B8(VideoCommon::CustomTextureData::ArraySlice::Level* level) |
| 202 | { |
| 203 | u8* data_ptr = level->data.data(); |
| 204 | for (u32 row = 0; row < level->height; row++) |
| 205 | { |
| 206 | for (u32 x = 0; x < level->row_length; x++) |
| 207 | { |
| 208 | // Byte swap ABGR -> RGBA |
| 209 | u32 val; |
| 210 | std::memcpy(&val, data_ptr, sizeof(val)); |
| 211 | val = ((val & 0xFF00FF00) | ((val >> 16) & 0xFF) | ((val << 16) & 0xFF0000)); |
| 212 | std::memcpy(data_ptr, &val, sizeof(u32)); |
| 213 | data_ptr += sizeof(u32); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | static void ConvertTexture_X8R8G8B8(VideoCommon::CustomTextureData::ArraySlice::Level* level) |
| 219 | { |
| 220 | u8* data_ptr = level->data.data(); |
| 221 | for (u32 row = 0; row < level->height; row++) |
| 222 | { |
| 223 | for (u32 x = 0; x < level->row_length; x++) |
| 224 | { |
| 225 | // Byte swap XBGR -> RGBX, and set alpha to full intensity. |
| 226 | u32 val; |
| 227 | std::memcpy(&val, data_ptr, sizeof(val)); |
| 228 | val = ((val & 0x0000FF00) | ((val >> 16) & 0xFF) | ((val << 16) & 0xFF0000)) | 0xFF000000; |
| 229 | std::memcpy(data_ptr, &val, sizeof(u32)); |
| 230 | data_ptr += sizeof(u32); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | static void ConvertTexture_R8G8B8(VideoCommon::CustomTextureData::ArraySlice::Level* level) |
| 236 | { |
| 237 | Common::UniqueBuffer<u8> new_data(level->row_length * level->height * sizeof(u32)); |
| 238 | |
| 239 | const u8* rgb_data_ptr = level->data.data(); |
| 240 | u8* data_ptr = new_data.data(); |
| 241 | |
| 242 | for (u32 row = 0; row < level->height; row++) |
| 243 | { |
| 244 | for (u32 x = 0; x < level->row_length; x++) |
| 245 | { |
| 246 | // This is BGR in memory. |
| 247 | u32 val; |
| 248 | std::memcpy(&val, rgb_data_ptr, sizeof(val)); |
| 249 | val = ((val & 0x0000FF00) | ((val >> 16) & 0xFF) | ((val << 16) & 0xFF0000)) | 0xFF000000; |
| 250 | std::memcpy(data_ptr, &val, sizeof(u32)); |
| 251 | data_ptr += sizeof(u32); |
| 252 | rgb_data_ptr += 3; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | level->data = std::move(new_data); |
| 257 | } |
| 258 | |
| 259 | static bool ParseDDSHeader(File::IOFile& file, DDSLoadInfo* info) |
| 260 | { |
| 261 | // Exit as early as possible for non-DDS textures, since all extensions are currently |
| 262 | // passed through this function. |
| 263 | u32 magic; |
| 264 | if (!file.ReadBytes(&magic, sizeof(magic)) || magic != DDS_MAGIC) |
| 265 | return false; |
| 266 | |
| 267 | DDS_HEADER header; |
| 268 | size_t header_size = sizeof(header); |
| 269 | if (!file.ReadBytes(&header, header_size) || header.dwSize < header_size) |
| 270 | return false; |
| 271 | |
| 272 | // Required fields. |
| 273 | if ((header.dwFlags & DDS_HEADER_FLAGS_TEXTURE) != DDS_HEADER_FLAGS_TEXTURE) |
| 274 | return false; |
| 275 | |
| 276 | // Image should be 2D. |
| 277 | if (header.dwFlags & DDS_HEADER_FLAGS_VOLUME) |
| 278 | return false; |
| 279 | |
| 280 | // Presence of width/height fields is already tested by DDS_HEADER_FLAGS_TEXTURE. |
| 281 | info->width = header.dwWidth; |
| 282 | info->height = header.dwHeight; |
| 283 | if (info->width == 0 || info->height == 0) |
| 284 | return false; |
| 285 | |
| 286 | // Check for mip levels. |
| 287 | if (header.dwFlags & DDS_HEADER_FLAGS_MIPMAP) |
| 288 | { |
| 289 | info->mip_count = header.dwMipMapCount; |
| 290 | if (header.dwMipMapCount != 0) |
| 291 | info->mip_count = header.dwMipMapCount; |
| 292 | else |
| 293 | info->mip_count = CalculateMipCount(info->width, info->height); |
| 294 | } |
| 295 | else |
| 296 | { |
| 297 | info->mip_count = 1; |
| 298 | } |
| 299 | |
| 300 | // Handle fourcc formats vs uncompressed formats. |
| 301 | bool has_fourcc = (header.ddspf.dwFlags & DDS_FOURCC) != 0; |
| 302 | bool needs_s3tc = false; |
| 303 | if (has_fourcc) |
| 304 | { |
| 305 | // Handle DX10 extension header. |
| 306 | u32 dxt10_format = 0; |
| 307 | if (header.ddspf.dwFourCC == MAKEFOURCC('D', 'X', '1', '0')) |
| 308 | { |
| 309 | DDS_HEADER_DXT10 dxt10_header; |
| 310 | if (!file.ReadBytes(&dxt10_header, sizeof(dxt10_header))) |
| 311 | return false; |
| 312 | |
| 313 | info->array_size = dxt10_header.arraySize; |
| 314 | header_size += sizeof(dxt10_header); |
| 315 | dxt10_format = dxt10_header.dxgiFormat; |
| 316 | } |
| 317 | else |
| 318 | { |
| 319 | if (header.dwCaps2 & DDS_CUBEMAP) |
| 320 | { |
| 321 | if ((header.dwCaps2 & DDS_CUBEMAP_ALLFACES) != DDS_CUBEMAP_ALLFACES) |
| 322 | { |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | info->array_size = 6; |
| 327 | } |
| 328 | else |
| 329 | { |
| 330 | info->array_size = 1; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // Currently, we only handle compressed textures here, and leave the rest to the SOIL loader. |
| 335 | // In the future, this could be extended, but these isn't much benefit in doing so currently. |
| 336 | if (header.ddspf.dwFourCC == MAKEFOURCC('D', 'X', 'T', '1') || dxt10_format == 71) |
| 337 | { |
| 338 | info->format = AbstractTextureFormat::DXT1; |
| 339 | info->block_size = 4; |
| 340 | info->bytes_per_block = 8; |
| 341 | needs_s3tc = true; |
| 342 | } |
| 343 | else if (header.ddspf.dwFourCC == MAKEFOURCC('D', 'X', 'T', '3') || dxt10_format == 74) |
| 344 | { |
| 345 | info->format = AbstractTextureFormat::DXT3; |
| 346 | info->block_size = 4; |
| 347 | info->bytes_per_block = 16; |
| 348 | needs_s3tc = true; |
| 349 | } |
| 350 | else if (header.ddspf.dwFourCC == MAKEFOURCC('D', 'X', 'T', '5') || dxt10_format == 77) |
| 351 | { |
| 352 | info->format = AbstractTextureFormat::DXT5; |
| 353 | info->block_size = 4; |
| 354 | info->bytes_per_block = 16; |
| 355 | needs_s3tc = true; |
| 356 | } |
| 357 | else if (dxt10_format == 98) |
| 358 | { |
| 359 | info->format = AbstractTextureFormat::BPTC; |
| 360 | info->block_size = 4; |
| 361 | info->bytes_per_block = 16; |
| 362 | if (!g_backend_info.bSupportsBPTCTextures) |
| 363 | return false; |
| 364 | } |
| 365 | else |
| 366 | { |
| 367 | // Leave all remaining formats to SOIL. |
| 368 | return false; |
| 369 | } |
| 370 | } |
| 371 | else |
| 372 | { |
| 373 | if (DDSPixelFormatMatches(header.ddspf, DDSPF_A8R8G8B8)) |
| 374 | { |
| 375 | info->conversion_function = ConvertTexture_A8R8G8B8; |
| 376 | } |
| 377 | else if (DDSPixelFormatMatches(header.ddspf, DDSPF_X8R8G8B8)) |
| 378 | { |
| 379 | info->conversion_function = ConvertTexture_X8R8G8B8; |
| 380 | } |
| 381 | else if (DDSPixelFormatMatches(header.ddspf, DDSPF_X8B8G8R8)) |
| 382 | { |
| 383 | info->conversion_function = ConvertTexture_X8B8G8R8; |
| 384 | } |
| 385 | else if (DDSPixelFormatMatches(header.ddspf, DDSPF_R8G8B8)) |
| 386 | { |
| 387 | info->conversion_function = ConvertTexture_R8G8B8; |
| 388 | } |
| 389 | else if (DDSPixelFormatMatches(header.ddspf, DDSPF_A8B8G8R8)) |
| 390 | { |
| 391 | // This format is already in RGBA order, so no conversion necessary. |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | if (header.dwCaps2 & DDS_CUBEMAP) |
| 399 | { |
| 400 | if ((header.dwCaps2 & DDS_CUBEMAP_ALLFACES) != DDS_CUBEMAP_ALLFACES) |
| 401 | { |
| 402 | return false; |
| 403 | } |
| 404 | |
| 405 | info->array_size = 6; |
| 406 | } |
| 407 | else |
| 408 | { |
| 409 | info->array_size = 1; |
| 410 | } |
| 411 | |
| 412 | // All these formats are RGBA, just with byte swapping. |
| 413 | info->format = AbstractTextureFormat::RGBA8; |
| 414 | info->block_size = 1; |
| 415 | info->bytes_per_block = header.ddspf.dwRGBBitCount / 8; |
| 416 | } |
| 417 | |
| 418 | // We also need to ensure the backend supports these formats natively before loading them, |
| 419 | // otherwise, fallback to SOIL, which will decompress them to RGBA. |
| 420 | if (needs_s3tc && !g_backend_info.bSupportsST3CTextures) |
| 421 | return false; |
| 422 | |
| 423 | // Mip levels smaller than the block size are padded to multiples of the block size. |
| 424 | u32 blocks_wide = GetBlockCount(info->width, info->block_size); |
| 425 | u32 blocks_high = GetBlockCount(info->height, info->block_size); |
| 426 | |
| 427 | // Pitch can be specified in the header, otherwise we can derive it from the dimensions. For |
| 428 | // compressed formats, both DDS_HEADER_FLAGS_LINEARSIZE and DDS_HEADER_FLAGS_PITCH should be |
| 429 | // set. See https://msdn.microsoft.com/en-us/library/windows/desktop/bb943982(v=vs.85).aspx |
| 430 | if (header.dwFlags & DDS_HEADER_FLAGS_PITCH && header.dwFlags & DDS_HEADER_FLAGS_LINEARSIZE) |
| 431 | { |
| 432 | // Convert pitch (in bytes) to texels/row length. |
| 433 | if (header.dwPitchOrLinearSize < info->bytes_per_block) |
| 434 | { |
| 435 | // Likely a corrupted or invalid file. |
| 436 | return false; |
| 437 | } |
| 438 | |
| 439 | info->first_mip_row_length = |
| 440 | std::max(header.dwPitchOrLinearSize / info->bytes_per_block, 1u) * info->block_size; |
| 441 | info->first_mip_size = static_cast<size_t>(info->first_mip_row_length / info->block_size) * |
| 442 | info->block_size * blocks_high; |
| 443 | } |
| 444 | else |
| 445 | { |
| 446 | // Assume no padding between rows of blocks. |
| 447 | info->first_mip_row_length = blocks_wide * info->block_size; |
| 448 | info->first_mip_size = blocks_wide * static_cast<size_t>(info->bytes_per_block) * blocks_high; |
| 449 | } |
| 450 | |
| 451 | // Check for truncated or corrupted files. |
| 452 | info->first_mip_offset = sizeof(magic) + header_size; |
| 453 | if (info->first_mip_offset >= file.GetSize()) |
| 454 | return false; |
| 455 | |
| 456 | return true; |
| 457 | } |
| 458 | |
| 459 | static bool ReadMipLevel(VideoCommon::CustomTextureData::ArraySlice::Level* level, |
| 460 | File::IOFile& file, const std::string& filename, u32 mip_level, |
| 461 | const DDSLoadInfo& info, u32 width, u32 height, u32 row_length, |
| 462 | size_t size) |
| 463 | { |
| 464 | // D3D11 cannot handle block compressed textures where the first mip level is |
| 465 | // not a multiple of the block size. |
| 466 | if (mip_level == 0 && info.block_size > 1 && |
| 467 | ((width % info.block_size) != 0 || (height % info.block_size) != 0)) |
| 468 | { |
| 469 | ERROR_LOG_FMT(VIDEO, |
| 470 | "Invalid dimensions for DDS texture {}. For compressed textures of this format, " |
| 471 | "the width/height of the first mip level must be a multiple of {}.", |
| 472 | filename, info.block_size); |
| 473 | return false; |
| 474 | } |
| 475 | |
| 476 | // Copy to the final storage location. |
| 477 | level->width = width; |
| 478 | level->height = height; |
| 479 | level->format = info.format; |
| 480 | level->row_length = row_length; |
| 481 | level->data.reset(size); |
| 482 | if (!file.ReadBytes(level->data.data(), level->data.size())) |
| 483 | return false; |
| 484 | |
| 485 | // Apply conversion function for uncompressed textures. |
| 486 | if (info.conversion_function) |
| 487 | info.conversion_function(level); |
| 488 | |
| 489 | return true; |
| 490 | } |
| 491 | |
| 492 | } // namespace |
| 493 | |
| 494 | namespace VideoCommon |
| 495 | { |
| 496 | bool LoadDDSTexture(CustomTextureData* texture, const std::string& filename) |
| 497 | { |
| 498 | File::IOFile file; |
| 499 | file.Open(filename, "rb"); |
| 500 | if (!file.IsOpen()) |
| 501 | return false; |
| 502 | |
| 503 | DDSLoadInfo info; |
| 504 | if (!ParseDDSHeader(file, &info)) |
| 505 | return false; |
| 506 | |
| 507 | if (!file.Seek(info.first_mip_offset, File::SeekOrigin::Begin)) |
| 508 | return false; |
| 509 | |
| 510 | for (u32 arr_i = 0; arr_i < info.array_size; arr_i++) |
| 511 | { |
| 512 | auto& slice = texture->m_slices.emplace_back(); |
| 513 | // Read first mip level, as it may have a custom pitch. |
| 514 | CustomTextureData::ArraySlice::Level first_level; |
| 515 | if (!ReadMipLevel(&first_level, file, filename, 0, info, info.width, info.height, |
| 516 | info.first_mip_row_length, info.first_mip_size)) |
| 517 | { |
| 518 | return false; |
| 519 | } |
| 520 | |
| 521 | slice.m_levels.push_back(std::move(first_level)); |
| 522 | |
| 523 | // Read in any remaining mip levels in the file. |
| 524 | // If the .dds file does not contain a full mip chain, we'll fall back to the old path. |
| 525 | u32 mip_width = info.width; |
| 526 | u32 mip_height = info.height; |
| 527 | for (u32 i = 1; i < info.mip_count; i++) |
| 528 | { |
| 529 | mip_width = std::max(mip_width / 2, 1u); |
| 530 | mip_height = std::max(mip_height / 2, 1u); |
| 531 | |
| 532 | // Pitch can't be specified with each mip level, so we have to calculate it ourselves. |
| 533 | u32 blocks_wide = GetBlockCount(mip_width, info.block_size); |
| 534 | u32 blocks_high = GetBlockCount(mip_height, info.block_size); |
| 535 | u32 mip_row_length = blocks_wide * info.block_size; |
| 536 | size_t mip_size = blocks_wide * static_cast<size_t>(info.bytes_per_block) * blocks_high; |
| 537 | CustomTextureData::ArraySlice::Level level; |
| 538 | if (!ReadMipLevel(&level, file, filename, i, info, mip_width, mip_height, mip_row_length, |
| 539 | mip_size)) |
| 540 | break; |
| 541 | |
| 542 | slice.m_levels.push_back(std::move(level)); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | return true; |
| 547 | } |
| 548 | |
| 549 | bool LoadDDSTexture(CustomTextureData::ArraySlice::Level* level, const std::string& filename, |
| 550 | u32 mip_level) |
| 551 | { |
| 552 | // Only loading a single mip level. |
| 553 | File::IOFile file; |
| 554 | file.Open(filename, "rb"); |
| 555 | if (!file.IsOpen()) |
| 556 | return false; |
| 557 | |
| 558 | DDSLoadInfo info; |
| 559 | if (!ParseDDSHeader(file, &info)) |
| 560 | return false; |
| 561 | |
| 562 | return ReadMipLevel(level, file, filename, mip_level, info, info.width, info.height, |
| 563 | info.first_mip_row_length, info.first_mip_size); |
| 564 | } |
| 565 | |
| 566 | bool LoadPNGTexture(CustomTextureData::ArraySlice::Level* level, const std::string& filename) |
| 567 | { |
| 568 | if (!level) [[unlikely]] |
| 569 | return false; |
| 570 | |
| 571 | File::IOFile file; |
| 572 | file.Open(filename, "rb"); |
| 573 | Common::UniqueBuffer<u8> buffer(file.GetSize()); |
| 574 | file.ReadBytes(buffer.data(), file.GetSize()); |
| 575 | |
| 576 | return LoadPNGTexture(level, buffer); |
| 577 | } |
| 578 | |
| 579 | bool LoadPNGTexture(CustomTextureData::ArraySlice::Level* level, std::span<const u8> buffer) |
| 580 | { |
| 581 | if (!level) [[unlikely]] |
| 582 | return false; |
| 583 | |
| 584 | if (!Common::LoadPNG(buffer, &level->data, &level->width, &level->height)) |
| 585 | return false; |
| 586 | |
| 587 | if (level->data.empty()) |
| 588 | return false; |
| 589 | |
| 590 | // Loaded PNG images are converted to RGBA. |
| 591 | level->format = AbstractTextureFormat::RGBA8; |
| 592 | level->row_length = level->width; |
| 593 | return true; |
| 594 | } |
| 595 | } // namespace VideoCommon |