|
| 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] |
0 commit comments