Overv / VulkanTutorial

Tutorial for the Vulkan graphics and compute API
https://vulkan-tutorial.com
Creative Commons Attribution Share Alike 4.0 International
3.18k stars 519 forks source link

Vertex shader input #103

Closed lewa-j closed 6 years ago

lewa-j commented 6 years ago

In the chapter "Vertex input description" you declared the vertex attribute as layout(location = 0) in vec2 inPosition; so to use it, you had to cast it to vec4, like this gl_Position = vec4(inPosition, 0.0, 1.0); Later in the chapter "Depth buffering" you had to change this to

layout(location = 0) in vec3 inPosition;
...
gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0);

All this does not make sense. Vulkan specification says

If the format does not include G, B, or A components, then those are filled with (0,0,1) as needed (using either 1.0f or integer 1 based on the format) for attributes that are not 64-bit data types. The number of components in the vertex shader input variable need not exactly match the number of components in the format. If the vertex shader has fewer components, the extra components are discarded.

Therefore, if you declare a vertex attribute, like this layout(location = 0) in vec4 inPosition; you do not need to cast it to vec4, whatever the format.

So this cast here just adds an extra operation. And forces us to recompile the shader when changing the format of the vertices.

Overv commented 6 years ago

Yes, it is possible to rely on the implicit values, but I think this only adds confusion. Just like some of the other implicit behavior in Vulkan that I have made explicit in the tutorial, I won't change this.