texture_loader_dds.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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_ALPHAPIXELS = 0x00000001,
  41. DDPF_ALPHAONLY = 0x00000002,
  42. DDPF_FOURCC = 0x00000004,
  43. DDPF_RGB = 0x00000040,
  44. DDPF_RG_SNORM = 0x00080000,
  45. DDSC2_CUBEMAP = 0x200,
  46. DDSC2_VOLUME = 0x200000,
  47. DX10D_1D = 2,
  48. DX10D_2D = 3,
  49. DX10D_3D = 4,
  50. };
  51. enum DDSFourCC {
  52. DDFCC_DXT1 = PF_FOURCC("DXT1"),
  53. DDFCC_DXT2 = PF_FOURCC("DXT2"),
  54. DDFCC_DXT3 = PF_FOURCC("DXT3"),
  55. DDFCC_DXT4 = PF_FOURCC("DXT4"),
  56. DDFCC_DXT5 = PF_FOURCC("DXT5"),
  57. DDFCC_ATI1 = PF_FOURCC("ATI1"),
  58. DDFCC_BC4U = PF_FOURCC("BC4U"),
  59. DDFCC_ATI2 = PF_FOURCC("ATI2"),
  60. DDFCC_BC5U = PF_FOURCC("BC5U"),
  61. DDFCC_A2XY = PF_FOURCC("A2XY"),
  62. DDFCC_DX10 = PF_FOURCC("DX10"),
  63. DDFCC_R16F = 111,
  64. DDFCC_RG16F = 112,
  65. DDFCC_RGBA16F = 113,
  66. DDFCC_R32F = 114,
  67. DDFCC_RG32F = 115,
  68. DDFCC_RGBA32F = 116
  69. };
  70. // Reference: https://learn.microsoft.com/en-us/windows/win32/api/dxgiformat/ne-dxgiformat-dxgi_format
  71. enum DXGIFormat {
  72. DXGI_R32G32B32A32_FLOAT = 2,
  73. DXGI_R32G32B32_FLOAT = 6,
  74. DXGI_R16G16B16A16_FLOAT = 10,
  75. DXGI_R32G32_FLOAT = 16,
  76. DXGI_R10G10B10A2_UNORM = 24,
  77. DXGI_R8G8B8A8_UNORM = 28,
  78. DXGI_R8G8B8A8_UNORM_SRGB = 29,
  79. DXGI_R16G16_FLOAT = 34,
  80. DXGI_R32_FLOAT = 41,
  81. DXGI_R8G8_UNORM = 49,
  82. DXGI_R16_FLOAT = 54,
  83. DXGI_R8_UNORM = 61,
  84. DXGI_A8_UNORM = 65,
  85. DXGI_R9G9B9E5 = 67,
  86. DXGI_BC1_UNORM = 71,
  87. DXGI_BC1_UNORM_SRGB = 72,
  88. DXGI_BC2_UNORM = 74,
  89. DXGI_BC2_UNORM_SRGB = 75,
  90. DXGI_BC3_UNORM = 77,
  91. DXGI_BC3_UNORM_SRGB = 78,
  92. DXGI_BC4_UNORM = 80,
  93. DXGI_BC5_UNORM = 83,
  94. DXGI_B5G6R5_UNORM = 85,
  95. DXGI_B5G5R5A1_UNORM = 86,
  96. DXGI_B8G8R8A8_UNORM = 87,
  97. DXGI_BC6H_UF16 = 95,
  98. DXGI_BC6H_SF16 = 96,
  99. DXGI_BC7_UNORM = 98,
  100. DXGI_BC7_UNORM_SRGB = 99,
  101. DXGI_B4G4R4A4_UNORM = 115
  102. };
  103. // The legacy bitmasked format names here represent the actual data layout in the files,
  104. // while their official names are flipped (e.g. RGBA8 layout is officially called ABGR8).
  105. enum DDSFormat {
  106. DDS_DXT1,
  107. DDS_DXT3,
  108. DDS_DXT5,
  109. DDS_ATI1,
  110. DDS_ATI2,
  111. DDS_BC6U,
  112. DDS_BC6S,
  113. DDS_BC7,
  114. DDS_R16F,
  115. DDS_RG16F,
  116. DDS_RGBA16F,
  117. DDS_R32F,
  118. DDS_RG32F,
  119. DDS_RGB32F,
  120. DDS_RGBA32F,
  121. DDS_RGB9E5,
  122. DDS_RGB8,
  123. DDS_RGBA8,
  124. DDS_BGR8,
  125. DDS_BGRA8,
  126. DDS_BGR5A1,
  127. DDS_BGR565,
  128. DDS_B2GR3,
  129. DDS_B2GR3A8,
  130. DDS_BGR10A2,
  131. DDS_RGB10A2,
  132. DDS_BGRA4,
  133. DDS_LUMINANCE,
  134. DDS_LUMINANCE_ALPHA,
  135. DDS_LUMINANCE_ALPHA_4,
  136. DDS_MAX
  137. };
  138. enum DDSType {
  139. DDST_2D = 1,
  140. DDST_CUBEMAP,
  141. DDST_3D,
  142. DDST_TYPE_MASK = 0x7F,
  143. DDST_ARRAY = 0x80,
  144. };
  145. struct DDSFormatInfo {
  146. const char *name = nullptr;
  147. bool compressed = false;
  148. uint32_t divisor = 0;
  149. uint32_t block_size = 0;
  150. Image::Format format = Image::Format::FORMAT_BPTC_RGBA;
  151. };
  152. static const DDSFormatInfo dds_format_info[DDS_MAX] = {
  153. { "DXT1/BC1", true, 4, 8, Image::FORMAT_DXT1 },
  154. { "DXT2/DXT3/BC2", true, 4, 16, Image::FORMAT_DXT3 },
  155. { "DXT4/DXT5/BC3", true, 4, 16, Image::FORMAT_DXT5 },
  156. { "ATI1/BC4", true, 4, 8, Image::FORMAT_RGTC_R },
  157. { "ATI2/A2XY/BC5", true, 4, 16, Image::FORMAT_RGTC_RG },
  158. { "BC6UF", true, 4, 16, Image::FORMAT_BPTC_RGBFU },
  159. { "BC6SF", true, 4, 16, Image::FORMAT_BPTC_RGBF },
  160. { "BC7", true, 4, 16, Image::FORMAT_BPTC_RGBA },
  161. { "R16F", false, 1, 2, Image::FORMAT_RH },
  162. { "RG16F", false, 1, 4, Image::FORMAT_RGH },
  163. { "RGBA16F", false, 1, 8, Image::FORMAT_RGBAH },
  164. { "R32F", false, 1, 4, Image::FORMAT_RF },
  165. { "RG32F", false, 1, 8, Image::FORMAT_RGF },
  166. { "RGB32F", false, 1, 12, Image::FORMAT_RGBF },
  167. { "RGBA32F", false, 1, 16, Image::FORMAT_RGBAF },
  168. { "RGB9E5", false, 1, 4, Image::FORMAT_RGBE9995 },
  169. { "RGB8", false, 1, 3, Image::FORMAT_RGB8 },
  170. { "RGBA8", false, 1, 4, Image::FORMAT_RGBA8 },
  171. { "BGR8", false, 1, 3, Image::FORMAT_RGB8 },
  172. { "BGRA8", false, 1, 4, Image::FORMAT_RGBA8 },
  173. { "BGR5A1", false, 1, 2, Image::FORMAT_RGBA8 },
  174. { "BGR565", false, 1, 2, Image::FORMAT_RGB8 },
  175. { "B2GR3", false, 1, 1, Image::FORMAT_RGB8 },
  176. { "B2GR3A8", false, 1, 2, Image::FORMAT_RGBA8 },
  177. { "BGR10A2", false, 1, 4, Image::FORMAT_RGBA8 },
  178. { "RGB10A2", false, 1, 4, Image::FORMAT_RGBA8 },
  179. { "BGRA4", false, 1, 2, Image::FORMAT_RGBA8 },
  180. { "GRAYSCALE", false, 1, 1, Image::FORMAT_L8 },
  181. { "GRAYSCALE_ALPHA", false, 1, 2, Image::FORMAT_LA8 },
  182. { "GRAYSCALE_ALPHA_4", false, 1, 1, Image::FORMAT_LA8 }
  183. };
  184. inline DDSFormat _dxgi_to_dds_format(uint32_t p_dxgi_format) {
  185. switch (p_dxgi_format) {
  186. case DXGI_R32G32B32A32_FLOAT: {
  187. return DDS_RGBA32F;
  188. }
  189. case DXGI_R32G32B32_FLOAT: {
  190. return DDS_RGB32F;
  191. }
  192. case DXGI_R16G16B16A16_FLOAT: {
  193. return DDS_RGBA16F;
  194. }
  195. case DXGI_R32G32_FLOAT: {
  196. return DDS_RG32F;
  197. }
  198. case DXGI_R10G10B10A2_UNORM: {
  199. return DDS_RGB10A2;
  200. }
  201. case DXGI_R8G8B8A8_UNORM:
  202. case DXGI_R8G8B8A8_UNORM_SRGB: {
  203. return DDS_RGBA8;
  204. }
  205. case DXGI_R16G16_FLOAT: {
  206. return DDS_RG16F;
  207. }
  208. case DXGI_R32_FLOAT: {
  209. return DDS_R32F;
  210. }
  211. case DXGI_R8_UNORM:
  212. case DXGI_A8_UNORM: {
  213. return DDS_LUMINANCE;
  214. }
  215. case DXGI_R16_FLOAT: {
  216. return DDS_R16F;
  217. }
  218. case DXGI_R8G8_UNORM: {
  219. return DDS_LUMINANCE_ALPHA;
  220. }
  221. case DXGI_R9G9B9E5: {
  222. return DDS_RGB9E5;
  223. }
  224. case DXGI_BC1_UNORM:
  225. case DXGI_BC1_UNORM_SRGB: {
  226. return DDS_DXT1;
  227. }
  228. case DXGI_BC2_UNORM:
  229. case DXGI_BC2_UNORM_SRGB: {
  230. return DDS_DXT3;
  231. }
  232. case DXGI_BC3_UNORM:
  233. case DXGI_BC3_UNORM_SRGB: {
  234. return DDS_DXT5;
  235. }
  236. case DXGI_BC4_UNORM: {
  237. return DDS_ATI1;
  238. }
  239. case DXGI_BC5_UNORM: {
  240. return DDS_ATI2;
  241. }
  242. case DXGI_B5G6R5_UNORM: {
  243. return DDS_BGR565;
  244. }
  245. case DXGI_B5G5R5A1_UNORM: {
  246. return DDS_BGR5A1;
  247. }
  248. case DXGI_B8G8R8A8_UNORM: {
  249. return DDS_BGRA8;
  250. }
  251. case DXGI_BC6H_UF16: {
  252. return DDS_BC6U;
  253. }
  254. case DXGI_BC6H_SF16: {
  255. return DDS_BC6S;
  256. }
  257. case DXGI_BC7_UNORM:
  258. case DXGI_BC7_UNORM_SRGB: {
  259. return DDS_BC7;
  260. }
  261. case DXGI_B4G4R4A4_UNORM: {
  262. return DDS_BGRA4;
  263. }
  264. default: {
  265. return DDS_MAX;
  266. }
  267. }
  268. }
  269. static Ref<Image> _dds_load_layer(Ref<FileAccess> p_file, DDSFormat p_dds_format, uint32_t p_width, uint32_t p_height, uint32_t p_mipmaps, uint32_t p_pitch, uint32_t p_flags, Vector<uint8_t> &r_src_data) {
  270. const DDSFormatInfo &info = dds_format_info[p_dds_format];
  271. uint32_t w = p_width;
  272. uint32_t h = p_height;
  273. if (info.compressed) {
  274. // BC compressed.
  275. w += w % info.divisor;
  276. h += h % info.divisor;
  277. if (w != p_width) {
  278. WARN_PRINT(vformat("%s: DDS width '%d' is not divisible by %d. This is not allowed as per the DDS specification, attempting to load anyway.", p_file->get_path(), p_width, info.divisor));
  279. }
  280. if (h != p_height) {
  281. WARN_PRINT(vformat("%s: DDS height '%d' is not divisible by %d. This is not allowed as per the DDS specification, attempting to load anyway.", p_file->get_path(), p_height, info.divisor));
  282. }
  283. uint32_t size = MAX(info.divisor, w) / info.divisor * MAX(info.divisor, h) / info.divisor * info.block_size;
  284. if (p_flags & DDSD_LINEARSIZE) {
  285. ERR_FAIL_COND_V_MSG(size != p_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.");
  286. } else {
  287. ERR_FAIL_COND_V_MSG(p_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.");
  288. }
  289. for (uint32_t i = 1; i < p_mipmaps; i++) {
  290. w = MAX(1u, w >> 1);
  291. h = MAX(1u, h >> 1);
  292. uint32_t bsize = MAX(info.divisor, w) / info.divisor * MAX(info.divisor, h) / info.divisor * info.block_size;
  293. size += bsize;
  294. }
  295. r_src_data.resize(size);
  296. uint8_t *wb = r_src_data.ptrw();
  297. p_file->get_buffer(wb, size);
  298. } else {
  299. // Generic uncompressed.
  300. uint32_t size = p_width * p_height * info.block_size;
  301. for (uint32_t i = 1; i < p_mipmaps; i++) {
  302. w = (w + 1) >> 1;
  303. h = (h + 1) >> 1;
  304. size += w * h * info.block_size;
  305. }
  306. // Calculate the space these formats will take up after decoding.
  307. switch (p_dds_format) {
  308. case DDS_BGR565:
  309. size = size * 3 / 2;
  310. break;
  311. case DDS_BGR5A1:
  312. case DDS_BGRA4:
  313. case DDS_B2GR3A8:
  314. case DDS_LUMINANCE_ALPHA_4:
  315. size = size * 2;
  316. break;
  317. case DDS_B2GR3:
  318. size = size * 3;
  319. break;
  320. default:
  321. break;
  322. }
  323. r_src_data.resize(size);
  324. uint8_t *wb = r_src_data.ptrw();
  325. p_file->get_buffer(wb, size);
  326. switch (p_dds_format) {
  327. case DDS_BGR5A1: {
  328. // To RGBA8.
  329. int colcount = size / 4;
  330. for (int i = colcount - 1; i >= 0; i--) {
  331. int src_ofs = i * 2;
  332. int dst_ofs = i * 4;
  333. uint8_t a = wb[src_ofs + 1] & 0x80;
  334. uint8_t b = wb[src_ofs] & 0x1F;
  335. uint8_t g = (wb[src_ofs] >> 5) | ((wb[src_ofs + 1] & 0x3) << 3);
  336. uint8_t r = (wb[src_ofs + 1] >> 2) & 0x1F;
  337. wb[dst_ofs + 0] = r << 3;
  338. wb[dst_ofs + 1] = g << 3;
  339. wb[dst_ofs + 2] = b << 3;
  340. wb[dst_ofs + 3] = a ? 255 : 0;
  341. }
  342. } break;
  343. case DDS_BGR565: {
  344. // To RGB8.
  345. int colcount = size / 3;
  346. for (int i = colcount - 1; i >= 0; i--) {
  347. int src_ofs = i * 2;
  348. int dst_ofs = i * 3;
  349. uint8_t b = wb[src_ofs] & 0x1F;
  350. uint8_t g = (wb[src_ofs] >> 5) | ((wb[src_ofs + 1] & 0x7) << 3);
  351. uint8_t r = wb[src_ofs + 1] >> 3;
  352. wb[dst_ofs + 0] = r << 3;
  353. wb[dst_ofs + 1] = g << 2;
  354. wb[dst_ofs + 2] = b << 3;
  355. }
  356. } break;
  357. case DDS_BGRA4: {
  358. // To RGBA8.
  359. int colcount = size / 4;
  360. for (int i = colcount - 1; i >= 0; i--) {
  361. int src_ofs = i * 2;
  362. int dst_ofs = i * 4;
  363. uint8_t b = wb[src_ofs] & 0x0F;
  364. uint8_t g = wb[src_ofs] & 0xF0;
  365. uint8_t r = wb[src_ofs + 1] & 0x0F;
  366. uint8_t a = wb[src_ofs + 1] & 0xF0;
  367. wb[dst_ofs] = (r << 4) | r;
  368. wb[dst_ofs + 1] = g | (g >> 4);
  369. wb[dst_ofs + 2] = (b << 4) | b;
  370. wb[dst_ofs + 3] = a | (a >> 4);
  371. }
  372. } break;
  373. case DDS_B2GR3: {
  374. // To RGB8.
  375. int colcount = size / 3;
  376. for (int i = colcount - 1; i >= 0; i--) {
  377. int src_ofs = i;
  378. int dst_ofs = i * 3;
  379. uint8_t b = (wb[src_ofs] & 0x3) << 6;
  380. uint8_t g = (wb[src_ofs] & 0x1C) << 3;
  381. uint8_t r = (wb[src_ofs] & 0xE0);
  382. wb[dst_ofs] = r;
  383. wb[dst_ofs + 1] = g;
  384. wb[dst_ofs + 2] = b;
  385. }
  386. } break;
  387. case DDS_B2GR3A8: {
  388. // To RGBA8.
  389. int colcount = size / 4;
  390. for (int i = colcount - 1; i >= 0; i--) {
  391. int src_ofs = i * 2;
  392. int dst_ofs = i * 4;
  393. uint8_t b = (wb[src_ofs] & 0x3) << 6;
  394. uint8_t g = (wb[src_ofs] & 0x1C) << 3;
  395. uint8_t r = (wb[src_ofs] & 0xE0);
  396. uint8_t a = wb[src_ofs + 1];
  397. wb[dst_ofs] = r;
  398. wb[dst_ofs + 1] = g;
  399. wb[dst_ofs + 2] = b;
  400. wb[dst_ofs + 3] = a;
  401. }
  402. } break;
  403. case DDS_RGB10A2: {
  404. // To RGBA8.
  405. int colcount = size / 4;
  406. for (int i = 0; i < colcount; i++) {
  407. int ofs = i * 4;
  408. 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);
  409. // This method follows the 'standard' way of decoding 10-bit dds files,
  410. // which means the ones created with DirectXTex will be loaded incorrectly.
  411. uint8_t a = (w32 & 0xc0000000) >> 24;
  412. uint8_t r = (w32 & 0x3ff) >> 2;
  413. uint8_t g = (w32 & 0xffc00) >> 12;
  414. uint8_t b = (w32 & 0x3ff00000) >> 22;
  415. wb[ofs + 0] = r;
  416. wb[ofs + 1] = g;
  417. wb[ofs + 2] = b;
  418. wb[ofs + 3] = a == 0xc0 ? 255 : a; // 0xc0 should be opaque.
  419. }
  420. } break;
  421. case DDS_BGR10A2: {
  422. // To RGBA8.
  423. int colcount = size / 4;
  424. for (int i = 0; i < colcount; i++) {
  425. int ofs = i * 4;
  426. 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);
  427. // This method follows the 'standard' way of decoding 10-bit dds files,
  428. // which means the ones created with DirectXTex will be loaded incorrectly.
  429. uint8_t a = (w32 & 0xc0000000) >> 24;
  430. uint8_t r = (w32 & 0x3ff00000) >> 22;
  431. uint8_t g = (w32 & 0xffc00) >> 12;
  432. uint8_t b = (w32 & 0x3ff) >> 2;
  433. wb[ofs + 0] = r;
  434. wb[ofs + 1] = g;
  435. wb[ofs + 2] = b;
  436. wb[ofs + 3] = a == 0xc0 ? 255 : a; // 0xc0 should be opaque.
  437. }
  438. } break;
  439. // Channel-swapped.
  440. case DDS_BGRA8: {
  441. // To RGBA8.
  442. int colcount = size / 4;
  443. for (int i = 0; i < colcount; i++) {
  444. SWAP(wb[i * 4 + 0], wb[i * 4 + 2]);
  445. }
  446. } break;
  447. case DDS_BGR8: {
  448. // To RGB8.
  449. int colcount = size / 3;
  450. for (int i = 0; i < colcount; i++) {
  451. SWAP(wb[i * 3 + 0], wb[i * 3 + 2]);
  452. }
  453. } break;
  454. // Grayscale.
  455. case DDS_LUMINANCE_ALPHA_4: {
  456. // To LA8.
  457. int colcount = size / 2;
  458. for (int i = colcount - 1; i >= 0; i--) {
  459. int src_ofs = i;
  460. int dst_ofs = i * 2;
  461. uint8_t l = wb[src_ofs] & 0x0F;
  462. uint8_t a = wb[src_ofs] & 0xF0;
  463. wb[dst_ofs] = (l << 4) | l;
  464. wb[dst_ofs + 1] = a | (a >> 4);
  465. }
  466. } break;
  467. default: {
  468. }
  469. }
  470. }
  471. return memnew(Image(p_width, p_height, p_mipmaps > 1, info.format, r_src_data));
  472. }
  473. 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) {
  474. if (r_error) {
  475. *r_error = ERR_CANT_OPEN;
  476. }
  477. Error err;
  478. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  479. if (f.is_null()) {
  480. return Ref<Resource>();
  481. }
  482. Ref<FileAccess> fref(f);
  483. if (r_error) {
  484. *r_error = ERR_FILE_CORRUPT;
  485. }
  486. ERR_FAIL_COND_V_MSG(err != OK, Ref<Resource>(), vformat("Unable to open DDS texture file '%s'.", p_path));
  487. uint32_t magic = f->get_32();
  488. uint32_t hsize = f->get_32();
  489. uint32_t flags = f->get_32();
  490. uint32_t height = f->get_32();
  491. uint32_t width = f->get_32();
  492. uint32_t pitch = f->get_32();
  493. uint32_t depth = f->get_32();
  494. uint32_t mipmaps = f->get_32();
  495. // Skip reserved.
  496. for (int i = 0; i < 11; i++) {
  497. f->get_32();
  498. }
  499. // Validate.
  500. // We don't check DDSD_CAPS or DDSD_PIXELFORMAT, as they're mandatory when writing,
  501. // but non-mandatory when reading (as some writers don't set them).
  502. if (magic != DDS_MAGIC || hsize != 124) {
  503. ERR_FAIL_V_MSG(Ref<Resource>(), vformat("Invalid or unsupported DDS texture file '%s'.", p_path));
  504. }
  505. /* uint32_t format_size = */ f->get_32();
  506. uint32_t format_flags = f->get_32();
  507. uint32_t format_fourcc = f->get_32();
  508. uint32_t format_rgb_bits = f->get_32();
  509. uint32_t format_red_mask = f->get_32();
  510. uint32_t format_green_mask = f->get_32();
  511. uint32_t format_blue_mask = f->get_32();
  512. uint32_t format_alpha_mask = f->get_32();
  513. /* uint32_t caps_1 = */ f->get_32();
  514. uint32_t caps_2 = f->get_32();
  515. /* uint32_t caps_3 = */ f->get_32();
  516. /* uint32_t caps_4 = */ f->get_32();
  517. // Skip reserved.
  518. f->get_32();
  519. if (f->get_position() < 128) {
  520. f->seek(128);
  521. }
  522. uint32_t layer_count = 1;
  523. uint32_t dds_type = DDST_2D;
  524. if (caps_2 & DDSC2_CUBEMAP) {
  525. dds_type = DDST_CUBEMAP;
  526. layer_count *= 6;
  527. } else if (caps_2 & DDSC2_VOLUME) {
  528. dds_type = DDST_3D;
  529. layer_count = depth;
  530. }
  531. DDSFormat dds_format = DDS_MAX;
  532. if (format_flags & DDPF_FOURCC) {
  533. // FourCC formats.
  534. switch (format_fourcc) {
  535. case DDFCC_DXT1: {
  536. dds_format = DDS_DXT1;
  537. } break;
  538. case DDFCC_DXT2:
  539. case DDFCC_DXT3: {
  540. dds_format = DDS_DXT3;
  541. } break;
  542. case DDFCC_DXT4:
  543. case DDFCC_DXT5: {
  544. dds_format = DDS_DXT5;
  545. } break;
  546. case DDFCC_ATI1:
  547. case DDFCC_BC4U: {
  548. dds_format = DDS_ATI1;
  549. } break;
  550. case DDFCC_ATI2:
  551. case DDFCC_BC5U:
  552. case DDFCC_A2XY: {
  553. dds_format = DDS_ATI2;
  554. } break;
  555. case DDFCC_R16F: {
  556. dds_format = DDS_R16F;
  557. } break;
  558. case DDFCC_RG16F: {
  559. dds_format = DDS_RG16F;
  560. } break;
  561. case DDFCC_RGBA16F: {
  562. dds_format = DDS_RGBA16F;
  563. } break;
  564. case DDFCC_R32F: {
  565. dds_format = DDS_R32F;
  566. } break;
  567. case DDFCC_RG32F: {
  568. dds_format = DDS_RG32F;
  569. } break;
  570. case DDFCC_RGBA32F: {
  571. dds_format = DDS_RGBA32F;
  572. } break;
  573. case DDFCC_DX10: {
  574. uint32_t dxgi_format = f->get_32();
  575. uint32_t dimension = f->get_32();
  576. /* uint32_t misc_flags_1 = */ f->get_32();
  577. uint32_t array_size = f->get_32();
  578. /* uint32_t misc_flags_2 = */ f->get_32();
  579. if (dimension == DX10D_3D) {
  580. dds_type = DDST_3D;
  581. layer_count = depth;
  582. }
  583. if (array_size > 1) {
  584. layer_count *= array_size;
  585. dds_type |= DDST_ARRAY;
  586. }
  587. dds_format = _dxgi_to_dds_format(dxgi_format);
  588. } break;
  589. default: {
  590. ERR_FAIL_V_MSG(Ref<Resource>(), vformat("Unrecognized or unsupported FourCC in DDS '%s'.", p_path));
  591. }
  592. }
  593. } else if (format_flags & DDPF_RGB) {
  594. // Channel-bitmasked formats.
  595. if (format_flags & DDPF_ALPHAPIXELS) {
  596. // With alpha.
  597. if (format_rgb_bits == 32 && format_red_mask == 0xff0000 && format_green_mask == 0xff00 && format_blue_mask == 0xff && format_alpha_mask == 0xff000000) {
  598. dds_format = DDS_BGRA8;
  599. } else if (format_rgb_bits == 32 && format_red_mask == 0xff && format_green_mask == 0xff00 && format_blue_mask == 0xff0000 && format_alpha_mask == 0xff000000) {
  600. dds_format = DDS_RGBA8;
  601. } else if (format_rgb_bits == 16 && format_red_mask == 0x00007c00 && format_green_mask == 0x000003e0 && format_blue_mask == 0x0000001f && format_alpha_mask == 0x00008000) {
  602. dds_format = DDS_BGR5A1;
  603. } else if (format_rgb_bits == 32 && format_red_mask == 0x3ff00000 && format_green_mask == 0xffc00 && format_blue_mask == 0x3ff && format_alpha_mask == 0xc0000000) {
  604. dds_format = DDS_BGR10A2;
  605. } else if (format_rgb_bits == 32 && format_red_mask == 0x3ff && format_green_mask == 0xffc00 && format_blue_mask == 0x3ff00000 && format_alpha_mask == 0xc0000000) {
  606. dds_format = DDS_RGB10A2;
  607. } else if (format_rgb_bits == 16 && format_red_mask == 0xf00 && format_green_mask == 0xf0 && format_blue_mask == 0xf && format_alpha_mask == 0xf000) {
  608. dds_format = DDS_BGRA4;
  609. } else if (format_rgb_bits == 16 && format_red_mask == 0xe0 && format_green_mask == 0x1c && format_blue_mask == 0x3 && format_alpha_mask == 0xff00) {
  610. dds_format = DDS_B2GR3A8;
  611. }
  612. } else {
  613. // Without alpha.
  614. if (format_rgb_bits == 24 && format_red_mask == 0xff0000 && format_green_mask == 0xff00 && format_blue_mask == 0xff) {
  615. dds_format = DDS_BGR8;
  616. } else if (format_rgb_bits == 24 && format_red_mask == 0xff && format_green_mask == 0xff00 && format_blue_mask == 0xff0000) {
  617. dds_format = DDS_RGB8;
  618. } else if (format_rgb_bits == 16 && format_red_mask == 0x0000f800 && format_green_mask == 0x000007e0 && format_blue_mask == 0x0000001f) {
  619. dds_format = DDS_BGR565;
  620. } else if (format_rgb_bits == 8 && format_red_mask == 0xe0 && format_green_mask == 0x1c && format_blue_mask == 0x3) {
  621. dds_format = DDS_B2GR3;
  622. }
  623. }
  624. } else {
  625. // Other formats.
  626. if (format_flags & DDPF_ALPHAONLY && format_rgb_bits == 8 && format_alpha_mask == 0xff) {
  627. // Alpha only.
  628. dds_format = DDS_LUMINANCE;
  629. }
  630. }
  631. // Depending on the writer, luminance formats may or may not have the DDPF_RGB or DDPF_LUMINANCE flags defined,
  632. // so we check for these formats after everything else failed.
  633. if (dds_format == DDS_MAX) {
  634. if (format_flags & DDPF_ALPHAPIXELS) {
  635. // With alpha.
  636. if (format_rgb_bits == 16 && format_red_mask == 0xff && format_alpha_mask == 0xff00) {
  637. dds_format = DDS_LUMINANCE_ALPHA;
  638. } else if (format_rgb_bits == 8 && format_red_mask == 0xf && format_alpha_mask == 0xf0) {
  639. dds_format = DDS_LUMINANCE_ALPHA_4;
  640. }
  641. } else {
  642. // Without alpha.
  643. if (format_rgb_bits == 8 && format_red_mask == 0xff) {
  644. dds_format = DDS_LUMINANCE;
  645. }
  646. }
  647. }
  648. // No format detected, error.
  649. if (dds_format == DDS_MAX) {
  650. ERR_FAIL_V_MSG(Ref<Resource>(), vformat("Unrecognized or unsupported color layout in DDS '%s'.", p_path));
  651. }
  652. if (!(flags & DDSD_MIPMAPCOUNT)) {
  653. mipmaps = 1;
  654. }
  655. Vector<uint8_t> src_data;
  656. Vector<Ref<Image>> images;
  657. images.resize(layer_count);
  658. for (uint32_t i = 0; i < layer_count; i++) {
  659. images.write[i] = _dds_load_layer(f, dds_format, width, height, mipmaps, pitch, flags, src_data);
  660. }
  661. if ((dds_type & DDST_TYPE_MASK) == DDST_2D) {
  662. if (dds_type & DDST_ARRAY) {
  663. Ref<Texture2DArray> texture = memnew(Texture2DArray());
  664. texture->create_from_images(images);
  665. if (r_error) {
  666. *r_error = OK;
  667. }
  668. return texture;
  669. } else {
  670. if (r_error) {
  671. *r_error = OK;
  672. }
  673. return ImageTexture::create_from_image(images[0]);
  674. }
  675. } else if ((dds_type & DDST_TYPE_MASK) == DDST_CUBEMAP) {
  676. ERR_FAIL_COND_V(layer_count % 6 != 0, Ref<Resource>());
  677. if (dds_type & DDST_ARRAY) {
  678. Ref<CubemapArray> texture = memnew(CubemapArray());
  679. texture->create_from_images(images);
  680. if (r_error) {
  681. *r_error = OK;
  682. }
  683. return texture;
  684. } else {
  685. Ref<Cubemap> texture = memnew(Cubemap());
  686. texture->create_from_images(images);
  687. if (r_error) {
  688. *r_error = OK;
  689. }
  690. return texture;
  691. }
  692. } else if ((dds_type & DDST_TYPE_MASK) == DDST_3D) {
  693. Ref<ImageTexture3D> texture = memnew(ImageTexture3D());
  694. texture->create(images[0]->get_format(), width, height, layer_count, mipmaps > 1, images);
  695. if (r_error) {
  696. *r_error = OK;
  697. }
  698. return texture;
  699. }
  700. return Ref<Resource>();
  701. }
  702. void ResourceFormatDDS::get_recognized_extensions(List<String> *p_extensions) const {
  703. p_extensions->push_back("dds");
  704. }
  705. bool ResourceFormatDDS::handles_type(const String &p_type) const {
  706. return ClassDB::is_parent_class(p_type, "Texture");
  707. }
  708. String ResourceFormatDDS::get_resource_type(const String &p_path) const {
  709. if (p_path.get_extension().to_lower() == "dds") {
  710. return "Texture";
  711. }
  712. return "";
  713. }