/game-porting-skills:using-metalfx-frame-interpolationfrom game-porting-skills
Adds MetalFX frame interpolation to a Metal app with a dedicated present thread, doubling display framerate (e.g., 60→120fps). Requires existing MetalFX temporal upscaling.
Ships in game-porting-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
This skill adds MetalFX frame interpolation to a Metal app that already has a MetalFX temporal scaler integrated. The interpolator generates intermediate frames between real rendered frames, effectively doubling the display framerate (e.g., 60fps render → 120fps display).
Two API variants exist:
MTLFXFrameInterpolator (macOS 15.0+ / iOS 18.0+)MTL4FXFrameInterpolator (macOS 26.0+ / iOS 26.0+), which inherits from the shared MTLFXFrameInterpolatorBase protocolAll per-frame properties are identical across both APIs (they live on MTLFXFrameInterpolatorBase). The differences are the creation call, the command buffer type used for encoding, and the support check — see the notes below.
Prerequisite: The app must already have MetalFX temporal upscaling working. If not, use the using-metalfx-temporal-upscaler skill first.
Before starting integration, check if the engine already has a high-level abstraction for frame interpolation (e.g., from DLSS Frame Generation, FSR Frame Gen). If it does, integrate MetalFX through the existing abstraction — follow the same execution paths wherever the APIs are compatible. This makes debugging easier and minimizes the chance for bugs. Only diverge from the existing patterns where MetalFX API differences require it.
If no existing frame interpolation abstraction exists, ask the developer: introduce a minimal abstraction layer (Recommended — cleaner, easier to maintain and debug), or implement MetalFX directly in the source without abstractions (faster, but harder to extend later)?
Important: When bridging a row-vector matrix library (DirectXMath for example) to Metal's column-vector simd::float4x4, the matrix must be transposed
Frame interpolation uses a dedicated present thread with precise pacing for smooth output:
Render thread (MTKView, 60fps) PresentThread
│ │
├─ Scene render → offscreen │
├─ Temporal upscale → back buffer │
├─ Commit command buffer │
├─ Present(interpolator, queue) ────────►│
│ ├─ Encode frame interpolation
│ (blocked until consumed) ├─ Acquire drawable → blit interpolated → present
│ ├─ Wait ~48% frame time (kevent64 timer)
│◄───────────────────────────────────────├─ Acquire drawable → blit real → present
│ │
The PresentThread class from references/present-thread.md provides the complete C++ implementation with ObjC wrapper.
AAPLPresentThread.h — ObjC interface wrapping the C++ PresentThreadAAPLPresentThread.mm — C++ PresentThread class + ObjC wrapper (Objective-C++ file)Read references/present-thread.md for the complete implementation. The class provides:
Create an ObjC wrapper (since the main renderer is .m, not .mm) with this interface:
@interface AAPLPresentThread : NSObject
- (instancetype)initWithMinDuration:(float)minDuration
metalLayer:(CAMetalLayer *)layer
library:(id<MTLLibrary>)library;
- (void)startFrame:(id<MTLCommandBuffer>)commandBuffer;
- (id<MTLTexture>)getBackBuffer;
- (void)present:(id<MTLFXFrameInterpolator>)interpolator queue:(id<MTLCommandQueue>)queue;
// Metal 4 equivalent: - (void)present:(id<MTL4FXFrameInterpolator>)interpolator queue:(id<MTL4CommandQueue>)queue;
- (void)resize:(uint32_t)width height:(uint32_t)height pixelFormat:(MTLPixelFormat)format;
- (void)drainPendingPresents;
@end
Important: The header must use #import <Metal/Metal.h> etc., NOT @import MetalFX; — the latter fails when included from C++ compilation units.
MTLFXFrameInterpolatorDescriptor *desc = [[MTLFXFrameInterpolatorDescriptor alloc] init];
desc.inputWidth = renderWidth; desc.inputHeight = renderHeight;
desc.outputWidth = displayWidth; desc.outputHeight = displayHeight;
desc.colorTextureFormat = MTLPixelFormatRGBA16Float;
desc.depthTextureFormat = MTLPixelFormatDepth32Float;
desc.motionTextureFormat = MTLPixelFormatRG16Float;
desc.outputTextureFormat = MTLPixelFormatRGBA16Float;
// Metal 3: id<MTLFXFrameInterpolator> interpolator = [desc newFrameInterpolatorWithDevice:device];
// Metal 4 (macOS/iOS 26.0+): pass an MTL4Compiler for pipeline state compilation
id<MTL4FXFrameInterpolator> interpolator = [desc newFrameInterpolatorWithDevice:device compiler:compiler];
Set these properties once (not per-frame):
interpolator.fieldOfView = fovDegrees; // e.g., 65.0
interpolator.nearPlane = nearZ; // e.g., 1.0
interpolator.farPlane = farZ; // e.g., 5000.0
interpolator.aspectRatio = aspect; // render width / render height
Set these per-frame before Present():
interpolator.depthTexture = depthTarget;
interpolator.motionTexture = motionTarget;
// Before assigning jitterOffsetX/Y, consult with the rest of the pipeline
// (temporal upscaling if present) whether this is necessary and in which
// space the jitter needs to be defined at that stage.
interpolator.jitterOffsetX = jitterX;
interpolator.jitterOffsetY = jitterY;
interpolator.motionVectorScaleX = renderWidth * 0.5f;
interpolator.motionVectorScaleY = renderHeight * 0.5f;
Note: MTLFXFrameInterpolator does NOT have a reset property. History management is internal.
Old flow (temporal upscaling only):
_upscaledOutput_upscaledOutput to drawableNew flow (with frame interpolation):
[_presentThread startFrame:commandBuffer] — sync with present thread[_presentThread getBackBuffer] (NOT your own output texture)[_presentThread present:_frameInterpolator queue:_commandQueue] — this encodes interpolation + handles dual presentationpresentDrawable: and blit-to-drawable code — PresentThread handles itKey changes:
outputTexture = _presentThread.getBackBuffer (PresentThread manages its own triple-buffered textures)_upscaledOutput or _blitPipelineState — PresentThread has its own copy pipeline[commandBuffer presentDrawable:] — PresentThread acquires drawables and presentsIn loadMetal::
CAMetalLayer *metalLayer = (CAMetalLayer *)view.layer;
metalLayer.maximumDrawableCount = 3; // Required for dual presentation
id<MTLLibrary> library = [_device newDefaultLibrary];
_presentThread = [[AAPLPresentThread alloc] initWithMinDuration:1.0f/120.0f
metalLayer:metalLayer
library:library];
On resize, call [_presentThread resize:width height:height pixelFormat:format].
The PresentThread's Resize() must guard against zero dimensions (the metal layer may report 0x0 before the first layout pass):
void Resize(uint32_t width, uint32_t height, MTLPixelFormat pixelFormat) {
if (width == 0 || height == 0) return; // Guard against pre-layout calls
// ... create textures ...
}
The PresentThread uses a sophisticated dual-thread model for precise frame pacing:
PresentThreadFunction (encoding thread):
Present()m_interpolationOutputs, presentsm_backBuffers, presentsPacingThreadFunction (pacing thread):
kqueue + kevent64 with NOTE_MACHTIME | NOTE_ABSOLUTE for sub-ms timer precisiontime + (delta * 31) >> 6MTLSharedEvent to gate the second presentPresent() (called from render thread):
colorTexture (current back buffer) and prevColorTexture (previous back buffer)All PresentThread textures:
ShaderRead | ShaderWrite | RenderTargetShaderRead | RenderTargetShaderRead | RenderTargetMTLPixelFormatRGBA16Float and StorageModePrivateResize()Present() is called from the render thread, not the present threadgetBackBuffer(), not a separate texture+[MTLFXFrameInterpolatorDescriptor supportsDevice:] (macOS 15.0+ / iOS 18.0+). Metal 4: +[MTLFXFrameInterpolatorDescriptor supportsMetal4FX:] (macOS/iOS 26.0+) — a distinct class method that checks Metal 4 compatibility.@import errors in .mm file: Use #import <Metal/Metal.h> etc. in the header, not @importConfirm the integration from debug logs:
The developer must confirm the debug log verification before the integration is considered complete.
After building and launching the app, verify rendering is working by capturing a screenshot of the app window. If possible closest match vs non-upscaled. This is useful both for self-checking your work and for showing the user what the result looks like.
Capture the app window (not the full screen — just the window):
# Find the main content window by app name, filtering for the largest window
APP_NAME="MyApp" # Replace with the actual app/process name
WID=$(python3 -c "
import Quartz
for w in Quartz.CGWindowListCopyWindowInfo(Quartz.kCGWindowListOptionAll, Quartz.kCGNullWindowID):
if '$APP_NAME' in w.get('kCGWindowOwnerName', ''):
b = w.get('kCGWindowBounds', {})
if b.get('Height', 0) > 100:
print(w['kCGWindowNumber']); break
")
screencapture -x -o -l "$WID" /tmp/mfx_screenshot.png
Then view the screenshot at /tmp/mfx_screenshot.png. Look for:
What SKILL.md sets; muted values are Claude Code's defaults
24 skills in this plugin
/compiling-with-metal-shaderconverterALWAYS use when compiling DXIL/HLSL to metallib with Metal shader converter (commonly referred to as MSC) via the metal-shaderconverter CLI or libmetalirconverter (IRCompiler, IRObject, IRShaderReflection) at runtime. Covers offline/runtime DXIL compilation, root signatures, reflection output, ray tracing shaders, geometry/tessellation emulation compilation, framebuffer fetch in HLSL, programmable blending in HLSL, Metal_HLSL.inc, and compatibility flags. Do NOT trigger for binding compiled shaders to encoders — use integrating-metal-shaderconverter-shaders for that./creating-metal4-shader-pipelinesALWAYS use when creating Metal pipeline state objects with Metal 4 descriptors (MTL4RenderPipelineDescriptor, MTL4ComputePipelineDescriptor, MTL4MeshRenderPipelineDescriptor) via MTL4Compiler. Covers function descriptors, function constant specialization, flexible/unspecialized PSOs, color-attachment mapping, async compilation (MTL4CompilerTask), pipeline caching (MTL4Archive, MTL4PipelineDataSetSerializer), static/binary shader linking, and pipeline reflection. Do NOT trigger for HLSL/DXIL → metallib — use compiling-with-metal-shaderconverter.npx claudepluginhub apple/game-porting-toolkit --plugin game-porting-skillsOther plugins' skills for the same job