Ver Fonte

Fixed calcTextureSize.

Branimir Karadžić há 9 anos atrás
pai
commit
8809a190ef

+ 16 - 16
examples/08-update/update.cpp

@@ -112,14 +112,14 @@ static void updateTextureCubeRectBgra8(bgfx::TextureHandle _handle, uint8_t _sid
 	bgfx::updateTextureCube(_handle, 0, _side, 0, _x, _y, _width, _height, mem);
 }
 
-static const uint32_t m_textureside   = 512;
-static const uint32_t m_texture2dSize = 256;
+static const uint16_t textureside   = 512;
+static const uint32_t texture2dSize = 256;
 
 class ExampleUpdate : public entry::AppI
 {
 public:
 	ExampleUpdate()
-		: m_cube(m_textureside)
+		: m_cube(textureside)
 	{
 	}
 
@@ -223,7 +223,7 @@ public:
 		u_time = bgfx::createUniform("u_time", bgfx::UniformType::Vec4);
 
 		m_textureCube[0] = bgfx::createTextureCube(
-				  m_textureside
+				  textureside
 				, false
 				, 1
 				, bgfx::TextureFormat::BGRA8
@@ -233,7 +233,7 @@ public:
 		if (m_blitSupported)
 		{
 			m_textureCube[1] = bgfx::createTextureCube(
-					  m_textureside
+					  textureside
 					, false
 					, 1
 					, bgfx::TextureFormat::BGRA8
@@ -242,15 +242,15 @@ public:
 		}
 
 		m_texture2d = bgfx::createTexture2D(
-				  m_texture2dSize
-				, m_texture2dSize
+				  texture2dSize
+				, texture2dSize
 				, false
 				, 1
 				, bgfx::TextureFormat::BGRA8
 				, BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT
 				);
 
-		m_texture2dData = (uint8_t*)malloc(m_texture2dSize*m_texture2dSize*4);
+		m_texture2dData = (uint8_t*)malloc(texture2dSize*texture2dSize*4);
 
 		m_rr = rand()%255;
 		m_gg = rand()%255;
@@ -341,8 +341,8 @@ public:
 			{
 				PackCube face;
 
-				uint32_t bw = bx::uint16_max(1, rand()%(m_textureside/4) );
-				uint32_t bh = bx::uint16_max(1, rand()%(m_textureside/4) );
+				uint32_t bw = bx::uint16_max(1, rand()%(textureside/4) );
+				uint32_t bh = bx::uint16_max(1, rand()%(textureside/4) );
 
 				if (m_cube.find(bw, bh, face) )
 				{
@@ -383,14 +383,14 @@ public:
 
 				{
 					// Fill rect.
-					const uint32_t pitch = m_texture2dSize*4;
+					const uint32_t pitch = texture2dSize*4;
 
-					const uint16_t tw = rand()%m_texture2dSize;
-					const uint16_t th = rand()%m_texture2dSize;
-					const uint16_t tx = rand()%(m_texture2dSize-tw);
-					const uint16_t ty = rand()%(m_texture2dSize-th);
+					const uint16_t tw = rand()% texture2dSize;
+					const uint16_t th = rand()% texture2dSize;
+					const uint16_t tx = rand()%(texture2dSize-tw);
+					const uint16_t ty = rand()%(texture2dSize-th);
 
-					uint8_t* dst = &m_texture2dData[(ty*m_texture2dSize+tx)*4];
+					uint8_t* dst = &m_texture2dData[(ty*texture2dSize+tx)*4];
 					uint8_t* next = dst + pitch;
 
 					// Using makeRef to pass texture memory without copying.

+ 30 - 17
examples/18-ibl/ibl.cpp

@@ -553,8 +553,8 @@ int _main_(int _argc, char** _argv)
 			| (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT  : 0)
 			| (mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
 			, mouseState.m_mz
-			, width
-			, height
+			, uint16_t(width)
+			, uint16_t(height)
 			);
 
 		static int32_t rightScrollArea = 0;
@@ -564,10 +564,16 @@ int _main_(int _argc, char** _argv)
 		imguiIndent();
 		imguiBool("IBL Diffuse",  settings.m_doDiffuseIbl);
 		imguiBool("IBL Specular", settings.m_doSpecularIbl);
-		currentLightProbe = LightProbe::Enum(imguiTabs(currentLightProbe, true, ImguiAlign::LeftIndented, 16, 2, 2
-													   , "Bolonga"
-													   , "Kyoto"
-													   ) );
+		currentLightProbe = LightProbe::Enum(imguiTabs(
+								  uint8_t(currentLightProbe)
+								, true
+								, ImguiAlign::LeftIndented
+								, 16
+								, 2
+								, 2
+								, "Bolonga"
+								, "Kyoto"
+								) );
 		if (imguiCube(lightProbes[currentLightProbe].m_tex, settings.m_lod, settings.m_crossCubemapPreview, true) )
 		{
 			settings.m_crossCubemapPreview = ImguiCubemap::Enum( (settings.m_crossCubemapPreview+1) % ImguiCubemap::Count);
@@ -592,14 +598,21 @@ int _main_(int _argc, char** _argv)
 		imguiIndent();
 		{
 			int32_t selection;
-			if      (0.0f == settings.m_bgType) { selection = 0; }
-			else if (7.0f == settings.m_bgType) { selection = 2; }
-			else                                { selection = 1; }
-			selection = imguiTabs(selection, true, ImguiAlign::LeftIndented, 16, 2, 3
-								 , "Skybox"
-								 , "Radiance"
-								 , "Irradiance"
-								 );
+			if      (0.0f == settings.m_bgType) { selection = UINT8_C(0); }
+			else if (7.0f == settings.m_bgType) { selection = UINT8_C(2); }
+			else                                { selection = UINT8_C(1); }
+
+			selection = imguiTabs(
+							  uint8_t(selection)
+							, true
+							, ImguiAlign::LeftIndented
+							, 16
+							, 2
+							, 3
+							, "Skybox"
+							, "Radiance"
+							, "Irradiance"
+							);
 			if      (0 == selection) { settings.m_bgType = 0.0f; }
 			else if (2 == selection) { settings.m_bgType = 7.0f; }
 			else                     { settings.m_bgType = settings.m_radianceSlider; }
@@ -622,7 +635,7 @@ int _main_(int _argc, char** _argv)
 
 		imguiLabel("Mesh:");
 		imguiIndent();
-		settings.m_meshSelection = imguiChoose(settings.m_meshSelection, "Bunny", "Orbs");
+		settings.m_meshSelection = uint8_t(imguiChoose(settings.m_meshSelection, "Bunny", "Orbs") );
 		imguiUnindent();
 
 		const bool isBunny = (0 == settings.m_meshSelection);
@@ -719,8 +732,8 @@ int _main_(int _argc, char** _argv)
 		bgfx::setViewTransform(1, view, proj);
 
 		// View rect.
-		bgfx::setViewRect(0, 0, 0, width, height);
-		bgfx::setViewRect(1, 0, 0, width, height);
+		bgfx::setViewRect(0, 0, 0, uint16_t(width), uint16_t(height) );
+		bgfx::setViewRect(1, 0, 0, uint16_t(width), uint16_t(height) );
 
 		// Env rotation.
 		const float amount = bx::fmin(deltaTimeSec/0.12f, 1.0f);

+ 10 - 10
examples/common/font/font_manager.cpp

@@ -466,7 +466,7 @@ FontManager::FontManager(Atlas* _atlas)
 	init();
 }
 
-FontManager::FontManager(uint32_t _textureSideWidth) 
+FontManager::FontManager(uint16_t _textureSideWidth)
 	: m_ownAtlas(true)
 	, m_atlas(new Atlas(_textureSideWidth) )
 {
@@ -546,8 +546,8 @@ FontHandle FontManager::createFontByPixelSize(TrueTypeHandle _ttfHandle, uint32_
 	CachedFont& font = m_cachedFonts[fontIdx];
 	font.trueTypeFont = ttf;
 	font.fontInfo = ttf->getFontInfo();
-	font.fontInfo.fontType = _fontType;
-	font.fontInfo.pixelSize = _pixelSize;
+	font.fontInfo.fontType  = int16_t(_fontType);
+	font.fontInfo.pixelSize = uint16_t(_pixelSize);
 	font.cachedGlyphs.clear();
 	font.masterFontHandle.idx = bx::HandleAlloc::invalid;
 
@@ -561,15 +561,15 @@ FontHandle FontManager::createScaledFontToPixelSize(FontHandle _baseFontHandle,
 	CachedFont& baseFont = m_cachedFonts[_baseFontHandle.idx];
 	FontInfo& fontInfo = baseFont.fontInfo;
 
-	FontInfo newFontInfo = fontInfo;
-	newFontInfo.pixelSize = _pixelSize;
-	newFontInfo.scale = (float)_pixelSize / (float) fontInfo.pixelSize;
-	newFontInfo.ascender = (newFontInfo.ascender * newFontInfo.scale);
+	FontInfo newFontInfo  = fontInfo;
+	newFontInfo.pixelSize = uint16_t(_pixelSize);
+	newFontInfo.scale     = (float)_pixelSize / (float) fontInfo.pixelSize;
+	newFontInfo.ascender  = (newFontInfo.ascender * newFontInfo.scale);
 	newFontInfo.descender = (newFontInfo.descender * newFontInfo.scale);
-	newFontInfo.lineGap = (newFontInfo.lineGap * newFontInfo.scale);
-	newFontInfo.maxAdvanceWidth = (newFontInfo.maxAdvanceWidth * newFontInfo.scale);
+	newFontInfo.lineGap   = (newFontInfo.lineGap * newFontInfo.scale);
+	newFontInfo.maxAdvanceWidth    = (newFontInfo.maxAdvanceWidth * newFontInfo.scale);
 	newFontInfo.underlineThickness = (newFontInfo.underlineThickness * newFontInfo.scale);
-	newFontInfo.underlinePosition = (newFontInfo.underlinePosition * newFontInfo.scale);
+	newFontInfo.underlinePosition  = (newFontInfo.underlinePosition * newFontInfo.scale);
 
 	uint16_t fontIdx = m_fontHandles.alloc();
 	BX_CHECK(fontIdx != bx::HandleAlloc::invalid, "Invalid handle used");

+ 7 - 7
examples/common/font/font_manager.h

@@ -33,7 +33,7 @@ struct FontInfo
 	float descender;
 	/// The spacing in pixels between one row's descent and the next row's ascent.
 	float lineGap;
-	/// This field gives the maximum horizontal cursor advance for all glyphs in the font. 
+	/// This field gives the maximum horizontal cursor advance for all glyphs in the font.
 	float maxAdvanceWidth;
 	/// The thickness of the under/hover/strike-trough line in pixels.
 	float underlineThickness;
@@ -100,12 +100,12 @@ struct GlyphInfo
 	float offset_y;
 
 	/// For horizontal text layouts, this is the unscaled horizontal
-	/// distance in pixels used to increment the pen position when the 
+	/// distance in pixels used to increment the pen position when the
 	/// glyph is drawn as part of a string of text.
 	float advance_x;
 
 	/// For vertical text layouts, this is the unscaled vertical distance
-	/// in pixels used to increment the pen position when the glyph is 
+	/// in pixels used to increment the pen position when the glyph is
 	/// drawn as part of a string of text.
 	float advance_y;
 
@@ -125,7 +125,7 @@ public:
 
 	/// Create the font manager and create the texture cube as BGRA8 with
 	/// linear filtering.
-	FontManager(uint32_t _textureSideWidth = 512);
+	FontManager(uint16_t _textureSideWidth = 512);
 
 	~FontManager();
 
@@ -135,7 +135,7 @@ public:
 		return m_atlas;
 	}
 
-	/// Load a TrueType font from a given buffer. The buffer is copied and 
+	/// Load a TrueType font from a given buffer. The buffer is copied and
 	/// thus can be freed or reused after this call.
 	///
 	/// @return invalid handle if the loading fail
@@ -155,7 +155,7 @@ public:
 
 	/// Preload a set of glyphs from a TrueType file.
 	///
-	/// @return True if every glyph could be preloaded, false otherwise if 
+	/// @return True if every glyph could be preloaded, false otherwise if
 	///   the Font is a baked font, this only do validation on the characters.
 	bool preloadGlyph(FontHandle _handle, const wchar_t* _string);
 
@@ -167,7 +167,7 @@ public:
 	/// @remark the handle is required to be valid
 	const FontInfo& getFontInfo(FontHandle _handle) const;
 
-	/// Return the rendering informations about the glyph region. Load the 
+	/// Return the rendering informations about the glyph region. Load the
 	/// glyph from a TrueType font if possible
 	///
 	const GlyphInfo* getGlyphInfo(FontHandle _handle, CodePoint _codePoint);

+ 1 - 1
examples/common/nanovg/nanovg.cpp

@@ -502,7 +502,7 @@ NVGcolor nvgLerpRGBA(NVGcolor c0, NVGcolor c1, float u)
 {
 	int i;
 	float oneminu;
-	NVGcolor cint = {0};
+	NVGcolor cint;
 
 	u = nvg__clampf(u, 0.0f, 1.0f);
 	oneminu = 1.0f - u;

+ 1 - 1
examples/common/packrect.h

@@ -127,7 +127,7 @@ public:
 
 	void reset(uint16_t _side)
 	{
-		for (uint32_t ii = 0; ii < 6; ++ii)
+		for (uint8_t ii = 0; ii < 6; ++ii)
 		{
 			m_mru[ii] = ii;
 			m_ra[ii].reset(_side, _side);

+ 34 - 1
src/bgfx.cpp

@@ -2898,7 +2898,26 @@ error:
 		_depth   = bx::uint16_max(1, _depth);
 		const uint8_t  numMips = calcNumMips(_hasMips, _width, _height, _depth);
 		const uint32_t sides   = _cubeMap ? 6 : 1;
-		const uint32_t size    = _width*_height*_depth*numMips*bpp/8 * sides * _numLayers;
+
+		uint32_t width  = _width;
+		uint32_t height = _height;
+		uint32_t depth  = _depth;
+		uint32_t size   = 0;
+
+		for (uint32_t lod = 0; lod < numMips; ++lod)
+		{
+			width  = bx::uint32_max(blockWidth  * minBlockX, ( (width  + blockWidth  - 1) / blockWidth )*blockWidth);
+			height = bx::uint32_max(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
+			depth  = bx::uint32_max(1, depth);
+
+			size += width*height*depth*bpp/8 * sides;
+
+			width  >>= 1;
+			height >>= 1;
+			depth  >>= 1;
+		}
+
+		size *= _numLayers;
 
 		_info.format  = _format;
 		_info.width   = _width;
@@ -2946,6 +2965,12 @@ error:
 			, "Format %s is not supported for 2D texture. Use bgfx::getCaps to check available texture formats."
 			, getName(_format)
 			);
+		BX_CHECK(false
+			|| 1 >= _numLayers
+			|| 0 != (g_caps.supported & BGFX_CAPS_TEXTURE_2D_ARRAY)
+			, "_numLayers is %d. Texture 2D array is not supported! Use bgfx::getCaps to check BGFX_CAPS_TEXTURE_2D_ARRAY backend renderer capabilities."
+			, _numLayers
+			);
 
 		if (BackbufferRatio::Count != _ratio)
 		{
@@ -2955,6 +2980,7 @@ error:
 		}
 
 		const uint8_t numMips = calcNumMips(_hasMips, _width, _height);
+		_numLayers = bx::uint16_max(_numLayers, 1);
 
 		if (BX_ENABLED(BGFX_CONFIG_DEBUG)
 		&&  NULL != _mem)
@@ -3052,8 +3078,15 @@ error:
 			, "Format %s is not supported for cube texture. Use bgfx::getCaps to check available texture formats."
 			, getName(_format)
 			);
+		BX_CHECK(false
+			|| 1 >= _numLayers
+			|| 0 != (g_caps.supported & BGFX_CAPS_TEXTURE_CUBE_ARRAY)
+			, "_numLayers is %d. Texture cube array is not supported! Use bgfx::getCaps to check BGFX_CAPS_TEXTURE_CUBE_ARRAY backend renderer capabilities."
+			, _numLayers
+			);
 
 		const uint8_t numMips = calcNumMips(_hasMips, _size, _size);
+		_numLayers = bx::uint16_max(_numLayers, 1);
 
 		if (BX_ENABLED(BGFX_CONFIG_DEBUG)
 		&&  NULL != _mem)

+ 1 - 1
src/bgfx_p.h

@@ -3171,7 +3171,7 @@ namespace bgfx
 			BX_CHECK(BackbufferRatio::Count != textureRef.m_bbRatio, "");
 
 			getTextureSizeFromRatio(BackbufferRatio::Enum(textureRef.m_bbRatio), _width, _height);
-			_numMips = calcNumMips(_numMips, _width, _height);
+			_numMips = calcNumMips(1 < _numMips, _width, _height);
 
 			BX_TRACE("Resize %3d: %4dx%d %s"
 				, _handle.idx