asny / three-d

2D/3D renderer - makes it simple to draw stuff across platforms (including web)
MIT License
1.34k stars 110 forks source link

Invalid enum on OpenGL ES 3.2, how to debug? #325

Closed surban closed 1 year ago

surban commented 1 year ago

Hi.

I am using three-d 0.14 on an Android device with OpenGL ES 3.2. It is working fine mostly, but in some cases I am getting ContextError("Invalid enum") with the following stack trace:

E/RUST    (15804): panic: thread '<unnamed>' panicked at 'Unexpected rendering error occured: ContextError("Invalid enum")': /home/surban/.cargo/registry/src/github.com-1ecc6299db9ec823/three-d-0.14.0/src/core/program.rs:508
E/RUST    (15804):    0: <backtrace::capture::Backtrace as core::default::Default>::default
E/RUST    (15804):              at home/surban/.cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.67/src/capture.rs:410:9
E/RUST    (15804):    1: log_panics::Config::install_panic_hook::{{closure}}
E/RUST    (15804):              at home/surban/.cargo/registry/src/github.com-1ecc6299db9ec823/log-panics-2.1.0/src/lib.rs:115:29
E/RUST    (15804):    2: <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call
E/RUST    (15804):              at rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/alloc/src/boxed.rs:2032:9
E/RUST    (15804):       std::panicking::rust_panic_with_hook
E/RUST    (15804):              at rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/std/src/panicking.rs:692:13
E/RUST    (15804):    3: std::panicking::begin_panic_handler::{{closure}}
E/RUST    (15804):              at rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/std/src/panicking.rs:579:13
E/RUST    (15804):    4: std::sys_common::backtrace::__rust_end_short_backtrace
E/RUST    (15804):              at rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/std/src/sys_common/backtrace.rs:137:18
E/RUST    (15804):    5: rust_begin_unwind
E/RUST    (15804):              at rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/std/src/panicking.rs:575:5
E/RUST    (15804):    6: core::panicking::panic_fmt
E/RUST    (15804):              at rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/panicking.rs:64:14
E/RUST    (15804):    7: core::result::unwrap_failed
E/RUST    (15804):              at rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/result.rs:1791:5
E/RUST    (15804):    8: core::result::Result<T,E>::expect
E/RUST    (15804):              at rustc/d5a82bbd26e1ad8b7401f6a718a9c57c96905483/library/core/src/result.rs:1070:23
E/RUST    (15804):    9: three_d::core::program::Program::draw_subset_of_elements
E/RUST    (15804):              at home/surban/.cargo/registry/src/github.com-1ecc6299db9ec823/three-d-0.14.0/src/core/program.rs:506:9
E/RUST    (15804):   10: three_d::core::program::Program::draw_elements
E/RUST    (15804):              at home/surban/.cargo/registry/src/github.com-1ecc6299db9ec823/three-d-0.14.0/src/core/program.rs:462:9
E/RUST    (15804):   11: three_d::renderer::geometry::mesh::Mesh::draw
E/RUST    (15804):              at home/surban/.cargo/registry/src/github.com-1ecc6299db9ec823/three-d-0.14.0/src/renderer/geometry/mesh.rs:108:13
E/RUST    (15804):   12: <three_d::renderer::geometry::mesh::Mesh as three_d::renderer::geometry::Geometry>::render_with_material::{{closure}}
E/RUST    (15804):              at home/surban/.cargo/registry/src/github.com-1ecc6299db9ec823/three-d-0.14.0/src/renderer/geometry/mesh.rs:184:21
E/RUST    (15804):   13: three_d::core::context::Context::program
E/RUST    (15804):              at home/surban/.cargo/registry/src/github.com-1ecc6299db9ec823/three-d-0.14.0/src/core/context.rs:67:9
E/RUST    (15804):   14: <three_d::renderer::geometry::mesh::Mesh as three_d::renderer::geometry::Geometry>::render_with_material
E/RUST    (15804):              at home/surban/.cargo/registry/src/github.com-1ecc6299db9ec823/three-d-0.14.0/src/renderer/geometry/mesh.rs:178:9
E/RUST    (15804):   15: <three_d::renderer::geometry::circle::Circle as three_d::renderer::geometry::Geometry>::render_with_material
E/RUST    (15804):              at home/surban/.cargo/registry/src/github.com-1ecc6299db9ec823/three-d-0.14.0/src/renderer/geometry/circle.rs:63:9
E/RUST    (15804):   16: <three_d::renderer::object::gm::Gm<G,M> as three_d::renderer::geometry::Geometry>::render_with_material
E/RUST    (15804):              at home/surban/.cargo/registry/src/github.com-1ecc6299db9ec823/three-d-0.14.0/src/renderer/object/gm.rs:44:9
E/RUST    (15804):   17: <three_d::renderer::object::gm::Gm<G,M> as three_d::renderer::object::Object>::render
E/RUST    (15804):              at home/surban/.cargo/registry/src/github.com-1ecc6299db9ec823/three-d-0.14.0/src/renderer/object/gm.rs:67:9
E/RUST    (15804):   18: <xxx::XxxWidget as xxx::gl_widget::GlWidget>::redraw_three_d::{{closure}}
E/RUST    (15804):   19: three_d::core::render_target::RenderTarget::write_partially
E/RUST    (15804):              at home/surban/.cargo/registry/src/github.com-1ecc6299db9ec823/three-d-0.14.0/src/core/render_target.rs:97:9
E/RUST    (15804):   20: three_d::core::render_target::RenderTarget::write
E/RUST    (15804):              at home/surban/.cargo/registry/src/github.com-1ecc6299db9ec823/three-d-0.14.0/src/core/render_target.rs:88:9
E/RUST    (15804):   21: <xxx:XxxWidget as xxx::gl_widget::GlWidget>::redraw_three_d
E/RUST    (15804):   22: xxx_app_support::gl_widget_flutter::render_callback
E/RUST    (15804):   23: Java_de_xxx_1app_Support_textureRender
E/RUST    (15804):   24: art_quick_generic_jni_trampoline
E/RUST    (15804):   25: nterp_helper
E/RUST    (15804):   26: art_quick_invoke_stub
E/RUST    (15804):   27: _ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc
E/RUST    (15804):   28: _ZN3art35InvokeVirtualOrInterfaceWithJValuesIPNS_9ArtMethodEEENS_6JValueERKNS_33ScopedObjectAccessAlreadyRunnableEP8_jobjectT_PK6jvalue
E/RUST    (15804):   29: _ZN3art6Thread14CreateCallbackEPv
E/RUST    (15804):   30: _ZL15__pthread_startPv
E/RUST    (15804):   31: __start_thread
F/libc    (15804): Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 16957 (Thread-375), pid 15804 (bs.rxxx_app)

