Beginning Metal - Part 3: Triangles | Ray Wenderlich

In this video tutorial, you'll discover why triangles are important and how to draw them using Metal.


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

Hey there!
Just a question. At the end of the tutorial when you call
commandEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: vertices.count)
doesn’t vertices.count return the number 9 in this case? But we only have 3 vertices right? Or does the function divide this number by 3 itself?

Thanks for the help and great tutorials!

1 Like

@bababoega - you are correct :smiley: - there are 9 in the array, but only 3 vertices to be drawn.

If you change vertices.count to 3, the triangle still draws correctly.

I’m actually not sure what the GPU is doing with the other 6.

Hi Caroline,

First of all thanks for these great tutorials :slight_smile:

I have one question though. In the fragment shader function, we have used RGBA scheme. Then why do we use .bgra8Unorm in the following line?
pipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm

Thanks again,
Suvrat :slight_smile:

It’s a great question, and I hadn’t really thought about it in depth before. bgra8Unorm is a 32 bit colour format with each component having 8 bits. So 255,255,255,1 will be fully white.

My research leads me to this paper from nVidia: http://http.download.nvidia.com/developer/Papers/2005/Fast_Texture_Transfers/Fast_Texture_Transfers.pdf

Page 5 says that the GPU prefers the BGRA format for textures. This may be for historical reasons - .tga files are in bgra format.

In addition, I’d like to clarify that the return value from the fragment function is completely distinct from the data in the Metal buffer.

Thank you so much for the quick reply! :slight_smile:
I’ll go through the paper. I don’t know if I’ll understand it completely :stuck_out_tongue:

Thanks again for these tutorials, they are awesome!

BGRA8UNORM is the default pixel format of the framebuffer, therefore it must match with the one you supply when creating the MTLRenderPipelineState.

the fast forwarding of code typing in these videos is asinine, forcing me to pause and replay the video dozens of times. what made you think this was a good idea?