Beginning Metal - Part 8: Depth | Ray Wenderlich

In this beginning Metal video tutorial, you’ll learn how to add depth to scenes and render models in front of other models.

This is a companion discussion topic for the original entry at
1 Like

This tutorial doesn’t have subtitles.
Please provide if you have it.

@klarheit - Can you try it again please? It works for me. Maybe refresh the page and ensure that you’ve turned on subtitles.

I did try, but it didn’t work.
Tutorial 11 and 12 are the same.

Now, Tutorial 12 is fine. : ]

Plese check it once more.
Thank you.

@klarheit - I’m sorry you’re having this trouble.

Today I tried part 8 again and my subtitles were gone! However, parts 11 and 12 do have subtitles available today for me.

I’ve passed this on so hopefully we may be able to sort this out.

@caroline - Thank you for your answer.
I’ve checked subtitles of part 11 and 12.

Thanks again. :]

@caroline - Hi, I’ve look at this tutorial html code in safari brower.
.tts’s contents in the <track …> is very short.
“depth” tutorial is about 8 minutes, but .tss is about 2 minutes.


@klarheit and @caroline

I’m not sure what happened, but video 8 subtitles had been switched off. I’ve now turned them on so they should be working. Please let me know if you are still experiencing any trouble viewing video 8 subtitles.

Also, video 11 and 12 were added recently so it’s possible you tried viewing them the other day before they were added, but they are up and running now. Please let me know if you’re having any trouble seeing those, or any other subtitles.

@csweigart and @caroline
Thank you.
I’ve checked working. :blush:


Hey Caroline, another question here. Im going through the whole tutorial again, but this time I’m trying to make it work on my mac itself. Not much different here only working with an different storyboard ofcourse but from there almost everything is the same up until now. During this tutorial however I get a problem when adding the depthStencilState. Something weird is going on with the depthCompareFunction. I’ve made some screenshot and put them online here: When I use depthCompareFunction set to .less almost everything dissapears except for some artifacts of the cube. Trying setting it to .greater reveals most of the cube, with some artifacts missing. And displays the quad on top of the cube. It seems as these two images together create the whole cube again. But I’m not sure why this isn’t working as expected (showing the quad behind the whole cube). Any ideas? It’s definitely caused by the depthStencilState as when I comment out the line where we add the depthStencilState to the commandEncoder, everything works properly again (except for depthCompare of course). I’ve uploaded my code to Github if you’d like to have a look, no worries if you’re too busy ofcourse! Thanks anyway!


Found a solution to my problem! In the ViewController class we forgot to set the following. In the viewDidLoad() function, right below where we set the clearColor of the metalView. Add the line
metalView.depthStencilPixelFormat = .depth32Float.
We also need to set this in our buildPipelineState() function in Renderable.swift. Add this just below where we set the pixelFormat of the colorattachment.
pipelineDescriptor.depthAttachmentPixelFormat = .depth32Float
Now when we capture a GPU frame, we also get a depthAttachment from the device. It’s kind of weird that it did work on iPhone before, and not on macOS but this addition works on both versions.


@bababoega - thank you so much for posting this. I haven’t had a chance to look over your project, but I’m hoping to over the weekend and this certainly helps :grinning:

@bababoega - thank you for posting your solution.

I believe the following explains why depth behavior is working differently (between iOS and macOS); it also provides a watch out for future compatibility.

1 Like

I was doing find right up until the penultimate edit. After adding to Renderer’s draw(in) function:


Build and run resulted in:
validateDepthStencilState:3528: failed assertion 'MTLDepthStencilDescriptor sets depth test but MTLRenderPassDescriptor has a nil depthAttachment texture'

The apps break in Primitive:DoRender on

commandEncoder.drawIndexedPrimitives( type: .triangle, indexCount: indices.count, indexType: .uint16, indexBuffer: indexBuffer, indexBufferOffset: 0)

Any suggestions welcome as I’m not really sure how to begin debugging.

@lordandrei - I regard this as an error in the original video.

@novistream posted a link to explain why it worked differently on iOS before, and now they’ve fixed it on iOS 11:

@bababoega posted the fix above:

In viewDidLoad(), right below metalView.clearColor = Colors.wenderlichGreen, add:

metalView.depthStencilPixelFormat = .depth32Float

Also, in Renderable.swift, below pipelineDescriptor.vertexDescriptor = vertexDescriptor,

pipelineDescriptor.depthAttachmentPixelFormat = .depth32Float

Thanks for your help, guys :wave:

Hello everyone!
I’m currently have only iOS 11 device, so I use Xcode 9 beta 6 and Swift 4 to pass this tutorial.
Everything before this video was easy and nice, but now I have a very strange case I do not know how to fix.
When I added a cube to scene I tried to launch it to see the result, but I have the next assert:

validateIndexBuffer:131: failed assertion `indexBufferOffset(0) + (indexCount(36) * 2) must be <= [indexBuffer length](32).'

First of all, I really do not understand why this assert shows me that I have 36 * 2 indices instead of just 36. Secondly, I do not see such assert in the video and my internet research was not successful.
Would be thankful for any ideas.

Ignore my previous message. My mistake, I just created an index buffer with size of vertices.

Hello, there are some problem here
when I write code

it’s will this error, I think it’s iOS11 and Xcode Version 9.0 (9A235)

failed assertion `MTLDepthStencilDescriptor sets depth test but MTLRenderPassDescriptor has a nil depthAttachment texture’

thank’s for your help

@xernaga - check the post a few above yours: Beginning Metal - Part 8: Depth | Ray Wenderlich and see if it fixes your problem.

It’s very appreciate. It fix this error!
I so sorry don’t see so carefully.