texture_loader_dds.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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-2016 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_BC1},
  71. {"DXT3",true,false,4,16,Image::FORMAT_BC2},
  72. {"DXT5",true,false,4,16,Image::FORMAT_BC3},
  73. {"ATI1",true,false,4,8,Image::FORMAT_BC4},
  74. {"ATI2",true,false,4,16,Image::FORMAT_BC5},
  75. {"BGRA8",false,false,1,4,Image::FORMAT_RGBA},
  76. {"BGR8",false,false,1,3,Image::FORMAT_RGB},
  77. {"RGBA8",false,false,1,4,Image::FORMAT_RGBA},
  78. {"RGB8",false,false,1,3,Image::FORMAT_RGB},
  79. {"BGR5A1",false,false,1,2,Image::FORMAT_RGBA},
  80. {"BGR565",false,false,1,2,Image::FORMAT_RGB},
  81. {"BGR10A2",false,false,1,4,Image::FORMAT_RGBA},
  82. {"INDEXED",false,true,1,1,Image::FORMAT_INDEXED},
  83. {"GRAYSCALE",false,false,1,1,Image::FORMAT_GRAYSCALE},
  84. {"GRAYSCALE_ALPHA",false,false,1,2,Image::FORMAT_GRAYSCALE_ALPHA}
  85. };
  86. RES ResourceFormatDDS::load(const String &p_path, const String& p_original_path, Error *r_error) {
  87. if (r_error)
  88. *r_error=ERR_CANT_OPEN;
  89. Error err;
  90. FileAccess *f = FileAccess::open(p_path,FileAccess::READ,&err);
  91. if (!f)
  92. return RES();
  93. FileAccessRef fref(f);
  94. if (r_error)
  95. *r_error=ERR_FILE_CORRUPT;
  96. ERR_EXPLAIN("Unable to open DDS texture file: "+p_path);
  97. ERR_FAIL_COND_V(err!=OK,RES());
  98. uint32_t magic = f->get_32();
  99. uint32_t hsize = f->get_32();
  100. uint32_t flags = f->get_32();
  101. uint32_t width = f->get_32();
  102. uint32_t height = f->get_32();
  103. uint32_t pitch = f->get_32();
  104. uint32_t depth = f->get_32();
  105. uint32_t mipmaps = f->get_32();
  106. //skip 11
  107. for(int i=0;i<11;i++)
  108. f->get_32();
  109. //validate
  110. if (magic!=DDS_MAGIC || hsize!=124 || !(flags&DDSD_PIXELFORMAT) || !(flags&DDSD_CAPS)) {
  111. ERR_EXPLAIN("Invalid or Unsupported DDS texture file: "+p_path);
  112. ERR_FAIL_V(RES());
  113. }
  114. uint32_t format_size = f->get_32();
  115. uint32_t format_flags = f->get_32();
  116. uint32_t format_fourcc = f->get_32();
  117. uint32_t format_rgb_bits = f->get_32();
  118. uint32_t format_red_mask = f->get_32();
  119. uint32_t format_green_mask = f->get_32();
  120. uint32_t format_blue_mask = f->get_32();
  121. uint32_t format_alpha_mask = f->get_32();
  122. uint32_t caps_1 = f->get_32();
  123. uint32_t caps_2 = f->get_32();
  124. uint32_t caps_ddsx = f->get_32();
  125. //reserved skip
  126. f->get_32();
  127. f->get_32();
  128. /*print_line("DDS width: "+itos(width));
  129. print_line("DDS height: "+itos(height));
  130. print_line("DDS mipmaps: "+itos(mipmaps));*/
  131. //printf("fourcc: %x fflags: %x, rgbbits: %x, fsize: %x\n",format_fourcc,format_flags,format_rgb_bits,format_size);
  132. //printf("rmask: %x gmask: %x, bmask: %x, amask: %x\n",format_red_mask,format_green_mask,format_blue_mask,format_alpha_mask);
  133. //must avoid this later
  134. while(f->get_pos()<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. // print_line("found format: "+String(dds_format_info[dds_format].name));
  175. DVector<uint8_t> src_data;
  176. const DDSFormatInfo &info=dds_format_info[dds_format];
  177. uint32_t w = width;
  178. uint32_t h = height;
  179. if (info.compressed) {
  180. //compressed bc
  181. uint32_t size = MAX( info.divisor, w )/info.divisor * MAX( info.divisor, h )/info.divisor * info.block_size;
  182. ERR_FAIL_COND_V( size!=pitch, RES() );
  183. ERR_FAIL_COND_V( !(flags&DDSD_LINEARSIZE), RES() );
  184. for(uint32_t i=1;i<mipmaps;i++) {
  185. w=MAX(1,w>>1);
  186. h=MAX(1,h>>1);
  187. uint32_t bsize = MAX( info.divisor, w )/info.divisor * MAX( info.divisor, h )/info.divisor * info.block_size;
  188. //printf("%i x %i - block: %i\n",w,h,bsize);
  189. size+= bsize;
  190. }
  191. src_data.resize(size);
  192. DVector<uint8_t>::Write wb = src_data.write();
  193. f->get_buffer(wb.ptr(),size);
  194. wb=DVector<uint8_t>::Write();
  195. } else if (info.palette) {
  196. //indexed
  197. ERR_FAIL_COND_V( !(flags&DDSD_PITCH), RES());
  198. ERR_FAIL_COND_V( format_rgb_bits!=8, RES() );
  199. uint32_t size = pitch*height;
  200. ERR_FAIL_COND_V( size != width*height * info.block_size, RES());
  201. uint8_t pallete[256*4];
  202. f->get_buffer(pallete,256*4);
  203. int colsize=3;
  204. for(int i=0;i<256;i++) {
  205. if (pallete[i*4+3]<255)
  206. colsize=4;
  207. }
  208. int w = width;
  209. int h = height;
  210. for(uint32_t i=1;i<mipmaps;i++) {
  211. w=(w+1)>>1;
  212. h=(h+1)>>1;
  213. size+= w*h*info.block_size;
  214. }
  215. src_data.resize(size + 256*colsize );
  216. DVector<uint8_t>::Write wb = src_data.write();
  217. f->get_buffer(wb.ptr(),size);
  218. for(int i=0;i<256;i++) {
  219. int dst_ofs = size+i*colsize;
  220. int src_ofs = i*4;
  221. wb[dst_ofs+0]=pallete[src_ofs+2];
  222. wb[dst_ofs+1]=pallete[src_ofs+1];
  223. wb[dst_ofs+2]=pallete[src_ofs+0];
  224. if (colsize==4)
  225. wb[dst_ofs+3]=pallete[src_ofs+3];
  226. }
  227. wb=DVector<uint8_t>::Write();
  228. } else {
  229. //uncompressed generic...
  230. uint32_t size = width*height*info.block_size;
  231. for(uint32_t i=1;i<mipmaps;i++) {
  232. w=(w+1)>>1;
  233. h=(h+1)>>1;
  234. size+= w*h*info.block_size;
  235. }
  236. if (dds_format==DDS_BGR565)
  237. size=size*3/2;
  238. else if (dds_format==DDS_BGR5A1)
  239. size=size*2;
  240. src_data.resize(size);
  241. DVector<uint8_t>::Write wb = src_data.write();
  242. f->get_buffer(wb.ptr(),size);
  243. switch(dds_format) {
  244. case DDS_BGR5A1: {
  245. // TO RGBA
  246. int colcount = size/4;
  247. for(int i=colcount-1;i>=0;i--) {
  248. int src_ofs = i*2;
  249. int dst_ofs = i*4;
  250. uint8_t a=wb[src_ofs+1]&0x80;
  251. uint8_t b= wb[src_ofs]&0x1F;
  252. uint8_t g= (wb[src_ofs]>>5) | ((wb[src_ofs+1]&0x3)<<3);
  253. uint8_t r= (wb[src_ofs+1]>>2)&0x1F;
  254. wb[dst_ofs+0]=r<<3;
  255. wb[dst_ofs+1]=g<<3;
  256. wb[dst_ofs+2]=b<<3;
  257. wb[dst_ofs+3]=a?255:0;
  258. }
  259. } break;
  260. case DDS_BGR565: {
  261. int colcount = size/3;
  262. for(int i=colcount-1;i>=0;i--) {
  263. int src_ofs = i*2;
  264. int dst_ofs = i*3;
  265. uint8_t b= wb[src_ofs]&0x1F;
  266. uint8_t g= (wb[src_ofs]>>5) | ((wb[src_ofs+1]&0x7)<<3);
  267. uint8_t r= wb[src_ofs+1]>>3;
  268. wb[dst_ofs+0]=r<<3;
  269. wb[dst_ofs+1]=g<<2;
  270. wb[dst_ofs+2]=b<<3;//b<<3;
  271. }
  272. } break;
  273. case DDS_BGR10A2: {
  274. // TO RGBA
  275. int colcount = size/4;
  276. for(int i=colcount-1;i>=0;i--) {
  277. int ofs = i*4;
  278. 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);
  279. uint8_t a= (w32&0xc0000000) >> 24;
  280. uint8_t r= (w32&0x3ff00000) >> 22;
  281. uint8_t g= (w32&0xffc00) >> 12;
  282. uint8_t b= (w32&0x3ff) >> 2;
  283. wb[ofs+0]=r;
  284. wb[ofs+1]=g;
  285. wb[ofs+2]=b;
  286. wb[ofs+3]=a==0xc0 ? 255 : a; //0xc0 should be opaque
  287. }
  288. } break;
  289. case DDS_BGRA8: {
  290. int colcount = size/4;
  291. for(int i=0;i<colcount;i++) {
  292. SWAP( wb[i*4+0],wb[i*4+2] );
  293. }
  294. } break;
  295. case DDS_BGR8: {
  296. int colcount = size/3;
  297. for(int i=0;i<colcount;i++) {
  298. SWAP( wb[i*3+0],wb[i*3+2] );
  299. }
  300. } break;
  301. case DDS_RGBA8: {
  302. /* do nothing either
  303. int colcount = size/4;
  304. for(int i=0;i<colcount;i++) {
  305. uint8_t r = wb[i*4+1];
  306. uint8_t g = wb[i*4+2];
  307. uint8_t b = wb[i*4+3];
  308. uint8_t a = wb[i*4+0];
  309. wb[i*4+0]=r;
  310. wb[i*4+1]=g;
  311. wb[i*4+2]=b;
  312. wb[i*4+3]=a;
  313. }
  314. */
  315. } break;
  316. case DDS_RGB8: {
  317. // do nothing
  318. /*
  319. int colcount = size/3;
  320. for(int i=0;i<colcount;i++) {
  321. SWAP( wb[i*3+0],wb[i*3+2] );
  322. }*/
  323. } break;
  324. case DDS_LUMINANCE: {
  325. // do nothing i guess?
  326. } break;
  327. case DDS_LUMINANCE_ALPHA: {
  328. // do nothing i guess?
  329. } break;
  330. default: {}
  331. }
  332. wb=DVector<uint8_t>::Write();
  333. }
  334. Image img(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 ObjectTypeDB::is_type(p_type,"Texture");
  346. }
  347. String ResourceFormatDDS::get_resource_type(const String &p_path) const {
  348. if (p_path.extension().to_lower()=="dds")
  349. return "ImageTexture";
  350. return "";
  351. }