From 66a0ee21e394f678017fb7e4ccf2eee372b4bfde Mon Sep 17 00:00:00 2001 From: chyyran Date: Tue, 5 Mar 2024 18:45:18 -0500 Subject: [PATCH] Revert "rt(gl): account for flipped coordinate space when blitting to output" This reverts commit 623c6776f7fa95da06f8c08302f5ea8198bf6f8e. --- .../src/filter_chain/filter_impl.rs | 51 +++++-------------- librashader-runtime-gl/src/gl/framebuffer.rs | 30 +---------- .../src/gl/gl3/framebuffer.rs | 10 ++-- .../src/gl/gl46/framebuffer.rs | 11 ++-- librashader-runtime-gl/src/gl/mod.rs | 2 +- librashader-runtime-gl/tests/triangle.rs | 5 +- 6 files changed, 33 insertions(+), 76 deletions(-) diff --git a/librashader-runtime-gl/src/filter_chain/filter_impl.rs b/librashader-runtime-gl/src/filter_chain/filter_impl.rs index c288b99..fcf66c1 100644 --- a/librashader-runtime-gl/src/filter_chain/filter_impl.rs +++ b/librashader-runtime-gl/src/filter_chain/filter_impl.rs @@ -10,7 +10,7 @@ use crate::texture::InputTexture; use crate::util::{gl_get_version, gl_u16_to_version}; use crate::{error, GLImage}; use gl::types::GLuint; -use librashader_common::{FilterMode, Viewport, WrapMode}; +use librashader_common::Viewport; use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig}; use librashader_reflect::back::glsl::GlslVersion; @@ -250,7 +250,7 @@ impl FilterChainImpl { T::FramebufferInterface::init(&mut back, input.size, input.format)?; } - back.copy_from::(input, false)?; + back.copy_from::(input)?; self.history_framebuffers.push_front(back) } @@ -302,17 +302,12 @@ impl FilterChainImpl { } // shader_gl3: 2067 - // let original = InputTexture { - // image: *input, - // filter, - // mip_filter: filter, - // wrap_mode, - // }; - - let mut flipped = T::FramebufferInterface::new(1); - flipped.copy_from::(&input, true)?; - - let original = flipped.as_texture(filter, wrap_mode); + let original = InputTexture { + image: *input, + filter, + mip_filter: filter, + wrap_mode, + }; let mut source = original; @@ -368,19 +363,9 @@ impl FilterChainImpl { } self.draw_quad.bind_vertices(QuadType::Final); - // try to hint the optimizer assert_eq!(last.len(), 1); if let Some(pass) = last.iter_mut().next() { - // Need to flip the output, so we'll render to a backbuffer before blitting back to the viewport. - let mut output = T::FramebufferInterface::new(1); - output.right_size::( - &viewport - .output - .as_texture(FilterMode::Linear, WrapMode::ClampToEdge) - .image, - )?; - source.filter = pass.config.filter; source.mip_filter = pass.config.filter; source.wrap_mode = pass.config.wrap_mode; @@ -393,22 +378,11 @@ impl FilterChainImpl { viewport, &original, &source, - RenderTarget::viewport_with_output(&output, viewport), + RenderTarget::viewport(viewport), ); - self.common.output_textures[passes_len - 1] = - output.as_texture(pass.config.filter, pass.config.wrap_mode); - - // SAFETY: viewport should be the same size as output texture. - unsafe { - viewport - .output - .copy_from_unchecked::( - &output - .as_texture(pass.config.filter, pass.config.wrap_mode) - .image, - true, - )?; - } + self.common.output_textures[passes_len - 1] = viewport + .output + .as_texture(pass.config.filter, pass.config.wrap_mode); } // swap feedback framebuffers with output @@ -418,6 +392,7 @@ impl FilterChainImpl { ); self.push_history(input)?; + self.draw_quad.unbind_vertices(); Ok(()) diff --git a/librashader-runtime-gl/src/gl/framebuffer.rs b/librashader-runtime-gl/src/gl/framebuffer.rs index 0ae3fe7..a8d31ea 100644 --- a/librashader-runtime-gl/src/gl/framebuffer.rs +++ b/librashader-runtime-gl/src/gl/framebuffer.rs @@ -67,34 +67,8 @@ impl GLFramebuffer { ) } - pub(crate) unsafe fn copy_from_unchecked( - &self, - image: &GLImage, - flip_y: bool, - ) -> Result<()> { - // SAFETY: checked size above. - unsafe { T::copy_from_unchecked(self, image, flip_y) } - } - - pub(crate) fn right_size(&mut self, image: &GLImage) -> Result<()> { - if image.size != self.size || image.format != self.format { - T::init(self, image.size, image.format)?; - } - - Ok(()) - } - - pub(crate) fn copy_from( - &mut self, - image: &GLImage, - flip_y: bool, - ) -> Result<()> { - if image.size != self.size || image.format != self.format { - T::init(self, image.size, image.format)?; - } - - // SAFETY: checked size above. - unsafe { self.copy_from_unchecked::(image, flip_y) } + pub(crate) fn copy_from(&mut self, image: &GLImage) -> Result<()> { + T::copy_from(self, image) } pub(crate) fn as_texture(&self, filter: FilterMode, wrap_mode: WrapMode) -> InputTexture { diff --git a/librashader-runtime-gl/src/gl/gl3/framebuffer.rs b/librashader-runtime-gl/src/gl/gl3/framebuffer.rs index 359ce5b..8b55809 100644 --- a/librashader-runtime-gl/src/gl/gl3/framebuffer.rs +++ b/librashader-runtime-gl/src/gl/gl3/framebuffer.rs @@ -80,8 +80,12 @@ impl FramebufferInterface for Gl3Framebuffer { } } } + fn copy_from(fb: &mut GLFramebuffer, image: &GLImage) -> Result<()> { + // todo: may want to use a shader and draw a quad to be faster. + if image.size != fb.size || image.format != fb.format { + Self::init(fb, image.size, image.format)?; + } - unsafe fn copy_from_unchecked(fb: &GLFramebuffer, image: &GLImage, flip_y: bool) -> Result<()> { unsafe { gl::BindFramebuffer(gl::FRAMEBUFFER, fb.fbo); @@ -108,9 +112,9 @@ impl FramebufferInterface for Gl3Framebuffer { fb.size.width as GLint, fb.size.height as GLint, 0, - if flip_y { fb.size.height as GLint } else { 0 }, + 0, fb.size.width as GLint, - if flip_y { 0 } else { fb.size.height as GLint }, + fb.size.height as GLint, gl::COLOR_BUFFER_BIT, gl::NEAREST, ); diff --git a/librashader-runtime-gl/src/gl/gl46/framebuffer.rs b/librashader-runtime-gl/src/gl/gl46/framebuffer.rs index a0df7c6..88e4750 100644 --- a/librashader-runtime-gl/src/gl/gl46/framebuffer.rs +++ b/librashader-runtime-gl/src/gl/gl46/framebuffer.rs @@ -77,12 +77,17 @@ impl FramebufferInterface for Gl46Framebuffer { ); } } - unsafe fn copy_from_unchecked(fb: &GLFramebuffer, image: &GLImage, flip_y: bool) -> Result<()> { + fn copy_from(fb: &mut GLFramebuffer, image: &GLImage) -> Result<()> { // todo: confirm this behaviour for unbound image. if image.handle == 0 { return Ok(()); } + // todo: may want to use a shader and draw a quad to be faster. + if image.size != fb.size || image.format != fb.format { + Self::init(fb, image.size, image.format)?; + } + unsafe { // gl::NamedFramebufferDrawBuffer(fb.handle, gl::COLOR_ATTACHMENT1); gl::NamedFramebufferReadBuffer(image.handle, gl::COLOR_ATTACHMENT0); @@ -96,9 +101,9 @@ impl FramebufferInterface for Gl46Framebuffer { image.size.width as GLint, image.size.height as GLint, 0, - if flip_y { fb.size.height as GLint } else { 0 }, + 0, fb.size.width as GLint, - if flip_y { 0 } else { fb.size.height as GLint }, + fb.size.height as GLint, gl::COLOR_BUFFER_BIT, gl::NEAREST, ); diff --git a/librashader-runtime-gl/src/gl/mod.rs b/librashader-runtime-gl/src/gl/mod.rs index 0cd3603..d7d7531 100644 --- a/librashader-runtime-gl/src/gl/mod.rs +++ b/librashader-runtime-gl/src/gl/mod.rs @@ -95,7 +95,7 @@ pub(crate) trait FramebufferInterface { mipmap: bool, ) -> Result>; fn clear(fb: &GLFramebuffer); - unsafe fn copy_from_unchecked(fb: &GLFramebuffer, image: &GLImage, flip_y: bool) -> Result<()>; + fn copy_from(fb: &mut GLFramebuffer, image: &GLImage) -> Result<()>; fn init(fb: &mut GLFramebuffer, size: Size, format: impl Into) -> Result<()>; } diff --git a/librashader-runtime-gl/tests/triangle.rs b/librashader-runtime-gl/tests/triangle.rs index 7adc581..9328d63 100644 --- a/librashader-runtime-gl/tests/triangle.rs +++ b/librashader-runtime-gl/tests/triangle.rs @@ -9,8 +9,7 @@ fn triangle_gl() { unsafe { let mut filter = FilterChainGL::load_from_path( - "../test/shaders_slang/crt/crt-hyllian.slangp", - // "../test/shaders_slang/bezel/Mega_Bezel/Presets/MBZ__0__SMOOTH-ADV.slangp", + "../test/shaders_slang/crt/crt-royale.slangp", Some(&FilterChainOptionsGL { glsl_version: 0, use_dsa: false, @@ -31,7 +30,7 @@ fn triangle_gl46() { let mut filter = FilterChainGL::load_from_path( // "../test/slang-shaders/vhs/VHSPro.slangp", // "../test/slang-shaders/test/history.slangp", - "../test/shaders_slang/bezel/Mega_Bezel/Presets/MBZ__0__SMOOTH-ADV.slangp", + "../test/shaders_slang/crt/crt-royale.slangp", // "../test/shadersslang/bezel/Mega_Bezel/Presets/MBZ__0__SMOOTH-ADV.slangp", Some(&FilterChainOptionsGL { glsl_version: 0,