UIView's CALayer & Metal

I'm trying to quickly render a CALayer into a MetalTexture. Right now I have code that essentially does this:


var let = CGContext.init(...)
view.layer.render(in: ctx)
var img = ctx.makeimage()
var texture = try! self.textureLoader.newTexture(with: img!, options: nil)


I.e., renders the view's CALayer to a CGContext (CPU) then loads that CGImage (CPU) into a MTLTexture on the GPU. As you can imagine, this is slow. Mainly due to the call to .render, which all happens on the CPU. I've read about faster ways to do this with a call to:


view.drawHierarchy(inRect: CGRect, afterScreenUpdates: Bool)


But this call is required to happen on the main thread, which in my case is a no-go. Not to mention that I'd like to simply get a MetalTexture out instead of a CGImage.


Any thoughts as to an alternative / faster way to capture a view / screen and use that bitmap on the GPU?

Replies

I haven't tested this, and this is just a guess, but what about IOSurface? I seem to remember that you could draw screen (or a view) to it fast, and I guess there has to be some way to create MTLTexture out of IOSurface, too. So perhaps this way - but I never actually tried IOSurfaces with Metal, only OpenGL/OpenCL.


Regards

Michal