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

Panic at extreme aspect ratios #3399

Closed
rparrett opened this issue Dec 20, 2021 · 7 comments
Closed

Panic at extreme aspect ratios #3399

rparrett opened this issue Dec 20, 2021 · 7 comments
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior P-Crash A sudden unexpected crash

Comments

@rparrett
Copy link
Contributor

rparrett commented Dec 20, 2021

Bevy version

Latest main
Also, PR #3369

Operating system & version

macos 12.0.1 (m1)

What you did

use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .init_resource::<ResizeTimer>()
        .init_resource::<Resolution>()
        .init_resource::<Resolutions>()
        .add_startup_system(setup)
        .add_system(change_resolution)
        .run();
}

struct ResizeTimer(Timer);
impl Default for ResizeTimer {
    fn default() -> ResizeTimer {
        ResizeTimer(Timer::from_seconds(1.0, true))
    }
}

#[derive(Default)]
struct Resolution(usize);

struct Resolutions(Vec<Vec2>);
impl Default for Resolutions {
    fn default() -> Resolutions {
        Resolutions(vec![
            Vec2::new(256.0, 256.0),
            Vec2::new(128.0, 128.0),
            Vec2::new(64.0, 64.0),
            Vec2::new(32.0, 32.0),
            Vec2::new(8.0, 8.0),
            Vec2::new(1280.0, 1.0),
            Vec2::new(1.0, 720.0),
            Vec2::new(1280.0, 0.0),
            Vec2::new(0.0, 720.0),
            Vec2::new(0.0, 0.0),
            Vec2::new(720.0, 720.0),
            Vec2::new(1280.0, 720.0),
        ])
    }
}

fn setup(mut commands: Commands) {
    commands.spawn_bundle(OrthographicCameraBundle::new_3d());
}

/// This system will then change the resolution during execution
fn change_resolution(
    time: Res<Time>,
    mut resize_timer: ResMut<ResizeTimer>,
    mut index: ResMut<Resolution>,
    resolutions: Res<Resolutions>,
    mut windows: ResMut<Windows>,
) {
    resize_timer.0.tick(time.delta());
    if resize_timer.0.just_finished() {
        let window = windows.get_primary_mut().unwrap();

        info!("resizing to {:?}", resolutions.0[index.0]);

        window.set_resolution(resolutions.0[index.0].x, resolutions.0[index.0].y);

        index.0 += 1;
        if index.0 >= resolutions.0.len() {
            index.0 = 0;
        }
    }
}

What actually happened

2021-12-20T14:46:25.717374Z  INFO bevy_render::renderer: AdapterInfo { name: "Apple M1 Max", vendor: 0, device: 0, device_type: DiscreteGpu, backend: Metal }
2021-12-20T14:46:26.833983Z  INFO resize_window: resizing to Vec2(1280.0, 1.0)
thread 'Compute Task Pool (0)' panicked at 'attempt to divide by zero', /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/core/src/ops/arith.rs:478:1
stack backtrace:
   0: rust_begin_unwind
             at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/panicking.rs:517:5
   1: core::panicking::panic_fmt
             at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/core/src/panicking.rs:100:14
   2: core::panicking::panic
             at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/core/src/panicking.rs:50:5
   3: <u32 as core::ops::arith::Div>::div
             at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/core/src/ops/arith.rs:471:45
   4: glam::core::scalar::vector::<impl glam::core::traits::vector::Vector<T> for glam::core::storage::XY<T>>::div
             at /Users/robparrett/.cargo/registry/src/github.com-1ecc6299db9ec823/glam-0.20.1/src/core/scalar/vector.rs:201:16
   5: <glam::vec2::UVec2 as core::ops::arith::Div>::div
             at /Users/robparrett/.cargo/registry/src/github.com-1ecc6299db9ec823/glam-0.20.1/src/vec.rs:518:22
   6: bevy_pbr::light::Clusters::from_screen_size_and_z_slices
             at ./crates/bevy_pbr/src/light.rs:233:23
   7: bevy_pbr::light::update_clusters
             at ./crates/bevy_pbr/src/light.rs:367:13

Additional information

Perhaps not a showstopper, but this situation can easily occur when providing users with resizable windows.

Probably just needs a .max(UVec2::ONE) somewhere in light.rs, but I don't know the renderer well enough to know if that's the right strategy.

@rparrett rparrett added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Dec 20, 2021
@DJMcNab
Copy link
Member

DJMcNab commented Dec 20, 2021

Duplicate of/closely related to #3368

@rparrett
Copy link
Contributor Author

Yeah, very closely related. I think there may be some platform-specific behavior causing slightly different panics while minimizing.

#3369 fixes minimizing on macos, but still crashes when resizing to these extreme aspect ratios.

@DJMcNab
Copy link
Member

DJMcNab commented Dec 20, 2021

Yes indeed. I missed that you had interacted with #3369 before opening this issue.

I did hypothesise that this issue would occur, but never explicitly tested it; it seemed like a trivial extension. In my view, a fix for #3368 should also fix this issue to be correct.

But you are correct, I should have included that in my original issue.

@alice-i-cecile alice-i-cecile added A-Rendering Drawing game state to the screen P-Crash A sudden unexpected crash and removed S-Needs-Triage This issue needs to be labelled labels Dec 20, 2021
@cart
Copy link
Member

cart commented Dec 20, 2021

@rparrett can you check to see if the "extreme aspect ratios" still crash on main now that #3369 is merged. The implementation has changed since the last time you tested to account for zeros only in one dimension, so that might fix the problem.

@rparrett
Copy link
Contributor Author

Sadly, no dice. Still panicking.

I also tested throwing .max(UVec2::ONE) at light.rs:233 and while it fixes the panic, the console gets spammed with

2021-12-20T22:35:42.390733Z  WARN bevy_pbr::render::light: cluster offset and count out of bounds!

@rparrett
Copy link
Contributor Author

rparrett commented Jan 8, 2022

Updated example in issue text to include repro resolutions for #3596

@rparrett
Copy link
Contributor Author

rparrett commented May 4, 2022

I believe this specific thing was fixed. At least I can no longer repro.

@rparrett rparrett closed this as completed May 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior P-Crash A sudden unexpected crash
Projects
None yet
Development

No branches or pull requests

4 participants