Files
IfcOpenShell/src/ifcviewer-wgpu/WgpuMetalSurface_mac.h
T
Dion Moult 21d394501f ifcviewer-wgpu: bring up macOS Metal surface (task #32)
Without this, BonsaiViewer.app launched on macOS would show a white
viewport for the same reason Windows did before d4d0c934b — the
`createSurface()` else branch fell through to "wgpu surface creation
not yet wired for this platform", `init()` returned false, and the
Qt window background was all the user saw.

Pieces:

- `WgpuMetalSurface_mac.{h,mm}`: tiny Objective-C++ bridge. Takes the
  NSView pointer that Qt's `winId()` returns on macOS, attaches a
  CAMetalLayer (using Qt's existing one when surfaceType is
  MetalSurface, attaching one ourselves as a defensive fallback),
  sets `contentsScale` from the window's backing scale factor so
  retina drawables come out at native resolution, and returns the
  layer as `void*`. The .mm keeps the Objective-C namespace pollution
  out of WgpuViewportWindow.cpp.

- `WgpuViewportWindow` ctor: `setSurfaceType(QSurface::MetalSurface)`
  on macOS so Qt backs the NSView with a CAMetalLayer at window
  creation; OpenGLSurface elsewhere as before.

- `WgpuViewportWindow::createSurface()`: new `#elif defined(Q_OS_MAC)`
  branch that fills a `WGPUSurfaceSourceMetalLayer` with the layer
  pointer from the bridge and hands it to `wgpuInstanceCreateSurface`.

- `CMakeLists.txt`: `enable_language(OBJCXX)` + the .mm file added to
  the source list on Apple, and links `-framework Cocoa` (NSView) +
  `-framework QuartzCore` (CAMetalLayer).

`winId()` on macOS returns the backing NSView*, not the NSWindow* —
that's the layer-bearing host wgpu-native expects.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-02 13:45:42 +10:00

33 lines
968 B
C

/**
* Objective-C++ bridge between WgpuViewportWindow (pure C++) and Cocoa /
* QuartzCore (Objective-C). Compiled only on macOS — see CMakeLists.txt.
*
* Qt's QWindow::winId() returns the backing NSView* (as a WId) on macOS;
* we need a CAMetalLayer attached to that view to hand to wgpu-native
* via WGPUSurfaceSourceMetalLayer. Doing that requires Objective-C, so
* the actual layer attach lives in WgpuMetalSurface_mac.mm.
*/
#ifndef WGPU_METAL_SURFACE_MAC_H
#define WGPU_METAL_SURFACE_MAC_H
#if defined(__APPLE__)
#ifdef __cplusplus
extern "C" {
#endif
/// Ensures the given NSView has a CAMetalLayer as its backing layer.
/// Returns the CAMetalLayer pointer (`void*` so callers don't need to
/// pull QuartzCore into their TU); the layer is owned by the NSView.
/// Returns nullptr if `nsview_ptr` is null.
void* wgpu_macos_attach_metal_layer(void* nsview_ptr);
#ifdef __cplusplus
}
#endif
#endif // __APPLE__
#endif // WGPU_METAL_SURFACE_MAC_H