From 403c4da2e0723670d7c0261a10bd636d2346404d Mon Sep 17 00:00:00 2001 From: Francis Hart Date: Tue, 28 Feb 2023 04:33:29 +0200 Subject: [PATCH] Fix compiler warnings for nanovg wrapper (#3048) * Fix compiler warnings for nanovg wrapper Code for the nanovg wrapper was generating compiler warnings for implicit casts from int32_t to uint16_t. These casts are unavoidable due to how bgfx and nanovg APIs use different types for texture width/height. * Fix compiler warning from nanovg fontstash.h This fixes some warnings for signed/unsigned comparsion coming from the nanovg fontstash.h header. --- examples/common/nanovg/fontstash.h | 2 +- examples/common/nanovg/nanovg_bgfx.cpp | 38 +++++++++++++++++--------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/examples/common/nanovg/fontstash.h b/examples/common/nanovg/fontstash.h index 7285d7e04..39a48fb90 100644 --- a/examples/common/nanovg/fontstash.h +++ b/examples/common/nanovg/fontstash.h @@ -231,7 +231,7 @@ void fons__tt_renderGlyphBitmap(FONSttFontImpl *font, unsigned char *output, int { FT_GlyphSlot ftGlyph = font->font->glyph; int ftGlyphOffset = 0; - int x, y; + unsigned int x, y; FONS_NOTUSED(outWidth); FONS_NOTUSED(outHeight); FONS_NOTUSED(scaleX); diff --git a/examples/common/nanovg/nanovg_bgfx.cpp b/examples/common/nanovg/nanovg_bgfx.cpp index c7626f949..b87eea013 100644 --- a/examples/common/nanovg/nanovg_bgfx.cpp +++ b/examples/common/nanovg/nanovg_bgfx.cpp @@ -320,9 +320,12 @@ namespace mem = bgfx::copy(_rgba, tex->height * pitch); } + BX_ASSERT(tex->width >= 0 && tex->width <= bx::max(), "Invalid tex width %d (max: %u)", tex->width, bx::max()); + BX_ASSERT(tex->height >= 0 && tex->height <= bx::max(), "Invalid tex height %d (max: %u)", tex->height, bx::max()); + tex->id = bgfx::createTexture2D( - tex->width - , tex->height + uint16_t(tex->width) + , uint16_t(tex->height) , false , 1 , NVG_TEXTURE_RGBA == _type ? bgfx::TextureFormat::RGBA8 : bgfx::TextureFormat::R8 @@ -337,8 +340,8 @@ namespace , 0 , 0 , 0 - , tex->width - , tex->height + , uint16_t(tex->width) + , uint16_t(tex->height) , mem ); } @@ -371,14 +374,19 @@ namespace w * bytesPerPixel, // stride h); // num + BX_ASSERT(x >= 0 && x <= bx::max(), "Invalid tex x pos %d (max: %u)", x, bx::max()); + BX_ASSERT(y >= 0 && y <= bx::max(), "Invalid tex y pos %d (max: %u)", y, bx::max()); + BX_ASSERT(w >= 0 && w <= bx::max(), "Invalid tex width %d (max: %u)", w, bx::max()); + BX_ASSERT(h >= 0 && h <= bx::max(), "Invalid tex width %d (max: %u)", h, bx::max()); + bgfx::updateTexture2D( tex->id , 0 , 0 - , x - , y - , w - , h + , uint16_t(x) + , uint16_t(y) + , uint16_t(w) + , uint16_t(h) , mem , UINT16_MAX ); @@ -569,9 +577,9 @@ namespace uint16_t* data = (uint16_t*)tib.data; for (uint32_t ii = 0; ii < numTris; ++ii) { - data[ii*3+0] = _start; - data[ii*3+1] = _start + ii + 1; - data[ii*3+2] = _start + ii + 2; + data[ii*3+0] = uint16_t(_start); + data[ii*3+1] = uint16_t(_start + ii + 1); + data[ii*3+2] = uint16_t(_start + ii + 2); } bgfx::setIndexBuffer(&tib); @@ -1197,10 +1205,14 @@ NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int32_t width, int32_t NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* _ctx, int32_t _width, int32_t _height, int32_t _imageFlags) { BX_UNUSED(_imageFlags); + BX_ASSERT(_width >= 0 && _width <= bx::max(), "Invalid tex width %d (max: %u)", _width, bx::max()); + BX_ASSERT(_height >= 0 && _height <= bx::max(), "Invalid tex height %d (max: %u)", _height, bx::max()); + const uint16_t w = uint16_t(_width); + const uint16_t h = uint16_t(_height); bgfx::TextureHandle textures[] = { - bgfx::createTexture2D(_width, _height, false, 1, bgfx::TextureFormat::RGBA8, BGFX_TEXTURE_RT), - bgfx::createTexture2D(_width, _height, false, 1, bgfx::TextureFormat::D24S8, BGFX_TEXTURE_RT | BGFX_TEXTURE_RT_WRITE_ONLY) + bgfx::createTexture2D(w, h, false, 1, bgfx::TextureFormat::RGBA8, BGFX_TEXTURE_RT), + bgfx::createTexture2D(w, h, false, 1, bgfx::TextureFormat::D24S8, BGFX_TEXTURE_RT | BGFX_TEXTURE_RT_WRITE_ONLY) }; bgfx::FrameBufferHandle fbh = bgfx::createFrameBuffer( BX_COUNTOF(textures)