# Chapter 5 : Meaning of stage\_in

**URL:** <https://forums.kodeco.com/t/chapter-5-meaning-of-stage-in/105161>\
**Category:** Metal by Tutorials\
**Created:** [April 13, 2020, 12:24pm UTC](https://forums.kodeco.com/t/chapter-5-meaning-of-stage-in/105161 "2020-04-13T12:24:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![behr](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/behr/32/81494_2.png) [@behr](https://forums.kodeco.com/u/behr)\
**Post date:** [April 13, 2020, 12:24pm UTC](https://forums.kodeco.com/t/chapter-5-meaning-of-stage-in/105161/1 "2020-04-13T12:24:28Z")

</div>

Hello

I hope you are have spending a nice Monday.  
I have a question that is maybe too early in my learning curve of Metal but …

in « fragment float4 fragment\_main(VertexOut in [[stage\_in]]) { return float4(in.normal, 1); } »  
in Shaders.metal

So far (chapter5), I do not really understand what does stage\_in mean. Can you provide a short explanation and point out what to read to understand better what it means ?

Regards

Behr

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [April 14, 2020, 2:36am UTC](https://forums.kodeco.com/t/chapter-5-meaning-of-stage-in/105161/2 "2020-04-14T02:36:12Z")

</div>

`[[stage_in]]` is an attribute that you can use with one vertex or fragment shader argument.

With the vertex shader, when you use the `stage_in` attribute, you match the shader struct with the pipeline’s `vertexDescriptor`. So you might have a vertex descriptor that describes

Position - float3  
Normal - float 3

and then in the shader you define a struct with

position: float4  
normal: float3

Using the `stage_in` attribute, the shader can look at the pipeline’s vertex descriptor and see what format the input buffer is in, and then match it with the struct as declared in the argument. Notice here that the position doesn’t match between vertexDescriptor and shader struct, but that doesn’t matter because `stage_in` will make it match.

You don’t have to use vertexDescriptors or `stage_in`. As long as your buffer matches what the shader struct expects, then that will work too.

Warren Moore’s answer on this question is great:

> <https://stackoverflow.com/questions/48455501/connection-between-stage-in-mtlvertexdescriptor-and-mtkmesh>

Also, become familiar with the Metal Shading Language specification. It’s really daunting at first, but as you use it more, it does start to make more sense:

> **[Metal-Shading-Language-Specification.pdf](https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf)**
>
> 10.01 MB

---

<div class="post-metadata">

**Author:** ![behr](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/behr/32/81494_2.png) [@behr](https://forums.kodeco.com/u/behr)\
**Post date:** [April 14, 2020, 2:07pm UTC](https://forums.kodeco.com/t/chapter-5-meaning-of-stage-in/105161/3 "2020-04-14T14:07:19Z")

</div>

Hello Caroline,

First, thank you for your answer. Very pedagogical you are.

For ‘stage\_in’ … I understand it now!

This made me realize first that stage\_in is defined (from the swift point of view) in VertexDescriptor.swift, which was not clear for me at this point.

Your answer made me read more (MBT book) and more especially back to chapter 2, were VertexDescriptors are well, described. But after this reading, I try to figure out why float3 are / can be translated into float4 in the shader…

It seems that it is possible because: the float3 contains less data than destination float4 and so far I can only speculate that the w value is set to 1 or 0 … by the Gremlins inside the computer…

I found a lot of interesting topics in Metal Shading Language Specification.pdf, but not this answer … so far.

Can I put a next question?  
VertexDescriptors have an array of 31 attributes: which mean if I understand well that we can set 31 differents type of data description in the struct of the shader. When I call such a line " renderEncoder.setVertexBytes " do these bytes uses the same 31 quota for type of data?

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [April 14, 2020, 11:25pm UTC](https://forums.kodeco.com/t/chapter-5-meaning-of-stage-in/105161/4 "2020-04-14T23:25:06Z")

</div>

You define the vertex descriptor on the Swift side, a struct on the GPU side, and the `[[stage_in]]` attribute links the two. This linking means that when the vertex buffer described by the vertex descriptor is transferred from RAM to video RAM, the “gremlins” can automatically make that `float3` to `float4` conversion.

You don’t have to use vertex descriptors. If you create an `MTLBuffer` without one, you make sure that the struct in your shaders file exactly matches the layout of the `MTLBuffer`. The vertex descriptor is a convenience.

A vertex descriptor describes how one or more vertex buffers are laid out. However, as I just said, not all vertex buffers use vertex descriptors. For example `uniforms` go into an `MTLBuffer`. That buffer is described by the Uniforms struct in `Common.h`, which both Swift and the shaders use.

`setVertexBytes` is a convenience which automatically creates an `MTLBuffer` so you don’t have to.

With that explanation, I hope that you can see that the 31 attributes for the vertex descriptor are plenty. In chapter 8, you’ll use a few more for joint animation.

I hope you are not getting confused with the 31 slots in the GPU buffer argument table. When you set the index of the vertex buffer on the render command encoder, you are setting the slot into the buffer argument table, and that has nothing at all to do with vertex descriptors.
