texture_loader_dds.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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. uint32_t size = MAX(info.divisor, w) / info.divisor * MAX(info.divisor, h) / info.divisor * info.block_size;
  276. if (p_flags & DDSD_LINEARSIZE) {
  277. 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.");
  278. } else {
  279. 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.");
  280. }
  281. for (uint32_t i = 1; i < p_mipmaps; i++) {
  282. w = MAX(1u, w >> 1);
  283. h = MAX(1u, h >> 1);
  284. uint32_t bsize = MAX(info.divisor, w) / info.divisor * MAX(info.divisor, h) / info.divisor * info.block_size;
  285. size += bsize;
  286. }
  287. r_src_data.resize(size);
  288. uint8_t *wb = r_src_data.ptrw();
  289. p_file->get_buffer(wb, size);
  290. } else {
  291. // Generic uncompressed.
  292. uint32_t size = p_width * p_height * info.block_size;
  293. for (uint32_t i = 1; i < p_mipmaps; i++) {
  294. w = (w + 1) >> 1;
  295. h = (h + 1) >> 1;
  296. size += w * h * info.block_size;
  297. }
  298. // Calculate the space these formats will take up after decoding.
  299. switch (p_dds_format) {
  300. case DDS_BGR565:
  301. size = size * 3 / 2;
  302. break;
  303. case DDS_BGR5A1:
  304. case DDS_BGRA4:
  305. case DDS_B2GR3A8:
  306. case DDS_LUMINANCE_ALPHA_4:
  307. size = size * 2;
  308. break;
  309. case DDS_B2GR3:
  310. size = size * 3;
  311. break;
  312. default:
  313. break;
  314. }
  315. r_src_data.resize(size);
  316. uint8_t *wb = r_src_data.ptrw();
  317. p_file->get_buffer(wb, size);
  318. switch (p_dds_format) {
  319. case DDS_BGR5A1: {
  320. // To RGBA8.
  321. int colcount = size / 4;
  322. for (int i = colcount - 1; i >= 0; i--) {
  323. int src_ofs = i * 2;
  324. int dst_ofs = i * 4;
  325. uint8_t a = wb[src_ofs + 1] & 0x80;
  326. uint8_t b = wb[src_ofs] & 0x1F;
  327. uint8_t g = (wb[src_ofs] >> 5) | ((wb[src_ofs + 1] & 0x3) << 3);
  328. uint8_t r = (wb[src_ofs + 1] >> 2) & 0x1F;
  329. wb[dst_ofs + 0] = r << 3;
  330. wb[dst_ofs + 1] = g << 3;
  331. wb[dst_ofs + 2] = b << 3;
  332. wb[dst_ofs + 3] = a ? 255 : 0;
  333. }
  334. } break;
  335. case DDS_BGR565: {
  336. // To RGB8.
  337. int colcount = size / 3;
  338. for (int i = colcount - 1; i >= 0; i--) {
  339. int src_ofs = i * 2;
  340. int dst_ofs = i * 3;
  341. uint8_t b = wb[src_ofs] & 0x1F;
  342. uint8_t g = (wb[src_ofs] >> 5) | ((wb[src_ofs + 1] & 0x7) << 3);
  343. uint8_t r = wb[src_ofs + 1] >> 3;
  344. wb[dst_ofs + 0] = r << 3;
  345. wb[dst_ofs + 1] = g << 2;
  346. wb[dst_ofs + 2] = b << 3;
  347. }
  348. } break;
  349. case DDS_BGRA4: {
  350. // To RGBA8.
  351. int colcount = size / 4;
  352. for (int i = colcount - 1; i >= 0; i--) {
  353. int src_ofs = i * 2;
  354. int dst_ofs = i * 4;
  355. uint8_t b = wb[src_ofs] & 0x0F;
  356. uint8_t g = wb[src_ofs] & 0xF0;
  357. uint8_t r = wb[src_ofs + 1] & 0x0F;
  358. uint8_t a = wb[src_ofs + 1] & 0xF0;
  359. wb[dst_ofs] = (r << 4) | r;
  360. wb[dst_ofs + 1] = g | (g >> 4);
  361. wb[dst_ofs + 2] = (b << 4) | b;
  362. wb[dst_ofs + 3] = a | (a >> 4);
  363. }
  364. } break;
  365. case DDS_B2GR3: {
  366. // To RGB8.
  367. int colcount = size / 3;
  368. for (int i = colcount - 1; i >= 0; i--) {
  369. int src_ofs = i;
  370. int dst_ofs = i * 3;
  371. uint8_t b = (wb[src_ofs] & 0x3) << 6;
  372. uint8_t g = (wb[src_ofs] & 0x1C) << 3;
  373. uint8_t r = (wb[src_ofs] & 0xE0);
  374. wb[dst_ofs] = r;
  375. wb[dst_ofs + 1] = g;
  376. wb[dst_ofs + 2] = b;
  377. }
  378. } break;
  379. case DDS_B2GR3A8: {
  380. // To RGBA8.
  381. int colcount = size / 4;
  382. for (int i = colcount - 1; i >= 0; i--) {
  383. int src_ofs = i * 2;
  384. int dst_ofs = i * 4;
  385. uint8_t b = (wb[src_ofs] & 0x3) << 6;
  386. uint8_t g = (wb[src_ofs] & 0x1C) << 3;
  387. uint8_t r = (wb[src_ofs] & 0xE0);
  388. uint8_t a = wb[src_ofs + 1];
  389. wb[dst_ofs] = r;
  390. wb[dst_ofs + 1] = g;
  391. wb[dst_ofs + 2] = b;
  392. wb[dst_ofs + 3] = a;
  393. }
  394. } break;
  395. case DDS_RGB10A2: {
  396. // To RGBA8.
  397. int colcount = size / 4;
  398. for (int i = 0; i < colcount; i++) {
  399. int ofs = i * 4;
  400. 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);
  401. // This method follows the 'standard' way of decoding 10-bit dds files,
  402. // which means the ones created with DirectXTex will be loaded incorrectly.
  403. uint8_t a = (w32 & 0xc0000000) >> 24;
  404. uint8_t r = (w32 & 0x3ff) >> 2;
  405. uint8_t g = (w32 & 0xffc00) >> 12;
  406. uint8_t b = (w32 & 0x3ff00000) >> 22;
  407. wb[ofs + 0] = r;
  408. wb[ofs + 1] = g;
  409. wb[ofs + 2] = b;
  410. wb[ofs + 3] = a == 0xc0 ? 255 : a; // 0xc0 should be opaque.
  411. }
  412. } break;
  413. case DDS_BGR10A2: {
  414. // To RGBA8.
  415. int colcount = size / 4;
  416. for (int i = 0; i < colcount; i++) {
  417. int ofs = i * 4;
  418. 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);
  419. // This method follows the 'standard' way of decoding 10-bit dds files,
  420. // which means the ones created with DirectXTex will be loaded incorrectly.
  421. uint8_t a = (w32 & 0xc0000000) >> 24;
  422. uint8_t r = (w32 & 0x3ff00000) >> 22;
  423. uint8_t g = (w32 & 0xffc00) >> 12;
  424. uint8_t b = (w32 & 0x3ff) >> 2;
  425. wb[ofs + 0] = r;
  426. wb[ofs + 1] = g;
  427. wb[ofs + 2] = b;
  428. wb[ofs + 3] = a == 0xc0 ? 255 : a; // 0xc0 should be opaque.
  429. }
  430. } break;
  431. // Channel-swapped.
  432. case DDS_BGRA8: {
  433. // To RGBA8.
  434. int colcount = size / 4;
  435. for (int i = 0; i < colcount; i++) {
  436. SWAP(wb[i * 4 + 0], wb[i * 4 + 2]);
  437. }
  438. } break;
  439. case DDS_BGR8: {
  440. // To RGB8.
  441. int colcount = size / 3;
  442. for (int i = 0; i < colcount; i++) {
  443. SWAP(wb[i * 3 + 0], wb[i * 3 + 2]);
  444. }
  445. } break;
  446. // Grayscale.
  447. case DDS_LUMINANCE_ALPHA_4: {
  448. // To LA8.
  449. int colcount = size / 2;
  450. for (int i = colcount - 1; i >= 0; i--) {
  451. int src_ofs = i;
  452. int dst_ofs = i * 2;
  453. uint8_t l = wb[src_ofs] & 0x0F;
  454. uint8_t a = wb[src_ofs] & 0xF0;
  455. wb[dst_ofs] = (l << 4) | l;
  456. wb[dst_ofs + 1] = a | (a >> 4);
  457. }
  458. } break;
  459. default: {
  460. }
  461. }
  462. }
  463. return memnew(Image(p_width, p_height, p_mipmaps > 1, info.format, r_src_data));
  464. }
  465. 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) {
  466. if (r_error) {
  467. *r_error = ERR_CANT_OPEN;
  468. }
  469. Error err;
  470. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  471. if (f.is_null()) {
  472. return Ref<Resource>();
  473. }
  474. Ref<FileAccess> fref(f);
  475. if (r_error) {
  476. *r_error = ERR_FILE_CORRUPT;
  477. }
  478. ERR_FAIL_COND_V_MSG(err != OK, Ref<Resource>(), vformat("Unable to open DDS texture file '%s'.", p_path));
  479. uint32_t magic = f->get_32();
  480. uint32_t hsize = f->get_32();
  481. uint32_t flags = f->get_32();
  482. uint32_t height = f->get_32();
  483. uint32_t width = f->get_32();
  484. uint32_t pitch = f->get_32();
  485. uint32_t depth = f->get_32();
  486. uint32_t mipmaps = f->get_32();
  487. // Skip reserved.
  488. for (int i = 0; i < 11; i++) {
  489. f->get_32();
  490. }
  491. // Validate.
  492. // We don't check DDSD_CAPS or DDSD_PIXELFORMAT, as they're mandatory when writing,
  493. // but non-mandatory when reading (as some writers don't set them).
  494. if (magic != DDS_MAGIC || hsize != 124) {
  495. ERR_FAIL_V_MSG(Ref<Resource>(), vformat("Invalid or unsupported DDS texture file '%s'.", p_path));
  496. }
  497. /* uint32_t format_size = */ f->get_32();
  498. uint32_t format_flags = f->get_32();
  499. uint32_t format_fourcc = f->get_32();
  500. uint32_t format_rgb_bits = f->get_32();
  501. uint32_t format_red_mask = f->get_32();
  502. uint32_t format_green_mask = f->get_32();
  503. uint32_t format_blue_mask = f->get_32();
  504. uint32_t format_alpha_mask = f->get_32();
  505. /* uint32_t caps_1 = */ f->get_32();
  506. uint32_t caps_2 = f->get_32();
  507. /* uint32_t caps_3 = */ f->get_32();
  508. /* uint32_t caps_4 = */ f->get_32();
  509. // Skip reserved.
  510. f->get_32();
  511. if (f->get_position() < 128) {
  512. f->seek(128);
  513. }
  514. uint32_t layer_count = 1;
  515. uint32_t dds_type = DDST_2D;
  516. if (caps_2 & DDSC2_CUBEMAP) {
  517. dds_type = DDST_CUBEMAP;
  518. layer_count *= 6;
  519. } else if (caps_2 & DDSC2_VOLUME) {
  520. dds_type = DDST_3D;
  521. layer_count = depth;
  522. }
  523. DDSFormat dds_format = DDS_MAX;
  524. if (format_flags & DDPF_FOURCC) {
  525. // FourCC formats.
  526. switch (format_fourcc) {
  527. case DDFCC_DXT1: {
  528. dds_format = DDS_DXT1;
  529. } break;
  530. case DDFCC_DXT2:
  531. case DDFCC_DXT3: {
  532. dds_format = DDS_DXT3;
  533. } break;
  534. case DDFCC_DXT4:
  535. case DDFCC_DXT5: {
  536. dds_format = DDS_DXT5;
  537. } break;
  538. case DDFCC_ATI1:
  539. case DDFCC_BC4U: {
  540. dds_format = DDS_ATI1;
  541. } break;
  542. case DDFCC_ATI2:
  543. case DDFCC_BC5U:
  544. case DDFCC_A2XY: {
  545. dds_format = DDS_ATI2;
  546. } break;
  547. case DDFCC_R16F: {
  548. dds_format = DDS_R16F;
  549. } break;
  550. case DDFCC_RG16F: {
  551. dds_format = DDS_RG16F;
  552. } break;
  553. case DDFCC_RGBA16F: {
  554. dds_format = DDS_RGBA16F;
  555. } break;
  556. case DDFCC_R32F: {
  557. dds_format = DDS_R32F;
  558. } break;
  559. case DDFCC_RG32F: {
  560. dds_format = DDS_RG32F;
  561. } break;
  562. case DDFCC_RGBA32F: {
  563. dds_format = DDS_RGBA32F;
  564. } break;
  565. case DDFCC_DX10: {
  566. uint32_t dxgi_format = f->get_32();
  567. uint32_t dimension = f->get_32();
  568. /* uint32_t misc_flags_1 = */ f->get_32();
  569. uint32_t array_size = f->get_32();
  570. /* uint32_t misc_flags_2 = */ f->get_32();
  571. if (dimension == DX10D_3D) {
  572. dds_type = DDST_3D;
  573. layer_count = depth;
  574. }
  575. if (array_size > 1) {
  576. layer_count *= array_size;
  577. dds_type |= DDST_ARRAY;
  578. }
  579. dds_format = _dxgi_to_dds_format(dxgi_format);
  580. } break;
  581. default: {
  582. ERR_FAIL_V_MSG(Ref<Resource>(), vformat("Unrecognized or unsupported FourCC in DDS '%s'.", p_path));
  583. }
  584. }
  585. } else if (format_flags & DDPF_RGB) {
  586. // Channel-bitmasked formats.
  587. if (format_flags & DDPF_ALPHAPIXELS) {
  588. // With alpha.
  589. if (format_rgb_bits == 32 && format_red_mask == 0xff0000 && format_green_mask == 0xff00 && format_blue_mask == 0xff && format_alpha_mask == 0xff000000) {
  590. dds_format = DDS_BGRA8;
  591. } else if (format_rgb_bits == 32 && format_red_mask == 0xff && format_green_mask == 0xff00 && format_blue_mask == 0xff0000 && format_alpha_mask == 0xff000000) {
  592. dds_format = DDS_RGBA8;
  593. } else if (format_rgb_bits == 16 && format_red_mask == 0x00007c00 && format_green_mask == 0x000003e0 && format_blue_mask == 0x0000001f && format_alpha_mask == 0x00008000) {
  594. dds_format = DDS_BGR5A1;
  595. } else if (format_rgb_bits == 32 && format_red_mask == 0x3ff00000 && format_green_mask == 0xffc00 && format_blue_mask == 0x3ff && format_alpha_mask == 0xc0000000) {
  596. dds_format = DDS_BGR10A2;
  597. } else if (format_rgb_bits == 32 && format_red_mask == 0x3ff && format_green_mask == 0xffc00 && format_blue_mask == 0x3ff00000 && format_alpha_mask == 0xc0000000) {
  598. dds_format = DDS_RGB10A2;
  599. } else if (format_rgb_bits == 16 && format_red_mask == 0xf00 && format_green_mask == 0xf0 && format_blue_mask == 0xf && format_alpha_mask == 0xf000) {
  600. dds_format = DDS_BGRA4;
  601. } else if (format_rgb_bits == 16 && format_red_mask == 0xe0 && format_green_mask == 0x1c && format_blue_mask == 0x3 && format_alpha_mask == 0xff00) {
  602. dds_format = DDS_B2GR3A8;
  603. }
  604. } else {
  605. // Without alpha.
  606. if (format_rgb_bits == 24 && format_red_mask == 0xff0000 && format_green_mask == 0xff00 && format_blue_mask == 0xff) {
  607. dds_format = DDS_BGR8;
  608. } else if (format_rgb_bits == 24 && format_red_mask == 0xff && format_green_mask == 0xff00 && format_blue_mask == 0xff0000) {
  609. dds_format = DDS_RGB8;
  610. } else if (format_rgb_bits == 16 && format_red_mask == 0x0000f800 && format_green_mask == 0x000007e0 && format_blue_mask == 0x0000001f) {
  611. dds_format = DDS_BGR565;
  612. } else if (format_rgb_bits == 8 && format_red_mask == 0xe0 && format_green_mask == 0x1c && format_blue_mask == 0x3) {
  613. dds_format = DDS_B2GR3;
  614. }
  615. }
  616. } else {
  617. // Other formats.
  618. if (format_flags & DDPF_ALPHAONLY && format_rgb_bits == 8 && format_alpha_mask == 0xff) {
  619. // Alpha only.
  620. dds_format = DDS_LUMINANCE;
  621. }
  622. }
  623. // Depending on the writer, luminance formats may or may not have the DDPF_RGB or DDPF_LUMINANCE flags defined,
  624. // so we check for these formats after everything else failed.
  625. if (dds_format == DDS_MAX) {
  626. if (format_flags & DDPF_ALPHAPIXELS) {
  627. // With alpha.
  628. if (format_rgb_bits == 16 && format_red_mask == 0xff && format_alpha_mask == 0xff00) {
  629. dds_format = DDS_LUMINANCE_ALPHA;
  630. } else if (format_rgb_bits == 8 && format_red_mask == 0xf && format_alpha_mask == 0xf0) {
  631. dds_format = DDS_LUMINANCE_ALPHA_4;
  632. }
  633. } else {
  634. // Without alpha.
  635. if (format_rgb_bits == 8 && format_red_mask == 0xff) {
  636. dds_format = DDS_LUMINANCE;
  637. }
  638. }
  639. }
  640. // No format detected, error.
  641. if (dds_format == DDS_MAX) {
  642. ERR_FAIL_V_MSG(Ref<Resource>(), vformat("Unrecognized or unsupported color layout in DDS '%s'.", p_path));
  643. }
  644. if (!(flags & DDSD_MIPMAPCOUNT)) {
  645. mipmaps = 1;
  646. }
  647. Vector<uint8_t> src_data;
  648. Vector<Ref<Image>> images;
  649. images.resize(layer_count);
  650. for (uint32_t i = 0; i < layer_count; i++) {
  651. images.write[i] = _dds_load_layer(f, dds_format, width, height, mipmaps, pitch, flags, src_data);
  652. }
  653. if ((dds_type & DDST_TYPE_MASK) == DDST_2D) {
  654. if (dds_type & DDST_ARRAY) {
  655. Ref<Texture2DArray> texture = memnew(Texture2DArray());
  656. texture->create_from_images(images);
  657. if (r_error) {
  658. *r_error = OK;
  659. }
  660. return texture;
  661. } else {
  662. if (r_error) {
  663. *r_error = OK;
  664. }
  665. return ImageTexture::create_from_image(images[0]);
  666. }
  667. } else if ((dds_type & DDST_TYPE_MASK) == DDST_CUBEMAP) {
  668. ERR_FAIL_COND_V(layer_count % 6 != 0, Ref<Resource>());
  669. if (dds_type & DDST_ARRAY) {
  670. Ref<CubemapArray> texture = memnew(CubemapArray());
  671. texture->create_from_images(images);
  672. if (r_error) {
  673. *r_error = OK;
  674. }
  675. return texture;
  676. } else {
  677. Ref<Cubemap> texture = memnew(Cubemap());
  678. texture->create_from_images(images);
  679. if (r_error) {
  680. *r_error = OK;
  681. }
  682. return texture;
  683. }
  684. } else if ((dds_type & DDST_TYPE_MASK) == DDST_3D) {
  685. Ref<ImageTexture3D> texture = memnew(ImageTexture3D());
  686. texture->create(images[0]->get_format(), width, height, layer_count, mipmaps > 1, images);
  687. if (r_error) {
  688. *r_error = OK;
  689. }
  690. return texture;
  691. }
  692. return Ref<Resource>();
  693. }
  694. void ResourceFormatDDS::get_recognized_extensions(List<String> *p_extensions) const {
  695. p_extensions->push_back("dds");
  696. }
  697. bool ResourceFormatDDS::handles_type(const String &p_type) const {
  698. return ClassDB::is_parent_class(p_type, "Texture");
  699. }
  700. String ResourceFormatDDS::get_resource_type(const String &p_path) const {
  701. if (p_path.get_extension().to_lower() == "dds") {
  702. return "Texture";
  703. }
  704. return "";
  705. }