From a6f23f00b430523bc77d4863e33f3c1df25f04b1 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Thu, 12 Dec 2019 23:32:31 +1100 Subject: [PATCH] [client] opengl: handle configuration failure properly --- VERSION | 2 +- client/renderers/OpenGL/opengl.c | 73 +++++++++++++++++++++----------- 2 files changed, 50 insertions(+), 25 deletions(-) diff --git a/VERSION b/VERSION index b20ad87e..05fb6cbb 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -B1-20-gdce6aaefea+1 \ No newline at end of file +B1-21-g30e3a43311+1 \ No newline at end of file diff --git a/client/renderers/OpenGL/opengl.c b/client/renderers/OpenGL/opengl.c index 2cb4ccc7..fb40971c 100644 --- a/client/renderers/OpenGL/opengl.c +++ b/client/renderers/OpenGL/opengl.c @@ -169,8 +169,15 @@ struct Inst static bool _check_gl_error(unsigned int line, const char * name); #define check_gl_error(name) _check_gl_error(__LINE__, name) +enum ConfigStatus +{ + CONFIG_STATUS_OK, + CONFIG_STATUS_ERROR, + CONFIG_STATUS_NOOP +}; + static void deconfigure(struct Inst * this); -static bool configure(struct Inst * this, SDL_Window *window); +static enum ConfigStatus configure(struct Inst * this, SDL_Window *window); static void update_mouse_shape(struct Inst * this, bool * newShape); static bool draw_frame(struct Inst * this); static void draw_mouse(struct Inst * this); @@ -549,10 +556,20 @@ bool opengl_render(void * opaque, SDL_Window * window) if (!this) return false; - if (configure(this, window)) - if (!draw_frame(this)) + switch(configure(this, window)) + { + case CONFIG_STATUS_ERROR: + DEBUG_ERROR("configure failed"); return false; + case CONFIG_STATUS_NOOP : + break; + + case CONFIG_STATUS_OK : + if (!draw_frame(this)) + return false; + } + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); @@ -828,13 +845,13 @@ static bool _check_gl_error(unsigned int line, const char * name) return true; } -static bool configure(struct Inst * this, SDL_Window *window) +static enum ConfigStatus configure(struct Inst * this, SDL_Window *window) { LG_LOCK(this->formatLock); if (!this->reconfigure) { LG_UNLOCK(this->formatLock); - return this->configured; + return CONFIG_STATUS_NOOP; } if (this->configured) @@ -862,7 +879,7 @@ static bool configure(struct Inst * this, SDL_Window *window) default: DEBUG_ERROR("Unknown/unsupported compression type"); - return false; + return CONFIG_STATUS_ERROR; } // calculate the texture size in bytes @@ -880,30 +897,36 @@ static bool configure(struct Inst * this, SDL_Window *window) if (this->amdPinnedMemSupport) { const int pagesize = getpagesize(); - this->texPixels[0] = memalign(pagesize, this->texSize * BUFFER_COUNT); - memset(this->texPixels[0], 0, this->texSize * BUFFER_COUNT); - for(int i = 1; i < BUFFER_COUNT; ++i) - this->texPixels[i] = this->texPixels[0] + this->texSize; for(int i = 0; i < BUFFER_COUNT; ++i) { - glBindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, this->vboID[i]); + this->texPixels[i] = aligned_alloc(pagesize, this->texSize); + if (!this->texPixels[i]) + { + DEBUG_ERROR("Failed to allocate memory for texture"); + return CONFIG_STATUS_ERROR; + } + memset(this->texPixels[i], 0, this->texSize); + + glBindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, this->vboID[i]); if (check_gl_error("glBindBuffer")) { LG_UNLOCK(this->formatLock); - return false; + return CONFIG_STATUS_ERROR; } + glBufferData( GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, this->texSize, this->texPixels[i], - GL_STREAM_DRAW); + GL_STREAM_DRAW + ); if (check_gl_error("glBufferData")) { LG_UNLOCK(this->formatLock); - return false; + return CONFIG_STATUS_ERROR; } } glBindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, 0); @@ -916,7 +939,7 @@ static bool configure(struct Inst * this, SDL_Window *window) if (check_gl_error("glBindBuffer")) { LG_UNLOCK(this->formatLock); - return false; + return CONFIG_STATUS_ERROR; } glBufferData( @@ -928,7 +951,7 @@ static bool configure(struct Inst * this, SDL_Window *window) if (check_gl_error("glBufferData")) { LG_UNLOCK(this->formatLock); - return false; + return CONFIG_STATUS_ERROR; } } glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); @@ -939,7 +962,7 @@ static bool configure(struct Inst * this, SDL_Window *window) if (check_gl_error("glGenTextures")) { LG_UNLOCK(this->formatLock); - return false; + return CONFIG_STATUS_ERROR; } this->hasFrames = true; @@ -950,7 +973,7 @@ static bool configure(struct Inst * this, SDL_Window *window) if (check_gl_error("glBindTexture")) { LG_UNLOCK(this->formatLock); - return false; + return CONFIG_STATUS_ERROR; } glTexImage2D( @@ -967,7 +990,7 @@ static bool configure(struct Inst * this, SDL_Window *window) if (check_gl_error("glTexImage2D")) { LG_UNLOCK(this->formatLock); - return false; + return CONFIG_STATUS_ERROR; } // configure the texture @@ -998,7 +1021,7 @@ static bool configure(struct Inst * this, SDL_Window *window) this->reconfigure = false; LG_UNLOCK(this->formatLock); - return true; + return CONFIG_STATUS_OK; } static void deconfigure(struct Inst * this) @@ -1026,9 +1049,6 @@ static void deconfigure(struct Inst * this) if (this->amdPinnedMemSupport) { - if (this->texPixels[0]) - free(this->texPixels[0]); - for(int i = 0; i < BUFFER_COUNT; ++i) { if (this->fences[i]) @@ -1036,7 +1056,12 @@ static void deconfigure(struct Inst * this) glDeleteSync(this->fences[i]); this->fences[i] = NULL; } - this->texPixels[i] = NULL; + + if (this->texPixels[i]) + { + free(this->texPixels[i]); + this->texPixels[i] = NULL; + } } }