Logo Metal Gpu Debug

Metal Gpu Debug

Cộng đồng
rudybear
metal-gpu-debug

Metal GPU debugging and profiling via xctrace and Apple's Metal toolchain. Use this skill when the user mentions: Metal debugging, .gputrace files, Metal shaders, Metal pipeline state, Metal performance, Metal System Trace, Apple GPU profiling, Metal validation, shader compilation errors, MSL (Metal Shading Language), .metallib files, .air files, Metal render pass, Metal command buffer, Metal encoder, MTLDevice, MTLTexture, Metal Performance HUD, GPU counters on Apple Silicon, Metal frame capture, Metal API validation, Metal shader validation, xcrun metal, xctrace, Instruments GPU trace, MoltenVK debugging, Vulkan on macOS, Apple Silicon GPU, Metal 3, Metal 4, Metal compute kernel, MetalFX, Metal Performance Shaders, Metal ray tracing, "profile my Metal app", "why is my shader slow", "capture a Metal frame", "Metal validation error", "debug Metal on Mac", "GPU trace on macOS". DO NOT use for: RenderDoc, Vulkan on Windows/Linux, D3D debugging, PIX, Nsight, CSS rendering, React rendering, server-side rendering, HTML layout, browser DevTools, web performance.

Tổng quan

Nhà phát hànhrudybear
Kho mãmetal-ai-skill
Tên kỹ năngmetal-gpu-debug
Sao
40
Bản fork
3
Tệp đi kèm
2
Giấy phépMIT
Liên kết
  • Hướng dẫn bằng Markdown

    Tệp SKILL.md được mô hình tải khi cần, nên chỉ tốn token khi có yêu cầu thực sự phù hợp.

  • Dùng được với mọi LLM

    Kỹ năng AI là Markdown thuần túy, không phải mã dành riêng cho một nhà cung cấp, nên dùng được với GPT, Claude, Gemini, Grok hoặc mô hình cục bộ.

  • 2 tệp đi kèm

    Các tập lệnh, mẫu và tài liệu tham khảo mà mô hình có thể đọc khi làm việc. Các tệp chỉ cho phép đọc và không bao giờ được thực thi.

  • Mã nguồn mở

    Được rudybear công bố trên GitHub. Hãy đọc nội dung nguồn trước khi cài đặt.

Cài đặt

Cài đặt kỹ năng AI Metal Gpu Debug trong TypingMind để dùng với bất kỳ LLM nào, hoặc thêm vào một tác nhân AI khác có thể đọc SKILL.md.

1

Cài đặt trong TypingMind

TypingMind cài đặt kỹ năng trực tiếp từ thư mục GitHub — đọc SKILL.md, đóng gói các tệp tài nguyên và lưu kết quả trên máy.

  1. Mở ứng dụng và vào Plugin → Kỹ năng.
  2. Chọn "Cài đặt từ GitHub".
  3. Dán URL thư mục kỹ năng bên dưới và xác nhận.
    https://github.com/rudybear/metal-ai-skill/tree/main/.claude/skills/metal-gpu-debug
  4. Bật kỹ năng trong từng cuộc trò chuyện mà bạn muốn sử dụng kỹ năng đó.
Plugin → Kỹ năng → Thêm kỹ năng → Từ URL GitHub, sau đó dán URL thư mục và nhấn Tiếp tục.
2

Cài đặt trong tác nhân AI khác

Bất kỳ tác nhân AI nào đọc được định dạng Agent Skills đều có thể dùng kỹ năng này — hãy sao chép thư mục vào thư mục kỹ năng của tác nhân AI đó.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/rudybear/metal-ai-skill.git /tmp/metal-ai-skill
mkdir -p .claude/skills
cp -r /tmp/metal-ai-skill/.claude/skills/metal-gpu-debug .claude/skills/metal-gpu-debug
Khởi động lại Claude Code sau khi sao chép để ứng dụng nhận kỹ năng mới.

Sử dụng trong TypingMind

Bật Metal Gpu Debug trong bất kỳ cuộc trò chuyện nào trên TypingMind, mô hình sẽ tự xử lý phần còn lại. Tên và mô tả của kỹ năng nằm trong prompt hệ thống. Ngay khi có yêu cầu phù hợp, mô hình tự tải toàn bộ hướng dẫn — bạn không cần gọi thủ công và kỹ năng không tốn token cho đến khi thực sự được dùng.

Mô hình tự tải Metal Gpu Debug ngay khi có yêu cầu phù hợp.

Dùng được với mọi mô hình AI

Kỹ năng AI là các hướng dẫn Markdown thuần túy, không phải mã dành riêng cho một nhà cung cấp, nên Metal Gpu Debug không bị giới hạn ở mô hình mà kỹ năng này được viết cho. Chỉ cần cài đặt một lần trong TypingMind rồi dùng với GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama hoặc mô hình cục bộ do bạn tự chạy — tất cả đều dùng khóa API của riêng bạn.

  • Chỉ tải khi cần

    Prompt hệ thống chỉ chứa tên và mô tả. Hướng dẫn được tải khi có yêu cầu phù hợp đầu tiên, nên kỹ năng chưa dùng không tốn chi phí.

  • Đổi mô hình ngay trong cuộc trò chuyện

    Vì kỹ năng là hướng dẫn chứ không phải mã, việc đổi mô hình không làm kỹ năng ngừng hoạt động — mô hình tiếp theo vẫn đọc cùng tệp SKILL.md.

Hướng dẫn của kỹ năng

Đây là nội dung SKILL.md mà mô hình tải. Hãy đọc trước khi cài đặt — kỹ năng là những hướng dẫn mà mô hình của bạn sẽ làm theo.

Metal GPU Debugging & Profiling Skill

Overview

This skill enables Metal GPU profiling, debugging, and shader analysis on macOS using Apple's command-line toolchain. The primary tools are:

  • xctrace — CLI for Instruments; records Metal System Traces, exports GPU data as XML
  • xcrun metal — Metal shader compiler toolchain (compile, archive, link)
  • Metal environment variables — validation layers, Performance HUD, programmatic capture
  • log — macOS unified logging for Metal HUD and validation output
  • parse_gputrace.py — CLI tool for extracting buffer/texture data from .gputrace captures

Prerequisites

Before any Metal debugging, verify the environment:

bash
# Check Xcode is installed (full Xcode, not just Command Line Tools)
xcode-select -p
# Expected: /Applications/Xcode.app/Contents/Developer

# Check xctrace is available
xcrun xctrace version

# Check Metal compiler
xcrun -sdk macosx metal --version

# List available GPU devices (requires a Swift/ObjC helper or Python)
system_profiler SPDisplaysDataType | grep -A5 "Chipset\|Metal"

IMPORTANT: xctrace and Metal tools require full Xcode, not just Command Line Tools.

1. Automated Session Lifecycle

Every Metal debugging session follows a strict pipeline: Doctor → Record → Export → Parse → Analyze → Report → Cleanup. Claude should execute this pipeline autonomously, not just suggest commands.

Step 0: Doctor — Verify environment

Run this FIRST before any debugging session. If any check fails, stop and tell the user what's missing.

bash
# Create working directories
mkdir -p ./traces/analysis

# Verify toolchain
xcode-select -p                          # Must show Xcode.app path, NOT CommandLineTools
xcrun xctrace version                    # Must succeed
xcrun -sdk macosx metal --version        # Must succeed
system_profiler SPDisplaysDataType | grep -i "metal\|chipset"  # GPU info

# For iOS: check connected devices
xcrun xctrace list devices 2>/dev/null   # List all available targets

# Check parse helper is available
python3 -c "import xml.etree.ElementTree" 2>/dev/null && echo "XML parser OK"

If xcode-select -p returns /Library/Developer/CommandLineTools, tell the user:

Xcode Command Line Tools is installed but full Xcode is required.
Install from: https://developer.apple.com/xcode/
Then run: sudo xcode-select -s /Applications/Xcode.app/Contents/Developer

Step 1: Record — Capture a Metal System Trace

Choose the right recording mode based on the user's request:

bash
# MODE A: Launch and profile (most common)
xcrun xctrace record \
  --template 'Metal System Trace' \
  --time-limit 10s \
  --no-prompt \
  --output ./traces/capture.trace \
  --launch -- /path/to/app [args...]

# MODE B: Attach to running process
xcrun xctrace record \
  --template 'Metal System Trace' \
  --attach <PID_OR_NAME> \
  --time-limit 10s \
  --no-prompt \
  --output ./traces/capture.trace

# MODE C: With validation enabled (for debugging errors)
xcrun xctrace record \
  --template 'Metal System Trace' \
  --env MTL_DEBUG_LAYER=1 \
  --env MTL_SHADER_VALIDATION=1 \
  --time-limit 10s \
  --no-prompt \
  --output ./traces/capture.trace \
  --launch -- /path/to/app

# MODE D: iOS device (over USB)
xcrun xctrace record \
  --template 'Metal System Trace' \
  --device '<DEVICE_NAME_OR_UDID>' \
  --attach <APP_NAME> \
  --time-limit 10s \
  --no-prompt \
  --output ./traces/capture.trace

Decision guide:

  • User says "profile my app" → Mode A
  • User says "it's already running" → Mode B
  • User says "I'm getting errors" or "something is wrong" → Mode C
  • User mentions iPhone/iPad → Mode D

IMPORTANT: Always use --no-prompt for automation. Always use --time-limit (default 10s, adjust if user specifies). Always use --output with explicit path.

Step 2: Export — Extract structured data from trace

First discover what's in the trace, then export the relevant tables.

bash
# 2a. Get table of contents (ALWAYS do this first)
xcrun xctrace export --input ./traces/capture.trace --toc \
  > ./traces/analysis/toc.xml

# 2b. Show available schemas to decide what to export
grep 'schema=' ./traces/analysis/toc.xml

# 2c. Export Metal GPU driver events (primary data source)
xcrun xctrace export --input ./traces/capture.trace \
  --output ./traces/analysis/gpu_events.xml \
  --xpath '/trace-toc/run[@number="1"]/data/table[@schema="metal-driver-event-intervals"]'

# 2d. Export GPU hardware counters (if available — Apple Silicon)
xcrun xctrace export --input ./traces/capture.trace \
  --output ./traces/analysis/gpu_counters.xml \
  --xpath '/trace-toc/run[@number="1"]/data/table[@schema="gpu-counter-intervals"]' \
  2>/dev/null || echo "No GPU counter data in this trace"

# 2e. Export Metal GPU execution intervals (if available)
xcrun xctrace export --input ./traces/capture.trace \
  --output ./traces/analysis/gpu_intervals.xml \
  --xpath '/trace-toc/run[@number="1"]/data/table[@schema="metal-gpu-intervals"]' \
  2>/dev/null || echo "No GPU interval data in this trace"

Schema availability varies by Xcode version, template, and GPU. Always check the TOC first and export what's there. Don't fail if a schema is missing — report what was found.

Step 3: Parse — Convert XML to structured data

Use the parse_trace.py helper (included in this repo) or inline Python to extract actionable data.

bash
# Summary of what was captured
python3 parse_trace.py ./traces/analysis/gpu_events.xml --summary

# Get structured data as JSON
python3 parse_trace.py ./traces/analysis/gpu_events.xml --format json --limit 50

# Get as TSV for scanning
python3 parse_trace.py ./traces/analysis/gpu_events.xml --format tsv --limit 30

# If parse_trace.py is not available, use inline Python:
python3 << 'PARSE_SCRIPT'
import xml.etree.ElementTree as ET
import json

tree = ET.parse('./traces/analysis/gpu_events.xml')
root = tree.getroot()

