ImageImporter.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Importer/ImageImporter.h>
  6. #include <AnKi/Importer/TinyExr.h>
  7. #include <AnKi/Gr/Common.h>
  8. #include <AnKi/Resource/Stb.h>
  9. #include <AnKi/Util/Process.h>
  10. #include <AnKi/Util/File.h>
  11. #include <AnKi/Util/Filesystem.h>
  12. namespace anki {
  13. namespace {
  14. class SurfaceOrVolumeData
  15. {
  16. public:
  17. DynamicArrayAuto<U8, PtrSize> m_pixels;
  18. DynamicArrayAuto<U8, PtrSize> m_s3tcPixels;
  19. DynamicArrayAuto<U8, PtrSize> m_astcPixels;
  20. SurfaceOrVolumeData(GenericMemoryPoolAllocator<U8> alloc)
  21. : m_pixels(alloc)
  22. , m_s3tcPixels(alloc)
  23. , m_astcPixels(alloc)
  24. {
  25. }
  26. };
  27. class Mipmap
  28. {
  29. public:
  30. /// One surface for each layer ore one per face or a single volume if it's a 3D texture.
  31. DynamicArrayAuto<SurfaceOrVolumeData> m_surfacesOrVolume;
  32. Mipmap(GenericMemoryPoolAllocator<U8> alloc)
  33. : m_surfacesOrVolume(alloc)
  34. {
  35. }
  36. };
  37. /// Image importer context.
  38. class ImageImporterContext
  39. {
  40. public:
  41. DynamicArrayAuto<Mipmap> m_mipmaps;
  42. U32 m_width = 0;
  43. U32 m_height = 0;
  44. U32 m_depth = 0;
  45. U32 m_faceCount = 0;
  46. U32 m_layerCount = 0;
  47. U32 m_channelCount = 0;
  48. U32 m_pixelSize = 0; ///< Texel size of an uncompressed image.
  49. Bool m_hdr = false;
  50. ImageImporterContext(GenericMemoryPoolAllocator<U8> alloc)
  51. : m_mipmaps(alloc)
  52. {
  53. }
  54. GenericMemoryPoolAllocator<U8> getAllocator() const
  55. {
  56. return m_mipmaps.getAllocator();
  57. }
  58. };
  59. class DdsPixelFormat
  60. {
  61. public:
  62. U32 m_dwSize;
  63. U32 m_dwFlags;
  64. Array<char, 4> m_dwFourCC;
  65. U32 m_dwRGBBitCount;
  66. U32 m_dwRBitMask;
  67. U32 m_dwGBitMask;
  68. U32 m_dwBBitMask;
  69. U32 m_dwABitMask;
  70. };
  71. class DdsHeader
  72. {
  73. public:
  74. Array<U8, 4> m_magic;
  75. U32 m_dwSize;
  76. U32 m_dwFlags;
  77. U32 m_dwHeight;
  78. U32 m_dwWidth;
  79. U32 m_dwPitchOrLinearSize;
  80. U32 m_dwDepth;
  81. U32 m_dwMipMapCount;
  82. Array<U32, 11> m_dwReserved1;
  83. DdsPixelFormat m_ddspf;
  84. U32 m_dwCaps;
  85. U32 m_dwCaps2;
  86. U32 m_dwCaps3;
  87. U32 m_dwCaps4;
  88. U32 m_dwReserved2;
  89. };
  90. enum D3D10ResourceDimension
  91. {
  92. D3D10_RESOURCE_DIMENSION_UNKNOWN = 0,
  93. D3D10_RESOURCE_DIMENSION_BUFFER = 1,
  94. D3D10_RESOURCE_DIMENSION_TEXTURE1D = 2,
  95. D3D10_RESOURCE_DIMENSION_TEXTURE2D = 3,
  96. D3D10_RESOURCE_DIMENSION_TEXTURE3D = 4
  97. };
  98. /// Extra header for some DDS formats.
  99. class DdsHeaderDxt10
  100. {
  101. public:
  102. U32 m_dxgiFormat;
  103. D3D10ResourceDimension m_resourceDimension;
  104. U32 m_miscFlag;
  105. U32 m_arraySize;
  106. U32 m_reserved;
  107. };
  108. class AstcHeader
  109. {
  110. public:
  111. Array<U8, 4> m_magic;
  112. U8 m_blockX;
  113. U8 m_blockY;
  114. U8 m_blockZ;
  115. Array<U8, 3> m_dimX;
  116. Array<U8, 3> m_dimY;
  117. Array<U8, 3> m_dimZ;
  118. };
  119. /// Simple class to delete a file when it goes out of scope.
  120. class CleanupFile
  121. {
  122. public:
  123. StringAuto m_fileToDelete;
  124. CleanupFile(GenericMemoryPoolAllocator<U8> alloc, CString filename)
  125. : m_fileToDelete(alloc, filename)
  126. {
  127. }
  128. ~CleanupFile()
  129. {
  130. if(!m_fileToDelete.isEmpty())
  131. {
  132. const Error err = removeFile(m_fileToDelete);
  133. if(!err)
  134. {
  135. ANKI_IMPORTER_LOGV("Deleted %s", m_fileToDelete.cstr());
  136. }
  137. }
  138. }
  139. };
  140. } // namespace
  141. static ANKI_USE_RESULT Error checkConfig(const ImageImporterConfig& config)
  142. {
  143. #define ANKI_CFG_ASSERT(x, message) \
  144. do \
  145. { \
  146. if(!(x)) \
  147. { \
  148. ANKI_IMPORTER_LOGE(message); \
  149. return Error::USER_DATA; \
  150. } \
  151. } while(false)
  152. // Filenames
  153. ANKI_CFG_ASSERT(config.m_outFilename.getLength() > 0, "Empty output filename");
  154. for(CString in : config.m_inputFilenames)
  155. {
  156. ANKI_CFG_ASSERT(in.getLength() > 0, "Empty input filename");
  157. }
  158. // Type
  159. ANKI_CFG_ASSERT(config.m_type != ImageBinaryType::NONE, "Wrong image type");
  160. ANKI_CFG_ASSERT(config.m_inputFilenames.getSize() == 1 || config.m_type != ImageBinaryType::_2D,
  161. "2D images require only one input image");
  162. ANKI_CFG_ASSERT(config.m_inputFilenames.getSize() != 1 || config.m_type != ImageBinaryType::_2D_ARRAY,
  163. "2D array images require more than one input image");
  164. ANKI_CFG_ASSERT(config.m_inputFilenames.getSize() != 1 || config.m_type != ImageBinaryType::_3D,
  165. "3D images require more than one input image");
  166. ANKI_CFG_ASSERT(config.m_inputFilenames.getSize() != 6 || config.m_type != ImageBinaryType::CUBE,
  167. "Cube images require 6 input images");
  168. // Compressions
  169. ANKI_CFG_ASSERT(config.m_compressions != ImageBinaryDataCompression::NONE, "Missing output compressions");
  170. ANKI_CFG_ASSERT(config.m_compressions == ImageBinaryDataCompression::RAW || config.m_type != ImageBinaryType::_3D,
  171. "Can't compress 3D textures");
  172. // ASTC
  173. if(!!(config.m_compressions & ImageBinaryDataCompression::ASTC))
  174. {
  175. ANKI_CFG_ASSERT(config.m_astcBlockSize == UVec2(4u) || config.m_astcBlockSize == UVec2(8u),
  176. "Incorrect ASTC block sizes");
  177. }
  178. // Mip size
  179. ANKI_CFG_ASSERT(config.m_minMipmapDimension >= 4, "Mimpap min dimension can be less than 4");
  180. // Color conversions
  181. ANKI_CFG_ASSERT(!(config.m_linearToSRgb && config.m_sRgbToLinear),
  182. "Can't have a conversion to sRGB and to linear at the same time");
  183. #undef ANKI_CFG_ASSERT
  184. return Error::NONE;
  185. }
  186. static ANKI_USE_RESULT Error identifyImage(CString filename, U32& width, U32& height, U32& channelCount, Bool& hdr)
  187. {
  188. I32 iwidth, iheight, ichannelCount;
  189. const I ok = stbi_info(filename.cstr(), &iwidth, &iheight, &ichannelCount);
  190. if(!ok)
  191. {
  192. ANKI_IMPORTER_LOGE("STB failed to load file: %s", filename.cstr());
  193. return Error::FUNCTION_FAILED;
  194. }
  195. const I ihdr = stbi_is_hdr(filename.cstr());
  196. width = U32(iwidth);
  197. height = U32(iheight);
  198. channelCount = U32(ichannelCount);
  199. hdr = U32(ihdr);
  200. return Error::NONE;
  201. }
  202. static ANKI_USE_RESULT Error checkInputImages(const ImageImporterConfig& config, U32& width, U32& height,
  203. U32& channelCount, Bool& isHdr)
  204. {
  205. width = 0;
  206. height = 0;
  207. channelCount = 0;
  208. isHdr = false;
  209. for(U32 i = 0; i < config.m_inputFilenames.getSize(); ++i)
  210. {
  211. U32 nwidth, nheight, nchannelCount;
  212. Bool nhdr;
  213. ANKI_CHECK(identifyImage(config.m_inputFilenames[i], nwidth, nheight, nchannelCount, nhdr));
  214. if(i == 0)
  215. {
  216. width = nwidth;
  217. height = nheight;
  218. channelCount = nchannelCount;
  219. isHdr = nhdr;
  220. }
  221. else if(width != nwidth || height != nheight || channelCount != nchannelCount || isHdr != nhdr)
  222. {
  223. ANKI_IMPORTER_LOGE("Input image doesn't match previous input images: %s",
  224. config.m_inputFilenames[i].cstr());
  225. return Error::USER_DATA;
  226. }
  227. }
  228. ANKI_ASSERT(width > 0 && height > 0 && channelCount > 0);
  229. if(!isPowerOfTwo(width) || !isPowerOfTwo(height))
  230. {
  231. ANKI_IMPORTER_LOGE("Only power of two images are accepted");
  232. return Error::USER_DATA;
  233. }
  234. return Error::NONE;
  235. }
  236. static ANKI_USE_RESULT Error resizeImage(CString inImageFilename, U32 outWidth, U32 outHeight, CString tempDirectory,
  237. GenericMemoryPoolAllocator<U8> alloc, StringAuto& tmpFilename)
  238. {
  239. U32 inWidth, inHeight, channelCount;
  240. Bool hdr;
  241. ANKI_CHECK(identifyImage(inImageFilename, inWidth, inHeight, channelCount, hdr));
  242. // Load
  243. void* inPixels;
  244. if(!hdr)
  245. {
  246. I32 width, height;
  247. inPixels = stbi_load(inImageFilename.cstr(), &width, &height, nullptr, channelCount);
  248. }
  249. else
  250. {
  251. I32 width, height;
  252. inPixels = stbi_loadf(inImageFilename.cstr(), &width, &height, nullptr, channelCount);
  253. }
  254. if(!inPixels)
  255. {
  256. ANKI_IMPORTER_LOGE("STB load failed: %s", inImageFilename.cstr());
  257. return Error::FUNCTION_FAILED;
  258. }
  259. // Resize
  260. I ok;
  261. DynamicArrayAuto<U8> outPixels(alloc);
  262. if(!hdr)
  263. {
  264. outPixels.resize(outWidth * outHeight * channelCount);
  265. ok = stbir_resize_uint8(static_cast<const U8*>(inPixels), inWidth, inHeight, 0, outPixels.getBegin(), outWidth,
  266. outHeight, 0, channelCount);
  267. }
  268. else
  269. {
  270. outPixels.resize(outWidth * outHeight * channelCount * sizeof(F32));
  271. ok = stbir_resize_float(static_cast<const F32*>(inPixels), inWidth, inHeight, 0,
  272. reinterpret_cast<F32*>(outPixels.getBegin()), outWidth, outHeight, 0, channelCount);
  273. }
  274. stbi_image_free(inPixels);
  275. if(!ok)
  276. {
  277. ANKI_IMPORTER_LOGE("stbir_resize_xxx() failed to resize the image: %s", inImageFilename.cstr());
  278. return Error::FUNCTION_FAILED;
  279. }
  280. // Store
  281. tmpFilename.sprintf("%s/AnKiImageImporter_%u.%s", tempDirectory.cstr(), U32(std::rand()), (hdr) ? "exr" : "png");
  282. ANKI_IMPORTER_LOGV("Will store: %s", tmpFilename.cstr());
  283. if(!hdr)
  284. {
  285. ok = stbi_write_png(tmpFilename.cstr(), outWidth, outHeight, channelCount, outPixels.getBegin(), 0);
  286. }
  287. else
  288. {
  289. const I ret = SaveEXR(reinterpret_cast<const F32*>(outPixels.getBegin()), outWidth, outHeight, channelCount, 0,
  290. tmpFilename.cstr(), nullptr);
  291. ok = ret >= 0;
  292. }
  293. if(!ok)
  294. {
  295. ANKI_IMPORTER_LOGE("Failed to create: %s", tmpFilename.cstr());
  296. return Error::FUNCTION_FAILED;
  297. }
  298. return Error::NONE;
  299. }
  300. static Vec3 linearToSRgb(Vec3 p)
  301. {
  302. Vec3 cutoff;
  303. cutoff.x() = (p.x() < 0.0031308f) ? 1.0f : 0.0f;
  304. cutoff.y() = (p.y() < 0.0031308f) ? 1.0f : 0.0f;
  305. cutoff.z() = (p.z() < 0.0031308f) ? 1.0f : 0.0f;
  306. const Vec3 higher = 1.055f * p.pow(1.0f / 2.4f) - 0.055f;
  307. const Vec3 lower = p * 12.92f;
  308. p = higher.lerp(lower, cutoff);
  309. return p;
  310. }
  311. static Vec3 sRgbToLinear(Vec3 p)
  312. {
  313. Vec3 cutoff;
  314. cutoff.x() = (p.x() < 0.04045f) ? 1.0f : 0.0f;
  315. cutoff.y() = (p.y() < 0.04045f) ? 1.0f : 0.0f;
  316. cutoff.z() = (p.z() < 0.04045f) ? 1.0f : 0.0f;
  317. const Vec3 higher = ((p + 0.055f) / 1.055f).pow(2.4f);
  318. const Vec3 lower = p / 12.92f;
  319. return higher.lerp(lower, cutoff);
  320. }
  321. template<typename TVec, typename TFunc>
  322. static void linearToSRgbBatch(WeakArray<TVec> pixels, TFunc func)
  323. {
  324. using S = typename TVec::Scalar;
  325. for(TVec& pixel : pixels)
  326. {
  327. Vec3 p;
  328. if(std::is_same<S, U8>::value)
  329. {
  330. p.x() = F32(pixel.x()) / 255.0f;
  331. p.y() = F32(pixel.y()) / 255.0f;
  332. p.z() = F32(pixel.z()) / 255.0f;
  333. }
  334. else
  335. {
  336. ANKI_ASSERT((std::is_same<S, F32>::value));
  337. p.x() = F32(pixel.x());
  338. p.y() = F32(pixel.y());
  339. p.z() = F32(pixel.z());
  340. }
  341. p = func(p);
  342. if(std::is_same<S, U8>::value)
  343. {
  344. pixel.x() = S(p.x() / 255.0f);
  345. pixel.y() = S(p.y() / 255.0f);
  346. pixel.z() = S(p.z() / 255.0f);
  347. }
  348. else
  349. {
  350. pixel.x() = S(p.x());
  351. pixel.y() = S(p.y());
  352. pixel.z() = S(p.z());
  353. }
  354. }
  355. }
  356. static ANKI_USE_RESULT Error loadFirstMipmap(const ImageImporterConfig& config, ImageImporterContext& ctx)
  357. {
  358. GenericMemoryPoolAllocator<U8> alloc = ctx.getAllocator();
  359. ctx.m_mipmaps.emplaceBack(alloc);
  360. Mipmap& mip0 = ctx.m_mipmaps[0];
  361. if(ctx.m_depth > 1)
  362. {
  363. mip0.m_surfacesOrVolume.create(1, alloc);
  364. mip0.m_surfacesOrVolume[0].m_pixels.create(ctx.m_pixelSize * ctx.m_width * ctx.m_height * ctx.m_depth);
  365. }
  366. else
  367. {
  368. mip0.m_surfacesOrVolume.create(ctx.m_faceCount * ctx.m_layerCount, alloc);
  369. ANKI_ASSERT(mip0.m_surfacesOrVolume.getSize() == config.m_inputFilenames.getSize());
  370. for(U32 f = 0; f < ctx.m_faceCount; ++f)
  371. {
  372. for(U32 l = 0; l < ctx.m_layerCount; ++l)
  373. {
  374. mip0.m_surfacesOrVolume[l * ctx.m_faceCount + f].m_pixels.create(ctx.m_pixelSize * ctx.m_width
  375. * ctx.m_height);
  376. }
  377. }
  378. }
  379. for(U32 i = 0; i < config.m_inputFilenames.getSize(); ++i)
  380. {
  381. I32 width, height;
  382. stbi_set_flip_vertically_on_load_thread(config.m_flipImage);
  383. void* data;
  384. if(!ctx.m_hdr)
  385. {
  386. data = stbi_load(config.m_inputFilenames[i].cstr(), &width, &height, nullptr, ctx.m_channelCount);
  387. }
  388. else
  389. {
  390. data = stbi_loadf(config.m_inputFilenames[i].cstr(), &width, &height, nullptr, ctx.m_channelCount);
  391. }
  392. if(!data)
  393. {
  394. ANKI_IMPORTER_LOGE("STB load failed: %s", config.m_inputFilenames[i].cstr());
  395. return Error::FUNCTION_FAILED;
  396. }
  397. const PtrSize dataSize = PtrSize(ctx.m_width) * ctx.m_height * ctx.m_pixelSize;
  398. // To conversions in place
  399. if(config.m_linearToSRgb)
  400. {
  401. ANKI_IMPORTER_LOGV("Will convert linear to sRGB");
  402. if(ctx.m_channelCount == 3)
  403. {
  404. if(!ctx.m_hdr)
  405. {
  406. linearToSRgbBatch(WeakArray<U8Vec3>(static_cast<U8Vec3*>(data), ctx.m_width * ctx.m_height),
  407. linearToSRgb);
  408. }
  409. else
  410. {
  411. linearToSRgbBatch(WeakArray<Vec3>(static_cast<Vec3*>(data), ctx.m_width * ctx.m_height),
  412. linearToSRgb);
  413. }
  414. }
  415. else
  416. {
  417. ANKI_ASSERT(ctx.m_channelCount == 4);
  418. if(!ctx.m_hdr)
  419. {
  420. linearToSRgbBatch(WeakArray<U8Vec4>(static_cast<U8Vec4*>(data), ctx.m_width * ctx.m_height),
  421. linearToSRgb);
  422. }
  423. else
  424. {
  425. linearToSRgbBatch(WeakArray<Vec4>(static_cast<Vec4*>(data), ctx.m_width * ctx.m_height),
  426. linearToSRgb);
  427. }
  428. }
  429. }
  430. else if(config.m_sRgbToLinear)
  431. {
  432. ANKI_IMPORTER_LOGV("Will convert sRGB to linear");
  433. if(ctx.m_channelCount == 3)
  434. {
  435. if(!ctx.m_hdr)
  436. {
  437. linearToSRgbBatch(WeakArray<U8Vec3>(static_cast<U8Vec3*>(data), ctx.m_width * ctx.m_height),
  438. sRgbToLinear);
  439. }
  440. else
  441. {
  442. linearToSRgbBatch(WeakArray<Vec3>(static_cast<Vec3*>(data), ctx.m_width * ctx.m_height),
  443. sRgbToLinear);
  444. }
  445. }
  446. else
  447. {
  448. ANKI_ASSERT(ctx.m_channelCount == 4);
  449. if(!ctx.m_hdr)
  450. {
  451. linearToSRgbBatch(WeakArray<U8Vec4>(static_cast<U8Vec4*>(data), ctx.m_width * ctx.m_height),
  452. sRgbToLinear);
  453. }
  454. else
  455. {
  456. linearToSRgbBatch(WeakArray<Vec4>(static_cast<Vec4*>(data), ctx.m_width * ctx.m_height),
  457. sRgbToLinear);
  458. }
  459. }
  460. }
  461. if(ctx.m_depth > 1)
  462. {
  463. memcpy(mip0.m_surfacesOrVolume[0].m_pixels.getBegin() + i * dataSize, data, dataSize);
  464. }
  465. else
  466. {
  467. memcpy(mip0.m_surfacesOrVolume[i].m_pixels.getBegin(), data, dataSize);
  468. }
  469. stbi_image_free(data);
  470. }
  471. return Error::NONE;
  472. }
  473. template<typename TStorageVec>
  474. static void generateSurfaceMipmap(ConstWeakArray<U8, PtrSize> inBuffer, U32 inWidth, U32 inHeight,
  475. WeakArray<U8, PtrSize> outBuffer)
  476. {
  477. constexpr U32 channelCount = TStorageVec::getSize();
  478. using FVecType = typename std::conditional_t<channelCount == 3, Vec3, Vec4>;
  479. const ConstWeakArray<TStorageVec, PtrSize> inPixels(reinterpret_cast<const TStorageVec*>(&inBuffer[0]),
  480. inBuffer.getSizeInBytes() / sizeof(TStorageVec));
  481. WeakArray<TStorageVec, PtrSize> outPixels(reinterpret_cast<TStorageVec*>(&outBuffer[0]),
  482. outBuffer.getSizeInBytes() / sizeof(TStorageVec));
  483. const U32 outWidth = inWidth >> 1;
  484. const U32 outHeight = inHeight >> 1;
  485. for(U32 h = 0; h < outHeight; ++h)
  486. {
  487. for(U32 w = 0; w < outWidth; ++w)
  488. {
  489. // Gather input
  490. FVecType average(0.0f);
  491. for(U32 y = 0; y < 2; ++y)
  492. {
  493. for(U32 x = 0; x < 2; ++x)
  494. {
  495. const U32 idx = (h * 2 + y) * inWidth + (w * 2 + x);
  496. const TStorageVec inPixel = inPixels[idx];
  497. for(U32 c = 0; c < channelCount; ++c)
  498. {
  499. average[c] += F32(inPixel[c]) * 0.25f;
  500. }
  501. }
  502. }
  503. TStorageVec uaverage;
  504. for(U32 c = 0; c < channelCount; ++c)
  505. {
  506. uaverage[c] = typename TStorageVec::Scalar(average[c]);
  507. }
  508. // Store
  509. const U32 idx = h * outWidth + w;
  510. outPixels[idx] = uaverage;
  511. }
  512. }
  513. }
  514. static ANKI_USE_RESULT Error compressS3tc(GenericMemoryPoolAllocator<U8> alloc, CString tempDirectory,
  515. CString compressonatorFilename, ConstWeakArray<U8, PtrSize> inPixels,
  516. U32 inWidth, U32 inHeight, U32 channelCount, Bool hdr,
  517. WeakArray<U8, PtrSize> outPixels)
  518. {
  519. ANKI_ASSERT(inPixels.getSizeInBytes()
  520. == PtrSize(inWidth) * inHeight * channelCount * ((hdr) ? sizeof(F32) : sizeof(U8)));
  521. ANKI_ASSERT(inWidth > 0 && isPowerOfTwo(inWidth) && inHeight > 0 && isPowerOfTwo(inHeight));
  522. ANKI_ASSERT(outPixels.getSizeInBytes()
  523. == PtrSize((hdr || channelCount == 4) ? 16 : 8) * (inWidth / 4) * (inHeight / 4));
  524. // Create a PNG image to feed to the compressor
  525. StringAuto tmpFilename(alloc);
  526. tmpFilename.sprintf("%s/AnKiImageImporter_%u.%s", tempDirectory.cstr(), U32(std::rand()), (hdr) ? "exr" : "png");
  527. ANKI_IMPORTER_LOGV("Will store: %s", tmpFilename.cstr());
  528. Bool saveTmpImageOk = false;
  529. if(!hdr)
  530. {
  531. const I ok = stbi_write_png(tmpFilename.cstr(), inWidth, inHeight, channelCount, inPixels.getBegin(), 0);
  532. saveTmpImageOk = !!ok;
  533. }
  534. else
  535. {
  536. const I ret = SaveEXR(reinterpret_cast<const F32*>(inPixels.getBegin()), inWidth, inHeight, channelCount, 0,
  537. tmpFilename.cstr(), nullptr);
  538. saveTmpImageOk = ret >= 0;
  539. }
  540. if(!saveTmpImageOk)
  541. {
  542. ANKI_IMPORTER_LOGE("Failed to create: %s", tmpFilename.cstr());
  543. return Error::FUNCTION_FAILED;
  544. }
  545. CleanupFile tmpCleanup(alloc, tmpFilename);
  546. // Invoke the compressor process
  547. StringAuto ddsFilename(alloc);
  548. ddsFilename.sprintf("%s/AnKiImageImporter_%u.dds", tempDirectory.cstr(), U32(std::rand()));
  549. Process proc;
  550. Array<CString, 5> args;
  551. U32 argCount = 0;
  552. args[argCount++] = "-nomipmap";
  553. args[argCount++] = "-fd";
  554. args[argCount++] = (hdr) ? "BC6H" : ((channelCount == 3) ? "BC1" : "BC3");
  555. args[argCount++] = tmpFilename;
  556. args[argCount++] = ddsFilename;
  557. ANKI_IMPORTER_LOGV("Will invoke process: compressonatorcli %s %s %s %s %s", args[0].cstr(), args[1].cstr(),
  558. args[2].cstr(), args[3].cstr(), args[4].cstr());
  559. ANKI_CHECK(proc.start(compressonatorFilename, args));
  560. CleanupFile ddsCleanup(alloc, ddsFilename);
  561. ProcessStatus status;
  562. I32 exitCode;
  563. ANKI_CHECK(proc.wait(60.0_sec, &status, &exitCode));
  564. if(!(status == ProcessStatus::NOT_RUNNING && exitCode == 0))
  565. {
  566. StringAuto errStr(alloc);
  567. if(exitCode != 0)
  568. {
  569. ANKI_CHECK(proc.readFromStdout(errStr));
  570. }
  571. if(errStr.isEmpty())
  572. {
  573. errStr = "Unknown error";
  574. }
  575. ANKI_IMPORTER_LOGE("Invoking compressor process failed: %s", errStr.cstr());
  576. return Error::FUNCTION_FAILED;
  577. }
  578. // Read the DDS file
  579. File ddsFile;
  580. ANKI_CHECK(ddsFile.open(ddsFilename, FileOpenFlag::READ | FileOpenFlag::BINARY));
  581. DdsHeader ddsHeader;
  582. ANKI_CHECK(ddsFile.read(&ddsHeader, sizeof(DdsHeader)));
  583. if(!hdr && channelCount == 3 && memcmp(&ddsHeader.m_ddspf.m_dwFourCC[0], "DXT1", 4) != 0)
  584. {
  585. ANKI_IMPORTER_LOGE("Incorrect format. Expecting DXT1");
  586. return Error::FUNCTION_FAILED;
  587. }
  588. if(!hdr && channelCount == 4 && memcmp(&ddsHeader.m_ddspf.m_dwFourCC[0], "DXT5", 4) != 0)
  589. {
  590. ANKI_IMPORTER_LOGE("Incorrect format. Expecting DXT5");
  591. return Error::FUNCTION_FAILED;
  592. }
  593. if(hdr && memcmp(&ddsHeader.m_ddspf.m_dwFourCC[0], "DX10", 4) != 0)
  594. {
  595. ANKI_IMPORTER_LOGE("Incorrect format. Expecting BC6H");
  596. return Error::FUNCTION_FAILED;
  597. }
  598. if(ddsHeader.m_dwWidth != inWidth || ddsHeader.m_dwHeight != inHeight)
  599. {
  600. ANKI_IMPORTER_LOGE("Incorrect DDS image size");
  601. return Error::FUNCTION_FAILED;
  602. }
  603. if(hdr)
  604. {
  605. DdsHeaderDxt10 dxt10Header;
  606. ANKI_CHECK(ddsFile.read(&dxt10Header, sizeof(dxt10Header)));
  607. }
  608. ANKI_CHECK(ddsFile.read(outPixels.getBegin(), outPixels.getSizeInBytes()));
  609. return Error::NONE;
  610. }
  611. static ANKI_USE_RESULT Error compressAstc(GenericMemoryPoolAllocator<U8> alloc, CString tempDirectory,
  612. CString astcencFilename, ConstWeakArray<U8, PtrSize> inPixels, U32 inWidth,
  613. U32 inHeight, U32 inChannelCount, UVec2 blockSize, Bool hdr,
  614. WeakArray<U8, PtrSize> outPixels)
  615. {
  616. const PtrSize blockBytes = 16;
  617. (void)blockBytes;
  618. ANKI_ASSERT(inPixels.getSizeInBytes()
  619. == PtrSize(inWidth) * inHeight * inChannelCount * ((hdr) ? sizeof(F32) : sizeof(U8)));
  620. ANKI_ASSERT(inWidth > 0 && isPowerOfTwo(inWidth) && inHeight > 0 && isPowerOfTwo(inHeight));
  621. ANKI_ASSERT(outPixels.getSizeInBytes() == blockBytes * (inWidth / blockSize.x()) * (inHeight / blockSize.y()));
  622. // Create a BMP image to feed to the astcebc
  623. StringAuto tmpFilename(alloc);
  624. tmpFilename.sprintf("%s/AnKiImageImporter_%u.%s", tempDirectory.cstr(), U32(std::rand()), (hdr) ? "exr" : "png");
  625. ANKI_IMPORTER_LOGV("Will store: %s", tmpFilename.cstr());
  626. Bool saveTmpImageOk = false;
  627. if(!hdr)
  628. {
  629. const I ok = stbi_write_png(tmpFilename.cstr(), inWidth, inHeight, inChannelCount, inPixels.getBegin(), 0);
  630. saveTmpImageOk = !!ok;
  631. }
  632. else
  633. {
  634. const I ret = SaveEXR(reinterpret_cast<const F32*>(inPixels.getBegin()), inWidth, inHeight, inChannelCount, 0,
  635. tmpFilename.cstr(), nullptr);
  636. saveTmpImageOk = ret >= 0;
  637. }
  638. if(!saveTmpImageOk)
  639. {
  640. ANKI_IMPORTER_LOGE("Failed to create: %s", tmpFilename.cstr());
  641. return Error::FUNCTION_FAILED;
  642. }
  643. CleanupFile pngCleanup(alloc, tmpFilename);
  644. // Invoke the compressor process
  645. StringAuto astcFilename(alloc);
  646. astcFilename.sprintf("%s/AnKiImageImporter_%u.astc", tempDirectory.cstr(), U32(std::rand()));
  647. StringAuto blockStr(alloc);
  648. blockStr.sprintf("%ux%u", blockSize.x(), blockSize.y());
  649. Process proc;
  650. Array<CString, 5> args;
  651. U32 argCount = 0;
  652. args[argCount++] = (!hdr) ? "-cl" : "-ch";
  653. args[argCount++] = tmpFilename;
  654. args[argCount++] = astcFilename;
  655. args[argCount++] = blockStr;
  656. args[argCount++] = "-fast";
  657. ANKI_IMPORTER_LOGV("Will invoke process: astcenc-avx2 %s %s %s %s %s", args[0].cstr(), args[1].cstr(),
  658. args[2].cstr(), args[3].cstr(), args[4].cstr());
  659. ANKI_CHECK(proc.start(astcencFilename, args));
  660. CleanupFile astcCleanup(alloc, astcFilename);
  661. ProcessStatus status;
  662. I32 exitCode;
  663. ANKI_CHECK(proc.wait(60.0_sec, &status, &exitCode));
  664. if(!(status == ProcessStatus::NOT_RUNNING && exitCode == 0))
  665. {
  666. StringAuto errStr(alloc);
  667. if(exitCode != 0)
  668. {
  669. ANKI_CHECK(proc.readFromStdout(errStr));
  670. }
  671. if(errStr.isEmpty())
  672. {
  673. errStr = "Unknown error";
  674. }
  675. ANKI_IMPORTER_LOGE("Invoking astcenc-avx2 process failed: %s", errStr.cstr());
  676. return Error::FUNCTION_FAILED;
  677. }
  678. // Read the astc file
  679. File astcFile;
  680. ANKI_CHECK(astcFile.open(astcFilename, FileOpenFlag::READ | FileOpenFlag::BINARY));
  681. AstcHeader header;
  682. ANKI_CHECK(astcFile.read(&header, sizeof(header)));
  683. auto unpackBytes = [](U8 a, U8 b, U8 c, U8 d) -> U32 {
  684. return (U32(a)) + (U32(b) << 8) + (U32(c) << 16) + (U32(d) << 24);
  685. };
  686. const U32 magicval = unpackBytes(header.m_magic[0], header.m_magic[1], header.m_magic[2], header.m_magic[3]);
  687. if(magicval != 0x5CA1AB13)
  688. {
  689. ANKI_IMPORTER_LOGE("astcenc-avx2 produced a file with wrong magic");
  690. return Error::FUNCTION_FAILED;
  691. }
  692. const U32 blockx = max<U32>(header.m_blockX, 1u);
  693. const U32 blocky = max<U32>(header.m_blockY, 1u);
  694. const U32 blockz = max<U32>(header.m_blockZ, 1u);
  695. if(blockx != blockSize.x() || blocky != blockSize.y() || blockz != 1)
  696. {
  697. ANKI_IMPORTER_LOGE("astcenc-avx2 with wrong block size");
  698. return Error::FUNCTION_FAILED;
  699. }
  700. const U32 dimx = unpackBytes(header.m_dimX[0], header.m_dimX[1], header.m_dimX[2], 0);
  701. const U32 dimy = unpackBytes(header.m_dimY[0], header.m_dimY[1], header.m_dimY[2], 0);
  702. const U32 dimz = unpackBytes(header.m_dimZ[0], header.m_dimZ[1], header.m_dimZ[2], 0);
  703. if(dimx != inWidth || dimy != inHeight || dimz != 1)
  704. {
  705. ANKI_IMPORTER_LOGE("astcenc-avx2 with wrong image size");
  706. return Error::FUNCTION_FAILED;
  707. }
  708. ANKI_CHECK(astcFile.read(outPixels.getBegin(), outPixels.getSizeInBytes()));
  709. return Error::NONE;
  710. }
  711. static ANKI_USE_RESULT Error storeAnkiImage(const ImageImporterConfig& config, const ImageImporterContext& ctx)
  712. {
  713. ANKI_IMPORTER_LOGV("Storing to %s", config.m_outFilename.cstr());
  714. File outFile;
  715. ANKI_CHECK(outFile.open(config.m_outFilename, FileOpenFlag::BINARY | FileOpenFlag::WRITE));
  716. // Header
  717. ImageBinaryHeader header = {};
  718. memcpy(&header.m_magic[0], &IMAGE_MAGIC[0], sizeof(header.m_magic));
  719. header.m_width = ctx.m_width;
  720. header.m_height = ctx.m_height;
  721. header.m_depthOrLayerCount = max(ctx.m_layerCount, ctx.m_depth);
  722. header.m_type = config.m_type;
  723. if(ctx.m_hdr)
  724. {
  725. header.m_colorFormat =
  726. (ctx.m_channelCount == 3) ? ImageBinaryColorFormat::RGBF32 : ImageBinaryColorFormat::RGBAF32;
  727. }
  728. else
  729. {
  730. header.m_colorFormat = (ctx.m_channelCount == 3) ? ImageBinaryColorFormat::RGB8 : ImageBinaryColorFormat::RGBA8;
  731. }
  732. header.m_compressionMask = config.m_compressions;
  733. header.m_isNormal = false;
  734. header.m_mipmapCount = ctx.m_mipmaps.getSize();
  735. header.m_astcBlockSizeX = config.m_astcBlockSize.x();
  736. header.m_astcBlockSizeY = config.m_astcBlockSize.y();
  737. ANKI_CHECK(outFile.write(&header, sizeof(header)));
  738. // Write RAW
  739. if(!!(config.m_compressions & ImageBinaryDataCompression::RAW))
  740. {
  741. ANKI_IMPORTER_LOGV("Storing RAW");
  742. // for(I32 mip = I32(ctx.m_mipmaps.getSize()) - 1; mip >= 0; --mip)
  743. for(U32 mip = 0; mip < ctx.m_mipmaps.getSize(); ++mip)
  744. {
  745. for(U32 l = 0; l < ctx.m_layerCount; ++l)
  746. {
  747. for(U32 f = 0; f < ctx.m_faceCount; ++f)
  748. {
  749. const U32 idx = l * ctx.m_faceCount + f;
  750. const ConstWeakArray<U8, PtrSize> pixels = ctx.m_mipmaps[mip].m_surfacesOrVolume[idx].m_pixels;
  751. ANKI_CHECK(outFile.write(&pixels[0], pixels.getSizeInBytes()));
  752. }
  753. }
  754. }
  755. }
  756. // Write S3TC
  757. if(!!(config.m_compressions & ImageBinaryDataCompression::S3TC))
  758. {
  759. ANKI_IMPORTER_LOGV("Storing S3TC");
  760. // for(I32 mip = I32(ctx.m_mipmaps.getSize()) - 1; mip >= 0; --mip)
  761. for(U32 mip = 0; mip < ctx.m_mipmaps.getSize(); ++mip)
  762. {
  763. for(U32 l = 0; l < ctx.m_layerCount; ++l)
  764. {
  765. for(U32 f = 0; f < ctx.m_faceCount; ++f)
  766. {
  767. const U32 idx = l * ctx.m_faceCount + f;
  768. const ConstWeakArray<U8, PtrSize> pixels = ctx.m_mipmaps[mip].m_surfacesOrVolume[idx].m_s3tcPixels;
  769. ANKI_CHECK(outFile.write(&pixels[0], pixels.getSizeInBytes()));
  770. }
  771. }
  772. }
  773. }
  774. // Write ASTC
  775. if(!!(config.m_compressions & ImageBinaryDataCompression::ASTC))
  776. {
  777. ANKI_IMPORTER_LOGV("Storing ASTC");
  778. // for(I32 mip = I32(ctx.m_mipmaps.getSize()) - 1; mip >= 0; --mip)
  779. for(U32 mip = 0; mip < ctx.m_mipmaps.getSize(); ++mip)
  780. {
  781. for(U32 l = 0; l < ctx.m_layerCount; ++l)
  782. {
  783. for(U32 f = 0; f < ctx.m_faceCount; ++f)
  784. {
  785. const U32 idx = l * ctx.m_faceCount + f;
  786. const ConstWeakArray<U8, PtrSize> pixels = ctx.m_mipmaps[mip].m_surfacesOrVolume[idx].m_astcPixels;
  787. ANKI_CHECK(outFile.write(&pixels[0], pixels.getSizeInBytes()));
  788. }
  789. }
  790. }
  791. }
  792. return Error::NONE;
  793. }
  794. static ANKI_USE_RESULT Error importImageInternal(const ImageImporterConfig& configOriginal)
  795. {
  796. GenericMemoryPoolAllocator<U8> alloc = configOriginal.m_allocator;
  797. ImageImporterConfig config = configOriginal;
  798. config.m_minMipmapDimension = max(config.m_minMipmapDimension, 4u);
  799. if(!!(config.m_compressions & ImageBinaryDataCompression::ASTC))
  800. {
  801. config.m_minMipmapDimension = max(config.m_minMipmapDimension, config.m_astcBlockSize.x());
  802. config.m_minMipmapDimension = max(config.m_minMipmapDimension, config.m_astcBlockSize.y());
  803. }
  804. // Checks
  805. ANKI_CHECK(checkConfig(config));
  806. U32 width, height, channelCount;
  807. Bool isHdr;
  808. ANKI_CHECK(checkInputImages(config, width, height, channelCount, isHdr));
  809. // Resize
  810. DynamicArrayAuto<StringAuto> newFilenames(alloc);
  811. DynamicArrayAuto<CString> newFilenamesCString(alloc);
  812. DynamicArrayAuto<CleanupFile> resizedImagesCleanup(alloc);
  813. if(width < config.m_minMipmapDimension || height < config.m_minMipmapDimension)
  814. {
  815. const U32 newWidth = max(width, config.m_minMipmapDimension);
  816. const U32 newHeight = max(height, config.m_minMipmapDimension);
  817. ANKI_IMPORTER_LOGV("Image is smaller than the min mipmap dimension. Will resize it to %ux%u", newWidth,
  818. newHeight);
  819. newFilenames.resize(config.m_inputFilenames.getSize(), StringAuto(alloc));
  820. newFilenamesCString.resize(config.m_inputFilenames.getSize());
  821. for(U32 i = 0; i < config.m_inputFilenames.getSize(); ++i)
  822. {
  823. ANKI_CHECK(resizeImage(config.m_inputFilenames[i], newWidth, newHeight, config.m_tempDirectory, alloc,
  824. newFilenames[i]));
  825. newFilenamesCString[i] = newFilenames[i];
  826. resizedImagesCleanup.emplaceBack(alloc, newFilenames[i]);
  827. }
  828. // Override config
  829. config.m_inputFilenames = newFilenamesCString;
  830. width = newWidth;
  831. height = newHeight;
  832. }
  833. // Init image
  834. ImageImporterContext ctx(alloc);
  835. ctx.m_width = width;
  836. ctx.m_height = height;
  837. ctx.m_depth = (config.m_type == ImageBinaryType::_3D) ? config.m_inputFilenames.getSize() : 1;
  838. ctx.m_faceCount = (config.m_type == ImageBinaryType::CUBE) ? 6 : 1;
  839. ctx.m_layerCount = (config.m_type == ImageBinaryType::_2D_ARRAY) ? config.m_inputFilenames.getSize() : 1;
  840. ctx.m_hdr = isHdr;
  841. U32 desiredChannelCount;
  842. if(isHdr && !!(config.m_compressions & ImageBinaryDataCompression::S3TC))
  843. {
  844. // BC6H doesn't have a 4th channel
  845. if(channelCount != 3)
  846. {
  847. ANKI_IMPORTER_LOGW("Input images have alpha but that can't be supported with BC6H");
  848. }
  849. if(!!(config.m_compressions & ImageBinaryDataCompression::RAW))
  850. {
  851. ANKI_IMPORTER_LOGE("Can't support both BC6H (which is 3 component) and RAW which requires 4 compoments");
  852. return Error::USER_DATA;
  853. }
  854. desiredChannelCount = 3;
  855. }
  856. else if(!!(config.m_compressions & ImageBinaryDataCompression::RAW))
  857. {
  858. // Always ask for 4 components because desktop GPUs don't always like 3
  859. desiredChannelCount = 4;
  860. }
  861. else if(config.m_noAlpha || channelCount == 1)
  862. {
  863. // no alpha or 1 component grey
  864. desiredChannelCount = 3;
  865. }
  866. else if(channelCount == 2)
  867. {
  868. // grey with alpha
  869. desiredChannelCount = 4;
  870. }
  871. else
  872. {
  873. desiredChannelCount = channelCount;
  874. }
  875. ctx.m_channelCount = desiredChannelCount;
  876. ctx.m_pixelSize = ctx.m_channelCount * U32((isHdr) ? sizeof(F32) : sizeof(U8));
  877. // Load first mip from the files
  878. ANKI_CHECK(loadFirstMipmap(config, ctx));
  879. // Generate mipmaps
  880. const U32 mipCount =
  881. min(config.m_mipmapCount, (config.m_type == ImageBinaryType::_3D)
  882. ? computeMaxMipmapCount3d(width, height, ctx.m_depth, config.m_minMipmapDimension)
  883. : computeMaxMipmapCount2d(width, height, config.m_minMipmapDimension));
  884. for(U32 mip = 1; mip < mipCount; ++mip)
  885. {
  886. ctx.m_mipmaps.emplaceBack(alloc);
  887. if(config.m_type != ImageBinaryType::_3D)
  888. {
  889. ctx.m_mipmaps[mip].m_surfacesOrVolume.create(ctx.m_faceCount * ctx.m_layerCount, alloc);
  890. for(U32 l = 0; l < ctx.m_layerCount; ++l)
  891. {
  892. for(U32 f = 0; f < ctx.m_faceCount; ++f)
  893. {
  894. const U32 idx = l * ctx.m_faceCount + f;
  895. const SurfaceOrVolumeData& inSurface = ctx.m_mipmaps[mip - 1].m_surfacesOrVolume[idx];
  896. SurfaceOrVolumeData& outSurface = ctx.m_mipmaps[mip].m_surfacesOrVolume[idx];
  897. outSurface.m_pixels.create((ctx.m_width >> mip) * (ctx.m_height >> mip) * ctx.m_pixelSize);
  898. if(ctx.m_channelCount == 3)
  899. {
  900. if(ctx.m_hdr)
  901. {
  902. generateSurfaceMipmap<Vec3>(ConstWeakArray<U8, PtrSize>(inSurface.m_pixels),
  903. ctx.m_width >> (mip - 1), ctx.m_height >> (mip - 1),
  904. WeakArray<U8, PtrSize>(outSurface.m_pixels));
  905. }
  906. else
  907. {
  908. generateSurfaceMipmap<U8Vec3>(ConstWeakArray<U8, PtrSize>(inSurface.m_pixels),
  909. ctx.m_width >> (mip - 1), ctx.m_height >> (mip - 1),
  910. WeakArray<U8, PtrSize>(outSurface.m_pixels));
  911. }
  912. }
  913. else
  914. {
  915. ANKI_ASSERT(ctx.m_channelCount == 4);
  916. if(ctx.m_hdr)
  917. {
  918. generateSurfaceMipmap<Vec4>(ConstWeakArray<U8, PtrSize>(inSurface.m_pixels),
  919. ctx.m_width >> (mip - 1), ctx.m_height >> (mip - 1),
  920. WeakArray<U8, PtrSize>(outSurface.m_pixels));
  921. }
  922. else
  923. {
  924. generateSurfaceMipmap<U8Vec4>(ConstWeakArray<U8, PtrSize>(inSurface.m_pixels),
  925. ctx.m_width >> (mip - 1), ctx.m_height >> (mip - 1),
  926. WeakArray<U8, PtrSize>(outSurface.m_pixels));
  927. }
  928. }
  929. }
  930. }
  931. }
  932. else
  933. {
  934. ANKI_ASSERT(!"TODO");
  935. }
  936. }
  937. // Compress
  938. if(!!(config.m_compressions & ImageBinaryDataCompression::S3TC))
  939. {
  940. ANKI_IMPORTER_LOGV("Will compress in S3TC");
  941. for(U32 mip = 0; mip < mipCount; ++mip)
  942. {
  943. for(U32 l = 0; l < ctx.m_layerCount; ++l)
  944. {
  945. for(U32 f = 0; f < ctx.m_faceCount; ++f)
  946. {
  947. const U32 idx = l * ctx.m_faceCount + f;
  948. SurfaceOrVolumeData& surface = ctx.m_mipmaps[mip].m_surfacesOrVolume[idx];
  949. const U32 width = ctx.m_width >> mip;
  950. const U32 height = ctx.m_height >> mip;
  951. const PtrSize blockSize = (ctx.m_hdr || ctx.m_channelCount == 4) ? 16 : 8;
  952. const PtrSize s3tcImageSize = blockSize * (width / 4) * (height / 4);
  953. surface.m_s3tcPixels.create(s3tcImageSize);
  954. ANKI_CHECK(compressS3tc(alloc, config.m_tempDirectory, config.m_compressonatorFilename,
  955. ConstWeakArray<U8, PtrSize>(surface.m_pixels), width, height,
  956. ctx.m_channelCount, ctx.m_hdr,
  957. WeakArray<U8, PtrSize>(surface.m_s3tcPixels)));
  958. }
  959. }
  960. }
  961. }
  962. if(!!(config.m_compressions & ImageBinaryDataCompression::ASTC))
  963. {
  964. ANKI_IMPORTER_LOGV("Will compress in ASTC");
  965. for(U32 mip = 0; mip < mipCount; ++mip)
  966. {
  967. for(U32 l = 0; l < ctx.m_layerCount; ++l)
  968. {
  969. for(U32 f = 0; f < ctx.m_faceCount; ++f)
  970. {
  971. const U32 idx = l * ctx.m_faceCount + f;
  972. SurfaceOrVolumeData& surface = ctx.m_mipmaps[mip].m_surfacesOrVolume[idx];
  973. const U32 width = ctx.m_width >> mip;
  974. const U32 height = ctx.m_height >> mip;
  975. const PtrSize blockSize = 16;
  976. const PtrSize astcImageSize =
  977. blockSize * (width / config.m_astcBlockSize.x()) * (height / config.m_astcBlockSize.y());
  978. surface.m_astcPixels.create(astcImageSize);
  979. ANKI_CHECK(compressAstc(alloc, config.m_tempDirectory, config.m_astcencFilename,
  980. ConstWeakArray<U8, PtrSize>(surface.m_pixels), width, height,
  981. ctx.m_channelCount, config.m_astcBlockSize, ctx.m_hdr,
  982. WeakArray<U8, PtrSize>(surface.m_astcPixels)));
  983. }
  984. }
  985. }
  986. }
  987. if(!!(config.m_compressions & ImageBinaryDataCompression::ETC))
  988. {
  989. ANKI_ASSERT(!"TODO");
  990. }
  991. // Store the image
  992. ANKI_CHECK(storeAnkiImage(config, ctx));
  993. return Error::NONE;
  994. }
  995. Error importImage(const ImageImporterConfig& config)
  996. {
  997. const Error err = importImageInternal(config);
  998. if(err)
  999. {
  1000. ANKI_IMPORTER_LOGE("Image importing failed");
  1001. return err;
  1002. }
  1003. return Error::NONE;
  1004. }
  1005. } // end namespace anki