1 /*
2 * Copyright © 2011 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 /**
25 * @file brw_vue_map.c
26 *
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.
30 *
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.
35 *
36 * Authors:
37 * Paul Berry <stereotype441@gmail.com>
38 * Chris Forbes <chrisf@ijw.co.nz>
39 * Eric Anholt <eric@anholt.net>
40 */
41
42
43 #include "brw_compiler.h"
44 #include "dev/intel_debug.h"
45
46 static inline void
assign_vue_slot(struct brw_vue_map * vue_map,int varying,int slot)47 assign_vue_slot(struct brw_vue_map *vue_map, int varying, int slot)
48 {
49 /* Make sure this varying hasn't been assigned a slot already */
50 assert (vue_map->varying_to_slot[varying] == -1);
51
52 vue_map->varying_to_slot[varying] = slot;
53 vue_map->slot_to_varying[slot] = varying;
54 }
55
56 /**
57 * Compute the VUE map for a shader stage.
58 */
59 void
brw_compute_vue_map(const struct intel_device_info * devinfo,struct brw_vue_map * vue_map,uint64_t slots_valid,bool separate,uint32_t pos_slots)60 brw_compute_vue_map(const struct intel_device_info *devinfo,
61 struct brw_vue_map *vue_map,
62 uint64_t slots_valid,
63 bool separate,
64 uint32_t pos_slots)
65 {
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.
69 */
70 if (devinfo->ver < 6)
71 separate = false;
72
73 if (separate) {
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.
78 *
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.
81 */
82 slots_valid |= BITFIELD64_BIT(VARYING_SLOT_CLIP_DIST0);
83 slots_valid |= BITFIELD64_BIT(VARYING_SLOT_CLIP_DIST1);
84 }
85
86 vue_map->slots_valid = slots_valid;
87 vue_map->separate = separate;
88
89 /* gl_Layer, gl_ViewportIndex & gl_PrimitiveShadingRateEXT don't get their
90 * own varying slots -- they are stored in the first VUE slot
91 * (VARYING_SLOT_PSIZ).
92 */
93 slots_valid &= ~(VARYING_BIT_LAYER | VARYING_BIT_VIEWPORT | VARYING_BIT_PRIMITIVE_SHADING_RATE);
94
95 /* Make sure that the values we store in vue_map->varying_to_slot and
96 * vue_map->slot_to_varying won't overflow the signed chars that are used
97 * to store them. Note that since vue_map->slot_to_varying sometimes holds
98 * values equal to BRW_VARYING_SLOT_COUNT, we need to ensure that
99 * BRW_VARYING_SLOT_COUNT is <= 127, not 128.
100 */
101 STATIC_ASSERT(BRW_VARYING_SLOT_COUNT <= 127);
102
103 for (int i = 0; i < BRW_VARYING_SLOT_COUNT; ++i) {
104 vue_map->varying_to_slot[i] = -1;
105 vue_map->slot_to_varying[i] = BRW_VARYING_SLOT_PAD;
106 }
107
108 int slot = 0;
109
110 /* VUE header: format depends on chip generation and whether clipping is
111 * enabled.
112 *
113 * See the Sandybridge PRM, Volume 2 Part 1, section 1.5.1 (page 30),
114 * "Vertex URB Entry (VUE) Formats" which describes the VUE header layout.
115 */
116 if (devinfo->ver < 6) {
117 /* There are 8 dwords in VUE header pre-Ironlake:
118 * dword 0-3 is indices, point width, clip flags.
119 * dword 4-7 is ndc position
120 * dword 8-11 is the first vertex data.
121 *
122 * On Ironlake the VUE header is nominally 20 dwords, but the hardware
123 * will accept the same header layout as Gfx4 [and should be a bit faster]
124 */
125 assign_vue_slot(vue_map, VARYING_SLOT_PSIZ, slot++);
126 assign_vue_slot(vue_map, BRW_VARYING_SLOT_NDC, slot++);
127 assign_vue_slot(vue_map, VARYING_SLOT_POS, slot++);
128 } else {
129 /* There are 8 or 16 DWs (D0-D15) in VUE header on Sandybridge:
130 * dword 0-3 of the header is shading rate, indices, point width, clip flags.
131 * dword 4-7 is the 4D space position
132 * dword 8-15 of the vertex header is the user clip distance if
133 * enabled.
134 * dword 8-11 or 16-19 is the first vertex element data we fill.
135 */
136 assign_vue_slot(vue_map, VARYING_SLOT_PSIZ, slot++);
137 assign_vue_slot(vue_map, VARYING_SLOT_POS, slot++);
138
139 /* When using Primitive Replication, multiple slots are used for storing
140 * positions for each view.
141 */
142 assert(pos_slots >= 1);
143 if (pos_slots > 1) {
144 for (int i = 1; i < pos_slots; i++) {
145 vue_map->slot_to_varying[slot++] = VARYING_SLOT_POS;
146 }
147 }
148
149 if (slots_valid & BITFIELD64_BIT(VARYING_SLOT_CLIP_DIST0))
150 assign_vue_slot(vue_map, VARYING_SLOT_CLIP_DIST0, slot++);
151 if (slots_valid & BITFIELD64_BIT(VARYING_SLOT_CLIP_DIST1))
152 assign_vue_slot(vue_map, VARYING_SLOT_CLIP_DIST1, slot++);
153
154 /* Vertex URB Formats table says: "Vertex Header shall be padded at the
155 * end so that the header ends on a 32-byte boundary".
156 */
157 slot += slot % 2;
158
159 /* front and back colors need to be consecutive so that we can use
160 * ATTRIBUTE_SWIZZLE_INPUTATTR_FACING to swizzle them when doing
161 * two-sided color.
162 */
163 if (slots_valid & BITFIELD64_BIT(VARYING_SLOT_COL0))
164 assign_vue_slot(vue_map, VARYING_SLOT_COL0, slot++);
165 if (slots_valid & BITFIELD64_BIT(VARYING_SLOT_BFC0))
166 assign_vue_slot(vue_map, VARYING_SLOT_BFC0, slot++);
167 if (slots_valid & BITFIELD64_BIT(VARYING_SLOT_COL1))
168 assign_vue_slot(vue_map, VARYING_SLOT_COL1, slot++);
169 if (slots_valid & BITFIELD64_BIT(VARYING_SLOT_BFC1))
170 assign_vue_slot(vue_map, VARYING_SLOT_BFC1, slot++);
171 }
172
173 /* The hardware doesn't care about the rest of the vertex outputs, so we
174 * can assign them however we like. For normal programs, we simply assign
175 * them contiguously.
176 *
177 * For separate shader pipelines, we first assign built-in varyings
178 * contiguous slots. This works because ARB_separate_shader_objects
179 * requires that all shaders have matching built-in varying interface
180 * blocks. Next, we assign generic varyings based on their location
181 * (either explicit or linker assigned). This guarantees a fixed layout.
182 *
183 * We generally don't need to assign a slot for VARYING_SLOT_CLIP_VERTEX,
184 * since it's encoded as the clip distances by emit_clip_distances().
185 * However, it may be output by transform feedback, and we'd rather not
186 * recompute state when TF changes, so we just always include it.
187 */
188 uint64_t builtins = slots_valid & BITFIELD64_MASK(VARYING_SLOT_VAR0);
189 while (builtins != 0) {
190 const int varying = ffsll(builtins) - 1;
191 if (vue_map->varying_to_slot[varying] == -1) {
192 assign_vue_slot(vue_map, varying, slot++);
193 }
194 builtins &= ~BITFIELD64_BIT(varying);
195 }
196
197 const int first_generic_slot = slot;
198 uint64_t generics = slots_valid & ~BITFIELD64_MASK(VARYING_SLOT_VAR0);
199 while (generics != 0) {
200 const int varying = ffsll(generics) - 1;
201 if (separate) {
202 slot = first_generic_slot + varying - VARYING_SLOT_VAR0;
203 }
204 assign_vue_slot(vue_map, varying, slot++);
205 generics &= ~BITFIELD64_BIT(varying);
206 }
207
208 vue_map->num_slots = slot;
209 vue_map->num_per_vertex_slots = 0;
210 vue_map->num_per_patch_slots = 0;
211 }
212
213 /**
214 * Compute the VUE map for tessellation control shader outputs and
215 * tessellation evaluation shader inputs.
216 */
217 void
brw_compute_tess_vue_map(struct brw_vue_map * vue_map,uint64_t vertex_slots,uint32_t patch_slots)218 brw_compute_tess_vue_map(struct brw_vue_map *vue_map,
219 uint64_t vertex_slots,
220 uint32_t patch_slots)
221 {
222 /* I don't think anything actually uses this... */
223 vue_map->slots_valid = vertex_slots;
224
225 /* separate isn't really meaningful, but make sure it's initialized */
226 vue_map->separate = false;
227
228 vertex_slots &= ~(VARYING_BIT_TESS_LEVEL_OUTER |
229 VARYING_BIT_TESS_LEVEL_INNER);
230
231 /* Make sure that the values we store in vue_map->varying_to_slot and
232 * vue_map->slot_to_varying won't overflow the signed chars that are used
233 * to store them. Note that since vue_map->slot_to_varying sometimes holds
234 * values equal to VARYING_SLOT_TESS_MAX , we need to ensure that
235 * VARYING_SLOT_TESS_MAX is <= 127, not 128.
236 */
237 STATIC_ASSERT(VARYING_SLOT_TESS_MAX <= 127);
238
239 for (int i = 0; i < VARYING_SLOT_TESS_MAX ; ++i) {
240 vue_map->varying_to_slot[i] = -1;
241 vue_map->slot_to_varying[i] = BRW_VARYING_SLOT_PAD;
242 }
243
244 int slot = 0;
245
246 /* The first 8 DWords are reserved for the "Patch Header".
247 *
248 * VARYING_SLOT_TESS_LEVEL_OUTER / INNER live here, but the exact layout
249 * depends on the domain type. They might not be in slots 0 and 1 as
250 * described here, but pretending they're separate allows us to uniquely
251 * identify them by distinct slot locations.
252 */
253 assign_vue_slot(vue_map, VARYING_SLOT_TESS_LEVEL_INNER, slot++);
254 assign_vue_slot(vue_map, VARYING_SLOT_TESS_LEVEL_OUTER, slot++);
255
256 /* first assign per-patch varyings */
257 while (patch_slots != 0) {
258 const int varying = ffsll(patch_slots) - 1;
259 if (vue_map->varying_to_slot[varying + VARYING_SLOT_PATCH0] == -1) {
260 assign_vue_slot(vue_map, varying + VARYING_SLOT_PATCH0, slot++);
261 }
262 patch_slots &= ~BITFIELD64_BIT(varying);
263 }
264
265 /* apparently, including the patch header... */
266 vue_map->num_per_patch_slots = slot;
267
268 /* then assign per-vertex varyings for each vertex in our patch */
269 while (vertex_slots != 0) {
270 const int varying = ffsll(vertex_slots) - 1;
271 if (vue_map->varying_to_slot[varying] == -1) {
272 assign_vue_slot(vue_map, varying, slot++);
273 }
274 vertex_slots &= ~BITFIELD64_BIT(varying);
275 }
276
277 vue_map->num_per_vertex_slots = slot - vue_map->num_per_patch_slots;
278 vue_map->num_slots = slot;
279 }
280
281 static const char *
varying_name(brw_varying_slot slot,gl_shader_stage stage)282 varying_name(brw_varying_slot slot, gl_shader_stage stage)
283 {
284 assume(slot < BRW_VARYING_SLOT_COUNT);
285
286 if (slot < VARYING_SLOT_MAX)
287 return gl_varying_slot_name_for_stage((gl_varying_slot)slot, stage);
288
289 static const char *brw_names[] = {
290 [BRW_VARYING_SLOT_NDC - VARYING_SLOT_MAX] = "BRW_VARYING_SLOT_NDC",
291 [BRW_VARYING_SLOT_PAD - VARYING_SLOT_MAX] = "BRW_VARYING_SLOT_PAD",
292 [BRW_VARYING_SLOT_PNTC - VARYING_SLOT_MAX] = "BRW_VARYING_SLOT_PNTC",
293 };
294
295 return brw_names[slot - VARYING_SLOT_MAX];
296 }
297
298 void
brw_print_vue_map(FILE * fp,const struct brw_vue_map * vue_map,gl_shader_stage stage)299 brw_print_vue_map(FILE *fp, const struct brw_vue_map *vue_map,
300 gl_shader_stage stage)
301 {
302 if (vue_map->num_per_vertex_slots > 0 || vue_map->num_per_patch_slots > 0) {
303 fprintf(fp, "PUE map (%d slots, %d/patch, %d/vertex, %s)\n",
304 vue_map->num_slots,
305 vue_map->num_per_patch_slots,
306 vue_map->num_per_vertex_slots,
307 vue_map->separate ? "SSO" : "non-SSO");
308 for (int i = 0; i < vue_map->num_slots; i++) {
309 if (vue_map->slot_to_varying[i] >= VARYING_SLOT_PATCH0) {
310 fprintf(fp, " [%d] VARYING_SLOT_PATCH%d\n", i,
311 vue_map->slot_to_varying[i] - VARYING_SLOT_PATCH0);
312 } else {
313 fprintf(fp, " [%d] %s\n", i,
314 varying_name(vue_map->slot_to_varying[i], stage));
315 }
316 }
317 } else {
318 fprintf(fp, "VUE map (%d slots, %s)\n",
319 vue_map->num_slots, vue_map->separate ? "SSO" : "non-SSO");
320 for (int i = 0; i < vue_map->num_slots; i++) {
321 fprintf(fp, " [%d] %s\n", i,
322 varying_name(vue_map->slot_to_varying[i], stage));
323 }
324 }
325 fprintf(fp, "\n");
326 }
327