Skip to content

Commit 019422b

Browse files
Add Location Component chapter (#292)
* Add Location Component chapter * Apply tobias feedback * Apply Alan feedback
1 parent e86cd1a commit 019422b

11 files changed

+192
-0
lines changed

README.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ The Vulkan Guide can be built as a single page using `asciidoctor guide.adoc`
180180

181181
// include::{chapters}descriptor_dynamic_offset.adoc[]
182182

183+
=== xref:{chapters}location_component_interface.adoc[Location and Component Interface]
184+
185+
// include::{chapters}location_component_interface.adoc[]
186+
183187
=== xref:{chapters}push_constants.adoc[Push Constants]
184188

185189
// include::{chapters}push_constants.adoc[]

chapters/images/location_example_16bit.svg

Lines changed: 4 additions & 0 deletions
Loading

chapters/images/location_example_64bit.svg

Lines changed: 4 additions & 0 deletions
Loading

chapters/images/location_example_array.svg

Lines changed: 4 additions & 0 deletions
Loading

chapters/images/location_example_array2.svg

Lines changed: 4 additions & 0 deletions
Loading

chapters/images/location_example_basic.svg

Lines changed: 4 additions & 0 deletions
Loading

chapters/images/location_example_boundaries.svg

Lines changed: 4 additions & 0 deletions
Loading

chapters/images/location_example_matrix.svg

Lines changed: 4 additions & 0 deletions
Loading
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
// Copyright 205 The Khronos Group, Inc.
2+
// SPDX-License-Identifier: CC-BY-4.0
3+
4+
ifndef::chapters[:chapters:]
5+
ifndef::images[:images: images/]
6+
7+
[[location-and-component-interface]]
8+
= Location and Component Interface
9+
10+
This chapter is an overview of the link:https://docs.vulkan.org/spec/latest/chapters/interfaces.html#interfaces-iointerfaces-locations[Location and Component Assignment] chapter to help give examples, especially around some of the more extreme edge cases.
11+
12+
The simplest way to think about a `Location` is that it is made up of four 32-bit `Component`s.
13+
This means a `vec4`/`float4`/`uvec4`/etc will fit perfectly in a single `Location`.
14+
Multiple variables can be packed into the same `Location` if their `Component`s do not overlap.
15+
16+
Locations are used for both the `Input` and `Output` to interface between shaders stages when possible.
17+
18+
== Advice for general users
19+
20+
For most people, this chapter is much deeper into edge cases compared who developers generally use the `Location` interface. For those developers, the simple advise to take away is:
21+
22+
1. Use less `Location` if possible.
23+
2. If you need many `Location`s, make sure you are link:https://docs.vulkan.org/spec/latest/chapters/interfaces.html#interfaces-iointerfaces-limits[under the limits].
24+
25+
== Basic Example
26+
27+
Here is a basic example:
28+
29+
[source,glsl]
30+
----
31+
layout(location=0) in vec4 a;
32+
layout(location=1, component = 0) in vec2 b;
33+
layout(location=1, component = 2) in float c;
34+
----
35+
36+
image::{images}location_example_basic.svg[location_example_basic]
37+
38+
== 16-bit
39+
40+
16-bit values always consume a full 32-bit `Component`. So a vector with 16-bit elements will consume the same resources as a vector with 32-bit elements; they are not tightly packed.
41+
42+
[source,glsl]
43+
----
44+
layout(location=0) in f16vec3 a;
45+
----
46+
47+
image::{images}location_example_16bit.svg[location_example_16bit]
48+
49+
== Crossing Location Boundaries
50+
51+
All 16-bit and 32-bit vectors must be inside a single `Location`, so the following is **not** allowed.
52+
The last two elements would consume `component = 4` and `component = 5`, which do not exist.
53+
54+
[source,glsl]
55+
----
56+
layout(location=0, component = 2) in vec4 a;
57+
----
58+
59+
image::{images}location_example_boundaries.svg[location_example_boundaries]
60+
61+
== 64-bit
62+
63+
64-bit are special as they can consume up to 2 `Location`, but they must only start at `Component` `0` or `2`.
64+
65+
[source,glsl]
66+
----
67+
layout(location=0) in f64vec3 a;
68+
----
69+
70+
image::{images}location_example_64bit.svg[location_example_64bit]
71+
72+
== Interleaving Components
73+
74+
The following attempt to have multiple variables alias the same component is link:https://godbolt.org/z/h61baYhT4[not allowed].
75+
76+
[source,glsl]
77+
----
78+
layout(location=0) in vec2 in_a; // Components 0 and 1
79+
layout(location=0, component=1) in float in_f; // Invalid: overlaps component 1
80+
----
81+
82+
The following modification would make it legal as multiple variable can share a `Location`, just not a `Component
83+
`
84+
[source,patch]
85+
----
86+
layout(location=0) in vec2 in_a;
87+
// Change in_f to use component 2 instead
88+
- layout(location=0, component=1) in float in_f;
89+
+ layout(location=0, component=2) in float in_f;
90+
----
91+
92+
== Array
93+
94+
An element of an array will consume all every `Component` in a `Location` that it would consume as a non-arrayed value, with each subsequent element consuming the next available `Location`.
95+
96+
For example:
97+
98+
[source,glsl]
99+
----
100+
layout(location=0) in float a[3];
101+
----
102+
103+
image::{images}location_example_array.svg[location_example_array]
104+
105+
As seen, using a scalar or something such as a `vec2`/`float2` will leave many `Component` slots unused.
106+
107+
It is **not** allowed to use any other `Component` in a `Location` that is being consumed by an array
108+
109+
[source,glsl]
110+
----
111+
layout(location=0) in float a[3];
112+
layout(location=2, component=2) in float b;
113+
----
114+
115+
`float b` is invalid because the array consumes all of `Location` 2.
116+
117+
image::{images}location_example_array2.svg[location_example_array2]
118+
119+
[NOTE]
120+
====
121+
Some shader stages, like geometry shaders, have an array around its interface matching, this array is disregarded for the above examples.
122+
====
123+
124+
== Matrix
125+
126+
A matrix is viewed as an array, which consume all 4 components.
127+
128+
So something like
129+
130+
[source,glsl]
131+
----
132+
layout(location = 0) in mat3x2 a;
133+
----
134+
135+
From a `Location`/`Component` point-of-view looks like
136+
137+
[source,glsl]
138+
----
139+
// N == 3
140+
// Arrays consume whole Location
141+
layout(location = 0) in vec2 a[3];
142+
----
143+
144+
As stated above, arrays consume the whole `Location` so the following is **not** allowed.
145+
146+
[source,glsl]
147+
----
148+
layout(location = 0) in mat3x2 a;
149+
layout(location = 2, component = 2) in float b;
150+
----
151+
152+
`float b` is invalid because the implicit array of the matrix consumes all of `Location` 2.
153+
154+
image::{images}location_example_matrix.svg[location_example_matrix]

chapters/vertex_input_data_processing.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ This chapter is an overview of the link:https://docs.vulkan.org/spec/latest/chap
1111

1212
It is also important to remember that Vulkan is a tool that can be used in different ways. The following are examples for educational purposes of how vertex data **can** be laid out.
1313

14+
For more information about `Location` and `Component`, see the xref:{chapters}location_component_interface.adoc[Location and Component Interface] chapter.
15+
1416
== Binding and Locations
1517

1618
A `binding` is tied to a position in the vertex buffer from which the vertex shader will start reading data out of during a `vkCmdDraw*` call. Changing the `bindings` does **not** require making any alterations to an app's vertex shader source code.

0 commit comments

Comments
 (0)