From 23458917f3cc2388beca2b55cc74fe6be8bd1f3e Mon Sep 17 00:00:00 2001
From: chyyran <ronny@ronnychan.ca>
Date: Wed, 11 Jan 2023 19:09:15 -0500
Subject: [PATCH] gl: kind of fix mipmap handlingneeds to check next frame

---
 librashader-runtime-d3d11/src/framebuffer.rs           | 1 +
 librashader-runtime-gl/src/filter_chain/filter_impl.rs | 3 +++
 librashader-runtime-gl/src/gl/framebuffer.rs           | 3 ++-
 librashader-runtime-gl/src/gl/gl3/framebuffer.rs       | 1 +
 librashader-runtime-gl/src/gl/gl46/framebuffer.rs      | 9 ++++++++-
 librashader-runtime-gl/src/gl/mod.rs                   | 1 +
 6 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/librashader-runtime-d3d11/src/framebuffer.rs b/librashader-runtime-d3d11/src/framebuffer.rs
index 929fd0b..e9260d5 100644
--- a/librashader-runtime-d3d11/src/framebuffer.rs
+++ b/librashader-runtime-d3d11/src/framebuffer.rs
@@ -98,6 +98,7 @@ impl OwnedFramebuffer {
                 | D3D11_FORMAT_SUPPORT_RENDER_TARGET.0,
         );
 
+        // todo: fix mipmap handling
         let desc = default_desc(size, format, 1);
         unsafe {
             let mut texture = self.device.CreateTexture2D(&desc, None)?;
diff --git a/librashader-runtime-gl/src/filter_chain/filter_impl.rs b/librashader-runtime-gl/src/filter_chain/filter_impl.rs
index ff9b8e8..eb98387 100644
--- a/librashader-runtime-gl/src/filter_chain/filter_impl.rs
+++ b/librashader-runtime-gl/src/filter_chain/filter_impl.rs
@@ -489,12 +489,14 @@ impl<T: GLInterface> FilterChainImpl<T> {
 
         // rescale render buffers to ensure all bindings are valid.
         for (index, pass) in passes.iter_mut().enumerate() {
+            // todo: properly check **next** pass for mipmap.
             self.output_framebuffers[index].scale::<T::FramebufferInterface>(
                 pass.config.scaling.clone(),
                 pass.get_format(),
                 viewport,
                 &original,
                 &source,
+                pass.config.mipmap_input
             )?;
 
             self.feedback_framebuffers[index].scale::<T::FramebufferInterface>(
@@ -503,6 +505,7 @@ impl<T: GLInterface> FilterChainImpl<T> {
                 viewport,
                 &original,
                 &source,
+                pass.config.mipmap_input
             )?;
         }
 
diff --git a/librashader-runtime-gl/src/gl/framebuffer.rs b/librashader-runtime-gl/src/gl/framebuffer.rs
index 42b2aa6..9bd0637 100644
--- a/librashader-runtime-gl/src/gl/framebuffer.rs
+++ b/librashader-runtime-gl/src/gl/framebuffer.rs
@@ -44,8 +44,9 @@ impl Framebuffer {
         viewport: &Viewport,
         original: &Texture,
         source: &Texture,
+        mipmap: bool
     ) -> Result<Size<u32>> {
-        T::scale(self, scaling, format, viewport, original, source)
+        T::scale(self, scaling, format, viewport, original, source, mipmap)
     }
 
     pub fn copy_from<T: FramebufferInterface>(&mut self, image: &GLImage) -> Result<()> {
diff --git a/librashader-runtime-gl/src/gl/gl3/framebuffer.rs b/librashader-runtime-gl/src/gl/gl3/framebuffer.rs
index 0ef00e6..dbd4ecc 100644
--- a/librashader-runtime-gl/src/gl/gl3/framebuffer.rs
+++ b/librashader-runtime-gl/src/gl/gl3/framebuffer.rs
@@ -59,6 +59,7 @@ impl FramebufferInterface for Gl3Framebuffer {
         viewport: &Viewport,
         _original: &Texture,
         source: &Texture,
+        mipmap: bool,
     ) -> Result<Size<u32>> {
         if fb.is_raw {
             return Ok(fb.size);
diff --git a/librashader-runtime-gl/src/gl/gl46/framebuffer.rs b/librashader-runtime-gl/src/gl/gl46/framebuffer.rs
index 527521c..099660f 100644
--- a/librashader-runtime-gl/src/gl/gl46/framebuffer.rs
+++ b/librashader-runtime-gl/src/gl/gl46/framebuffer.rs
@@ -57,6 +57,7 @@ impl FramebufferInterface for Gl46Framebuffer {
         viewport: &Viewport,
         _original: &Texture,
         source: &Texture,
+        mipmap: bool,
     ) -> Result<Size<u32>> {
         if fb.is_raw {
             return Ok(fb.size);
@@ -67,9 +68,15 @@ impl FramebufferInterface for Gl46Framebuffer {
             .size
             .scale_viewport(scaling, viewport.output.size);
 
-        if fb.size != size {
+        if fb.size != size || (mipmap && fb.max_levels == 1) || (!mipmap && fb.max_levels != 1) {
             fb.size = size;
 
+            if mipmap {
+                fb.max_levels = u32::MAX;
+            } else {
+                fb.max_levels = 1
+            }
+
             Self::init(
                 fb,
                 size,
diff --git a/librashader-runtime-gl/src/gl/mod.rs b/librashader-runtime-gl/src/gl/mod.rs
index b5876e2..abe54c7 100644
--- a/librashader-runtime-gl/src/gl/mod.rs
+++ b/librashader-runtime-gl/src/gl/mod.rs
@@ -52,6 +52,7 @@ pub trait FramebufferInterface {
         viewport: &Viewport,
         _original: &Texture,
         source: &Texture,
+        mipmap: bool,
     ) -> Result<Size<u32>>;
     fn clear<const REBIND: bool>(fb: &Framebuffer);
     fn copy_from(fb: &mut Framebuffer, image: &GLImage) -> Result<()>;
