Beginning Metal - Part 10: Instancing | Ray Wenderlich

Learn how you can using instancing to render crowd scenes or a field of grass using minimal resources.


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

Great tutorial series! Watching the last 3 lessons today.

One code issue in InstanceScene.swift in the video and the download materialsā€¦

In InstanceScene.swift, the outer ā€œfor _ in 0ā€¦<40ā€ loop is not needed after the ā€œfor human in humans.nodesā€ loop is added is it?

Hello Caroline. Awesome tutorial! I have a question regarding the uint instanceID. Is it possible to create an ā€œinstanceDescriptorā€ and utilize the [stage_in] qualifier as opposed to using the [instance_id] qualifier.

@twohyjr - Iā€™m not quite sure where your questionā€™s coming from.

The [[ stage_in ]] qualifier is per vertex.

When you use instancing, youā€™re using the same vertex over and over, but just different parameters for each instance.

Hi Caroline. Great series so far ā€“ thanks!

One problem. I tried following the challenge through but noticed that no grass was appearing (this is using Swift 4 and Xcode 9, beta 6). Tapping ā€˜Capture GPU Frameā€™ I noticed the following issue appears in the instance debug group:

The Compiler was not able to Preload your Buffer
(Issue) The compiler was not able to preload your buffer. Simplify your memory access pattern by using the constant address space, a struct, or a descriptor.

I double checked against the final project included in the chapter resources, only adding the two necessary lines to compile (pipelineDescriptor.depthAttachmentPixelFormat = .depth32Float and metalView.depthStencilPixelFormat = .depth32Float), and Iā€™m seeing the same result ā€“ although the models do display here.

Any ideas?

Missing grass and the issue above were in fact unrelated.

My grass was just underground. Clearly just needed some watering a.k.a. setting grass.position.z not grass.position.y!

1 Like

@cldr - so glad you were able to fix it! It can be very frustrating when the mesh just disappears.

1 Like