Skip to content

Panic at extreme aspect ratios #3399

@rparrett

Description

@rparrett

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-RenderingDrawing game state to the screenC-BugAn unexpected or incorrect behaviorP-CrashA sudden unexpected crash

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions