Created
November 26, 2026 07:08
-
-
Save banjun/06ce070ffb4e2530bc374f3c3aca3836 to your computer and use it in GitHub Desktop.
Get RealityKit IK (inverse kinematics) results
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| import Metal | |
| import ObjectiveC.runtime | |
| import Synchronization | |
| // Purpose: get RealityKit IK (inverse kinematics) results | |
| // Current result: got joint transforms affected by the IK | |
| // | |
| // How it works: | |
| // hook -[MTLDebugDevice newBufferWithBytes:length:options:], | |
| // used by re::ecs2::SkeletalPoseBufferComponent::update | |
| // Based on observations in macOS 27, the newBufferWithBytes is called every frame right before performSkinning compute shader internally by RealityKit IK | |
| private func isSkeletalPoseBufferUpdate() -> Bool { | |
| Thread.callStackSymbols.contains { | |
| $0.contains("SkeletalPoseBufferComponent") | |
| && $0.contains("update") | |
| } | |
| } | |
| // MARK: - Objective-C IMP | |
| private typealias NewBufferIMP = @convention(c) ( | |
| AnyObject, | |
| Selector, | |
| UnsafeRawPointer, | |
| Int, | |
| MTLResourceOptions | |
| ) -> Unmanaged<AnyObject>? | |
| // MARK: - Hook state | |
| private struct HookState: @unchecked Sendable { | |
| var originalIMPs: [ObjectIdentifier: NewBufferIMP] = [:] | |
| var captures: [ObjectIdentifier: PoseBufferCapture] = [:] | |
| } | |
| private let hookState = Mutex(HookState()) | |
| // MARK: - Hook function | |
| private let hookedNewBuffer: NewBufferIMP = { device, selector, bytes, length, options in | |
| let deviceID = ObjectIdentifier(device) | |
| let (original, capture) = hookState.withLock { | |
| ($0.originalIMPs[deviceIDForClass(device)], | |
| $0.captures[deviceID]) | |
| } | |
| guard let original else { return nil } | |
| let result = original(device, selector, bytes, length, options) | |
| // hook | |
| if let capture, length == capture.length, isSkeletalPoseBufferUpdate() { | |
| capture.capture(bytes, length: length) | |
| } | |
| return result | |
| } | |
| private func deviceIDForClass(_ device: AnyObject) -> ObjectIdentifier { | |
| ObjectIdentifier(object_getClass(device)!) | |
| } | |
| final class PoseBufferCapture: Sendable { | |
| private let state = Mutex<Data?>(nil) | |
| let length: Int // additional condition to hook | |
| init(length: Int) { | |
| self.length = length | |
| } | |
| func install(on device: any MTLDevice = MTLCreateSystemDefaultDevice()!) { | |
| let object = device as AnyObject | |
| let cls: AnyClass = object_getClass(object)! | |
| let classID = ObjectIdentifier(cls) | |
| let deviceID = ObjectIdentifier(object) | |
| hookState.withLock { state in | |
| if state.originalIMPs[classID] == nil { | |
| let selector = NSSelectorFromString("newBufferWithBytes:length:options:") | |
| guard let method = class_getInstanceMethod(cls, selector) else { | |
| fatalError("method not found") | |
| } | |
| let original = method_getImplementation(method) | |
| let replacement = unsafeBitCast(hookedNewBuffer, to: IMP.self) | |
| // check override case | |
| let added = class_addMethod( | |
| cls, | |
| selector, | |
| replacement, | |
| method_getTypeEncoding(method) | |
| ) | |
| // not overriden, ordinal method | |
| if !added { | |
| method_setImplementation(method, replacement) | |
| } | |
| state.originalIMPs[classID] = unsafeBitCast(original, to: NewBufferIMP.self) | |
| } | |
| state.captures[deviceID] = self | |
| } | |
| } | |
| fileprivate func capture(_ source: UnsafeRawPointer, length: Int) { | |
| let data = Data(bytes: source, count: length) | |
| state.withLock { | |
| $0 = data | |
| } | |
| } | |
| func read() -> Data? { | |
| state.withLock {$0} | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment