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

OpenGL renderer glitches on window scale (MacOS, retina) #109

Closed
ytaras opened this issue Mar 31, 2020 · 20 comments
Closed

OpenGL renderer glitches on window scale (MacOS, retina) #109

ytaras opened this issue Mar 31, 2020 · 20 comments
Assignees
Labels
awaiting_confirmation Believed fix, waiting for testing bug Something isn't working

Comments

@ytaras
Copy link
Contributor

ytaras commented Mar 31, 2020

If I try to run simple example (using default OpenGL renderer) and try to resize game window, it acts weird. With original size render area take 100% of the window. If I shrink window vertically, render area shrinks non in proportion, leaving some of the window blank. On the other hand, if I resize window to be larger, drawing area doesn't fit into window. Same happens to horizontal scaling.
I think is caused by retina display on my laptop, as I've seen other issues in tracker related to hdpi display.
Here's an example of drawing 80x50 red area and resizing a window:
Screenshot 2020-03-31 at 11 47 35

Source code for example:

use bracket_lib::prelude::*;
struct State {
}

impl GameState for State {
    fn tick(&mut self, ctx: &mut BTerm) {
        ctx.cls();
        ctx.fill_region(Rect::with_size(0, 0, 80, 50), ' ' as u8, RGB::named(BLACK), RGB::named(RED));
    }
}
fn main()  {
    let context = BTermBuilder::simple80x50()
        .with_title("Hello RLTK World")
        .build();
    let gs: State = State {
    };

    main_loop(context, gs)
}
@ytaras
Copy link
Contributor Author

ytaras commented Mar 31, 2020

Have just verified same issue with latest (master) version of code

@thebracket
Copy link
Collaborator

I wish I had a Mac to test on; I can't get the same results on Windows or Linux messing with display scaling. Does it also do that on the included examples?

@ytaras
Copy link
Contributor Author

ytaras commented Apr 1, 2020

Unfortunately it does.
If that helps - it works even for native_gl example, including custom-drawn triangle:
out

Do you have any suggestions how I could gather required information which helps to debug?
I've looked into window sizes - it has scaling factor 2.0, and physical size at start is width: 1280, height: 820 by default (for simple80x50() + 8x8 font). If I resize a window, I see nothing suspicious in a window size as well.

@thebracket thebracket self-assigned this Apr 8, 2020
@thebracket thebracket added the bug Something isn't working label Apr 8, 2020
@djmittens
Copy link

I am also affected by this plague, is there any workarounds?

@thebracket
Copy link
Collaborator

Oh no, I thought this was fixed. Are you using the crate or the github version? I don't have a Mac to test on, but I'll see what I can do.

@djmittens
Copy link

djmittens commented Aug 29, 2020

I am following the rogue like tutorial and i am using the following version of rltk:

[dependencies]
rltk = "0.8.1"

Example code having this issue:
https://github.com/djmittens/tetra

@garritfra
Copy link

Just checking in to see if there's any progress on this? I've got the same issue, but I'm glad that it only seems to affect MacOS.

@singalen
Copy link
Contributor

singalen commented Feb 8, 2021

I tried debugging it, but didn't get very far, as my Rust is very basic.
Would love to get some clue where to look.

@thebracket
Copy link
Collaborator

Just getting back into the swing of things after being unwell and then having to care for my mother being unwell. It does look like a DPI issue, I have a few ideas - I'll do some digging and post results. Trying to avoid having to buy a Mac to test with!

@singalen
Copy link
Contributor

singalen commented Feb 9, 2021

