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