diff options
Diffstat (limited to '3rdparty/bimg/tools/texturec/texturec.cpp')
-rw-r--r-- | 3rdparty/bimg/tools/texturec/texturec.cpp | 161 |
1 files changed, 120 insertions, 41 deletions
diff --git a/3rdparty/bimg/tools/texturec/texturec.cpp b/3rdparty/bimg/tools/texturec/texturec.cpp index b9ea4a2291f..51025c614e3 100644 --- a/3rdparty/bimg/tools/texturec/texturec.cpp +++ b/3rdparty/bimg/tools/texturec/texturec.cpp @@ -1,9 +1,8 @@ /* - * Copyright 2011-2018 Branimir Karadzic. All rights reserved. + * Copyright 2011-2019 Branimir Karadzic. All rights reserved. * License: https://github.com/bkaradzic/bimg#license-bsd-2-clause */ -#include <stdio.h> #include <bx/allocator.h> #include <bx/readerwriter.h> #include <bx/endian.h> @@ -13,7 +12,7 @@ #include <bimg/encode.h> #if 0 -# define DBG(_format, ...) fprintf(stderr, "" _format "\n", ##__VA_ARGS__) +# define DBG(_format, ...) bx::printf("" _format "\n", ##__VA_ARGS__) #else # define DBG(...) BX_NOOP() #endif // DEBUG @@ -90,12 +89,8 @@ void imageRgba32fNormalize(void* _dst, uint32_t _width, uint32_t _height, uint32 const float* rgba = (const float*)&src[0]; for (uint32_t xx = 0; xx < _width; ++xx, rgba += 4, dst += 16) { - float xyz[3]; - - xyz[0] = rgba[0]; - xyz[1] = rgba[1]; - xyz[2] = rgba[2]; - bx::vec3Norm( (float*)dst, xyz); + const bx::Vec3 xyz = bx::load<bx::Vec3>(rgba); + bx::store(dst, bx::normalize(xyz) ); } } } @@ -161,12 +156,12 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData const bimg::ImageBlockInfo& inputBlockInfo = bimg::getBlockInfo(inputFormat); const bimg::ImageBlockInfo& outputBlockInfo = bimg::getBlockInfo(outputFormat); const uint32_t blockWidth = outputBlockInfo.blockWidth; - const uint32_t blockHeight = outputBlockInfo.blockHeight; - const uint32_t minBlockX = outputBlockInfo.minBlockX; - const uint32_t minBlockY = outputBlockInfo.minBlockY; - uint32_t outputWidth = bx::max(blockWidth * minBlockX, ( (input->m_width + blockWidth - 1) / blockWidth )*blockWidth); - uint32_t outputHeight = bx::max(blockHeight * minBlockY, ( (input->m_height + blockHeight - 1) / blockHeight)*blockHeight); - uint32_t outputDepth = input->m_depth; + const uint32_t blockHeight = outputBlockInfo.blockHeight; + const uint32_t minBlockX = outputBlockInfo.minBlockX; + const uint32_t minBlockY = outputBlockInfo.minBlockY; + uint32_t outputWidth = bx::max(blockWidth * minBlockX, ( (input->m_width + blockWidth - 1) / blockWidth )*blockWidth); + uint32_t outputHeight = bx::max(blockHeight * minBlockY, ( (input->m_height + blockHeight - 1) / blockHeight)*blockHeight); + uint32_t outputDepth = input->m_depth; if (_options.mips && _options.mipSkip != 0) @@ -454,6 +449,7 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData , rgba ); + bimg::Quality::Enum nmapQuality = bimg::Quality::Enum(_options.quality + bimg::Quality::NormalMapDefault); bimg::imageEncodeFromRgba32f(_allocator , dstData , rgbaDst @@ -461,7 +457,7 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData , dstMip.m_height , dstMip.m_depth , outputFormat - , _options.quality + , nmapQuality , _err ); @@ -493,7 +489,7 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData , dstMip.m_height , dstMip.m_depth , outputFormat - , _options.quality + , nmapQuality , _err ); } @@ -624,10 +620,10 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData , bimg::TextureFormat::R8 ); temp = BX_ALLOC(_allocator, size); - uint8_t* rgba = (uint8_t*)temp; + uint8_t* r8 = (uint8_t*)temp; bimg::imageDecodeToR8(_allocator - , rgba + , r8 , mip.m_data , mip.m_width , mip.m_height @@ -644,8 +640,85 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData , mip.m_width , mip.m_height , mip.m_width - , rgba + , r8 + ); + + if (_options.mips) { + const float alphaRef = 0.5f; + float coverage = bimg::imageAlphaTestCoverage(bimg::TextureFormat::A8 + , mip.m_width + , mip.m_height + , mip.m_width + , r8 + , alphaRef + ); + + size = bimg::imageGetSize( + NULL + , uint16_t(dstMip.m_width) + , uint16_t(dstMip.m_height) + , uint16_t(dstMip.m_depth) + , false + , false + , 1 + , bimg::TextureFormat::RGBA8 ); + void* rgbaTemp = BX_ALLOC(_allocator, size); + uint8_t* rgba = (uint8_t*)rgbaTemp; + + bimg::imageDecodeToRgba8( + _allocator + , rgba + , dstMip.m_data + , dstMip.m_width + , dstMip.m_height + , dstMip.m_width * 4 + , bimg::TextureFormat::A8 + ); + + for (uint8_t lod = 1; lod < numMips && _err->isOk(); ++lod) { + bimg::imageRgba8Downsample2x2(rgba + , dstMip.m_width + , dstMip.m_height + , dstMip.m_depth + , dstMip.m_width * 4 + , bx::strideAlign(dstMip.m_width / 2, blockWidth) * 4 + , rgba + ); + + // For each mip, upscale to original size, + // scale image alpha to get same coverage as mip0 + uint32_t upsample = 1 << lod; + uint32_t destWidth = dstMip.m_width / 2; + uint32_t destHeight = dstMip.m_height / 2; + bimg::imageScaleAlphaToCoverage(bimg::TextureFormat::RGBA8 + , destWidth + , destHeight + , destWidth * 4 + , rgba + , coverage + , alphaRef + , upsample + ); + + bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip); + dstData = const_cast<uint8_t*>(dstMip.m_data); + + bimg::imageEncodeFromRgba8( + _allocator + , dstData + , rgba + , dstMip.m_width + , dstMip.m_height + , dstMip.m_depth + , bimg::TextureFormat::A8 + , _options.quality + , _err + ); + } + + BX_FREE(_allocator, rgbaTemp); + } } // RGBA8 else @@ -787,7 +860,7 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData , uint16_t(mip.m_height) ); - printf("%f\n", result); + bx::printf("%f\n", result); BX_FREE(_allocator, ref); } @@ -814,7 +887,7 @@ void help(const char* _error = NULL, bool _showHelp = true) { if (NULL != _error) { - fprintf(stderr, "Error:\n%s\n\n", _error); + bx::printf("Error:\n%s\n\n", _error); if (!_showHelp) { @@ -822,17 +895,17 @@ void help(const char* _error = NULL, bool _showHelp = true) } } - fprintf(stderr - , "texturec, bgfx texture compiler tool, version %d.%d.%d.\n" - "Copyright 2011-2018 Branimir Karadzic. All rights reserved.\n" + bx::printf( + "texturec, bgfx texture compiler tool, version %d.%d.%d.\n" + "Copyright 2011-2019 Branimir Karadzic. All rights reserved.\n" "License: https://github.com/bkaradzic/bimg#license-bsd-2-clause\n\n" , BIMG_TEXTUREC_VERSION_MAJOR , BIMG_TEXTUREC_VERSION_MINOR , BIMG_API_VERSION ); - fprintf(stderr - , "Usage: texturec -f <in> -o <out> [-t <texture format>]\n" + bx::printf( + "Usage: texturec -f <in> -o <out> [-t <texture format>]\n" "\n" "Supported file formats:\n" @@ -846,7 +919,7 @@ void help(const char* _error = NULL, bool _showHelp = true) " *.png (input, output) Portable Network Graphics.\n" " *.psd (input) Photoshop Document.\n" " *.pvr (input) PowerVR.\n" - " *.tga (input) Targa.\n" + " *.tga (input) Truevision TGA.\n" "\n" "Options:\n" @@ -870,7 +943,7 @@ void help(const char* _error = NULL, bool _showHelp = true) " aspect ratio will be preserved.\n" " --radiance <model> Radiance cubemap filter. (Lighting model: Phong, PhongBrdf, Blinn, BlinnBrdf, GGX)\n" " --as <extension> Save as.\n" - " --formats List all supported formats.\n" + " --formats List all supported formats.\n" " --validate *DEBUG* Validate that output image produced matches after loading.\n" "\n" @@ -923,8 +996,8 @@ int main(int _argc, const char* _argv[]) if (cmdLine.hasArg('v', "version") ) { - fprintf(stderr - , "texturec, bgfx texture compiler tool, version %d.%d.%d.\n" + bx::printf( + "texturec, bgfx texture compiler tool, version %d.%d.%d.\n" , BIMG_TEXTUREC_VERSION_MAJOR , BIMG_TEXTUREC_VERSION_MINOR , BIMG_API_VERSION @@ -940,20 +1013,26 @@ int main(int _argc, const char* _argv[]) if (cmdLine.hasArg("formats")) { - printf("Uncompressed formats:\n"); + bx::printf("Uncompressed formats:\n"); - for (int format = bimg::TextureFormat::Unknown + 1; format < bimg::TextureFormat::UnknownDepth; format++) - printf(" %s\n", bimg::getName((bimg::TextureFormat::Enum) format)); + for (int format = bimg::TextureFormat::Unknown + 1; format < bimg::TextureFormat::UnknownDepth; format++) + { + bx::printf(" %s\n", bimg::getName((bimg::TextureFormat::Enum) format)); + } - for (int format = bimg::TextureFormat::UnknownDepth + 1; format < bimg::TextureFormat::Count; format++) - printf(" %s\n", bimg::getName((bimg::TextureFormat::Enum) format)); + for (int format = bimg::TextureFormat::UnknownDepth + 1; format < bimg::TextureFormat::Count; format++) + { + bx::printf(" %s\n", bimg::getName((bimg::TextureFormat::Enum) format)); + } - printf("Compressed formats:\n"); + bx::printf("Compressed formats:\n"); - for (int format = 0; format < bimg::TextureFormat::Unknown; format++) - printf(" %s\n", bimg::getName((bimg::TextureFormat::Enum) format)); + for (int format = 0; format < bimg::TextureFormat::Unknown; format++) + { + bx::printf(" %s\n", bimg::getName((bimg::TextureFormat::Enum) format)); + } - return bx::kExitSuccess; + return bx::kExitSuccess; } const char* inputFileName = cmdLine.findOption('f'); @@ -1289,7 +1368,7 @@ int main(int _argc, const char* _argv[]) } else { - help(NULL, err); + help("Failed to create output", err); return bx::kExitFailure; } |