0004-remove-tinydds-qoi.patch 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. diff --git a/thirdparty/basis_universal/encoder/basisu_enc.cpp b/thirdparty/basis_universal/encoder/basisu_enc.cpp
  2. index 2bf486a028..fff98e8301 100644
  3. --- a/thirdparty/basis_universal/encoder/basisu_enc.cpp
  4. +++ b/thirdparty/basis_universal/encoder/basisu_enc.cpp
  5. @@ -37,9 +37,6 @@
  6. #endif
  7. #include "basisu_miniz.h"
  8. -#define QOI_IMPLEMENTATION
  9. -#include "3rdparty/qoi.h"
  10. -
  11. #if defined(_WIN32)
  12. // For QueryPerformanceCounter/QueryPerformanceFrequency
  13. #define WIN32_LEAN_AND_MEAN
  14. @@ -408,16 +405,7 @@ namespace basisu
  15. bool load_qoi(const char* pFilename, image& img)
  16. {
  17. - qoi_desc desc;
  18. - clear_obj(desc);
  19. -
  20. - void* p = qoi_read(pFilename, &desc, 4);
  21. - if (!p)
  22. - return false;
  23. -
  24. - img.grant_ownership(static_cast<color_rgba *>(p), desc.width, desc.height);
  25. -
  26. - return true;
  27. + return false;
  28. }
  29. bool load_png(const uint8_t *pBuf, size_t buf_size, image &img, const char *pFilename)
  30. diff --git a/thirdparty/basis_universal/encoder/basisu_gpu_texture.cpp b/thirdparty/basis_universal/encoder/basisu_gpu_texture.cpp
  31. index 000869a533..648cfb47ae 100644
  32. --- a/thirdparty/basis_universal/encoder/basisu_gpu_texture.cpp
  33. +++ b/thirdparty/basis_universal/encoder/basisu_gpu_texture.cpp
  34. @@ -19,9 +19,6 @@
  35. #include "basisu_bc7enc.h"
  36. #include "../transcoder/basisu_astc_hdr_core.h"
  37. -#define TINYDDS_IMPLEMENTATION
  38. -#include "3rdparty/tinydds.h"
  39. -
  40. namespace basisu
  41. {
  42. //------------------------------------------------------------------------------------------------
  43. @@ -1980,207 +1977,7 @@ namespace basisu
  44. // and cubemap, cubemap mipmapped, and cubemap array mipmapped.
  45. bool write_dds_file(uint8_vec &dds_data, const basisu::vector<gpu_image_vec>& gpu_images, bool cubemap_flag, bool use_srgb_format)
  46. {
  47. - if (!gpu_images.size())
  48. - {
  49. - assert(0);
  50. - return false;
  51. - }
  52. -
  53. - // Sanity check the input
  54. - uint32_t slices = 1;
  55. - if (cubemap_flag)
  56. - {
  57. - if ((gpu_images.size() % 6) != 0)
  58. - {
  59. - assert(0);
  60. - return false;
  61. - }
  62. - slices = gpu_images.size() / 6;
  63. - }
  64. - else
  65. - {
  66. - slices = gpu_images.size();
  67. - }
  68. -
  69. - uint32_t width = 0, height = 0, total_levels = 0;
  70. - basisu::texture_format fmt = texture_format::cInvalidTextureFormat;
  71. -
  72. - // Sanity check the input for consistent # of dimensions and mip levels
  73. - for (uint32_t array_index = 0; array_index < gpu_images.size(); array_index++)
  74. - {
  75. - const gpu_image_vec& levels = gpu_images[array_index];
  76. -
  77. - if (!levels.size())
  78. - {
  79. - // Empty mip chain
  80. - assert(0);
  81. - return false;
  82. - }
  83. -
  84. - if (!array_index)
  85. - {
  86. - width = levels[0].get_pixel_width();
  87. - height = levels[0].get_pixel_height();
  88. - total_levels = (uint32_t)levels.size();
  89. - fmt = levels[0].get_format();
  90. - }
  91. - else
  92. - {
  93. - if ((width != levels[0].get_pixel_width()) ||
  94. - (height != levels[0].get_pixel_height()) ||
  95. - (total_levels != levels.size()))
  96. - {
  97. - // All cubemap/texture array faces must be the same dimension
  98. - assert(0);
  99. - return false;
  100. - }
  101. - }
  102. -
  103. - for (uint32_t level_index = 0; level_index < levels.size(); level_index++)
  104. - {
  105. - if (level_index)
  106. - {
  107. - if ((levels[level_index].get_pixel_width() != maximum<uint32_t>(1, levels[0].get_pixel_width() >> level_index)) ||
  108. - (levels[level_index].get_pixel_height() != maximum<uint32_t>(1, levels[0].get_pixel_height() >> level_index)))
  109. - {
  110. - // Malformed mipmap chain
  111. - assert(0);
  112. - return false;
  113. - }
  114. - }
  115. -
  116. - if (fmt != levels[level_index].get_format())
  117. - {
  118. - // All input textures must use the same GPU format
  119. - assert(0);
  120. - return false;
  121. - }
  122. - }
  123. - }
  124. -
  125. - // No mipmap levels
  126. - if (!total_levels)
  127. - {
  128. - assert(0);
  129. - return false;
  130. - }
  131. -
  132. - // Create the DDS mipmap level data
  133. - uint8_vec mipmaps[32];
  134. -
  135. - // See https://learn.microsoft.com/en-us/windows/win32/direct3ddds/dds-file-layout-for-cubic-environment-maps
  136. - // DDS cubemap organization is cubemap face 0 followed by all mips, then cubemap face 1 followed by all mips, etc.
  137. - // Unfortunately tinydds.h's writer doesn't handle this case correctly, so we work around it here.
  138. - // This also applies with 2D texture arrays, too. RenderDoc and ddsview (DirectXTex) views each type (cubemap array and 2D texture array) correctly.
  139. - // Also see "Using Texture Arrays in Direct3D 10/11":
  140. - // https://learn.microsoft.com/en-us/windows/win32/direct3ddds/dx-graphics-dds-pguide
  141. - for (uint32_t array_index = 0; array_index < gpu_images.size(); array_index++)
  142. - {
  143. - const gpu_image_vec& levels = gpu_images[array_index];
  144. -
  145. - for (uint32_t level_index = 0; level_index < levels.size(); level_index++)
  146. - {
  147. - append_vector(mipmaps[0], (uint8_t*)levels[level_index].get_ptr(), levels[level_index].get_size_in_bytes());
  148. -
  149. - } // level_index
  150. - } // array_index
  151. -
  152. -#if 0
  153. - // This organization, required by tinydds.h's API, is wrong.
  154. - {
  155. - for (uint32_t array_index = 0; array_index < gpu_images.size(); array_index++)
  156. - {
  157. - const gpu_image_vec& levels = gpu_images[array_index];
  158. -
  159. - for (uint32_t level_index = 0; level_index < levels.size(); level_index++)
  160. - {
  161. - append_vector(mipmaps[level_index], (uint8_t*)levels[level_index].get_ptr(), levels[level_index].get_size_in_bytes());
  162. -
  163. - } // level_index
  164. - } // array_index
  165. - }
  166. -#endif
  167. -
  168. - // Write DDS file using tinydds
  169. - TinyDDS_WriteCallbacks cbs;
  170. - cbs.error = [](void* user, char const* msg) { BASISU_NOTE_UNUSED(user); fprintf(stderr, "tinydds: %s\n", msg); };
  171. - cbs.alloc = [](void* user, size_t size) -> void* { BASISU_NOTE_UNUSED(user); return malloc(size); };
  172. - cbs.free = [](void* user, void* memory) { BASISU_NOTE_UNUSED(user); free(memory); };
  173. - cbs.write = [](void* user, void const* buffer, size_t byteCount) { BASISU_NOTE_UNUSED(user); uint8_vec* pVec = (uint8_vec*)user; append_vector(*pVec, (const uint8_t*)buffer, byteCount); };
  174. -
  175. - uint32_t mipmap_sizes[32];
  176. - const void* mipmap_ptrs[32];
  177. -
  178. - clear_obj(mipmap_sizes);
  179. - clear_obj(mipmap_ptrs);
  180. -
  181. - assert(total_levels < 32);
  182. - for (uint32_t i = 0; i < total_levels; i++)
  183. - {
  184. - mipmap_sizes[i] = mipmaps[i].size_in_bytes();
  185. - mipmap_ptrs[i] = mipmaps[i].get_ptr();
  186. - }
  187. -
  188. - // Select tinydds texture format
  189. - uint32_t tinydds_fmt = 0;
  190. -
  191. - switch (fmt)
  192. - {
  193. - case texture_format::cBC1_NV:
  194. - case texture_format::cBC1_AMD:
  195. - case texture_format::cBC1:
  196. - tinydds_fmt = use_srgb_format ? TDDS_BC1_RGBA_SRGB_BLOCK : TDDS_BC1_RGBA_UNORM_BLOCK;
  197. - break;
  198. - case texture_format::cBC3:
  199. - tinydds_fmt = use_srgb_format ? TDDS_BC3_SRGB_BLOCK : TDDS_BC3_UNORM_BLOCK;
  200. - break;
  201. - case texture_format::cBC4:
  202. - tinydds_fmt = TDDS_BC4_UNORM_BLOCK;
  203. - break;
  204. - case texture_format::cBC5:
  205. - tinydds_fmt = TDDS_BC5_UNORM_BLOCK;
  206. - break;
  207. - case texture_format::cBC6HSigned:
  208. - tinydds_fmt = TDDS_BC6H_SFLOAT_BLOCK;
  209. - break;
  210. - case texture_format::cBC6HUnsigned:
  211. - tinydds_fmt = TDDS_BC6H_UFLOAT_BLOCK;
  212. - break;
  213. - case texture_format::cBC7:
  214. - tinydds_fmt = use_srgb_format ? TDDS_BC7_SRGB_BLOCK : TDDS_BC7_UNORM_BLOCK;
  215. - break;
  216. - default:
  217. - {
  218. - fprintf(stderr, "Warning: Unsupported format in write_dds_file().\n");
  219. - return false;
  220. - }
  221. - }
  222. -
  223. - // DirectXTex's DDSView doesn't handle odd sizes textures correctly. RenderDoc loads them fine, however.
  224. - // Trying to work around this here results in invalid mipmaps.
  225. - //width = (width + 3) & ~3;
  226. - //height = (height + 3) & ~3;
  227. -
  228. - bool status = TinyDDS_WriteImage(&cbs,
  229. - &dds_data,
  230. - width,
  231. - height,
  232. - 1,
  233. - slices,
  234. - total_levels,
  235. - (TinyDDS_Format)tinydds_fmt,
  236. - cubemap_flag,
  237. - true,
  238. - mipmap_sizes,
  239. - mipmap_ptrs);
  240. -
  241. - if (!status)
  242. - {
  243. - fprintf(stderr, "write_dds_file: Failed creating DDS file\n");
  244. - return false;
  245. - }
  246. -
  247. - return true;
  248. + return false;
  249. }
  250. bool write_dds_file(const char* pFilename, const basisu::vector<gpu_image_vec>& gpu_images, bool cubemap_flag, bool use_srgb_format)
  251. @@ -2201,188 +1998,6 @@ namespace basisu
  252. bool read_uncompressed_dds_file(const char* pFilename, basisu::vector<image> &ldr_mips, basisu::vector<imagef>& hdr_mips)
  253. {
  254. - const uint32_t MAX_IMAGE_DIM = 16384;
  255. -
  256. - TinyDDS_Callbacks cbs;
  257. -
  258. - cbs.errorFn = [](void* user, char const* msg) { BASISU_NOTE_UNUSED(user); fprintf(stderr, "tinydds: %s\n", msg); };
  259. - cbs.allocFn = [](void* user, size_t size) -> void* { BASISU_NOTE_UNUSED(user); return malloc(size); };
  260. - cbs.freeFn = [](void* user, void* memory) { BASISU_NOTE_UNUSED(user); free(memory); };
  261. - cbs.readFn = [](void* user, void* buffer, size_t byteCount) -> size_t { return (size_t)fread(buffer, 1, byteCount, (FILE*)user); };
  262. -
  263. -#ifdef _MSC_VER
  264. - cbs.seekFn = [](void* user, int64_t ofs) -> bool { return _fseeki64((FILE*)user, ofs, SEEK_SET) == 0; };
  265. - cbs.tellFn = [](void* user) -> int64_t { return _ftelli64((FILE*)user); };
  266. -#else
  267. - cbs.seekFn = [](void* user, int64_t ofs) -> bool { return fseek((FILE*)user, (long)ofs, SEEK_SET) == 0; };
  268. - cbs.tellFn = [](void* user) -> int64_t { return (int64_t)ftell((FILE*)user); };
  269. -#endif
  270. -
  271. - FILE* pFile = fopen_safe(pFilename, "rb");
  272. - if (!pFile)
  273. - {
  274. - error_printf("Can't open .DDS file \"%s\"\n", pFilename);
  275. - return false;
  276. - }
  277. -
  278. - // These are the formats AMD Compressonator supports in its UI.
  279. - enum dds_fmt
  280. - {
  281. - cRGBA32,
  282. - cRGBA_HALF,
  283. - cRGBA_FLOAT
  284. - };
  285. -
  286. - bool status = false;
  287. - dds_fmt fmt = cRGBA32;
  288. - uint32_t width = 0, height = 0;
  289. - bool hdr_flag = false;
  290. - TinyDDS_Format tfmt = TDDS_UNDEFINED;
  291. -
  292. - TinyDDS_ContextHandle ctx = TinyDDS_CreateContext(&cbs, pFile);
  293. - if (!ctx)
  294. - goto failure;
  295. -
  296. - status = TinyDDS_ReadHeader(ctx);
  297. - if (!status)
  298. - {
  299. - error_printf("Failed parsing DDS header in file \"%s\"\n", pFilename);
  300. - goto failure;
  301. - }
  302. -
  303. - if ((!TinyDDS_Is2D(ctx)) || (TinyDDS_ArraySlices(ctx) > 1) || (TinyDDS_IsCubemap(ctx)))
  304. - {
  305. - error_printf("Unsupported DDS texture type in file \"%s\"\n", pFilename);
  306. - goto failure;
  307. - }
  308. -
  309. - width = TinyDDS_Width(ctx);
  310. - height = TinyDDS_Height(ctx);
  311. -
  312. - if (!width || !height)
  313. - {
  314. - error_printf("DDS texture dimensions invalid in file \"%s\"\n", pFilename);
  315. - goto failure;
  316. - }
  317. -
  318. - if ((width > MAX_IMAGE_DIM) || (height > MAX_IMAGE_DIM))
  319. - {
  320. - error_printf("DDS texture dimensions too large in file \"%s\"\n", pFilename);
  321. - goto failure;
  322. - }
  323. -
  324. - tfmt = TinyDDS_GetFormat(ctx);
  325. - switch (tfmt)
  326. - {
  327. - case TDDS_R8G8B8A8_SRGB:
  328. - case TDDS_R8G8B8A8_UNORM:
  329. - case TDDS_B8G8R8A8_SRGB:
  330. - case TDDS_B8G8R8A8_UNORM:
  331. - fmt = cRGBA32;
  332. - break;
  333. - case TDDS_R16G16B16A16_SFLOAT:
  334. - fmt = cRGBA_HALF;
  335. - hdr_flag = true;
  336. - break;
  337. - case TDDS_R32G32B32A32_SFLOAT:
  338. - fmt = cRGBA_FLOAT;
  339. - hdr_flag = true;
  340. - break;
  341. - default:
  342. - error_printf("File \"%s\" has an unsupported DDS texture format (only supports RGBA/BGRA 32bpp, RGBA HALF float, or RGBA FLOAT)\n", pFilename);
  343. - goto failure;
  344. - }
  345. -
  346. - if (hdr_flag)
  347. - hdr_mips.resize(TinyDDS_NumberOfMipmaps(ctx));
  348. - else
  349. - ldr_mips.resize(TinyDDS_NumberOfMipmaps(ctx));
  350. -
  351. - for (uint32_t level = 0; level < TinyDDS_NumberOfMipmaps(ctx); level++)
  352. - {
  353. - const uint32_t level_width = TinyDDS_MipMapReduce(width, level);
  354. - const uint32_t level_height = TinyDDS_MipMapReduce(height, level);
  355. - const uint32_t total_level_texels = level_width * level_height;
  356. -
  357. - const void* pImage = TinyDDS_ImageRawData(ctx, level);
  358. - const uint32_t image_size = TinyDDS_ImageSize(ctx, level);
  359. -
  360. - if (fmt == cRGBA32)
  361. - {
  362. - ldr_mips[level].resize(level_width, level_height);
  363. -
  364. - if ((ldr_mips[level].get_total_pixels() * sizeof(uint32_t) != image_size))
  365. - {
  366. - assert(0);
  367. - goto failure;
  368. - }
  369. -
  370. - memcpy(ldr_mips[level].get_ptr(), pImage, image_size);
  371. -
  372. - if ((tfmt == TDDS_B8G8R8A8_SRGB) || (tfmt == TDDS_B8G8R8A8_UNORM))
  373. - {
  374. - // Swap R and B components.
  375. - uint32_t *pTexels = (uint32_t *)ldr_mips[level].get_ptr();
  376. - for (uint32_t i = 0; i < total_level_texels; i++)
  377. - {
  378. - const uint32_t v = pTexels[i];
  379. - const uint32_t r = (v >> 16) & 0xFF;
  380. - const uint32_t b = v & 0xFF;
  381. - pTexels[i] = r | (b << 16) | (v & 0xFF00FF00);
  382. - }
  383. - }
  384. - }
  385. - else if (fmt == cRGBA_FLOAT)
  386. - {
  387. - hdr_mips[level].resize(level_width, level_height);
  388. -
  389. - if ((hdr_mips[level].get_total_pixels() * sizeof(float) * 4 != image_size))
  390. - {
  391. - assert(0);
  392. - goto failure;
  393. - }
  394. -
  395. - memcpy(hdr_mips[level].get_ptr(), pImage, image_size);
  396. - }
  397. - else if (fmt == cRGBA_HALF)
  398. - {
  399. - hdr_mips[level].resize(level_width, level_height);
  400. -
  401. - if ((hdr_mips[level].get_total_pixels() * sizeof(basist::half_float) * 4 != image_size))
  402. - {
  403. - assert(0);
  404. - goto failure;
  405. - }
  406. -
  407. - // Unpack half to float.
  408. - const basist::half_float* pSrc_comps = static_cast<const basist::half_float*>(pImage);
  409. - vec4F* pDst_texels = hdr_mips[level].get_ptr();
  410. -
  411. - for (uint32_t i = 0; i < total_level_texels; i++)
  412. - {
  413. - (*pDst_texels)[0] = basist::half_to_float(pSrc_comps[0]);
  414. - (*pDst_texels)[1] = basist::half_to_float(pSrc_comps[1]);
  415. - (*pDst_texels)[2] = basist::half_to_float(pSrc_comps[2]);
  416. - (*pDst_texels)[3] = basist::half_to_float(pSrc_comps[3]);
  417. -
  418. - pSrc_comps += 4;
  419. - pDst_texels++;
  420. - } // y
  421. - }
  422. - } // level
  423. -
  424. - TinyDDS_DestroyContext(ctx);
  425. - fclose(pFile);
  426. -
  427. - return true;
  428. -
  429. - failure:
  430. - if (ctx)
  431. - TinyDDS_DestroyContext(ctx);
  432. -
  433. - if (pFile)
  434. - fclose(pFile);
  435. -
  436. return false;
  437. }