texture_loader_dds.cpp 15 KB

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