From cbea0806653dfeac495635f84c7f2da20e113562 Mon Sep 17 00:00:00 2001 From: lixianjing Date: Tue, 24 Nov 2020 17:32:00 +0800 Subject: [PATCH] improve glad and vgcanvas_nanovg_gl --- 3rd/glad/glad.c | 6 +++++- src/vgcanvas/vgcanvas_nanovg_gl.c | 24 ++++++++++++------------ src/vgcanvas/vgcanvas_nanovg_gl.inc | 9 ++++----- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/3rd/glad/glad.c b/3rd/glad/glad.c index 7640f646a..e37f1e339 100644 --- a/3rd/glad/glad.c +++ b/3rd/glad/glad.c @@ -69,15 +69,19 @@ int open_gl(void) { "/System/Library/Frameworks/OpenGL.framework/OpenGL", "/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL" }; +#else +#ifdef WITH_GLAD_SPECIAL_OPENGL_LIB + static const char *NAMES[] = { WITH_GLAD_SPECIAL_OPENGL_LIB }; #else static const char *NAMES[] = {"libGL.so.1", "libGL.so", "libGLESv2.so"}; +#endif #endif unsigned int index = 0; for(index = 0; index < (sizeof(NAMES) / sizeof(NAMES[0])); index++) { libGL = dlopen(NAMES[index], RTLD_NOW | RTLD_GLOBAL); if(libGL != NULL) { -#ifdef __APPLE__ +#if defined(__APPLE__) || defined(WITH_GLAD_SPECIAL_OPENGL_LIB) return 1; #else if (index < 2) { diff --git a/src/vgcanvas/vgcanvas_nanovg_gl.c b/src/vgcanvas/vgcanvas_nanovg_gl.c index 83801f6ab..816a86155 100644 --- a/src/vgcanvas/vgcanvas_nanovg_gl.c +++ b/src/vgcanvas/vgcanvas_nanovg_gl.c @@ -65,8 +65,8 @@ typedef struct _vgcanvas_nanovg_screen_shader_info_t { GLuint coord_loc; GLuint position_loc; GLuint screentexture_loc; - GLuint indexs[6]; - GLuint vboIds[3]; + GLuint vboIds[2]; + uint32_t draw_arrays; #if defined NANOVG_GL3 GLuint vao; #endif @@ -170,12 +170,13 @@ static inline GLuint vgcanvas_create_LoadShader(const char* g_strShaderHeader, } vgcanvas_nanovg_screen_shader_info_t* vgcanvas_create_init_screen_shader() { - const GLuint indexs[] = {0, 1, 3, 1, 2, 3}; const GLfloat vertexs[] = { // Position 1.0f, 1.0f, 0.0f, // top right 1.0f, -1.0f, 0.0f, // bottm right + -1.0f, 1.0f, 0.0f, // top left + 1.0f, -1.0f, 0.0f, // bottm right -1.0f, -1.0f, 0.0f, // bottm left -1.0f, 1.0f, 0.0f // top left }; @@ -184,6 +185,8 @@ vgcanvas_nanovg_screen_shader_info_t* vgcanvas_create_init_screen_shader() { // texture coords 1.0f, 1.0f, // top right 1.0f, 0.0f, // bottm right + 0.0f, 1.0f, // top left + 1.0f, 0.0f, // bottm right 0.0f, 0.0f, // bottm left 0.0f, 1.0f // top left }; @@ -248,8 +251,6 @@ vgcanvas_nanovg_screen_shader_info_t* vgcanvas_create_init_screen_shader() { (vgcanvas_nanovg_screen_shader_info_t*)TKMEM_ZALLOC(vgcanvas_nanovg_screen_shader_info_t); return_value_if_fail(shader_info != NULL, NULL); - memcpy(shader_info->indexs, indexs, sizeof(indexs)); - shader_info->program_object = vgcanvas_create_LoadShader(vgcanvas_nanovg_shader_header, vertex_shader, fragment_shader); @@ -265,6 +266,8 @@ vgcanvas_nanovg_screen_shader_info_t* vgcanvas_create_init_screen_shader() { glGenBuffers(sizeof(shader_info->vboIds) / sizeof(GLuint), shader_info->vboIds); + shader_info->draw_arrays = ARRAY_SIZE(vertexs) / 3; + #if defined NANOVG_GL3 glGenVertexArrays(1, &shader_info->vao); @@ -275,10 +278,6 @@ vgcanvas_nanovg_screen_shader_info_t* vgcanvas_create_init_screen_shader() { glBindBuffer(GL_ARRAY_BUFFER, shader_info->vboIds[1]); glBufferData(GL_ARRAY_BUFFER, sizeof(tcoords), tcoords, GL_STATIC_DRAW); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, shader_info->vboIds[2]); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(shader_info->indexs), shader_info->indexs, - GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); #else @@ -288,9 +287,6 @@ vgcanvas_nanovg_screen_shader_info_t* vgcanvas_create_init_screen_shader() { glBindBuffer(GL_ARRAY_BUFFER, shader_info->vboIds[1]); glBufferData(GL_ARRAY_BUFFER, sizeof(tcoords), tcoords, GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, shader_info->vboIds[2]); - glBufferData(GL_ARRAY_BUFFER, sizeof(shader_info->indexs), shader_info->indexs, GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); #endif @@ -356,6 +352,10 @@ static vgcanvas_nanovg_offline_fb_t* vgcanvas_create_offline_fb(uint32_t width, return FALSE; } + glViewport(0, 0, width, height); + glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + glBindTexture(GL_TEXTURE_2D, 0); glBindFramebuffer(GL_FRAMEBUFFER, default_fbo); glBindRenderbuffer(GL_RENDERBUFFER, default_rbo); diff --git a/src/vgcanvas/vgcanvas_nanovg_gl.inc b/src/vgcanvas/vgcanvas_nanovg_gl.inc index d2a8c3170..af608709b 100644 --- a/src/vgcanvas/vgcanvas_nanovg_gl.inc +++ b/src/vgcanvas/vgcanvas_nanovg_gl.inc @@ -85,25 +85,24 @@ static inline void vgcanvas_nanovg_offline_fb_flush(vgcanvas_nanovg_t* canvas) { #if defined NANOVG_GL3 glBindVertexArray(shader_info->vao); #endif + glEnableVertexAttribArray(shader_info->coord_loc); + glEnableVertexAttribArray(shader_info->position_loc); glBindBuffer(GL_ARRAY_BUFFER, shader_info->vboIds[0]); glVertexAttribPointer(shader_info->position_loc, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0); - glEnableVertexAttribArray(shader_info->position_loc); glBindBuffer(GL_ARRAY_BUFFER, shader_info->vboIds[1]); glVertexAttribPointer(shader_info->coord_loc, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), 0); - glEnableVertexAttribArray(shader_info->coord_loc); // Bind the texture glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, offline_fb->textureId); glUniform1i(shader_info->screentexture_loc, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, shader_info->vboIds[2]); - glDrawElements(GL_TRIANGLES, sizeof(shader_info->indexs) / sizeof(GLuint), GL_UNSIGNED_INT, 0); + glDrawArrays(GL_TRIANGLES, 0, shader_info->draw_arrays); - glDisableVertexAttribArray(shader_info->position_loc); glDisableVertexAttribArray(shader_info->coord_loc); + glDisableVertexAttribArray(shader_info->position_loc); glBindTexture(GL_TEXTURE_2D, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);