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

Crashes with throttling render assets #310

Open
MalekiRe opened this issue Sep 27, 2024 · 3 comments
Open

Crashes with throttling render assets #310

MalekiRe opened this issue Sep 27, 2024 · 3 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@MalekiRe
Copy link

try
commands.insert_resource( bevy::render::render_asset::RenderAssetBytesPerFrame::new(4096), );
Result: crash

called `Option::unwrap()` on a `None` value
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Encountered a panic in system `bevy_render::renderer::render_system`!
@mvlabat
Copy link
Owner

mvlabat commented Oct 4, 2024

Hi! Are you sure this is an issue with bevy_egui? If so, could you provide a more complete example on how to reproduce this?

@MalekiRe
Copy link
Author

MalekiRe commented Oct 8, 2024

This doesn't actually cause a crash but it causes the window to not render.

use bevy::prelude::*;
use bevy_egui::{EguiContexts, EguiPlugin};

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugins(EguiPlugin)
        // Systems that create Egui widgets should be run during the `CoreSet::Update` set,
        // or after the `EguiSet::BeginPass` system (which belongs to the `CoreSet::PreUpdate` set).
        .add_systems(Update, ui_example_system)
        .run();
}

fn ui_example_system(mut contexts: EguiContexts, mut commands: Commands) {
    commands.insert_resource( bevy::render::render_asset::RenderAssetBytesPerFrame::new(4096));
    egui::Window::new("Hello").show(contexts.ctx_mut(), |ui| {
        ui.label("world");
    });
}

This one causes it to crash

use bevy::prelude::*;
use bevy_egui::{EguiContexts, EguiPlugin, EguiRenderToTextureHandle};
use wgpu_types::{Extent3d, TextureUsages};

fn main() {
    let mut app = App::new();
    app.add_plugins(DefaultPlugins);
    app.add_plugins(EguiPlugin);
    app.add_systems(Startup, setup_worldspace);
    app.add_systems(Update, (update_screenspace, update_worldspace));
    app.run();
}

fn update_screenspace(mut contexts: EguiContexts, mut commands: Commands) {
    commands.insert_resource( bevy::render::render_asset::RenderAssetBytesPerFrame::new(4096));
    egui::Window::new("Screenspace UI").show(contexts.ctx_mut(), |ui| {
        ui.label("I'm rendering to screenspace!");
    });
}

fn update_worldspace(
    mut contexts: Query<&mut bevy_egui::EguiContext, With<EguiRenderToTextureHandle>>,
) {
    for mut ctx in contexts.iter_mut() {
        egui::Window::new("Worldspace UI").show(ctx.get_mut(), |ui| {
            ui.label("I'm rendering to a texture in worldspace!");
        });
    }
}

fn setup_worldspace(
    mut images: ResMut<Assets<Image>>,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
    mut commands: Commands,
) {
    let output_texture = images.add({
        let size = Extent3d {
            width: 256,
            height: 256,
            depth_or_array_layers: 1,
        };
        let mut output_texture = Image {
            // You should use `0` so that the pixels are transparent.
            data: vec![0; (size.width * size.height * 4) as usize],
            ..default()
        };
        output_texture.texture_descriptor.usage |= TextureUsages::RENDER_ATTACHMENT;
        output_texture.texture_descriptor.size = size;
        output_texture
    });

    commands.spawn(PbrBundle {
        mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0).mesh()),
        material: materials.add(StandardMaterial {
            base_color: Color::WHITE,
            base_color_texture: Some(Handle::clone(&output_texture)),
            alpha_mode: AlphaMode::Blend,
            // Remove this if you want it to use the world's lighting.
            unlit: true,
            ..default()
        }),
        ..default()
    });
    commands.spawn(EguiRenderToTextureHandle(output_texture));
    commands.spawn(Camera3dBundle {
        transform: Transform::from_xyz(1.5, 1.5, 1.5).looking_at(Vec3::new(0., 0., 0.), Vec3::Y),
        ..default()
    });
}

( these are taken from your examples and just inserted the command )

@mvlabat
Copy link
Owner

mvlabat commented Oct 20, 2024

Thank you for the example! I was able to reproduce the issue.
Btw, moving commands.insert_resource( bevy::render::render_asset::RenderAssetBytesPerFrame::new(4096)); to the setup system fixes rendering in screen-space, but the worldspace rendering still crashes for me at

thread 'main' panicked at src/egui_render_to_texture_node.rs:248:72:
called `Option::unwrap()` on a `None` value

@mvlabat mvlabat added bug Something isn't working help wanted Extra attention is needed labels Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants