image_saver_dds.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /**************************************************************************/
  2. /* image_saver_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 "image_saver_dds.h"
  31. #include "dds_enums.h"
  32. #include "core/io/file_access.h"
  33. #include "core/io/stream_peer.h"
  34. Error save_dds(const String &p_path, const Ref<Image> &p_img) {
  35. Vector<uint8_t> buffer = save_dds_buffer(p_img);
  36. Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE);
  37. if (file.is_null()) {
  38. return ERR_CANT_CREATE;
  39. }
  40. file->store_buffer(buffer.ptr(), buffer.size());
  41. return OK;
  42. }
  43. enum DDSFormatType {
  44. DDFT_BITMASK,
  45. DDFT_FOURCC,
  46. DDFT_DXGI,
  47. };
  48. DDSFormatType _dds_format_get_type(DDSFormat p_format) {
  49. switch (p_format) {
  50. case DDS_DXT1:
  51. case DDS_DXT3:
  52. case DDS_DXT5:
  53. case DDS_ATI1:
  54. case DDS_ATI2:
  55. case DDS_R16F:
  56. case DDS_RG16F:
  57. case DDS_RGBA16F:
  58. case DDS_R32F:
  59. case DDS_RG32F:
  60. case DDS_RGBA32F:
  61. case DDS_RGBA16:
  62. return DDFT_FOURCC;
  63. case DDS_BC6S:
  64. case DDS_BC6U:
  65. case DDS_BC7:
  66. case DDS_RGB9E5:
  67. case DDS_RGB32F:
  68. case DDS_R16I:
  69. case DDS_RG16I:
  70. case DDS_RGBA16I:
  71. return DDFT_DXGI;
  72. default:
  73. return DDFT_BITMASK;
  74. }
  75. }
  76. DDSFormat _image_format_to_dds_format(Image::Format p_image_format) {
  77. switch (p_image_format) {
  78. case Image::FORMAT_RGBAF: {
  79. return DDS_RGBA32F;
  80. }
  81. case Image::FORMAT_RGBF: {
  82. return DDS_RGB32F;
  83. }
  84. case Image::FORMAT_RGBAH: {
  85. return DDS_RGBA16F;
  86. }
  87. case Image::FORMAT_RGF: {
  88. return DDS_RG32F;
  89. }
  90. case Image::FORMAT_RGBA8: {
  91. return DDS_RGBA8;
  92. }
  93. case Image::FORMAT_RGH: {
  94. return DDS_RG16F;
  95. }
  96. case Image::FORMAT_RF: {
  97. return DDS_R32F;
  98. }
  99. case Image::FORMAT_L8:
  100. case Image::FORMAT_R8: {
  101. return DDS_LUMINANCE;
  102. }
  103. case Image::FORMAT_RH: {
  104. return DDS_R16F;
  105. }
  106. case Image::FORMAT_LA8:
  107. case Image::FORMAT_RG8: {
  108. return DDS_LUMINANCE_ALPHA;
  109. }
  110. case Image::FORMAT_RGBA4444: {
  111. return DDS_BGRA4;
  112. }
  113. case Image::FORMAT_RGB565: {
  114. return DDS_BGR565;
  115. }
  116. case Image::FORMAT_RGBE9995: {
  117. return DDS_RGB9E5;
  118. }
  119. case Image::FORMAT_DXT1: {
  120. return DDS_DXT1;
  121. }
  122. case Image::FORMAT_DXT3: {
  123. return DDS_DXT3;
  124. }
  125. case Image::FORMAT_DXT5: {
  126. return DDS_DXT5;
  127. }
  128. case Image::FORMAT_RGTC_R: {
  129. return DDS_ATI1;
  130. }
  131. case Image::FORMAT_RGTC_RG: {
  132. return DDS_ATI2;
  133. }
  134. case Image::FORMAT_RGB8: {
  135. return DDS_RGB8;
  136. }
  137. case Image::FORMAT_BPTC_RGBFU: {
  138. return DDS_BC6U;
  139. }
  140. case Image::FORMAT_BPTC_RGBF: {
  141. return DDS_BC6S;
  142. }
  143. case Image::FORMAT_BPTC_RGBA: {
  144. return DDS_BC7;
  145. }
  146. case Image::FORMAT_R16: {
  147. return DDS_R16;
  148. }
  149. case Image::FORMAT_RG16: {
  150. return DDS_RG16;
  151. }
  152. case Image::FORMAT_RGBA16: {
  153. return DDS_RGBA16;
  154. }
  155. case Image::FORMAT_R16I: {
  156. return DDS_R16I;
  157. }
  158. case Image::FORMAT_RG16I: {
  159. return DDS_RG16I;
  160. }
  161. case Image::FORMAT_RGBA16I: {
  162. return DDS_RGBA16I;
  163. }
  164. default: {
  165. return DDS_MAX;
  166. }
  167. }
  168. }
  169. uint32_t _image_format_to_fourcc_format(Image::Format p_format) {
  170. switch (p_format) {
  171. case Image::FORMAT_DXT1:
  172. return DDFCC_DXT1;
  173. case Image::FORMAT_DXT3:
  174. return DDFCC_DXT3;
  175. case Image::FORMAT_DXT5:
  176. return DDFCC_DXT5;
  177. case Image::FORMAT_RGTC_R:
  178. return DDFCC_ATI1;
  179. case Image::FORMAT_RGTC_RG:
  180. return DDFCC_ATI2;
  181. case Image::FORMAT_RF:
  182. return DDFCC_R32F;
  183. case Image::FORMAT_RGF:
  184. return DDFCC_RG32F;
  185. case Image::FORMAT_RGBAF:
  186. return DDFCC_RGBA32F;
  187. case Image::FORMAT_RH:
  188. return DDFCC_R16F;
  189. case Image::FORMAT_RGH:
  190. return DDFCC_RG16F;
  191. case Image::FORMAT_RGBAH:
  192. return DDFCC_RGBA16F;
  193. case Image::FORMAT_RGBA16:
  194. return DDFCC_RGBA16;
  195. default:
  196. return 0;
  197. }
  198. }
  199. uint32_t _image_format_to_dxgi_format(Image::Format p_format) {
  200. switch (p_format) {
  201. case Image::FORMAT_DXT1:
  202. return DXGI_BC1_UNORM;
  203. case Image::FORMAT_DXT3:
  204. return DXGI_BC2_UNORM;
  205. case Image::FORMAT_DXT5:
  206. return DXGI_BC3_UNORM;
  207. case Image::FORMAT_RGTC_R:
  208. return DXGI_BC4_UNORM;
  209. case Image::FORMAT_RGTC_RG:
  210. return DXGI_BC5_UNORM;
  211. case Image::FORMAT_BPTC_RGBFU:
  212. return DXGI_BC6H_UF16;
  213. case Image::FORMAT_BPTC_RGBF:
  214. return DXGI_BC6H_SF16;
  215. case Image::FORMAT_BPTC_RGBA:
  216. return DXGI_BC7_UNORM;
  217. case Image::FORMAT_RF:
  218. return DXGI_R32_FLOAT;
  219. case Image::FORMAT_RGF:
  220. return DXGI_R32G32_FLOAT;
  221. case Image::FORMAT_RGBF:
  222. return DXGI_R32G32B32_FLOAT;
  223. case Image::FORMAT_RGBAF:
  224. return DXGI_R32G32B32A32_FLOAT;
  225. case Image::FORMAT_RH:
  226. return DXGI_R16_FLOAT;
  227. case Image::FORMAT_RGH:
  228. return DXGI_R16G16_FLOAT;
  229. case Image::FORMAT_RGBAH:
  230. return DXGI_R16G16B16A16_FLOAT;
  231. case Image::FORMAT_RGBE9995:
  232. return DXGI_R9G9B9E5;
  233. case Image::FORMAT_R16:
  234. return DXGI_R16_UNORM;
  235. case Image::FORMAT_RG16:
  236. return DXGI_R16G16_UNORM;
  237. case Image::FORMAT_RGBA16:
  238. return DXGI_R16G16B16A16_UNORM;
  239. case Image::FORMAT_R16I:
  240. return DXGI_R16_UINT;
  241. case Image::FORMAT_RG16I:
  242. return DXGI_R16G16_UINT;
  243. case Image::FORMAT_RGBA16I:
  244. return DXGI_R16G16B16A16_UINT;
  245. default:
  246. return 0;
  247. }
  248. }
  249. void _get_dds_pixel_bitmask(Image::Format p_format, uint32_t &r_bit_count, uint32_t &r_red_mask, uint32_t &r_green_mask, uint32_t &r_blue_mask, uint32_t &r_alpha_mask) {
  250. switch (p_format) {
  251. case Image::FORMAT_R8:
  252. case Image::FORMAT_L8: {
  253. r_bit_count = 8;
  254. r_red_mask = 0xff;
  255. r_green_mask = 0;
  256. r_blue_mask = 0;
  257. r_alpha_mask = 0;
  258. } break;
  259. case Image::FORMAT_RG8:
  260. case Image::FORMAT_LA8: {
  261. r_bit_count = 16;
  262. r_red_mask = 0xff;
  263. r_green_mask = 0;
  264. r_blue_mask = 0;
  265. r_alpha_mask = 0xff00;
  266. } break;
  267. case Image::FORMAT_RGB8: {
  268. // BGR8
  269. r_bit_count = 24;
  270. r_red_mask = 0xff0000;
  271. r_green_mask = 0xff00;
  272. r_blue_mask = 0xff;
  273. r_alpha_mask = 0;
  274. } break;
  275. case Image::FORMAT_RGBA8: {
  276. r_bit_count = 32;
  277. r_red_mask = 0xff;
  278. r_green_mask = 0xff00;
  279. r_blue_mask = 0xff0000;
  280. r_alpha_mask = 0xff000000;
  281. } break;
  282. case Image::FORMAT_RGBA4444: {
  283. // BGRA4444
  284. r_bit_count = 16;
  285. r_red_mask = 0xf00;
  286. r_green_mask = 0xf0;
  287. r_blue_mask = 0xf;
  288. r_alpha_mask = 0xf000;
  289. } break;
  290. case Image::FORMAT_RGB565: {
  291. // BGR565
  292. r_bit_count = 16;
  293. r_red_mask = 0xf800;
  294. r_green_mask = 0x7e0;
  295. r_blue_mask = 0x1f;
  296. r_alpha_mask = 0;
  297. } break;
  298. case Image::FORMAT_R16: {
  299. r_bit_count = 16;
  300. r_red_mask = 0xffff;
  301. r_green_mask = 0;
  302. r_blue_mask = 0;
  303. r_alpha_mask = 0;
  304. } break;
  305. case Image::FORMAT_RG16: {
  306. r_bit_count = 32;
  307. r_red_mask = 0xffff;
  308. r_green_mask = 0xffff0000;
  309. r_blue_mask = 0;
  310. r_alpha_mask = 0;
  311. } break;
  312. default: {
  313. r_bit_count = 0;
  314. r_red_mask = 0;
  315. r_green_mask = 0;
  316. r_blue_mask = 0;
  317. r_alpha_mask = 0;
  318. } break;
  319. }
  320. }
  321. Vector<uint8_t> save_dds_buffer(const Ref<Image> &p_img) {
  322. Ref<StreamPeerBuffer> stream_buffer;
  323. stream_buffer.instantiate();
  324. Ref<Image> image = p_img;
  325. stream_buffer->put_32(DDS_MAGIC);
  326. stream_buffer->put_32(DDS_HEADER_SIZE);
  327. uint32_t flags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_LINEARSIZE;
  328. if (image->has_mipmaps()) {
  329. flags |= DDSD_MIPMAPCOUNT;
  330. }
  331. stream_buffer->put_32(flags);
  332. uint32_t height = image->get_height();
  333. stream_buffer->put_32(height);
  334. uint32_t width = image->get_width();
  335. stream_buffer->put_32(width);
  336. DDSFormat dds_format = _image_format_to_dds_format(image->get_format());
  337. const DDSFormatInfo &info = dds_format_info[dds_format];
  338. uint32_t depth = 1; // Default depth for 2D textures
  339. uint32_t pitch;
  340. if (info.compressed) {
  341. pitch = ((MAX(info.divisor, width) + info.divisor - 1) / info.divisor) * ((MAX(info.divisor, height) + info.divisor - 1) / info.divisor) * info.block_size;
  342. } else {
  343. pitch = width * info.block_size;
  344. }
  345. stream_buffer->put_32(pitch);
  346. stream_buffer->put_32(depth);
  347. uint32_t mipmaps = image->get_mipmap_count() + 1;
  348. stream_buffer->put_32(mipmaps);
  349. uint32_t reserved = 0;
  350. for (int i = 0; i < 11; i++) {
  351. stream_buffer->put_32(reserved);
  352. }
  353. stream_buffer->put_32(DDS_PIXELFORMAT_SIZE);
  354. uint32_t pf_flags = 0;
  355. DDSFormatType format_type = _dds_format_get_type(dds_format);
  356. if (format_type == DDFT_BITMASK) {
  357. pf_flags = DDPF_RGB;
  358. if (image->get_format() == Image::FORMAT_LA8 || image->get_format() == Image::FORMAT_RG8 || image->get_format() == Image::FORMAT_RGBA8 || image->get_format() == Image::FORMAT_RGBA4444) {
  359. pf_flags |= DDPF_ALPHAPIXELS;
  360. }
  361. } else {
  362. pf_flags = DDPF_FOURCC;
  363. }
  364. stream_buffer->put_32(pf_flags);
  365. bool needs_pixeldata_swap = false;
  366. if (format_type == DDFT_BITMASK) {
  367. // Uncompressed bitmasked.
  368. stream_buffer->put_32(0); // FourCC
  369. uint32_t bit_count, r_mask, g_mask, b_mask, a_mask;
  370. _get_dds_pixel_bitmask(image->get_format(), bit_count, r_mask, g_mask, b_mask, a_mask);
  371. stream_buffer->put_32(bit_count);
  372. stream_buffer->put_32(r_mask);
  373. stream_buffer->put_32(g_mask);
  374. stream_buffer->put_32(b_mask);
  375. stream_buffer->put_32(a_mask);
  376. if (image->get_format() == Image::FORMAT_RGBA4444 || image->get_format() == Image::FORMAT_RGB8) {
  377. needs_pixeldata_swap = true;
  378. }
  379. } else if (format_type == DDFT_FOURCC) {
  380. // FourCC.
  381. uint32_t fourcc = _image_format_to_fourcc_format(image->get_format());
  382. stream_buffer->put_32(fourcc);
  383. stream_buffer->put_32(0); // Bit count
  384. stream_buffer->put_32(0); // R Bitmask
  385. stream_buffer->put_32(0); // G Bitmask
  386. stream_buffer->put_32(0); // B Bitmask
  387. stream_buffer->put_32(0); // A Bitmask
  388. } else {
  389. // DXGI format and DX10 header.
  390. stream_buffer->put_32(DDFCC_DX10);
  391. stream_buffer->put_32(0); // Bit count
  392. stream_buffer->put_32(0); // R Bitmask
  393. stream_buffer->put_32(0); // G Bitmask
  394. stream_buffer->put_32(0); // B Bitmask
  395. stream_buffer->put_32(0); // A Bitmask
  396. }
  397. uint32_t caps1 = info.compressed ? DDSD_LINEARSIZE : DDSD_PITCH;
  398. stream_buffer->put_32(caps1);
  399. stream_buffer->put_32(0); // Caps2
  400. stream_buffer->put_32(0); // Caps3
  401. stream_buffer->put_32(0); // Caps4
  402. stream_buffer->put_32(0); // Reserved 2
  403. if (format_type == DDFT_DXGI) {
  404. // DX10 header.
  405. uint32_t dxgi_format = _image_format_to_dxgi_format(image->get_format());
  406. stream_buffer->put_32(dxgi_format);
  407. stream_buffer->put_32(DX10D_2D);
  408. stream_buffer->put_32(0); // Misc flags 1
  409. stream_buffer->put_32(1); // Array size
  410. stream_buffer->put_32(0); // Misc flags 2
  411. }
  412. for (uint32_t mip_i = 0; mip_i < mipmaps; mip_i++) {
  413. uint32_t mip_width = MAX(1u, width >> mip_i);
  414. uint32_t mip_height = MAX(1u, height >> mip_i);
  415. uint32_t expected_size = 0;
  416. if (info.compressed) {
  417. uint32_t blocks_x = (mip_width + info.divisor - 1) / info.divisor;
  418. uint32_t blocks_y = (mip_height + info.divisor - 1) / info.divisor;
  419. expected_size = blocks_x * blocks_y * info.block_size;
  420. } else {
  421. expected_size = mip_width * mip_height * info.block_size;
  422. }
  423. if (needs_pixeldata_swap) {
  424. // The image's channels need to be swapped.
  425. Ref<Image> mip_image = image->get_image_from_mipmap(mip_i);
  426. Vector<uint8_t> data = mip_image->get_data();
  427. ERR_FAIL_COND_V_MSG(data.size() != expected_size, Vector<uint8_t>(),
  428. "Image data size mismatch for mipmap level " + itos(mip_i) +
  429. ". Expected size: " + itos(expected_size) + ", actual size: " + itos(data.size()) + ".");
  430. if (mip_image->get_format() == Image::FORMAT_RGBA4444) {
  431. // RGBA4 to BGRA4
  432. const int64_t data_size = data.size();
  433. uint8_t *wb = data.ptrw();
  434. for (int64_t data_i = 0; data_i < data_size; data_i += 2) {
  435. uint8_t ar = wb[data_i + 0];
  436. uint8_t gb = wb[data_i + 1];
  437. wb[data_i + 1] = ((ar & 0x0F) << 4) | ((gb & 0xF0) >> 4);
  438. wb[data_i + 0] = ((ar & 0xF0) >> 4) | ((gb & 0x0F) << 4);
  439. }
  440. } else if (mip_image->get_format() == Image::FORMAT_RGB8) {
  441. // RGB8 to BGR8
  442. const int64_t data_size = data.size();
  443. uint8_t *wb = data.ptrw();
  444. for (int64_t data_i = 0; data_i < data_size; data_i += 3) {
  445. SWAP(wb[data_i], wb[data_i + 2]);
  446. }
  447. }
  448. stream_buffer->put_data(data.ptr(), data.size());
  449. } else {
  450. int64_t ofs, size;
  451. image->get_mipmap_offset_and_size(mip_i, ofs, size);
  452. ERR_FAIL_COND_V_MSG(size != expected_size, Vector<uint8_t>(),
  453. "Image data size mismatch for mipmap level " + itos(mip_i) +
  454. ". Expected size: " + itos(expected_size) + ", actual size: " + itos(size) + ".");
  455. stream_buffer->put_data(image->ptr() + ofs, size);
  456. }
  457. }
  458. return stream_buffer->get_data_array();
  459. }