2 * Copyright © 2011 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27 * This file computes the "VUE map" for a (non-fragment) shader stage, which
28 * describes the layout of its output varyings. The VUE map is used to match
29 * outputs from one stage with the inputs of the next.
31 * Largely, varyings can be placed however we like - producers/consumers simply
32 * have to agree on the layout. However, there is also a "VUE Header" that
33 * prescribes a fixed-layout for items that interact with fixed function
34 * hardware, such as the clipper and rasterizer.
37 * Paul Berry <stereotype441@gmail.com>
38 * Chris Forbes <chrisf@ijw.co.nz>
39 * Eric Anholt <eric@anholt.net>
43 #include "brw_compiler.h"
44 #include "dev/gen_debug.h"
47 assign_vue_slot(struct brw_vue_map
*vue_map
, int varying
, int slot
)
49 /* Make sure this varying hasn't been assigned a slot already */
50 assert (vue_map
->varying_to_slot
[varying
] == -1);
52 vue_map
->varying_to_slot
[varying
] = slot
;
53 vue_map
->slot_to_varying
[slot
] = varying
;
57 * Compute the VUE map for a shader stage.
60 brw_compute_vue_map(const struct gen_device_info
*devinfo
,
61 struct brw_vue_map
*vue_map
,
66 /* Keep using the packed/contiguous layout on old hardware - we only need
67 * the SSO layout when using geometry/tessellation shaders or 32 FS input
68 * varyings, which only exist on Gen >= 6. It's also a bit more efficient.
74 /* In SSO mode, we don't know whether the adjacent stage will
75 * read/write gl_ClipDistance, which has a fixed slot location.
76 * We have to assume the worst and reserve a slot for it, or else
77 * the rest of our varyings will be off by a slot.
79 * Note that we don't have to worry about COL/BFC, as those built-in
80 * variables only exist in legacy GL, which only supports VS and FS.
82 slots_valid
|= BITFIELD64_BIT(VARYING_SLOT_CLIP_DIST0
);
83 slots_valid
|= BITFIELD64_BIT(VARYING_SLOT_CLIP_DIST1
);
86 vue_map
->slots_valid
= slots_valid
;
87 vue_map
->separate
= separate
;
89 /* gl_Layer and gl_ViewportIndex don't get their own varying slots -- they
90 * are stored in the first VUE slot (VARYING_SLOT_PSIZ).
92 slots_valid
&= ~(VARYING_BIT_LAYER
| VARYING_BIT_VIEWPORT
);
94 /* Make sure that the values we store in vue_map->varying_to_slot and
95 * vue_map->slot_to_varying won't overflow the signed chars that are used
96 * to store them. Note that since vue_map->slot_to_varying sometimes holds
97 * values equal to BRW_VARYING_SLOT_COUNT, we need to ensure that
98 * BRW_VARYING_SLOT_COUNT is <= 127, not 128.
100 STATIC_ASSERT(BRW_VARYING_SLOT_COUNT
<= 127);
102 for (int i
= 0; i
< BRW_VARYING_SLOT_COUNT
; ++i
) {
103 vue_map
->varying_to_slot
[i
] = -1;
104 vue_map
->slot_to_varying
[i
] = BRW_VARYING_SLOT_PAD
;
109 /* VUE header: format depends on chip generation and whether clipping is
112 * See the Sandybridge PRM, Volume 2 Part 1, section 1.5.1 (page 30),
113 * "Vertex URB Entry (VUE) Formats" which describes the VUE header layout.
115 if (devinfo
->gen
< 6) {
116 /* There are 8 dwords in VUE header pre-Ironlake:
117 * dword 0-3 is indices, point width, clip flags.
118 * dword 4-7 is ndc position
119 * dword 8-11 is the first vertex data.
121 * On Ironlake the VUE header is nominally 20 dwords, but the hardware
122 * will accept the same header layout as Gen4 [and should be a bit faster]
124 assign_vue_slot(vue_map
, VARYING_SLOT_PSIZ
, slot
++);
125 assign_vue_slot(vue_map
, BRW_VARYING_SLOT_NDC
, slot
++);
126 assign_vue_slot(vue_map
, VARYING_SLOT_POS
, slot
++);
128 /* There are 8 or 16 DWs (D0-D15) in VUE header on Sandybridge:
129 * dword 0-3 of the header is indices, point width, clip flags.
130 * dword 4-7 is the 4D space position
131 * dword 8-15 of the vertex header is the user clip distance if
133 * dword 8-11 or 16-19 is the first vertex element data we fill.
135 assign_vue_slot(vue_map
, VARYING_SLOT_PSIZ
, slot
++);
136 assign_vue_slot(vue_map
, VARYING_SLOT_POS
, slot
++);
138 /* When using Primitive Replication, multiple slots are used for storing
139 * positions for each view.
141 assert(pos_slots
>= 1);
143 for (int i
= 1; i
< pos_slots
; i
++) {
144 vue_map
->slot_to_varying
[slot
++] = VARYING_SLOT_POS
;
148 if (slots_valid
& BITFIELD64_BIT(VARYING_SLOT_CLIP_DIST0
))
149 assign_vue_slot(vue_map
, VARYING_SLOT_CLIP_DIST0
, slot
++);
150 if (slots_valid
& BITFIELD64_BIT(VARYING_SLOT_CLIP_DIST1
))
151 assign_vue_slot(vue_map
, VARYING_SLOT_CLIP_DIST1
, slot
++);
153 /* Vertex URB Formats table says: "Vertex Header shall be padded at the
154 * end so that the header ends on a 32-byte boundary".
158 /* front and back colors need to be consecutive so that we can use
159 * ATTRIBUTE_SWIZZLE_INPUTATTR_FACING to swizzle them when doing
162 if (slots_valid
& BITFIELD64_BIT(VARYING_SLOT_COL0
))
163 assign_vue_slot(vue_map
, VARYING_SLOT_COL0
, slot
++);
164 if (slots_valid
& BITFIELD64_BIT(VARYING_SLOT_BFC0
))
165 assign_vue_slot(vue_map
, VARYING_SLOT_BFC0
, slot
++);
166 if (slots_valid
& BITFIELD64_BIT(VARYING_SLOT_COL1
))
167 assign_vue_slot(vue_map
, VARYING_SLOT_COL1
, slot
++);
168 if (slots_valid
& BITFIELD64_BIT(VARYING_SLOT_BFC1
))
169 assign_vue_slot(vue_map
, VARYING_SLOT_BFC1
, slot
++);
172 /* The hardware doesn't care about the rest of the vertex outputs, so we
173 * can assign them however we like. For normal programs, we simply assign
176 * For separate shader pipelines, we first assign built-in varyings
177 * contiguous slots. This works because ARB_separate_shader_objects
178 * requires that all shaders have matching built-in varying interface
179 * blocks. Next, we assign generic varyings based on their location
180 * (either explicit or linker assigned). This guarantees a fixed layout.
182 * We generally don't need to assign a slot for VARYING_SLOT_CLIP_VERTEX,
183 * since it's encoded as the clip distances by emit_clip_distances().
184 * However, it may be output by transform feedback, and we'd rather not
185 * recompute state when TF changes, so we just always include it.
187 uint64_t builtins
= slots_valid
& BITFIELD64_MASK(VARYING_SLOT_VAR0
);
188 while (builtins
!= 0) {
189 const int varying
= ffsll(builtins
) - 1;
190 if (vue_map
->varying_to_slot
[varying
] == -1) {
191 assign_vue_slot(vue_map
, varying
, slot
++);
193 builtins
&= ~BITFIELD64_BIT(varying
);
196 const int first_generic_slot
= slot
;
197 uint64_t generics
= slots_valid
& ~BITFIELD64_MASK(VARYING_SLOT_VAR0
);
198 while (generics
!= 0) {
199 const int varying
= ffsll(generics
) - 1;
201 slot
= first_generic_slot
+ varying
- VARYING_SLOT_VAR0
;
203 assign_vue_slot(vue_map
, varying
, slot
++);
204 generics
&= ~BITFIELD64_BIT(varying
);
207 vue_map
->num_slots
= slot
;
208 vue_map
->num_per_vertex_slots
= 0;
209 vue_map
->num_per_patch_slots
= 0;
213 * Compute the VUE map for tessellation control shader outputs and
214 * tessellation evaluation shader inputs.
217 brw_compute_tess_vue_map(struct brw_vue_map
*vue_map
,
218 uint64_t vertex_slots
,
219 uint32_t patch_slots
)
221 /* I don't think anything actually uses this... */
222 vue_map
->slots_valid
= vertex_slots
;
224 /* separate isn't really meaningful, but make sure it's initialized */
225 vue_map
->separate
= false;
227 vertex_slots
&= ~(VARYING_BIT_TESS_LEVEL_OUTER
|
228 VARYING_BIT_TESS_LEVEL_INNER
);
230 /* Make sure that the values we store in vue_map->varying_to_slot and
231 * vue_map->slot_to_varying won't overflow the signed chars that are used
232 * to store them. Note that since vue_map->slot_to_varying sometimes holds
233 * values equal to VARYING_SLOT_TESS_MAX , we need to ensure that
234 * VARYING_SLOT_TESS_MAX is <= 127, not 128.
236 STATIC_ASSERT(VARYING_SLOT_TESS_MAX
<= 127);
238 for (int i
= 0; i
< VARYING_SLOT_TESS_MAX
; ++i
) {
239 vue_map
->varying_to_slot
[i
] = -1;
240 vue_map
->slot_to_varying
[i
] = BRW_VARYING_SLOT_PAD
;
245 /* The first 8 DWords are reserved for the "Patch Header".
247 * VARYING_SLOT_TESS_LEVEL_OUTER / INNER live here, but the exact layout
248 * depends on the domain type. They might not be in slots 0 and 1 as
249 * described here, but pretending they're separate allows us to uniquely
250 * identify them by distinct slot locations.
252 assign_vue_slot(vue_map
, VARYING_SLOT_TESS_LEVEL_INNER
, slot
++);
253 assign_vue_slot(vue_map
, VARYING_SLOT_TESS_LEVEL_OUTER
, slot
++);
255 /* first assign per-patch varyings */
256 while (patch_slots
!= 0) {
257 const int varying
= ffsll(patch_slots
) - 1;
258 if (vue_map
->varying_to_slot
[varying
+ VARYING_SLOT_PATCH0
] == -1) {
259 assign_vue_slot(vue_map
, varying
+ VARYING_SLOT_PATCH0
, slot
++);
261 patch_slots
&= ~BITFIELD64_BIT(varying
);
264 /* apparently, including the patch header... */
265 vue_map
->num_per_patch_slots
= slot
;
267 /* then assign per-vertex varyings for each vertex in our patch */
268 while (vertex_slots
!= 0) {
269 const int varying
= ffsll(vertex_slots
) - 1;
270 if (vue_map
->varying_to_slot
[varying
] == -1) {
271 assign_vue_slot(vue_map
, varying
, slot
++);
273 vertex_slots
&= ~BITFIELD64_BIT(varying
);
276 vue_map
->num_per_vertex_slots
= slot
- vue_map
->num_per_patch_slots
;
277 vue_map
->num_slots
= slot
;
281 varying_name(brw_varying_slot slot
)
283 assume(slot
< BRW_VARYING_SLOT_COUNT
);
285 if (slot
< VARYING_SLOT_MAX
)
286 return gl_varying_slot_name((gl_varying_slot
)slot
);
288 static const char *brw_names
[] = {
289 [BRW_VARYING_SLOT_NDC
- VARYING_SLOT_MAX
] = "BRW_VARYING_SLOT_NDC",
290 [BRW_VARYING_SLOT_PAD
- VARYING_SLOT_MAX
] = "BRW_VARYING_SLOT_PAD",
291 [BRW_VARYING_SLOT_PNTC
- VARYING_SLOT_MAX
] = "BRW_VARYING_SLOT_PNTC",
294 return brw_names
[slot
- VARYING_SLOT_MAX
];
298 brw_print_vue_map(FILE *fp
, const struct brw_vue_map
*vue_map
)
300 if (vue_map
->num_per_vertex_slots
> 0 || vue_map
->num_per_patch_slots
> 0) {
301 fprintf(fp
, "PUE map (%d slots, %d/patch, %d/vertex, %s)\n",
303 vue_map
->num_per_patch_slots
,
304 vue_map
->num_per_vertex_slots
,
305 vue_map
->separate
? "SSO" : "non-SSO");
306 for (int i
= 0; i
< vue_map
->num_slots
; i
++) {
307 if (vue_map
->slot_to_varying
[i
] >= VARYING_SLOT_PATCH0
) {
308 fprintf(fp
, " [%d] VARYING_SLOT_PATCH%d\n", i
,
309 vue_map
->slot_to_varying
[i
] - VARYING_SLOT_PATCH0
);
311 fprintf(fp
, " [%d] %s\n", i
,
312 varying_name(vue_map
->slot_to_varying
[i
]));
316 fprintf(fp
, "VUE map (%d slots, %s)\n",
317 vue_map
->num_slots
, vue_map
->separate
? "SSO" : "non-SSO");
318 for (int i
= 0; i
< vue_map
->num_slots
; i
++) {
319 fprintf(fp
, " [%d] %s\n", i
,
320 varying_name(vue_map
->slot_to_varying
[i
]));