# Capturing a Metal workload programmatically 

**URL:** <https://forums.unrealengine.com/t/capturing-a-metal-workload-programmatically/2672949>\
**Category:** Mobile\
**Tags:** unreal-engine, Epic-Pro-Support, question\
**Created:** [October 2, 2025, 12:10pm UTC](https://forums.unrealengine.com/t/capturing-a-metal-workload-programmatically/2672949 "2025-10-02T12:10:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous-edc](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous-edc](https://forums.unrealengine.com/u/anonymous-edc)\
**Post date:** [October 2, 2025, 12:10pm UTC](https://forums.unrealengine.com/t/capturing-a-metal-workload-programmatically/2672949/1 "2025-10-02T12:10:15Z")

</div>

To get a better view of the performance of our game, we would like to run a Metal Capture while running on iOS. For this, we would like to use the system documented by Apple here [https://developer.apple.com/documentation/xcode/capturing\-a\-metal\-workload\-programmatically](https://developer.apple.com/documentation/xcode/capturing%5C-a%5C-metal%5C-workload%5C-programmatically).

Over the past couple of weeks, I have attempted to translate the Swift code to work within the Objective C Unreal Engine 4.27 configuration.

Below is the code that I am using for this. When the metal capture starts, the framerate gets a lot worse as you would expect when performing such an intensive profiling run. The code also successfully generates a .gputrace file, but when I try to open it in Xcode, I get an error saying “Xcode failed to open the gputrace document. The index file does not exist. The capture may be incomplete or corrupt. (2)”.

I believe that this is because I do not commit the command buffer before stopping the capture. In StopMetalCapture, I have commented out the code that does so, because running that code crashes the game. What is the correct code to commit the Unreal Metal Command Buffer?

```auto
#if PLATFORM_IOS
void StartMetalCapture()
{
    FMetalContext& Context = GetMetalDeviceContext();

    mtlpp::Device MetalDevice = Context.GetDevice();

    id<MTLDevice> device = MetalDevice.GetPtr();
    
    // Get Documents directory path
    NSArray<NSURL *> *documentsDirectories = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];

    NSURL* documentsDirectory = [documentsDirectories firstObject];
    NSURL* outputURL = [documentsDirectory URLByAppendingPathComponent : @"MyCapture.gputrace"];

    MTLCaptureManager *captureManager = [MTLCaptureManager sharedCaptureManager];
    MTLCaptureDescriptor *captureDescriptor = [[MTLCaptureDescriptor alloc] init];
    captureDescriptor.captureObject = device;
    captureDescriptor.destination = MTLCaptureDestinationGPUTraceDocument;
    captureDescriptor.outputURL = outputURL;

    if (![captureManager supportsDestination:MTLCaptureDestinationGPUTraceDocument])
    {
        NSLog(@"Capturing to a GPU trace file isn't supported.");
        return;
    }

    NSError *error = nil;
    if (![captureManager startCaptureWithDescriptor:captureDescriptor error:&error])
    {
        NSLog([Content removed] error);
    }
    else
    {
        NSLog([Content removed] outputURL);
    }
}

void StopMetalCapture()
{
    /*FMetalContext& Context = GetMetalDeviceContext();
    mtlpp::CommandBuffer& CommandBuffer = Context.GetCurrentCommandBuffer();
    id<MTLCommandBuffer> NativeCommandBuffer = CommandBuffer.GetPtr();

    [NativeCommandBuffer commit];*/

    MTLCaptureManager *captureManager = [MTLCaptureManager sharedCaptureManager];
    [captureManager stopCapture];

    NSLog(@"Finished metal capture");
}
#endif

```

---

<div class="post-metadata">

**Author:** ![anonymous-edc](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous-edc](https://forums.unrealengine.com/u/anonymous-edc)\
**Post date:** [October 2, 2025, 12:10pm UTC](https://forums.unrealengine.com/t/capturing-a-metal-workload-programmatically/2672949/2 "2025-10-02T12:10:18Z")

</div>

**Steps to Reproduce**

---

<div class="post-metadata">

**Author:** ![step\_jac\_eg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@step\_jac\_eg](https://forums.unrealengine.com/u/step_jac_eg)\
**Post date:** [October 9, 2025, 6:03pm UTC](https://forums.unrealengine.com/t/capturing-a-metal-workload-programmatically/2672949/3 "2025-10-09T18:03:52Z")

</div>

Hi Jimmy,

Looking at the implementation of capture from the [XcodeGPUDebuggerPlugin](https://github.com/EpicGames/UnrealEngine/tree/ue5-main/Engine/Plugins/Developer/XcodeGPUDebuggerPlugin/Source/XcodeGPUDebuggerPlugin), it appears that a full Submit and flush is happening before programmatic capture.

`FRHICommandListExecutor::GetImmediateCommandList().SubmitCommandsAndFlushGPU();`Let us know if this doesn’t resolve on your end.

Best regards.
