Browse Source

Fixed assert macros, and improved error handling.

Бранимир Караџић 4 years ago
parent
commit
c4941995d3
8 changed files with 85 additions and 38 deletions
  1. 2 1
      scripts/bimg.lua
  2. 2 1
      scripts/bimg_decode.lua
  3. 2 1
      scripts/bimg_encode.lua
  4. 1 3
      scripts/genie.lua
  5. 2 2
      scripts/texturec.lua
  6. 35 0
      src/bimg_p.h
  7. 40 29
      src/image.cpp
  8. 1 1
      src/image_gnf.cpp

+ 2 - 1
scripts/bimg.lua

@@ -7,7 +7,6 @@ project "bimg"
 	kind "StaticLib"
 
 	includedirs {
-		path.join(BX_DIR, "include"),
 		path.join(BIMG_DIR, "include"),
 		path.join(BIMG_DIR, "3rdparty/astc-codec"),
 		path.join(BIMG_DIR, "3rdparty/astc-codec/include"),
@@ -36,6 +35,8 @@ project "bimg"
 		path.join(BIMG_DIR, "3rdparty/tinyexr/deps/miniz/miniz.*"),
 	}
 
+	using_bx()
+
 	configuration { "linux-*" }
 		buildoptions {
 			"-fPIC",

+ 2 - 1
scripts/bimg_decode.lua

@@ -7,7 +7,6 @@ project "bimg_decode"
 	kind "StaticLib"
 
 	includedirs {
-		path.join(BX_DIR, "include"),
 		path.join(BIMG_DIR, "include"),
 		path.join(BIMG_DIR, "3rdparty"),
 		path.join(BIMG_DIR, "3rdparty/tinyexr/deps/miniz"),
@@ -18,6 +17,8 @@ project "bimg_decode"
 		path.join(BIMG_DIR, "src/image_decode.*"),
 	}
 
+	using_bx()
+
 	configuration { "linux-*" }
 		buildoptions {
 			"-fPIC",

+ 2 - 1
scripts/bimg_encode.lua

@@ -7,7 +7,6 @@ project "bimg_encode"
 	kind "StaticLib"
 
 	includedirs {
-		path.join(BX_DIR, "include"),
 		path.join(BIMG_DIR, "include"),
 		path.join(BIMG_DIR, "3rdparty"),
 		path.join(BIMG_DIR, "3rdparty/nvtt"),
@@ -38,6 +37,8 @@ project "bimg_encode"
 		path.join(BIMG_DIR, "3rdparty/iqa/source/**.c"),
 	}
 
+	using_bx()
+
 	configuration { "linux-*" }
 		buildoptions {
 			"-fPIC",

+ 1 - 3
scripts/genie.lua

@@ -37,7 +37,6 @@ solution "bimg"
 	end
 
 	language "C++"
-	startproject "example-00-helloworld"
 
 MODULE_DIR = path.getabsolute("..")
 BIMG_DIR   = path.getabsolute("..")
@@ -64,12 +63,11 @@ function copyLib()
 end
 
 group "libs"
+dofile(path.join(BX_DIR, "scripts/bx.lua"))
 dofile "bimg.lua"
 dofile "bimg_decode.lua"
 dofile "bimg_encode.lua"
 
-dofile(path.join(BX_DIR, "scripts/bx.lua"))
-
 if _OPTIONS["with-tools"] then
 	group "tools"
 	dofile "texturec.lua"

+ 2 - 2
scripts/texturec.lua

@@ -7,7 +7,6 @@ project "texturec"
 	kind "ConsoleApp"
 
 	includedirs {
-		path.join(BX_DIR,   "include"),
 		path.join(BIMG_DIR, "include"),
 	}
 
@@ -20,9 +19,10 @@ project "texturec"
 		"bimg_decode",
 		"bimg_encode",
 		"bimg",
-		"bx",
 	}
 
+	using_bx()
+
 	configuration { "mingw-*" }
 		targetextension ".exe"
 

+ 35 - 0
src/bimg_p.h

@@ -6,8 +6,43 @@
 #ifndef BIMG_P_H_HEADER_GUARD
 #define BIMG_P_H_HEADER_GUARD
 
+#ifndef BX_CONFIG_DEBUG
+#	error "BX_CONFIG_DEBUG must be defined in build script!"
+#endif // BIMG_CONFIG_DEBUG
+
+#if BX_CONFIG_DEBUG
+#	define BX_TRACE  _BIMG_TRACE
+#	define BX_WARN   _BIMG_WARN
+#	define BX_ASSERT _BIMG_ASSERT
+#endif // BX_CONFIG_DEBUG
+
+#define BX_ASSERT2 BX_ASSERT
+
+#define _BIMG_TRACE(_format, ...)                                                                  \
+	BX_MACRO_BLOCK_BEGIN                                                                           \
+		bx::debugPrintf(__FILE__ "(" BX_STRINGIZE(__LINE__) "): BX " _format "\n", ##__VA_ARGS__); \
+	BX_MACRO_BLOCK_END
+
+#define _BIMG_WARN(_condition, _format, ...)          \
+	BX_MACRO_BLOCK_BEGIN                              \
+		if (!BX_IGNORE_C4127(_condition) )            \
+		{                                             \
+			BX_TRACE("WARN " _format, ##__VA_ARGS__); \
+		}                                             \
+	BX_MACRO_BLOCK_END
+
+#define _BIMG_ASSERT(_condition, _format, ...)          \
+	BX_MACRO_BLOCK_BEGIN                                \
+		if (!BX_IGNORE_C4127(_condition) )              \
+		{                                               \
+			BX_TRACE("ASSERT " _format, ##__VA_ARGS__); \
+			bx::debugBreak();                           \
+		}                                               \
+	BX_MACRO_BLOCK_END
+
 #include <bimg/bimg.h>
 #include <bx/allocator.h>
+#include <bx/debug.h>
 #include <bx/readerwriter.h>
 #include <bx/pixelformat.h>
 #include <bx/endian.h>

+ 40 - 29
src/image.cpp

@@ -1284,7 +1284,7 @@ namespace bimg
 		bx::MemoryReader reader(_src, _size);
 
 		uint32_t magic;
-		bx::read(&reader, magic);
+		bx::read(&reader, magic, bx::ErrorIgnore{});
 
 		ImageContainer imageContainer;
 		if (magicT != magic)
@@ -3994,7 +3994,7 @@ namespace bimg
 		BX_ERROR_SCOPE(_err);
 
 		uint8_t identifier[8];
-		bx::read(_reader, identifier);
+		bx::read(_reader, identifier, _err);
 
 		if (identifier[1] != '1'
 		&&  identifier[2] != '1')
@@ -4004,45 +4004,50 @@ namespace bimg
 		}
 
 		uint32_t endianness;
-		bx::read(_reader, endianness);
+		bx::read(_reader, endianness, _err);
 
 		bool fromLittleEndian = 0x04030201 == endianness;
 
 		uint32_t glType;
-		bx::readHE(_reader, glType, fromLittleEndian);
+		bx::readHE(_reader, glType, fromLittleEndian, _err);
 
 		uint32_t glTypeSize;
-		bx::readHE(_reader, glTypeSize, fromLittleEndian);
+		bx::readHE(_reader, glTypeSize, fromLittleEndian, _err);
 
 		uint32_t glFormat;
-		bx::readHE(_reader, glFormat, fromLittleEndian);
+		bx::readHE(_reader, glFormat, fromLittleEndian, _err);
 
 		uint32_t glInternalFormat;
-		bx::readHE(_reader, glInternalFormat, fromLittleEndian);
+		bx::readHE(_reader, glInternalFormat, fromLittleEndian, _err);
 
 		uint32_t glBaseInternalFormat;
-		bx::readHE(_reader, glBaseInternalFormat, fromLittleEndian);
+		bx::readHE(_reader, glBaseInternalFormat, fromLittleEndian, _err);
 
 		uint32_t width;
-		bx::readHE(_reader, width, fromLittleEndian);
+		bx::readHE(_reader, width, fromLittleEndian, _err);
 
 		uint32_t height;
-		bx::readHE(_reader, height, fromLittleEndian);
+		bx::readHE(_reader, height, fromLittleEndian, _err);
 
 		uint32_t depth;
-		bx::readHE(_reader, depth, fromLittleEndian);
+		bx::readHE(_reader, depth, fromLittleEndian, _err);
 
 		uint32_t numberOfArrayElements;
-		bx::readHE(_reader, numberOfArrayElements, fromLittleEndian);
+		bx::readHE(_reader, numberOfArrayElements, fromLittleEndian, _err);
 
 		uint32_t numFaces;
-		bx::readHE(_reader, numFaces, fromLittleEndian);
+		bx::readHE(_reader, numFaces, fromLittleEndian, _err);
 
 		uint32_t numMips;
-		bx::readHE(_reader, numMips, fromLittleEndian);
+		bx::readHE(_reader, numMips, fromLittleEndian, _err);
 
 		uint32_t metaDataSize;
-		bx::readHE(_reader, metaDataSize, fromLittleEndian);
+		bx::readHE(_reader, metaDataSize, fromLittleEndian, _err);
+
+		if (!_err->isOk() )
+		{
+			return false;
+		}
 
 		// skip meta garbage...
 		int64_t offset = bx::skip(_reader, metaDataSize);
@@ -4198,37 +4203,42 @@ namespace bimg
 		BX_ERROR_SCOPE(_err);
 
 		uint32_t flags;
-		bx::read(_reader, flags);
+		bx::read(_reader, flags, _err);
 
 		uint64_t pixelFormat;
-		bx::read(_reader, pixelFormat);
+		bx::read(_reader, pixelFormat, _err);
 
 		uint32_t colorSpace;
-		bx::read(_reader, colorSpace); // 0 - linearRGB, 1 - sRGB
+		bx::read(_reader, colorSpace, _err); // 0 - linearRGB, 1 - sRGB
 
 		uint32_t channelType;
-		bx::read(_reader, channelType);
+		bx::read(_reader, channelType, _err);
 
 		uint32_t height;
-		bx::read(_reader, height);
+		bx::read(_reader, height, _err);
 
 		uint32_t width;
-		bx::read(_reader, width);
+		bx::read(_reader, width, _err);
 
 		uint32_t depth;
-		bx::read(_reader, depth);
+		bx::read(_reader, depth, _err);
 
 		uint32_t numSurfaces;
-		bx::read(_reader, numSurfaces);
+		bx::read(_reader, numSurfaces, _err);
 
 		uint32_t numFaces;
-		bx::read(_reader, numFaces);
+		bx::read(_reader, numFaces, _err);
 
 		uint32_t numMips;
-		bx::read(_reader, numMips);
+		bx::read(_reader, numMips, _err);
 
 		uint32_t metaDataSize;
-		bx::read(_reader, metaDataSize);
+		bx::read(_reader, metaDataSize, _err);
+
+		if (!_err->isOk() )
+		{
+			return false;
+		}
 
 		// skip meta garbage...
 		int64_t offset = bx::skip(_reader, metaDataSize);
@@ -4298,7 +4308,7 @@ namespace bimg
 		else if (BIMG_CHUNK_MAGIC_TEX == magic)
 		{
 			TextureCreate tc;
-			bx::read(_reader, tc);
+			bx::read(_reader, tc, _err);
 
 			_imageContainer.m_format      = tc.m_format;
 			_imageContainer.m_orientation = Orientation::R0;
@@ -5586,7 +5596,8 @@ namespace bimg
 			0,
 			0,
 		};
-		total += bx::write(_writer, caps, sizeof(caps) );
+
+		total += bx::write(_writer, caps, sizeof(caps), _err);
 
 		total += bx::writeRep(_writer, 0, 4, _err); // reserved2
 
@@ -5598,7 +5609,7 @@ namespace bimg
 
 		if (UINT32_MAX != dxgiFormat)
 		{
-			total += bx::write(_writer, dxgiFormat);
+			total += bx::write(_writer, dxgiFormat, _err);
 			total += bx::write(_writer, uint32_t(1 < _depth ? DDS_DX10_DIMENSION_TEXTURE3D : DDS_DX10_DIMENSION_TEXTURE2D), _err); // dims
 			total += bx::write(_writer, uint32_t(_cubeMap   ? DDS_DX10_MISC_TEXTURECUBE    : 0), _err); // miscFlags
 			total += bx::write(_writer, uint32_t(1), _err); // arraySize

+ 1 - 1
src/image_gnf.cpp

@@ -21,7 +21,7 @@ namespace bimg
 		bx::MemoryReader reader(_src, _size);
 
 		uint32_t magic;
-		bx::read(&reader, magic);
+		bx::read(&reader, magic, bx::ErrorIgnore{});
 
 		ImageContainer imageContainer;
 		if (BIMG_CHUNK_MAGIC_GNF != magic