# Build ref resolution map
id_map = {}
for elem in root.iter():
    eid = elem.get('id')
    if eid is not None:
        id_map[eid] = elem

def resolve(elem):
    ref = elem.get('ref')
    if ref and ref in id_map:
        return id_map[ref]
    return elem

def get_val(elem):
    r = resolve(elem)
    return r.get('fmt', r.text or '')

rows = root.findall('.//row')
print(f"Total events: {len(rows)}")

# Parse first 30 rows into dicts
parsed = []
headers = None
for row in rows[:30]:
    cols = list(row)
    if headers is None:
        headers = [c.tag for c in cols]
    entry = {headers[i]: get_val(c) for i, c in enumerate(cols)}
    parsed.append(entry)

print(json.dumps(parsed, indent=2))
PARSE_SCRIPT

Step 4: Analyze — Interpret the data

After parsing, Claude should analyze the results and look for:

Performance profiling:

  • GPU busy time per frame (>16ms = below 60fps, >8ms = below 120fps)
  • Large wire memory events (excessive per-frame resource allocation)
  • Gaps between GPU submissions (CPU-bound)
  • Long shader execution intervals (complex shaders)
  • Imbalanced encoder durations (one pass dominating)

Validation errors:

  • Pattern-match stderr and log output for Metal error codes
  • Classify errors: API misuse vs shader bug vs resource issue
  • Map errors to specific command encoders via labels

Shader issues:

  • Compile-time warnings/errors from xcrun metal
  • Runtime validation errors from MTL_SHADER_VALIDATION

Step 5: Report — Present findings to user

Structure the report as:

  1. Environment: GPU model, macOS version, device (Mac/iOS)
  2. Summary: Total events, recording duration, overall health
  3. Key findings: Sorted by severity (errors → warnings → info)
  4. Specific data: Relevant numbers, event counts, durations
  5. Recommendations: Concrete next steps

Step 6: Cleanup

bash
# Remove large trace files when analysis is complete
# (only if user doesn't need the raw trace)
rm -rf ./traces/capture.trace

# Keep analysis outputs for reference
ls -la ./traces/analysis/

Complete automated workflow example

This is what Claude should execute end-to-end when a user says "profile my Metal app":

bash
#!/bin/bash
set -e
APP_PATH="$1"
TRACE_DIR="./traces"
ANALYSIS_DIR="$TRACE_DIR/analysis"

# Doctor
mkdir -p "$ANALYSIS_DIR"
xcode-select -p >/dev/null 2>&1 || { echo "ERROR: Xcode not found"; exit 1; }
xcrun xctrace version >/dev/null 2>&1 || { echo "ERROR: xctrace not available"; exit 1; }

# Record
echo "Recording Metal System Trace (10s)..."
xcrun xctrace record \
  --template 'Metal System Trace' \
  --time-limit 10s \
  --no-prompt \
  --output "$TRACE_DIR/capture.trace" \
  --launch -- "$APP_PATH"

# Export TOC
echo "Exporting trace data..."
xcrun xctrace export --input "$TRACE_DIR/capture.trace" --toc \
  > "$ANALYSIS_DIR/toc.xml"

# Export all available Metal tables
for schema in metal-driver-event-intervals gpu-counter-intervals metal-gpu-intervals; do
  if grep -q "schema=\"$schema\"" "$ANALYSIS_DIR/toc.xml"; then
    echo "Exporting $schema..."
    xcrun xctrace export --input "$TRACE_DIR/capture.trace" \
      --output "$ANALYSIS_DIR/${schema}.xml" \
      --xpath "/trace-toc/run[@number=\"1\"]/data/table[@schema=\"$schema\"]"
  fi
done

