Explorar o código

Merge pull request #41 from rogual/srgb

Enable KTX sRGB formats
Бранимир Караџић %!s(int64=5) %!d(string=hai) anos
pai
achega
de9ed6b2ef
Modificáronse 3 ficheiros con 30 adicións e 7 borrados
  1. 1 0
      include/bimg/bimg.h
  2. 19 5
      src/image.cpp
  3. 10 2
      tools/texturec/texturec.cpp

+ 1 - 0
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
 		);

+ 19 - 5
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
 			);
 

+ 10 - 2
tools/texturec/texturec.cpp

@@ -931,7 +931,7 @@ void help(const char* _error = NULL, bool _showHelp = true)
 		  "  -q <quality>             Encoding quality (default, fastest, highest).\n"
 		  "  -m, --mips               Generate mip-maps.\n"
 		  "      --mipskip <N>        Skip <N> 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 <max size>     Maximum width/height (image will be scaled down and\n"
-		  "                           aspect ratio will be preserved.\n"
+		  "                           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"
@@ -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) )
 		{