Description
Bevy version
0.3.0 509b138
Operating system & version
Windows 10
What you did
Load 2 sprite texture atlas back to back in the setup like so... (This is a modified version of the sprite_sheet.rs example that is not animated and loads an additional and different sprite sheet.)
I am not sure if a specific sprite sheet is required to trigger the error.
Note: The error can be inconsistent to trigger.
use bevy::prelude::*;
fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.run();
}
fn setup(
commands: &mut Commands,
asset_server: Res,
mut texture_atlases: ResMut<Assets>,
) {
let texture_handle1: Handle<Texture> = asset_server.load("sprite_sheet1");
let texture_handle2: Handle<Texture> = asset_server.load("sprite_sheet1");
let texture_atlas1 = TextureAtlas::from_grid(texture_handle1, Vec2::new(x, y), c, r);
let texture_atlas2 = TextureAtlas::from_grid(texture_handle2, Vec2::new(x, y), c, r);
let texture_atlas_handle1 = texture_atlases.add(texture_atlas1);
let texture_atlas_handle2 = texture_atlases.add(texture_atlas2);
commands
.spawn(Camera2dBundle::default())
.spawn(SpriteSheetBundle {
texture_atlas: texture_atlas_handle1,
transform: Transform::from_scale(Vec3::splat(6.0)),
..Default::default()
})
.with(Timer::from_seconds(0.1, true));
}
What you expected to happen
No crash
What actually happened
An error occurred.
thread 'main' panicked at 'range end index 1768 out of range for slice of length 1024', ...\bevy\crates\bevy_render\src\render_graph\nodes\render_resources_node.rs:336:26
Additional information
Error occurs in render_resources_node.rs file
I am not too familiar with rendering code, but after doing some investigating, the first part of the problem occurs in the loop in (lines 681-687). In the loop, when the prepare_uniform_buffers() function is called, it updates the field required_staging_buffer_size field with the size required determined by the byte len of the buffer. The problem is that if one of the render_resource buffer byte len differs from the buffer_array's item size then if the buffer array needs to be resized then it will resize the buffer to a value less than what is needed because the buffer_arrays item size is not correct.
The second part of the issue seems to be caused by the set_required_staging_buffer_size_to_max() function in the render_resources_node.rs file called from line ~692 in the same file. The required_staging_buffer_size field is updated/resized with a size that is less than what the buffer requires. This is because the item_size field is incorrect and so when the new_size is calculated (by multiplying the buffer len), it is less than what is required by the buffer.
For example, the prepare_uniform_buffers() is called and the required_staging_buffer_size is set to a new size of 8 + 1760 to get a value of 1768 for the buffer. The buffer_array.item_size = 256. Then when the set_required_staging_buffer_size_to_max() is called, it will update/resize the required_staging_buffer_size to item_size(256) * buffer_len(4) = 1024. The error will happen because of this, 1768 > 1024 and an index out of range will occur down the line.
Seems to me that the item_size for the buffer_array needs to be updated properly.