# Parse and summarize
echo "Analyzing..."
for xml in "$ANALYSIS_DIR"/*.xml; do
  [ "$xml" = "$ANALYSIS_DIR/toc.xml" ] && continue
  echo "=== $(basename "$xml") ==="
  python3 parse_trace.py "$xml" --summary 2>/dev/null || \
    python3 -c "
import xml.etree.ElementTree as ET
tree = ET.parse('$xml')
rows = tree.getroot().findall('.//row')
print(f'  Rows: {len(rows)}')
"
done

echo "Done. Analysis files in $ANALYSIS_DIR/"

Parallel workflows: Validation + Profiling + Logs

Claude can run multiple debugging streams simultaneously for maximum signal:

bash
# Record trace with all validation layers AND HUD logging in one shot
xcrun xctrace record \
  --template 'Metal System Trace' \
  --env MTL_DEBUG_LAYER=1 \
  --env MTL_SHADER_VALIDATION=1 \
  --env MTL_HUD_ENABLED=1 \
  --env MTL_HUD_LOGGING_ENABLED=1 \
  --time-limit 10s \
  --no-prompt \
  --output ./traces/full_debug.trace \
  --launch -- /path/to/app 2> ./traces/analysis/stderr.log &

TRACE_PID=$!

# Simultaneously capture Metal logs from unified log
log stream --predicate 'subsystem == "com.apple.Metal"' \
  --timeout 15 > ./traces/analysis/metal_log.txt 2>/dev/null &

LOG_PID=$!

# Wait for trace to finish
wait $TRACE_PID

# Give log stream a moment then stop it
sleep 2
kill $LOG_PID 2>/dev/null

# Now analyze ALL data sources:
echo "=== Validation Errors (stderr) ==="
grep -i "error\|warning\|invalid\|fault" ./traces/analysis/stderr.log || echo "None"

echo "=== Metal Log Entries ==="
wc -l < ./traces/analysis/metal_log.txt
grep -i "error" ./traces/analysis/metal_log.txt || echo "No errors in log"

echo "=== Trace Data ==="
xcrun xctrace export --input ./traces/full_debug.trace --toc

Session state awareness

Unlike RenderDoc's daemon model (open/close), xctrace sessions are fire-and-forget: each record command runs to completion, and the resulting .trace file is a self-contained snapshot. This means:

  • No session to manage — no open/close, no daemon, no leaked processes
  • Multiple traces can coexist — name them meaningfully (before.trace, after.trace)
  • Traces are immutable — once recorded, they don't change
  • Export is idempotent — re-export the same trace as many times as needed
  • Cleanup is just file deletion — rm the .trace bundle when done

2. iOS Device Support

xctrace can profile Metal apps on physical iOS/iPadOS devices over USB or Wi-Fi.

List connected devices

bash
xcrun xctrace list devices
# Shows: Mac, connected iPhones/iPads, simulators

Record on iOS device

bash
# By device name
xcrun xctrace record \
  --template 'Metal System Trace' \
  --device 'iPhone 15 Pro' \
  --attach MyApp \
  --time-limit 10s \
  --output ios_trace.trace

# By UDID (more reliable)
xcrun xctrace record \
  --template 'Metal System Trace' \
  --device 00008110-XXXXXXXXXXXX \
  --attach MyApp \
  --time-limit 10s \
  --output ios_trace.trace

# Launch app on device (must be installed)
xcrun xctrace record \
  --template 'Metal System Trace' \
  --device 'iPhone 15 Pro' \
  --time-limit 10s \
  --output ios_trace.trace \
  --launch -- com.yourcompany.yourapp

iOS-specific notes

  • Device must be unlocked and trusted by the Mac
  • App must be installed on device (use Xcode or ios-deploy)
  • --launch uses bundle identifier (not path) on iOS
  • --attach uses process name or PID
  • Export/parse works identically — the .trace format is the same
  • Shader compilation for iOS uses -sdk iphoneos:
    bash
    xcrun -sdk iphoneos metal -c Shader.metal -o Shader.air
  • .gputrace capture on iOS requires Xcode attached to the device

3. Metal System Trace — Record & Export

Metal System Trace is the primary tool for GPU profiling. It captures CPU/GPU timeline, driver events, shader execution, and hardware counters.

Record a trace

bash
# Profile a running app by name
xcrun xctrace record \
  --template 'Metal System Trace' \
  --attach <PID_OR_NAME> \
  --time-limit 5s \
  --output trace.trace

# Launch and profile
xcrun xctrace record \
  --template 'Metal System Trace' \
  --time-limit 5s \
  --output trace.trace \
  --launch -- /path/to/your/app [args...]

# With environment variables (e.g., enable validation)
xcrun xctrace record \
  --template 'Metal System Trace' \
  --env MTL_DEBUG_LAYER=1 \
  --env MTL_SHADER_VALIDATION=1 \
  --time-limit 5s \
  --output trace.trace \
  --launch -- /path/to/your/app

Key options:

  • --time-limit 5s — auto-stop after duration (supports ms, s, m, h)
  • --attach PID — attach to running process
  • --all-processes — trace all Metal apps system-wide
  • --env VAR=value — set environment variables for launched process
  • --target-stdout - — redirect app stdout to terminal
  • --no-prompt — skip prompts (useful in scripts)

Other useful templates

bash
# List all available templates
xcrun xctrace list templates
# Key Metal-relevant templates:
#   - Metal System Trace    (GPU timeline, driver events, counters)
#   - Game Performance       (Metal + display + thermal)
#   - Counters               (hardware performance counters)
#   - GPU                    (GPU-focused template, if available)

Export trace data as XML

bash
# See what's in the trace (table of contents)
xcrun xctrace export --input trace.trace --toc

# Export Metal driver events (GPU work intervals, wire memory, etc.)
xcrun xctrace export --input trace.trace \
  --xpath '/trace-toc/run[@number="1"]/data/table[@schema="metal-driver-event-intervals"]'

# Export to file instead of stdout
xcrun xctrace export --input trace.trace \
  --output metal_events.xml \
  --xpath '/trace-toc/run[@number="1"]/data/table[@schema="metal-driver-event-intervals"]'

# Export GPU counter data
xcrun xctrace export --input trace.trace \
  --xpath '/trace-toc/run[@number="1"]/data/table[@schema="gpu-counter-intervals"]'

Common Metal table schemas

SchemaContains
metal-driver-event-intervalsMetal driver events (GPU work, wire memory, resource events)
gpu-counter-intervalsHardware GPU performance counters
metal-gpu-intervalsGPU execution intervals per encoder
time-profileCPU time profiling samples

TIP: Always run --toc first to see available schemas — they vary by template, Xcode version, and GPU.

Parse exported XML

The XML uses a reference system to avoid duplication. Nodes with id attributes are originals; nodes with ref attributes point back to them.

bash
# Quick extraction with xmllint or python
xcrun xctrace export --input trace.trace \
  --xpath '/trace-toc/run[@number="1"]/data/table[@schema="metal-driver-event-intervals"]' \
  | python3 -c "
import sys, xml.etree.ElementTree as ET
tree = ET.parse(sys.stdin)
for row in tree.findall('.//row'):
    print([col.get('fmt', col.text or '') for col in row])
"

4. Metal Validation Layers

Runtime error detection for Metal API misuse and shader bugs.

Enable via environment variables

bash
# API Validation — catches Metal API misuse
export MTL_DEBUG_LAYER=1

# Shader Validation — instruments shaders to detect GPU-side errors
export MTL_SHADER_VALIDATION=1

# Combined: launch app with both
MTL_DEBUG_LAYER=1 MTL_SHADER_VALIDATION=1 /path/to/your/app

Enable via xctrace

bash
xcrun xctrace record \
  --template 'Metal System Trace' \
  --env MTL_DEBUG_LAYER=1 \
  --env MTL_SHADER_VALIDATION=1 \
  --time-limit 10s \
  --output validated.trace \
  --launch -- /path/to/your/app

Read validation errors

Validation errors appear in stderr and in macOS unified log:

bash
# Stream Metal validation errors live
log stream --predicate 'subsystem == "com.apple.Metal"' --level error

# Search recent logs
log show --predicate 'subsystem == "com.apple.Metal"' --last 5m

Programmatic validation log access

If you're writing Metal code, commandBuffer.logs provides structured error info after completion — encoder label, debug location (file + line), and error classification.

5. Metal Performance HUD

Real-time overlay showing FPS, frame time, GPU time, memory usage.

Enable

bash
# Per-process via environment variable
MTL_HUD_ENABLED=1 /path/to/your/app

# System-wide (all Metal apps)
/bin/launchctl setenv MTL_HUD_ENABLED 1
# Disable:
/bin/launchctl unsetenv MTL_HUD_ENABLED

# Enable HUD data logging to syslog
MTL_HUD_ENABLED=1 MTL_HUD_LOGGING_ENABLED=1 /path/to/your/app

Parse HUD log data

When MTL_HUD_LOGGING_ENABLED=1, metrics are logged to the system log:

bash
# Stream HUD metrics
log stream --predicate 'subsystem == "com.apple.Metal" AND category == "HUD"'

# Export recent HUD data
log show --predicate 'subsystem == "com.apple.Metal" AND category == "HUD"' --last 1m

HUD metrics include: FPS, present interval (frame time), GPU time, process memory, GPU memory, display refresh rate, direct vs composited rendering path.

6. Programmatic Frame Capture (.gputrace)

Capture Metal frames to .gputrace files without Xcode attached, then inspect buffer/texture data from CLI.

Via environment variables (MoltenVK / any Metal app)

bash
# For Vulkan apps via MoltenVK:
export METAL_CAPTURE_ENABLED=1
export MVK_CONFIG_AUTO_GPU_CAPTURE_SCOPE=2          # 1=device lifecycle, 2=first frame
export MVK_CONFIG_AUTO_GPU_CAPTURE_OUTPUT_FILE=/tmp/capture.gputrace
/path/to/vulkan/app

# For native Metal apps (requires Info.plist MetalCaptureEnabled=true
# or METAL_CAPTURE_ENABLED=1 environment variable):
METAL_CAPTURE_ENABLED=1 /path/to/metal/app

Via MTLCaptureManager (in-app)

For apps you control, add capture support using MTLCaptureManager. See capture_frame.swift for a complete example. The key pattern for adding capture to an existing app:

swift
let captureManager = MTLCaptureManager.shared()
if captureManager.supportsDestination(.gpuTraceDocument) {
    let descriptor = MTLCaptureDescriptor()
    descriptor.captureObject = device
    descriptor.destination = .gpuTraceDocument
    descriptor.outputURL = URL(fileURLWithPath: "./capture.gputrace")
    try captureManager.startCapture(with: descriptor)

    // ... encode and submit Metal work ...

    captureManager.stopCapture()
}

Run with: METAL_CAPTURE_ENABLED=1 ./your_app

Open .gputrace in Xcode

bash
open /tmp/capture.gputrace
# Opens in Xcode's Metal Debugger with full inspection capabilities:
# - Draw call list and stepping
# - Pipeline state at each draw
# - Texture/buffer inspection
# - Shader debugging
# - Performance profiling
# - Dependency viewer

Autonomous Metal Debugging Workflow

When debugging a Metal rendering issue, Claude executes this workflow autonomously. It maximizes signal by gathering data from multiple sources in parallel, then branches based on what data is actually available.

Step 1: Gather all signal sources (run in parallel)

Launch these data-gathering steps simultaneously — they are independent:

bash
# A. Screenshot the app's rendered output
./build_and_run.sh --screenshot        # preferred: in-app framebuffer readback
# OR: screencapture -w -x output.png   # fallback: macOS window capture

# B. Capture .gputrace (programmatic)
METAL_CAPTURE_ENABLED=1 ./your_app     # produces capture.gputrace

# C. Compile shaders with maximum warnings
xcrun -sdk macosx metal -c -Weverything Shaders.metal -o /dev/null 2>&1

# D. Read source code — Claude reads .metal files and Swift renderer code directly
Step 2: Analyze the capture
bash
# 2a. List all resources and shader functions
python3 parse_gputrace.py capture.gputrace

# 2b. Check what data files exist in the capture
ls capture.gputrace/MTLBuffer-* 2>/dev/null && echo "BUFFER DATA AVAILABLE" || echo "NO BUFFER FILES"
ls capture.gputrace/MTLTexture-* 2>/dev/null && echo "TEXTURE DATA AVAILABLE" || echo "NO TEXTURE FILES"

Branch on buffer availability:

  • MTLBuffer files exist (Xcode-initiated captures): Parse buffer data directly
    bash
    python3 parse_gputrace.py capture.gputrace --buffer "Vertex" --layout float4 --index 0-10
    python3 parse_gputrace.py capture.gputrace --dump-all
  • No MTLBuffer files (typical for programmatic captures): Fall back to source code analysis
    • Read the Swift/ObjC code that creates and fills buffers
    • Read the .metal shader code that consumes the buffers
    • The capture metadata still tells you what resources exist and what shaders run — use this as an inventory to guide source code reading
  • MTLTexture files exist: Read raw texture data for render target analysis
Step 3: Diagnose from all available sources

Combine signal from every source gathered in Steps 1–2. Each source catches different bug categories:

SourceWhat it revealsBug categories
ScreenshotVisual outputWrong colors, flipped geometry, missing faces, transparency issues, blank screen
Source code (.metal + Swift)Logic and data flowWrong vertex data, shader math errors, incorrect pipeline config, buffer layout mismatches
Capture metadataResource inventoryConfirms what buffers/textures exist, shader function names, resource labels
Shader compilation warningsStatic analysisUnused variables, implicit conversions, potential precision issues
Validation errors (if enabled)API correctnessMismatched formats, missing bindings, out-of-bounds access, shader faults

Decision logic when buffer data is unavailable:

  • If no MTLBuffer files → Claude reads source code to understand vertex data, uniform values, and buffer layouts
  • If no MTLTexture files → Claude uses screenshot for visual inspection of rendered output
  • Programmatic captures always provide: resource labels, shader function names, texture snapshots (render targets)
Step 4: Fix and verify
bash
# 1. Apply fixes to source code (.metal shaders, Swift renderer)

# 2. Rebuild and screenshot
./build_and_run.sh --screenshot

# 3. Compare before/after visually (Claude reads output.png)

# 4. If still wrong, repeat from Step 1

The loop continues until the screenshot shows correct output or the user is satisfied.

Label Injection

Claude should ensure every Metal object has a .label set in source code. Labels are preserved in .gputrace captures and enable resource identification from CLI.

swift
// Add .label to every MTLBuffer, MTLTexture, MTLCommandBuffer, MTLCommandEncoder
vertexBuffer.label = "Vertex Buffer"
colorBuffer.label = "Color Buffer"
uniformBuffer.label = "Uniform Buffer"
commandBuffer.label = "Frame \(frameNumber)"
encoder.label = "Main Render Pass"

Rules:

  • Use descriptive names with spaces (e.g., "Particle Buffer" not "particleBuf")
  • Labels with spaces are reliably distinguishable from binary data in the .gputrace
  • Never remove existing labels — only add missing ones
  • Add labels to every object that lacks one

Reference: parse_gputrace.py usage

bash
# List all captured resources with their labels
python3 parse_gputrace.py capture.gputrace

# Read specific buffer by label (partial match works)
python3 parse_gputrace.py capture.gputrace --buffer "Color Output" --layout float4 --index 100

# Read compound struct (e.g., Particle = position + velocity + color)
python3 parse_gputrace.py capture.gputrace --buffer "Particle" --layout "float4,float4,float4" --index 0-10

# Dump summary statistics for all buffers
python3 parse_gputrace.py capture.gputrace --dump-all

# Output as JSON
python3 parse_gputrace.py capture.gputrace --buffer "Color Output" --layout float4 --index 100 --json
Supported layout types
LayoutBytesExample
float4Scalar energy, distance, time
float28UV coordinates
float312Position, normal (without padding)
float416RGBA color, position+mass, SIMD4
uint324Index, count
int324Signed integer
float4,float4,float448Compound struct (e.g., Particle)

Reference: .gputrace internal structure

FileFormatContents
MTLBuffer-{id}-{snap}Raw binary (IEEE 754 LE)GPU buffer memory snapshot
MTLTexture-{id}-{snap}Raw binaryGPU texture memory snapshot
metadataBinary plistSession UUID, API, device info
device-resources-*MTSP binaryResource registry with labels, shader names
store0zlib-compressed MTSPEncoded command buffer data
capture / unsorted-captureMTSP binaryMetal API call sequences
indexCustom (xdic magic)File table / hash table

Reference: CLI capabilities by capture type

CapabilityCLI (programmatic capture)CLI (Xcode-initiated capture)Xcode GUI
List resources with labels✅✅✅
List shader function names✅✅✅
Read texture/render target data✅ (MTLTexture files present)✅✅
Read buffer contents at index❌ (no MTLBuffer files)✅✅
Buffer statistics (min/max/mean)❌✅❌ (manual)
Draw call stepping❌❌✅
Pixel history❌❌✅
Shader step-through❌❌✅
Pipeline state inspection❌❌✅
Dependency viewer❌❌✅
Command buffer replay❌❌✅

Reference: Screenshot capture techniques

Method 1: In-app auto-screenshot (Preferred)

The most reliable approach is to add a --screenshot mode to the app itself. This renders a few frames, reads back the framebuffer texture, and saves it as a PNG — no external tools required.

Swift code to add to your Metal app:

swift
// Add to your renderer class:
static func saveTexture(_ texture: MTLTexture, to path: String) {
    let w = texture.width, h = texture.height
    let bytesPerRow = w * 4
    var pixels = [UInt8](repeating: 0, count: h * bytesPerRow)
    texture.getBytes(&pixels, bytesPerRow: bytesPerRow,
                     from: MTLRegion(origin: MTLOrigin(), size: MTLSize(width: w, height: h, depth: 1)),
                     mipmapLevel: 0)
    // BGRA → RGBA, force alpha to 255 for screenshot visibility
    for i in stride(from: 0, to: pixels.count, by: 4) {
        let tmp = pixels[i]
        pixels[i] = pixels[i + 2]
        pixels[i + 2] = tmp
        pixels[i + 3] = 255
    }
    let rep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: w, pixelsHigh: h,
                                bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true,
                                isPlanar: false, colorSpaceName: .deviceRGB,
                                bytesPerRow: bytesPerRow, bitsPerPixel: 32)!
    memcpy(rep.bitmapData!, &pixels, pixels.count)
    let data = rep.representation(using: .png, properties: [:])!
    try! data.write(to: URL(fileURLWithPath: path))
}

Usage in the draw loop (save after a few frames, then exit):

swift
// In draw(in:) after cb.present(drawable) and cb.commit():
if frameCount == 3 && screenshotMode {
    cb.waitUntilCompleted()
    Self.saveTexture(drawable.texture, to: "./output.png")
    print("Screenshot saved: ./output.png")
    DispatchQueue.main.async { NSApp.terminate(nil) }
}

Important: Set metalView.framebufferOnly = false before rendering to allow texture readback.

Method 2: macOS screencapture (Fallback)

If the app doesn't support --screenshot, use macOS screencapture to capture the window:

bash
# Launch the app in background
./your_app &
APP_PID=$!
sleep 1

# Capture the app's window (-w = frontmost window, -x = no sound)
screencapture -w -x /tmp/screenshot.png

# Stop the app
kill $APP_PID 2>/dev/null

7. Metal Shader Compilation

Compile, validate, and inspect Metal shaders from the command line.

Compile shaders

bash
# Compile .metal to intermediate representation (.air)
xcrun -sdk macosx metal -c MyShader.metal -o MyShader.air

# With warnings and debug info
xcrun -sdk macosx metal -c -gline-tables-only -Weverything MyShader.metal -o MyShader.air

# Archive
xcrun -sdk macosx metal-ar rcs MyShader.metalar MyShader.air

# Link into a Metal library (.metallib)
xcrun -sdk macosx metallib MyShader.metalar -o MyShader.metallib

# One-step compile + link
xcrun -sdk macosx metal MyShader.metal -o MyShader.metallib

Cross-compile for specific targets

bash
# List available GPU architectures
xcrun metal-arch

# Target specific platform
xcrun -sdk iphoneos metal -c Shader.metal -o Shader.air
xcrun -sdk macosx metal -c -target air64-apple-macos14 Shader.metal -o Shader.air

HLSL to Metal (via shader converter)

bash
# Convert HLSL → DXIL → Metal IR
dxc shaders.hlsl -T vs_6_0 -E MainVS -Fo vertex.dxil
metal-shaderconverter vertex.dxil -o vertex.metallib

Validate shaders without running

bash
# Compile with all warnings — catches issues at build time
xcrun -sdk macosx metal -c -Weverything -Werror MyShader.metal -o /dev/null

# Check Metal version compatibility
xcrun -sdk macosx metal -c -std=metal3.0 MyShader.metal -o /dev/null

8. GPU Device Information

bash
# System-level GPU info
system_profiler SPDisplaysDataType

# Metal feature set / GPU family (grep for relevant info)
system_profiler SPDisplaysDataType | grep -E "Chipset|Metal|VRAM|GPU"

9. Debugging Recipes

Recipe: Profile GPU performance

bash
# 1. Record a Metal System Trace
xcrun xctrace record \
  --template 'Metal System Trace' \
  --time-limit 10s \
  --output perf.trace \
  --launch -- /path/to/app

# 2. See what data is available
xcrun xctrace export --input perf.trace --toc

# 3. Export GPU driver events
xcrun xctrace export --input perf.trace \
  --output gpu_events.xml \
  --xpath '/trace-toc/run[@number="1"]/data/table[@schema="metal-driver-event-intervals"]'

# 4. Export GPU counters (if available)
xcrun xctrace export --input perf.trace \
  --output gpu_counters.xml \
  --xpath '/trace-toc/run[@number="1"]/data/table[@schema="gpu-counter-intervals"]'

# 5. Open in Instruments for visual analysis
open perf.trace

Recipe: Find shader compilation errors

bash
# 1. Compile with maximum warnings
xcrun -sdk macosx metal -c -Weverything -Werror MyShader.metal -o /dev/null 2>&1

# 2. If it compiles, check at runtime with validation
MTL_SHADER_VALIDATION=1 /path/to/app 2>&1 | tee shader_errors.log

# 3. Check logs for shader validation errors
log show --predicate 'subsystem == "com.apple.Metal"' --last 5m --level error

Recipe: Detect Metal API misuse

bash
# 1. Run with API validation
MTL_DEBUG_LAYER=1 /path/to/app 2>&1 | tee api_errors.log

# 2. Record with validation for post-mortem analysis
xcrun xctrace record \
  --template 'Metal System Trace' \
  --env MTL_DEBUG_LAYER=1 \
  --time-limit 10s \
  --output validated.trace \
  --launch -- /path/to/app

# 3. Check for errors
grep -i "error\|warning\|invalid\|violation" api_errors.log

Recipe: Monitor real-time performance

bash
# 1. Enable HUD with logging
MTL_HUD_ENABLED=1 MTL_HUD_LOGGING_ENABLED=1 /path/to/app &
APP_PID=$!

# 2. Stream performance data
log stream --predicate 'subsystem == "com.apple.Metal" AND category == "HUD"' &

# 3. When done, stop
kill $APP_PID

Recipe: Capture a frame for Xcode debugging

bash
# For Vulkan apps (via MoltenVK)
METAL_CAPTURE_ENABLED=1 \
MVK_CONFIG_AUTO_GPU_CAPTURE_SCOPE=2 \
MVK_CONFIG_AUTO_GPU_CAPTURE_OUTPUT_FILE=/tmp/frame.gputrace \
/path/to/vulkan/app

# Open in Xcode Metal Debugger
open /tmp/frame.gputrace

Recipe: Debug Metal rendering issues (visual + data)

Use the Autonomous Metal Debugging Workflow in Section 6. It covers screenshot capture, .gputrace analysis, source code reading, shader compilation, and the fix/verify loop — with fallbacks when buffer data is unavailable.

Recipe: Compare performance before/after a change

bash
# 1. Record baseline
xcrun xctrace record \
  --template 'Metal System Trace' \
  --time-limit 10s \
  --output before.trace \
  --launch -- /path/to/app

# 2. Make your change, rebuild

# 3. Record after
xcrun xctrace record \
  --template 'Metal System Trace' \
  --time-limit 10s \
  --output after.trace \
  --launch -- /path/to/app

# 4. Export both and compare
xcrun xctrace export --input before.trace --output before.xml \
  --xpath '/trace-toc/run[@number="1"]/data/table[@schema="metal-driver-event-intervals"]'
xcrun xctrace export --input after.trace --output after.xml \
  --xpath '/trace-toc/run[@number="1"]/data/table[@schema="metal-driver-event-intervals"]'

# 5. Diff the XML or parse with Python for numerical comparison

10. Output Size Management

Metal System Traces can be very large. Follow these rules:

  1. Use --time-limit: Always limit recording duration (5-10s is usually enough)
  2. Export specific schemas: Use --xpath to extract only the data you need
  3. Use --toc first: Understand what's in the trace before bulk-exporting
  4. Pipe through filters: Use grep, xmllint, or Python to extract specific data
  5. Clean up traces: .trace bundles can be 100MB+; delete when done

11. Limitations vs RenderDoc

CapabilityMetal SkillRenderDoc Skill
Frame capture✅ .gputrace✅ .rdc (full CLI)
GPU profiling✅ xctrace + export⚠️ GPU counters if available
Shader compilation✅ xcrun metalN/A (different workflow)
Validation errors✅ env vars + log stream✅ API validation layer
Performance HUD✅ MTL_HUD_ENABLEDN/A
Buffer inspection (CLI)⚠️ Xcode-initiated captures only✅ rdc buffer
Buffer statistics⚠️ Xcode-initiated captures only❌
Draw call inspection❌ Xcode only✅ rdc draws/pipeline
Pixel history❌ Xcode only✅ rdc pixel
Shader step-through❌ Xcode only✅ rdc debug pixel
Render target export❌ Xcode only✅ rdc rt → PNG
Texture readback (CLI)⚠️ MTLTexture files (if present)✅ Full rdc-cli API
Visual output inspection✅ in-app saveTexture or screencapture❌ (headless only)

Key difference: Metal debugging is split between CLI (profiling/validation/buffer inspection) and GUI (Xcode for draw calls, pixel history, shader debugging). The label injection technique with parse_gputrace.py partially bridges this gap by enabling CLI buffer/texture data inspection from .gputrace captures. RenderDoc still provides more complete CLI access.

Command Reference

For the complete xctrace command reference, see references/xctrace-quick-ref.md.

For extended debugging recipes, see references/debugging-recipes.md.

Tệp đi kèm

Mô hình đọc các tệp này khi cần trong lúc kỹ năng được tải. Các tệp được cung cấp ở dạng có thể đọc và không bao giờ được thực thi.

Câu hỏi thường gặp

Kỹ năng AI Metal Gpu Debug làm được gì?

Metal GPU debugging and profiling via xctrace and Apple's Metal toolchain. Use this skill when the user mentions: Metal debugging, .gputrace files, Metal shaders, Metal pipeline state, Metal performance, Metal System Trace, Apple GPU profiling, Metal validation, shader compilation errors, MSL (Metal Shading Language), .metallib files, .air files, Metal render pass, Metal command buffer, Metal encoder, MTLDevice, MTLTexture, Metal Performance HUD, GPU counters on Apple Silicon, Metal frame capture, Metal API validation, Metal shader validation, xcrun metal, xctrace, Instruments GPU trace, Mo...

Vì sao nên dùng Metal Gpu Debug trên TypingMind?

Vì bạn chỉ cần cài một lần rồi dùng với bất kỳ mô hình nào. Metal Gpu Debug là nội dung Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên cùng một kỹ năng có thể chạy trên GPT-5, Claude, Gemini, Grok hoặc mô hình trên thiết bị — và bạn có thể đổi mô hình ngay trong cuộc trò chuyện mà kỹ năng vẫn hoạt động. TypingMind dùng khóa API riêng của bạn, nên bạn thanh toán trực tiếp cho nhà cung cấp thay vì trả phí thuê bao theo suất người dùng, đồng thời kỹ năng và các cuộc trò chuyện vẫn nằm trong bộ lưu trữ của bạn.

Làm thế nào để cài Metal Gpu Debug trong TypingMind?

Mở Plugin → Kỹ năng → Cài đặt từ GitHub trong TypingMind rồi dán https://github.com/rudybear/metal-ai-skill/tree/main/.claude/skills/metal-gpu-debug. TypingMind đọc tệp SKILL.md, đóng gói các tệp đi kèm và cài đặt thành một kỹ năng mà bạn có thể bật cho từng cuộc trò chuyện.

Những mô hình AI nào có thể dùng Metal Gpu Debug?

Bất kỳ mô hình nào bạn kết nối trong TypingMind. Kỹ năng AI là các hướng dẫn bằng Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên GPT, Claude, Gemini, Grok và các mô hình chạy trên thiết bị đều có thể tải kỹ năng này khi có yêu cầu phù hợp.

Bạn có thể dùng Metal Gpu Debug với bao nhiêu mô hình AI?

Bao nhiêu cũng được. Miễn là mô hình hỗ trợ kỹ năng, bạn có thể dùng Metal Gpu Debug với mô hình đó — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama và nhiều mô hình khác — tất cả đều trên TypingMind với khóa API riêng của bạn.

Kỹ năng AI Metal Gpu Debug có miễn phí không?

Có. Kỹ năng này được rudybear phát hành trên GitHub theo giấy phép MIT. Bạn chỉ trả tiền cho nhà cung cấp AI của mình theo số token đã dùng.

Kỹ năng AI là gì?

Kỹ năng AI là một gói hướng dẫn có thể tái sử dụng, chỉ cho mô hình AI cách thực hiện một tác vụ cụ thể. Kỹ năng tuân theo định dạng mở Agent Skills: một tệp SKILL.md chứa tên và mô tả, cùng các tập lệnh, mẫu hoặc tệp tham khảo mà mô hình có thể cần. Mô hình chỉ đọc hướng dẫn khi yêu cầu của bạn phù hợp với kỹ năng, nên kỹ năng đã cài đặt không phát sinh chi phí cho đến khi được sử dụng.

Kỹ năng AI khác gì với plugin hoặc máy chủ MCP?

Plugin hoặc máy chủ MCP cung cấp cho mô hình các công cụ mới để gọi — mã được chạy ở đâu đó và trả về kết quả. Còn kỹ năng AI cung cấp kiến thức và quy trình: cách tiếp cận tác vụ, các bước cần thực hiện và tiêu chí của đầu ra tốt. Kỹ năng chỉ là nội dung Markdown, nên không cần máy chủ, khóa API hay môi trường thực thi và hoạt động với mọi mô hình.

Xem tất cả

Tạo ngay không gian làm việc AI riêng

Đăng ký bản tin của chúng tôi để nhận thông báo về tính năng mới và các đợt tặng quà sắp tới 👇