glsl: use better location in struct and block error messages
[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 /* 'subroutine' isnt a real qualifier. */
44 ast_type_qualifier subroutine_only;
45 subroutine_only.flags.i = 0;
46 subroutine_only.flags.q.subroutine = 1;
47 subroutine_only.flags.q.subroutine_def = 1;
48 return (this->qualifier.flags.i & ~subroutine_only.flags.i) != 0;
49 }
50
51 bool ast_type_qualifier::has_interpolation() const
52 {
53 return this->flags.q.smooth
54 || this->flags.q.flat
55 || this->flags.q.noperspective;
56 }
57
58 bool
59 ast_type_qualifier::has_layout() const
60 {
61 return this->flags.q.origin_upper_left
62 || this->flags.q.pixel_center_integer
63 || this->flags.q.depth_any
64 || this->flags.q.depth_greater
65 || this->flags.q.depth_less
66 || this->flags.q.depth_unchanged
67 || this->flags.q.std140
68 || this->flags.q.std430
69 || this->flags.q.shared
70 || this->flags.q.column_major
71 || this->flags.q.row_major
72 || this->flags.q.packed
73 || this->flags.q.explicit_location
74 || this->flags.q.explicit_index
75 || this->flags.q.explicit_binding
76 || this->flags.q.explicit_offset;
77 }
78
79 bool
80 ast_type_qualifier::has_storage() const
81 {
82 return this->flags.q.constant
83 || this->flags.q.attribute
84 || this->flags.q.varying
85 || this->flags.q.in
86 || this->flags.q.out
87 || this->flags.q.uniform
88 || this->flags.q.buffer
89 || this->flags.q.shared_storage;
90 }
91
92 bool
93 ast_type_qualifier::has_auxiliary_storage() const
94 {
95 return this->flags.q.centroid
96 || this->flags.q.sample
97 || this->flags.q.patch;
98 }
99
100 const char*
101 ast_type_qualifier::interpolation_string() const
102 {
103 if (this->flags.q.smooth)
104 return "smooth";
105 else if (this->flags.q.flat)
106 return "flat";
107 else if (this->flags.q.noperspective)
108 return "noperspective";
109 else
110 return NULL;
111 }
112
113 bool
114 ast_type_qualifier::merge_qualifier(YYLTYPE *loc,
115 _mesa_glsl_parse_state *state,
116 ast_type_qualifier q)
117 {
118 ast_type_qualifier ubo_mat_mask;
119 ubo_mat_mask.flags.i = 0;
120 ubo_mat_mask.flags.q.row_major = 1;
121 ubo_mat_mask.flags.q.column_major = 1;
122
123 ast_type_qualifier ubo_layout_mask;
124 ubo_layout_mask.flags.i = 0;
125 ubo_layout_mask.flags.q.std140 = 1;
126 ubo_layout_mask.flags.q.packed = 1;
127 ubo_layout_mask.flags.q.shared = 1;
128 ubo_layout_mask.flags.q.std430 = 1;
129
130 ast_type_qualifier ubo_binding_mask;
131 ubo_binding_mask.flags.i = 0;
132 ubo_binding_mask.flags.q.explicit_binding = 1;
133 ubo_binding_mask.flags.q.explicit_offset = 1;
134
135 ast_type_qualifier stream_layout_mask;
136 stream_layout_mask.flags.i = 0;
137 stream_layout_mask.flags.q.stream = 1;
138
139 /* Uniform block layout qualifiers get to overwrite each
140 * other (rightmost having priority), while all other
141 * qualifiers currently don't allow duplicates.
142 */
143 ast_type_qualifier allowed_duplicates_mask;
144 allowed_duplicates_mask.flags.i =
145 ubo_mat_mask.flags.i |
146 ubo_layout_mask.flags.i |
147 ubo_binding_mask.flags.i;
148
149 /* Geometry shaders can have several layout qualifiers
150 * assigning different stream values.
151 */
152 if (state->stage == MESA_SHADER_GEOMETRY)
153 allowed_duplicates_mask.flags.i |=
154 stream_layout_mask.flags.i;
155
156 if ((this->flags.i & q.flags.i & ~allowed_duplicates_mask.flags.i) != 0) {
157 _mesa_glsl_error(loc, state,
158 "duplicate layout qualifiers used");
159 return false;
160 }
161
162 if (q.flags.q.prim_type) {
163 if (this->flags.q.prim_type && this->prim_type != q.prim_type) {
164 _mesa_glsl_error(loc, state,
165 "conflicting primitive type qualifiers used");
166 return false;
167 }
168 this->prim_type = q.prim_type;
169 }
170
171 if (q.flags.q.max_vertices) {
172 if (this->flags.q.max_vertices && this->max_vertices != q.max_vertices) {
173 _mesa_glsl_error(loc, state,
174 "geometry shader set conflicting max_vertices "
175 "(%d and %d)", this->max_vertices, q.max_vertices);
176 return false;
177 }
178 this->max_vertices = q.max_vertices;
179 }
180
181 if (q.flags.q.invocations) {
182 if (this->flags.q.invocations && this->invocations != q.invocations) {
183 _mesa_glsl_error(loc, state,
184 "geometry shader set conflicting invocations "
185 "(%d and %d)", this->invocations, q.invocations);
186 return false;
187 }
188 this->invocations = q.invocations;
189 }
190
191 if (state->stage == MESA_SHADER_GEOMETRY &&
192 state->has_explicit_attrib_stream()) {
193 if (q.flags.q.stream && q.stream >= state->ctx->Const.MaxVertexStreams) {
194 _mesa_glsl_error(loc, state,
195 "`stream' value is larger than MAX_VERTEX_STREAMS - 1 "
196 "(%d > %d)",
197 q.stream, state->ctx->Const.MaxVertexStreams - 1);
198 }
199 if (this->flags.q.explicit_stream &&
200 this->stream >= state->ctx->Const.MaxVertexStreams) {
201 _mesa_glsl_error(loc, state,
202 "`stream' value is larger than MAX_VERTEX_STREAMS - 1 "
203 "(%d > %d)",
204 this->stream, state->ctx->Const.MaxVertexStreams - 1);
205 }
206
207 if (!this->flags.q.explicit_stream) {
208 if (q.flags.q.stream) {
209 this->flags.q.stream = 1;
210 this->stream = q.stream;
211 } else if (!this->flags.q.stream && this->flags.q.out) {
212 /* Assign default global stream value */
213 this->flags.q.stream = 1;
214 this->stream = state->out_qualifier->stream;
215 }
216 } else {
217 if (q.flags.q.explicit_stream) {
218 _mesa_glsl_error(loc, state,
219 "duplicate layout `stream' qualifier");
220 }
221 }
222 }
223
224 if (q.flags.q.vertices) {
225 if (this->flags.q.vertices && this->vertices != q.vertices) {
226 _mesa_glsl_error(loc, state,
227 "tessellation control shader set conflicting "
228 "vertices (%d and %d)",
229 this->vertices, q.vertices);
230 return false;
231 }
232 this->vertices = q.vertices;
233 }
234
235 if (q.flags.q.vertex_spacing) {
236 if (this->flags.q.vertex_spacing && this->vertex_spacing != q.vertex_spacing) {
237 _mesa_glsl_error(loc, state,
238 "conflicting vertex spacing used");
239 return false;
240 }
241 this->vertex_spacing = q.vertex_spacing;
242 }
243
244 if (q.flags.q.ordering) {
245 if (this->flags.q.ordering && this->ordering != q.ordering) {
246 _mesa_glsl_error(loc, state,
247 "conflicting ordering used");
248 return false;
249 }
250 this->ordering = q.ordering;
251 }
252
253 if (q.flags.q.point_mode) {
254 if (this->flags.q.point_mode && this->point_mode != q.point_mode) {
255 _mesa_glsl_error(loc, state,
256 "conflicting point mode used");
257 return false;
258 }
259 this->point_mode = q.point_mode;
260 }
261
262 if ((q.flags.i & ubo_mat_mask.flags.i) != 0)
263 this->flags.i &= ~ubo_mat_mask.flags.i;
264 if ((q.flags.i & ubo_layout_mask.flags.i) != 0)
265 this->flags.i &= ~ubo_layout_mask.flags.i;
266
267 for (int i = 0; i < 3; i++) {
268 if (q.flags.q.local_size & (1 << i)) {
269 if ((this->flags.q.local_size & (1 << i)) &&
270 this->local_size[i] != q.local_size[i]) {
271 _mesa_glsl_error(loc, state,
272 "compute shader set conflicting values for "
273 "local_size_%c (%d and %d)", 'x' + i,
274 this->local_size[i], q.local_size[i]);
275 return false;
276 }
277 this->local_size[i] = q.local_size[i];
278 }
279 }
280
281 this->flags.i |= q.flags.i;
282
283 if (q.flags.q.explicit_location)
284 this->location = q.location;
285
286 if (q.flags.q.explicit_index)
287 this->index = q.index;
288
289 if (q.flags.q.explicit_binding)
290 this->binding = q.binding;
291
292 if (q.flags.q.explicit_offset)
293 this->offset = q.offset;
294
295 if (q.precision != ast_precision_none)
296 this->precision = q.precision;
297
298 if (q.flags.q.explicit_image_format) {
299 this->image_format = q.image_format;
300 this->image_base_type = q.image_base_type;
301 }
302
303 return true;
304 }
305
306 bool
307 ast_type_qualifier::merge_out_qualifier(YYLTYPE *loc,
308 _mesa_glsl_parse_state *state,
309 ast_type_qualifier q,
310 ast_node* &node)
311 {
312 void *mem_ctx = state;
313 const bool r = this->merge_qualifier(loc, state, q);
314
315 if (state->stage == MESA_SHADER_TESS_CTRL) {
316 node = new(mem_ctx) ast_tcs_output_layout(*loc, q.vertices);
317 }
318
319 return r;
320 }
321
322 bool
323 ast_type_qualifier::merge_in_qualifier(YYLTYPE *loc,
324 _mesa_glsl_parse_state *state,
325 ast_type_qualifier q,
326 ast_node* &node)
327 {
328 void *mem_ctx = state;
329 bool create_gs_ast = false;
330 bool create_cs_ast = false;
331 ast_type_qualifier valid_in_mask;
332 valid_in_mask.flags.i = 0;
333
334 switch (state->stage) {
335 case MESA_SHADER_TESS_EVAL:
336 if (q.flags.q.prim_type) {
337 /* Make sure this is a valid input primitive type. */
338 switch (q.prim_type) {
339 case GL_TRIANGLES:
340 case GL_QUADS:
341 case GL_ISOLINES:
342 break;
343 default:
344 _mesa_glsl_error(loc, state,
345 "invalid tessellation evaluation "
346 "shader input primitive type");
347 break;
348 }
349 }
350
351 valid_in_mask.flags.q.prim_type = 1;
352 valid_in_mask.flags.q.vertex_spacing = 1;
353 valid_in_mask.flags.q.ordering = 1;
354 valid_in_mask.flags.q.point_mode = 1;
355 break;
356 case MESA_SHADER_GEOMETRY:
357 if (q.flags.q.prim_type) {
358 /* Make sure this is a valid input primitive type. */
359 switch (q.prim_type) {
360 case GL_POINTS:
361 case GL_LINES:
362 case GL_LINES_ADJACENCY:
363 case GL_TRIANGLES:
364 case GL_TRIANGLES_ADJACENCY:
365 break;
366 default:
367 _mesa_glsl_error(loc, state,
368 "invalid geometry shader input primitive type");
369 break;
370 }
371 }
372
373 create_gs_ast |=
374 q.flags.q.prim_type &&
375 !state->in_qualifier->flags.q.prim_type;
376
377 valid_in_mask.flags.q.prim_type = 1;
378 valid_in_mask.flags.q.invocations = 1;
379 break;
380 case MESA_SHADER_FRAGMENT:
381 valid_in_mask.flags.q.early_fragment_tests = 1;
382 break;
383 case MESA_SHADER_COMPUTE:
384 create_cs_ast |=
385 q.flags.q.local_size != 0 &&
386 state->in_qualifier->flags.q.local_size == 0;
387
388 valid_in_mask.flags.q.local_size = 7;
389 break;
390 default:
391 _mesa_glsl_error(loc, state,
392 "input layout qualifiers only valid in "
393 "geometry, fragment and compute shaders");
394 break;
395 }
396
397 /* Generate an error when invalid input layout qualifiers are used. */
398 if ((q.flags.i & ~valid_in_mask.flags.i) != 0) {
399 _mesa_glsl_error(loc, state,
400 "invalid input layout qualifiers used");
401 return false;
402 }
403
404 /* Input layout qualifiers can be specified multiple
405 * times in separate declarations, as long as they match.
406 */
407 if (this->flags.q.prim_type) {
408 if (q.flags.q.prim_type &&
409 this->prim_type != q.prim_type) {
410 _mesa_glsl_error(loc, state,
411 "conflicting input primitive %s specified",
412 state->stage == MESA_SHADER_GEOMETRY ?
413 "type" : "mode");
414 }
415 } else if (q.flags.q.prim_type) {
416 state->in_qualifier->flags.q.prim_type = 1;
417 state->in_qualifier->prim_type = q.prim_type;
418 }
419
420 if (this->flags.q.invocations &&
421 q.flags.q.invocations &&
422 this->invocations != q.invocations) {
423 _mesa_glsl_error(loc, state,
424 "conflicting invocations counts specified");
425 return false;
426 } else if (q.flags.q.invocations) {
427 this->flags.q.invocations = 1;
428 this->invocations = q.invocations;
429 }
430
431 if (q.flags.q.early_fragment_tests) {
432 state->fs_early_fragment_tests = true;
433 }
434
435 if (this->flags.q.vertex_spacing) {
436 if (q.flags.q.vertex_spacing &&
437 this->vertex_spacing != q.vertex_spacing) {
438 _mesa_glsl_error(loc, state,
439 "conflicting vertex spacing specified");
440 }
441 } else if (q.flags.q.vertex_spacing) {
442 this->flags.q.vertex_spacing = 1;
443 this->vertex_spacing = q.vertex_spacing;
444 }
445
446 if (this->flags.q.ordering) {
447 if (q.flags.q.ordering &&
448 this->ordering != q.ordering) {
449 _mesa_glsl_error(loc, state,
450 "conflicting ordering specified");
451 }
452 } else if (q.flags.q.ordering) {
453 this->flags.q.ordering = 1;
454 this->ordering = q.ordering;
455 }
456
457 if (this->flags.q.point_mode) {
458 if (q.flags.q.point_mode &&
459 this->point_mode != q.point_mode) {
460 _mesa_glsl_error(loc, state,
461 "conflicting point mode specified");
462 }
463 } else if (q.flags.q.point_mode) {
464 this->flags.q.point_mode = 1;
465 this->point_mode = q.point_mode;
466 }
467
468 if (create_gs_ast) {
469 node = new(mem_ctx) ast_gs_input_layout(*loc, q.prim_type);
470 } else if (create_cs_ast) {
471 /* Infer a local_size of 1 for every unspecified dimension */
472 unsigned local_size[3];
473 for (int i = 0; i < 3; i++) {
474 if (q.flags.q.local_size & (1 << i))
475 local_size[i] = q.local_size[i];
476 else
477 local_size[i] = 1;
478 }
479 node = new(mem_ctx) ast_cs_input_layout(*loc, local_size);
480 }
481
482 return true;
483 }