Skip to content

Add Metal variant (macOS + iOS) - #47

Open
igagis-bot wants to merge 11 commits into
mainfrom
add-metal-variant
Open

igagis-bot wants to merge 11 commits into
mainfrom
add-metal-variant

Conversation

@igagis-bot

@igagis-bot igagis-bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add a Metal variant of the ruisapp glue layer for macOS and iOS:

  • macOS: CAMetalLayer-based window (MetalView/CocoaWindow), Cocoa glue, key-code mapping
  • iOS: UIKit/CAMetalLayer-based window
  • CMake: build the Metal variant library (shared sources + the platform Metal glue) with the RUISAPP_RENDER_METAL define, and the ruisapp-metal-test application
  • The glue passes the window's CAMetalLayer by reference to the Metal context (the one held by the native_window) — no native_window-specific method is needed (mirrors the Vulkan renderer's surface approach)

Depends on ruis PR #211 (register the renderer in the monorepo build) and on the Metal renderer PR (cppfw/ruis-render-metal PR #3).

Add a Metal variant of the ruisapp glue layer for macOS and iOS:
- macOS: MetalView/CAMetalLayer-based window, Cocoa glue, key-code mapping
- iOS: UIKit/CAMetalLayer-based window
- CMake: build the Metal variant library (with the corresponding glue sources)
  for the current platform, plus the ruisapp-metal-test application.

