Browse Source

Only copy the relevant portion of the screen when copying to backbuffer in Compatibility backend

(cherry picked from commit e69968b79432d1a3f9544e70b634f8fe08cb1aa8)
clayjohn 1 year ago
parent
commit
52b3d2915a

+ 12 - 0
drivers/gles3/effects/copy_effects.cpp

@@ -125,6 +125,18 @@ void CopyEffects::copy_to_rect(const Rect2 &p_rect) {
 	draw_screen_quad();
 	draw_screen_quad();
 }
 }
 
 
+void CopyEffects::copy_to_and_from_rect(const Rect2 &p_rect) {
+	bool success = copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_COPY_SECTION_SOURCE);
+	if (!success) {
+		return;
+	}
+
+	copy.shader.version_set_uniform(CopyShaderGLES3::COPY_SECTION, p_rect.position.x, p_rect.position.y, p_rect.size.x, p_rect.size.y, copy.shader_version, CopyShaderGLES3::MODE_COPY_SECTION_SOURCE);
+	copy.shader.version_set_uniform(CopyShaderGLES3::SOURCE_SECTION, p_rect.position.x, p_rect.position.y, p_rect.size.x, p_rect.size.y, copy.shader_version, CopyShaderGLES3::MODE_COPY_SECTION_SOURCE);
+
+	draw_screen_quad();
+}
+
 void CopyEffects::copy_screen() {
 void CopyEffects::copy_screen() {
 	bool success = copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_DEFAULT);
 	bool success = copy.shader.version_bind_shader(copy.shader_version, CopyShaderGLES3::MODE_DEFAULT);
 	if (!success) {
 	if (!success) {

+ 1 - 0
drivers/gles3/effects/copy_effects.h

@@ -62,6 +62,7 @@ public:
 
 
 	// These functions assume that a framebuffer and texture are bound already. They only manage the shader, uniforms, and vertex array.
 	// These functions assume that a framebuffer and texture are bound already. They only manage the shader, uniforms, and vertex array.
 	void copy_to_rect(const Rect2 &p_rect);
 	void copy_to_rect(const Rect2 &p_rect);
+	void copy_to_and_from_rect(const Rect2 &p_rect);
 	void copy_screen();
 	void copy_screen();
 	void copy_cube_to_rect(const Rect2 &p_rect);
 	void copy_cube_to_rect(const Rect2 &p_rect);
 	void bilinear_blur(GLuint p_source_texture, int p_mipmap_count, const Rect2i &p_region);
 	void bilinear_blur(GLuint p_source_texture, int p_mipmap_count, const Rect2i &p_region);

+ 3 - 2
drivers/gles3/shaders/copy.glsl

@@ -3,6 +3,7 @@
 
 
 mode_default = #define MODE_SIMPLE_COPY
 mode_default = #define MODE_SIMPLE_COPY
 mode_copy_section = #define USE_COPY_SECTION \n#define MODE_SIMPLE_COPY
 mode_copy_section = #define USE_COPY_SECTION \n#define MODE_SIMPLE_COPY
+mode_copy_section_source = #define USE_COPY_SECTION \n#define MODE_SIMPLE_COPY \n#define MODE_COPY_FROM
 mode_gaussian_blur = #define MODE_GAUSSIAN_BLUR
 mode_gaussian_blur = #define MODE_GAUSSIAN_BLUR
 mode_mipmap = #define MODE_MIPMAP
 mode_mipmap = #define MODE_MIPMAP
 mode_simple_color = #define MODE_SIMPLE_COLOR \n#define USE_COPY_SECTION
 mode_simple_color = #define MODE_SIMPLE_COLOR \n#define USE_COPY_SECTION
@@ -21,7 +22,7 @@ out vec2 uv_interp;
 // Defined in 0-1 coords.
 // Defined in 0-1 coords.
 uniform highp vec4 copy_section;
 uniform highp vec4 copy_section;
 #endif
 #endif
-#ifdef MODE_GAUSSIAN_BLUR
+#if defined(MODE_GAUSSIAN_BLUR) || defined(MODE_COPY_FROM)
 uniform highp vec4 source_section;
 uniform highp vec4 source_section;
 #endif
 #endif
 
 
@@ -32,7 +33,7 @@ void main() {
 #if defined(USE_COPY_SECTION) || defined(MODE_GAUSSIAN_BLUR)
 #if defined(USE_COPY_SECTION) || defined(MODE_GAUSSIAN_BLUR)
 	gl_Position.xy = (copy_section.xy + uv_interp.xy * copy_section.zw) * 2.0 - 1.0;
 	gl_Position.xy = (copy_section.xy + uv_interp.xy * copy_section.zw) * 2.0 - 1.0;
 #endif
 #endif
-#ifdef MODE_GAUSSIAN_BLUR
+#if defined(MODE_GAUSSIAN_BLUR) || defined(MODE_COPY_FROM)
 	uv_interp = source_section.xy + uv_interp * source_section.zw;
 	uv_interp = source_section.xy + uv_interp * source_section.zw;
 #endif
 #endif
 }
 }

+ 4 - 1
drivers/gles3/storage/texture_storage.cpp

@@ -2597,7 +2597,10 @@ void TextureStorage::render_target_copy_to_back_buffer(RID p_render_target, cons
 	glBindFramebuffer(GL_FRAMEBUFFER, rt->backbuffer_fbo);
 	glBindFramebuffer(GL_FRAMEBUFFER, rt->backbuffer_fbo);
 	glActiveTexture(GL_TEXTURE0);
 	glActiveTexture(GL_TEXTURE0);
 	glBindTexture(GL_TEXTURE_2D, rt->color);
 	glBindTexture(GL_TEXTURE_2D, rt->color);
-	GLES3::CopyEffects::get_singleton()->copy_screen();
+	Rect2 normalized_region = region;
+	normalized_region.position = normalized_region.position / Size2(rt->size);
+	normalized_region.size = normalized_region.size / Size2(rt->size);
+	GLES3::CopyEffects::get_singleton()->copy_to_and_from_rect(normalized_region);
 
 
 	if (p_gen_mipmaps) {
 	if (p_gen_mipmaps) {
 		GLES3::CopyEffects::get_singleton()->gaussian_blur(rt->backbuffer, rt->mipmap_count, region, rt->size);
 		GLES3::CopyEffects::get_singleton()->gaussian_blur(rt->backbuffer, rt->mipmap_count, region, rt->size);