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

SpriteBatches can be overwritten in prepare_sprite_image_bind_groups #17351

Open
ickshonpe opened this issue Jan 13, 2025 · 1 comment
Open
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior S-Needs-Investigation This issue requires detective work to figure out what's going wrong

Comments

@ickshonpe
Copy link
Contributor

ickshonpe commented Jan 13, 2025

Bevy version

Main + 0.15

What you did

fn main() {
    App::new()
        .add_plugins(DefaultPlugins.set(WindowPlugin {
            primary_window: Some(Window {
                resolution: WindowResolution::new(1000., 1000.).with_scale_factor_override(1.),
                title: "Window 1".to_string(),
                ..default()
            }),
            ..default()
        }))
        .add_systems(Startup, setup)
        .run();
}

fn setup(mut commands: Commands) {
    // The primary window is the default camera target and 0 is the default render layer, there's no need to set these normally
    // the explicitness is just to make this example as clear as possible.

    commands.spawn((
        Camera2d::default(),
        Camera {
            target: RenderTarget::Window(WindowRef::Primary),
            ..default()
        },
        RenderLayers::layer(0),
    ));

    // Spawn a second window
    let window_2 = commands
        .spawn(Window {
            title: "Window 2".to_owned(),
            resolution: WindowResolution::new(1000., 1000.).with_scale_factor_override(1.),
            ..default()
        })
        .id();

    commands.spawn((
        Camera2d::default(),
        Camera {
            target: RenderTarget::Window(WindowRef::Entity(window_2)),
            ..default()
        },
        RenderLayers::layer(1),
    ));

    // Text drawn to window 1
    commands.spawn((Text2d::new("1"), RenderLayers::layer(0)));

    // Text drawn to window 2
    commands.spawn((Text2d::new("2"), RenderLayers::layer(1)));

    commands.spawn((
        Text2d::new("3"),
        Transform::from_translation(-25. * Vec3::Y),
        RenderLayers::from_layers(&[0, 1]),
    ));
}

What went wrong

In the second window it should display 2 and 3, but sometimes it displays the wrong glyphs.

Additional information

Font's use a texture atlas, so there is only one texture.
So each render phase generates a single SpriteBatch.
Then each SpriteBatch is inserted on the render entity for the first phase item in that batch.
This means that if the phase item corresponding to the "3" glyph is the first item in each batch, both SpriteBatchs will be inserted on the same render entity, overwriting one of the batches.
The "3" glyph sprite is visible in both views so that single SpriteBatch is then drawn for both views.

It also seems odd how the same sprite batch goes to each view as well.

@ickshonpe ickshonpe added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled A-Rendering Drawing game state to the screen labels Jan 13, 2025
@ickshonpe ickshonpe changed the title Sprite batches can get overwritten in prepare_sprite_image_bind_groups SpriteBatches are overwritten in prepare_sprite_image_bind_groups Jan 13, 2025
@ickshonpe ickshonpe changed the title SpriteBatches are overwritten in prepare_sprite_image_bind_groups SpriteBatches can be overwritten in prepare_sprite_image_bind_groups Jan 13, 2025
@alice-i-cecile alice-i-cecile added S-Needs-Investigation This issue requires detective work to figure out what's going wrong and removed S-Needs-Triage This issue needs to be labelled labels Jan 13, 2025
@ickshonpe
Copy link
Contributor Author

ickshonpe commented Jan 13, 2025

Needs someone more comfortable with the rendering abstractions to take a look, it's not difficult to fix but I'm not sure how to get it done without some ugly hacks

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 S-Needs-Investigation This issue requires detective work to figure out what's going wrong
Projects
None yet
Development

No branches or pull requests

2 participants