texture_loader_dds.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*************************************************************************/
  2. /* texture_loader_dds.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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/os/file_access.h"
  32. #define PF_FOURCC(s) ((uint32_t)(((s)[3] << 24U) | ((s)[2] << 16U) | ((s)[1] << 8U) | ((s)[0])))
  33. enum {
  34. DDS_MAGIC = 0x20534444,
  35. DDSD_CAPS = 0x00000001,
  36. DDSD_PIXELFORMAT = 0x00001000,
  37. DDSD_PITCH = 0x00000008,
  38. DDSD_LINEARSIZE = 0x00080000,
  39. DDSD_MIPMAPCOUNT = 0x00020000,
  40. DDPF_FOURCC = 0x00000004,
  41. DDPF_ALPHAPIXELS = 0x00000001,
  42. DDPF_INDEXED = 0x00000020,
  43. DDPF_RGB = 0x00000040,
  44. };
  45. enum DDSFormat {
  46. DDS_DXT1,
  47. DDS_DXT3,
  48. DDS_DXT5,
  49. DDS_ATI1,
  50. DDS_ATI2,
  51. DDS_A2XY,
  52. DDS_BGRA8,
  53. DDS_BGR8,
  54. DDS_RGBA8, //flipped in dds
  55. DDS_RGB8, //flipped in dds
  56. DDS_BGR5A1,
  57. DDS_BGR565,
  58. DDS_BGR10A2,
  59. DDS_INDEXED,
  60. DDS_LUMINANCE,
  61. DDS_LUMINANCE_ALPHA,
  62. DDS_MAX
  63. };
  64. struct DDSFormatInfo {
  65. const char *name;
  66. bool compressed;
  67. bool palette;
  68. uint32_t divisor;
  69. uint32_t block_size;
  70. Image::Format format;
  71. };
  72. static const DDSFormatInfo dds_format_info[DDS_MAX] = {
  73. { "DXT1/BC1", true, false, 4, 8, Image::FORMAT_DXT1 },
  74. { "DXT3/BC2", true, false, 4, 16, Image::FORMAT_DXT3 },
  75. { "DXT5/BC3", true, false, 4, 16, Image::FORMAT_DXT5 },
  76. { "ATI1/BC4", true, false, 4, 8, Image::FORMAT_RGTC_R },
  77. { "ATI2/3DC/BC5", true, false, 4, 16, Image::FORMAT_RGTC_RG },
  78. { "A2XY/DXN/BC5", true, false, 4, 16, Image::FORMAT_RGTC_RG },
  79. { "BGRA8", false, false, 1, 4, Image::FORMAT_RGBA8 },
  80. { "BGR8", false, false, 1, 3, Image::FORMAT_RGB8 },
  81. { "RGBA8", false, false, 1, 4, Image::FORMAT_RGBA8 },
  82. { "RGB8", false, false, 1, 3, Image::FORMAT_RGB8 },
  83. { "BGR5A1", false, false, 1, 2, Image::FORMAT_RGBA8 },
  84. { "BGR565", false, false, 1, 2, Image::FORMAT_RGB8 },
  85. { "BGR10A2", false, false, 1, 4, Image::FORMAT_RGBA8 },
  86. { "GRAYSCALE", false, false, 1, 1, Image::FORMAT_L8 },
  87. { "GRAYSCALE_ALPHA", false, false, 1, 2, Image::FORMAT_LA8 }
  88. };
  89. RES ResourceFormatDDS::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, bool p_no_cache) {
  90. if (r_error)
  91. *r_error = ERR_CANT_OPEN;
  92. Error err;
  93. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  94. if (!f)
  95. return RES();
  96. FileAccessRef fref(f);
  97. if (r_error)
  98. *r_error = ERR_FILE_CORRUPT;
  99. ERR_FAIL_COND_V_MSG(err != OK, RES(), "Unable to open DDS texture file '" + p_path + "'.");
  100. uint32_t magic = f->get_32();
  101. uint32_t hsize = f->get_32();
  102. uint32_t flags = f->get_32();
  103. uint32_t height = f->get_32();
  104. uint32_t width = f->get_32();
  105. uint32_t pitch = f->get_32();
  106. /* uint32_t depth = */ f->get_32();
  107. uint32_t mipmaps = f->get_32();
  108. //skip 11
  109. for (int i = 0; i < 11; i++)
  110. f->get_32();
  111. //validate
  112. if (magic != DDS_MAGIC || hsize != 124 || !(flags & DDSD_PIXELFORMAT) || !(flags & DDSD_CAPS)) {
  113. ERR_FAIL_V_MSG(RES(), "Invalid or unsupported DDS texture file '" + p_path + "'.");
  114. }
  115. /* uint32_t format_size = */ f->get_32();
  116. uint32_t format_flags = f->get_32();
  117. uint32_t format_fourcc = f->get_32();
  118. uint32_t format_rgb_bits = f->get_32();
  119. uint32_t format_red_mask = f->get_32();
  120. uint32_t format_green_mask = f->get_32();
  121. uint32_t format_blue_mask = f->get_32();
  122. uint32_t format_alpha_mask = f->get_32();
  123. /* uint32_t caps_1 = */ f->get_32();
  124. /* uint32_t caps_2 = */ f->get_32();
  125. /* uint32_t caps_ddsx = */ f->get_32();
  126. //reserved skip
  127. f->get_32();
  128. f->get_32();
  129. /*
  130. print_line("DDS width: "+itos(width));
  131. print_line("DDS height: "+itos(height));
  132. print_line("DDS mipmaps: "+itos(mipmaps));
  133. printf("fourcc: %x fflags: %x, rgbbits: %x, fsize: %x\n",format_fourcc,format_flags,format_rgb_bits,format_size);
  134. printf("rmask: %x gmask: %x, bmask: %x, amask: %x\n",format_red_mask,format_green_mask,format_blue_mask,format_alpha_mask);
  135. */
  136. //must avoid this later
  137. while (f->get_position() < 128)
  138. f->get_8();
  139. DDSFormat dds_format;
  140. if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("DXT1")) {
  141. dds_format = DDS_DXT1;
  142. } else if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("DXT3")) {
  143. dds_format = DDS_DXT3;
  144. } else if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("DXT5")) {
  145. dds_format = DDS_DXT5;
  146. } else if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("ATI1")) {
  147. dds_format = DDS_ATI1;
  148. } else if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("ATI2")) {
  149. dds_format = DDS_ATI2;
  150. } else if (format_flags & DDPF_FOURCC && format_fourcc == PF_FOURCC("A2XY")) {
  151. dds_format = DDS_A2XY;
  152. } else if (format_flags & DDPF_RGB && format_flags & DDPF_ALPHAPIXELS && format_rgb_bits == 32 && format_red_mask == 0xff0000 && format_green_mask == 0xff00 && format_blue_mask == 0xff && format_alpha_mask == 0xff000000) {
  153. dds_format = DDS_BGRA8;
  154. } else if (format_flags & DDPF_RGB && !(format_flags & DDPF_ALPHAPIXELS) && format_rgb_bits == 24 && format_red_mask == 0xff0000 && format_green_mask == 0xff00 && format_blue_mask == 0xff) {
  155. dds_format = DDS_BGR8;
  156. } else if (format_flags & DDPF_RGB && format_flags & DDPF_ALPHAPIXELS && format_rgb_bits == 32 && format_red_mask == 0xff && format_green_mask == 0xff00 && format_blue_mask == 0xff0000 && format_alpha_mask == 0xff000000) {
  157. dds_format = DDS_RGBA8;
  158. } else if (format_flags & DDPF_RGB && !(format_flags & DDPF_ALPHAPIXELS) && format_rgb_bits == 24 && format_red_mask == 0xff && format_green_mask == 0xff00 && format_blue_mask == 0xff0000) {
  159. dds_format = DDS_RGB8;
  160. } else if (format_flags & DDPF_RGB && format_flags & DDPF_ALPHAPIXELS && format_rgb_bits == 16 && format_red_mask == 0x00007c00 && format_green_mask == 0x000003e0 && format_blue_mask == 0x0000001f && format_alpha_mask == 0x00008000) {
  161. dds_format = DDS_BGR5A1;
  162. } else if (format_flags & DDPF_RGB && format_flags & DDPF_ALPHAPIXELS && format_rgb_bits == 32 && format_red_mask == 0x3ff00000 && format_green_mask == 0xffc00 && format_blue_mask == 0x3ff && format_alpha_mask == 0xc0000000) {
  163. dds_format = DDS_BGR10A2;
  164. } else if (format_flags & DDPF_RGB && !(format_flags & DDPF_ALPHAPIXELS) && format_rgb_bits == 16 && format_red_mask == 0x0000f800 && format_green_mask == 0x000007e0 && format_blue_mask == 0x0000001f) {
  165. dds_format = DDS_BGR565;
  166. } else if (!(format_flags & DDPF_ALPHAPIXELS) && format_rgb_bits == 8 && format_red_mask == 0xff && format_green_mask == 0xff && format_blue_mask == 0xff) {
  167. dds_format = DDS_LUMINANCE;
  168. } else if ((format_flags & DDPF_ALPHAPIXELS) && format_rgb_bits == 16 && format_red_mask == 0xff && format_green_mask == 0xff && format_blue_mask == 0xff && format_alpha_mask == 0xff00) {
  169. dds_format = DDS_LUMINANCE_ALPHA;
  170. } else if (format_flags & DDPF_INDEXED && format_rgb_bits == 8) {
  171. dds_format = DDS_BGR565;
  172. } else {
  173. printf("unrecognized fourcc %x format_flags: %x - rgbbits %i - red_mask %x green mask %x blue mask %x alpha mask %x\n", format_fourcc, format_flags, format_rgb_bits, format_red_mask, format_green_mask, format_blue_mask, format_alpha_mask);
  174. ERR_FAIL_V_MSG(RES(), "Unrecognized or unsupported color layout in DDS '" + p_path + "'.");
  175. }
  176. if (!(flags & DDSD_MIPMAPCOUNT))
  177. mipmaps = 1;
  178. Vector<uint8_t> src_data;
  179. const DDSFormatInfo &info = dds_format_info[dds_format];
  180. uint32_t w = width;
  181. uint32_t h = height;
  182. if (info.compressed) {
  183. //compressed bc
  184. uint32_t size = MAX(info.divisor, w) / info.divisor * MAX(info.divisor, h) / info.divisor * info.block_size;
  185. ERR_FAIL_COND_V(size != pitch, RES());
  186. ERR_FAIL_COND_V(!(flags & DDSD_LINEARSIZE), RES());
  187. for (uint32_t i = 1; i < mipmaps; i++) {
  188. w = MAX(1, w >> 1);
  189. h = MAX(1, h >> 1);
  190. uint32_t bsize = MAX(info.divisor, w) / info.divisor * MAX(info.divisor, h) / info.divisor * info.block_size;
  191. //printf("%i x %i - block: %i\n",w,h,bsize);
  192. size += bsize;
  193. }
  194. src_data.resize(size);
  195. uint8_t *wb = src_data.ptrw();
  196. f->get_buffer(wb, size);
  197. } else if (info.palette) {
  198. //indexed
  199. ERR_FAIL_COND_V(!(flags & DDSD_PITCH), RES());
  200. ERR_FAIL_COND_V(format_rgb_bits != 8, RES());
  201. uint32_t size = pitch * height;
  202. ERR_FAIL_COND_V(size != width * height * info.block_size, RES());
  203. uint8_t palette[256 * 4];
  204. f->get_buffer(palette, 256 * 4);
  205. int colsize = 3;
  206. for (int i = 0; i < 256; i++) {
  207. if (palette[i * 4 + 3] < 255)
  208. colsize = 4;
  209. }
  210. int w2 = width;
  211. int h2 = height;
  212. for (uint32_t i = 1; i < mipmaps; i++) {
  213. w2 = (w2 + 1) >> 1;
  214. h2 = (h2 + 1) >> 1;
  215. size += w2 * h2 * info.block_size;
  216. }
  217. src_data.resize(size + 256 * colsize);
  218. uint8_t *wb = src_data.ptrw();
  219. f->get_buffer(wb, size);
  220. for (int i = 0; i < 256; i++) {
  221. int dst_ofs = size + i * colsize;
  222. int src_ofs = i * 4;
  223. wb[dst_ofs + 0] = palette[src_ofs + 2];
  224. wb[dst_ofs + 1] = palette[src_ofs + 1];
  225. wb[dst_ofs + 2] = palette[src_ofs + 0];
  226. if (colsize == 4)
  227. wb[dst_ofs + 3] = palette[src_ofs + 3];
  228. }
  229. } else {
  230. //uncompressed generic...
  231. uint32_t size = width * height * info.block_size;
  232. for (uint32_t i = 1; i < mipmaps; i++) {
  233. w = (w + 1) >> 1;
  234. h = (h + 1) >> 1;
  235. size += w * h * info.block_size;
  236. }
  237. if (dds_format == DDS_BGR565)
  238. size = size * 3 / 2;
  239. else if (dds_format == DDS_BGR5A1)
  240. size = size * 2;
  241. src_data.resize(size);
  242. uint8_t *wb = src_data.ptrw();
  243. f->get_buffer(wb, size);
  244. switch (dds_format) {
  245. case DDS_BGR5A1: {
  246. // TO RGBA
  247. int colcount = size / 4;
  248. for (int i = colcount - 1; i >= 0; i--) {
  249. int src_ofs = i * 2;
  250. int dst_ofs = i * 4;
  251. uint8_t a = wb[src_ofs + 1] & 0x80;
  252. uint8_t b = wb[src_ofs] & 0x1F;
  253. uint8_t g = (wb[src_ofs] >> 5) | ((wb[src_ofs + 1] & 0x3) << 3);
  254. uint8_t r = (wb[src_ofs + 1] >> 2) & 0x1F;
  255. wb[dst_ofs + 0] = r << 3;
  256. wb[dst_ofs + 1] = g << 3;
  257. wb[dst_ofs + 2] = b << 3;
  258. wb[dst_ofs + 3] = a ? 255 : 0;
  259. }
  260. } break;
  261. case DDS_BGR565: {
  262. int colcount = size / 3;
  263. for (int i = colcount - 1; i >= 0; i--) {
  264. int src_ofs = i * 2;
  265. int dst_ofs = i * 3;
  266. uint8_t b = wb[src_ofs] & 0x1F;
  267. uint8_t g = (wb[src_ofs] >> 5) | ((wb[src_ofs + 1] & 0x7) << 3);
  268. uint8_t r = wb[src_ofs + 1] >> 3;
  269. wb[dst_ofs + 0] = r << 3;
  270. wb[dst_ofs + 1] = g << 2;
  271. wb[dst_ofs + 2] = b << 3; //b<<3;
  272. }
  273. } break;
  274. case DDS_BGR10A2: {
  275. // TO RGBA
  276. int colcount = size / 4;
  277. for (int i = colcount - 1; i >= 0; i--) {
  278. int ofs = i * 4;
  279. 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);
  280. uint8_t a = (w32 & 0xc0000000) >> 24;
  281. uint8_t r = (w32 & 0x3ff00000) >> 22;
  282. uint8_t g = (w32 & 0xffc00) >> 12;
  283. uint8_t b = (w32 & 0x3ff) >> 2;
  284. wb[ofs + 0] = r;
  285. wb[ofs + 1] = g;
  286. wb[ofs + 2] = b;
  287. wb[ofs + 3] = a == 0xc0 ? 255 : a; //0xc0 should be opaque
  288. }
  289. } break;
  290. case DDS_BGRA8: {
  291. int colcount = size / 4;
  292. for (int i = 0; i < colcount; i++) {
  293. SWAP(wb[i * 4 + 0], wb[i * 4 + 2]);
  294. }
  295. } break;
  296. case DDS_BGR8: {
  297. int colcount = size / 3;
  298. for (int i = 0; i < colcount; i++) {
  299. SWAP(wb[i * 3 + 0], wb[i * 3 + 2]);
  300. }
  301. } break;
  302. case DDS_RGBA8: {
  303. /* do nothing either
  304. int colcount = size/4;
  305. for(int i=0;i<colcount;i++) {
  306. uint8_t r = wb[i*4+1];
  307. uint8_t g = wb[i*4+2];
  308. uint8_t b = wb[i*4+3];
  309. uint8_t a = wb[i*4+0];
  310. wb[i*4+0]=r;
  311. wb[i*4+1]=g;
  312. wb[i*4+2]=b;
  313. wb[i*4+3]=a;
  314. }
  315. */
  316. } break;
  317. case DDS_RGB8: {
  318. // do nothing
  319. /*
  320. int colcount = size/3;
  321. for(int i=0;i<colcount;i++) {
  322. SWAP( wb[i*3+0],wb[i*3+2] );
  323. }*/
  324. } break;
  325. case DDS_LUMINANCE: {
  326. // do nothing i guess?
  327. } break;
  328. case DDS_LUMINANCE_ALPHA: {
  329. // do nothing i guess?
  330. } break;
  331. default: {
  332. }
  333. }
  334. }
  335. Ref<Image> img = memnew(Image(width, height, mipmaps - 1, info.format, src_data));
  336. Ref<ImageTexture> texture = memnew(ImageTexture);
  337. texture->create_from_image(img);
  338. if (r_error)
  339. *r_error = OK;
  340. return texture;
  341. }
  342. void ResourceFormatDDS::get_recognized_extensions(List<String> *p_extensions) const {
  343. p_extensions->push_back("dds");
  344. }
  345. bool ResourceFormatDDS::handles_type(const String &p_type) const {
  346. return ClassDB::is_parent_class(p_type, "Texture2D");
  347. }
  348. String ResourceFormatDDS::get_resource_type(const String &p_path) const {
  349. if (p_path.get_extension().to_lower() == "dds")
  350. return "ImageTexture";
  351. return "";
  352. }