texture_loader_dds.cpp 11 KB

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