• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Memory Layout Examples
2
3Note, this is how we'll layout the data in Amber. Some of these types don't
4exist in standard GLSL (they exist in GL_EXT_shader_explicit_arithmetic_types).
5We allow them in Amber to give flexibility in the data provided, but attempt to
6follow the layout rules with all types for consistency.
7
8For the purposes of this document, all scalars are stored in little-endian. The
9lower number components of a vector appear earlier than higher numbered ones.
10Similar for columns and rows of matrices.
11
12All type names used in the file are the Amber names. They differ from GLSL but
13are a bit more explicit, so are used here for clarity.
14
15## Scalars
16
17| Name | Bytes |
18|------|-------|
19| int8    | <kbd>[b]</kbd> |
20| uint8   | <kbd>[b]</kbd> |
21| int16   | <kbd>[bb]</kbd> |
22| uint16  | <kbd>[bb]</kbd> |
23| float16 | <kbd>[bb]</kbd> |
24| int32   | <kbd>[bbbb]</kbd> |
25| uint32  | <kbd>[bbbb]</kbd> |
26| float   | <kbd>[bbbb]</kbd> |
27| int64   | <kbd>[bbbb][bbbb]</kbd> |
28| uint64  | <kbd>[bbbb][bbbb]</kbd> |
29| double  | <kbd>[bbbb][bbbb]</kbd> |
30
31## Vectors
32### STD140 & STD430
33
34| Name | Bytes |
35|------|-------|
36| vec2\<float> | <kbd>[bbbb][bbbb]</kbd> |
37| vec3\<float> | <kbd>[bbbb][bbbb][bbbb][----]</kbd> |
38| vec4\<float> | <kbd>[bbbb][bbbb][bbbb][bbbb]</kbd> |
39| vec2\<int8>  | <kbd>[bb]</kbd> |
40| vec3\<int8>  | <kbd>[bbb-]</kbd> |
41| vec4\<int8>  | <kbd>[bbbb]</kbd> |
42| vec2\<int16> | <kbd>[bbbb]</kbd> |
43| vec3\<int16> | <kbd>[bbbb][bb--]</kbd> |
44| vec4\<int16> | <kbd>[bbbb][bbbb]</kbd> |
45| vec2\<int32> | <kbd>[bbbb][bbbb]</kbd> |
46| vec3\<int32> | <kbd>[bbbb][bbbb][bbbb][----]</kbd> |
47| vec4\<int32> | <kbd>[bbbb][bbbb][bbbb][bbbb]</kbd> |
48
49
50## Scalar Arrays
51### STD140
52| Name | Bytes |
53|------|-------|
54| int8[]  | <kbd>[b---][----][----][----]</kbd> |
55| int16[] | <kbd>[bb--][----][----][----]</kbd> |
56| int32[] | <kbd>[bbbb][----][----][----]</kbd> |
57| int64[] | <kbd>[bbbb][bbbb][----][----]</kbd> |
58| float[] | <kbd>[bbbb][----][----][----]</kbd> |
59
60### STD430
61| Name | Bytes |
62|------|-------|
63| int8[]  | <kbd>[b]</kbd> |
64| int16[] | <kbd>[bb]</kbd> |
65| int32[] | <kbd>[bbbb]</kbd> |
66| int64[] | <kbd>[bbbb][bbbb]</kbd> |
67| float[] | <kbd>[bbbb]</kbd> |
68
69
70## Vector Arrays
71### STD140
72| Name | Bytes |
73|------|-------|
74| vec2\<float>[] | <kbd>[bbbb][bbbb][----][----]</kbd> |
75| vec3\<float>[] | <kbd>[bbbb][bbbb][bbbb][----]</kbd> |
76| vec4\<float>[] | <kbd>[bbbb][bbbb][bbbb][bbbb]</kbd> |
77| vec2\<int8>[]  | <kbd>[bb--][----][----][----]</kbd> |
78| vec2\<int16>[] | <kbd>[bbbb][----][----][----]</kbd> |
79| vec2\<int32>[] | <kbd>[bbbb][bbbb][----][----]</kbd> |
80| vec3\<int8>[]  | <kbd>[bbb-][----][----][----]</kbd> |
81| vec3\<int16>[] | <kbd>[bbbb][bb--][----][----]</kbd> |
82| vec3\<int32>[] | <kbd>[bbbb][bbbb][bbbb][----]</kbd> |
83| vec4\<int8>[]  | <kbd>[bbbb][----][----][----]</kbd> |
84| vec4\<int16>[] | <kbd>[bbbb][bbbb][----][----]</kbd> |
85| vec4\<int32>[] | <kbd>[bbbb][bbbb][bbbb][bbbb]</kbd> |
86
87### STD430
88| Name | Bytes |
89|------|-------|
90| vec2\<float>[] | <kbd>[bbbb][bbbb]</kbd> |
91| vec3\<float>[] | <kbd>[bbbb][bbbb][bbbb][----]</kbd> |
92| vec4\<float>[] | <kbd>[bbbb][bbbb][bbbb][bbbb]</kbd> |
93| vec2\<int8>[]  | <kbd>[bb]</kbd> |
94| vec2\<int16>[] | <kbd>[bbbb]</kbd> |
95| vec2\<int32>[] | <kbd>[bbbb][bbbb]</kbd> |
96| vec3\<int8>[]  | <kbd>[bbb-]</kbd> |
97| vec3\<int16>[] | <kbd>[bbbb][bb--]</kbd> |
98| vec3\<int32>[] | <kbd>[bbbb][bbbb][bbbb][----]</kbd> |
99| vec4\<int8>[]  | <kbd>[bbbb]</kbd> |
100| vec4\<int16>[] | <kbd>[bbbb][bbbb]</kbd> |
101| vec4\<int32>[] | <kbd>[bbbb][bbbb][bbbb][bbbb]</kbd> |
102
103
104## Matrices (All matrices are column-major matrices, format is matCxR)
105
106Note, GLSL does not have matrices with integer components although they exist in
107other shader languages.
108
109### STD140
110| Name | Bytes |
111|------|-------|
112| mat2x2\<int8>  | <kbd>[bb--][----][----][----]<br/>[bb--][----][----][----]</kbd> |
113| mat2x2\<fp16>  | <kbd>[bbbb][----][----][----]<br/>[bbbb][----][----][----]</kbd> |
114| mat2x2\<float> | <kbd>[bbbb][bbbb][----][----]<br/>[bbbb][bbbb][----][----]</kbd> |
115| mat2x3\<float> | <kbd>[bbbb][bbbb][bbbb][----]<br/>[bbbb][bbbb][bbbb][----]</kbd> |
116| mat2x4\<float> | <kbd>[bbbb][bbbb][bbbb][bbbb]<br/>[bbbb][bbbb][bbbb][bbbb]</kbd> |
117| mat3x2\<float> | <kbd>[bbbb][bbbb][----][----]<br/>[bbbb][bbbb][----][----]<br/>[bbbb][bbbb][----][----]</kbd> |
118| mat4x2\<float> | <kbd>[bbbb][bbbb][----][----]<br/>[bbbb][bbbb][----][----]<br/>[bbbb][bbbb][----][----]<br/>[bbbb][bbbb][----][----]</kbd> |
119| mat4x3\<float> | <kbd>[bbbb][bbbb][bbbb][----]<br/>[bbbb][bbbb][bbbb][----]<br/>[bbbb][bbbb][bbbb][----]<br/>[bbbb][bbbb][bbbb][----]</kbd> |
120
121### STD430
122| Name | Bytes |
123|------|-------|
124| mat2x2\<fp16>  | <kbd>[bbbb]<br/>[bbbb]</kbd> |
125| mat2x2\<float> | <kbd>[bbbb][bbbb]<br/>[bbbb][bbbb]</kbd> |
126| mat2x3\<float> | <kbd>[bbbb][bbbb][bbbb][----]<br/>[bbbb][bbbb][bbbb][----]</kbd> |
127| mat2x4\<float> | <kbd>[bbbb][bbbb][bbbb][bbbb]<br/>[bbbb][bbbb][bbbb][bbbb]</kbd> |
128| mat2x2\<int8>  | <kbd>[bb]<br/>[bb]</kbd> |
129| mat3x2\<float> | <kbd>[bbbb][bbbb]<br/>[bbbb][bbbb]<br/>[bbbb][bbbb]</kbd> |
130| mat3x3\<float> | <kbd>[bbbb][bbbb][bbbb][----]<br/>[bbbb][bbbb][bbbb][----]<br/>[bbbb][bbbb][bbbb][----]</kbd> |
131| mat4x2\<float> | <kbd>[bbbb][bbbb][bbbb][bbbb]<br/>[bbbb][bbbb][bbbb][bbbb]</kbd> |
132| mat4x3\<float> | <kbd>[bbbb][bbbb][bbbb][----]<br/>[bbbb][bbbb][bbbb][----]<br/>[bbbb][bbbb][bbbb][----]<br/>[bbbb][bbbb][bbbb][----]</kbd> |
133
134
135## Matrix Array (All matrices are column-major matrices).
136
137In the examples shown the array stride is equal to the base alignment of the matrix
138so no extra padding is added between elements.
139
140### STD140
141| Name | Bytes |
142|------|-------|
143| mat2x2\<int8>[]  | <kbd>[bb--][----][----][----]<br/>[bb--][----][----][----]</kbd> |
144| mat2x2\<fp16>[]  | <kbd>[bbbb][----][----][----]<br/>[bbbb][----][----][----]</kbd> |
145| mat2x2\<float>[] | <kbd>[bbbb][bbbb][----][----]<br/>[bbbb][bbbb][----][----]</kbd> |
146| mat2x3\<float>[] | <kbd>[bbbb][bbbb][bbbb][----]<br/>[bbbb][bbbb][bbbb][----]</kbd> |
147| mat2x4\<float>[] | <kbd>[bbbb][bbbb][bbbb][bbbb]<br/>[bbbb][bbbb][bbbb][bbbb]</kbd> |
148| mat3x2\<float>[] | <kbd>[bbbb][bbbb][----][----]<br/>[bbbb][bbbb][----][----]<br/>[bbbb][bbbb][----][----]</kbd> |
149| mat4x2\<float>[] | <kbd>[bbbb][bbbb][----][----]<br/>[bbbb][bbbb][----][----]<br/>[bbbb][bbbb][----][----]<br/>[bbbb][bbbb][----][----]</kbd> |
150| mat4x3\<float>[] | <kbd>[bbbb][bbbb][bbbb][----]<br/>[bbbb][bbbb][bbbb][----]<br/>[bbbb][bbbb][bbbb][----]<br/>[bbbb][bbbb][bbbb][----]</kbd> |
151
152### STD430
153| Name | Bytes |
154|------|-------|
155| mat2x2\<fp16>[]  | <kbd>[bbbb]<br/>[bbbb]</kbd> |
156| mat2x2\<float>[] | <kbd>[bbbb][bbbb]<br/>[bbbb][bbbb]</kbd> |
157| mat2x3\<float>[] | <kbd>[bbbb][bbbb][bbbb][----]<br/>[bbbb][bbbb][bbbb][----]</kbd> |
158| mat2x4\<float>[] | <kbd>[bbbb][bbbb][bbbb][bbbb]<br/>[bbbb][bbbb][bbbb][bbbb]</kbd> |
159| mat2x2\<int8>[]  | <kbd>[bb]<br/>[bb]</kbd> |
160| mat3x2\<float>[] | <kbd>[bbbb][bbbb][bbbb][bbbb]<br/>[bbbb][bbbb]</kbd> |
161| mat3x3\<float>[] | <kbd>[bbbb][bbbb][bbbb][----]<br/>[bbbb][bbbb][bbbb][----]<br/>[bbbb][bbbb][bbbb][----]</kbd> |
162| mat4x2\<float>[] | <kbd>[bbbb][bbbb][bbbb][bbbb]<br/>[bbbb][bbbb][bbbb][bbbb]</kbd> |
163| mat4x3\<float>[] | <kbd>[bbbb][bbbb][bbbb][----]<br/>[bbbb][bbbb][bbbb][----]<br/>[bbbb][bbbb][bbbb][----]<br/>[bbbb][bbbb][bbbb][----]</kbd> |
164
165## Structures
166```
167struct {
168  int32 w;
169  float x;
170}
171```
172
173The STD140 pads 8 bytes at the end to become a multiple of 16 bytes.
174
175| STD | Array Stride | Bytes |
176|-----|:--------------------:|-------|
177| 140 | 16 | <kbd>{w [bbbb]}<br/>{x [bbbb]}<br/> [----][----]</kbd> |
178| 430 |  8 | <kbd>{w [bbbb]}<br/>{x [bbbb]}</kbd> |
179
180<hr>
181
182```
183struct {
184  struct {
185    int32 a;
186    float b;
187  } x;
188  float y;
189}
190```
191
192The STD140 pads 8 bytes at the end to become a multiple of 16 bytes.
193
194| STD | Array Stride | Bytes |
195|-----|:--------------------:|-------|
196| 140 | 32 | <kbd>{x {a [bbbb]}<br/> &nbsp;&nbsp; {b [bbbb]}<br/> &nbsp;&nbsp; [----][----]<br/>{y [bbbb][----][----][----]}</kbd> |
197| 430 | 12 | <kbd>{x {a [bbbb]}<br/> &nbsp;&nbsp; {b [bbbb]}<br/>{y [bbbb]}</kbd> |
198
199
200<hr>
201
202```
203struct {
204  int32 w;
205  vec2<float> x;
206  float y;
207}
208```
209
210For both cases, the `vec2` is the member with the largest base alignment
211requirement, so giving a base alignment of 8 bytes. The STD140 cases pads 8
212bytes at the end (in the array element case) to become a multiple of 16.
213
214| STD | Array Stride  | Bytes |
215|-----|:---------------------:|-------|
216| 140 | 32 | <kbd>{w [bbbb][----]}<br/>{x [bbbb][bbbb]}<br/>{y [bbbb][----]}<br/> [----][----]</kbd> |
217| 430 | 24 | <kbd>{w [bbbb][----]}<br/>{x [bbbb][bbbb]}<br/>{y [bbbb][----]}</kbd> |
218
219<hr>
220
221
222```
223struct {
224  int32 w;
225  vec3<float> x;
226  float y;
227}
228```
229
230The `vec3` expands to a `vec4`. This gives a base alignment of 16 bytes. So
231the `w` pads to 16 bytes. the `float y` is packed into the `vec3` as there is
232space (effectively making it a vec4).
233
234
235| STD | Array Stride | Bytes |
236|-----|:--------------------:|-------|
237| 140 | 32 | <kbd>{w [bbbb][----][----][----]}<br/>{x [bbbb][bbbb][bbbb]}<br/>{y [bbbb]}</kbd> |
238| 430 | 32 | <kbd>{w [bbbb][----][----][----]}<br/>{x [bbbb][bbbb][bbbb]}<br/>{y [bbbb]}</kbd> |
239
240<hr>
241
242
243```
244struct {
245  int32 w;
246  vec3<float> x;
247  vec2<float> y;
248}
249```
250
251The `vec3` expands to a `vec4`. This gives a base alignment of 16 bytes. So
252the `w` pads to 16 bytes.
253
254
255| STD | Array Stride | Bytes |
256|-----|:--------------------:|-------|
257| 140 | 48 | <kbd>{w [bbbb][----][----][----]}<br/>{x [bbbb][bbbb][bbbb][----]}<br/>{y [bbbb][bbbb]}<br/>[----][----]</kbd> |
258| 430 | 48 | <kbd>{w [bbbb][----][----][----]}<br/>{x [bbbb][bbbb][bbbb][----]}<br/>{y [bbbb][bbbb]}<br/>[----][----]</kbd> |
259
260<hr>
261
262
263```
264struct {
265  int32 w;
266  mat2x2<float> x;
267  float y;
268}
269```
270
271In STD140 the `mat2x2<float>` has a base alignment of 16 bytes (the mat2x2 is, effectively,
272an array of vec2's The vec2's have a size of 8 bytes this size rounds up to a vec4, so 16 bytes).
273
274In STD430, the round up doesn't happen, so the base alignment is 8 bytes.
275
276| STD | Array Stride  | Bytes |
277|-----|:---------------------:|-------|
278| 140 | 64 | <kbd>{w [bbbb][----][----][----]}<br/>{x [bbbb][bbbb][----][----]<br/> &nbsp;&nbsp; [bbbb][bbbb][----][----]}<br/>{y [bbbb][----][----][----]}</kbd> |
279| 430 | 32 | <kbd>{w [bbbb][----]}<br/>{x [bbbb][bbbb][bbbb][bbbb]}<br/>{y [bbbb][----]}</kbd> |
280
281<hr>
282
283
284```
285struct {
286  int32 w;
287  struct {
288    int32 a;
289    int32 b;
290    float c;
291  } x;
292  float y;
293}
294```
295
296The base alignment of the largest item is 4 bytes. In STD140, this rounds up to
29716 bytes because of the substructure.
298
299| STD | Array Stride | Bytes |
300|-----|:--------------------:|-------|
301| 140 | 48 | <kbd>{w [bbbb][----][----][----]}<br/>{x a{[bbbb]}<br/> &nbsp;&nbsp; b{[bbbb]}<br/> &nbsp;&nbsp; c{[bbbb]}<br /> &nbsp;&nbsp; &nbsp;&nbsp;[----]<br/>{y [bbbb][----][----][----]}</kbd> |
302| 430 | 20 | <kbd>{w [bbbb]}<br/>{x a{[bbbb]}<br/> &nbsp;&nbsp; b{[bbbb]}<br/> &nbsp;&nbsp; c{[bbbb]}<br/>{y [bbbb]}</kbd> |
303
304<hr>
305
306```
307struct {
308  int32 w;
309  struct {
310    int32 a;
311    int32 b;
312    float c[3];
313  } x;
314  float y;
315}
316```
317
318The `int a` and `int b` end up packing together so 16 bytes of padding are added (instead of 24 bytes).
319The `float c[3]` has an array stride of 16 bytes in STD140 and 4 bytes in STD430.
320
321| STD | Array Stride | Bytes |
322|-----|:--------------------:|-------|
323| 140 | 96 | <kbd>{w [bbbb][----][----][----]}<br/>{x {a [bbbb]}<br/> &nbsp;&nbsp; {b [bbbb]}<br/> &nbsp;&nbsp; &nbsp;&nbsp; [----][----]<br/> &nbsp;&nbsp; {c [bbbb][----][----][----]<br/> &nbsp;&nbsp; &nbsp;&nbsp; [bbbb][----][----][----]<br/> &nbsp;&nbsp; &nbsp;&nbsp; [bbbb][----][----][----]}}<br/>{y [bbbb][----][----][----]}</kbd> |
324| 430 | 28 | <kbd>{w [bbbb]}<br/>{x a{[bbbb]}<br/> &nbsp;&nbsp; {b [bbbb]}<br/> &nbsp;&nbsp; {c [bbbb][bbbb][bbbb] }}<br/>{y [bbbb]}</kbd> |
325