Sprites with many distinct images render far slower in 0.20 (bindless sprite materials)

Open
#26,508 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
rust

Research direction

Start by reproducing the regression with the 6,400-sprite example in the issue, comparing bindless and NO_BINDLESS=1 runs. Read the sprite material and bindless batching paths, then profile how material bind groups change across Transparent2d draws. The issue provides perf hot paths but names no source files or tests; done means identifying and fixing the regression while preserving the reported shared-atlas performance benefit.

Written by the indexing model from the issue text.

Description

C-Bug C-Performance S-Needs-Triage

Bevy version

  • Original: 0.19.1 (wgpu 29.0.4)
  • Current: 0.20.1 (wgpu 30.0.1)
  • Also reproduces on main at 26df274845b039cacccf34bde524344241ffb2e6 (wgpu 30.0.1)

Relevant system information

  • cargo 1.99.1 (5f94df478 2026-08-27)
  • Fedora Linux 44, kernel 7.2.15, Vulkan, NVIDIA GeForce RTX 2080 (driver 615.78.08)

What's performing poorly?

Drawing thousands of Sprites that each have their own Image. Disabling WgpuFeatures::TEXTURE_BINDING_ARRAY (which turns bindless off) brings performance close to 0.19 again.

Median frame time of the repro below. Vsync is off, 15 s runs, and the median is taken over the second half of the frames:

Build 0.19.1 0.20.1 0.20.1, NO_BINDLESS=1 main main, NO_BINDLESS=1
release 5.6 ms 362 ms 8.0 ms 402 ms 8.4 ms
dev 59 ms 2185 ms 85 ms 2883 ms 96 ms

Bindless does help where it should: in bevy_image_font's tests, 31,950 sprites sharing a few atlas images render about 2× faster with bindless than without.

Repro (6,400 sprites, each with its own image):

use bevy::asset::RenderAssetUsages;
use bevy::prelude::*;
use bevy::render::RenderPlugin;
use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat};
use bevy::render::settings::{RenderCreation, WgpuFeatures, WgpuSettings};
use bevy::window::PresentMode;

fn main() {
    // Run with `NO_BINDLESS=1` to disable bindless.
    let no_bindless = std::env::var_os("NO_BINDLESS").is_some();
    App::new()
        .add_plugins(
            DefaultPlugins
                .set(RenderPlugin {
                    render_creation: RenderCreation::Automatic(Box::new(WgpuSettings {
                        disabled_features: no_bindless
                            .then_some(WgpuFeatures::TEXTURE_BINDING_ARRAY),
                        ..default()
                    })),
                    ..default()
                })
                .set(WindowPlugin {
                    primary_window: Some(Window {
                        present_mode: PresentMode::AutoNoVsync,
                        ..default()
                    }),
                    ..default()
                }),
        )
        .add_systems(Startup, setup)
        .add_systems(Update, log_frame_time)
        .run();
}

// 6400 sprites, each with its own small image.
fn setup(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
    commands.spawn(Camera2d);
    for i in 0..6400u32 {
        let image = images.add(Image::new_fill(
            Extent3d {
                width: 24,
                height: 12,
                depth_or_array_layers: 1,
            },
            TextureDimension::D2,
            &[(i % 256) as u8, 128, 255, 255],
            TextureFormat::Rgba8UnormSrgb,
            RenderAssetUsages::RENDER_WORLD,
        ));
        let (x, y) = ((i % 80) as f32, (i / 80) as f32);
        commands.spawn((
            Sprite::from_image(image),
            Transform::from_xyz(x * 24. - 960., y * 12. - 480., 0.),
        ));
    }
}

fn log_frame_time(time: Res<Time<Real>>) {
    info!("frame time: {:?}", time.delta());
}

Before and After Traces

No Tracy traces were captured. The profile below is from perf on 0.20.1 (dev build, frame pointers) of an equivalent scene with 6,390 sprites, each with its own image. Percentages include children and cover the whole run:

83.4%  wgpu::CommandEncoder::finish → wgpu_core::command::render::encode_render_pass
54.1%    draw_indexed → State::flush_bindings → pass::flush_bindings_helper
39.0%      CommandBufferTextureMemoryActions::register_init_action
22.3%      TextureInitTracker::check_action
29.1%    set_bind_group → UsageScope::merge_bind_group → TextureUsageScope::merge_bind_group
21.8%      TextureUsageScope::merge_single
  • Before: not captured
  • After: as above

I'm reporting perf data since I already did a bunch of profiling of my own before heading here to read the performance regression issue template and profiling documentation here. Let me know if it's not sufficient and I can do more profiling using your preferred tools, too. Still, this performance issue is incredibly obvious from the reproduction above, so hopefully that'll suffice.

Additional information

  • Theory: since 0.20, a Sprite is a Mesh2d with a SpriteMeshMaterial, and SpriteMeshMaterial is #[bindless]. Each distinct image gets its own material, and materials share bindless slabs of up to AUTO_BINDLESS_SLAB_RESOURCE_LIMIT (2048) textures. Transparent2d is a sorted phase, so consecutive draws keep switching between slabs. On each switch, wgpu-core re-tracks and re-checks the initialization of every texture in the slab's binding array, which gives O(draws × slab size) per frame.
  • Related:
    • gfx-rs/wgpu#9738 has the same hot path (flush_bindings_helper / register_init_action running for every bindless texture on every draw). It was closed after the reporter moved the bind group that changed per draw ahead of the bindless group. That doesn't apply here: the 2D material group is already last (index 2), and TrackedRenderPass skips re-setting a group that is already bound, so the material bind group itself must be changing between draws.
    • gfx-rs/wgpu#7856 (open) would make merge_bind_group cheaper.
  • Workaround: WgpuSettings { disabled_features: Some(WgpuFeatures::TEXTURE_BINDING_ARRAY), .. }, at the cost of slower rendering for sprites that share images.
  • Found via the visual tests of bevy_image_font, where each pre-rendered text entity is a sprite with its own image.
Dominant language
Rust
Stars
49.8k
Forks
5.1k
Avg merge
3d 4h
Merged PRs (30d)
136

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from bevyengine/bevy

All issues in bevyengine/bevy

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.