Repository navigation
Add a native Metal renderer for macOS - #92
AlexMcArdle wants to merge 57 commits into
Conversation
7762960 to
09f53c9
Compare
You didn't request review so no notification was sent Happy to close the other PR if we rather go with these changes |
a191305 to
4f44ee7
Compare
|
Superseded by the July 15 refresh: CloudSync was later removed from this branch because it did not provide reliable cross-device continuity. The current PR tree does not contain CloudSync. The parts of this update that remain are the Metal legacy alpha-test fix and the macOS OpenAL dylib bundling/runtime discovery work. Thanks again to @Dahlgren for identifying the missing runtime artifact gap. See the current refresh summary: #92 (comment) |
6177d12 to
492e1f0
Compare
Sure @AlexMcArdle, just give me a time please. I'll start to consolidate PRs probably at the second part of next week finally. 🙏 I have some workload to finish first 😉. |
4df7bcc to
fdc7e0d
Compare
|
Refreshed the three curated commits from the latest macOS/Metal work:
Current tip: |
78ca1b0 to
2b63927
Compare
|
Small follow-up folded into the existing Metal/portability commit: macOS now defaults to Added regression coverage for the platform default; the macOS build passes and |
ade09fc to
ae25c57
Compare
Update: this PR has been rebuilt around current upstreamThe original version of this PR combined macOS platform support with the Metal renderer. Since macOS support has now landed in this repository, I rebuilt the branch on the current upstream What changed:
In short: this is no longer a macOS port PR. It is a renderer-only PR that adds native Metal support to the macOS foundation already present upstream. |
|
looks like wrong sampling |
Cutout edges are most of what MSAA smooths in this game, and Metal's opaque pipeline hard-discarded them regardless of sample count, so the MSAA option looked like a no-op next to GL33. Add an alpha-to-coverage specialization of the opaque mesh pipeline that emits GL33's sharpened coverage in alpha, selected for measured cutouts whenever MSAA is on.
Title-effect objects (cutObj, RscObject) and ControlObject draws are screen-space 3D overlays, and the descriptor layer already has a PassKindHint for that which nothing produced. Set it at both producers so Metal's legacy path can keep every such overlay on GL33's blend path instead of the measured-cutout alpha test, which was hard-cutting the TV set's glass vignette the same way it cut the binocular reticle. Also carry the scene constant colour per object into the TL shaders; IsColored fades and tints rendered at texture alpha on Metal.
Implement the Engine instanced-run contract for the Metal backend: the scene's runs of identical static shapes upload their camera-relative world matrices and per-instance light indices to a per-frame stream buffer, and every DrawSectionTL inside the run issues one instanced draw. Lights for instances come from a frame light table built by UploadLocalLights, selected by index in the vertex shader and scaled by the night-adjusted material there, matching GL33. Legacy fan emission inside a run marks it impure so the scene redraws the tail scalar. Also count Metal draw calls for the perf overlay, which never ticked on this backend.
Upload the terrain height grid as an R32 vertex-stage texture and port GL33's land-clip snapping to vsMesh: ClipLandOn vertices pin to the sampled surface, ClipLandKeep vertices keep their authored height above the terrain at the object's bounding centre, and mode 2 rides the land square's plane. Normals are bent by the surface gradient as before. LandClipInVS() now reports true once a heightmap is loaded, so the scene skips its CPU ApplyLandClip deform and land-clipped shapes join instanced runs. VertexMeshMTL grows a land-clip class per vertex.
Port GL33's opt-in shadow-map path: the scene's caster set is copied into the frame stream and rendered from each cascade's light view-projection into a Depth32Float array at the end of the frame (solid casters keep their back faces, alpha batches discard on texture alpha), and the lit fragment shaders sample the previous frame's array with GL33's tiered select, 3x3 PCF, cross-fade and far fade. The tuning set, sun factor, depth probe and cascade-0 dump follow the Engine contract; the probe matches the CPU oracle exactly (iou 1.000). Metal's top-down texture rows are accounted for in the sampling UV and the readbacks.
Host the scalar lit path on UploadLocalLights like GL33: SetMaterial packs the object's light indices instead of embedding up to eight pre-multiplied lights per draw, and vsMesh reads the same frame table for scalar and instanced draws. The table now lives in rotating persistent buffers, since the terrain draws before the scene uploads the frame's lights and reads the previous table, as GL33's UBO does.
A mod remount now drops every texture's GPU surfaces and pixel copy while the objects and their Refs stay valid, and the next use reloads them from the current VFS, the way GL33's ReleaseMemory path works; previously the Metal bank kept the old mod's textures under their names because its bulk release could only run with nothing alive. FlushBank forgets the unloaded bank's textures so a later Load decodes the replacement, and textures now free their surfaces through their bank when destroyed instead of holding them until shutdown.
fsMeshBlend dropped texels below 18/255 alpha, a threshold chosen when AI88 textures were expanded through ARGB4444; the decoder has kept their 8-bit alpha since, and the higher cut punched a hole in the A-10 canopy glass where its reflection texture dips below it, visible as a pale blotch GL33 does not show. Use GL33's 1/255 reference instead.
The legacy measured-cutout guard gained the ScreenSpace3D exclusion and DrawSectionTL uploads the per-draw copy of the object constants; the source-matching test still looked for the old spellings.
The original gun textures (o\guns\handle_bmp.pac and friends) are AI88 data in .pac files. Both texture sources chose the level reader by file extension, and the .pac reader has no case for the 16-bit formats, so those textures failed to decode; Metal, which decodes on load, logged it while GL33 stayed quiet unless the model was drawn. Treat a format marker as PAA-style level data regardless of extension. Adds an LZSS-packed AI88 .pac fixture and a decode test that fails without the fix.
|
Parity achieved. And an FPS boost to boot. Please give it a look when you can! |
Good news. I don't have access to Mac currently, but that will probably change soon. Meanwhile I would like to finish porting perf updates from #126. Once done, we can start on adding more renderers. I have on side also Android (OpenGL ES) one also. 🙏 Thanks a lot for your work and patience. |
|
Got it. I'll be waiting paitently for #126 . Please let me know if there's anything that will help speed things up! |
|
What about now @simi ? |
|
I can finally test on my own, so I'll start with that. 🙏 |
The instanced ground pass is only implemented by GL33, so the landscape now asks the backend before taking it and otherwise keeps the mesh-based ground and water. Metal uploads its local light table itself, once per frame on the first draw that packs light indices, now that the scene no longer calls UploadLocalLights. The vendored ImGui Metal backend builds without ARC, where its bridge casts are no-ops that warn.
Port the instanced ground pass to the Metal backend: one grid mesh per segment displaced from the height map in the vertex stage, surface textures in one array per size class, per-segment light sets read from a frame buffer, and water drawn flat at sea level through the same instances. The frame half of PrepareMeshTL moves into UpdateFrameConstants so the ground can build its constants without a mesh draw before it. A map whose setup fails falls back to the mesh-based ground.
|
Refreshed against current Catching up with
Instanced terrain and water on Metal ( Metal now takes the same instanced ground path as GL33: one grid mesh per segment displaced from the height map in the vertex stage, surface textures in one array per size class, per-segment light sets, and water drawn through the same instances. It is contained in Verified locally on Apple Silicon
Not covered yet
To try it on a Mac Swap in |
|
Follow-up on the "not covered yet" list above. No code changes; these are local runs on Apple Silicon at
Whole-frame time did not move measurably in this scene (80 to 89 fps across the four runs); most of the frame there is spent waiting on present. Still open: open-sea water is about 3.5% darker on Metal than on GL33. It is repeatable, so it is a real difference and not capture noise, and I have not found the cause. |
The ground lighting helper took its output by unqualified reference, which only the newest Metal shading language accepts. Builds with an older deployment target failed to compile the whole mesh shader library. It now returns the vertex it lights.
|
One fix since the comments above ( |


Why
CWR-CE now supports macOS, but its hardware renderer is OpenGL. Apple deprecated OpenGL in macOS 10.14 and recommends Metal for high-performance GPU work: Apple OpenGL Programming Guide.
This PR adds a native Metal renderer on top of the macOS support already present in this repository. Metal becomes the default renderer on Apple platforms, while
--render gl33,--render dummy, and--render autoremain available.What this adds
PoseidonMTLbackend, including SDL/CAMetalLayer integration and the hardware-T&L and legacy draw paths.Parity with the GL33 backend
A second batch brings the Metal backend to feature parity with
EngineGL33, implementing every applicableEnginevirtual the GL33 backend overrides:Display/monitor/window-mode queries and switching, guard band, resizable windowed mode.
Graphics options that previously no-op'd on Metal: MSAA (with alpha-to-coverage for cutouts), render scale (SSAA), gamma, VSync — via an offscreen frame target and present pass.
Trident harness hooks: back-buffer sampling, test patterns, flat shading, debug error count, recorded draws for the frame validator; readbacks are pre-gamma to match GL33's capture path.
Stars/laser points, night-eye desaturation, queue-reorder/projection-update hooks, IsColored tints, and the ScreenSpace3D pass-kind hint for 3D UI overlays (binocular reticle, TV vignette).
Instanced runs and GPU land clip (heightmap in a vertex-stage texture), so Metal takes the same perf path as GL33.
Cascaded shadow maps (opt-in, as on GL33), with the depth probe matching the CPU oracle.
Local lights selected from a per-frame light table, matching GL33's
UploadLocalLightsdesign.Textures are released in place on a mod remount and reload from the new mod set on next use (GL33's model); previously Metal kept the old set under the same names.
Blended sections discard at GL33's 1/255 alpha reference (the earlier 18/255 cut punched holes in faint glass reflections such as the A-10 canopy).
The
tests/integration/renderingsuite is failure-for-failure identical between--render mtland--render gl33locally. Note: on macOS, GL33's shadow-map lit pass currently draws no shadows because Apple's GL driver rejects thesampler2DArraydepth array ("GLD_TEXTURE_INDEX_2D_ARRAY is unloadable"); Metal's works.One shared-engine fix came out of this: the original gun textures (
o\guns\handle_bmp.pacand friends) are AI88 data in.pacfiles, and both texture sources picked the level reader by file extension, so they failed to decode on every backend (Metal, decoding at load, logged it; GL33 only fails once the model is drawn). A header format marker now selects the PAA-style readers regardless of extension, with an LZSS-packed AI88.pacfixture and decode test.Scope
This is centered on the Metal renderer and includes the narrow macOS integration work needed to build and exercise it across the repository:
PoseidonMTLinto the Apple Tetris application and unit-test targets./procis unavailable.The broader macOS toolchain, distribution, packaging, and CI support remain outside this PR and are already merged upstream. Two small exceptions, both macOS-specific and both easy to drop if you would rather they came separately:
.gitignoreentry for.DS_Store. Finder writes these into any directory it browses, so they turn up constantly while working on the macOS side; the repo does not ignore them yet.Kept current with upstream
This branch tracks
main. Synced again on 2026-09-10, pulling in 52 more upstream commits (instancing/perf work, mission-browsing improvements including a new "blocked mission" UI state, mod/download robustness, Linux hardening). No conflicts touched Metal code this time; three non-Metal conflicts came up and were resolved by keeping our fork's independent fixes where they were strict supersets of upstream's version of the same bug:engine/Poseidon/Foundation/Common/Platform.cpp(fileCopy) — both sides independently fixed a missingopen()mode argument; kept our version, which adds atomic copy-then-rename on top (upstream's doesn't) and matchesQOFStream::close()'s save-file permission convention.0644destination mode that doesn't hold against our fixed0600; kept our existing coverage instead.engine/Poseidon/UI/OptionsUIImpl.cpp— upstream's new blocked-mission-message UI merged in cleanly; kept ourCreateSingleMissionBank(directory, true)call in the preview path (a real fix for a bank-name collision between packed missions on the same island) over upstream's default.Earlier syncs required two small fixes, both still in place:
PoseidonMTL's vertex-buffer init still referencedVBSmallDiscardable, an enum valuemainremoved when it collapsed the discardable vertex-buffer types intoVBStatic. GL33 had already been updated to match; Metal hadn't. Fixed to mirror GL33's simplified check.FormatTime's test hardcoded an expected year against alocaltime()-formatted timestamp, so it failed outside UTC. Pinned to UTC for the duration of the test, matching the equivalent fix already onmain.Verification
macos-arm64-clang-rwdion Apple Silicon.PoseidonGame,MetalSmokeTest, andPoseidonTests.MetalSmokeTest --check --frames 3.ctestsuite after the latest upstream sync (3994/3994 passing).Screenshots