vc4: Store reloc pointers as pointers, not offsets.
[mesa.git] / src / glsl / ast_type.cpp
1 /*
2 * Copyright © 2010 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
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "ast.h"
25
26 void
27 ast_type_specifier::print(void) const
28 {
29 if (structure) {
30 structure->print();
31 } else {
32 printf("%s ", type_name);
33 }
34
35 if (array_specifier) {
36 array_specifier->print();
37 }
38 }
39
40 bool
41 ast_fully_specified_type::has_qualifiers() const
42 {
43 return this->qualifier.flags.i != 0;
44 }
45
46 bool ast_type_qualifier::has_interpolation() const
47 {
48 return this->flags.q.smooth
49 || this->flags.q.flat
50 || this->flags.q.noperspective;
51 }
52
53 bool
54 ast_type_qualifier::has_layout() const
55 {
56 return this->flags.q.origin_upper_left
57 || this->flags.q.pixel_center_integer
58 || this->flags.q.depth_any
59 || this->flags.q.depth_greater
60 || this->flags.q.depth_less
61 || this->flags.q.depth_unchanged
62 || this->flags.q.std140
63 || this->flags.q.shared
64 || this->flags.q.column_major
65 || this->flags.q.row_major
66 || this->flags.q.packed
67 || this->flags.q.explicit_location
68 || this->flags.q.explicit_index
69 || this->flags.q.explicit_binding
70 || this->flags.q.explicit_offset;
71 }
72
73 bool
74 ast_type_qualifier::has_storage() const
75 {
76 return this->flags.q.constant
77 || this->flags.q.attribute
78 || this->flags.q.varying
79 || this->flags.q.in
80 || this->flags.q.out
81 || this->flags.q.uniform
82 || this->flags.q.buffer;
83 }
84
85 bool
86 ast_type_qualifier::has_auxiliary_storage() const
87 {
88 return this->flags.q.centroid
89 || this->flags.q.sample;
90 }
91
92 const char*
93 ast_type_qualifier::interpolation_string() const
94 {
95 if (this->flags.q.smooth)
96 return "smooth";
97 else if (this->flags.q.flat)
98 return "flat";
99 else if (this->flags.q.noperspective)
100 return "noperspective";
101 else
102 return NULL;
103 }
104
105 bool
106 ast_type_qualifier::merge_qualifier(YYLTYPE *loc,
107 _mesa_glsl_parse_state *state,
108 ast_type_qualifier q)
109 {
110 ast_type_qualifier ubo_mat_mask;
111 ubo_mat_mask.flags.i = 0;
112 ubo_mat_mask.flags.q.row_major = 1;
113 ubo_mat_mask.flags.q.column_major = 1;
114
115 ast_type_qualifier ubo_layout_mask;
116 ubo_layout_mask.flags.i = 0;
117 ubo_layout_mask.flags.q.std140 = 1;
118 ubo_layout_mask.flags.q.packed = 1;
119 ubo_layout_mask.flags.q.shared = 1;
120
121 ast_type_qualifier ubo_binding_mask;
122 ubo_binding_mask.flags.i = 0;
123 ubo_binding_mask.flags.q.explicit_binding = 1;
124 ubo_binding_mask.flags.q.explicit_offset = 1;
125
126 ast_type_qualifier stream_layout_mask;
127 stream_layout_mask.flags.i = 0;
128 stream_layout_mask.flags.q.stream = 1;
129
130 /* Uniform block layout qualifiers get to overwrite each
131 * other (rightmost having priority), while all other
132 * qualifiers currently don't allow duplicates.
133 */
134 ast_type_qualifier allowed_duplicates_mask;
135 allowed_duplicates_mask.flags.i =
136 ubo_mat_mask.flags.i |
137 ubo_layout_mask.flags.i |
138 ubo_binding_mask.flags.i;
139
140 /* Geometry shaders can have several layout qualifiers
141 * assigning different stream values.
142 */
143 if (state->stage == MESA_SHADER_GEOMETRY)
144 allowed_duplicates_mask.flags.i |=
145 stream_layout_mask.flags.i;
146
147 if ((this->flags.i & q.flags.i & ~allowed_duplicates_mask.flags.i) != 0) {
148 _mesa_glsl_error(loc, state,
149 "duplicate layout qualifiers used");
150 return false;
151 }
152
153 if (q.flags.q.prim_type) {
154 if (this->flags.q.prim_type && this->prim_type != q.prim_type) {
155 _mesa_glsl_error(loc, state,
156 "conflicting primitive type qualifiers used");
157 return false;
158 }
159 this->prim_type = q.prim_type;
160 }
161
162 if (q.flags.q.max_vertices) {
163 if (this->flags.q.max_vertices && this->max_vertices != q.max_vertices) {
164 _mesa_glsl_error(loc, state,
165 "geometry shader set conflicting max_vertices "
166 "(%d and %d)", this->max_vertices, q.max_vertices);
167 return false;
168 }
169 this->max_vertices = q.max_vertices;
170 }
171
172 if (q.flags.q.invocations) {
173 if (this->flags.q.invocations && this->invocations != q.invocations) {
174 _mesa_glsl_error(loc, state,
175 "geometry shader set conflicting invocations "
176 "(%d and %d)", this->invocations, q.invocations);
177 return false;
178 }
179 this->invocations = q.invocations;
180 }
181
182 if (state->stage == MESA_SHADER_GEOMETRY &&
183 state->has_explicit_attrib_stream()) {
184 if (q.flags.q.stream && q.stream >= state->ctx->Const.MaxVertexStreams) {
185 _mesa_glsl_error(loc, state,
186 "`stream' value is larger than MAX_VERTEX_STREAMS - 1 "
187 "(%d > %d)",
188 q.stream, state->ctx->Const.MaxVertexStreams - 1);
189 }
190 if (this->flags.q.explicit_stream &&
191 this->stream >= state->ctx->Const.MaxVertexStreams) {
192 _mesa_glsl_error(loc, state,
193 "`stream' value is larger than MAX_VERTEX_STREAMS - 1 "
194 "(%d > %d)",
195 this->stream, state->ctx->Const.MaxVertexStreams - 1);
196 }
197
198 if (!this->flags.q.explicit_stream) {
199 if (q.flags.q.stream) {
200 this->flags.q.stream = 1;
201 this->stream = q.stream;
202 } else if (!this->flags.q.stream && this->flags.q.out) {
203 /* Assign default global stream value */
204 this->flags.q.stream = 1;
205 this->stream = state->out_qualifier->stream;
206 }
207 } else {
208 if (q.flags.q.explicit_stream) {
209 _mesa_glsl_error(loc, state,
210 "duplicate layout `stream' qualifier");
211 }
212 }
213 }
214
215 if ((q.flags.i & ubo_mat_mask.flags.i) != 0)
216 this->flags.i &= ~ubo_mat_mask.flags.i;
217 if ((q.flags.i & ubo_layout_mask.flags.i) != 0)
218 this->flags.i &= ~ubo_layout_mask.flags.i;
219
220 for (int i = 0; i < 3; i++) {
221 if (q.flags.q.local_size & (1 << i)) {
222 if ((this->flags.q.local_size & (1 << i)) &&
223 this->local_size[i] != q.local_size[i]) {
224 _mesa_glsl_error(loc, state,
225 "compute shader set conflicting values for "
226 "local_size_%c (%d and %d)", 'x' + i,
227 this->local_size[i], q.local_size[i]);
228 return false;
229 }
230 this->local_size[i] = q.local_size[i];
231 }
232 }
233
234 this->flags.i |= q.flags.i;
235
236 if (q.flags.q.explicit_location)
237 this->location = q.location;
238
239 if (q.flags.q.explicit_index)
240 this->index = q.index;
241
242 if (q.flags.q.explicit_binding)
243 this->binding = q.binding;
244
245 if (q.flags.q.explicit_offset)
246 this->offset = q.offset;
247
248 if (q.precision != ast_precision_none)
249 this->precision = q.precision;
250
251 if (q.flags.q.explicit_image_format) {
252 this->image_format = q.image_format;
253 this->image_base_type = q.image_base_type;
254 }
255
256 return true;
257 }
258
259 bool
260 ast_type_qualifier::merge_in_qualifier(YYLTYPE *loc,
261 _mesa_glsl_parse_state *state,
262 ast_type_qualifier q,
263 ast_node* &node)
264 {
265 void *mem_ctx = state;
266 bool create_gs_ast = false;
267 bool create_cs_ast = false;
268 ast_type_qualifier valid_in_mask;
269 valid_in_mask.flags.i = 0;
270
271 switch (state->stage) {
272 case MESA_SHADER_GEOMETRY:
273 if (q.flags.q.prim_type) {
274 /* Make sure this is a valid input primitive type. */
275 switch (q.prim_type) {
276 case GL_POINTS:
277 case GL_LINES:
278 case GL_LINES_ADJACENCY:
279 case GL_TRIANGLES:
280 case GL_TRIANGLES_ADJACENCY:
281 break;
282 default:
283 _mesa_glsl_error(loc, state,
284 "invalid geometry shader input primitive type");
285 break;
286 }
287 }
288
289 create_gs_ast |=
290 q.flags.q.prim_type &&
291 !state->in_qualifier->flags.q.prim_type;
292
293 valid_in_mask.flags.q.prim_type = 1;
294 valid_in_mask.flags.q.invocations = 1;
295 break;
296 case MESA_SHADER_FRAGMENT:
297 valid_in_mask.flags.q.early_fragment_tests = 1;
298 break;
299 case MESA_SHADER_COMPUTE:
300 create_cs_ast |=
301 q.flags.q.local_size != 0 &&
302 state->in_qualifier->flags.q.local_size == 0;
303
304 valid_in_mask.flags.q.local_size = 7;
305 break;
306 default:
307 _mesa_glsl_error(loc, state,
308 "input layout qualifiers only valid in "
309 "geometry, fragment and compute shaders");
310 break;
311 }
312
313 /* Generate an error when invalid input layout qualifiers are used. */
314 if ((q.flags.i & ~valid_in_mask.flags.i) != 0) {
315 _mesa_glsl_error(loc, state,
316 "invalid input layout qualifiers used");
317 return false;
318 }
319
320 /* Input layout qualifiers can be specified multiple
321 * times in separate declarations, as long as they match.
322 */
323 if (this->flags.q.prim_type) {
324 if (q.flags.q.prim_type &&
325 this->prim_type != q.prim_type) {
326 _mesa_glsl_error(loc, state,
327 "conflicting input primitive types specified");
328 }
329 } else if (q.flags.q.prim_type) {
330 state->in_qualifier->flags.q.prim_type = 1;
331 state->in_qualifier->prim_type = q.prim_type;
332 }
333
334 if (this->flags.q.invocations &&
335 q.flags.q.invocations &&
336 this->invocations != q.invocations) {
337 _mesa_glsl_error(loc, state,
338 "conflicting invocations counts specified");
339 return false;
340 } else if (q.flags.q.invocations) {
341 this->flags.q.invocations = 1;
342 this->invocations = q.invocations;
343 }
344
345 if (q.flags.q.early_fragment_tests) {
346 state->fs_early_fragment_tests = true;
347 }
348
349 if (create_gs_ast) {
350 node = new(mem_ctx) ast_gs_input_layout(*loc, q.prim_type);
351 } else if (create_cs_ast) {
352 /* Infer a local_size of 1 for every unspecified dimension */
353 unsigned local_size[3];
354 for (int i = 0; i < 3; i++) {
355 if (q.flags.q.local_size & (1 << i))
356 local_size[i] = q.local_size[i];
357 else
358 local_size[i] = 1;
359 }
360 node = new(mem_ctx) ast_cs_input_layout(*loc, local_size);
361 }
362
363 return true;
364 }