From 491059ca82142eb7b7c611875538e033d27160c3 Mon Sep 17 00:00:00 2001 From: lawnjelly Date: Tue, 23 Jun 2020 08:34:17 +0100 Subject: [PATCH] GLES2 Batching - Fix texture wrapping state bug. For textures that were imported as wrapping, the legacy renderer relied on GL repeat state being set as a once off during load, and didn't alter the GL wrapping state at runtime. Batching was setting wrapping according to the CANVAS_RECT_TILE flag on rects, however this reset GL wrapping to clamp after use, which was conflicting with later drawcalls that relied on the default wrapping being preserved. In this PR we only set the wrapping in GL if the texture has not been imported with wrapping. This duplicates the logic in the legacy renderer and solves the state bug. --- drivers/gles2/rasterizer_canvas_gles2.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/drivers/gles2/rasterizer_canvas_gles2.cpp b/drivers/gles2/rasterizer_canvas_gles2.cpp index e840636b61..1de9fb9d0e 100644 --- a/drivers/gles2/rasterizer_canvas_gles2.cpp +++ b/drivers/gles2/rasterizer_canvas_gles2.cpp @@ -659,13 +659,22 @@ void RasterizerCanvasGLES2::_batch_render_rects(const Batch &p_batch, Rasterizer glEnableVertexAttribArray(VS::ARRAY_COLOR); } + // We only want to set the GL wrapping mode if the texture is not already tiled (i.e. set in Import). + // This is an optimization left over from the legacy renderer. + // If we DID set tiling in the API, and reverted to clamped, then the next draw using this texture + // may use clamped mode incorrectly. + bool tex_is_already_tiled = tex.flags & VS::TEXTURE_FLAG_REPEAT; + switch (tex.tile_mode) { case BatchTex::TILE_FORCE_REPEAT: { state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_FORCE_REPEAT, true); } break; case BatchTex::TILE_NORMAL: { - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + // if the texture is imported as tiled, no need to set GL state, as it will already be bound with repeat + if (!tex_is_already_tiled) { + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + } } break; default: { } break; @@ -689,8 +698,11 @@ void RasterizerCanvasGLES2::_batch_render_rects(const Batch &p_batch, Rasterizer state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_FORCE_REPEAT, false); } break; case BatchTex::TILE_NORMAL: { - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + // if the texture is imported as tiled, no need to revert GL state + if (!tex_is_already_tiled) { + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + } } break; default: { } break;