ImageImporter.cpp 32 KB

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