xref: /aosp_15_r20/external/mesa3d/src/compiler/glsl/gl_nir_link_varyings.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2012 Intel Corporation
3  * Copyright © 2022 Valve Corporation
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 #ifndef GLSL_LINK_VARYINGS_H
26 #define GLSL_LINK_VARYINGS_H
27 
28 /**
29  * Linker functions related specifically to linking varyings between shader
30  * stages.
31  */
32 
33 
34 #include "util/glheader.h"
35 #include "program/prog_parameter.h"
36 #include "util/bitset.h"
37 
38 #include "nir.h"
39 
40 struct gl_shader_program;
41 struct gl_shader_stage;
42 struct gl_shader;
43 struct gl_linked_shader;
44 struct gl_type;
45 
46 
47 /**
48  * Data structure describing a varying which is available for use in transform
49  * feedback.
50  *
51  * For example, if the vertex shader contains:
52  *
53  *     struct S {
54  *       vec4 foo;
55  *       float[3] bar;
56  *     };
57  *
58  *     varying S[2] v;
59  *
60  * Then there would be tfeedback_candidate objects corresponding to the
61  * following varyings:
62  *
63  *     v[0].foo
64  *     v[0].bar
65  *     v[1].foo
66  *     v[1].bar
67  */
68 struct tfeedback_candidate
69 {
70    /**
71     * Toplevel variable containing this varying.  In the above example, this
72     * would point to the declaration of the varying v.
73     */
74    nir_variable *toplevel_var;
75 
76    /**
77     * Type of this varying.  In the above example, this would point to the
78     * glsl_type for "vec4" or "float[3]".
79     */
80    const struct glsl_type *type;
81 
82    /**
83     * Offset within the toplevel variable where this varying occurs.
84     * Counted in floats.
85     */
86    unsigned struct_offset_floats;
87 
88    /**
89     * Offset within the xfb with respect to alignment requirements.
90     * Counted in floats.
91     */
92    unsigned xfb_offset_floats;
93 
94    /* Used to match varyings and update toplevel_var pointer after NIR
95     * optimisations have been performed.
96     */
97    unsigned initial_location;
98    unsigned initial_location_frac;
99 };
100 
101 enum lowered_builtin_array_var {
102    none,
103    clip_distance,
104    cull_distance,
105 };
106 
107 /**
108  * Data structure tracking information about a transform feedback declaration
109  * during linking.
110  */
111 struct xfb_decl
112 {
113    /**
114     * The name that was supplied to glTransformFeedbackVaryings.  Used for
115     * error reporting and glGetTransformFeedbackVarying().
116     */
117    const char *orig_name;
118 
119    /**
120     * The name of the variable, parsed from orig_name.
121     */
122    const char *var_name;
123 
124    /**
125     * True if the declaration in orig_name represents an array.
126     */
127    bool is_subscripted;
128 
129    /**
130     * If is_subscripted is true, the subscript that was specified in orig_name.
131     */
132    unsigned array_subscript;
133 
134    /**
135     * Non-zero if the variable is gl_ClipDistance and the driver lowers it to
136     * gl_*MESA.
137     */
138    enum lowered_builtin_array_var lowered_builtin_array_variable;
139 
140    /**
141     * The vertex shader output location that the linker assigned for this
142     * variable.  -1 if a location hasn't been assigned yet.
143     */
144    int location;
145 
146    /**
147     * Used to store the buffer assigned by xfb_buffer.
148     */
149    unsigned buffer;
150 
151    /**
152     * Used to store the offset assigned by xfb_offset.
153     */
154    unsigned offset;
155 
156    /**
157     * If non-zero, then this variable may be packed along with other variables
158     * into a single varying slot, so this offset should be applied when
159     * accessing components.  For example, an offset of 1 means that the x
160     * component of this variable is actually stored in component y of the
161     * location specified by \c location.
162     *
163     * Only valid if location != -1.
164     */
165    unsigned location_frac;
166 
167    /**
168     * If location != -1, the number of vector elements in this variable, or 1
169     * if this variable is a scalar.
170     */
171    unsigned vector_elements;
172 
173    /**
174     * If location != -1, the number of matrix columns in this variable, or 1
175     * if this variable is not a matrix.
176     */
177    unsigned matrix_columns;
178 
179    /** Type of the varying returned by glGetTransformFeedbackVarying() */
180    GLenum type;
181 
182    /**
183     * If location != -1, the size that should be returned by
184     * glGetTransformFeedbackVarying().
185     */
186    unsigned size;
187 
188    /**
189     * How many components to skip. If non-zero, this is
190     * gl_SkipComponents{1,2,3,4} from ARB_transform_feedback3.
191     */
192    unsigned skip_components;
193 
194    /**
195     * Whether this is gl_NextBuffer from ARB_transform_feedback3.
196     */
197    bool next_buffer_separator;
198 
199    /**
200     * If find_candidate() has been called, pointer to the tfeedback_candidate
201     * data structure that was found.  Otherwise NULL.
202     */
203    struct tfeedback_candidate *matched_candidate;
204 
205    /**
206     * StreamId assigned to this varying (defaults to 0). Can only be set to
207     * values other than 0 in geometry shaders that use the stream layout
208     * modifier. Accepted values must be in the range [0, MAX_VERTEX_STREAMS-1].
209     */
210    unsigned stream_id;
211 };
212 
213 static inline bool
xfb_decl_is_varying(const struct xfb_decl * xfb_decl)214 xfb_decl_is_varying(const struct xfb_decl *xfb_decl)
215 {
216    return !xfb_decl->next_buffer_separator && !xfb_decl->skip_components;
217 }
218 
219 void
220 resize_tes_inputs(const struct gl_constants *consts,
221                   struct gl_shader_program *prog);
222 
223 void
224 set_geom_shader_input_array_size(struct gl_shader_program *prog);
225 
226 bool
227 gl_assign_attribute_or_color_locations(const struct gl_constants *consts,
228                                        struct gl_shader_program *prog);
229 
230 void
231 gl_nir_validate_first_and_last_interface_explicit_locations(const struct gl_constants *consts,
232                                                             struct gl_shader_program *prog,
233                                                             gl_shader_stage first_stage,
234                                                             gl_shader_stage last_stage);
235 
236 void
237 gl_nir_cross_validate_outputs_to_inputs(const struct gl_constants *consts,
238                                         struct gl_shader_program *prog,
239                                         struct gl_linked_shader *producer,
240                                         struct gl_linked_shader *consumer);
241 
242 #endif /* GLSL_LINK_VARYINGS_H */
243