texture_loader_dds.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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 "dds_enums.h"
  32. #include "core/io/file_access.h"
  33. #include "core/io/file_access_memory.h"
  34. #include "scene/resources/image_texture.h"
  35. DDSFormat _dxgi_to_dds_format(uint32_t p_dxgi_format) {
  36. switch (p_dxgi_format) {
  37. case DXGI_R32G32B32A32_FLOAT: {
  38. return DDS_RGBA32F;
  39. }
  40. case DXGI_R32G32B32_FLOAT: {
  41. return DDS_RGB32F;
  42. }
  43. case DXGI_R16G16B16A16_FLOAT: {
  44. return DDS_RGBA16F;
  45. }
  46. case DXGI_R16G16B16A16_UNORM: {
  47. return DDS_RGBA16;
  48. }
  49. case DXGI_R16G16B16A16_UINT: {
  50. return DDS_RGBA16I;
  51. }
  52. case DXGI_R32G32_FLOAT: {
  53. return DDS_RG32F;
  54. }
  55. case DXGI_R10G10B10A2_UNORM: {
  56. return DDS_RGB10A2;
  57. }
  58. case DXGI_R8G8B8A8_UNORM:
  59. case DXGI_R8G8B8A8_UNORM_SRGB: {
  60. return DDS_RGBA8;
  61. }
  62. case DXGI_R16G16_FLOAT: {
  63. return DDS_RG16F;
  64. }
  65. case DXGI_R16G16_UNORM: {
  66. return DDS_RG16;
  67. }
  68. case DXGI_R16G16_UINT: {
  69. return DDS_RG16I;
  70. }
  71. case DXGI_R32_FLOAT: {
  72. return DDS_R32F;
  73. }
  74. case DXGI_R8_UNORM:
  75. case DXGI_A8_UNORM: {
  76. return DDS_LUMINANCE;
  77. }
  78. case DXGI_R16_FLOAT: {
  79. return DDS_R16F;
  80. }
  81. case DXGI_R16_UNORM: {
  82. return DDS_R16;
  83. }
  84. case DXGI_R16_UINT: {
  85. return DDS_R16I;
  86. }
  87. case DXGI_R8G8_UNORM: {
  88. return DDS_LUMINANCE_ALPHA;
  89. }
  90. case DXGI_R9G9B9E5: {
  91. return DDS_RGB9E5;
  92. }
  93. case DXGI_BC1_UNORM:
  94. case DXGI_BC1_UNORM_SRGB: {
  95. return DDS_DXT1;
  96. }
  97. case DXGI_BC2_UNORM:
  98. case DXGI_BC2_UNORM_SRGB: {
  99. return DDS_DXT3;
  100. }
  101. case DXGI_BC3_UNORM:
  102. case DXGI_BC3_UNORM_SRGB: {
  103. return DDS_DXT5;
  104. }
  105. case DXGI_BC4_UNORM: {
  106. return DDS_ATI1;
  107. }
  108. case DXGI_BC5_UNORM: {
  109. return DDS_ATI2;
  110. }
  111. case DXGI_B5G6R5_UNORM: {
  112. return DDS_BGR565;
  113. }
  114. case DXGI_B5G5R5A1_UNORM: {
  115. return DDS_BGR5A1;
  116. }
  117. case DXGI_B8G8R8A8_UNORM: {
  118. return DDS_BGRA8;
  119. }
  120. case DXGI_BC6H_UF16: {
  121. return DDS_BC6U;
  122. }
  123. case DXGI_BC6H_SF16: {
  124. return DDS_BC6S;
  125. }
  126. case DXGI_BC7_UNORM:
  127. case DXGI_BC7_UNORM_SRGB: {
  128. return DDS_BC7;
  129. }
  130. case DXGI_B4G4R4A4_UNORM: {
  131. return DDS_BGRA4;
  132. }
  133. default: {
  134. return DDS_MAX;
  135. }
  136. }
  137. }
  138. 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) {
  139. const DDSFormatInfo &info = dds_format_info[p_dds_format];
  140. uint32_t w = p_width;
  141. uint32_t h = p_height;
  142. if (info.compressed) {
  143. // BC compressed.
  144. w += w % info.divisor;
  145. h += h % info.divisor;
  146. if (w != p_width) {
  147. 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));
  148. }
  149. if (h != p_height) {
  150. 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));
  151. }
  152. uint32_t size = MAX(1u, (w + 3) / 4) * MAX(1u, (h + 3) / 4) * info.block_size;
  153. if (p_flags & DDSD_LINEARSIZE) {
  154. 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.");
  155. } else {
  156. 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.");
  157. }
  158. for (uint32_t i = 1; i < p_mipmaps; i++) {
  159. w = MAX(1u, w >> 1);
  160. h = MAX(1u, h >> 1);
  161. uint32_t bsize = MAX(1u, (w + 3) / 4) * MAX(1u, (h + 3) / 4) * info.block_size;
  162. size += bsize;
  163. }
  164. r_src_data.resize(size);
  165. uint8_t *wb = r_src_data.ptrw();
  166. p_file->get_buffer(wb, size);
  167. } else {
  168. // Generic uncompressed.
  169. uint32_t size = p_width * p_height * info.block_size;
  170. for (uint32_t i = 1; i < p_mipmaps; i++) {
  171. w = MAX(1u, w >> 1);
  172. h = MAX(1u, h >> 1);
  173. size += w * h * info.block_size;
  174. }
  175. // Calculate the space these formats will take up after decoding.
  176. switch (p_dds_format) {
  177. case DDS_BGR5A1:
  178. case DDS_B2GR3A8:
  179. case DDS_LUMINANCE_ALPHA_4:
  180. size = size * 2;
  181. break;
  182. case DDS_B2GR3:
  183. size = size * 3;
  184. break;
  185. default:
  186. break;
  187. }
  188. r_src_data.resize(size);
  189. uint8_t *wb = r_src_data.ptrw();
  190. p_file->get_buffer(wb, size);
  191. switch (p_dds_format) {
  192. case DDS_BGR5A1: {
  193. // To RGBA8.
  194. int colcount = size / 4;
  195. for (int i = colcount - 1; i >= 0; i--) {
  196. int src_ofs = i * 2;
  197. int dst_ofs = i * 4;
  198. uint8_t a = wb[src_ofs + 1] & 0x80;
  199. uint8_t b = wb[src_ofs] & 0x1F;
  200. uint8_t g = (wb[src_ofs] >> 5) | ((wb[src_ofs + 1] & 0x3) << 3);
  201. uint8_t r = (wb[src_ofs + 1] >> 2) & 0x1F;
  202. wb[dst_ofs + 0] = r << 3;
  203. wb[dst_ofs + 1] = g << 3;
  204. wb[dst_ofs + 2] = b << 3;
  205. wb[dst_ofs + 3] = a ? 255 : 0;
  206. }
  207. } break;
  208. case DDS_BGRA4: {
  209. // To RGBA4.
  210. for (uint32_t i = 0; i < size; i += 2) {
  211. uint8_t ar = wb[i + 0];
  212. uint8_t gb = wb[i + 1];
  213. wb[i + 0] = ((ar & 0x0F) << 4) | ((gb & 0xF0) >> 4);
  214. wb[i + 1] = ((ar & 0xF0) >> 4) | ((gb & 0x0F) << 4);
  215. }
  216. } break;
  217. case DDS_B2GR3: {
  218. // To RGB8.
  219. int colcount = size / 3;
  220. for (int i = colcount - 1; i >= 0; i--) {
  221. int src_ofs = i;
  222. int dst_ofs = i * 3;
  223. uint8_t b = (wb[src_ofs] & 0x3) << 6;
  224. uint8_t g = (wb[src_ofs] & 0x1C) << 3;
  225. uint8_t r = (wb[src_ofs] & 0xE0);
  226. wb[dst_ofs] = r;
  227. wb[dst_ofs + 1] = g;
  228. wb[dst_ofs + 2] = b;
  229. }
  230. } break;
  231. case DDS_B2GR3A8: {
  232. // To RGBA8.
  233. int colcount = size / 4;
  234. for (int i = colcount - 1; i >= 0; i--) {
  235. int src_ofs = i * 2;
  236. int dst_ofs = i * 4;
  237. uint8_t b = (wb[src_ofs] & 0x3) << 6;
  238. uint8_t g = (wb[src_ofs] & 0x1C) << 3;
  239. uint8_t r = (wb[src_ofs] & 0xE0);
  240. uint8_t a = wb[src_ofs + 1];
  241. wb[dst_ofs] = r;
  242. wb[dst_ofs + 1] = g;
  243. wb[dst_ofs + 2] = b;
  244. wb[dst_ofs + 3] = a;
  245. }
  246. } break;
  247. case DDS_RGB10A2: {
  248. // To RGBA8.
  249. int colcount = size / 4;
  250. for (int i = 0; i < colcount; i++) {
  251. int ofs = i * 4;
  252. 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);
  253. // This method follows the 'standard' way of decoding 10-bit dds files,
  254. // which means the ones created with DirectXTex will be loaded incorrectly.
  255. uint8_t a = (w32 & 0xc0000000) >> 24;
  256. uint8_t r = (w32 & 0x3ff) >> 2;
  257. uint8_t g = (w32 & 0xffc00) >> 12;
  258. uint8_t b = (w32 & 0x3ff00000) >> 22;
  259. wb[ofs + 0] = r;
  260. wb[ofs + 1] = g;
  261. wb[ofs + 2] = b;
  262. wb[ofs + 3] = a == 0xc0 ? 255 : a; // 0xc0 should be opaque.
  263. }
  264. } break;
  265. case DDS_BGR10A2: {
  266. // To RGBA8.
  267. int colcount = size / 4;
  268. for (int i = 0; i < colcount; i++) {
  269. int ofs = i * 4;
  270. 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);
  271. // This method follows the 'standard' way of decoding 10-bit dds files,
  272. // which means the ones created with DirectXTex will be loaded incorrectly.
  273. uint8_t a = (w32 & 0xc0000000) >> 24;
  274. uint8_t r = (w32 & 0x3ff00000) >> 22;
  275. uint8_t g = (w32 & 0xffc00) >> 12;
  276. uint8_t b = (w32 & 0x3ff) >> 2;
  277. wb[ofs + 0] = r;
  278. wb[ofs + 1] = g;
  279. wb[ofs + 2] = b;
  280. wb[ofs + 3] = a == 0xc0 ? 255 : a; // 0xc0 should be opaque.
  281. }
  282. } break;
  283. // Channel-swapped.
  284. case DDS_BGRA8: {
  285. // To RGBA8.
  286. int colcount = size / 4;
  287. for (int i = 0; i < colcount; i++) {
  288. SWAP(wb[i * 4 + 0], wb[i * 4 + 2]);
  289. }
  290. } break;
  291. case DDS_BGR8: {
  292. // To RGB8.
  293. int colcount = size / 3;
  294. for (int i = 0; i < colcount; i++) {
  295. SWAP(wb[i * 3 + 0], wb[i * 3 + 2]);
  296. }
  297. } break;
  298. case DDS_RGBX8: {
  299. // To RGB8.
  300. int colcount = size / 4;
  301. for (int i = 0; i < colcount; i++) {
  302. int src_ofs = i * 4;
  303. int dst_ofs = i * 3;
  304. wb[dst_ofs + 0] = wb[src_ofs + 0];
  305. wb[dst_ofs + 1] = wb[src_ofs + 1];
  306. wb[dst_ofs + 2] = wb[src_ofs + 2];
  307. }
  308. r_src_data.resize(size * 3 / 4);
  309. } break;
  310. case DDS_BGRX8: {
  311. // To RGB8.
  312. int colcount = size / 4;
  313. for (int i = 0; i < colcount; i++) {
  314. int src_ofs = i * 4;
  315. int dst_ofs = i * 3;
  316. wb[dst_ofs + 0] = wb[src_ofs + 2];
  317. wb[dst_ofs + 1] = wb[src_ofs + 1];
  318. wb[dst_ofs + 2] = wb[src_ofs + 0];
  319. }
  320. r_src_data.resize(size * 3 / 4);
  321. } break;
  322. // Grayscale.
  323. case DDS_LUMINANCE_ALPHA_4: {
  324. // To LA8.
  325. int colcount = size / 2;
  326. for (int i = colcount - 1; i >= 0; i--) {
  327. int src_ofs = i;
  328. int dst_ofs = i * 2;
  329. uint8_t l = wb[src_ofs] & 0x0F;
  330. uint8_t a = wb[src_ofs] & 0xF0;
  331. wb[dst_ofs] = (l << 4) | l;
  332. wb[dst_ofs + 1] = a | (a >> 4);
  333. }
  334. } break;
  335. default: {
  336. }
  337. }
  338. }
  339. return memnew(Image(p_width, p_height, p_mipmaps > 1, info.format, r_src_data));
  340. }
  341. static Vector<Ref<Image>> _dds_load_images(Ref<FileAccess> p_f, DDSFormat p_dds_format, uint32_t p_width, uint32_t p_height, uint32_t p_mipmaps, uint32_t p_pitch, uint32_t p_flags, uint32_t p_layer_count) {
  342. Vector<uint8_t> src_data;
  343. Vector<Ref<Image>> images;
  344. images.resize(p_layer_count);
  345. for (uint32_t i = 0; i < p_layer_count; i++) {
  346. images.write[i] = _dds_load_layer(p_f, p_dds_format, p_width, p_height, p_mipmaps, p_pitch, p_flags, src_data);
  347. ERR_FAIL_COND_V(images.write[i].is_null(), Vector<Ref<Image>>());
  348. }
  349. return images;
  350. }
  351. static Ref<Resource> _dds_create_texture(const Vector<Ref<Image>> &p_images, uint32_t p_dds_type, uint32_t p_width, uint32_t p_height, uint32_t p_layer_count, uint32_t p_mipmaps, Error *r_error) {
  352. ERR_FAIL_COND_V(p_images.is_empty(), Ref<Resource>());
  353. if ((p_dds_type & DDST_TYPE_MASK) == DDST_2D) {
  354. if (p_dds_type & DDST_ARRAY) {
  355. Ref<Texture2DArray> texture;
  356. texture.instantiate();
  357. texture->create_from_images(p_images);
  358. if (r_error) {
  359. *r_error = OK;
  360. }
  361. return texture;
  362. } else {
  363. if (r_error) {
  364. *r_error = OK;
  365. }
  366. return ImageTexture::create_from_image(p_images[0]);
  367. }
  368. } else if ((p_dds_type & DDST_TYPE_MASK) == DDST_CUBEMAP) {
  369. ERR_FAIL_COND_V(p_layer_count % 6 != 0, Ref<Resource>());
  370. if (p_dds_type & DDST_ARRAY) {
  371. Ref<CubemapArray> texture;
  372. texture.instantiate();
  373. texture->create_from_images(p_images);
  374. if (r_error) {
  375. *r_error = OK;
  376. }
  377. return texture;
  378. } else {
  379. Ref<Cubemap> texture;
  380. texture.instantiate();
  381. texture->create_from_images(p_images);
  382. if (r_error) {
  383. *r_error = OK;
  384. }
  385. return texture;
  386. }
  387. } else if ((p_dds_type & DDST_TYPE_MASK) == DDST_3D) {
  388. Ref<ImageTexture3D> texture;
  389. texture.instantiate();
  390. texture->create(p_images[0]->get_format(), p_width, p_height, p_layer_count, p_mipmaps > 1, p_images);
  391. if (r_error) {
  392. *r_error = OK;
  393. }
  394. return texture;
  395. }
  396. return Ref<Resource>();
  397. }
  398. static Ref<Resource> _dds_create_texture_from_images(const Vector<Ref<Image>> &p_images, DDSFormat p_dds_format, uint32_t p_width, uint32_t p_height, uint32_t p_mipmaps, uint32_t p_pitch, uint32_t p_flags, uint32_t p_layer_count, uint32_t p_dds_type, Error *r_error) {
  399. return _dds_create_texture(p_images, p_dds_type, p_width, p_height, p_layer_count, p_mipmaps, r_error);
  400. }
  401. static Vector<Ref<Image>> _dds_load_images_from_buffer(Ref<FileAccess> p_f, DDSFormat &r_dds_format, uint32_t &r_width, uint32_t &r_height, uint32_t &r_mipmaps, uint32_t &r_pitch, uint32_t &r_flags, uint32_t &r_layer_count, uint32_t &r_dds_type, const String &p_path = "") {
  402. ERR_FAIL_COND_V_MSG(p_f.is_null(), Vector<Ref<Image>>(), vformat("Empty DDS texture file."));
  403. ERR_FAIL_COND_V_MSG(!p_f->get_length(), Vector<Ref<Image>>(), vformat("Empty DDS texture file."));
  404. uint32_t magic = p_f->get_32();
  405. uint32_t hsize = p_f->get_32();
  406. r_flags = p_f->get_32();
  407. r_height = p_f->get_32();
  408. r_width = p_f->get_32();
  409. r_pitch = p_f->get_32();
  410. uint32_t depth = p_f->get_32();
  411. r_mipmaps = p_f->get_32();
  412. // Skip reserved.
  413. for (int i = 0; i < 11; i++) {
  414. p_f->get_32();
  415. }
  416. // Validate.
  417. // We don't check DDSD_CAPS or DDSD_PIXELFORMAT, as they're mandatory when writing,
  418. // but non-mandatory when reading (as some writers don't set them).
  419. if (magic != DDS_MAGIC || hsize != 124) {
  420. ERR_FAIL_V_MSG(Vector<Ref<Image>>(), vformat("Invalid or unsupported DDS texture file '%s'.", p_path));
  421. }
  422. /* uint32_t format_size = */ p_f->get_32();
  423. uint32_t format_flags = p_f->get_32();
  424. uint32_t format_fourcc = p_f->get_32();
  425. uint32_t format_rgb_bits = p_f->get_32();
  426. uint32_t format_red_mask = p_f->get_32();
  427. uint32_t format_green_mask = p_f->get_32();
  428. uint32_t format_blue_mask = p_f->get_32();
  429. uint32_t format_alpha_mask = p_f->get_32();
  430. /* uint32_t caps_1 = */ p_f->get_32();
  431. uint32_t caps_2 = p_f->get_32();
  432. /* uint32_t caps_3 = */ p_f->get_32();
  433. /* uint32_t caps_4 = */ p_f->get_32();
  434. // Skip reserved.
  435. p_f->get_32();
  436. if (p_f->get_position() < 128) {
  437. p_f->seek(128);
  438. }
  439. r_layer_count = 1;
  440. r_dds_type = DDST_2D;
  441. if (caps_2 & DDSC2_CUBEMAP) {
  442. r_dds_type = DDST_CUBEMAP;
  443. r_layer_count *= 6;
  444. } else if (caps_2 & DDSC2_VOLUME) {
  445. r_dds_type = DDST_3D;
  446. r_layer_count = depth;
  447. }
  448. r_dds_format = DDS_MAX;
  449. if (format_flags & DDPF_FOURCC) {
  450. // FourCC formats.
  451. switch (format_fourcc) {
  452. case DDFCC_DXT1: {
  453. r_dds_format = DDS_DXT1;
  454. } break;
  455. case DDFCC_DXT2:
  456. case DDFCC_DXT3: {
  457. r_dds_format = DDS_DXT3;
  458. } break;
  459. case DDFCC_DXT4:
  460. case DDFCC_DXT5: {
  461. r_dds_format = DDS_DXT5;
  462. } break;
  463. case DDFCC_ATI1:
  464. case DDFCC_BC4U: {
  465. r_dds_format = DDS_ATI1;
  466. } break;
  467. case DDFCC_ATI2:
  468. case DDFCC_BC5U:
  469. case DDFCC_A2XY: {
  470. r_dds_format = DDS_ATI2;
  471. } break;
  472. case DDFCC_RGBA16: {
  473. r_dds_format = DDS_RGBA16;
  474. } break;
  475. case DDFCC_R16F: {
  476. r_dds_format = DDS_R16F;
  477. } break;
  478. case DDFCC_RG16F: {
  479. r_dds_format = DDS_RG16F;
  480. } break;
  481. case DDFCC_RGBA16F: {
  482. r_dds_format = DDS_RGBA16F;
  483. } break;
  484. case DDFCC_R32F: {
  485. r_dds_format = DDS_R32F;
  486. } break;
  487. case DDFCC_RG32F: {
  488. r_dds_format = DDS_RG32F;
  489. } break;
  490. case DDFCC_RGBA32F: {
  491. r_dds_format = DDS_RGBA32F;
  492. } break;
  493. case DDFCC_DX10: {
  494. uint32_t dxgi_format = p_f->get_32();
  495. uint32_t dimension = p_f->get_32();
  496. /* uint32_t misc_flags_1 = */ p_f->get_32();
  497. uint32_t array_size = p_f->get_32();
  498. /* uint32_t misc_flags_2 = */ p_f->get_32();
  499. if (dimension == DX10D_3D) {
  500. r_dds_type = DDST_3D;
  501. r_layer_count = depth;
  502. }
  503. if (array_size > 1) {
  504. r_layer_count *= array_size;
  505. r_dds_type |= DDST_ARRAY;
  506. }
  507. r_dds_format = _dxgi_to_dds_format(dxgi_format);
  508. } break;
  509. default: {
  510. ERR_FAIL_V_MSG(Vector<Ref<Image>>(), vformat("Unrecognized or unsupported FourCC in DDS '%s'.", p_path));
  511. }
  512. }
  513. } else if (format_flags & DDPF_RGB) {
  514. // Channel-bitmasked formats.
  515. if (format_flags & DDPF_ALPHAPIXELS) {
  516. // With alpha.
  517. if (format_rgb_bits == 32 && format_red_mask == 0xff0000 && format_green_mask == 0xff00 && format_blue_mask == 0xff && format_alpha_mask == 0xff000000) {
  518. r_dds_format = DDS_BGRA8;
  519. } else if (format_rgb_bits == 32 && format_red_mask == 0xff && format_green_mask == 0xff00 && format_blue_mask == 0xff0000 && format_alpha_mask == 0xff000000) {
  520. r_dds_format = DDS_RGBA8;
  521. } else if (format_rgb_bits == 16 && format_red_mask == 0x00007c00 && format_green_mask == 0x000003e0 && format_blue_mask == 0x0000001f && format_alpha_mask == 0x00008000) {
  522. r_dds_format = DDS_BGR5A1;
  523. } else if (format_rgb_bits == 32 && format_red_mask == 0x3ff00000 && format_green_mask == 0xffc00 && format_blue_mask == 0x3ff && format_alpha_mask == 0xc0000000) {
  524. r_dds_format = DDS_BGR10A2;
  525. } else if (format_rgb_bits == 32 && format_red_mask == 0x3ff && format_green_mask == 0xffc00 && format_blue_mask == 0x3ff00000 && format_alpha_mask == 0xc0000000) {
  526. r_dds_format = DDS_RGB10A2;
  527. } else if (format_rgb_bits == 16 && format_red_mask == 0xf00 && format_green_mask == 0xf0 && format_blue_mask == 0xf && format_alpha_mask == 0xf000) {
  528. r_dds_format = DDS_BGRA4;
  529. } else if (format_rgb_bits == 16 && format_red_mask == 0xe0 && format_green_mask == 0x1c && format_blue_mask == 0x3 && format_alpha_mask == 0xff00) {
  530. r_dds_format = DDS_B2GR3A8;
  531. }
  532. } else {
  533. // Without alpha.
  534. if (format_rgb_bits == 24 && format_red_mask == 0xff0000 && format_green_mask == 0xff00 && format_blue_mask == 0xff) {
  535. r_dds_format = DDS_BGR8;
  536. } else if (format_rgb_bits == 24 && format_red_mask == 0xff && format_green_mask == 0xff00 && format_blue_mask == 0xff0000) {
  537. r_dds_format = DDS_RGB8;
  538. } else if (format_rgb_bits == 16 && format_red_mask == 0x0000f800 && format_green_mask == 0x000007e0 && format_blue_mask == 0x0000001f) {
  539. r_dds_format = DDS_BGR565;
  540. } else if (format_rgb_bits == 8 && format_red_mask == 0xe0 && format_green_mask == 0x1c && format_blue_mask == 0x3) {
  541. r_dds_format = DDS_B2GR3;
  542. } else if (format_rgb_bits == 32 && format_red_mask == 0xff0000 && format_green_mask == 0xff00 && format_blue_mask == 0xff) {
  543. r_dds_format = DDS_BGRX8;
  544. } else if (format_rgb_bits == 32 && format_red_mask == 0xff && format_green_mask == 0xff00 && format_blue_mask == 0xff0000) {
  545. r_dds_format = DDS_RGBX8;
  546. }
  547. }
  548. if (format_rgb_bits == 32 && format_red_mask == 0xffff && format_green_mask == 0xffff0000) {
  549. r_dds_format = DDS_RG16;
  550. }
  551. } else {
  552. // Other formats.
  553. if (format_flags & DDPF_ALPHAONLY && format_rgb_bits == 8 && format_alpha_mask == 0xff) {
  554. // Alpha only.
  555. r_dds_format = DDS_LUMINANCE;
  556. }
  557. }
  558. // Depending on the writer, luminance formats may or may not have the DDPF_RGB or DDPF_LUMINANCE flags defined,
  559. // so we check for these formats after everything else failed.
  560. if (r_dds_format == DDS_MAX) {
  561. if (format_flags & DDPF_ALPHAPIXELS) {
  562. // With alpha.
  563. if (format_rgb_bits == 16 && format_red_mask == 0xff && format_alpha_mask == 0xff00) {
  564. r_dds_format = DDS_LUMINANCE_ALPHA;
  565. } else if (format_rgb_bits == 8 && format_red_mask == 0xf && format_alpha_mask == 0xf0) {
  566. r_dds_format = DDS_LUMINANCE_ALPHA_4;
  567. }
  568. } else {
  569. // Without alpha.
  570. if (format_rgb_bits == 8 && format_red_mask == 0xff) {
  571. r_dds_format = DDS_LUMINANCE;
  572. } else if (format_rgb_bits == 16 && format_red_mask == 0xffff) {
  573. r_dds_format = DDS_R16;
  574. }
  575. }
  576. }
  577. // No format detected, error.
  578. if (r_dds_format == DDS_MAX) {
  579. ERR_FAIL_V_MSG(Vector<Ref<Image>>(), vformat("Unrecognized or unsupported color layout in DDS '%s'.", p_path));
  580. }
  581. if (!(r_flags & DDSD_MIPMAPCOUNT)) {
  582. r_mipmaps = 1;
  583. }
  584. return _dds_load_images(p_f, r_dds_format, r_width, r_height, r_mipmaps, r_pitch, r_flags, r_layer_count);
  585. }
  586. static Ref<Resource> _dds_load_from_buffer(Ref<FileAccess> p_f, Error *r_error, const String &p_path = "") {
  587. if (r_error) {
  588. *r_error = ERR_FILE_CORRUPT;
  589. }
  590. DDSFormat dds_format;
  591. uint32_t width = 0, height = 0, mipmaps = 0, pitch = 0, flags = 0, layer_count = 0, dds_type = 0;
  592. Vector<Ref<Image>> images = _dds_load_images_from_buffer(p_f, dds_format, width, height, mipmaps, pitch, flags, layer_count, dds_type, p_path);
  593. return _dds_create_texture_from_images(images, dds_format, width, height, mipmaps, pitch, flags, layer_count, dds_type, r_error);
  594. }
  595. static Ref<Resource> _dds_load_from_file(const String &p_path, Error *r_error) {
  596. if (r_error) {
  597. *r_error = ERR_CANT_OPEN;
  598. }
  599. Error err;
  600. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  601. if (f.is_null()) {
  602. return Ref<Resource>();
  603. }
  604. return _dds_load_from_buffer(f, r_error, p_path);
  605. }
  606. 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) {
  607. return _dds_load_from_file(p_path, r_error);
  608. }
  609. void ResourceFormatDDS::get_recognized_extensions(List<String> *p_extensions) const {
  610. p_extensions->push_back("dds");
  611. }
  612. bool ResourceFormatDDS::handles_type(const String &p_type) const {
  613. return ClassDB::is_parent_class(p_type, "Texture");
  614. }
  615. String ResourceFormatDDS::get_resource_type(const String &p_path) const {
  616. if (p_path.has_extension("dds")) {
  617. return "Texture";
  618. }
  619. return "";
  620. }
  621. Ref<Image> load_mem_dds(const uint8_t *p_dds, int p_size) {
  622. ERR_FAIL_NULL_V(p_dds, Ref<Image>());
  623. ERR_FAIL_COND_V(!p_size, Ref<Image>());
  624. Ref<FileAccessMemory> memfile;
  625. memfile.instantiate();
  626. Error open_memfile_error = memfile->open_custom(p_dds, p_size);
  627. ERR_FAIL_COND_V_MSG(open_memfile_error, Ref<Image>(), "Could not create memfile for DDS image buffer.");
  628. DDSFormat dds_format;
  629. uint32_t width, height, mipmaps, pitch, flags, layer_count, dds_type;
  630. Vector<Ref<Image>> images = _dds_load_images_from_buffer(memfile, dds_format, width, height, mipmaps, pitch, flags, layer_count, dds_type);
  631. ERR_FAIL_COND_V_MSG(images.is_empty(), Ref<Image>(), "Failed to load DDS image.");
  632. return images[0];
  633. }
  634. ResourceFormatDDS::ResourceFormatDDS() {
  635. Image::_dds_mem_loader_func = load_mem_dds;
  636. }