texture_resource.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "allocator.h"
  24. #include "filesystem.h"
  25. #include "texture_resource.h"
  26. #include "reader_writer.h"
  27. #include "json_parser.h"
  28. #include "math_utils.h"
  29. #include "log.h"
  30. namespace crown
  31. {
  32. struct PixelFormat
  33. {
  34. enum Enum
  35. {
  36. DXT1,
  37. DXT3,
  38. DXT5,
  39. R8G8B8,
  40. R8G8B8A8,
  41. D16,
  42. D24,
  43. D32,
  44. D24S8,
  45. COUNT
  46. };
  47. };
  48. namespace pixel_format
  49. {
  50. //-----------------------------------------------------------------------------
  51. inline uint32_t size(PixelFormat::Enum fmt)
  52. {
  53. switch (fmt)
  54. {
  55. case PixelFormat::DXT1: return 8;
  56. case PixelFormat::DXT3: return 16;
  57. case PixelFormat::DXT5: return 16;
  58. case PixelFormat::R8G8B8: return 3;
  59. case PixelFormat::R8G8B8A8: return 4;
  60. case PixelFormat::D16: return 2;
  61. case PixelFormat::D24: return 3;
  62. case PixelFormat::D32: return 4;
  63. case PixelFormat::D24S8: return 4;
  64. default: CE_FATAL("Unknown pixel format"); return 0;
  65. }
  66. }
  67. //-----------------------------------------------------------------------------
  68. inline bool is_compressed(PixelFormat::Enum fmt)
  69. {
  70. return fmt < PixelFormat::R8G8B8;
  71. }
  72. //-----------------------------------------------------------------------------
  73. inline bool is_color(PixelFormat::Enum fmt)
  74. {
  75. return fmt >= PixelFormat::R8G8B8 && fmt < PixelFormat::D16;
  76. }
  77. //-----------------------------------------------------------------------------
  78. inline bool is_depth(PixelFormat::Enum fmt)
  79. {
  80. return fmt >= PixelFormat::D16 && fmt < PixelFormat::COUNT;
  81. }
  82. } // namespace pixel_format
  83. namespace texture_resource
  84. {
  85. struct ImageData
  86. {
  87. uint32_t width;
  88. uint32_t height;
  89. uint32_t pitch;
  90. PixelFormat::Enum format;
  91. uint32_t num_mips;
  92. char* data;
  93. };
  94. struct MipData
  95. {
  96. uint32_t width;
  97. uint32_t height;
  98. PixelFormat::Enum format;
  99. uint32_t size;
  100. char* data;
  101. };
  102. //-----------------------------------------------------------------------------
  103. void read_mip_image(const ImageData& image, uint8_t mip, MipData& data)
  104. {
  105. uint32_t width = image.width;
  106. uint32_t height = image.height;
  107. //uint32_t pitch = image.pitch;
  108. uint32_t cur_mip = 0;
  109. char* src = image.data;
  110. while (1)
  111. {
  112. const uint32_t size = width * height * pixel_format::size(image.format);
  113. if (cur_mip == mip)
  114. {
  115. data.width = width;
  116. data.height = height;
  117. data.format = image.format;
  118. data.size = size;
  119. data.data = src;
  120. return;
  121. }
  122. width = math::max(1u, width >> 1);
  123. height = math::max(1u, height >> 1);
  124. cur_mip++;
  125. src += size;
  126. }
  127. }
  128. //-----------------------------------------------------------------------------
  129. void swap_red_blue(uint32_t width, uint32_t height, uint8_t channels, char* data)
  130. {
  131. uint32_t i = 0;
  132. for (uint32_t h = 0; h < height; h++)
  133. {
  134. for (uint32_t w = 0; w < width; w++)
  135. {
  136. const uint8_t tmp = data[i + 0];
  137. data[i + 0] = data[i + 2];
  138. data[i + 2] = tmp;
  139. i += channels;
  140. }
  141. }
  142. }
  143. //-----------------------------------------------------------------------------
  144. void read_tga_uncompressed(BinaryReader& br, uint32_t width, uint32_t height, uint8_t channels, ImageData& image)
  145. {
  146. if (channels == 2)
  147. {
  148. uint32_t i = 0;
  149. for (uint32_t h = 0; h < height; h++)
  150. {
  151. for (uint32_t w = 0; w < width; w++)
  152. {
  153. uint16_t data;
  154. br.read(data);
  155. image.data[i + 0] = (data & 0x7c) >> 10;
  156. image.data[i + 1] = (data & 0x3e) >> 5;
  157. image.data[i + 2] = (data & 0x1f);
  158. i += 3;
  159. }
  160. }
  161. }
  162. else
  163. {
  164. br.read(image.data, width * height * channels);
  165. swap_red_blue(width, height, channels, image.data);
  166. }
  167. }
  168. //-----------------------------------------------------------------------------
  169. void read_tga_compressed(BinaryReader& br, uint32_t width, uint32_t height, uint8_t channels, ImageData& image)
  170. {
  171. uint8_t rle_id = 0;
  172. uint32_t i = 0;
  173. uint32_t colors_read = 0;
  174. // Can't be more than 4 channels
  175. uint8_t colors[4];
  176. while (i < width * height)
  177. {
  178. br.read(rle_id);
  179. // If MSB == 1
  180. if (rle_id & 0x80)
  181. {
  182. rle_id -= 127;
  183. br.read(colors[0]);
  184. br.read(colors[1]);
  185. br.read(colors[2]);
  186. if (channels == 4)
  187. br.read(colors[3]);
  188. while (rle_id)
  189. {
  190. image.data[colors_read + 0] = colors[2];
  191. image.data[colors_read + 1] = colors[1];
  192. image.data[colors_read + 2] = colors[0];
  193. if (channels == 4)
  194. image.data[colors_read + 3] = colors[3];
  195. rle_id--;
  196. colors_read += channels;
  197. i++;
  198. }
  199. }
  200. else
  201. {
  202. rle_id++;
  203. while (rle_id)
  204. {
  205. br.read(colors[0]);
  206. br.read(colors[1]);
  207. br.read(colors[2]);
  208. if (channels == 4)
  209. br.read(colors[3]);
  210. image.data[colors_read + 0] = colors[2];
  211. image.data[colors_read + 1] = colors[1];
  212. image.data[colors_read + 2] = colors[0];
  213. if (channels == 4)
  214. image.data[colors_read + 3] = colors[3];
  215. rle_id--;
  216. colors_read += channels;
  217. i++;
  218. }
  219. }
  220. }
  221. swap_red_blue(width, height, channels, image.data);
  222. }
  223. //-----------------------------------------------------------------------------
  224. void parse_tga(BinaryReader& br, ImageData& image)
  225. {
  226. uint8_t id;
  227. br.read(id);
  228. uint8_t cmap_type;
  229. br.read(cmap_type);
  230. uint8_t image_type;
  231. br.read(image_type);
  232. uint8_t garbage;
  233. for (uint32_t i = 0; i < 5; i++)
  234. br.read(garbage);
  235. uint16_t x_offt;
  236. br.read(x_offt);
  237. uint16_t y_offt;
  238. br.read(y_offt);
  239. uint16_t width;
  240. br.read(width);
  241. uint16_t height;
  242. br.read(height);
  243. uint8_t depth;
  244. br.read(depth);
  245. uint8_t desc;
  246. br.read(desc);
  247. // Skip TGA ID
  248. br.skip(id);
  249. CE_ASSERT(image_type != 0, "TGA does not contain image data");
  250. CE_ASSERT(image_type == 2 || image_type == 10, "TGA image format not supported");
  251. const uint32_t channels = depth / 8;
  252. image.width = width;
  253. image.height = height;
  254. image.num_mips = 1;
  255. switch (channels)
  256. {
  257. case 2: image.format = PixelFormat::R8G8B8; break;
  258. case 3: image.format = PixelFormat::R8G8B8; break;
  259. case 4: image.format = PixelFormat::R8G8B8A8; break;
  260. default: CE_FATAL("TGA channels not supported"); break;
  261. }
  262. image.data = (char*) default_allocator().allocate(pixel_format::size(image.format) * width * height);
  263. if (image_type == 2)
  264. {
  265. read_tga_uncompressed(br, width, height, channels, image);
  266. }
  267. else if (image_type == 10)
  268. {
  269. read_tga_compressed(br, width, height, channels, image);
  270. }
  271. return;
  272. }
  273. //-----------------------------------------------------------------------------
  274. void parse_dds(BinaryReader& br, ImageData& image)
  275. {
  276. // Read header
  277. uint32_t magic;
  278. br.read(magic);
  279. CE_ASSERT(magic == DDSD_MAGIC, "DDS bad magic number");
  280. uint32_t hsize;
  281. br.read(hsize);
  282. CE_ASSERT(hsize == DDSD_HEADERSIZE, "DDS bas header size");
  283. uint32_t flags;
  284. br.read(flags);
  285. CE_ASSERT(flags & (DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT), "DDS bad header flags");
  286. uint32_t height;
  287. br.read(height);
  288. uint32_t width;
  289. br.read(width);
  290. uint32_t pitch;
  291. br.read(pitch);
  292. uint32_t depth;
  293. br.read(depth);
  294. uint32_t num_mips;
  295. br.read(num_mips);
  296. // Skip reserved bits
  297. br.skip(sizeof(uint32_t) * 11);
  298. // Read pixel format
  299. uint32_t pf_hsize;
  300. br.read(pf_hsize);
  301. CE_ASSERT(pf_hsize == DDPF_HEADERSIZE, "DDS bad pf header size");
  302. uint32_t pf_flags;
  303. br.read(pf_flags);
  304. uint32_t pf_fourcc;
  305. br.read(pf_fourcc);
  306. uint32_t pf_bitcount;
  307. br.read(pf_bitcount);
  308. uint32_t pf_rmask;
  309. br.read(pf_rmask);
  310. uint32_t pf_gmask;
  311. br.read(pf_gmask);
  312. uint32_t pf_bmask;
  313. br.read(pf_bmask);
  314. uint32_t pf_amask;
  315. br.read(pf_amask);
  316. uint32_t caps;
  317. br.read(caps);
  318. CE_ASSERT((caps & DDSCAPS_TEXTURE), "DDS bad caps");
  319. uint32_t caps2;
  320. br.read(caps2);
  321. uint32_t caps3;
  322. br.read(caps3);
  323. uint32_t caps4;
  324. br.read(caps4);
  325. uint32_t reserved2;
  326. br.read(reserved2);
  327. CE_LOGD("width = %u", width);
  328. CE_LOGD("height = %u", height);
  329. CE_LOGD("mips = %u", num_mips);
  330. CE_LOGD("pitch = %u (valid = %s)", pitch, flags & DDSD_PITCH ? "yes" : "no");
  331. CE_LOGD("pfflags = %.8x", pf_flags);
  332. image.width = width;
  333. image.height = height;
  334. image.pitch = pitch;
  335. image.num_mips = (flags & DDSD_MIPMAPCOUNT) ? num_mips : 1;
  336. image.data = (char*) (uintptr_t) DDS_DATA_OFFSET;
  337. const uint32_t raw_fmt = (pf_flags & DDPF_FOURCC) ? pf_fourcc : pf_flags;
  338. switch (raw_fmt)
  339. {
  340. case DDPF_FOURCC_DXT1: image.format = PixelFormat::DXT1; break;
  341. case DDPF_FOURCC_DXT3: image.format = PixelFormat::DXT3; break;
  342. case DDPF_FOURCC_DXT5: image.format = PixelFormat::DXT5; break;
  343. case DDS_RGB: image.format = PixelFormat::R8G8B8; break;
  344. case DDS_RGBA: image.format = PixelFormat::R8G8B8A8; break;
  345. default: image.format = PixelFormat::COUNT; break;
  346. }
  347. CE_ASSERT(image.format != PixelFormat::COUNT, "DDS pixel format not supported");
  348. CE_LOGD("PixelFormat = %u", image.format);
  349. }
  350. //-----------------------------------------------------------------------------
  351. void write_dds(BinaryWriter& bw, const ImageData& image)
  352. {
  353. bw.write(DDSD_MAGIC);
  354. // Header
  355. bw.write(DDSD_HEADERSIZE); // dwSize
  356. bw.write(DDS_HEADER_FLAGS_TEXTURE
  357. | DDSD_MIPMAPCOUNT
  358. | (pixel_format::is_compressed(image.format) ? DDSD_LINEARSIZE : DDSD_PITCH)
  359. | (image.num_mips ? DDSD_MIPMAPCOUNT : 0)); // dwFlags
  360. bw.write(image.height); // dwHeight
  361. bw.write(image.width); // dwWidth
  362. const uint32_t pitch = pixel_format::is_compressed(image.format) ? 0 // fixme
  363. : (image.width * pixel_format::size(image.format) * 8 + 7) / 8;
  364. bw.write(pitch); // dwPitchOrLinearSize
  365. bw.write(DDSD_UNUSED); // dwDepth
  366. bw.write(image.num_mips); // dwMipMapCount;
  367. for (uint32_t i = 0; i < 11; i++)
  368. bw.write(DDSD_UNUSED); // dwReserved1[11];
  369. // Pixel format
  370. bw.write(DDPF_HEADERSIZE); // dwSize;
  371. uint32_t pf = 0;
  372. switch (image.format)
  373. {
  374. case PixelFormat::DXT1: pf = DDPF_FOURCC_DXT1; break;
  375. case PixelFormat::DXT3: pf = DDPF_FOURCC_DXT3; break;
  376. case PixelFormat::DXT5: pf = DDPF_FOURCC_DXT5; break;
  377. case PixelFormat::R8G8B8: pf = DDS_RGB; break;
  378. case PixelFormat::R8G8B8A8: pf = DDS_RGBA; break;
  379. default: CE_FATAL("Pixel format unknown"); break;
  380. }
  381. bw.write(pixel_format::is_compressed(image.format) ? DDPF_FOURCC : pf); // dwFlags;
  382. bw.write(pixel_format::is_compressed(image.format) ? pf : DDSD_UNUSED); // dwFourCC;
  383. bw.write(uint32_t(pixel_format::size(image.format) * 8)); // dwRGBBitCount;
  384. bw.write(uint32_t(0x00FF0000)); // dwRBitMask;
  385. bw.write(uint32_t(0x0000FF00)); // dwGBitMask;
  386. bw.write(uint32_t(0x000000FF)); // dwBBitMask;
  387. bw.write(uint32_t(0xFF000000)); // dwABitMask;
  388. bw.write(DDSCAPS_TEXTURE
  389. | (image.num_mips > 1 ? DDSCAPS_COMPLEX : DDSD_UNUSED) // also for cubemap, depth mipmap
  390. | (image.num_mips > 1 ? DDSCAPS_MIPMAP : DDSD_UNUSED)); // dwCaps;
  391. bw.write(DDSD_UNUSED); // dwCaps2;
  392. bw.write(DDSD_UNUSED); // dwCaps3;
  393. bw.write(DDSD_UNUSED); // dwCaps4;
  394. bw.write(DDSD_UNUSED); // dwReserved2;
  395. // Image data
  396. for (uint32_t i = 0; i < image.num_mips; i++)
  397. {
  398. MipData mip;
  399. read_mip_image(image, i, mip);
  400. // CE_LOGD("Writing mip: (%ux%u) byes = %u", mip.width, mip.height, mip.size);
  401. bw.write(mip.data, mip.size);
  402. }
  403. }
  404. //-----------------------------------------------------------------------------
  405. void compile(Filesystem& fs, const char* resource_path, File* out_file)
  406. {
  407. File* in_file = fs.open(resource_path, FOM_READ);
  408. JSONParser json(*in_file);
  409. fs.close(in_file);
  410. // Read source file
  411. JSONElement root = json.root();
  412. DynamicString name;
  413. root.key("source").to_string(name);
  414. File* source = fs.open(name.c_str(), FOM_READ);
  415. BinaryReader br(*source);
  416. ImageData image;
  417. if (name.ends_with(".tga"))
  418. {
  419. parse_tga(br, image);
  420. }
  421. else if (name.ends_with(".dds"))
  422. {
  423. // parse_dds(br, image);
  424. // size_t size = source->size();
  425. // image.data = (char*) default_allocator().allocate(size);
  426. // source->seek(0);
  427. // source->read(image.data, size);
  428. // image.data += DDS_DATA_OFFSET;
  429. // BinaryWriter bw(*out_file);
  430. // write_dds(bw, image);
  431. }
  432. else
  433. {
  434. CE_FATAL("Source image not supported");
  435. }
  436. fs.close(source);
  437. BinaryWriter bw(*out_file);
  438. // Write DDS
  439. bw.write(uint32_t(1)); // Version
  440. bw.write(uint32_t(0)); // Size
  441. write_dds(bw, image);
  442. default_allocator().deallocate(image.data);
  443. }
  444. } // namespace texture_resource
  445. } // namespace crown