From 9430e68cf3013aaa0673a4dfff844324a9c1274a Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Tue, 12 Feb 2019 18:49:09 -0300 Subject: [PATCH] Support SCREEN_TEXTURE in GLES2, fixes #25405 --- drivers/gles2/rasterizer_canvas_gles2.cpp | 17 +++++++++-------- drivers/gles2/rasterizer_canvas_gles2.h | 1 + drivers/gles2/rasterizer_scene_gles2.cpp | 10 ++++++++++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/drivers/gles2/rasterizer_canvas_gles2.cpp b/drivers/gles2/rasterizer_canvas_gles2.cpp index 72bc33e407c..069dbe027df 100644 --- a/drivers/gles2/rasterizer_canvas_gles2.cpp +++ b/drivers/gles2/rasterizer_canvas_gles2.cpp @@ -1172,7 +1172,7 @@ void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *cur } } -void RasterizerCanvasGLES2::_copy_texscreen(const Rect2 &p_rect) { +void RasterizerCanvasGLES2::_copy_screen(const Rect2 &p_rect) { if (storage->frame.current_rt->copy_screen_effect.color == 0) { ERR_EXPLAIN("Can't use screen texture copying in a render target configured without copy buffers"); @@ -1181,8 +1181,6 @@ void RasterizerCanvasGLES2::_copy_texscreen(const Rect2 &p_rect) { glDisable(GL_BLEND); - state.canvas_texscreen_used = true; - Vector2 wh(storage->frame.current_rt->width, storage->frame.current_rt->height); Color copy_section(p_rect.position.x / wh.x, p_rect.position.y / wh.y, p_rect.size.x / wh.x, p_rect.size.y / wh.y); @@ -1195,9 +1193,6 @@ void RasterizerCanvasGLES2::_copy_texscreen(const Rect2 &p_rect) { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->color); - glClearColor(1, 0, 1, 1); - glClear(GL_COLOR_BUFFER_BIT); - storage->shaders.copy.bind(); storage->shaders.copy.set_uniform(CopyShaderGLES2::COPY_SECTION, copy_section); @@ -1225,14 +1220,20 @@ void RasterizerCanvasGLES2::_copy_texscreen(const Rect2 &p_rect) { storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_COPY_SECTION, false); glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo); //back to front + glEnable(GL_BLEND); +} + +void RasterizerCanvasGLES2::_copy_texscreen(const Rect2 &p_rect) { + + state.canvas_texscreen_used = true; + + _copy_texscreen(p_rect); // back to canvas, force rebind state.using_texture_rect = false; state.canvas_shader.bind(); _bind_canvas_texture(state.current_tex, state.current_normal); _set_uniforms(); - - glEnable(GL_BLEND); } void RasterizerCanvasGLES2::canvas_render_items(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform) { diff --git a/drivers/gles2/rasterizer_canvas_gles2.h b/drivers/gles2/rasterizer_canvas_gles2.h index 221427198a1..0d069ebabc0 100644 --- a/drivers/gles2/rasterizer_canvas_gles2.h +++ b/drivers/gles2/rasterizer_canvas_gles2.h @@ -118,6 +118,7 @@ public: _FORCE_INLINE_ void _draw_generic(GLuint p_primitive, int p_vertex_count, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, bool p_singlecolor); _FORCE_INLINE_ void _canvas_item_render_commands(Item *p_item, Item *current_clip, bool &reclip, RasterizerStorageGLES2::Material *p_material); + void _copy_screen(const Rect2 &p_rect); _FORCE_INLINE_ void _copy_texscreen(const Rect2 &p_rect); virtual void canvas_render_items(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform); diff --git a/drivers/gles2/rasterizer_scene_gles2.cpp b/drivers/gles2/rasterizer_scene_gles2.cpp index bddf8cd5c41..2c6168241ec 100644 --- a/drivers/gles2/rasterizer_scene_gles2.cpp +++ b/drivers/gles2/rasterizer_scene_gles2.cpp @@ -1178,6 +1178,11 @@ bool RasterizerSceneGLES2::_setup_material(RasterizerStorageGLES2::Material *p_m state.scene_shader.set_custom_shader(p_material->shader->custom_code_id); + if (p_material->shader->spatial.uses_screen_texture && storage->frame.current_rt) { + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4); + glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->copy_screen_effect.color); + } + bool shader_rebind = state.scene_shader.bind(); if (p_material->shader->spatial.no_depth_test) { @@ -2593,6 +2598,7 @@ void RasterizerSceneGLES2::render_scene(const Transform &p_cam_transform, const viewport_height = storage->frame.current_rt->height; } + state.used_screen_texture = false; state.viewport_size.x = viewport_width; state.viewport_size.y = viewport_height; state.screen_pixel_size.x = 1.0 / viewport_width; @@ -2728,6 +2734,10 @@ void RasterizerSceneGLES2::render_scene(const Transform &p_cam_transform, const render_list.sort_by_key(false); _render_render_list(render_list.elements, render_list.element_count, cam_transform, p_cam_projection, p_shadow_atlas, env, env_radiance_tex, 0.0, 0.0, reverse_cull, false, false); + if (storage->frame.current_rt && state.used_screen_texture) { + //copy screen texture + storage->canvas->_copy_screen(Rect2()); + } // alpha pass glBlendEquation(GL_FUNC_ADD);