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

Merging meshes with different primitive topologies leads to rendering panics rather than returning an error #18546

Open
motgenror opened this issue Mar 25, 2025 · 3 comments · May be fixed by #18561
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior D-Domain-Agnostic Can be tackled by anyone with generic programming or Rust skills D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it!

Comments

@motgenror
Copy link

Bevy version

0.15.3

What you did

Created an empty Mesh, tried to merge another Mesh into it:

let mut merged = Mesh::new(
    PrimitiveTopology::LineList,
    RenderAssetUsages::default(),
);
merged.merge(&another_mesh);

And used the merged Mesh for rendering.

What went wrong

thread 'Compute Task Pool (17)' panicked at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\bevy_render-0.15.3\src\mesh\allocator.rs:656:48:
attempt to divide by zero
stack backtrace:
   0:     0x7ffd4f167211 - std::backtrace_rs::backtrace::dbghelp64::trace
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library\std\src\..\..\backtrace\src\backtrace\dbghelp64.rs:91      
   1:     0x7ffd4f167211 - std::backtrace_rs::backtrace::trace_unsynchronized
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library\std\src\..\..\backtrace\src\backtrace\mod.rs:66
   2:     0x7ffd4f167211 - std::sys::backtrace::_print_fmt
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library\std\src\sys\backtrace.rs:66
   3:     0x7ffd4f167211 - std::sys::backtrace::impl$0::print::impl$0::fmt
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library\std\src\sys\backtrace.rs:39
   4:     0x7ffd4f199d6a - core::fmt::rt::Argument::fmt
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library\core\src\fmt\rt.rs:177
   5:     0x7ffd4f199d6a - core::fmt::write
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library\core\src\fmt\mod.rs:1440
   6:     0x7ffd4f15cb57 - std::io::Write::write_fmt<std::sys::pal::windows::stdio::Stderr>
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library\std\src\io\mod.rs:1887
   7:     0x7ffd4f167055 - std::sys::backtrace::BacktraceLock::print
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library\std\src\sys\backtrace.rs:42
   8:     0x7ffd4f169ffd - std::panicking::default_hook::closure$1
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library\std\src\panicking.rs:295
   9:     0x7ffd4f169d60 - std::panicking::default_hook
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library\std\src\panicking.rs:322
  10:     0x7ffd4f16a8af - std::panicking::rust_panic_with_hook
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library\std\src\panicking.rs:828
  11:     0x7ffd4f16a612 - std::panicking::begin_panic_handler::closure$0
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library\std\src\panicking.rs:694
  12:     0x7ffd4f167cef - std::sys::backtrace::__rust_end_short_backtrace<std::panicking::begin_panic_handler::closure_env$0,never$>
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library\std\src\sys\backtrace.rs:168
  13:     0x7ffd4f16a24e - std::panicking::begin_panic_handler
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library\std\src\panicking.rs:692
  14:     0x7ffd4f1c4781 - core::panicking::panic_fmt
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library\core\src\panicking.rs:75
  15:     0x7ffd4f1c4e88 - core::panicking::panic_const::panic_const_div_by_zero
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library\core\src\panicking.rs:178
  16:     0x7ffd51e907df - core::num::impl$9::div_ceil
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\num\uint_macros.rs:3118
  17:     0x7ffd51e907df - bevy_render::mesh::allocator::MeshAllocator::allocate
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\bevy_render-0.15.3\src\mesh\allocator.rs:656    
  18:     0x7ffd51e8c368 - bevy_render::mesh::allocator::MeshAllocator::allocate_meshes
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\bevy_render-0.15.3\src\mesh\allocator.rs:427    
  19:     0x7ffd51e8c368 - bevy_render::mesh::allocator::allocate_and_free_meshes
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\bevy_render-0.15.3\src\mesh\allocator.rs:351    
  20:     0x7ffd51e5cec0 - core::ops::function::FnMut::call_mut
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:166
  21:     0x7ffd51e5cec0 - core::ops::function::impls::impl$3::call_mut
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:294
  22:     0x7ffd51e5cec0 - bevy_ecs::system::function_system::impl$43::run::call_inner
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\bevy_ecs-0.15.3\src\system\function_system.rs:1002
  23:     0x7ffd51e5cec0 - bevy_ecs::system::function_system::impl$43::run
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\bevy_ecs-0.15.3\src\system\function_system.rs:1005
  24:     0x7ffd51e5cec0 - bevy_ecs::system::function_system::impl$9::run_unsafe<void (*)(bevy_ecs::change_detection::ResMut<bevy_render::mesh::allocator::MeshAllocator>,bevy_ecs::change_detection::Res<bevy_render::mesh::allocator::MeshAllocatorSettings>,bevy_ecs::change_detection::
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\bevy_ecs-0.15.3\src\system\function_system.rs:800
  25:     0x7ffd530d3d3d - core::hint::black_box
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\hint.rs:475   
  26:     0x7ffd530d3d3d - bevy_ecs::schedule::executor::__rust_begin_short_backtrace::run_unsafe
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\bevy_ecs-0.15.3\src\schedule\executor\mod.rs:154
  27:     0x7ffd53085ad9 - bevy_ecs::schedule::executor::multi_threaded::impl$5::spawn_system_task::async_block$0::closure$0
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\bevy_ecs-0.15.3\src\schedule\executor\multi_threaded.rs:606
  28:     0x7ffd53085ad9 - core::ops::function::FnOnce::call_once
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:250
  29:     0x7ffd53085ad9 - core::panic::unwind_safe::impl$25::call_once
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\panic\unwind_safe.rs:272
  30:     0x7ffd53085ad9 - std::panicking::try::do_call
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\panicking.rs:584
  31:     0x7ffd53085ad9 - std::panicking::try
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\panicking.rs:547
  32:     0x7ffd53085ad9 - std::panic::catch_unwind
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\panic.rs:358   
  33:     0x7ffd53085ad9 - bevy_ecs::schedule::executor::multi_threaded::impl$5::spawn_system_task::async_block$0
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\bevy_ecs-0.15.3\src\schedule\executor\multi_threaded.rs:600
  34:     0x7ffd53085ad9 - core::panic::unwind_safe::impl$28::poll
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\panic\unwind_safe.rs:297
  35:     0x7ffd53085ad9 - futures_lite::future::impl$11::poll::closure$0
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\futures-lite-2.6.0\src\future.rs:656
  36:     0x7ffd53085ad9 - core::panic::unwind_safe::impl$25::call_once
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\panic\unwind_safe.rs:272
  37:     0x7ffd53085ad9 - std::panicking::try::do_call
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\panicking.rs:584
  38:     0x7ffd53085ad9 - std::panicking::try
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\panicking.rs:547
  39:     0x7ffd53085ad9 - std::panic::catch_unwind
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\panic.rs:358   
  40:     0x7ffd53085ad9 - futures_lite::future::impl$11::poll<core::panic::unwind_safe::AssertUnwindSafe<enum2$<bevy_ecs::schedule::executor::multi_threaded::impl$5::spawn_system_task::async_block_env$0> > >
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\futures-lite-2.6.0\src\future.rs:656
  41:     0x7ffd530ffaee - async_executor::impl$5::spawn_inner::async_block$0<enum2$<core::result::Result<tuple$<>,alloc::boxed::Box<dyn$<core::any::Any,core::marker::Send>,alloc::alloc::Global> > >,futures_lite::future::CatchUnwind<core::panic::unwind_safe::AssertUnwindSafe<enum2$<
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\async-executor-1.13.1\src\lib.rs:250
  42:     0x7ffd530fdc2f - async_task::raw::impl$3::run::closure$1
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\async-task-4.7.1\src\raw.rs:550
  43:     0x7ffd530fdc2f - core::ops::function::FnOnce::call_once
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:250
  44:     0x7ffd530fdc2f - core::panic::unwind_safe::impl$25::call_once
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\panic\unwind_safe.rs:272
  45:     0x7ffd530fdc2f - std::panicking::try::do_call
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\panicking.rs:584
  46:     0x7ffd530fdc2f - std::panicking::try
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\panicking.rs:547
  47:     0x7ffd530fdc2f - std::panic::catch_unwind
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\panic.rs:358   
  48:     0x7ffd530fdc2f - async_task::raw::RawTask<enum2$<async_executor::impl$5::spawn_inner::async_block_env$0<enum2$<core::result::Result<tuple$<>,alloc::boxed::Box<dyn$<core::any::Any,core::marker::Send>,alloc::alloc::Global> > >,futures_lite::future::CatchUnwind<core::panic::u
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\async-task-4.7.1\src\raw.rs:549
  49:     0x7ffd5310bae1 - async_task::runnable::Runnable<tuple$<> >::run
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\async-task-4.7.1\src\runnable.rs:781
  50:     0x7ffd5310bae1 - async_executor::impl$13::run::async_fn$0::async_block$0
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\async-executor-1.13.1\src\lib.rs:741
  51:     0x7ffd5310bae1 - futures_lite::future::impl$7::poll
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\futures-lite-2.6.0\src\future.rs:457
  52:     0x7ffd5310bae1 - async_executor::impl$13::run::async_fn$0
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\async-executor-1.13.1\src\lib.rs:748
  53:     0x7ffd5310bae1 - async_executor::impl$5::run::async_fn$0
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\async-executor-1.13.1\src\lib.rs:344
  54:     0x7ffd5310bae1 - futures_lite::future::block_on::closure$0
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\futures-lite-2.6.0\src\future.rs:99
  55:     0x7ffd5310bae1 - std::thread::local::LocalKey<core::cell::RefCell<tuple$<parking::Parker,core::task::wake::Waker> > >::try_with
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\thread\local.rs:308
  56:     0x7ffd5310bae1 - std::thread::local::LocalKey<core::cell::RefCell<tuple$<parking::Parker,core::task::wake::Waker> > >::with
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\thread\local.rs:272
  57:     0x7ffd5310bae1 - futures_lite::future::block_on<enum2$<core::result::Result<tuple$<>,async_channel::RecvError> >,enum2$<async_executor::impl$5::run::async_fn_env$0<enum2$<core::result::Result<tuple$<>,async_channel::RecvError> >,futures_lite::future::Or<enum2$<bevy_tasks::
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\futures-lite-2.6.0\src\future.rs:78
  58:     0x7ffd53106aee - bevy_tasks::task_pool::impl$2::new_internal::closure$0::closure$0::closure$0::closure$0
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\bevy_tasks-0.15.3\src\task_pool.rs:171
  59:     0x7ffd53106aee - std::panicking::try::do_call
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\panicking.rs:584
  60:     0x7ffd53106aee - std::panicking::try
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\panicking.rs:547
  61:     0x7ffd53106aee - std::panic::catch_unwind
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\panic.rs:358   
  62:     0x7ffd53106aee - bevy_tasks::task_pool::impl$2::new_internal::closure$0::closure$0::closure$0
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\bevy_tasks-0.15.3\src\task_pool.rs:165
  63:     0x7ffd53106aee - std::thread::local::LocalKey<async_executor::LocalExecutor>::try_with
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\thread\local.rs:308
  64:     0x7ffd53106aee - std::thread::local::LocalKey<async_executor::LocalExecutor>::with
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\thread\local.rs:272
  65:     0x7ffd53106aee - bevy_tasks::task_pool::impl$2::new_internal::closure$0::closure$0
                               at C:\Users\user\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\bevy_tasks-0.15.3\src\task_pool.rs:158
  66:     0x7ffd53106aee - std::sys::backtrace::__rust_begin_short_backtrace<bevy_tasks::task_pool::impl$2::new_internal::closure$0::closure_env$0,tuple$<> >
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\sys\backtrace.rs:152
  67:     0x7ffd5310f6dd - core::ops::function::FnOnce::call_once<std::thread::impl$0::spawn_unchecked_::closure_env$1<bevy_tasks::task_pool::impl$2::new_internal::closure$0::closure_env$0,tuple$<> >,tuple$<> >
                               at C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:250
  68:     0x7ffd4f17c81d - alloc::boxed::impl$28::call_once
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library\alloc\src\boxed.rs:1993
  69:     0x7ffd4f17c81d - alloc::boxed::impl$28::call_once
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library\alloc\src\boxed.rs:1993
  70:     0x7ffd4f17c81d - std::sys::pal::windows::thread::impl$0::new::thread_start
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library\std\src\sys\pal\windows\thread.rs:56
  71:     0x7ffdb6e7e8d7 - BaseThreadInitThunk
  72:     0x7ffdb7a7bf6c - RtlUserThreadStart
Encountered a panic in system `bevy_render::mesh::allocator::allocate_and_free_meshes`!

What were you expecting?

Error handling.

  1. Bevy to catch the invalid mesh earlier, and to point out the entity id and its components, to make it possible to find the mesh in the code. Without any diagnostic info, you're left guessing what Mesh it was.

  2. Mesh::merge to return a Result, rather than produce a Mesh that Bevy renderer panics on.

Additional information

Pretty sure merge produced an empty mesh because another_mesh had some NaNs in its values.

@motgenror motgenror added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Mar 25, 2025
@alice-i-cecile
Copy link
Member

Is this fixed on main following #17475?

@alice-i-cecile alice-i-cecile added A-Rendering Drawing game state to the screen 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 Mar 25, 2025
@Pnoenix
Copy link

Pnoenix commented Mar 25, 2025

We found the problem on discord (message), turns out that you can merge meshes that have different primitive topologies, without a panic or an error. The pull request mentioned above doesn't fix this, it only checks if the attributes match, not the primitive topologies. It wouldn't be a very hard fix though, just an if statement to check if they match, and a rework of MergeMeshError struct.

@alice-i-cecile alice-i-cecile added S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it! and removed S-Needs-Investigation This issue requires detective work to figure out what's going wrong labels Mar 25, 2025
@alice-i-cecile alice-i-cecile changed the title Panic on an empty Mesh Merging meshes with different primitive topologies panics rather than returning an error Mar 25, 2025
@alice-i-cecile alice-i-cecile added D-Straightforward Simple bug fixes and API improvements, docs, test and examples D-Domain-Agnostic Can be tackled by anyone with generic programming or Rust skills labels Mar 25, 2025
@motgenror
Copy link
Author

Merging meshes with different primitive topologies panics rather than returning an error

Panics in Bevy Render, not while merging. Just to clarify.

@alice-i-cecile alice-i-cecile changed the title Merging meshes with different primitive topologies panics rather than returning an error Merging meshes with different primitive topologies leads to rendering panics rather than returning an error Mar 25, 2025
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 D-Domain-Agnostic Can be tackled by anyone with generic programming or Rust skills D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants