xref: /aosp_15_r20/external/mesa3d/src/intel/compiler/brw_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
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 "brw_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
brw_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 brw_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    if (separate) {
67       /* In SSO mode, we don't know whether the adjacent stage will
68        * read/write gl_ClipDistance, which has a fixed slot location.
69        * We have to assume the worst and reserve a slot for it, or else
70        * the rest of our varyings will be off by a slot.
71        *
72        * Note that we don't have to worry about COL/BFC, as those built-in
73        * variables only exist in legacy GL, which only supports VS and FS.
74        */
75       slots_valid |= BITFIELD64_BIT(VARYING_SLOT_CLIP_DIST0);
76       slots_valid |= BITFIELD64_BIT(VARYING_SLOT_CLIP_DIST1);
77    }
78 
79    vue_map->slots_valid = slots_valid;
80    vue_map->separate = separate;
81 
82    /* gl_Layer, gl_ViewportIndex & gl_PrimitiveShadingRateEXT don't get their
83     * own varying slots -- they are stored in the first VUE slot
84     * (VARYING_SLOT_PSIZ).
85     */
86    slots_valid &= ~(VARYING_BIT_LAYER | VARYING_BIT_VIEWPORT | VARYING_BIT_PRIMITIVE_SHADING_RATE);
87 
88    /* Make sure that the values we store in vue_map->varying_to_slot and
89     * vue_map->slot_to_varying won't overflow the signed chars that are used
90     * to store them.  Note that since vue_map->slot_to_varying sometimes holds
91     * values equal to BRW_VARYING_SLOT_COUNT, we need to ensure that
92     * BRW_VARYING_SLOT_COUNT is <= 127, not 128.
93     */
94    STATIC_ASSERT(BRW_VARYING_SLOT_COUNT <= 127);
95 
96    for (int i = 0; i < BRW_VARYING_SLOT_COUNT; ++i) {
97       vue_map->varying_to_slot[i] = -1;
98       vue_map->slot_to_varying[i] = BRW_VARYING_SLOT_PAD;
99    }
100 
101    int slot = 0;
102 
103    /* VUE header: format depends on chip generation and whether clipping is
104     * enabled.
105     *
106     * See the Sandybridge PRM, Volume 2 Part 1, section 1.5.1 (page 30),
107     * "Vertex URB Entry (VUE) Formats" which describes the VUE header layout.
108     *
109     * There are 8 or 16 DWs (D0-D15) in VUE header on Sandybridge:
110     * dword 0-3 of the header is shading rate, indices, point width, clip flags.
111     * dword 4-7 is the 4D space position
112     * dword 8-15 of the vertex header is the user clip distance if
113     * enabled.
114     * dword 8-11 or 16-19 is the first vertex element data we fill.
115     */
116    assign_vue_slot(vue_map, VARYING_SLOT_PSIZ, slot++);
117    assign_vue_slot(vue_map, VARYING_SLOT_POS, slot++);
118 
119    /* When using Primitive Replication, multiple slots are used for storing
120     * positions for each view.
121     */
122    assert(pos_slots >= 1);
123    if (pos_slots > 1) {
124       for (int i = 1; i < pos_slots; i++) {
125          vue_map->slot_to_varying[slot++] = VARYING_SLOT_POS;
126       }
127    }
128 
129    if (slots_valid & BITFIELD64_BIT(VARYING_SLOT_CLIP_DIST0))
130       assign_vue_slot(vue_map, VARYING_SLOT_CLIP_DIST0, slot++);
131    if (slots_valid & BITFIELD64_BIT(VARYING_SLOT_CLIP_DIST1))
132       assign_vue_slot(vue_map, VARYING_SLOT_CLIP_DIST1, slot++);
133 
134    /* Vertex URB Formats table says: "Vertex Header shall be padded at the
135     * end so that the header ends on a 32-byte boundary".
136     */
137    slot += slot % 2;
138 
139    /* front and back colors need to be consecutive so that we can use
140     * ATTRIBUTE_SWIZZLE_INPUTATTR_FACING to swizzle them when doing
141     * two-sided color.
142     */
143    if (slots_valid & BITFIELD64_BIT(VARYING_SLOT_COL0))
144       assign_vue_slot(vue_map, VARYING_SLOT_COL0, slot++);
145    if (slots_valid & BITFIELD64_BIT(VARYING_SLOT_BFC0))
146       assign_vue_slot(vue_map, VARYING_SLOT_BFC0, slot++);
147    if (slots_valid & BITFIELD64_BIT(VARYING_SLOT_COL1))
148       assign_vue_slot(vue_map, VARYING_SLOT_COL1, slot++);
149    if (slots_valid & BITFIELD64_BIT(VARYING_SLOT_BFC1))
150       assign_vue_slot(vue_map, VARYING_SLOT_BFC1, slot++);
151 
152    /* The hardware doesn't care about the rest of the vertex outputs, so we
153     * can assign them however we like.  For normal programs, we simply assign
154     * them contiguously.
155     *
156     * For separate shader pipelines, we first assign built-in varyings
157     * contiguous slots.  This works because ARB_separate_shader_objects
158     * requires that all shaders have matching built-in varying interface
159     * blocks.  Next, we assign generic varyings based on their location
160     * (either explicit or linker assigned).  This guarantees a fixed layout.
161     *
162     * We generally don't need to assign a slot for VARYING_SLOT_CLIP_VERTEX,
163     * since it's encoded as the clip distances by emit_clip_distances().
164     * However, it may be output by transform feedback, and we'd rather not
165     * recompute state when TF changes, so we just always include it.
166     */
167    uint64_t builtins = slots_valid & BITFIELD64_MASK(VARYING_SLOT_VAR0);
168    while (builtins != 0) {
169       const int varying = ffsll(builtins) - 1;
170       if (vue_map->varying_to_slot[varying] == -1) {
171          assign_vue_slot(vue_map, varying, slot++);
172       }
173       builtins &= ~BITFIELD64_BIT(varying);
174    }
175 
176    const int first_generic_slot = slot;
177    uint64_t generics = slots_valid & ~BITFIELD64_MASK(VARYING_SLOT_VAR0);
178    while (generics != 0) {
179       const int varying = ffsll(generics) - 1;
180       if (separate) {
181          slot = first_generic_slot + varying - VARYING_SLOT_VAR0;
182       }
183       assign_vue_slot(vue_map, varying, slot++);
184       generics &= ~BITFIELD64_BIT(varying);
185    }
186 
187    vue_map->num_slots = slot;
188    vue_map->num_pos_slots = pos_slots;
189    vue_map->num_per_vertex_slots = 0;
190    vue_map->num_per_patch_slots = 0;
191 }
192 
193 /**
194  * Compute the VUE map for tessellation control shader outputs and
195  * tessellation evaluation shader inputs.
196  */
197 void
brw_compute_tess_vue_map(struct intel_vue_map * vue_map,uint64_t vertex_slots,uint32_t patch_slots)198 brw_compute_tess_vue_map(struct intel_vue_map *vue_map,
199                          uint64_t vertex_slots,
200                          uint32_t patch_slots)
201 {
202    /* I don't think anything actually uses this... */
203    vue_map->slots_valid = vertex_slots;
204 
205    /* separate isn't really meaningful, but make sure it's initialized */
206    vue_map->separate = false;
207 
208    vertex_slots &= ~(VARYING_BIT_TESS_LEVEL_OUTER |
209                      VARYING_BIT_TESS_LEVEL_INNER);
210 
211    /* Make sure that the values we store in vue_map->varying_to_slot and
212     * vue_map->slot_to_varying won't overflow the signed chars that are used
213     * to store them.  Note that since vue_map->slot_to_varying sometimes holds
214     * values equal to VARYING_SLOT_TESS_MAX , we need to ensure that
215     * VARYING_SLOT_TESS_MAX is <= 127, not 128.
216     */
217    STATIC_ASSERT(VARYING_SLOT_TESS_MAX <= 127);
218 
219    for (int i = 0; i < VARYING_SLOT_TESS_MAX ; ++i) {
220       vue_map->varying_to_slot[i] = -1;
221       vue_map->slot_to_varying[i] = BRW_VARYING_SLOT_PAD;
222    }
223 
224    int slot = 0;
225 
226    /* The first 8 DWords are reserved for the "Patch Header".
227     *
228     * VARYING_SLOT_TESS_LEVEL_OUTER / INNER live here, but the exact layout
229     * depends on the domain type.  They might not be in slots 0 and 1 as
230     * described here, but pretending they're separate allows us to uniquely
231     * identify them by distinct slot locations.
232     */
233    assign_vue_slot(vue_map, VARYING_SLOT_TESS_LEVEL_INNER, slot++);
234    assign_vue_slot(vue_map, VARYING_SLOT_TESS_LEVEL_OUTER, slot++);
235 
236    /* first assign per-patch varyings */
237    while (patch_slots != 0) {
238       const int varying = ffsll(patch_slots) - 1;
239       if (vue_map->varying_to_slot[varying + VARYING_SLOT_PATCH0] == -1) {
240          assign_vue_slot(vue_map, varying + VARYING_SLOT_PATCH0, slot++);
241       }
242       patch_slots &= ~BITFIELD64_BIT(varying);
243    }
244 
245    /* apparently, including the patch header... */
246    vue_map->num_per_patch_slots = slot;
247 
248    /* then assign per-vertex varyings for each vertex in our patch */
249    while (vertex_slots != 0) {
250       const int varying = ffsll(vertex_slots) - 1;
251       if (vue_map->varying_to_slot[varying] == -1) {
252          assign_vue_slot(vue_map, varying, slot++);
253       }
254       vertex_slots &= ~BITFIELD64_BIT(varying);
255    }
256 
257    vue_map->num_per_vertex_slots = slot - vue_map->num_per_patch_slots;
258    vue_map->num_pos_slots = 0;
259    vue_map->num_slots = slot;
260 }
261 
262 static const char *
varying_name(brw_varying_slot slot,gl_shader_stage stage)263 varying_name(brw_varying_slot slot, gl_shader_stage stage)
264 {
265    assume(slot < BRW_VARYING_SLOT_COUNT);
266 
267    if (slot < VARYING_SLOT_MAX)
268       return gl_varying_slot_name_for_stage((gl_varying_slot)slot, stage);
269 
270    static const char *brw_names[] = {
271       [BRW_VARYING_SLOT_PAD - VARYING_SLOT_MAX] = "BRW_VARYING_SLOT_PAD",
272    };
273 
274    return brw_names[slot - VARYING_SLOT_MAX];
275 }
276 
277 void
brw_print_vue_map(FILE * fp,const struct intel_vue_map * vue_map,gl_shader_stage stage)278 brw_print_vue_map(FILE *fp, const struct intel_vue_map *vue_map,
279                   gl_shader_stage stage)
280 {
281    if (vue_map->num_per_vertex_slots > 0 || vue_map->num_per_patch_slots > 0) {
282       fprintf(fp, "PUE map (%d slots, %d/patch, %d/vertex, %s)\n",
283               vue_map->num_slots,
284               vue_map->num_per_patch_slots,
285               vue_map->num_per_vertex_slots,
286               vue_map->separate ? "SSO" : "non-SSO");
287       for (int i = 0; i < vue_map->num_slots; i++) {
288          if (vue_map->slot_to_varying[i] >= VARYING_SLOT_PATCH0) {
289             fprintf(fp, "  [%d] VARYING_SLOT_PATCH%d\n", i,
290                     vue_map->slot_to_varying[i] - VARYING_SLOT_PATCH0);
291          } else {
292             fprintf(fp, "  [%d] %s\n", i,
293                     varying_name(vue_map->slot_to_varying[i], stage));
294          }
295       }
296    } else {
297       fprintf(fp, "VUE map (%d slots, %s)\n",
298               vue_map->num_slots, vue_map->separate ? "SSO" : "non-SSO");
299       for (int i = 0; i < vue_map->num_slots; i++) {
300          fprintf(fp, "  [%d] %s\n", i,
301                  varying_name(vue_map->slot_to_varying[i], stage));
302       }
303    }
304    fprintf(fp, "\n");
305 }
306