texture_loader_dds.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /**************************************************************************/
  2. /* texture_loader_dds.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "texture_loader_dds.h"
  31. #include "core/io/file_access.h"
  32. #include "scene/resources/image_texture.h"
  33. #define PF_FOURCC(s) ((uint32_t)(((s)[3] << 24U) | ((s)[2] << 16U) | ((s)[1] << 8U) | ((s)[0])))
  34. // Reference: https://docs.microsoft.com/en-us/windows/win32/direct3ddds/dds-header
  35. enum {
  36. DDS_MAGIC = 0x20534444,
  37. DDSD_PITCH = 0x00000008,
  38. DDSD_LINEARSIZE = 0x00080000,
  39. DDSD_MIPMAPCOUNT = 0x00020000,
  40. DDPF_FOURCC = 0x00000004,
  41. DDPF_ALPHAPIXELS = 0x00000001,
  42. DDPF_RGB = 0x00000040
  43. };
  44. enum DDSFourCC {
  45. DDFCC_DXT1 = PF_FOURCC("DXT1"),
  46. DDFCC_DXT3 = PF_FOURCC("DXT3"),
  47. DDFCC_DXT5 = PF_FOURCC("DXT5"),
  48. DDFCC_ATI1 = PF_FOURCC("ATI1"),
  49. DDFCC_BC4U = PF_FOURCC("BC4U"),
  50. DDFCC_ATI2 = PF_FOURCC("ATI2"),
  51. DDFCC_BC5U = PF_FOURCC("BC5U"),
  52. DDFCC_A2XY = PF_FOURCC("A2XY"),
  53. DDFCC_DX10 = PF_FOURCC("DX10"),
  54. DDFCC_R16F = 111,
  55. DDFCC_RG16F = 112,
  56. DDFCC_RGBA16F = 113,
  57. DDFCC_R32F = 114,
  58. DDFCC_RG32F = 115,
  59. DDFCC_RGBA32F = 116
  60. };
  61. // Reference: https://learn.microsoft.com/en-us/windows/win32/api/dxgiformat/ne-dxgiformat-dxgi_format
  62. enum DXGIFormat {
  63. DXGI_R32G32B32A32_FLOAT = 2,
  64. DXGI_R16G16B16A16_FLOAT = 10,
  65. DXGI_R32G32_FLOAT = 16,
  66. DXGI_R10G10B10A2_UNORM = 24,
  67. DXGI_R8G8B8A8_UNORM = 28,
  68. DXGI_R16G16_FLOAT = 34,
  69. DXGI_R32_FLOAT = 41,
  70. DXGI_R16_FLOAT = 54,
  71. DXGI_R9G9B9E5 = 67,
  72. DXGI_BC1_UNORM = 71,
  73. DXGI_BC2_UNORM = 74,
  74. DXGI_BC3_UNORM = 77,
  75. DXGI_BC4_UNORM = 80,
  76. DXGI_BC5_UNORM = 83,
  77. DXGI_B5G6R5_UNORM = 85,
  78. DXGI_B5G5R5A1_UNORM = 86,
  79. DXGI_B8G8R8A8_UNORM = 87,
  80. DXGI_BC6H_UF16 = 95,
  81. DXGI_BC6H_SF16 = 96,
  82. DXGI_BC7_UNORM = 98,
  83. DXGI_B4G4R4A4_UNORM = 115
  84. };
  85. // The legacy bitmasked format names here represent the actual data layout in the files,
  86. // while their official names are flipped (e.g. RGBA8 layout is officially called ABGR8).
  87. enum DDSFormat {
  88. DDS_DXT1,
  89. DDS_DXT3,
  90. DDS_DXT5,
  91. DDS_ATI1,
  92. DDS_ATI2,
  93. DDS_BC6U,
  94. DDS_BC6S,
  95. DDS_BC7U,
  96. DDS_R16F,
  97. DDS_RG16F,
  98. DDS_RGBA16F,
  99. DDS_R32F,
  100. DDS_RG32F,
  101. DDS_RGBA32F,
  102. DDS_RGB9E5,
  103. DDS_BGRA8,
  104. DDS_BGR8,
  105. DDS_RGBA8,
  106. DDS_RGB8,
  107. DDS_BGR5A1,
  108. DDS_BGR565,
  109. DDS_BGR10A2,
  110. DDS_RGB10A2,
  111. DDS_BGRA4,
  112. DDS_LUMINANCE,
  113. DDS_LUMINANCE_ALPHA,
  114. DDS_MAX
  115. };
  116. struct DDSFormatInfo {
  117. const char *name = nullptr;
  118. bool compressed = false;
  119. uint32_t divisor = 0;
  120. uint32_t block_size = 0;
  121. Image::Format format = Image::Format::FORMAT_BPTC_RGBA;
  122. };
  123. static const DDSFormatInfo dds_format_info[DDS_MAX] = {
  124. { "DXT1/BC1", true, 4, 8, Image::FORMAT_DXT1 },
  125. { "DXT3/BC2", true, 4, 16, Image::FORMAT_DXT3 },
  126. { "DXT5/BC3", true, 4, 16, Image::FORMAT_DXT5 },
  127. { "ATI1/BC4", true, 4, 8, Image::FORMAT_RGTC_R },
  128. { "ATI2/A2XY/BC5", true, 4, 16, Image::FORMAT_RGTC_RG },
  129. { "BC6U", true, 4, 16, Image::FORMAT_BPTC_RGBFU },
  130. { "BC6S", true, 4, 16, Image::FORMAT_BPTC_RGBF },
  131. { "BC7U", true, 4, 16, Image::FORMAT_BPTC_RGBA },
  132. { "R16F", false, 1, 2, Image::FORMAT_RH },
  133. { "RG16F", false, 1, 4, Image::FORMAT_RGH },
  134. { "RGBA16F", false, 1, 8, Image::FORMAT_RGBAH },
  135. { "R32F", false, 1, 4, Image::FORMAT_RF },
  136. { "RG32F", false, 1, 8, Image::FORMAT_RGF },
  137. { "RGBA32F", false, 1, 16, Image::FORMAT_RGBAF },
  138. { "RGB9E5", false, 1, 4, Image::FORMAT_RGBE9995 },
  139. { "BGRA8", false, 1, 4, Image::FORMAT_RGBA8 },
  140. { "BGR8", false, 1, 3, Image::FORMAT_RGB8 },
  141. { "RGBA8", false, 1, 4, Image::FORMAT_RGBA8 },
  142. { "RGB8", false, 1, 3, Image::FORMAT_RGB8 },
  143. { "BGR5A1", false, 1, 2, Image::FORMAT_RGBA8 },
  144. { "BGR565", false, 1, 2, Image::FORMAT_RGB8 },
  145. { "BGR10A2", false, 1, 4, Image::FORMAT_RGBA8 },
  146. { "RGB10A2", false, 1, 4, Image::FORMAT_RGBA8 },
  147. { "BGRA4", false, 1, 2, Image::FORMAT_RGBA8 },
  148. { "GRAYSCALE", false, 1, 1, Image::FORMAT_L8 },
  149. { "GRAYSCALE_ALPHA", false, 1, 2, Image::FORMAT_LA8 }
  150. };
  151. static DDSFormat dxgi_to_dds_format(uint32_t p_dxgi_format) {
  152. switch (p_dxgi_format) {
  153. case DXGI_R32G32B32A32_FLOAT: {
  154. return DDS_RGBA32F;
  155. }
  156. case DXGI_R16G16B16A16_FLOAT: {
  157. return DDS_RGBA16F;
  158. }
  159. case DXGI_R32G32_FLOAT: {
  160. return DDS_RG32F;
  161. }
  162. case DXGI_R10G10B10A2_UNORM: {
  163. return DDS_RGB10A2;
  164. }
  165. case DXGI_R8G8B8A8_UNORM: {
  166. return DDS_RGBA8;
  167. }
  168. case DXGI_R16G16_FLOAT: {
  169. return DDS_RG16F;
  170. }
  171. case DXGI_R32_FLOAT: {
  172. return DDS_R32F;
  173. }
  174. case DXGI_R16_FLOAT: {
  175. return DDS_R16F;
  176. }
  177. case DXGI_R9G9B9E5: {
  178. return DDS_RGB9E5;
  179. }
  180. case DXGI_BC1_UNORM: {
  181. return DDS_DXT1;
  182. }
  183. case DXGI_BC2_UNORM: {
  184. return DDS_DXT3;
  185. }
  186. case DXGI_BC3_UNORM: {
  187. return DDS_DXT5;
  188. }
  189. case DXGI_BC4_UNORM: {
  190. return DDS_ATI1;
  191. }
  192. case DXGI_BC5_UNORM: {
  193. return DDS_ATI2;
  194. }
  195. case DXGI_B5G6R5_UNORM: {
  196. return DDS_BGR565;
  197. }
  198. case DXGI_B5G5R5A1_UNORM: {
  199. return DDS_BGR5A1;
  200. }
  201. case DXGI_B8G8R8A8_UNORM: {
  202. return DDS_BGRA8;
  203. }
  204. case DXGI_BC6H_UF16: {
  205. return DDS_BC6U;
  206. }
  207. case DXGI_BC6H_SF16: {
  208. return DDS_BC6S;
  209. }
  210. case DXGI_BC7_UNORM: {
  211. return DDS_BC7U;
  212. }
  213. case DXGI_B4G4R4A4_UNORM: {
  214. return DDS_BGRA4;
  215. }
  216. default: {
  217. return DDS_MAX;
  218. }
  219. }
  220. }
  221. Ref<Resource> ResourceFormatDDS::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
  222. if (r_error) {
  223. *r_error = ERR_CANT_OPEN;
  224. }
  225. Error err;
  226. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  227. if (f.is_null()) {
  228. return Ref<Resource>();
  229. }
  230. Ref<FileAccess> fref(f);
  231. if (r_error) {
  232. *r_error = ERR_FILE_CORRUPT;
  233. }
  234. ERR_FAIL_COND_V_MSG(err != OK, Ref<Resource>(), "Unable to open DDS texture file '" + p_path + "'.");
  235. uint32_t magic = f->get_32();
  236. uint32_t hsize = f->get_32();
  237. uint32_t flags = f->get_32();
  238. uint32_t height = f->get_32();
  239. uint32_t width = f->get_32();
  240. uint32_t pitch = f->get_32();
  241. /* uint32_t depth = */ f->get_32();
  242. uint32_t mipmaps = f->get_32();
  243. // Skip reserved.
  244. for (int i = 0; i < 11; i++) {
  245. f->get_32();
  246. }
  247. // Validate.
  248. // We don't check DDSD_CAPS or DDSD_PIXELFORMAT, as they're mandatory when writing,
  249. // but non-mandatory when reading (as some writers don't set them).
  250. if (magic != DDS_MAGIC || hsize != 124) {
  251. ERR_FAIL_V_MSG(Ref<Resource>(), "Invalid or unsupported DDS texture file '" + p_path + "'.");
  252. }
  253. /* uint32_t format_size = */ f->get_32();
  254. uint32_t format_flags = f->get_32();
  255. uint32_t format_fourcc = f->get_32();
  256. uint32_t format_rgb_bits = f->get_32();
  257. uint32_t format_red_mask = f->get_32();
  258. uint32_t format_green_mask = f->get_32();
  259. uint32_t format_blue_mask = f->get_32();
  260. uint32_t format_alpha_mask = f->get_32();
  261. /* uint32_t caps_1 = */ f->get_32();
  262. /* uint32_t caps_2 = */ f->get_32();
  263. /* uint32_t caps_3 = */ f->get_32();
  264. /* uint32_t caps_4 = */ f->get_32();
  265. // Skip reserved.
  266. f->get_32();
  267. if (f->get_position() < 128) {
  268. f->seek(128);
  269. }
  270. DDSFormat dds_format = DDS_MAX;
  271. if (format_flags & DDPF_FOURCC) {
  272. // FourCC formats.
  273. switch (format_fourcc) {
  274. case DDFCC_DXT1: {
  275. dds_format = DDS_DXT1;
  276. } break;
  277. case DDFCC_DXT3: {
  278. dds_format = DDS_DXT3;
  279. } break;
  280. case DDFCC_DXT5: {
  281. dds_format = DDS_DXT5;
  282. } break;
  283. case DDFCC_ATI1:
  284. case DDFCC_BC4U: {
  285. dds_format = DDS_ATI1;
  286. } break;
  287. case DDFCC_ATI2:
  288. case DDFCC_BC5U:
  289. case DDFCC_A2XY: {
  290. dds_format = DDS_ATI2;
  291. } break;
  292. case DDFCC_R16F: {
  293. dds_format = DDS_R16F;
  294. } break;
  295. case DDFCC_RG16F: {
  296. dds_format = DDS_RG16F;
  297. } break;
  298. case DDFCC_RGBA16F: {
  299. dds_format = DDS_RGBA16F;
  300. } break;
  301. case DDFCC_R32F: {
  302. dds_format = DDS_R32F;
  303. } break;
  304. case DDFCC_RG32F: {
  305. dds_format = DDS_RG32F;
  306. } break;
  307. case DDFCC_RGBA32F: {
  308. dds_format = DDS_RGBA32F;
  309. } break;
  310. case DDFCC_DX10: {
  311. uint32_t dxgi_format = f->get_32();
  312. /* uint32_t dimension = */ f->get_32();
  313. /* uint32_t misc_flags_1 = */ f->get_32();
  314. /* uint32_t array_size = */ f->get_32();
  315. /* uint32_t misc_flags_2 = */ f->get_32();
  316. dds_format = dxgi_to_dds_format(dxgi_format);
  317. } break;
  318. default: {
  319. ERR_FAIL_V_MSG(Ref<Resource>(), "Unrecognized or unsupported FourCC in DDS '" + p_path + "'.");
  320. }
  321. }
  322. } else if (format_flags & DDPF_RGB) {
  323. // Channel-bitmasked formats.
  324. if (format_flags & DDPF_ALPHAPIXELS) {
  325. // With alpha.
  326. if (format_rgb_bits == 32 && format_red_mask == 0xff0000 && format_green_mask == 0xff00 && format_blue_mask == 0xff && format_alpha_mask == 0xff000000) {
  327. dds_format = DDS_BGRA8;
  328. } else if (format_rgb_bits == 32 && format_red_mask == 0xff && format_green_mask == 0xff00 && format_blue_mask == 0xff0000 && format_alpha_mask == 0xff000000) {
  329. dds_format = DDS_RGBA8;
  330. } else if (format_rgb_bits == 16 && format_red_mask == 0x00007c00 && format_green_mask == 0x000003e0 && format_blue_mask == 0x0000001f && format_alpha_mask == 0x00008000) {
  331. dds_format = DDS_BGR5A1;
  332. } else if (format_rgb_bits == 32 && format_red_mask == 0x3ff00000 && format_green_mask == 0xffc00 && format_blue_mask == 0x3ff && format_alpha_mask == 0xc0000000) {
  333. dds_format = DDS_BGR10A2;
  334. } else if (format_rgb_bits == 32 && format_red_mask == 0x3ff && format_green_mask == 0xffc00 && format_blue_mask == 0x3ff00000 && format_alpha_mask == 0xc0000000) {
  335. dds_format = DDS_RGB10A2;
  336. } else if (format_rgb_bits == 16 && format_red_mask == 0xf00 && format_green_mask == 0xf0 && format_blue_mask == 0xf && format_alpha_mask == 0xf000) {
  337. dds_format = DDS_BGRA4;
  338. }
  339. } else {
  340. // Without alpha.
  341. if (format_rgb_bits == 24 && format_red_mask == 0xff0000 && format_green_mask == 0xff00 && format_blue_mask == 0xff) {
  342. dds_format = DDS_BGR8;
  343. } else if (format_rgb_bits == 24 && format_red_mask == 0xff && format_green_mask == 0xff00 && format_blue_mask == 0xff0000) {
  344. dds_format = DDS_RGB8;
  345. } else if (format_rgb_bits == 16 && format_red_mask == 0x0000f800 && format_green_mask == 0x000007e0 && format_blue_mask == 0x0000001f) {
  346. dds_format = DDS_BGR565;
  347. }
  348. }
  349. } else {
  350. // Other formats.
  351. if (format_flags & DDPF_ALPHAPIXELS && format_rgb_bits == 16 && format_red_mask == 0xff && format_alpha_mask == 0xff00) {
  352. dds_format = DDS_LUMINANCE_ALPHA;
  353. } else if (!(format_flags & DDPF_ALPHAPIXELS) && format_rgb_bits == 8 && format_red_mask == 0xff) {
  354. dds_format = DDS_LUMINANCE;
  355. }
  356. }
  357. // No format detected, error.
  358. if (dds_format == DDS_MAX) {
  359. ERR_FAIL_V_MSG(Ref<Resource>(), "Unrecognized or unsupported color layout in DDS '" + p_path + "'.");
  360. }
  361. if (!(flags & DDSD_MIPMAPCOUNT)) {
  362. mipmaps = 1;
  363. }
  364. Vector<uint8_t> src_data;
  365. const DDSFormatInfo &info = dds_format_info[dds_format];
  366. uint32_t w = width;
  367. uint32_t h = height;
  368. if (info.compressed) {
  369. // BC compressed.
  370. uint32_t size = MAX(info.divisor, w) / info.divisor * MAX(info.divisor, h) / info.divisor * info.block_size;
  371. if (flags & DDSD_LINEARSIZE) {
  372. ERR_FAIL_COND_V_MSG(size != pitch, Ref<Resource>(), "DDS header flags specify that a linear size of the top-level image is present, but the specified size does not match the expected value.");
  373. } else {
  374. ERR_FAIL_COND_V_MSG(pitch != 0, Ref<Resource>(), "DDS header flags specify that no linear size will given for the top-level image, but a non-zero linear size value is present in the header.");
  375. }
  376. for (uint32_t i = 1; i < mipmaps; i++) {
  377. w = MAX(1u, w >> 1);
  378. h = MAX(1u, h >> 1);
  379. uint32_t bsize = MAX(info.divisor, w) / info.divisor * MAX(info.divisor, h) / info.divisor * info.block_size;
  380. size += bsize;
  381. }
  382. src_data.resize(size);
  383. uint8_t *wb = src_data.ptrw();
  384. f->get_buffer(wb, size);
  385. } else {
  386. // Generic uncompressed.
  387. uint32_t size = width * height * info.block_size;
  388. for (uint32_t i = 1; i < mipmaps; i++) {
  389. w = (w + 1) >> 1;
  390. h = (h + 1) >> 1;
  391. size += w * h * info.block_size;
  392. }
  393. // Calculate the space these formats will take up after decoding.
  394. if (dds_format == DDS_BGR565) {
  395. size = size * 3 / 2;
  396. } else if (dds_format == DDS_BGR5A1 || dds_format == DDS_BGRA4) {
  397. size = size * 2;
  398. }
  399. src_data.resize(size);
  400. uint8_t *wb = src_data.ptrw();
  401. f->get_buffer(wb, size);
  402. // Decode nonstandard formats.
  403. switch (dds_format) {
  404. case DDS_BGR5A1: {
  405. // To RGBA8.
  406. int colcount = size / 4;
  407. for (int i = colcount - 1; i >= 0; i--) {
  408. int src_ofs = i * 2;
  409. int dst_ofs = i * 4;
  410. uint8_t a = wb[src_ofs + 1] & 0x80;
  411. uint8_t b = wb[src_ofs] & 0x1F;
  412. uint8_t g = (wb[src_ofs] >> 5) | ((wb[src_ofs + 1] & 0x3) << 3);
  413. uint8_t r = (wb[src_ofs + 1] >> 2) & 0x1F;
  414. wb[dst_ofs + 0] = r << 3;
  415. wb[dst_ofs + 1] = g << 3;
  416. wb[dst_ofs + 2] = b << 3;
  417. wb[dst_ofs + 3] = a ? 255 : 0;
  418. }
  419. } break;
  420. case DDS_BGR565: {
  421. // To RGB8.
  422. int colcount = size / 3;
  423. for (int i = colcount - 1; i >= 0; i--) {
  424. int src_ofs = i * 2;
  425. int dst_ofs = i * 3;
  426. uint8_t b = wb[src_ofs] & 0x1F;
  427. uint8_t g = (wb[src_ofs] >> 5) | ((wb[src_ofs + 1] & 0x7) << 3);
  428. uint8_t r = wb[src_ofs + 1] >> 3;
  429. wb[dst_ofs + 0] = r << 3;
  430. wb[dst_ofs + 1] = g << 2;
  431. wb[dst_ofs + 2] = b << 3;
  432. }
  433. } break;
  434. case DDS_BGRA4: {
  435. // To RGBA8.
  436. int colcount = size / 4;
  437. for (int i = colcount - 1; i >= 0; i--) {
  438. int src_ofs = i * 2;
  439. int dst_ofs = i * 4;
  440. uint8_t b = wb[src_ofs] & 0x0F;
  441. uint8_t g = wb[src_ofs] & 0xF0;
  442. uint8_t r = wb[src_ofs + 1] & 0x0F;
  443. uint8_t a = wb[src_ofs + 1] & 0xF0;
  444. wb[dst_ofs] = (r << 4) | r;
  445. wb[dst_ofs + 1] = g | (g >> 4);
  446. wb[dst_ofs + 2] = (b << 4) | b;
  447. wb[dst_ofs + 3] = a | (a >> 4);
  448. }
  449. } break;
  450. case DDS_RGB10A2: {
  451. // To RGBA8.
  452. int colcount = size / 4;
  453. for (int i = 0; i < colcount; i++) {
  454. int ofs = i * 4;
  455. uint32_t w32 = uint32_t(wb[ofs + 0]) | (uint32_t(wb[ofs + 1]) << 8) | (uint32_t(wb[ofs + 2]) << 16) | (uint32_t(wb[ofs + 3]) << 24);
  456. // This method follows the 'standard' way of decoding 10-bit dds files,
  457. // which means the ones created with DirectXTex will be loaded incorrectly.
  458. uint8_t a = (w32 & 0xc0000000) >> 24;
  459. uint8_t r = (w32 & 0x3ff) >> 2;
  460. uint8_t g = (w32 & 0xffc00) >> 12;
  461. uint8_t b = (w32 & 0x3ff00000) >> 22;
  462. wb[ofs + 0] = r;
  463. wb[ofs + 1] = g;
  464. wb[ofs + 2] = b;
  465. wb[ofs + 3] = a == 0xc0 ? 255 : a; // 0xc0 should be opaque.
  466. }
  467. } break;
  468. case DDS_BGR10A2: {
  469. // To RGBA8.
  470. int colcount = size / 4;
  471. for (int i = 0; i < colcount; i++) {
  472. int ofs = i * 4;
  473. uint32_t w32 = uint32_t(wb[ofs + 0]) | (uint32_t(wb[ofs + 1]) << 8) | (uint32_t(wb[ofs + 2]) << 16) | (uint32_t(wb[ofs + 3]) << 24);
  474. // This method follows the 'standard' way of decoding 10-bit dds files,
  475. // which means the ones created with DirectXTex will be loaded incorrectly.
  476. uint8_t a = (w32 & 0xc0000000) >> 24;
  477. uint8_t r = (w32 & 0x3ff00000) >> 22;
  478. uint8_t g = (w32 & 0xffc00) >> 12;
  479. uint8_t b = (w32 & 0x3ff) >> 2;
  480. wb[ofs + 0] = r;
  481. wb[ofs + 1] = g;
  482. wb[ofs + 2] = b;
  483. wb[ofs + 3] = a == 0xc0 ? 255 : a; // 0xc0 should be opaque.
  484. }
  485. } break;
  486. case DDS_BGRA8: {
  487. // To RGBA8.
  488. int colcount = size / 4;
  489. for (int i = 0; i < colcount; i++) {
  490. SWAP(wb[i * 4 + 0], wb[i * 4 + 2]);
  491. }
  492. } break;
  493. case DDS_BGR8: {
  494. // To RGB8.
  495. int colcount = size / 3;
  496. for (int i = 0; i < colcount; i++) {
  497. SWAP(wb[i * 3 + 0], wb[i * 3 + 2]);
  498. }
  499. } break;
  500. default: {
  501. }
  502. }
  503. }
  504. Ref<Image> img = memnew(Image(width, height, mipmaps - 1, info.format, src_data));
  505. Ref<ImageTexture> texture = ImageTexture::create_from_image(img);
  506. if (r_error) {
  507. *r_error = OK;
  508. }
  509. return texture;
  510. }
  511. void ResourceFormatDDS::get_recognized_extensions(List<String> *p_extensions) const {
  512. p_extensions->push_back("dds");
  513. }
  514. bool ResourceFormatDDS::handles_type(const String &p_type) const {
  515. return ClassDB::is_parent_class(p_type, "Texture2D");
  516. }
  517. String ResourceFormatDDS::get_resource_type(const String &p_path) const {
  518. if (p_path.get_extension().to_lower() == "dds") {
  519. return "ImageTexture";
  520. }
  521. return "";
  522. }