texture_loader_dds.cpp 11 KB

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