From d98041b4ee29585977feb9e69b403d4804af5d5c Mon Sep 17 00:00:00 2001 From: Oli Larkin Date: Mon, 2 Mar 2020 23:18:05 +0000 Subject: [PATCH 1/3] Bind a dummy texture in setUniforms when compiling with emscripten fixes the WebGL error "RENDER WARNING: there is no texture bound to the unit 0"" in Chrome --- src/nanovg_gl.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/nanovg_gl.h b/src/nanovg_gl.h index 5332da1..046648b 100644 --- a/src/nanovg_gl.h +++ b/src/nanovg_gl.h @@ -268,6 +268,10 @@ struct GLNVGcontext { GLuint stencilFuncMask; GLNVGblend blendFunc; #endif + +#ifdef __EMSCRIPTEN__ + int dummyTex; +#endif }; typedef struct GLNVGcontext GLNVGcontext; @@ -704,6 +708,10 @@ static int glnvg__renderCreate(void* uptr) glFinish(); +#ifdef __EMSCRIPTEN__ + gl->dummyTex = 0; +#endif + return 1; } @@ -985,7 +993,15 @@ static void glnvg__setUniforms(GLNVGcontext* gl, int uniformOffset, int image) glnvg__bindTexture(gl, tex != NULL ? tex->tex : 0); glnvg__checkError(gl, "tex paint tex"); } else { +#ifdef __EMSCRIPTEN__ + if(gl->dummyTex == 0) { + gl->dummyTex = glnvg__renderCreateTexture(&gl, NVG_TEXTURE_ALPHA, 2, 2, 0, NULL); + } + GLNVGtexture* tex = glnvg__findTexture(gl, gl->dummyTex); + glnvg__bindTexture(gl, tex->tex); +#else glnvg__bindTexture(gl, 0); +#endif } } From 3e986cdf4bc9ba8f411bb8d4ca911613abf9eb04 Mon Sep 17 00:00:00 2001 From: Oli Larkin Date: Thu, 5 Mar 2020 22:10:53 +0000 Subject: [PATCH 2/3] call glnvg__renderCreateTexture() in glnvg__renderCreate --- src/nanovg_gl.h | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/nanovg_gl.h b/src/nanovg_gl.h index 046648b..768e71c 100644 --- a/src/nanovg_gl.h +++ b/src/nanovg_gl.h @@ -704,14 +704,14 @@ static int glnvg__renderCreate(void* uptr) #endif gl->fragSize = sizeof(GLNVGfragUniforms) + align - sizeof(GLNVGfragUniforms) % align; +#ifdef __EMSCRIPTEN__ + gl->dummyTex = glnvg__renderCreateTexture(&gl, NVG_TEXTURE_ALPHA, 1, 1, 0, NULL); +#endif + glnvg__checkError(gl, "create done"); glFinish(); -#ifdef __EMSCRIPTEN__ - gl->dummyTex = 0; -#endif - return 1; } @@ -994,9 +994,6 @@ static void glnvg__setUniforms(GLNVGcontext* gl, int uniformOffset, int image) glnvg__checkError(gl, "tex paint tex"); } else { #ifdef __EMSCRIPTEN__ - if(gl->dummyTex == 0) { - gl->dummyTex = glnvg__renderCreateTexture(&gl, NVG_TEXTURE_ALPHA, 2, 2, 0, NULL); - } GLNVGtexture* tex = glnvg__findTexture(gl, gl->dummyTex); glnvg__bindTexture(gl, tex->tex); #else From 35dbc98160f267420e5343d8b87a9f0d16b0a16f Mon Sep 17 00:00:00 2001 From: Oli Larkin Date: Fri, 6 Mar 2020 11:10:34 +0000 Subject: [PATCH 3/3] modify to use dummy texture even without emscripten --- src/nanovg_gl.h | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/nanovg_gl.h b/src/nanovg_gl.h index 768e71c..798b236 100644 --- a/src/nanovg_gl.h +++ b/src/nanovg_gl.h @@ -269,9 +269,7 @@ struct GLNVGcontext { GLNVGblend blendFunc; #endif -#ifdef __EMSCRIPTEN__ int dummyTex; -#endif }; typedef struct GLNVGcontext GLNVGcontext; @@ -504,6 +502,8 @@ static void glnvg__getUniforms(GLNVGshader* shader) #endif } +static int glnvg__renderCreateTexture(void* uptr, int type, int w, int h, int imageFlags, const unsigned char* data); + static int glnvg__renderCreate(void* uptr) { GLNVGcontext* gl = (GLNVGcontext*)uptr; @@ -704,9 +704,9 @@ static int glnvg__renderCreate(void* uptr) #endif gl->fragSize = sizeof(GLNVGfragUniforms) + align - sizeof(GLNVGfragUniforms) % align; -#ifdef __EMSCRIPTEN__ - gl->dummyTex = glnvg__renderCreateTexture(&gl, NVG_TEXTURE_ALPHA, 1, 1, 0, NULL); -#endif + // Some platforms does not allow to have samples to unset textures. + // Create empty one which is bound when there's no texture specified. + gl->dummyTex = glnvg__renderCreateTexture(gl, NVG_TEXTURE_ALPHA, 1, 1, 0, NULL); glnvg__checkError(gl, "create done"); @@ -981,6 +981,7 @@ static GLNVGfragUniforms* nvg__fragUniformPtr(GLNVGcontext* gl, int i); static void glnvg__setUniforms(GLNVGcontext* gl, int uniformOffset, int image) { + GLNVGtexture* tex = NULL; #if NANOVG_GL_USE_UNIFORMBUFFER glBindBufferRange(GL_UNIFORM_BUFFER, GLNVG_FRAG_BINDING, gl->fragBuf, uniformOffset, sizeof(GLNVGfragUniforms)); #else @@ -989,17 +990,14 @@ static void glnvg__setUniforms(GLNVGcontext* gl, int uniformOffset, int image) #endif if (image != 0) { - GLNVGtexture* tex = glnvg__findTexture(gl, image); - glnvg__bindTexture(gl, tex != NULL ? tex->tex : 0); - glnvg__checkError(gl, "tex paint tex"); - } else { -#ifdef __EMSCRIPTEN__ - GLNVGtexture* tex = glnvg__findTexture(gl, gl->dummyTex); - glnvg__bindTexture(gl, tex->tex); -#else - glnvg__bindTexture(gl, 0); -#endif + tex = glnvg__findTexture(gl, image); + } + // If no image is set, use empty texture + if (tex == NULL) { + tex = glnvg__findTexture(gl, gl->dummyTex); } + glnvg__bindTexture(gl, tex != NULL ? tex->tex : 0); + glnvg__checkError(gl, "tex paint tex"); } static void glnvg__renderViewport(void* uptr, float width, float height, float devicePixelRatio)