madsmtm / objc2

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

Support protocol.MTLDevice.methods."newTextureWithDescriptor:iosurface:plane:" #643

Open sotaroikeda opened 1 month ago

sotaroikeda commented 1 month ago

The newTextureWithDescriptor:iosurface:plane: is not supported. It is necessary for https://github.com/gfx-rs/wgpu/pull/5641 to be used by gecko see Bug 1910043.

madsmtm commented 1 month ago

Indeed, it is intentionally skipped because we don't expose IOSurfaceRef in any shape or form.

The proper solution needs support for CoreFoundation, for now you can do something like:

#[repr(transparent)]
struct IOSurfaceRefWrapper(io_surface::IOSurfaceRef);

// SAFETY: `IOSurfaceRefWrapper` is `#[repr(transparent)]` over
// `IOSurfaceRef`, which is a typedef to `struct __IOSurface *`.
unsafe impl Encode for IOSurfaceRefWrapper {
    const ENCODING: Encoding = Encoding::Pointer(&Encoding::Struct("__IOSurface", &[]));
}

pub fn new_with_descriptor_surface_plane(
    device: &ProtocolObject<dyn MTLDevice>,
    descriptor: &MTLTextureDescriptor,
    surface: io_surface::IOSurfaceRef,
    plane: NSUInteger,
) -> Retained<ProtocolObject<dyn MTLTexture>> {
    let surface = IOSurfaceRefWrapper(surface);
    msg_send_id![device, newTextureWithDescriptor: descriptor, iosurface: surface, plane: plane]
}

This approach is somewhat documented in here.