madsmtm / objc2

Bindings to Apple's frameworks in Rust
https://docs.rs/objc2/
MIT License
281 stars 35 forks source link

`currentDrawable` property is not accessible in `MTKView` #570

Closed dmbfm closed 1 week ago

dmbfm commented 3 months ago

When using MTKView for rendering with the Metal API there seems to be no way to access the currentDrawable property of MTKView so we can present it.

Currently I do this as a workaround and it works fine:


type ID<T> = Id<ProtocolObject<T>>;

trait MTKViewCurrentDrawable {
    fn current_drawable(&self) -> Option<ID<dyn MTLDrawable>>;
}

impl MTKViewCurrentDrawable for MTKView {
    fn current_drawable(&self) -> Option<ID<dyn MTLDrawable>> {
        unsafe { msg_send_id![self, currentDrawable] }
    }
}
madsmtm commented 3 months ago

That's because it actually returns CAMetalDrawable, which itself is kinda weird; it exists in the QuartzCore/CoreAnimation framework (along with a bunch of other CAMetalX), but extends the MTLDrawable protocol from the Metal framework, so there's a kind of circular dependency here, which I think I was unsure of how to resolve cleanly.

madsmtm commented 3 months ago

Hmm, no, actually, I think MTKView is defined in MetalKit, so this should be fixable

madsmtm commented 3 months ago

I've fixed part of this in https://github.com/madsmtm/objc2/commit/0f1479ba6f9fe368f1bf0c872d3fe8cff46c4337, the QuartzCore framework wasn't properly public, so we couldn't really use it in the other frameworks.

Still need to actually generate the CAMetalDrawable class.