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

Create new texture in egui makes the framerate very low #283

Open
crane-may opened this issue May 28, 2024 · 3 comments
Open

Create new texture in egui makes the framerate very low #283

crane-may opened this issue May 28, 2024 · 3 comments

Comments

@crane-may
Copy link

In my application, I need to generate a new texture each time in egui update. I found that update_egui_textures_system sometimes takes more than 0.5 seconds to run. Then I located the code that was causing the performance:

pub(crate) fn color_image_as_bevy_image(
    egui_image: &egui::ColorImage,
    sampler_descriptor: ImageSampler,
) -> Image {
    let pixels = egui_image
        .pixels
        .iter()
        // We unmultiply Egui textures to premultiply them later in the fragment shader.
        // As user textures loaded as Bevy assets are not premultiplied (and there seems to be no
        // convenient way to convert them to premultiplied ones), we do the this with Egui ones.
        .flat_map(|color| color.to_srgba_unmultiplied())
        .collect();

...

My guess is to_srgba_unmultiplied is causing the slowdown. Then I replaced it with the crate fast-srgb8, which is x10 times faster!

pub(crate) fn color_image_as_bevy_image(
    egui_image: &egui::ColorImage,
    sampler_descriptor: ImageSampler,
) -> Image {
    let pixels = egui_image
        .pixels
        .iter()
        // We unmultiply Egui textures to premultiply them later in the fragment shader.
        // As user textures loaded as Bevy assets are not premultiplied (and there seems to be no
        // convenient way to convert them to premultiplied ones), we do the this with Egui ones.
        .flat_map(|color| fast_srgb8::f32x4_to_srgb8(color.to_normalized_gamma_f32()))
        .collect();

...

I hope it helps if you are having this same problem.

@gogo2077
Copy link

Thank you, this is very helpful.

@mvlabat
Copy link
Owner

mvlabat commented Jun 20, 2024

Hi! Thank you for sharing. I haven't got a chance to test it myself yet. Btw, have you tried running your app in the release mode, or at least compiling dependencies with optimisations?

Will the slowdown still be this significant?

@crane-may
Copy link
Author

Hi! Thank you for sharing. I haven't got a chance to test it myself yet. Btw, have you tried running your app in the release mode, or at least compiling dependencies with optimisations?

Will the slowdown still be this significant?

Sorry for not paying attention to this message and replying a bit late.

Yes, I was testing in release mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants