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