Depends on ruis with the native_window::get_native_metal_layer() method and
on ruis-render-metal (Metal renderer).
Replace the native_window::get_native_metal_layer() virtual with the surface
approach (mirroring the Vulkan renderer): the glue creates a
ruis::render::metal::surface holding the window's CAMetalLayer and the shared
device, and passes it to the Metal context, which keeps it alive for its own
lifetime.
The surface now holds a shared_ref to the window (ruis-render-metal PR #3),
so the glue passes its window reference to the surface constructor.
The context now takes the CAMetalLayer by reference, so the glue passes it
directly instead of creating a surface provider.
The content view is layer-backed, so AppKit keeps the CAMetalLayer's
geometry in sync with the view's bounds. resize() set layer.frame to a
screen-coordinate rect, which fought that sync and mispositioned the
rendered content (it appeared shifted up by the title-bar height, easing
into place at launch). Let AppKit own the layer geometry; only set
contentsScale, which AppKit does not derive for the drawable.
@igagis

igagis commented Dec 4, 2026

Copy link
Copy Markdown
Member

why separate glue and application created for metal, is it too different from opengl one? can't it be done using #ifdef interlieved in same glue and application?

@igagis-bot

Copy link
Copy Markdown
Contributor Author

The split is per-variant because each variant owns a different rendering surface, which is inherent to the backend:

  • GL (macos/): the native window owns an NSOpenGLContext (created via NSOpenGLPixelFormat), plus a hidden shared-context window.
  • Metal (macos/metal/): the native window owns a CAMetalLayer (installed as the view's backing layer).

The native_window and the Cocoa view are coupled to that surface, so they're backend-specific. Concretely, the GL window.hxx carries an opengl_context_wrapper (~60 lines of NSOpenGLPixelFormat/NSOpenGLContext setup) that has no Metal counterpart, and window.cxx is 719 lines for GL vs 557 for Metal. Interleaving those with #ifdef would put two large, unrelated blocks in one file, which I think is harder to maintain than the current split.

The application main loop is the shared part (~83% identical); the only differences are in the constructor, which wires up the backend-specific factory/context (GL passes the shared-context window; Metal extracts the CAMetalLayer).

So: yes, it can be done with #ifdef, but my recommendation is to keep the per-variant native_window separate. If reducing duplication is the goal, the cleaner move is to extract the shared main loop into a common base and keep only the surface/context creation per-variant — no #ifdef needed.

Let me know which direction you'd like and I'll do it.

@igagis

igagis commented Dec 4, 2026

Copy link
Copy Markdown
Member

make two window implementations opengl_window and metal_window, everything else merge into same files

… separate

Per review feedback, instead of a fully separate glue tree for the metal
variant, the shared glue files (application, glue, cocoa) are now a single
copy that selects the rendering backend via #ifdef RUISAPP_RENDER_METAL.
Only the window implementations are kept as separate per-backend files:

  opengl_window.{hxx,cxx}  - the opengl / opengles window (and its GL context
                             wrapper on macos)
  metal_window.{hxx,cxx}   - the metal window (owns the CAMetalLayer)

This removes the duplicated application/glue/cocoa/key_code code that
differed between the two backends by only a handful of lines.

macos:
  - macos/window.{hxx,cxx}        -> macos/opengl_window.{hxx,cxx}
  - macos/metal/window.{hxx,cxx}  -> macos/metal_window.{hxx,cxx}
  - macos/metal/{application,glue,cocoa,key_code.map}.* removed (merged)

ios:
  - ios/window.{hxx,cxx}          -> ios/opengl_window.{hxx,cxx}
  - ios/metal/window.{hxx,cxx}    -> ios/metal_window.{hxx,cxx}
  - ios/metal/{application,glue}.* removed (merged)

build/cmake: the metal variant now reuses the shared glue directory instead
of a per-variant /metal subdirectory.
@igagis-bot

Copy link
Copy Markdown
Contributor Author

Done — pushed as 75ef761.

There are now two window implementations (opengl_window.{hxx,cxx} and metal_window.{hxx,cxx}) and everything else is merged into the shared glue files. The shared application.{hxx,cxx}, glue.mm, cocoa.hxx (and key_code.map.hxx on macos) are a single copy that selects the backend via #ifdef RUISAPP_RENDER_METAL:

  • constructor: metal creates a metal::factory; opengl keeps the shared-GL-context window.
  • make_window(): metal extracts the CAMetalLayer and builds a metal::context; opengl passes the shared GL context and builds an opengl::context.
  • the window files are the only per-backend sources (the rest of the window code — event handling, resize, fullscreen, vsync — is identical and stays in the respective window impl).

The redundant per-variant metal/{application,glue,cocoa,key_code.map}.* files are removed. Net change: 107 insertions / 1297 deletions. Both the opengl and metal macos variants still build, and the metal test app renders identically to before.

LICENSE file and all other sources (the license check compares each file's
header against the LICENSE file).
@igagis-bot

Copy link
Copy Markdown
Contributor Author

Note on CI: the vcpkg - macos and vcpkg - ios jobs fail with assertion failure: target ruis-render-metal::ruis-render-metal does not exist. That's expected — the ruis-render-metal vcpkg port isn't in the cppfw vcpkg-repo yet (it's PR cppfw/ruis-render-metal#3, still open). Those jobs should go green once that port is merged.

The other failures (lint, msys2, sanitizer, macosx) were a license-header year mismatch in the renamed metal_window.{cxx,hxx} (had 2016-2026 while the LICENSE file has 2016-2025); fixed in 50a39a6.

Comment thread src/ruisapp/glue/macos/application.cxx Outdated
#ifdef RUISAPP_RENDER_METAL
# include <ruis/render/metal/context.hpp>
# include <ruis/render/metal/factory.hpp>
#else

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be elif OPENGL

else
error unknown rendering api

Comment thread src/ruisapp/glue/macos/application.cxx Outdated
utki::logcat_debug("application_glue::application_glue(): metal render factory created", '\n');
return c;
}()),
#else

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be elif defined OPENGL else error unsknown rendering api

Comment thread src/ruisapp/glue/ios/glue.mm Outdated
#ifdef RUISAPP_RENDER_METAL
#include <ruis/render/metal/context.hpp>
#include <ruis/render/metal/factory.hpp>
#else

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be elif defined OPENGLES else error unknown rendering api

Comment thread src/ruisapp/glue/ios/glue.mm Outdated
#include "window.cxx"
#ifdef RUISAPP_RENDER_METAL
#include "metal_window.cxx"
#else

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Comment thread src/ruisapp/glue/macos/application.hxx Outdated
#include "window.hxx"
#ifdef RUISAPP_RENDER_METAL
# include "metal_window.hxx"
#else

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Comment thread src/ruisapp/glue/macos/application.hxx Outdated
{
const utki::version_duplet gl_version;

#ifndef RUISAPP_RENDER_METAL

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

convert to positive condition, ifdef OPENGL

Comment thread src/ruisapp/glue/macos/glue.mm Outdated

#ifdef RUISAPP_RENDER_METAL
#include <ruis/render/metal/context.hpp>
#else

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Comment thread src/ruisapp/glue/macos/glue.mm Outdated
#include "window.cxx"
#ifdef RUISAPP_RENDER_METAL
#include "metal_window.cxx"
#else

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

@end

namespace {
class native_window : public ruis::render::native_window

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't opengl_window and metal_window share something? would it make sense to make base cocoa_window : public ruis::native_window and derive metal and opengl windows from the cocoa window?

iOS touch coordinate offset:
- Convert touch locations (view coords, points) to GUI coordinates by
  scaling to pixels and subtracting the viewport origin (content_rect.p).
  Previously the status-bar height was not subtracted, so taps landed
  below the actual touch position; most noticeable with Metal, whose
  viewport is offset by the status-bar height (the GLES viewport starts
  at the top of the screen).

iOS fullscreen:
- Add a custom Info.plist template (ios_info.plist.in) declaring an empty
  UILaunchScreen dict. Without a launch screen iOS runs the app in a
  scaled compatibility mode (320x480pt) instead of natively filling the
  whole screen, like the Android backend does.
- The template uses Xcode build-setting vars ($(EXECUTABLE_NAME),
  $(PRODUCT_BUNDLE_IDENTIFIER), $(PRODUCT_NAME)) because custom
  MACOSX_BUNDLE_INFO_PLIST templates do not get ${APPLE_GUI_*} subst.

iOS deprecations / Metal layer:
- Replace deprecated UI_USER_INTERFACE_IDIOM() with
  [[UIDevice currentDevice] userInterfaceIdiom]; silence statusBarFrame
  and mainScreen deprecations with pragmas.
- Use the CAMetalLayer created via the +layerClass override instead of
  assigning a new layer to the readonly UIView.layer.

Review feedback - rendering API conditionals:
- Restructure "#ifdef RUISAPP_RENDER_METAL / #else" into
  "#ifdef METAL / #elif defined(OPENGL|OPENGLES) / #else #error
  "Unknown graphics API"", matching the other glue backends.
- Convert the "#ifndef RUISAPP_RENDER_METAL" shared-GL-context member to
  the positive condition "#ifdef RUISAPP_RENDER_OPENGL".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants