# iOS Metal Tutorial with Swift Part 5: Switching to MetalKit

**URL:** <https://forums.kodeco.com/t/ios-metal-tutorial-with-swift-part-5-switching-to-metalkit/19283>\
**Category:** 🎓 Courses & Articles\
**Created:** [September 2, 2016, 4:00pm UTC](https://forums.kodeco.com/t/ios-metal-tutorial-with-swift-part-5-switching-to-metalkit/19283 "2016-09-02T16:00:59Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![haawa](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/haawa/32/97362_2.png) [@haawa](https://forums.kodeco.com/u/haawa)\
**Post date:** [September 2, 2016, 4:00pm UTC](https://forums.kodeco.com/t/ios-metal-tutorial-with-swift-part-5-switching-to-metalkit/19283/1 "2016-09-02T16:00:59Z")

</div>

Learn how to use MetalKit in this 5th part of our Metal tutorial series.

* * *
 This is a companion discussion topic for the original entry at [https://www.raywenderlich.com/976-ios-metal-tutorial-with-swift-part-5-switching-to-metalkit](https://www.raywenderlich.com/976-ios-metal-tutorial-with-swift-part-5-switching-to-metalkit)

---

<div class="post-metadata">

**Author:** ![warrenm](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/warrenm/32/88014_2.png) [@warrenm](https://forums.kodeco.com/u/warrenm)\
**Post date:** [September 2, 2016, 5:59pm UTC](https://forums.kodeco.com/t/ios-metal-tutorial-with-swift-part-5-switching-to-metalkit/19283/2 "2016-09-02T17:59:14Z")

</div>

Nice tutorial! One small note: OpenGL uses different clip-space coordinates than Metal (in GL, z goes from -1 to 1, while in Metal z goes from 0 to 1), so using `GLKMatrix4MakePerspective` doesn’t give you a matrix that properly transforms from eye space to clip space. Instead, it transforms half of the viewing volume behind the eye, causing sometimes-subtle clipping and culling issues. You can fix up the matrix you get back from GLK by setting the matrix elements that are relevant to depth by adding the following code to `makePerspectiveViewAngle`:

```
let zs = farZ / (nearZ - farZ)
q[2][2] = zs
q[3][2] = zs * nearZ

```

---

<div class="post-metadata">

**Author:** ![haawa](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/haawa/32/97362_2.png) [@haawa](https://forums.kodeco.com/u/haawa)\
**Post date:** [September 5, 2016, 5:45am UTC](https://forums.kodeco.com/t/ios-metal-tutorial-with-swift-part-5-switching-to-metalkit/19283/3 "2016-09-05T05:45:14Z")

</div>

Thanks for the note! Will use it in the next tutorial.

---

<div class="post-metadata">

**Author:** ![lemonhead94](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/lemonhead94/32/41107_2.png) [@lemonhead94](https://forums.kodeco.com/u/lemonhead94)\
**Post date:** [September 11, 2016, 12:16pm UTC](https://forums.kodeco.com/t/ios-metal-tutorial-with-swift-part-5-switching-to-metalkit/19283/4 "2016-09-11T12:16:53Z")

</div>

Thanks for your detailed Tutorial this far. 😃

I got one issue with **MetalKit Texture Loading** , when replacing:

> ```
> let texture = MetalTexture(resourceName: "cube", ext: "png", mipmaped: true)
> texture.loadTexture(device: device, commandQ: commandQ, flip: true) 
> 
> ```

With:

> let path = Bundle.main.path(forResource: “cube”, ofType: “png”)!  
> let data = NSData(contentsOfFile: path) as! Data  
> let texture = try! textureLoader.newTexture(with: data, options: [MTKTextureLoaderOptionSRGB : (false as NSNumber)])

_The Images are now mirrored and only Bottom and Front are rendered…_

---

<div class="post-metadata">

**Author:** ![bajocode](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bajocode/32/93348_2.png) [@bajocode](https://forums.kodeco.com/u/bajocode)\
**Post date:** [September 12, 2016, 9:19am UTC](https://forums.kodeco.com/t/ios-metal-tutorial-with-swift-part-5-switching-to-metalkit/19283/5 "2016-09-12T09:19:30Z")

</div>

Hmm, same issue here…

---

<div class="post-metadata">

**Author:** ![bajocode](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bajocode/32/93348_2.png) [@bajocode](https://forums.kodeco.com/u/bajocode)\
**Post date:** [September 12, 2016, 9:27am UTC](https://forums.kodeco.com/t/ios-metal-tutorial-with-swift-part-5-switching-to-metalkit/19283/6 "2016-09-12T09:27:58Z")

</div>

I tried adding this to the options dictionary for the texture loading: “MTKTextureLoaderOptionOrigin : MTKTextureLoaderOriginFlippedVertically as NSObject” But this option is only available in iOS10 so i couldn’t test it

---

<div class="post-metadata">

**Author:** ![haawa](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/haawa/32/97362_2.png) [@haawa](https://forums.kodeco.com/u/haawa)\
**Post date:** [September 12, 2016, 9:38am UTC](https://forums.kodeco.com/t/ios-metal-tutorial-with-swift-part-5-switching-to-metalkit/19283/7 "2016-09-12T09:38:52Z")

</div>

It seems that there is a difference in texture loading for different iOS versions.  
You should use TextureLoader option as @bajocode wrote. If it’s not available and you have this issue, then there are two options:

1. Flip the image file.
2. Flip image as a CGImage and use it

---

<div class="post-metadata">

**Author:** ![bajocode](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bajocode/32/93348_2.png) [@bajocode](https://forums.kodeco.com/u/bajocode)\
**Post date:** [September 12, 2016, 10:07am UTC](https://forums.kodeco.com/t/ios-metal-tutorial-with-swift-part-5-switching-to-metalkit/19283/8 "2016-09-12T10:07:39Z")

</div>

Flipping the image itself works indeed, thanks 😉

---

<div class="post-metadata">

**Author:** ![rcepeda](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/rcepeda/32/41238_2.png) [@rcepeda](https://forums.kodeco.com/u/rcepeda)\
**Post date:** [September 12, 2016, 10:37pm UTC](https://forums.kodeco.com/t/ios-metal-tutorial-with-swift-part-5-switching-to-metalkit/19283/9 "2016-09-12T22:37:28Z")

</div>

Thanks for the tutorial Andriy, this does a good job at highlighting the benefits of MetalKit 🙂

As a quick edit, you are instructing developers to remove this code twice:

> metalLayer = CAMetalLayer()  
> metalLayer.device = device  
> metalLayer.pixelFormat = .bgra8Unorm  
> metalLayer.framebufferOnly = true  
> view.layer.addSublayer(metalLayer)

This is first removed in “Removing Redundant Code From MetalViewController.swift”, but then again in “Adding the MTKViewDelegate Protocol”.

FYI, the starter project now needs to be updated for Xcode 8 GM. This mainly involves a bunch of quick changes from “newXYZ” to “makeXYZ”.

Thanks again!

---

<div class="post-metadata">

**Author:** ![bababoega](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bababoega/32/108667_2.png) [@bababoega](https://forums.kodeco.com/u/bababoega)\
**Post date:** [October 18, 2016, 8:32pm UTC](https://forums.kodeco.com/t/ios-metal-tutorial-with-swift-part-5-switching-to-metalkit/19283/11 "2016-10-18T20:32:14Z")

</div>

Hi! Just a question out of curiosity here. How would I go around making the final result of the tutorial work, without using a storyboard? Normally I would go and do the following in AppDelegate.swift  
in `application(didFinishLaunchingWithOptions)`  
I would make a window and add an instance of MySceneViewController to it

`window = UIWindow(frame: UIScreen.main.bounds) let sceneViewController = MySceneViewController() window!.rootViewController = sceneViewController window!.makeKeyAndVisible()`

But in this case I wouldn’t know what to do with the IBOutlet in MetalViewController. And the view created in MySceneViewController won’t be an MTKView but a normal UIView.

Any thoughts?

---

<div class="post-metadata">

**Author:** ![haawa](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/haawa/32/97362_2.png) [@haawa](https://forums.kodeco.com/u/haawa)\
**Post date:** [October 19, 2016, 8:15am UTC](https://forums.kodeco.com/t/ios-metal-tutorial-with-swift-part-5-switching-to-metalkit/19283/12 "2016-10-19T08:15:27Z")

</div>

I think you need to add MTKView as a subview of your view controller. And then set it up.

---

<div class="post-metadata">

**Author:** ![tonylattke](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/tonylattke/32/52392_2.png) [@tonylattke](https://forums.kodeco.com/u/tonylattke)\
**Post date:** [December 23, 2016, 12:23am UTC](https://forums.kodeco.com/t/ios-metal-tutorial-with-swift-part-5-switching-to-metalkit/19283/13 "2016-12-23T00:23:45Z")

</div>

Hi! Amazing tutorial 🙂  
But I ask me, on the last tutorial you have the updateLogic function called in the newFrame function. How can I update the logic now? Because I want to add a FPS counter, but I don’t find the right place in the code to do it ☹

---

<div class="post-metadata">

**Author:** ![haawa](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/haawa/32/97362_2.png) [@haawa](https://forums.kodeco.com/u/haawa)\
**Post date:** [December 26, 2016, 9:50am UTC](https://forums.kodeco.com/t/ios-metal-tutorial-with-swift-part-5-switching-to-metalkit/19283/14 "2016-12-26T09:50:15Z")

</div>

Thank you. Sorry for delayed reply.  
In the **MetalViewControllerDelegate** there are two methods:

func updateLogic(\_ timeSinceLastUpdate:CFTimeInterval)  
func renderObjects(\_ drawable:CAMetalDrawable)

You can update logic in updateLogic method in **MySceneViewController** , after or before you animate the object.

---

<div class="post-metadata">

**Author:** ![roxiga](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/roxiga/32/68906_2.png) [@roxiga](https://forums.kodeco.com/u/roxiga)\
**Post date:** [June 2, 2017, 10:37am UTC](https://forums.kodeco.com/t/ios-metal-tutorial-with-swift-part-5-switching-to-metalkit/19283/15 "2017-06-02T10:37:54Z")

</div>

Tell me how to create many instances of Cube.

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [November 15, 2017, 9:54pm UTC](https://forums.kodeco.com/t/ios-metal-tutorial-with-swift-part-5-switching-to-metalkit/19283/16 "2017-11-15T21:54:19Z")

</div>

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [November 15, 2017, 9:54pm UTC](https://forums.kodeco.com/t/ios-metal-tutorial-with-swift-part-5-switching-to-metalkit/19283/17 "2017-11-15T21:54:21Z")

</div>


