texture_resource.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*
  2. * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "filesystem.h"
  6. #include "texture_resource.h"
  7. #include "reader_writer.h"
  8. #include "json_parser.h"
  9. #include "math_utils.h"
  10. #include "resource_manager.h"
  11. #include "log.h"
  12. #include "compile_options.h"
  13. namespace crown
  14. {
  15. #define FOURCC(a, b, c, d) uint32_t(a | (b << 8) | (c << 16) | (d << 24))
  16. #define DDSD_MAGIC FOURCC('D', 'D', 'S', ' ')
  17. #define DDSD_HEADERSIZE uint32_t(124)
  18. #define DDSD_UNUSED uint32_t(0x00000000)
  19. #define DDSD_CAPS uint32_t(0x00000001) // Required in every .dds file.
  20. #define DDSD_HEIGHT uint32_t(0x00000002) // Required in every .dds file.
  21. #define DDSD_WIDTH uint32_t(0x00000004) // Required in every .dds file.
  22. #define DDSD_PITCH uint32_t(0x00000008) // Required when pitch is provided for an uncompressed texture.
  23. #define DDSD_PIXELFORMAT uint32_t(0x00001000) // Required in every .dds file.
  24. #define DDSD_MIPMAPCOUNT uint32_t(0x00020000) // Required in a mipmapped texture.
  25. #define DDSD_LINEARSIZE uint32_t(0x00080000) // Required when pitch is provided for a compressed texture.
  26. #define DDSD_DEPTH uint32_t(0x00800000) // Required in a depth texture.
  27. #define DDS_HEADER_FLAGS_TEXTURE uint32_t(DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT)
  28. #define DDS_HEADER_FLAGS_MIPMAP uint32_t(DDSD_MIPMAPCOUNT)
  29. #define DDS_HEADER_FLAGS_VOLUME uint32_t(DDSD_DEPTH)
  30. #define DDS_HEADER_FLAGS_PITCH uint32_t(DDSD_PITCH)
  31. #define DDS_HEADER_FLAGS_LINEARSIZE uint32_t(DDSD_LINEARSIZE)
  32. #define DDSCAPS_COMPLEX uint32_t(0x00000008) // Optional; must be used on any file that contains more than one surface (a mipmap, a cubic environment map, or mipmapped volume texture).
  33. #define DDSCAPS_MIPMAP uint32_t(0x00400000) // Optional; should be used for a mipmap.
  34. #define DDSCAPS_TEXTURE uint32_t(0x00001000) // Required
  35. #define DDSCAPS2_CUBEMAP uint32_t(0x00000200) // Required for a cube map.
  36. #define DDSCAPS2_CUBEMAP_POSITIVEX uint32_t(0x00000400) // Required when these surfaces are stored in a cube map.
  37. #define DDSCAPS2_CUBEMAP_NEGATIVEX uint32_t(0x00000800) // Required when these surfaces are stored in a cube map.
  38. #define DDSCAPS2_CUBEMAP_POSITIVEY uint32_t(0x00001000) // Required when these surfaces are stored in a cube map.
  39. #define DDSCAPS2_CUBEMAP_NEGATIVEY uint32_t(0x00002000) // Required when these surfaces are stored in a cube map.
  40. #define DDSCAPS2_CUBEMAP_POSITIVEZ uint32_t(0x00004000) // Required when these surfaces are stored in a cube map.
  41. #define DDSCAPS2_CUBEMAP_NEGATIVEZ uint32_t(0x00008000) // Required when these surfaces are stored in a cube map.
  42. #define DDSCAPS2_VOLUME uint32_t(0x00200000) // Required for a volume texture.
  43. #define DDPF_HEADERSIZE uint32_t(32)
  44. #define DDPF_ALPHAPIXELS uint32_t(0x00000001) // Texture contains alpha data; dwRGBAlphaBitMask contains valid data.
  45. #define DDPF_ALPHA uint32_t(0x00000002) // Used in some older DDS files for alpha channel only uncompressed data (dwRGBBitCount contains the alpha channel bitcount; dwABitMask contains valid data)
  46. #define DDPF_FOURCC uint32_t(0x00000004) // Texture contains compressed RGB data; dwFourCC contains valid data.
  47. #define DDPF_RGB uint32_t(0x00000040) // Texture contains uncompressed RGB data; dwRGBBitCount and the RGB masks (dwRBitMask, dwGBitMask, dwBBitMask) contain valid data.
  48. #define DDPF_YUV uint32_t(0x00000200) // Used in some older DDS files for YUV uncompressed data (dwRGBBitCount contains the YUV bit count; dwRBitMask contains the Y mask, dwGBitMask contains the U mask, dwBBitMask contains the V mask)
  49. #define DDPF_LUMINANCE uint32_t(0x00020000) // Used in some older DDS files for single channel color uncompressed data (dwRGBBitCount contains the luminance channel bit count; dwRBitMask contains the channel mask). Can be combined with DDPF_ALPHAPIXELS for a two channel DDS file.
  50. #define DDS_FOURCC uint32_t(DDPF_FOURCC)
  51. #define DDS_RGB uint32_t(DDPF_RGB)
  52. #define DDS_RGBA uint32_t(DDPF_RGB | DDPF_ALPHAPIXELS)
  53. #define DDS_LUMINANCE uint32_t(DDPF_LUMINANCE)
  54. #define DDS_LUMINANCEA uint32_t(DDPF_LUMINANCE | DDPF_ALPHAPIXELS)
  55. #define DDS_ALPHA uint32_t(DDPF_ALPHA)
  56. #define DDPF_FOURCC_DXT1 FOURCC('D', 'X', 'T', '1')
  57. #define DDPF_FOURCC_DXT2 FOURCC('D', 'X', 'T', '2')
  58. #define DDPF_FOURCC_DXT3 FOURCC('D', 'X', 'T', '3')
  59. #define DDPF_FOURCC_DXT4 FOURCC('D', 'X', 'T', '4')
  60. #define DDPF_FOURCC_DXT5 FOURCC('D', 'X', 'T', '5')
  61. #define DDPF_FOURCC_DX10 FOURCC('D', 'X', '1', '0')
  62. #define DDS_HEADER_OFFSET uint32_t(sizeof(TextureHeader))
  63. #define DDS_DATA_OFFSET uint32_t(DDS_HEADER_OFFSET + DDSD_HEADERSIZE)
  64. struct DdsPixelFormat
  65. {
  66. uint32_t size;
  67. uint32_t flags;
  68. uint32_t fourcc;
  69. uint32_t bitcount;
  70. uint32_t rmask;
  71. uint32_t gmask;
  72. uint32_t bmask;
  73. uint32_t amask;
  74. };
  75. struct DdsHeader
  76. {
  77. uint32_t magic;
  78. uint32_t size;
  79. uint32_t flags;
  80. uint32_t height;
  81. uint32_t width;
  82. uint32_t pitch_or_linear_size;
  83. uint32_t depth;
  84. uint32_t num_mips;
  85. uint32_t reserved[11];
  86. DdsPixelFormat ddspf;
  87. uint32_t caps;
  88. uint32_t caps2;
  89. uint32_t caps3;
  90. uint32_t caps4;
  91. uint32_t reserved2;
  92. };
  93. struct PixelFormat
  94. {
  95. enum Enum
  96. {
  97. DXT1,
  98. DXT3,
  99. DXT5,
  100. R8G8B8,
  101. R8G8B8A8,
  102. D16,
  103. D24,
  104. D32,
  105. D24S8,
  106. COUNT
  107. };
  108. };
  109. namespace pixel_format
  110. {
  111. inline uint32_t size(PixelFormat::Enum fmt)
  112. {
  113. switch (fmt)
  114. {
  115. case PixelFormat::DXT1: return 8;
  116. case PixelFormat::DXT3: return 16;
  117. case PixelFormat::DXT5: return 16;
  118. case PixelFormat::R8G8B8: return 3;
  119. case PixelFormat::R8G8B8A8: return 4;
  120. case PixelFormat::D16: return 2;
  121. case PixelFormat::D24: return 3;
  122. case PixelFormat::D32: return 4;
  123. case PixelFormat::D24S8: return 4;
  124. default: CE_FATAL("Unknown pixel format"); return 0;
  125. }
  126. }
  127. inline bool is_compressed(PixelFormat::Enum fmt)
  128. {
  129. return fmt < PixelFormat::R8G8B8;
  130. }
  131. inline bool is_color(PixelFormat::Enum fmt)
  132. {
  133. return fmt >= PixelFormat::R8G8B8 && fmt < PixelFormat::D16;
  134. }
  135. inline bool is_depth(PixelFormat::Enum fmt)
  136. {
  137. return fmt >= PixelFormat::D16 && fmt < PixelFormat::COUNT;
  138. }
  139. } // namespace pixel_format
  140. namespace texture_resource
  141. {
  142. struct ImageData
  143. {
  144. uint32_t width;
  145. uint32_t height;
  146. uint32_t pitch;
  147. PixelFormat::Enum format;
  148. uint32_t num_mips;
  149. char* data;
  150. };
  151. struct MipData
  152. {
  153. uint32_t width;
  154. uint32_t height;
  155. PixelFormat::Enum format;
  156. uint32_t size;
  157. char* data;
  158. };
  159. void read_mip_image(const ImageData& image, uint8_t mip, MipData& data)
  160. {
  161. uint32_t width = image.width;
  162. uint32_t height = image.height;
  163. //uint32_t pitch = image.pitch;
  164. uint32_t cur_mip = 0;
  165. char* src = image.data;
  166. while (1)
  167. {
  168. const uint32_t size = width * height * pixel_format::size(image.format);
  169. if (cur_mip == mip)
  170. {
  171. data.width = width;
  172. data.height = height;
  173. data.format = image.format;
  174. data.size = size;
  175. data.data = src;
  176. return;
  177. }
  178. width = max(1u, width >> 1);
  179. height = max(1u, height >> 1);
  180. cur_mip++;
  181. src += size;
  182. }
  183. }
  184. void swap_red_blue(uint32_t width, uint32_t height, uint8_t channels, char* data)
  185. {
  186. uint32_t i = 0;
  187. for (uint32_t h = 0; h < height; h++)
  188. {
  189. for (uint32_t w = 0; w < width; w++)
  190. {
  191. const uint8_t tmp = data[i + 0];
  192. data[i + 0] = data[i + 2];
  193. data[i + 2] = tmp;
  194. i += channels;
  195. }
  196. }
  197. }
  198. void read_tga_uncompressed(BinaryReader& br, uint32_t width, uint32_t height, uint8_t channels, ImageData& image)
  199. {
  200. if (channels == 2)
  201. {
  202. uint32_t i = 0;
  203. for (uint32_t h = 0; h < height; h++)
  204. {
  205. for (uint32_t w = 0; w < width; w++)
  206. {
  207. uint16_t data;
  208. br.read(data);
  209. image.data[i + 0] = (data & 0x7c) >> 10;
  210. image.data[i + 1] = (data & 0x3e) >> 5;
  211. image.data[i + 2] = (data & 0x1f);
  212. i += 3;
  213. }
  214. }
  215. }
  216. else
  217. {
  218. br.read(image.data, width * height * channels);
  219. swap_red_blue(width, height, channels, image.data);
  220. }
  221. }
  222. void read_tga_compressed(BinaryReader& br, uint32_t width, uint32_t height, uint8_t channels, ImageData& image)
  223. {
  224. uint8_t rle_id = 0;
  225. uint32_t i = 0;
  226. uint32_t colors_read = 0;
  227. // Can't be more than 4 channels
  228. uint8_t colors[4];
  229. while (i < width * height)
  230. {
  231. br.read(rle_id);
  232. // If MSB == 1
  233. if (rle_id & 0x80)
  234. {
  235. rle_id -= 127;
  236. br.read(colors[0]);
  237. br.read(colors[1]);
  238. br.read(colors[2]);
  239. if (channels == 4)
  240. br.read(colors[3]);
  241. while (rle_id)
  242. {
  243. image.data[colors_read + 0] = colors[2];
  244. image.data[colors_read + 1] = colors[1];
  245. image.data[colors_read + 2] = colors[0];
  246. if (channels == 4)
  247. image.data[colors_read + 3] = colors[3];
  248. rle_id--;
  249. colors_read += channels;
  250. i++;
  251. }
  252. }
  253. else
  254. {
  255. rle_id++;
  256. while (rle_id)
  257. {
  258. br.read(colors[0]);
  259. br.read(colors[1]);
  260. br.read(colors[2]);
  261. if (channels == 4)
  262. br.read(colors[3]);
  263. image.data[colors_read + 0] = colors[2];
  264. image.data[colors_read + 1] = colors[1];
  265. image.data[colors_read + 2] = colors[0];
  266. if (channels == 4)
  267. image.data[colors_read + 3] = colors[3];
  268. rle_id--;
  269. colors_read += channels;
  270. i++;
  271. }
  272. }
  273. }
  274. swap_red_blue(width, height, channels, image.data);
  275. }
  276. void parse_tga(BinaryReader& br, ImageData& image)
  277. {
  278. uint8_t id;
  279. br.read(id);
  280. uint8_t cmap_type;
  281. br.read(cmap_type);
  282. uint8_t image_type;
  283. br.read(image_type);
  284. uint8_t garbage;
  285. for (uint32_t i = 0; i < 5; i++)
  286. br.read(garbage);
  287. uint16_t x_offt;
  288. br.read(x_offt);
  289. uint16_t y_offt;
  290. br.read(y_offt);
  291. uint16_t width;
  292. br.read(width);
  293. uint16_t height;
  294. br.read(height);
  295. uint8_t depth;
  296. br.read(depth);
  297. uint8_t desc;
  298. br.read(desc);
  299. // Skip TGA ID
  300. br.skip(id);
  301. CE_ASSERT(image_type != 0, "TGA does not contain image data");
  302. CE_ASSERT(image_type == 2 || image_type == 10, "TGA image format not supported");
  303. const uint32_t channels = depth / 8;
  304. image.width = width;
  305. image.height = height;
  306. image.num_mips = 1;
  307. switch (channels)
  308. {
  309. case 2: image.format = PixelFormat::R8G8B8; break;
  310. case 3: image.format = PixelFormat::R8G8B8; break;
  311. case 4: image.format = PixelFormat::R8G8B8A8; break;
  312. default: CE_FATAL("TGA channels not supported"); break;
  313. }
  314. image.data = (char*) default_allocator().allocate(pixel_format::size(image.format) * width * height);
  315. if (image_type == 2)
  316. {
  317. read_tga_uncompressed(br, width, height, channels, image);
  318. }
  319. else if (image_type == 10)
  320. {
  321. read_tga_compressed(br, width, height, channels, image);
  322. }
  323. return;
  324. }
  325. void parse_dds(BinaryReader& br, ImageData& image)
  326. {
  327. // Read header
  328. uint32_t magic;
  329. br.read(magic);
  330. CE_ASSERT(magic == DDSD_MAGIC, "DDS bad magic number");
  331. uint32_t hsize;
  332. br.read(hsize);
  333. CE_ASSERT(hsize == DDSD_HEADERSIZE, "DDS bas header size");
  334. uint32_t flags;
  335. br.read(flags);
  336. CE_ASSERT(flags & (DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT), "DDS bad header flags");
  337. uint32_t height;
  338. br.read(height);
  339. uint32_t width;
  340. br.read(width);
  341. uint32_t pitch;
  342. br.read(pitch);
  343. uint32_t depth;
  344. br.read(depth);
  345. uint32_t num_mips;
  346. br.read(num_mips);
  347. // Skip reserved bits
  348. br.skip(sizeof(uint32_t) * 11);
  349. // Read pixel format
  350. uint32_t pf_hsize;
  351. br.read(pf_hsize);
  352. CE_ASSERT(pf_hsize == DDPF_HEADERSIZE, "DDS bad pf header size");
  353. uint32_t pf_flags;
  354. br.read(pf_flags);
  355. uint32_t pf_fourcc;
  356. br.read(pf_fourcc);
  357. uint32_t pf_bitcount;
  358. br.read(pf_bitcount);
  359. uint32_t pf_rmask;
  360. br.read(pf_rmask);
  361. uint32_t pf_gmask;
  362. br.read(pf_gmask);
  363. uint32_t pf_bmask;
  364. br.read(pf_bmask);
  365. uint32_t pf_amask;
  366. br.read(pf_amask);
  367. uint32_t caps;
  368. br.read(caps);
  369. CE_ASSERT((caps & DDSCAPS_TEXTURE), "DDS bad caps");
  370. uint32_t caps2;
  371. br.read(caps2);
  372. uint32_t caps3;
  373. br.read(caps3);
  374. uint32_t caps4;
  375. br.read(caps4);
  376. uint32_t reserved2;
  377. br.read(reserved2);
  378. CE_LOGD("width = %u", width);
  379. CE_LOGD("height = %u", height);
  380. CE_LOGD("mips = %u", num_mips);
  381. CE_LOGD("pitch = %u (valid = %s)", pitch, flags & DDSD_PITCH ? "yes" : "no");
  382. CE_LOGD("pfflags = %.8x", pf_flags);
  383. image.width = width;
  384. image.height = height;
  385. image.pitch = pitch;
  386. image.num_mips = (flags & DDSD_MIPMAPCOUNT) ? num_mips : 1;
  387. image.data = (char*) (uintptr_t) DDS_DATA_OFFSET;
  388. const uint32_t raw_fmt = (pf_flags & DDPF_FOURCC) ? pf_fourcc : pf_flags;
  389. switch (raw_fmt)
  390. {
  391. case DDPF_FOURCC_DXT1: image.format = PixelFormat::DXT1; break;
  392. case DDPF_FOURCC_DXT3: image.format = PixelFormat::DXT3; break;
  393. case DDPF_FOURCC_DXT5: image.format = PixelFormat::DXT5; break;
  394. case DDS_RGB: image.format = PixelFormat::R8G8B8; break;
  395. case DDS_RGBA: image.format = PixelFormat::R8G8B8A8; break;
  396. default: image.format = PixelFormat::COUNT; break;
  397. }
  398. CE_ASSERT(image.format != PixelFormat::COUNT, "DDS pixel format not supported");
  399. CE_LOGD("PixelFormat = %u", image.format);
  400. }
  401. void write_dds(BinaryWriter& bw, const ImageData& image)
  402. {
  403. bw.write(DDSD_MAGIC);
  404. // Header
  405. bw.write(DDSD_HEADERSIZE); // dwSize
  406. bw.write(DDS_HEADER_FLAGS_TEXTURE
  407. | DDSD_MIPMAPCOUNT
  408. | (pixel_format::is_compressed(image.format) ? DDSD_LINEARSIZE : DDSD_PITCH)
  409. | (image.num_mips ? DDSD_MIPMAPCOUNT : 0)); // dwFlags
  410. bw.write(image.height); // dwHeight
  411. bw.write(image.width); // dwWidth
  412. const uint32_t pitch = pixel_format::is_compressed(image.format) ? 0 // fixme
  413. : (image.width * pixel_format::size(image.format) * 8 + 7) / 8;
  414. bw.write(pitch); // dwPitchOrLinearSize
  415. bw.write(DDSD_UNUSED); // dwDepth
  416. bw.write(image.num_mips); // dwMipMapCount;
  417. for (uint32_t i = 0; i < 11; i++)
  418. bw.write(DDSD_UNUSED); // dwReserved1[11];
  419. // Pixel format
  420. bw.write(DDPF_HEADERSIZE); // dwSize;
  421. uint32_t pf = 0;
  422. switch (image.format)
  423. {
  424. case PixelFormat::DXT1: pf = DDPF_FOURCC_DXT1; break;
  425. case PixelFormat::DXT3: pf = DDPF_FOURCC_DXT3; break;
  426. case PixelFormat::DXT5: pf = DDPF_FOURCC_DXT5; break;
  427. case PixelFormat::R8G8B8: pf = DDS_RGB; break;
  428. case PixelFormat::R8G8B8A8: pf = DDS_RGBA; break;
  429. default: CE_FATAL("Pixel format unknown"); break;
  430. }
  431. bw.write(pixel_format::is_compressed(image.format) ? DDPF_FOURCC : pf); // dwFlags;
  432. bw.write(pixel_format::is_compressed(image.format) ? pf : DDSD_UNUSED); // dwFourCC;
  433. bw.write(uint32_t(pixel_format::size(image.format) * 8)); // dwRGBBitCount;
  434. bw.write(uint32_t(0x00ff0000)); // dwRBitMask;
  435. bw.write(uint32_t(0x0000ff00)); // dwGBitMask;
  436. bw.write(uint32_t(0x000000ff)); // dwBBitMask;
  437. bw.write(uint32_t(0xff000000)); // dwABitMask;
  438. bw.write(DDSCAPS_TEXTURE
  439. | (image.num_mips > 1 ? DDSCAPS_COMPLEX : DDSD_UNUSED) // also for cubemap, depth mipmap
  440. | (image.num_mips > 1 ? DDSCAPS_MIPMAP : DDSD_UNUSED)); // dwCaps;
  441. bw.write(DDSD_UNUSED); // dwCaps2;
  442. bw.write(DDSD_UNUSED); // dwCaps3;
  443. bw.write(DDSD_UNUSED); // dwCaps4;
  444. bw.write(DDSD_UNUSED); // dwReserved2;
  445. // Image data
  446. for (uint32_t i = 0; i < image.num_mips; i++)
  447. {
  448. MipData mip;
  449. read_mip_image(image, i, mip);
  450. // CE_LOGD("Writing mip: (%ux%u) byes = %u", mip.width, mip.height, mip.size);
  451. bw.write(mip.data, mip.size);
  452. }
  453. }
  454. void compile(const char* path, CompileOptions& opts)
  455. {
  456. Buffer buf = opts.read(path);
  457. JSONParser json(buf);
  458. JSONElement root = json.root();
  459. DynamicString name;
  460. root.key("source").to_string(name);
  461. File* source = opts._fs.open(name.c_str(), FOM_READ);
  462. BinaryReader br(*source);
  463. ImageData image;
  464. if (name.ends_with(".tga"))
  465. {
  466. parse_tga(br, image);
  467. }
  468. else if (name.ends_with(".dds"))
  469. {
  470. // parse_dds(br, image);
  471. // size_t size = source->size();
  472. // image.data = (char*) default_allocator().allocate(size);
  473. // source->seek(0);
  474. // source->read(image.data, size);
  475. // image.data += DDS_DATA_OFFSET;
  476. // BinaryWriter bw(*out_file);
  477. // write_dds(bw, image);
  478. }
  479. else
  480. {
  481. CE_FATAL("Source image not supported");
  482. }
  483. opts._fs.close(source);
  484. // Write DDS
  485. opts.write(TEXTURE_VERSION); // Version
  486. opts.write(uint32_t(0)); // Size
  487. write_dds(opts._bw, image);
  488. default_allocator().deallocate(image.data);
  489. }
  490. void* load(File& file, Allocator& a)
  491. {
  492. const size_t file_size = file.size();
  493. file.skip(sizeof(TextureHeader));
  494. const bgfx::Memory* mem = bgfx::alloc(file_size);
  495. file.read(mem->data, file_size - sizeof(TextureHeader));
  496. TextureResource* teximg = (TextureResource*) a.allocate(sizeof(TextureResource));
  497. teximg->mem = mem;
  498. teximg->handle.idx = bgfx::invalidHandle;
  499. return teximg;
  500. }
  501. void online(StringId64 id, ResourceManager& rm)
  502. {
  503. TextureResource* teximg = (TextureResource*) rm.get(TEXTURE_TYPE, id);
  504. teximg->handle = bgfx::createTexture(teximg->mem);
  505. }
  506. void offline(StringId64 id, ResourceManager& rm)
  507. {
  508. TextureResource* teximg = (TextureResource*) rm.get(TEXTURE_TYPE, id);
  509. bgfx::destroyTexture(teximg->handle);
  510. }
  511. void unload(Allocator& a, void* resource)
  512. {
  513. a.deallocate(resource);
  514. }
  515. } // namespace texture_resource
  516. } // namespace crown