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
46static inline void
47assign_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 */
59void
60brw_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 and gl_ViewportIndex don't get their own varying slots -- they
90    * are stored in the first VUE slot (VARYING_SLOT_PSIZ).
91    */
92   slots_valid &= ~(VARYING_BIT_LAYER | VARYING_BIT_VIEWPORT);
93
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.
99    */
100   STATIC_ASSERT(BRW_VARYING_SLOT_COUNT <= 127);
101
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;
105   }
106
107   int slot = 0;
108
109   /* VUE header: format depends on chip generation and whether clipping is
110    * enabled.
111    *
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.
114    */
115   if (devinfo->ver < 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.
120       *
121       * On Ironlake the VUE header is nominally 20 dwords, but the hardware
122       * will accept the same header layout as Gfx4 [and should be a bit faster]
123       */
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++);
127   } else {
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
132       * enabled.
133       * dword 8-11 or 16-19 is the first vertex element data we fill.
134       */
135      assign_vue_slot(vue_map, VARYING_SLOT_PSIZ, slot++);
136      assign_vue_slot(vue_map, VARYING_SLOT_POS, slot++);
137
138      /* When using Primitive Replication, multiple slots are used for storing
139       * positions for each view.
140       */
141      assert(pos_slots >= 1);
142      if (pos_slots > 1) {
143         for (int i = 1; i < pos_slots; i++) {
144            vue_map->slot_to_varying[slot++] = VARYING_SLOT_POS;
145         }
146      }
147
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++);
152
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".
155       */
156      slot += slot % 2;
157
158      /* front and back colors need to be consecutive so that we can use
159       * ATTRIBUTE_SWIZZLE_INPUTATTR_FACING to swizzle them when doing
160       * two-sided color.
161       */
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++);
170   }
171
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
174    * them contiguously.
175    *
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.
181    *
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.
186    */
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++);
192      }
193      builtins &= ~BITFIELD64_BIT(varying);
194   }
195
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;
200      if (separate) {
201         slot = first_generic_slot + varying - VARYING_SLOT_VAR0;
202      }
203      assign_vue_slot(vue_map, varying, slot++);
204      generics &= ~BITFIELD64_BIT(varying);
205   }
206
207   vue_map->num_slots = slot;
208   vue_map->num_per_vertex_slots = 0;
209   vue_map->num_per_patch_slots = 0;
210}
211
212/**
213 * Compute the VUE map for tessellation control shader outputs and
214 * tessellation evaluation shader inputs.
215 */
216void
217brw_compute_tess_vue_map(struct brw_vue_map *vue_map,
218                         uint64_t vertex_slots,
219                         uint32_t patch_slots)
220{
221   /* I don't think anything actually uses this... */
222   vue_map->slots_valid = vertex_slots;
223
224   /* separate isn't really meaningful, but make sure it's initialized */
225   vue_map->separate = false;
226
227   vertex_slots &= ~(VARYING_BIT_TESS_LEVEL_OUTER |
228                     VARYING_BIT_TESS_LEVEL_INNER);
229
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.
235    */
236   STATIC_ASSERT(VARYING_SLOT_TESS_MAX <= 127);
237
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;
241   }
242
243   int slot = 0;
244
245   /* The first 8 DWords are reserved for the "Patch Header".
246    *
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.
251    */
252   assign_vue_slot(vue_map, VARYING_SLOT_TESS_LEVEL_INNER, slot++);
253   assign_vue_slot(vue_map, VARYING_SLOT_TESS_LEVEL_OUTER, slot++);
254
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++);
260      }
261      patch_slots &= ~BITFIELD64_BIT(varying);
262   }
263
264   /* apparently, including the patch header... */
265   vue_map->num_per_patch_slots = slot;
266
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++);
272      }
273      vertex_slots &= ~BITFIELD64_BIT(varying);
274   }
275
276   vue_map->num_per_vertex_slots = slot - vue_map->num_per_patch_slots;
277   vue_map->num_slots = slot;
278}
279
280static const char *
281varying_name(brw_varying_slot slot, gl_shader_stage stage)
282{
283   assume(slot < BRW_VARYING_SLOT_COUNT);
284
285   if (slot < VARYING_SLOT_MAX)
286      return gl_varying_slot_name_for_stage((gl_varying_slot)slot, stage);
287
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",
292   };
293
294   return brw_names[slot - VARYING_SLOT_MAX];
295}
296
297void
298brw_print_vue_map(FILE *fp, const struct brw_vue_map *vue_map,
299                  gl_shader_stage stage)
300{
301   if (vue_map->num_per_vertex_slots > 0 || vue_map->num_per_patch_slots > 0) {
302      fprintf(fp, "PUE map (%d slots, %d/patch, %d/vertex, %s)\n",
303              vue_map->num_slots,
304              vue_map->num_per_patch_slots,
305              vue_map->num_per_vertex_slots,
306              vue_map->separate ? "SSO" : "non-SSO");
307      for (int i = 0; i < vue_map->num_slots; i++) {
308         if (vue_map->slot_to_varying[i] >= VARYING_SLOT_PATCH0) {
309            fprintf(fp, "  [%d] VARYING_SLOT_PATCH%d\n", i,
310                    vue_map->slot_to_varying[i] - VARYING_SLOT_PATCH0);
311         } else {
312            fprintf(fp, "  [%d] %s\n", i,
313                    varying_name(vue_map->slot_to_varying[i], stage));
314         }
315      }
316   } else {
317      fprintf(fp, "VUE map (%d slots, %s)\n",
318              vue_map->num_slots, vue_map->separate ? "SSO" : "non-SSO");
319      for (int i = 0; i < vue_map->num_slots; i++) {
320         fprintf(fp, "  [%d] %s\n", i,
321                 varying_name(vue_map->slot_to_varying[i], stage));
322      }
323   }
324   fprintf(fp, "\n");
325}
326