Beginning Metal - Part 1: Introduction | Ray Wenderlich

Find out what’s covered in our Beginning Metal video tutorial series.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3537-beginning-metal/lessons/1

Hi, I have a question.
Do you or raywenderlich.com have plan to lecture intermediate or advanced level metal programming?

@klarheit - I personally don’t know of any plans for advanced metal videos at this time. But it may change depending on people’s interest.

@Caroline Thanks a lot for your comment, and I hope people get a lot of attention to advanced or intermediate course.

1 Like

Hello, I love this class so much. I hope have something about metal use on ARkit, that will be great.

By the way, I will crash in the last demo just download. It’s Xcode9 beta6 and iOS11, I looking for why.If I get something will call back. Thank’s so mush for this class.

ps. sorry for poor English.

@xernaga - Thank you :blush:

Unfortunately because Swift and Metal are new, they change all the time.

When you say the last demo, I assume you mean the final challenge project in video 14?

These are the changes that you will need to do to make the final challenge project work in Xcode 9 and High Sierra and iOS 11. Download the Materials from video 14 and open up the project in the challenge folder.

  1. On the Issue Navigator click “Conversion to Swift 4 is available”, and click Next, Next, OK, Save

  2. On the Issue Navigator click “Update to recommended settings”, and click Perform Changes

  3. On the Project Navigator, click the top item RayBreak and choose the target Raybreak. Enter your Team to set the Provisioning Profile and Signing Certificate.

  4. In Model.swift in loadModel(device:modelName:)

Change:

    do {
      meshes = try MTKMesh.newMeshes(from: asset,
                                     device: device,
                                     sourceMeshes: nil)
    } 

To:

    do {
      var metalKitMeshes: [MTKMesh]
      (_, metalKitMeshes) = try MTKMesh.newMeshes(asset: asset,
                                     device: device)
      meshes = metalKitMeshes
    } 
  1. In Texturable.swift in setTexture(device:imageName:) change
     let textureLoaderOptions: [String: NSObject]
     if #available(iOS 10.0, *) {
       let origin = NSString(string: NSString(MTKTextureLoader.Origin.bottomLeft.rawValue))
       textureLoaderOptions = [MTKTextureLoader.Option.origin : origin]
     } 

To:

    let textureLoaderOptions: [MTKTextureLoader.Option : Any]
    if #available(iOS 10.0, *) {
      textureLoaderOptions = [.origin : MTKTextureLoader.Origin.bottomLeft, .SRGB : false]
    } 
  1. In that same method, also change:
      do {
        texture = try textureLoader.newTexture(withContentsOf: textureURL,
                                               options: textureLoaderOptions)
      } 

To:

      do {
        texture = try textureLoader.newTexture(URL: textureURL, options: textureLoaderOptions)
      } 
  1. In ViewController.swift, in viewDidLoad() right right below metalView.clearColor = Colors.wenderlichGreen, add:
    metalView.depthStencilPixelFormat = .depth32Float
  1. In Renderable.swift, below pipelineDescriptor.vertexDescriptor = vertexDescriptor,
    add:
    pipelineDescriptor.depthAttachmentPixelFormat = .depth32Float

If you make all these changes, the final project should run. As you progress through the course, you should make these changes at the appropriate place.

I hope this helps :smiley:

@caroline Thank you so much for your help.

It’s my honor to meet you, this is very helpful.
I still have a question. How can Metal help us, in develop?
Can we use metal to make our project, more pretty? or more speed? or more power saving?

It’s very happy for your help.

@xernaga - Nice to meet you too :smiley:

Metal is low-level code. This means that you can directly access the GPU and perform parallel programming.

Frameworks such as Core Animation, SpriteKit and SceneKit use Metal under the hood. If you can do what you want to do using those frameworks, then you should stay with them.

Eventually you may find that they do not perform fast enough. Coding directly in Metal gives you more control over where GPU effort is spent.

So, for example, you may want to create a particle system of some sort. You could do this very easily using CAEmitterLayer. However, you might want more particle processing than CAEmitterLayer can give you. I have to admit I have not made comparisons but using Metal directly, you should be able to harness the GPU for your particles more efficiently. (But with a lot more work on your part!)

If you want to write a game, use SpriteKit for 2D games, SceneKit for 3D games or Unity or Unreal Engine for cross-platform games. These frameworks are built on top of Metal specifically for ease of use and it would be inefficient of your time to create games in Metal.

However, if you wanted to do something that Unity doesn’t, or write new effect shaders, you can do this in Metal.

Machine Learning using parallel processing is a hot topic. Using Metal you can create kernel functions on the GPU that perform in parallel on the CPU.

By learning Metal, you’ll have a deeper understanding of computer architecture, and a deeper understanding of the underlying principles of graphic rendering.

Because the GPU is so good at parallel processing (it has many thousands of processors instead of just a handful like the CPU), it is good at 3D rendering. It can take many vertices of 3D models and position them all at the same time in 3D space.

So to sum up. Yes, more pretty (more effects), more speed. Power saving? Probably not. Although you would have more control over where the power is used.