iOS Development

ios – CIContext colorspace for MTKView

Spread the love


I’ve the next two contexts:

 personal lazy var firstContext: CIContext  = {
    let choices:[CIContextOption:Any] = [CIContextOption.cacheIntermediates: false];
    
    return CIContext(mtlDevice: self.machine!, choices: choices)
}()

 personal lazy var secondContext: CIContext  = {
    let choices:[CIContextOption:Any] = [CIContextOption.cacheIntermediates: false, CIContextOption.outputColorSpace: NSNull(), CIContextOption.workingColorSpace: NSNull()];
    
    return CIContext(mtlDevice: self.machine!, choices: choices)
}()

 public var useFirstContext = true

personal var context: CIContext {

    return useFirstContext ? firstContext : secondContext
}

And my MTKView is configured as follows:

    metalLayer = self.layer as? CAMetalLayer
    
    metalLayer?.wantsExtendedDynamicRangeContent = true
    metalLayer.colorspace = CGColorSpace(title: CGColorSpace.itur_2100_HLG)
    
    colorPixelFormat = .bgr10a2Unorm

Once I render incoming pixel buffers utilizing ciContext, I see completely different outputs on MTKView with the 2 contexts. There isn’t any filter chain configured, apart from rescaling and remodeling CIImage. Why is the output completely different with the 2 contexts when incoming buffers are in the identical coloration house? Right here is the drawing code:

 func drawCIImage(_ ciImage:CIImage?) {
    guard let picture = ciImage,
        let currentDrawable = currentDrawable,
        let commandBuffer = commandQueue?.makeCommandBuffer()
        else {
            return
    }

    let drawableSize = self.drawableSize
    
    let scaleX = drawableSize.width / picture.extent.width
    let scaleY = drawableSize.top / picture.extent.top
    let scaledImage = picture.remodeled(by: CGAffineTransform(scaleX: scaleX, y: scaleY))
    let filteredImage = scaledImage
    
   
    let vacation spot = CIRenderDestination(width: Int(drawableSize.width),
                                          top: Int(drawableSize.top),
                                          pixelFormat: self.colorPixelFormat,
                                          commandBuffer: commandBuffer,
                                          mtlTextureProvider: { () -> MTLTexture in
                                             return currentDrawable.texture
    })
    
    _ = strive? context.startTask(toRender: filteredImage, to: vacation spot)
    
    commandBuffer.current(currentDrawable)
    commandBuffer.commit()
}

Leave a Reply

Your email address will not be published. Required fields are marked *