xref: /aosp_15_r20/external/mesa3d/src/intel/compiler/elk/elk_vue_map.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 elk_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 <[email protected]>
38  *   Chris Forbes <[email protected]>
39  *   Eric Anholt <[email protected]>
40  */
41 
42 
43 #include "elk_compiler.h"
44 #include "dev/intel_debug.h"
45 
46 static inline void
assign_vue_slot(struct intel_vue_map * vue_map,int varying,int slot)47 assign_vue_slot(struct intel_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
elk_compute_vue_map(const struct intel_device_info * devinfo,struct intel_vue_map * vue_map,uint64_t slots_valid,bool separate,uint32_t pos_slots)60 elk_compute_vue_map(const struct intel_device_info *devinfo,
61                     struct intel_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 ELK_VARYING_SLOT_COUNT, we need to ensure that
99     * ELK_VARYING_SLOT_COUNT is <= 127, not 128.
100     */
101    STATIC_ASSERT(ELK_VARYING_SLOT_COUNT <= 127);
102 
103    for (int i = 0; i < ELK_VARYING_SLOT_COUNT; ++i) {
104       vue_map->varying_to_slot[i] = -1;
105       vue_map->slot_to_varying[i] = ELK_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, ELK_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_pos_slots = pos_slots;
210    vue_map->num_per_vertex_slots = 0;
211    vue_map->num_per_patch_slots = 0;
212 }
213 
214 /**
215  * Compute the VUE map for tessellation control shader outputs and
216  * tessellation evaluation shader inputs.
217  */
218 void
elk_compute_tess_vue_map(struct intel_vue_map * vue_map,uint64_t vertex_slots,uint32_t patch_slots)219 elk_compute_tess_vue_map(struct intel_vue_map *vue_map,
220                          uint64_t vertex_slots,
221                          uint32_t patch_slots)
222 {
223    /* I don't think anything actually uses this... */
224    vue_map->slots_valid = vertex_slots;
225 
226    /* separate isn't really meaningful, but make sure it's initialized */
227    vue_map->separate = false;
228 
229    vertex_slots &= ~(VARYING_BIT_TESS_LEVEL_OUTER |
230                      VARYING_BIT_TESS_LEVEL_INNER);
231 
232    /* Make sure that the values we store in vue_map->varying_to_slot and
233     * vue_map->slot_to_varying won't overflow the signed chars that are used
234     * to store them.  Note that since vue_map->slot_to_varying sometimes holds
235     * values equal to VARYING_SLOT_TESS_MAX , we need to ensure that
236     * VARYING_SLOT_TESS_MAX is <= 127, not 128.
237     */
238    STATIC_ASSERT(VARYING_SLOT_TESS_MAX <= 127);
239 
240    for (int i = 0; i < VARYING_SLOT_TESS_MAX ; ++i) {
241       vue_map->varying_to_slot[i] = -1;
242       vue_map->slot_to_varying[i] = ELK_VARYING_SLOT_PAD;
243    }
244 
245    int slot = 0;
246 
247    /* The first 8 DWords are reserved for the "Patch Header".
248     *
249     * VARYING_SLOT_TESS_LEVEL_OUTER / INNER live here, but the exact layout
250     * depends on the domain type.  They might not be in slots 0 and 1 as
251     * described here, but pretending they're separate allows us to uniquely
252     * identify them by distinct slot locations.
253     */
254    assign_vue_slot(vue_map, VARYING_SLOT_TESS_LEVEL_INNER, slot++);
255    assign_vue_slot(vue_map, VARYING_SLOT_TESS_LEVEL_OUTER, slot++);
256 
257    /* first assign per-patch varyings */
258    while (patch_slots != 0) {
259       const int varying = ffsll(patch_slots) - 1;
260       if (vue_map->varying_to_slot[varying + VARYING_SLOT_PATCH0] == -1) {
261          assign_vue_slot(vue_map, varying + VARYING_SLOT_PATCH0, slot++);
262       }
263       patch_slots &= ~BITFIELD64_BIT(varying);
264    }
265 
266    /* apparently, including the patch header... */
267    vue_map->num_per_patch_slots = slot;
268 
269    /* then assign per-vertex varyings for each vertex in our patch */
270    while (vertex_slots != 0) {
271       const int varying = ffsll(vertex_slots) - 1;
272       if (vue_map->varying_to_slot[varying] == -1) {
273          assign_vue_slot(vue_map, varying, slot++);
274       }
275       vertex_slots &= ~BITFIELD64_BIT(varying);
276    }
277 
278    vue_map->num_per_vertex_slots = slot - vue_map->num_per_patch_slots;
279    vue_map->num_pos_slots = 0;
280    vue_map->num_slots = slot;
281 }
282 
283 static const char *
varying_name(elk_varying_slot slot,gl_shader_stage stage)284 varying_name(elk_varying_slot slot, gl_shader_stage stage)
285 {
286    assume(slot < ELK_VARYING_SLOT_COUNT);
287 
288    if (slot < VARYING_SLOT_MAX)
289       return gl_varying_slot_name_for_stage((gl_varying_slot)slot, stage);
290 
291    static const char *elk_names[] = {
292       [ELK_VARYING_SLOT_NDC - VARYING_SLOT_MAX] = "ELK_VARYING_SLOT_NDC",
293       [ELK_VARYING_SLOT_PAD - VARYING_SLOT_MAX] = "ELK_VARYING_SLOT_PAD",
294       [ELK_VARYING_SLOT_PNTC - VARYING_SLOT_MAX] = "ELK_VARYING_SLOT_PNTC",
295    };
296 
297    return elk_names[slot - VARYING_SLOT_MAX];
298 }
299 
300 void
elk_print_vue_map(FILE * fp,const struct intel_vue_map * vue_map,gl_shader_stage stage)301 elk_print_vue_map(FILE *fp, const struct intel_vue_map *vue_map,
302                   gl_shader_stage stage)
303 {
304    if (vue_map->num_per_vertex_slots > 0 || vue_map->num_per_patch_slots > 0) {
305       fprintf(fp, "PUE map (%d slots, %d/patch, %d/vertex, %s)\n",
306               vue_map->num_slots,
307               vue_map->num_per_patch_slots,
308               vue_map->num_per_vertex_slots,
309               vue_map->separate ? "SSO" : "non-SSO");
310       for (int i = 0; i < vue_map->num_slots; i++) {
311          if (vue_map->slot_to_varying[i] >= VARYING_SLOT_PATCH0) {
312             fprintf(fp, "  [%d] VARYING_SLOT_PATCH%d\n", i,
313                     vue_map->slot_to_varying[i] - VARYING_SLOT_PATCH0);
314          } else {
315             fprintf(fp, "  [%d] %s\n", i,
316                     varying_name(vue_map->slot_to_varying[i], stage));
317          }
318       }
319    } else {
320       fprintf(fp, "VUE map (%d slots, %s)\n",
321               vue_map->num_slots, vue_map->separate ? "SSO" : "non-SSO");
322       for (int i = 0; i < vue_map->num_slots; i++) {
323          fprintf(fp, "  [%d] %s\n", i,
324                  varying_name(vue_map->slot_to_varying[i], stage));
325       }
326    }
327    fprintf(fp, "\n");
328 }
329