Happy to help to diagnose it, I run into it each time. I also cannot build bracket-lib with Metal (#188).
Hope you family gets well.

@thebracket
Copy link
Collaborator

thebracket commented Feb 9, 2021

Now that's interesting. Updating glutin and winit has given me similar behavior on Windows! It looks like when PhysicalSize returns now, it already has the scale factor applied to it. I just tested on Windows, and removing the dpi_size division was required to make it work properly. I'm now testing that in a Linux VM.

Update: The fix worked with display scaling on Ubuntu X/11. Weird stuff happened on Ubuntu/Wayland - but it often does, so I'll worry about that as a separate issue.

The code I'm testing is with the latest head (to update dependencies; do a clean first). Then in bracket-terminal/src/hal/native/mainloop.rs:

fn on_resize(
    bterm: &mut BTerm,
    physical_size: glutin::dpi::PhysicalSize<u32>,
    dpi_scale_factor: f64,
    send_event: bool,
) -> Result<()> {
    INPUT.lock().set_scale_factor(dpi_scale_factor);
    let mut be = BACKEND.lock();
    if send_event {
        bterm.resize_pixels(
            physical_size.width as u32,
            physical_size.height as u32,
            be.resize_scaling,
        );
    }
    let gl = be.gl.as_ref().unwrap();
    unsafe {
        gl_error_wrap!(
            gl,
            gl.viewport(
                0,
                0,
                physical_size.width as i32,
                physical_size.height as i32,
            )
        );
    }

@thebracket
Copy link
Collaborator

I just committed the code that gives sensible behavior on Windows and Ubuntu/X11. Would someone mind giving this a go on a Mac for me, please?

@singalen
Copy link
Contributor

singalen commented Feb 9, 2021

It got much better now, thank you!
The default size is spot on (before it had offset on the left and was off the window on the right), but resize still misbehaves a bit. Looks like there's an extra multiplier.
bracket-mac

@thebracket
Copy link
Collaborator

That's progress. :-) I suspect I can find a couple of other uses of DPI that are causing that issue. I'll go digging in a minute. (The bouncy gif is trippy!)

@thebracket
Copy link
Collaborator

Found the downside of the above fix - it messes up mouse position calculation after the window is resized. I think I know how to get this fixed, though - it may be tomorrow, I'm running short of time for the day. Thanks for the help - I'm pretty sure I've narrowed it down.

thebracket added a commit that referenced this issue Feb 10, 2021
#109 Scaling handler.

* Phase 1 - mouse position is a simple extent based calculation. Test commit to move to my other test machines.

* Display scaling isn't supported yet, and there's some cleaning to do - but this is scaling perfectly on X11/Ubuntu and Windows for me with different display scaling settings.

* Format run

* This should fix scaling.

* Cleanup commented code.
@thebracket
Copy link
Collaborator

Ok, I've taken the changes that helped earlier and gone a little further with them - and completely rewritten the mouse positioning section. This should help with the scaling issues. On my Ubuntu/X11 and Windows installations it's dead on the money both initially and after a resize on various scaling settings (tested 125%, 150%, 200%). So I'm really hopeful that this will help with the OS X issues.

@singalen
Copy link
Contributor

Thank you very much!
Will try the mouse as soon as I can. So far, resizing the window still messes up the scale like on the image before 8(

thebracket added a commit that referenced this issue Feb 11, 2021
…rather than trusting the event parameter. Getting some slightly odd results in testing when I trust the event.
thebracket added a commit that referenced this issue Feb 11, 2021
thebracket added a commit that referenced this issue Feb 11, 2021
…ow works on my test Wayland install. Now also includes DPI changes.
@thebracket
Copy link
Collaborator

Progress! I found someone else who had suffered a similar problem, and applied their fix. Turned out to be two lines of code! It now scales correctly on everything I have available to test with - Windows, Ubuntu 20.1/X11, Ubuntu 20.1/Wayland. That's the first time Wayland has look right in a few winit releases, which is a good sign. :-)

@thebracket
Copy link
Collaborator

A friend (Red blob games on the roguelikedev discord) informs me that it is working really well on his Mac now. :-)

@thebracket
Copy link
Collaborator

I've now had two people tell me that this is working correctly now. So I'm closing this issue - please re-open if the plague returns!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting_confirmation Believed fix, waiting for testing bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants