diff options
Diffstat (limited to '3rdparty/bgfx/tools/texturec/texturec.cpp')
-rw-r--r-- | 3rdparty/bgfx/tools/texturec/texturec.cpp | 362 |
1 files changed, 306 insertions, 56 deletions
diff --git a/3rdparty/bgfx/tools/texturec/texturec.cpp b/3rdparty/bgfx/tools/texturec/texturec.cpp index 7c66cdd310c..81bccfeb6f6 100644 --- a/3rdparty/bgfx/tools/texturec/texturec.cpp +++ b/3rdparty/bgfx/tools/texturec/texturec.cpp @@ -13,9 +13,11 @@ #include "image.h" #include <libsquish/squish.h> #include <etc1/etc1.h> +#include <etc2/ProcessRGB.hpp> #include <nvtt/nvtt.h> #include <pvrtc/PvrTcEncoder.h> #include <tinyexr/tinyexr.h> +#include <edtaa3/edtaa3func.h> #define STB_IMAGE_IMPLEMENTATION #include <stb/stb_image.c> @@ -54,7 +56,7 @@ namespace bgfx ::free(mem); } - void imageEncodeFromRgba8(uint8_t* _dst, const uint8_t* _src, uint32_t _width, uint32_t _height, uint8_t _format) + bool imageEncodeFromRgba8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint8_t _format) { TextureFormat::Enum format = TextureFormat::Enum(_format); @@ -65,32 +67,52 @@ namespace bgfx case TextureFormat::BC3: case TextureFormat::BC4: case TextureFormat::BC5: - squish::CompressImage(_src, _width, _height, _dst + squish::CompressImage( (const uint8_t*)_src, _width, _height, _dst , format == TextureFormat::BC2 ? squish::kDxt3 : format == TextureFormat::BC3 ? squish::kDxt5 : format == TextureFormat::BC4 ? squish::kBc4 : format == TextureFormat::BC5 ? squish::kBc5 : squish::kDxt1 ); - break; + return true; case TextureFormat::BC6H: - nvtt::compressBC6H(_src, _width, _height, 4, _dst); - break; + nvtt::compressBC6H( (const uint8_t*)_src, _width, _height, 4, _dst); + return true; case TextureFormat::BC7: - nvtt::compressBC7(_src, _width, _height, 4, _dst); - break; + nvtt::compressBC7( (const uint8_t*)_src, _width, _height, 4, _dst); + return true; case TextureFormat::ETC1: - etc1_encode_image(_src, _width, _height, 4, _width*4, _dst); - break; + etc1_encode_image( (const uint8_t*)_src, _width, _height, 4, _width*4, (uint8_t*)_dst); + return true; case TextureFormat::ETC2: - case TextureFormat::ETC2A: - case TextureFormat::ETC2A1: - case TextureFormat::PTC12: - break; + { + const uint32_t blockWidth = (_width +3)/4; + const uint32_t blockHeight = (_height+3)/4; + const uint32_t pitch = _width*4; + const uint8_t* src = (const uint8_t*)_src; + uint64_t* dst = (uint64_t*)_dst; + for (uint32_t yy = 0; yy < blockHeight; ++yy) + { + for (uint32_t xx = 0; xx < blockWidth; ++xx) + { + uint8_t block[4*4*4]; + const uint8_t* ptr = &src[(yy*pitch+xx*4)*4]; + + for (uint32_t ii = 0; ii < 16; ++ii) + { // BGRx + memcpy(&block[ii*4], &ptr[(ii%4)*pitch + (ii&~3)], 4); + bx::xchg(block[ii*4+0], block[ii*4+2]); + } + + *dst++ = ProcessRGB_ETC2(block); + } + } + } + return true; case TextureFormat::PTC14: { @@ -98,14 +120,11 @@ namespace bgfx RgbBitmap bmp; bmp.width = _width; bmp.height = _height; - bmp.data = const_cast<uint8_t*>(_src); + bmp.data = (uint8_t*)const_cast<void*>(_src); PvrTcEncoder::EncodeRgb4Bpp(_dst, bmp); bmp.data = NULL; } - break; - - case TextureFormat::PTC12A: - break; + return true; case TextureFormat::PTC14A: { @@ -113,27 +132,186 @@ namespace bgfx RgbaBitmap bmp; bmp.width = _width; bmp.height = _height; - bmp.data = const_cast<uint8_t*>(_src); + bmp.data = (uint8_t*)const_cast<void*>(_src); PvrTcEncoder::EncodeRgba4Bpp(_dst, bmp); bmp.data = NULL; } - break; - - case TextureFormat::PTC22: - case TextureFormat::PTC24: - break; + return true; case TextureFormat::BGRA8: imageSwizzleBgra8(_width, _height, _width*4, _src, _dst); - break; + return true; case TextureFormat::RGBA8: memcpy(_dst, _src, _width*_height*4); - break; + return true; default: - break; + return imageConvert(_dst, format, _src, TextureFormat::RGBA8, _width, _height); + } + + return false; + } + + bool imageEncodeFromRgba32f(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint8_t _format) + { + TextureFormat::Enum format = TextureFormat::Enum(_format); + + const uint8_t* src = (const uint8_t*)_src; + + switch (format) + { + case TextureFormat::RGBA8: + { + uint8_t* dst = (uint8_t*)_dst; + for (uint32_t yy = 0; yy < _height; ++yy) + { + for (uint32_t xx = 0; xx < _width; ++xx) + { + const uint32_t offset = yy*_width + xx; + const float* input = (const float*)&src[offset * 16]; + uint8_t* output = &dst[offset * 4]; + output[0] = uint8_t(input[0]*255.0f + 0.5f); + output[1] = uint8_t(input[1]*255.0f + 0.5f); + output[2] = uint8_t(input[2]*255.0f + 0.5f); + output[3] = uint8_t(input[3]*255.0f + 0.5f); + } + } + } + return true; + + case TextureFormat::BC5: + { + uint8_t* temp = (uint8_t*)BX_ALLOC(_allocator, _width*_height*4); + for (uint32_t yy = 0; yy < _height; ++yy) + { + for (uint32_t xx = 0; xx < _width; ++xx) + { + const uint32_t offset = yy*_width + xx; + const float* input = (const float*)&src[offset * 16]; + uint8_t* output = &temp[offset * 4]; + output[0] = uint8_t(input[0]*255.0f + 0.5f); + output[1] = uint8_t(input[1]*255.0f + 0.5f); + output[2] = uint8_t(input[2]*255.0f + 0.5f); + output[3] = uint8_t(input[3]*255.0f + 0.5f); + } + } + + imageEncodeFromRgba8(_dst, temp, _width, _height, _format); + BX_FREE(_allocator, temp); + } + return true; + + default: + return imageConvert(_dst, format, _src, TextureFormat::RGBA32F, _width, _height); + } + + return false; + } + + void imageRgba32f11to01(void* _dst, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src) + { + const uint8_t* src = (const uint8_t*)_src; + uint8_t* dst = (uint8_t*)_dst; + + for (uint32_t yy = 0; yy < _height; ++yy) + { + for (uint32_t xx = 0; xx < _width; ++xx) + { + const uint32_t offset = yy*_pitch + xx * 16; + const float* input = (const float*)&src[offset]; + float* output = (float*)&dst[offset]; + output[0] = input[0]*0.5f + 0.5f; + output[1] = input[1]*0.5f + 0.5f; + output[2] = input[2]*0.5f + 0.5f; + output[3] = input[3]*0.5f + 0.5f; + } + } + } + + static void edtaa3(bx::AllocatorI* _allocator, double* _dst, uint32_t _width, uint32_t _height, double* _src) + { + const uint32_t numPixels = _width*_height; + + short* xdist = (short *)BX_ALLOC(_allocator, numPixels*sizeof(short) ); + short* ydist = (short *)BX_ALLOC(_allocator, numPixels*sizeof(short) ); + double* gx = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) ); + double* gy = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) ); + + ::computegradient(_src, _width, _height, gx, gy); + ::edtaa3(_src, gx, gy, _width, _height, xdist, ydist, _dst); + + for (uint32_t ii = 0; ii < numPixels; ++ii) + { + if (_dst[ii] < 0.0) + { + _dst[ii] = 0.0; + } } + + BX_FREE(_allocator, xdist); + BX_FREE(_allocator, ydist); + BX_FREE(_allocator, gx); + BX_FREE(_allocator, gy); + } + + inline double min(double _a, double _b) + { + return _a > _b ? _b : _a; + } + + inline double max(double _a, double _b) + { + return _a > _b ? _a : _b; + } + + inline double clamp(double _val, double _min, double _max) + { + return max(min(_val, _max), _min); + } + + void imageMakeDist(bx::AllocatorI* _allocator, void* _dst, uint32_t _width, uint32_t _height, uint32_t _pitch, float _edge, const void* _src) + { + const uint32_t numPixels = _width*_height; + + double* imgIn = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) ); + double* outside = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) ); + double* inside = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) ); + + for (uint32_t yy = 0; yy < _height; ++yy) + { + const uint8_t* src = (const uint8_t*)_src + yy*_pitch; + double* dst = &imgIn[yy*_width]; + for (uint32_t xx = 0; xx < _width; ++xx) + { + dst[xx] = double(src[xx])/255.0; + } + } + + edtaa3(_allocator, outside, _width, _height, imgIn); + + for (uint32_t ii = 0; ii < numPixels; ++ii) + { + imgIn[ii] = 1.0 - imgIn[ii]; + } + + edtaa3(_allocator, inside, _width, _height, imgIn); + + BX_FREE(_allocator, imgIn); + + uint8_t* dst = (uint8_t*)_dst; + + double edgeOffset = _edge*0.5; + double invEdge = 1.0/_edge; + + for (uint32_t ii = 0; ii < numPixels; ++ii) + { + double dist = clamp( ( (outside[ii] - inside[ii])+edgeOffset) * invEdge, 0.0, 1.0); + dst[ii] = 255-uint8_t(dist * 255.0); + } + + BX_FREE(_allocator, inside); + BX_FREE(_allocator, outside); } } // namespace bgfx @@ -168,6 +346,8 @@ void help(const char* _error = NULL) " -o <file path> Output file path (file will be written in KTX format).\n" " -t <format> Output format type (BC1/2/3/4/5, ETC1, PVR14, etc.).\n" " -m, --mips Generate mip-maps.\n" + " -n, --normalmap Input texture is normal map.\n" + " --sdf <edge> Compute SDF texture.\n" "\n" "For additional information, see https://github.com/bkaradzic/bgfx\n" @@ -198,14 +378,23 @@ int main(int _argc, const char* _argv[]) return EXIT_FAILURE; } + bool sdf = false; + double edge = 16.0; + const char* edgeOpt = cmdLine.findOption("sdf"); + if (NULL != edgeOpt) + { + sdf = true; + edge = atof(edgeOpt); + } + BX_UNUSED(sdf, edge); + bx::CrtFileReader reader; - if (0 != bx::open(&reader, inputFileName) ) + if (!bx::open(&reader, inputFileName) ) { help("Failed to open input file."); return EXIT_FAILURE; } - const bool mips = cmdLine.hasArg('m', "mips"); const char* type = cmdLine.findOption('t'); bgfx::TextureFormat::Enum format = bgfx::TextureFormat::BGRA8; @@ -220,6 +409,9 @@ int main(int _argc, const char* _argv[]) } } + const bool mips = cmdLine.hasArg('m', "mips"); + const bool normalMap = cmdLine.hasArg('n', "normalmap"); + uint32_t size = (uint32_t)bx::getSize(&reader); const bgfx::Memory* mem = bgfx::alloc(size); bx::read(&reader, mem->data, mem->size); @@ -271,46 +463,99 @@ int main(int _argc, const char* _argv[]) ImageMip mip; if (imageGetRawData(imageContainer, 0, 0, mem->data, mem->size, mip) ) { - uint32_t size = imageGetSize(TextureFormat::RGBA8, mip.m_width, mip.m_height); - uint8_t* rgba = (uint8_t*)BX_ALLOC(&allocator, size); - - imageDecodeToRgba8(rgba - , mip.m_data - , mip.m_width - , mip.m_height - , mip.m_width*mip.m_bpp/8 - , mip.m_format - ); - uint8_t numMips = mips ? imageGetNumMips(format, mip.m_width, mip.m_height) : 1 ; - imageContainer.m_size = imageGetSize(format, mip.m_width, mip.m_height, 0, false, numMips); - imageContainer.m_format = format; - output = alloc(imageContainer.m_size); - imageEncodeFromRgba8(output->data, rgba, mip.m_width, mip.m_height, format); + void* temp = NULL; + + if (normalMap) + { + uint32_t size = imageGetSize(TextureFormat::RGBA32F, mip.m_width, mip.m_height); + temp = BX_ALLOC(&allocator, size); + float* rgba = (float*)temp; + float* rgbaDst = (float*)BX_ALLOC(&allocator, size); + + imageDecodeToRgba32f(&allocator + , rgba + , mip.m_data + , mip.m_width + , mip.m_height + , mip.m_width*mip.m_bpp/8 + , mip.m_format + ); + + if (TextureFormat::BC5 != mip.m_format) + { + for (uint32_t yy = 0; yy < mip.m_height; ++yy) + { + for (uint32_t xx = 0; xx < mip.m_width; ++xx) + { + const uint32_t offset = (yy*mip.m_width + xx) * 4; + float* inout = &rgba[offset]; + inout[0] = inout[0] * 2.0f/255.0f - 1.0f; + inout[1] = inout[1] * 2.0f/255.0f - 1.0f; + inout[2] = inout[2] * 2.0f/255.0f - 1.0f; + inout[3] = inout[3] * 2.0f/255.0f - 1.0f; + } + } + } + + output = imageAlloc(imageContainer, format, mip.m_width, mip.m_height, 0, false, mips); + + imageRgba32f11to01(rgbaDst, mip.m_width, mip.m_height, mip.m_width*16, rgba); + imageEncodeFromRgba32f(&allocator, output->data, rgbaDst, mip.m_width, mip.m_height, format); - for (uint8_t lod = 1; lod < numMips; ++lod) + for (uint8_t lod = 1; lod < numMips; ++lod) + { + imageRgba32fDownsample2x2NormalMap(mip.m_width, mip.m_height, mip.m_width*16, rgba, rgba); + imageRgba32f11to01(rgbaDst, mip.m_width, mip.m_height, mip.m_width*16, rgba); + + ImageMip dstMip; + imageGetRawData(imageContainer, 0, lod, output->data, output->size, dstMip); + uint8_t* data = const_cast<uint8_t*>(dstMip.m_data); + imageEncodeFromRgba32f(&allocator, data, rgbaDst, dstMip.m_width, dstMip.m_height, format); + } + + BX_FREE(&allocator, rgbaDst); + } + else { - ImageMip mip1; - imageGetRawData(imageContainer, 0, lod, output->data, output->size, mip1); - uint8_t* data = const_cast<uint8_t*>(mip1.m_data); - - uint32_t width = bx::uint32_max(1, mip.m_width >>lod); - uint32_t height = bx::uint32_max(1, mip.m_height>>lod); - imageRgba8Downsample2x2(width, height, width*4, rgba, rgba); - imageEncodeFromRgba8(data, rgba, mip.m_width, mip.m_height, format); + uint32_t size = imageGetSize(TextureFormat::RGBA8, mip.m_width, mip.m_height); + temp = BX_ALLOC(&allocator, size); + uint8_t* rgba = (uint8_t*)temp; + + imageDecodeToRgba8(rgba + , mip.m_data + , mip.m_width + , mip.m_height + , mip.m_width*mip.m_bpp/8 + , mip.m_format + ); + + output = imageAlloc(imageContainer, format, mip.m_width, mip.m_height, 0, false, mips); + + imageEncodeFromRgba8(output->data, rgba, mip.m_width, mip.m_height, format); + + for (uint8_t lod = 1; lod < numMips; ++lod) + { + imageRgba8Downsample2x2(mip.m_width, mip.m_height, mip.m_width*4, rgba, rgba); + + ImageMip dstMip; + imageGetRawData(imageContainer, 0, lod, output->data, output->size, dstMip); + uint8_t* data = const_cast<uint8_t*>(dstMip.m_data); + imageEncodeFromRgba8(data, rgba, dstMip.m_width, dstMip.m_height, format); + } } - BX_FREE(&allocator, rgba); + BX_FREE(&allocator, temp); } if (NULL != output) { bx::CrtFileWriter writer; - if (0 == bx::open(&writer, outputFileName) ) + if (bx::open(&writer, outputFileName) ) { if (NULL != bx::stristr(outputFileName, ".ktx") ) { @@ -319,8 +564,13 @@ int main(int _argc, const char* _argv[]) bx::close(&writer); } + else + { + help("Failed to open output file."); + return EXIT_FAILURE; + } - release(output); + imageFree(output); } } |