From 7d226675ea024babd46c46cc0d270e7e168233d4 Mon Sep 17 00:00:00 2001 From: Robin Allen Date: Tue, 3 Mar 2020 22:14:59 +0000 Subject: [PATCH 1/2] Enable KTX sRGB formats - When parsing, set m_srgb flag on the ImageContainer if the image is in an sRGB format. - When writing, take a new srgb parameter. If set, use the appropriate sRGB format. --- include/bimg/bimg.h | 1 + src/image.cpp | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/include/bimg/bimg.h b/include/bimg/bimg.h index 0f1a986..7fe8a75 100644 --- a/include/bimg/bimg.h +++ b/include/bimg/bimg.h @@ -541,6 +541,7 @@ namespace bimg , uint32_t _depth , uint8_t _numMips , uint32_t _numLayers + , bool _srgb , const void* _src , bx::Error* _err = NULL ); diff --git a/src/image.cpp b/src/image.cpp index 637ecbf..ad8ba3d 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -4001,6 +4001,7 @@ namespace bimg TextureFormat::Enum format = TextureFormat::Unknown; bool hasAlpha = false; + bool srgb = false; for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKtxFormat); ++ii) { @@ -4009,6 +4010,13 @@ namespace bimg format = TextureFormat::Enum(ii); break; } + + if (s_translateKtxFormat[ii].m_internalFmtSrgb == glInternalFormat) + { + format = TextureFormat::Enum(ii); + srgb = true; + break; + } } if (TextureFormat::Unknown == format) @@ -4038,7 +4046,7 @@ namespace bimg _imageContainer.m_cubeMap = numFaces > 1; _imageContainer.m_ktx = true; _imageContainer.m_ktxLE = fromLittleEndian; - _imageContainer.m_srgb = false; + _imageContainer.m_srgb = srgb; if (TextureFormat::Unknown == format) { @@ -5564,19 +5572,24 @@ namespace bimg return total; } - static int32_t imageWriteKtxHeader(bx::WriterI* _writer, TextureFormat::Enum _format, bool _cubeMap, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips, uint32_t _numLayers, bx::Error* _err) + static int32_t imageWriteKtxHeader(bx::WriterI* _writer, TextureFormat::Enum _format, bool _cubeMap, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips, uint32_t _numLayers, bool _srgb, bx::Error* _err) { BX_ERROR_SCOPE(_err); const KtxFormatInfo& tfi = s_translateKtxFormat[_format]; + uint32_t internalFmt = tfi.m_internalFmt; + if (_srgb && tfi.m_internalFmtSrgb != KTX_ZERO) { + internalFmt = tfi.m_internalFmtSrgb; + } + int32_t total = 0; total += bx::write(_writer, "\xabKTX 11\xbb\r\n\x1a\n", 12, _err); total += bx::write(_writer, uint32_t(0x04030201), _err); total += bx::write(_writer, uint32_t(0), _err); // glType total += bx::write(_writer, uint32_t(1), _err); // glTypeSize total += bx::write(_writer, uint32_t(0), _err); // glFormat - total += bx::write(_writer, tfi.m_internalFmt, _err); // glInternalFormat + total += bx::write(_writer, internalFmt, _err); // glInternalFormat total += bx::write(_writer, tfi.m_fmt, _err); // glBaseInternalFormat total += bx::write(_writer, _width, _err); total += bx::write(_writer, _height, _err); @@ -5590,12 +5603,12 @@ namespace bimg return total; } - int32_t imageWriteKtx(bx::WriterI* _writer, TextureFormat::Enum _format, bool _cubeMap, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips, uint32_t _numLayers, const void* _src, bx::Error* _err) + int32_t imageWriteKtx(bx::WriterI* _writer, TextureFormat::Enum _format, bool _cubeMap, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips, uint32_t _numLayers, bool _srgb, const void* _src, bx::Error* _err) { BX_ERROR_SCOPE(_err); int32_t total = 0; - total += imageWriteKtxHeader(_writer, _format, _cubeMap, _width, _height, _depth, _numMips, _numLayers, _err); + total += imageWriteKtxHeader(_writer, _format, _cubeMap, _width, _height, _depth, _numMips, _numLayers, _srgb, _err); if (!_err->isOk() ) { @@ -5658,6 +5671,7 @@ namespace bimg , _imageContainer.m_depth , _imageContainer.m_numMips , _imageContainer.m_numLayers + , _imageContainer.m_srgb , _err ); From 60dea9bd7423c9c582b4b649f9c46b4565b2b87e Mon Sep 17 00:00:00 2001 From: Robin Allen Date: Tue, 3 Mar 2020 22:16:16 +0000 Subject: [PATCH 2/2] texturec: Write sRGB images unless linear is requested --- tools/texturec/texturec.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tools/texturec/texturec.cpp b/tools/texturec/texturec.cpp index 315f0e6..ca03362 100644 --- a/tools/texturec/texturec.cpp +++ b/tools/texturec/texturec.cpp @@ -931,7 +931,7 @@ void help(const char* _error = NULL, bool _showHelp = true) " -q Encoding quality (default, fastest, highest).\n" " -m, --mips Generate mip-maps.\n" " --mipskip Skip number of mips.\n" - " -n, --normalmap Input texture is normal map.\n" + " -n, --normalmap Input texture is normal map. (Implies --linear)\n" " --equirect Input texture is equirectangular projection of cubemap.\n" " --strip Input texture is horizontal strip of cubemap.\n" " --sdf Compute SDF texture.\n" @@ -940,7 +940,7 @@ void help(const char* _error = NULL, bool _showHelp = true) " --pma Premultiply alpha into RGB channel.\n" " --linear Input and output texture is linear color space (gamma correction won't be applied).\n" " --max Maximum width/height (image will be scaled down and\n" - " aspect ratio will be preserved.\n" + " aspect ratio will be preserved)\n" " --radiance Radiance cubemap filter. (Lighting model: Phong, PhongBrdf, Blinn, BlinnBrdf, GGX)\n" " --as Save as.\n" " --formats List all supported formats.\n" @@ -1091,6 +1091,12 @@ int main(int _argc, const char* _argv[]) return bx::kExitFailure; } + // Normal maps are always linear + if (options.normalMap) + { + options.linear = true; + } + const char* maxSize = cmdLine.findOption("max"); if (NULL != maxSize) { @@ -1215,6 +1221,8 @@ int main(int _argc, const char* _argv[]) if (NULL != output) { + output->m_srgb = !options.linear; + bx::FileWriter writer; if (bx::open(&writer, outputFileName, false, &err) ) {