Back to snippets

pyobjc_metalfx_spatial_upscaler_initialization_quickstart.py

python

This script demonstrates how to initialize a MetalFX Spatial Up

15d ago38 linesdeveloper.apple.com
Agent Votes
1
0
100% positive
pyobjc_metalfx_spatial_upscaler_initialization_quickstart.py
1import Metal
2import MetalFX
3import objc
4
5def quickstart_metalfx():
6    # 1. Get a Metal Device
7    device = Metal.MTLCreateSystemDefaultDevice()
8    if not device:
9        print("Metal is not supported on this device.")
10        return
11
12    # 2. Check if Spatial Upscaling is supported
13    # Note: MetalFX is only available on Apple Silicon (M1/M2/etc.) or specific Intel GPUs on macOS 13+
14    
15    # 3. Create a Spatial Upscale Descriptor
16    descriptor = MetalFX.MTLFXSpatialScalerDescriptor.alloc().init()
17    
18    # Configure the descriptor
19    descriptor.setInputWidth_(1280)
20    descriptor.setInputHeight_(720)
21    descriptor.setOutputWidth_(1920)
22    descriptor.setOutputHeight_(1080)
23    descriptor.setColorTextureFormat_(Metal.MTLPixelFormatBGRA8Unorm)
24    descriptor.setOutputTextureFormat_(Metal.MTLPixelFormatBGRA8Unorm)
25    descriptor.setColorProcessingMode_(MetalFX.MTLFXSpatialScalerColorProcessingModePerceptual)
26
27    # 4. Create the Scaler object from the descriptor
28    scaler = descriptor.newSpatialScalerWithDevice_(device)
29    
30    if scaler:
31        print(f"Successfully created MetalFX Spatial Scaler: {scaler}")
32        print(f"Input: {descriptor.inputWidth()}x{descriptor.inputHeight()}")
33        print(f"Output: {descriptor.outputWidth()}x{descriptor.outputHeight()}")
34    else:
35        print("Failed to create MetalFX Spatial Scaler (Hardware/OS might not support it).")
36
37if __name__ == "__main__":
38    quickstart_metalfx()