How can HDR be added to Metal?

Asked on 07/30/2024

1 search

To add HDR to Metal, you can follow these steps as outlined in the session "Use HDR for dynamic image experiences in your app" from WWDC 2024:

  1. Set Up MTKView for Extended Range Content:

    • First, configure your MTKView to support extended range content. This is essential for displaying HDR images correctly.
  2. Apply Filters to Create an Edited CIImage:

    • Use Core Image filters to process your HDR image. This might involve applying a tonemap headroom filter if you are working with adaptive HDR images.
  3. Display the Result in MTKView:

    • Finally, render the processed CIImage in your MTKView.

Here is a brief code example to illustrate these steps:

// Set up MTKView for extended range content
mtkView.colorPixelFormat = .bgra10_xr_srgb

// Create a CIImage from your HDR source
let ciImage = CIImage(contentsOf: hdrImageURL, options: [.expandHDR: true])

// Apply necessary filters (e.g., tonemap headroom filter)
let tonemapFilter = CIFilter(name: "CITonemapHeadroom")
tonemapFilter?.setValue(ciImage, forKey: kCIInputImageKey)
let outputImage = tonemapFilter?.outputImage

// Render the CIImage in MTKView
let context = CIContext(mtlDevice: mtkView.device!)
context.render(outputImage!, to: mtkView.currentDrawable!.texture, commandBuffer: nil, bounds: outputImage!.extent, colorSpace: CGColorSpaceCreateDeviceRGB())

For more detailed information, you can refer to the chapter on Display tone mapping in the session "Use HDR for dynamic image experiences in your app".

Session Chapter Markers

  • Introduction
  • HDR concepts and technologies
  • Adaptive HDR
  • Read HDR images
  • Edit strategies
  • Display tone mapping
  • Saving images