rust-windowing/glutin

Cannot create CGL context

tronical opened this issue · 9 comments

With the latest glutin-winit release (0.4.2), creating a (C)GL context does not work anymore. This is reproducible for me with the window example:

 % RUST_BACKTRACE=1 cargo run --example window
    Finished dev [unoptimized + debuginfo] target(s) in 0.26s
     Running `/Users/simon/src/glutin/target/debug/examples/window`
Picked a config with 0 samples
thread 'main' panicked at 'invalid message send to -[NSOpenGLContext CGLContextObj]: expected return to have type code '^{_CGLContextObject=^{__GLIContextRec}{__GLIFunctionDispatchRec=^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?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^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?^?}^{_CGLPrivateObject}^v}', but found '^{_CGLContextObject=}'', glutin/src/api/cgl/appkit.rs:38:1
stack backtrace:
   0: rust_begin_unwind
             at /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/std/src/panicking.rs:593:5
   1: core::panicking::panic_fmt
             at /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/core/src/panicking.rs:67:14
   2: objc2::message::panic_verify
             at /Users/simon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.4.1/src/message/mod.rs:83:5
   3: objc2::message::msg_send_check
             at /Users/simon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.4.1/src/message/mod.rs:71:5
   4: objc2::message::MessageReceiver::send_message
             at /Users/simon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.4.1/src/message/mod.rs:231:13
   5: glutin::api::cgl::appkit::NSOpenGLContext::CGLContextObj
             at /Users/simon/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.4.1/src/macros/__attribute_helpers.rs:126:21
   6: glutin::api::cgl::context::<impl glutin::api::cgl::display::Display>::create_context
             at /Users/simon/src/glutin/glutin/src/api/cgl/context.rs:57:33
   7: <glutin::display::Display as glutin::display::GlDisplay>::create_context
             at /Users/simon/src/glutin/glutin/src/display.rs:311:43
   8: glutin_examples::main
             at /Users/simon/src/glutin/glutin_examples/src/lib.rs:90:9
   9: window::main
             at ./window.rs:6:5
  10: core::ops::function::FnOnce::call_once
             at /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

This is on macOS Sonoma 14.0 on an Intel MBP.

We have other rendering code that doesn't use glutin-winit but glutin directly and that still works. I'll try to dig a little to find out what's happening.

Given that glutin-winit doesn't use the macOS specific code it would mean that something is called in the wrong time?

Could also be a regression of objc-0.4 update.

Could also be a regression of objc-0.4 update.

Indeed, reverting that change "fixes" it.

It seems that this is the code that fails:

        if config.inner.transparency {
            let opacity = 0;
            super::check_error(unsafe {
                CGLSetParameter(raw.CGLContextObj().cast(), cgl::kCGLCPSurfaceOpacity, &opacity)
            })?;
        }

maybe the bindings got somehow wrong? Or the call itself is not exactly like it was before?

I have a feeling that the cast stuff is wrong? I don't think CGL stuff wants Obj but rather raw stuff?

More specifically, this fails let _ = raw.CGLContextObj();

It seems to be before the CGLSetParameter call, it's already the CGLContextObj call that fails. Suddenly the error message also makes "sense": invalid message send to -[NSOpenGLContext CGLContextObj]: expected return to have type code 'XXX' , but found 'YYY'. I don't understand the encoding bit though.

Declaration of the method:

 #[method(CGLContextObj)]
        pub(crate) fn CGLContextObj(&self) -> *mut CGLContextObj;

and encoding:

#[repr(C)]
pub struct CGLContextObj {
    __inner: [u8; 0],
}

unsafe impl RefEncode for CGLContextObj {
    const ENCODING_REF: Encoding = Encoding::Pointer(&Encoding::Struct("_CGLContextObject", &[]));
}

I wonder where the expected ^{_CGLContextObject=^{__GLIContextRec}.... comes from. I can see that the above would give `'^{_CGLContextObject=}''.

@tronical can you test #1641 ?

Yep, that fixes it. Thanks a lot!