texture_loader_dds.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*************************************************************************/
  2. /* texture_loader_dds.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "texture_loader_dds.h"
  30. #include "os/file_access.h"
  31. enum {
  32. DDS_MAGIC=0x20534444,
  33. DDSD_CAPS=0x00000001,
  34. DDSD_PIXELFORMAT=0x00001000,
  35. DDSD_PITCH=0x00000008,
  36. DDSD_LINEARSIZE=0x00080000,
  37. DDSD_MIPMAPCOUNT=0x00020000,
  38. DDPF_FOURCC=0x00000004,
  39. DDPF_ALPHAPIXELS=0x00000001,
  40. DDPF_INDEXED=0x00000020,
  41. DDPF_RGB=0x00000040,
  42. };
  43. enum DDSFormat {
  44. DDS_DXT1,
  45. DDS_DXT3,
  46. DDS_DXT5,
  47. DDS_ATI1,
  48. DDS_ATI2,
  49. DDS_BGRA8,
  50. DDS_BGR8,
  51. DDS_RGBA8, //flipped in dds
  52. DDS_RGB8, //flipped in dds
  53. DDS_BGR5A1,
  54. DDS_BGR565,
  55. DDS_BGR10A2,
  56. DDS_INDEXED,
  57. DDS_LUMINANCE,
  58. DDS_LUMINANCE_ALPHA,
  59. DDS_MAX
  60. };
  61. struct DDSFormatInfo {
  62. const char *name;
  63. bool compressed;
  64. bool palette;
  65. uint32_t divisor;
  66. uint32_t block_size;
  67. Image::Format format;
  68. };
  69. static const DDSFormatInfo dds_format_info[DDS_MAX]={
  70. {"DXT1",true,false,4,8,Image::FORMAT_DXT1},
  71. {"DXT3",true,false,4,16,Image::FORMAT_DXT3},
  72. {"DXT5",true,false,4,16,Image::FORMAT_DXT5},
  73. {"ATI1",true,false,4,8,Image::FORMAT_ATI1},
  74. {"ATI2",true,false,4,16,Image::FORMAT_ATI2},
  75. {"BGRA8",false,false,1,4,Image::FORMAT_RGBA8},
  76. {"BGR8",false,false,1,3,Image::FORMAT_RGB8},
  77. {"RGBA8",false,false,1,4,Image::FORMAT_RGBA8},
  78. {"RGB8",false,false,1,3,Image::FORMAT_RGB8},
  79. {"BGR5A1",false,false,1,2,Image::FORMAT_RGBA8},
  80. {"BGR565",false,false,1,2,Image::FORMAT_RGB8},
  81. {"BGR10A2",false,false,1,4,Image::FORMAT_RGBA8},
  82. {"GRAYSCALE",false,false,1,1,Image::FORMAT_L8},
  83. {"GRAYSCALE_ALPHA",false,false,1,2,Image::FORMAT_LA8}
  84. };
  85. RES ResourceFormatDDS::load(const String &p_path, const String& p_original_path, Error *r_error) {
  86. if (r_error)
  87. *r_error=ERR_CANT_OPEN;
  88. Error err;
  89. FileAccess *f = FileAccess::open(p_path,FileAccess::READ,&err);
  90. if (!f)
  91. return RES();
  92. FileAccessRef fref(f);
  93. if (r_error)
  94. *r_error=ERR_FILE_CORRUPT;
  95. ERR_EXPLAIN("Unable to open DDS texture file: "+p_path);
  96. ERR_FAIL_COND_V(err!=OK,RES());
  97. uint32_t magic = f->get_32();
  98. uint32_t hsize = f->get_32();
  99. uint32_t flags = f->get_32();
  100. uint32_t width = f->get_32();
  101. uint32_t height = f->get_32();
  102. uint32_t pitch = f->get_32();
  103. uint32_t depth = f->get_32();
  104. uint32_t mipmaps = f->get_32();
  105. //skip 11
  106. for(int i=0;i<11;i++)
  107. f->get_32();
  108. //validate
  109. if (magic!=DDS_MAGIC || hsize!=124 || !(flags&DDSD_PIXELFORMAT) || !(flags&DDSD_CAPS)) {
  110. ERR_EXPLAIN("Invalid or Unsupported DDS texture file: "+p_path);
  111. ERR_FAIL_V(RES());
  112. }
  113. uint32_t format_size = f->get_32();
  114. uint32_t format_flags = f->get_32();
  115. uint32_t format_fourcc = f->get_32();
  116. uint32_t format_rgb_bits = f->get_32();
  117. uint32_t format_red_mask = f->get_32();
  118. uint32_t format_green_mask = f->get_32();
  119. uint32_t format_blue_mask = f->get_32();
  120. uint32_t format_alpha_mask = f->get_32();
  121. uint32_t caps_1 = f->get_32();
  122. uint32_t caps_2 = f->get_32();
  123. uint32_t caps_ddsx = f->get_32();
  124. //reserved skip
  125. f->get_32();
  126. f->get_32();
  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. //must avoid this later
  133. while(f->get_pos()<128)
  134. f->get_8();
  135. DDSFormat dds_format;
  136. if (format_flags&DDPF_FOURCC && format_fourcc==0x31545844) { //'1TXD'
  137. dds_format=DDS_DXT1;
  138. } else if (format_flags&DDPF_FOURCC && format_fourcc==0x33545844) { //'3TXD'
  139. dds_format=DDS_DXT3;
  140. } else if (format_flags&DDPF_FOURCC && format_fourcc==0x35545844) { //'5TXD'
  141. dds_format=DDS_DXT5;
  142. } else if (format_flags&DDPF_FOURCC && format_fourcc==0x31495441) { //'1ITA'
  143. dds_format=DDS_ATI1;
  144. } else if (format_flags&DDPF_FOURCC && format_fourcc==0x32495441) { //'2ITA'
  145. dds_format=DDS_ATI2;
  146. } 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) {
  147. dds_format=DDS_BGRA8;
  148. } 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) {
  149. dds_format=DDS_BGR8;
  150. } 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) {
  151. dds_format=DDS_RGBA8;
  152. } 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) {
  153. dds_format=DDS_RGB8;
  154. } 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) {
  155. dds_format=DDS_BGR5A1;
  156. } 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) {
  157. dds_format=DDS_BGR10A2;
  158. } 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) {
  159. dds_format=DDS_BGR565;
  160. } else if (!(format_flags&DDPF_ALPHAPIXELS) && format_rgb_bits==8 && format_red_mask==0xff && format_green_mask==0xff && format_blue_mask==0xff) {
  161. dds_format=DDS_LUMINANCE;
  162. } 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) {
  163. dds_format=DDS_LUMINANCE_ALPHA;
  164. } else if (format_flags&DDPF_INDEXED && format_rgb_bits==8) {
  165. dds_format=DDS_BGR565;
  166. } else {
  167. 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);
  168. ERR_EXPLAIN("Unrecognized or Unsupported color layout in DDS: "+p_path);
  169. ERR_FAIL_V(RES());
  170. }
  171. if (!(flags&DDSD_MIPMAPCOUNT))
  172. mipmaps=1;
  173. // print_line("found format: "+String(dds_format_info[dds_format].name));
  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 pallete[256*4];
  201. f->get_buffer(pallete,256*4);
  202. int colsize=3;
  203. for(int i=0;i<256;i++) {
  204. if (pallete[i*4+3]<255)
  205. colsize=4;
  206. }
  207. int w = width;
  208. int h = height;
  209. for(uint32_t i=1;i<mipmaps;i++) {
  210. w=(w+1)>>1;
  211. h=(h+1)>>1;
  212. size+= w*h*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]=pallete[src_ofs+2];
  221. wb[dst_ofs+1]=pallete[src_ofs+1];
  222. wb[dst_ofs+2]=pallete[src_ofs+0];
  223. if (colsize==4)
  224. wb[dst_ofs+3]=pallete[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. wb=PoolVector<uint8_t>::Write();
  332. }
  333. Image img(width,height,mipmaps-1,info.format,src_data);
  334. Ref<ImageTexture> texture = memnew( ImageTexture );
  335. texture->create_from_image(img);
  336. if (r_error)
  337. *r_error=OK;
  338. return texture;
  339. }
  340. void ResourceFormatDDS::get_recognized_extensions(List<String> *p_extensions) const {
  341. p_extensions->push_back("dds");
  342. }
  343. bool ResourceFormatDDS::handles_type(const String& p_type) const {
  344. return ClassDB::is_parent_class(p_type,"Texture");
  345. }
  346. String ResourceFormatDDS::get_resource_type(const String &p_path) const {
  347. if (p_path.get_extension().to_lower()=="dds")
  348. return "ImageTexture";
  349. return "";
  350. }