Could you provide some guidance on how to find the location where the invalid OpenGL enum is coming from?

It does not happen on desktop OpenGL, so I guess this must be some limitation of OpenGL ES.

asny commented 1 year ago

Hi. Thanks for the report 👍 Yes certainly. It's failing at an error check after rendering here so if you could insert that error check context.error_check() everywhere where relevant and pinpoint the exact OpenGL call that causes trouble, that would be most helpful 🙂

surban commented 1 year ago

The problem is with TEXTURE_CUBE_MAP_SEAMLESS. Seems that it is not supported by OpenGL ES. Commenting out that line makes the error disappear.

surban commented 1 year ago

Also I've found an easier method for debugging.

First I've switched from X11 to Wayland, which uses OpenGL ES for all rendering. This allows me to debug on my workstation without having to deal with Android.

Then I loaded the program in rust-gdb and set a breakpoint on _mesa_error. This function gets called immediately in case of an error. From there a backtrace directly shows the offending OpenGL call.

asny commented 1 year ago

The problem is with TEXTURE_CUBE_MAP_SEAMLESS. Seems that it is not supported by OpenGL ES. Commenting out that line makes the error disappear.

Nice, that seems easy to fix. I tried to make a fix in #327, can you test it? I only tested on desktop and web, but it works on both.

Also I've found an easier method for debugging.

First I've switched from X11 to Wayland, which uses OpenGL ES for all rendering. This allows me to debug on my workstation without having to deal with Android.

Then I loaded the program in rust-gdb and set a breakpoint on _mesa_error. This function gets called immediately in case of an error. From there a backtrace directly shows the offending OpenGL call.

Oh wow, that's some next level debugging 🧙

asny commented 1 year ago

Hopefully fixed in version 0.14.1 and also on master.

surban commented 1 year ago

Sorry, no time this week. I will verify next week.

asny commented 1 year ago

No problem at all, take your time 👍 I was just 99% sure that it works, and if it doesn't, I'll just make a new release, it's no problem 🙂

surban commented 1 year ago

Thanks. It's working now!