2
0

ImageImporter.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. // Copyright (C) 2009-2021, 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/Gr/Common.h>
  7. #include <AnKi/Resource/Stb.h>
  8. #include <AnKi/Util/Process.h>
  9. #include <AnKi/Util/File.h>
  10. namespace anki {
  11. namespace {
  12. class SurfaceOrVolumeData
  13. {
  14. public:
  15. DynamicArrayAuto<U8, PtrSize> m_pixels;
  16. DynamicArrayAuto<U8, PtrSize> m_s3tcPixels;
  17. DynamicArrayAuto<U8, PtrSize> m_astcPixels;
  18. SurfaceOrVolumeData(GenericMemoryPoolAllocator<U8> alloc)
  19. : m_pixels(alloc)
  20. , m_s3tcPixels(alloc)
  21. , m_astcPixels(alloc)
  22. {
  23. }
  24. };
  25. class Mipmap
  26. {
  27. public:
  28. DynamicArrayAuto<SurfaceOrVolumeData> m_surfacesOrVolume;
  29. Mipmap(GenericMemoryPoolAllocator<U8> alloc)
  30. : m_surfacesOrVolume(alloc)
  31. {
  32. }
  33. };
  34. /// Image importer context.
  35. class ImageImporterContext
  36. {
  37. public:
  38. DynamicArrayAuto<Mipmap> m_mipmaps;
  39. U32 m_width = 0;
  40. U32 m_height = 0;
  41. U32 m_depth = 0;
  42. U32 m_faceCount = 0;
  43. U32 m_layerCount = 0;
  44. U32 m_channelCount = 0;
  45. U32 m_pixelSize = 0;
  46. ImageImporterContext(GenericMemoryPoolAllocator<U8> alloc)
  47. : m_mipmaps(alloc)
  48. {
  49. }
  50. GenericMemoryPoolAllocator<U8> getAllocator() const
  51. {
  52. return m_mipmaps.getAllocator();
  53. }
  54. };
  55. class DdsPixelFormat
  56. {
  57. public:
  58. U32 m_dwSize;
  59. U32 m_dwFlags;
  60. Array<char, 4> m_dwFourCC;
  61. U32 m_dwRGBBitCount;
  62. U32 m_dwRBitMask;
  63. U32 m_dwGBitMask;
  64. U32 m_dwBBitMask;
  65. U32 m_dwABitMask;
  66. };
  67. class DdsHeader
  68. {
  69. public:
  70. Array<U8, 4> m_magic;
  71. U32 m_dwSize;
  72. U32 m_dwFlags;
  73. U32 m_dwHeight;
  74. U32 m_dwWidth;
  75. U32 m_dwPitchOrLinearSize;
  76. U32 m_dwDepth;
  77. U32 m_dwMipMapCount;
  78. Array<U32, 11> m_dwReserved1;
  79. DdsPixelFormat m_ddspf;
  80. U32 m_dwCaps;
  81. U32 m_dwCaps2;
  82. U32 m_dwCaps3;
  83. U32 m_dwCaps4;
  84. U32 m_dwReserved2;
  85. };
  86. class AstcHeader
  87. {
  88. public:
  89. Array<U8, 4> m_magic;
  90. U8 m_blockX;
  91. U8 m_blockY;
  92. U8 m_blockZ;
  93. Array<U8, 3> m_dimX;
  94. Array<U8, 3> m_dimY;
  95. Array<U8, 3> m_dimZ;
  96. };
  97. /// Simple class to delete a file when it goes out of scope.
  98. class CleanupFile
  99. {
  100. public:
  101. StringAuto m_fileToDelete;
  102. CleanupFile(GenericMemoryPoolAllocator<U8> alloc, CString filename)
  103. : m_fileToDelete(alloc, filename)
  104. {
  105. }
  106. ~CleanupFile()
  107. {
  108. if(!m_fileToDelete.isEmpty())
  109. {
  110. const int err = std::remove(m_fileToDelete.cstr());
  111. if(err)
  112. {
  113. ANKI_IMPORTER_LOGE("Couldn't delete file: %s", m_fileToDelete.cstr());
  114. }
  115. ANKI_IMPORTER_LOGV("Deleted %s", m_fileToDelete.cstr());
  116. }
  117. }
  118. };
  119. } // namespace
  120. static ANKI_USE_RESULT Error checkConfig(const ImageImporterConfig& config)
  121. {
  122. #define ANKI_CFG_ASSERT(x, message) \
  123. do \
  124. { \
  125. if(!(x)) \
  126. { \
  127. ANKI_IMPORTER_LOGE(message); \
  128. return Error::USER_DATA; \
  129. } \
  130. } while(false)
  131. // Filenames
  132. ANKI_CFG_ASSERT(config.m_outFilename.getLength() > 0, "Empty output filename");
  133. for(CString in : config.m_inputFilenames)
  134. {
  135. ANKI_CFG_ASSERT(in.getLength() > 0, "Empty input filename");
  136. }
  137. // Type
  138. ANKI_CFG_ASSERT(config.m_type != ImageBinaryType::NONE, "Wrong image type");
  139. ANKI_CFG_ASSERT(config.m_inputFilenames.getSize() == 1 || config.m_type != ImageBinaryType::_2D,
  140. "2D images require only one input image");
  141. ANKI_CFG_ASSERT(config.m_inputFilenames.getSize() != 1 || config.m_type != ImageBinaryType::_2D_ARRAY,
  142. "2D array images require more than one input image");
  143. ANKI_CFG_ASSERT(config.m_inputFilenames.getSize() != 1 || config.m_type != ImageBinaryType::_3D,
  144. "3D images require more than one input image");
  145. ANKI_CFG_ASSERT(config.m_inputFilenames.getSize() != 6 || config.m_type != ImageBinaryType::CUBE,
  146. "Cube images require 6 input images");
  147. // Compressions
  148. ANKI_CFG_ASSERT(config.m_compressions != ImageBinaryDataCompression::NONE, "Missing output compressions");
  149. ANKI_CFG_ASSERT(config.m_compressions == ImageBinaryDataCompression::RAW || config.m_type != ImageBinaryType::_3D,
  150. "Can't compress 3D textures");
  151. // ASTC
  152. if(!!(config.m_compressions & ImageBinaryDataCompression::ASTC))
  153. {
  154. ANKI_CFG_ASSERT(config.m_astcBlockSize == UVec2(4u) || config.m_astcBlockSize == UVec2(8u),
  155. "Incorrect ASTC block sizes");
  156. }
  157. // Mip size
  158. ANKI_CFG_ASSERT(config.m_minMipmapDimension >= 4, "Mimpap min dimension can be less than 4");
  159. #undef ANKI_CFG_ASSERT
  160. return Error::NONE;
  161. }
  162. static ANKI_USE_RESULT Error checkInputImages(const ImageImporterConfig& config, U32& width, U32& height,
  163. U32& channelCount)
  164. {
  165. width = 0;
  166. height = 0;
  167. channelCount = 0;
  168. for(U32 i = 0; i < config.m_inputFilenames.getSize(); ++i)
  169. {
  170. I32 iwidth, iheight, ichannelCount;
  171. const I ok = stbi_info(config.m_inputFilenames[i].cstr(), &iwidth, &iheight, &ichannelCount);
  172. if(!ok)
  173. {
  174. ANKI_IMPORTER_LOGE("STB failed to load file: %s", config.m_inputFilenames[i].cstr());
  175. return Error::FUNCTION_FAILED;
  176. }
  177. if(width == 0)
  178. {
  179. width = U32(iwidth);
  180. height = U32(iheight);
  181. channelCount = U32(ichannelCount);
  182. }
  183. else if(width != U32(iwidth) || height != U32(iheight) || channelCount != U32(ichannelCount))
  184. {
  185. ANKI_IMPORTER_LOGE("Input image doesn't match previous input images: %s",
  186. config.m_inputFilenames[i].cstr());
  187. return Error::USER_DATA;
  188. }
  189. }
  190. ANKI_ASSERT(width > 0 && height > 0 && channelCount > 0);
  191. if(!isPowerOfTwo(width) || !isPowerOfTwo(height))
  192. {
  193. ANKI_IMPORTER_LOGE("Only power of two images are accepted");
  194. return Error::USER_DATA;
  195. }
  196. return Error::NONE;
  197. }
  198. static ANKI_USE_RESULT Error loadFirstMipmap(const ImageImporterConfig& config, ImageImporterContext& ctx)
  199. {
  200. GenericMemoryPoolAllocator<U8> alloc = ctx.getAllocator();
  201. ctx.m_mipmaps.emplaceBack(alloc);
  202. Mipmap& mip0 = ctx.m_mipmaps[0];
  203. if(ctx.m_depth > 1)
  204. {
  205. mip0.m_surfacesOrVolume.create(1, alloc);
  206. mip0.m_surfacesOrVolume[0].m_pixels.create(ctx.m_pixelSize * ctx.m_width * ctx.m_height * ctx.m_depth);
  207. }
  208. else
  209. {
  210. mip0.m_surfacesOrVolume.create(ctx.m_faceCount * ctx.m_layerCount, alloc);
  211. for(U32 f = 0; f < ctx.m_faceCount; ++f)
  212. {
  213. for(U32 l = 0; l < ctx.m_layerCount; ++l)
  214. {
  215. mip0.m_surfacesOrVolume[l * ctx.m_faceCount + f].m_pixels.create(ctx.m_pixelSize * ctx.m_width
  216. * ctx.m_height);
  217. }
  218. }
  219. }
  220. for(U32 i = 0; i < config.m_inputFilenames.getSize(); ++i)
  221. {
  222. I32 width, height, c;
  223. stbi_set_flip_vertically_on_load_thread(true);
  224. void* data = stbi_load(config.m_inputFilenames[i].cstr(), &width, &height, &c, ctx.m_channelCount);
  225. ANKI_ASSERT(U32(c) == ctx.m_channelCount);
  226. if(!data)
  227. {
  228. ANKI_IMPORTER_LOGE("STB load failed: %s", config.m_inputFilenames[i].cstr());
  229. return Error::FUNCTION_FAILED;
  230. }
  231. const PtrSize dataSize = PtrSize(ctx.m_width) * ctx.m_height * ctx.m_pixelSize;
  232. if(ctx.m_depth > 1)
  233. {
  234. memcpy(mip0.m_surfacesOrVolume.getBegin() + i * dataSize, data, dataSize);
  235. }
  236. else
  237. {
  238. for(U32 l = 0; l < ctx.m_layerCount; ++l)
  239. {
  240. for(U32 f = 0; f < ctx.m_faceCount; ++f)
  241. {
  242. memcpy(mip0.m_surfacesOrVolume[l * ctx.m_faceCount + f].m_pixels.getBegin(), data, dataSize);
  243. }
  244. }
  245. }
  246. stbi_image_free(data);
  247. }
  248. return Error::NONE;
  249. }
  250. template<U32 TCHANNEL_COUNT>
  251. static void generateSurfaceMipmap(ConstWeakArray<U8, PtrSize> inBuffer, U32 inWidth, U32 inHeight,
  252. WeakArray<U8, PtrSize> outBuffer)
  253. {
  254. using UVecType = typename std::conditional_t<TCHANNEL_COUNT == 3, U8Vec3, U8Vec4>;
  255. using FVecType = typename std::conditional_t<TCHANNEL_COUNT == 3, Vec3, Vec4>;
  256. const ConstWeakArray<UVecType, PtrSize> inPixels(reinterpret_cast<const UVecType*>(&inBuffer[0]),
  257. inBuffer.getSizeInBytes() / sizeof(UVecType));
  258. WeakArray<UVecType, PtrSize> outPixels(reinterpret_cast<UVecType*>(&outBuffer[0]),
  259. outBuffer.getSizeInBytes() / sizeof(UVecType));
  260. const U32 outWidth = inWidth >> 1;
  261. const U32 outHeight = inHeight >> 1;
  262. for(U32 h = 0; h < outHeight; ++h)
  263. {
  264. for(U32 w = 0; w < outWidth; ++w)
  265. {
  266. // Gather input
  267. FVecType average(0.0f);
  268. for(U32 y = 0; y < 2; ++y)
  269. {
  270. for(U32 x = 0; x < 2; ++x)
  271. {
  272. const U32 idx = (h * 2 + y) * inWidth + (w * 2 + x);
  273. const UVecType inPixel = inPixels[idx];
  274. for(U32 c = 0; c < TCHANNEL_COUNT; ++c)
  275. {
  276. average[c] += F32(inPixel[c]) / 255.0f * 0.25f;
  277. }
  278. }
  279. }
  280. UVecType uaverage;
  281. for(U32 c = 0; c < TCHANNEL_COUNT; ++c)
  282. {
  283. uaverage[c] = U8(average[c] * 255.0f);
  284. }
  285. // Store
  286. const U32 idx = h * outWidth + w;
  287. outPixels[idx] = uaverage;
  288. }
  289. }
  290. }
  291. static ANKI_USE_RESULT Error compressS3tc(GenericMemoryPoolAllocator<U8> alloc, CString tempDirectory,
  292. CString compressonatorPath, ConstWeakArray<U8, PtrSize> inPixels, U32 inWidth,
  293. U32 inHeight, U32 channelCount, WeakArray<U8, PtrSize> outPixels)
  294. {
  295. ANKI_ASSERT(inPixels.getSizeInBytes() == PtrSize(inWidth) * inHeight * channelCount);
  296. ANKI_ASSERT(inWidth > 0 && isPowerOfTwo(inWidth) && inHeight > 0 && isPowerOfTwo(inHeight));
  297. ANKI_ASSERT(outPixels.getSizeInBytes() == PtrSize((channelCount == 3) ? 8 : 16) * (inWidth / 4) * (inHeight / 4));
  298. // Create a PNG image to feed to the compressor
  299. StringAuto pngFilename(alloc);
  300. pngFilename.sprintf("%s/AnKiImageImporter_%u.png", tempDirectory.cstr(), U32(std::rand()));
  301. ANKI_IMPORTER_LOGV("Will store: %s", pngFilename.cstr());
  302. if(!stbi_write_png(pngFilename.cstr(), inWidth, inHeight, channelCount, inPixels.getBegin(), 0))
  303. {
  304. ANKI_IMPORTER_LOGE("STB failed to create: %s", pngFilename.cstr());
  305. return Error::FUNCTION_FAILED;
  306. }
  307. CleanupFile pngCleanup(alloc, pngFilename);
  308. // Invoke the compressor process
  309. StringAuto ddsFilename(alloc);
  310. ddsFilename.sprintf("%s/AnKiImageImporter_%u.dds", tempDirectory.cstr(), U32(std::rand()));
  311. Process proc;
  312. Array<CString, 5> args;
  313. U32 argCount = 0;
  314. args[argCount++] = "-nomipmap";
  315. args[argCount++] = "-fd";
  316. args[argCount++] = (channelCount == 3) ? "BC1" : "BC3";
  317. args[argCount++] = pngFilename;
  318. args[argCount++] = ddsFilename;
  319. ANKI_IMPORTER_LOGV("Will invoke process: CompressonatorCLI %s %s %s %s %s", args[0].cstr(), args[1].cstr(),
  320. args[2].cstr(), args[3].cstr(), args[4].cstr());
  321. ANKI_CHECK(proc.start("CompressonatorCLI", args,
  322. (compressonatorPath.isEmpty()) ? ConstWeakArray<CString>()
  323. : Array<CString, 2>{{"PATH", compressonatorPath}}));
  324. CleanupFile ddsCleanup(alloc, ddsFilename);
  325. ProcessStatus status;
  326. I32 exitCode;
  327. ANKI_CHECK(proc.wait(60.0, &status, &exitCode));
  328. if(status != ProcessStatus::NORMAL_EXIT || exitCode != 0)
  329. {
  330. StringAuto errStr(alloc);
  331. if(exitCode != 0)
  332. {
  333. ANKI_CHECK(proc.readFromStdout(errStr));
  334. }
  335. if(errStr.isEmpty())
  336. {
  337. errStr = "Unknown error";
  338. }
  339. ANKI_IMPORTER_LOGE("Invoking compressor process failed: %s", errStr.cstr());
  340. return Error::FUNCTION_FAILED;
  341. }
  342. // Read the DDS file
  343. File ddsFile;
  344. ANKI_CHECK(ddsFile.open(ddsFilename, FileOpenFlag::READ | FileOpenFlag::BINARY));
  345. DdsHeader ddsHeader;
  346. ANKI_CHECK(ddsFile.read(&ddsHeader, sizeof(DdsHeader)));
  347. if(channelCount == 3 && memcmp(&ddsHeader.m_ddspf.m_dwFourCC[0], "DXT1", 4) != 0)
  348. {
  349. ANKI_IMPORTER_LOGE("Incorrect format. Expecting DXT1");
  350. return Error::FUNCTION_FAILED;
  351. }
  352. if(channelCount == 4 && memcmp(&ddsHeader.m_ddspf.m_dwFourCC[0], "DXT5", 4) != 0)
  353. {
  354. ANKI_IMPORTER_LOGE("Incorrect format. Expecting DXT5");
  355. return Error::FUNCTION_FAILED;
  356. }
  357. if(ddsHeader.m_dwWidth != inWidth || ddsHeader.m_dwHeight != inHeight)
  358. {
  359. ANKI_IMPORTER_LOGE("Incorrect DDS image size");
  360. return Error::FUNCTION_FAILED;
  361. }
  362. ANKI_CHECK(ddsFile.read(outPixels.getBegin(), outPixels.getSizeInBytes()));
  363. return Error::NONE;
  364. }
  365. static ANKI_USE_RESULT Error compressAstc(GenericMemoryPoolAllocator<U8> alloc, CString tempDirectory,
  366. CString astcencPath, ConstWeakArray<U8, PtrSize> inPixels, U32 inWidth,
  367. U32 inHeight, U32 inChannelCount, UVec2 blockSize,
  368. WeakArray<U8, PtrSize> outPixels)
  369. {
  370. const PtrSize blockBytes = 16;
  371. (void)blockBytes;
  372. ANKI_ASSERT(inPixels.getSizeInBytes() == PtrSize(inWidth) * inHeight * inChannelCount);
  373. ANKI_ASSERT(inWidth > 0 && isPowerOfTwo(inWidth) && inHeight > 0 && isPowerOfTwo(inHeight));
  374. ANKI_ASSERT(outPixels.getSizeInBytes() == blockBytes * (inWidth / blockSize.x()) * (inHeight / blockSize.y()));
  375. // Create a BMP image to feed to the astcebc
  376. StringAuto pngFilename(alloc);
  377. pngFilename.sprintf("%s/AnKiImageImporter_%u.png", tempDirectory.cstr(), U32(std::rand()));
  378. ANKI_IMPORTER_LOGV("Will store: %s", pngFilename.cstr());
  379. if(!stbi_write_png(pngFilename.cstr(), inWidth, inHeight, inChannelCount, inPixels.getBegin(), 0))
  380. {
  381. ANKI_IMPORTER_LOGE("STB failed to create: %s", pngFilename.cstr());
  382. return Error::FUNCTION_FAILED;
  383. }
  384. CleanupFile pngCleanup(alloc, pngFilename);
  385. // Invoke the compressor process
  386. StringAuto astcFilename(alloc);
  387. astcFilename.sprintf("%s/AnKiImageImporter_%u.astc", tempDirectory.cstr(), U32(std::rand()));
  388. StringAuto blockStr(alloc);
  389. blockStr.sprintf("%ux%u", blockSize.x(), blockSize.y());
  390. Process proc;
  391. Array<CString, 5> args;
  392. U32 argCount = 0;
  393. args[argCount++] = "-cl";
  394. args[argCount++] = pngFilename;
  395. args[argCount++] = astcFilename;
  396. args[argCount++] = blockStr;
  397. args[argCount++] = "-fast";
  398. ANKI_IMPORTER_LOGV("Will invoke process: astcenc-avx2 %s %s %s %s %s", args[0].cstr(), args[1].cstr(),
  399. args[2].cstr(), args[3].cstr(), args[4].cstr());
  400. ANKI_CHECK(
  401. proc.start("astcenc-avx2", args,
  402. (astcencPath.isEmpty()) ? ConstWeakArray<CString>() : Array<CString, 2>{{"PATH", astcencPath}}));
  403. CleanupFile astcCleanup(alloc, astcFilename);
  404. ProcessStatus status;
  405. I32 exitCode;
  406. ANKI_CHECK(proc.wait(60.0, &status, &exitCode));
  407. if(status != ProcessStatus::NORMAL_EXIT || exitCode != 0)
  408. {
  409. StringAuto errStr(alloc);
  410. if(exitCode != 0)
  411. {
  412. ANKI_CHECK(proc.readFromStdout(errStr));
  413. }
  414. if(errStr.isEmpty())
  415. {
  416. errStr = "Unknown error";
  417. }
  418. ANKI_IMPORTER_LOGE("Invoking astcenc-avx2 process failed: %s", errStr.cstr());
  419. return Error::FUNCTION_FAILED;
  420. }
  421. // Read the astc file
  422. File astcFile;
  423. ANKI_CHECK(astcFile.open(astcFilename, FileOpenFlag::READ | FileOpenFlag::BINARY));
  424. AstcHeader header;
  425. ANKI_CHECK(astcFile.read(&header, sizeof(header)));
  426. auto unpackBytes = [](U8 a, U8 b, U8 c, U8 d) -> U32 {
  427. return (U32(a)) + (U32(b) << 8) + (U32(c) << 16) + (U32(d) << 24);
  428. };
  429. const U32 magicval = unpackBytes(header.m_magic[0], header.m_magic[1], header.m_magic[2], header.m_magic[3]);
  430. if(magicval != 0x5CA1AB13)
  431. {
  432. ANKI_IMPORTER_LOGE("astcenc-avx2 produced a file with wrong magic");
  433. return Error::FUNCTION_FAILED;
  434. }
  435. const U32 blockx = max<U32>(header.m_blockX, 1u);
  436. const U32 blocky = max<U32>(header.m_blockY, 1u);
  437. const U32 blockz = max<U32>(header.m_blockZ, 1u);
  438. if(blockx != blockSize.x() || blocky != blockSize.y() || blockz != 1)
  439. {
  440. ANKI_IMPORTER_LOGE("astcenc-avx2 with wrong block size");
  441. return Error::FUNCTION_FAILED;
  442. }
  443. const U32 dimx = unpackBytes(header.m_dimX[0], header.m_dimX[1], header.m_dimX[2], 0);
  444. const U32 dimy = unpackBytes(header.m_dimY[0], header.m_dimY[1], header.m_dimY[2], 0);
  445. const U32 dimz = unpackBytes(header.m_dimZ[0], header.m_dimZ[1], header.m_dimZ[2], 0);
  446. if(dimx != inWidth || dimy != inHeight || dimz != 1)
  447. {
  448. ANKI_IMPORTER_LOGE("astcenc-avx2 with wrong image size");
  449. return Error::FUNCTION_FAILED;
  450. }
  451. ANKI_CHECK(astcFile.read(outPixels.getBegin(), outPixels.getSizeInBytes()));
  452. return Error::NONE;
  453. }
  454. static ANKI_USE_RESULT Error storeAnkiImage(const ImageImporterConfig& config, const ImageImporterContext& ctx)
  455. {
  456. ANKI_IMPORTER_LOGV("Storing to %s", config.m_outFilename.cstr());
  457. File outFile;
  458. ANKI_CHECK(outFile.open(config.m_outFilename, FileOpenFlag::BINARY | FileOpenFlag::WRITE));
  459. // Header
  460. ImageBinaryHeader header = {};
  461. memcpy(&header.m_magic[0], &IMAGE_MAGIC[0], sizeof(header.m_magic));
  462. header.m_width = ctx.m_width;
  463. header.m_height = ctx.m_height;
  464. header.m_depthOrLayerCount = max(ctx.m_layerCount, ctx.m_depth);
  465. header.m_type = config.m_type;
  466. header.m_colorFormat = (ctx.m_channelCount == 3) ? ImageBinaryColorFormat::RGB8 : ImageBinaryColorFormat::RGBA8;
  467. header.m_compressionMask = config.m_compressions;
  468. header.m_isNormal = false;
  469. header.m_mipmapCount = ctx.m_mipmaps.getSize();
  470. header.m_astcBlockSizeX = config.m_astcBlockSize.x();
  471. header.m_astcBlockSizeY = config.m_astcBlockSize.y();
  472. ANKI_CHECK(outFile.write(&header, sizeof(header)));
  473. // Write RAW
  474. if(!!(config.m_compressions & ImageBinaryDataCompression::RAW))
  475. {
  476. ANKI_IMPORTER_LOGV("Storing RAW");
  477. // for(I32 mip = I32(ctx.m_mipmaps.getSize()) - 1; mip >= 0; --mip)
  478. for(U32 mip = 0; mip < ctx.m_mipmaps.getSize(); ++mip)
  479. {
  480. for(U32 l = 0; l < ctx.m_layerCount; ++l)
  481. {
  482. for(U32 f = 0; f < ctx.m_faceCount; ++f)
  483. {
  484. const U32 idx = l * ctx.m_faceCount + f;
  485. const ConstWeakArray<U8, PtrSize> pixels = ctx.m_mipmaps[mip].m_surfacesOrVolume[idx].m_pixels;
  486. ANKI_CHECK(outFile.write(&pixels[0], pixels.getSizeInBytes()));
  487. }
  488. }
  489. }
  490. }
  491. // Write S3TC
  492. if(!!(config.m_compressions & ImageBinaryDataCompression::S3TC))
  493. {
  494. ANKI_IMPORTER_LOGV("Storing S3TC");
  495. // for(I32 mip = I32(ctx.m_mipmaps.getSize()) - 1; mip >= 0; --mip)
  496. for(U32 mip = 0; mip < ctx.m_mipmaps.getSize(); ++mip)
  497. {
  498. for(U32 l = 0; l < ctx.m_layerCount; ++l)
  499. {
  500. for(U32 f = 0; f < ctx.m_faceCount; ++f)
  501. {
  502. const U32 idx = l * ctx.m_faceCount + f;
  503. const ConstWeakArray<U8, PtrSize> pixels = ctx.m_mipmaps[mip].m_surfacesOrVolume[idx].m_s3tcPixels;
  504. ANKI_CHECK(outFile.write(&pixels[0], pixels.getSizeInBytes()));
  505. }
  506. }
  507. }
  508. }
  509. // Write ASTC
  510. if(!!(config.m_compressions & ImageBinaryDataCompression::ASTC))
  511. {
  512. ANKI_IMPORTER_LOGV("Storing ASTC");
  513. // for(I32 mip = I32(ctx.m_mipmaps.getSize()) - 1; mip >= 0; --mip)
  514. for(U32 mip = 0; mip < ctx.m_mipmaps.getSize(); ++mip)
  515. {
  516. for(U32 l = 0; l < ctx.m_layerCount; ++l)
  517. {
  518. for(U32 f = 0; f < ctx.m_faceCount; ++f)
  519. {
  520. const U32 idx = l * ctx.m_faceCount + f;
  521. const ConstWeakArray<U8, PtrSize> pixels = ctx.m_mipmaps[mip].m_surfacesOrVolume[idx].m_astcPixels;
  522. ANKI_CHECK(outFile.write(&pixels[0], pixels.getSizeInBytes()));
  523. }
  524. }
  525. }
  526. }
  527. return Error::NONE;
  528. }
  529. static ANKI_USE_RESULT Error importImageInternal(const ImageImporterConfig& config)
  530. {
  531. // Checks
  532. ANKI_CHECK(checkConfig(config));
  533. U32 width, height, channelCount;
  534. ANKI_CHECK(checkInputImages(config, width, height, channelCount));
  535. // Init image
  536. GenericMemoryPoolAllocator<U8> alloc = config.m_allocator;
  537. ImageImporterContext ctx(alloc);
  538. ctx.m_width = width;
  539. ctx.m_height = height;
  540. ctx.m_depth = (config.m_type == ImageBinaryType::_3D) ? config.m_inputFilenames.getSize() : 1;
  541. ctx.m_faceCount = (config.m_type == ImageBinaryType::CUBE) ? 6 : 1;
  542. ctx.m_layerCount = (config.m_type == ImageBinaryType::_2D_ARRAY) ? config.m_inputFilenames.getSize() : 1;
  543. U32 desiredChannelCount;
  544. if(config.m_noAlpha || channelCount == 1)
  545. {
  546. // no alpha or 1 component grey
  547. desiredChannelCount = 3;
  548. }
  549. else if(channelCount == 2)
  550. {
  551. // grey with alpha
  552. desiredChannelCount = 4;
  553. }
  554. else
  555. {
  556. desiredChannelCount = channelCount;
  557. }
  558. ctx.m_channelCount = desiredChannelCount;
  559. ctx.m_pixelSize = ctx.m_channelCount;
  560. // Load first mip from the files
  561. ANKI_CHECK(loadFirstMipmap(config, ctx));
  562. // Generate mipmaps
  563. U32 minMipDimension = max(config.m_minMipmapDimension, 4u);
  564. if(!!(config.m_compressions & ImageBinaryDataCompression::ASTC))
  565. {
  566. minMipDimension = max(minMipDimension, config.m_astcBlockSize.x());
  567. minMipDimension = max(minMipDimension, config.m_astcBlockSize.y());
  568. }
  569. const U32 mipCount =
  570. min(config.m_mipmapCount, (config.m_type == ImageBinaryType::_3D)
  571. ? computeMaxMipmapCount3d(width, height, ctx.m_depth, minMipDimension)
  572. : computeMaxMipmapCount2d(width, height, minMipDimension));
  573. for(U32 mip = 1; mip < mipCount; ++mip)
  574. {
  575. ctx.m_mipmaps.emplaceBack(alloc);
  576. if(config.m_type != ImageBinaryType::_3D)
  577. {
  578. ctx.m_mipmaps[mip].m_surfacesOrVolume.create(ctx.m_faceCount * ctx.m_layerCount, alloc);
  579. for(U32 l = 0; l < ctx.m_layerCount; ++l)
  580. {
  581. for(U32 f = 0; f < ctx.m_faceCount; ++f)
  582. {
  583. const U32 idx = l * ctx.m_faceCount + f;
  584. const SurfaceOrVolumeData& inSurface = ctx.m_mipmaps[mip - 1].m_surfacesOrVolume[idx];
  585. SurfaceOrVolumeData& outSurface = ctx.m_mipmaps[mip].m_surfacesOrVolume[idx];
  586. outSurface.m_pixels.create((ctx.m_width >> mip) * (ctx.m_height >> mip) * ctx.m_pixelSize);
  587. if(ctx.m_channelCount == 3)
  588. {
  589. generateSurfaceMipmap<3>(ConstWeakArray<U8, PtrSize>(inSurface.m_pixels),
  590. ctx.m_width >> (mip - 1), ctx.m_height >> (mip - 1),
  591. WeakArray<U8, PtrSize>(outSurface.m_pixels));
  592. }
  593. else
  594. {
  595. ANKI_ASSERT(ctx.m_channelCount == 4);
  596. generateSurfaceMipmap<4>(ConstWeakArray<U8, PtrSize>(inSurface.m_pixels),
  597. ctx.m_width >> (mip - 1), ctx.m_height >> (mip - 1),
  598. WeakArray<U8, PtrSize>(outSurface.m_pixels));
  599. }
  600. }
  601. }
  602. }
  603. else
  604. {
  605. ANKI_ASSERT(!"TODO");
  606. }
  607. }
  608. // Compress
  609. if(!!(config.m_compressions & ImageBinaryDataCompression::S3TC))
  610. {
  611. ANKI_IMPORTER_LOGV("Will compress in S3TC");
  612. for(U32 mip = 0; mip < mipCount; ++mip)
  613. {
  614. for(U32 l = 0; l < ctx.m_layerCount; ++l)
  615. {
  616. for(U32 f = 0; f < ctx.m_faceCount; ++f)
  617. {
  618. const U32 idx = l * ctx.m_faceCount + f;
  619. SurfaceOrVolumeData& surface = ctx.m_mipmaps[mip].m_surfacesOrVolume[idx];
  620. const U32 width = ctx.m_width >> mip;
  621. const U32 height = ctx.m_height >> mip;
  622. const PtrSize blockSize = (ctx.m_channelCount == 3) ? 8 : 16;
  623. const PtrSize s3tcImageSize = blockSize * (width / 4) * (height / 4);
  624. surface.m_s3tcPixels.create(s3tcImageSize);
  625. ANKI_CHECK(compressS3tc(alloc, config.m_tempDirectory, config.m_compressonatorPath,
  626. ConstWeakArray<U8, PtrSize>(surface.m_pixels), width, height,
  627. ctx.m_channelCount, WeakArray<U8, PtrSize>(surface.m_s3tcPixels)));
  628. }
  629. }
  630. }
  631. }
  632. if(!!(config.m_compressions & ImageBinaryDataCompression::ASTC))
  633. {
  634. ANKI_IMPORTER_LOGV("Will compress in ASTC");
  635. for(U32 mip = 0; mip < mipCount; ++mip)
  636. {
  637. for(U32 l = 0; l < ctx.m_layerCount; ++l)
  638. {
  639. for(U32 f = 0; f < ctx.m_faceCount; ++f)
  640. {
  641. const U32 idx = l * ctx.m_faceCount + f;
  642. SurfaceOrVolumeData& surface = ctx.m_mipmaps[mip].m_surfacesOrVolume[idx];
  643. const U32 width = ctx.m_width >> mip;
  644. const U32 height = ctx.m_height >> mip;
  645. const PtrSize blockSize = 16;
  646. const PtrSize astcImageSize =
  647. blockSize * (width / config.m_astcBlockSize.x()) * (height / config.m_astcBlockSize.y());
  648. surface.m_astcPixels.create(astcImageSize);
  649. ANKI_CHECK(compressAstc(alloc, config.m_tempDirectory, config.m_astcencPath,
  650. ConstWeakArray<U8, PtrSize>(surface.m_pixels), width, height,
  651. ctx.m_channelCount, config.m_astcBlockSize,
  652. WeakArray<U8, PtrSize>(surface.m_astcPixels)));
  653. }
  654. }
  655. }
  656. }
  657. if(!!(config.m_compressions & ImageBinaryDataCompression::ETC))
  658. {
  659. ANKI_ASSERT(!"TODO");
  660. }
  661. // Store the image
  662. ANKI_CHECK(storeAnkiImage(config, ctx));
  663. return Error::NONE;
  664. }
  665. Error importImage(const ImageImporterConfig& config)
  666. {
  667. const Error err = importImageInternal(config);
  668. if(err)
  669. {
  670. ANKI_IMPORTER_LOGE("Image importing failed");
  671. return err;
  672. }
  673. return Error::NONE;
  674. }
  675. } // end namespace anki