Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: Document coordinate system expectations for borrowed textures #2984

Merged
merged 1 commit into from Jun 26, 2023

Conversation

tronical
Copy link
Member

No description provided.

@tronical
Copy link
Member Author

Verified using the following patch that renders the thumbs up image the correct way by default:

diff --git a/examples/opengl_texture/Cargo.toml b/examples/opengl_texture/Cargo.toml
index 4a12dc04d..d12a6e34f 100644
--- a/examples/opengl_texture/Cargo.toml
+++ b/examples/opengl_texture/Cargo.toml
@@ -18,6 +18,7 @@ name = "opengl_texture"
 slint = { path = "../../api/rs/slint" }
 glow = { version = "0.12" }
 instant = { version = "0.1", features = [ "now" ] }
+image = { version = "0.24.0" }
 
 [build-dependencies]
 slint-build = { path = "../../api/rs/build" }
diff --git a/examples/opengl_texture/main.rs b/examples/opengl_texture/main.rs
index f5582e458..eee744825 100644
--- a/examples/opengl_texture/main.rs
+++ b/examples/opengl_texture/main.rs
@@ -154,6 +154,7 @@ struct DemoRenderer {
     start_time: instant::Instant,
     displayed_texture: DemoTexture,
     next_texture: DemoTexture,
+    image: image::RgbaImage,
 }
 
 impl DemoRenderer {
@@ -308,6 +309,11 @@ fn new(gl: glow::Context) -> Self {
             let displayed_texture = DemoTexture::new(&gl, 320, 200);
             let next_texture = DemoTexture::new(&gl, 320, 200);
 
+            let image =
+                image::open(concat!(env!("CARGO_MANIFEST_DIR"), "/../gallery/thumbsup.png"))
+                    .unwrap()
+                    .into_rgba8();
+
             Self {
                 gl,
                 program,
@@ -318,6 +324,7 @@ fn new(gl: glow::Context) -> Self {
                 start_time: instant::Instant::now(),
                 displayed_texture,
                 next_texture,
+                image,
             }
         }
     }
@@ -355,6 +362,66 @@ fn render(
                 std::mem::swap(&mut self.next_texture, &mut new_texture);
             }
 
+            let target_image = image::imageops::resize(
+                &self.image,
+                self.next_texture.width,
+                self.next_texture.height,
+                image::imageops::FilterType::Nearest,
+            );
+
+            unsafe {
+                let _saved_texture_binding =
+                    ScopedTextureBinding::new(gl, Some(self.next_texture.texture));
+
+                let old_unpack_alignment = gl.get_parameter_i32(glow::UNPACK_ALIGNMENT);
+                let old_unpack_row_length = gl.get_parameter_i32(glow::UNPACK_ROW_LENGTH);
+                let old_unpack_skip_pixels = gl.get_parameter_i32(glow::UNPACK_SKIP_PIXELS);
+                let old_unpack_skip_rows = gl.get_parameter_i32(glow::UNPACK_SKIP_ROWS);
+
+                gl.pixel_store_i32(glow::UNPACK_ALIGNMENT, 1);
+                gl.tex_parameter_i32(
+                    glow::TEXTURE_2D,
+                    glow::TEXTURE_MIN_FILTER,
+                    glow::LINEAR as i32,
+                );
+                gl.tex_parameter_i32(
+                    glow::TEXTURE_2D,
+                    glow::TEXTURE_MAG_FILTER,
+                    glow::LINEAR as i32,
+                );
+                gl.tex_parameter_i32(
+                    glow::TEXTURE_2D,
+                    glow::TEXTURE_WRAP_S,
+                    glow::CLAMP_TO_EDGE as i32,
+                );
+                gl.tex_parameter_i32(
+                    glow::TEXTURE_2D,
+                    glow::TEXTURE_WRAP_T,
+                    glow::CLAMP_TO_EDGE as i32,
+                );
+                gl.pixel_store_i32(glow::UNPACK_ROW_LENGTH, width as i32);
+                gl.pixel_store_i32(glow::UNPACK_SKIP_PIXELS, 0);
+                gl.pixel_store_i32(glow::UNPACK_SKIP_ROWS, 0);
+
+                gl.tex_image_2d(
+                    glow::TEXTURE_2D,
+                    0,
+                    glow::RGBA as _,
+                    target_image.width() as _,
+                    target_image.height() as _,
+                    0,
+                    glow::RGBA,
+                    glow::UNSIGNED_BYTE,
+                    Some(target_image.as_raw().align_to().1),
+                );
+
+                gl.pixel_store_i32(glow::UNPACK_ALIGNMENT, old_unpack_alignment);
+                gl.pixel_store_i32(glow::UNPACK_ROW_LENGTH, old_unpack_row_length);
+                gl.pixel_store_i32(glow::UNPACK_SKIP_PIXELS, old_unpack_skip_pixels);
+                gl.pixel_store_i32(glow::UNPACK_SKIP_ROWS, old_unpack_skip_rows);
+            }
+
+            /*
             self.next_texture.with_texture_as_active_fbo(|| {
                 let mut saved_viewport: [i32; 4] = [0, 0, 0, 0];
                 gl.get_parameter_i32_slice(glow::VIEWPORT, &mut saved_viewport);
@@ -379,6 +446,7 @@ fn render(
                     saved_viewport[3],
                 );
             });
+            */
 
             gl.use_program(None);
         }

@tronical tronical merged commit d1daa8d into master Jun 26, 2023
26 checks passed
@tronical tronical deleted the simon/document-texture-borrow-coordinate-system branch June 26, 2023 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants