Merge remote-tracking branch 'origin/master' into vulkan
[mesa.git] / src / compiler / 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(_mesa_glsl_parse_state *state) 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 if (state->has_explicit_uniform_location()) {
49 subroutine_only.flags.q.explicit_index = 1;
50 }
51 return (this->qualifier.flags.i & ~subroutine_only.flags.i) != 0;
52 }
53
54 bool ast_type_qualifier::has_interpolation() const
55 {
56 return this->flags.q.smooth
57 || this->flags.q.flat
58 || this->flags.q.noperspective;
59 }
60
61 bool
62 ast_type_qualifier::has_layout() const
63 {
64 return this->flags.q.origin_upper_left
65 || this->flags.q.pixel_center_integer
66 || this->flags.q.depth_any
67 || this->flags.q.depth_greater
68 || this->flags.q.depth_less
69 || this->flags.q.depth_unchanged
70 || this->flags.q.std140
71 || this->flags.q.std430
72 || this->flags.q.shared
73 || this->flags.q.column_major
74 || this->flags.q.row_major
75 || this->flags.q.packed
76 || this->flags.q.explicit_location
77 || this->flags.q.explicit_image_format
78 || this->flags.q.explicit_index
79 || this->flags.q.explicit_binding
80 || this->flags.q.explicit_offset
81 || this->flags.q.explicit_stream;
82 }
83
84 bool
85 ast_type_qualifier::has_storage() const
86 {
87 return this->flags.q.constant
88 || this->flags.q.attribute
89 || this->flags.q.varying
90 || this->flags.q.in
91 || this->flags.q.out
92 || this->flags.q.uniform
93 || this->flags.q.buffer
94 || this->flags.q.shared_storage;
95 }
96
97 bool
98 ast_type_qualifier::has_auxiliary_storage() const
99 {
100 return this->flags.q.centroid
101 || this->flags.q.sample
102 || this->flags.q.patch;
103 }
104
105 /**
106 * This function merges both duplicate identifies within a single layout and
107 * multiple layout qualifiers on a single variable declaration. The
108 * is_single_layout_merge param is used differentiate between the two.
109 */
110 bool
111 ast_type_qualifier::merge_qualifier(YYLTYPE *loc,
112 _mesa_glsl_parse_state *state,
113 const ast_type_qualifier &q,
114 bool is_single_layout_merge)
115 {
116 ast_type_qualifier ubo_mat_mask;
117 ubo_mat_mask.flags.i = 0;
118 ubo_mat_mask.flags.q.row_major = 1;
119 ubo_mat_mask.flags.q.column_major = 1;
120
121 ast_type_qualifier ubo_layout_mask;
122 ubo_layout_mask.flags.i = 0;
123 ubo_layout_mask.flags.q.std140 = 1;
124 ubo_layout_mask.flags.q.packed = 1;
125 ubo_layout_mask.flags.q.shared = 1;
126 ubo_layout_mask.flags.q.std430 = 1;
127
128 ast_type_qualifier ubo_binding_mask;
129 ubo_binding_mask.flags.i = 0;
130 ubo_binding_mask.flags.q.explicit_binding = 1;
131 ubo_binding_mask.flags.q.explicit_offset = 1;
132
133 ast_type_qualifier stream_layout_mask;
134 stream_layout_mask.flags.i = 0;
135 stream_layout_mask.flags.q.stream = 1;
136
137 /* Uniform block layout qualifiers get to overwrite each
138 * other (rightmost having priority), while all other
139 * qualifiers currently don't allow duplicates.
140 */
141 ast_type_qualifier allowed_duplicates_mask;
142 allowed_duplicates_mask.flags.i =
143 ubo_mat_mask.flags.i |
144 ubo_layout_mask.flags.i |
145 ubo_binding_mask.flags.i;
146
147 /* Geometry shaders can have several layout qualifiers
148 * assigning different stream values.
149 */
150 if (state->stage == MESA_SHADER_GEOMETRY)
151 allowed_duplicates_mask.flags.i |=
152 stream_layout_mask.flags.i;
153
154 if (is_single_layout_merge && !state->has_enhanced_layouts() &&
155 (this->flags.i & q.flags.i & ~allowed_duplicates_mask.flags.i) != 0) {
156 _mesa_glsl_error(loc, state,
157 "duplicate layout qualifiers used");
158 return false;
159 }
160
161 if (q.flags.q.prim_type) {
162 if (this->flags.q.prim_type && this->prim_type != q.prim_type) {
163 _mesa_glsl_error(loc, state,
164 "conflicting primitive type qualifiers used");
165 return false;
166 }
167 this->prim_type = q.prim_type;
168 }
169
170 if (q.flags.q.max_vertices) {
171 if (this->max_vertices) {
172 this->max_vertices->merge_qualifier(q.max_vertices);
173 } else {
174 this->max_vertices = q.max_vertices;
175 }
176 }
177
178 if (q.flags.q.subroutine_def) {
179 if (this->flags.q.subroutine_def) {
180 _mesa_glsl_error(loc, state,
181 "conflicting subroutine qualifiers used");
182 } else {
183 this->subroutine_list = q.subroutine_list;
184 }
185 }
186
187 if (q.flags.q.invocations) {
188 if (this->invocations) {
189 this->invocations->merge_qualifier(q.invocations);
190 } else {
191 this->invocations = q.invocations;
192 }
193 }
194
195 if (state->stage == MESA_SHADER_GEOMETRY &&
196 state->has_explicit_attrib_stream()) {
197 if (!this->flags.q.explicit_stream) {
198 if (q.flags.q.stream) {
199 this->flags.q.stream = 1;
200 this->stream = q.stream;
201 } else if (!this->flags.q.stream && this->flags.q.out) {
202 /* Assign default global stream value */
203 this->flags.q.stream = 1;
204 this->stream = state->out_qualifier->stream;
205 }
206 }
207 }
208
209 if (q.flags.q.vertices) {
210 if (this->vertices) {
211 this->vertices->merge_qualifier(q.vertices);
212 } else {
213 this->vertices = q.vertices;
214 }
215 }
216
217 if (q.flags.q.vertex_spacing) {
218 if (this->flags.q.vertex_spacing && this->vertex_spacing != q.vertex_spacing) {
219 _mesa_glsl_error(loc, state,
220 "conflicting vertex spacing used");
221 return false;
222 }
223 this->vertex_spacing = q.vertex_spacing;
224 }
225
226 if (q.flags.q.ordering) {
227 if (this->flags.q.ordering && this->ordering != q.ordering) {
228 _mesa_glsl_error(loc, state,
229 "conflicting ordering used");
230 return false;
231 }
232 this->ordering = q.ordering;
233 }
234
235 if (q.flags.q.point_mode) {
236 if (this->flags.q.point_mode && this->point_mode != q.point_mode) {
237 _mesa_glsl_error(loc, state,
238 "conflicting point mode used");
239 return false;
240 }
241 this->point_mode = q.point_mode;
242 }
243
244 if ((q.flags.i & ubo_mat_mask.flags.i) != 0)
245 this->flags.i &= ~ubo_mat_mask.flags.i;
246 if ((q.flags.i & ubo_layout_mask.flags.i) != 0)
247 this->flags.i &= ~ubo_layout_mask.flags.i;
248
249 for (int i = 0; i < 3; i++) {
250 if (q.flags.q.local_size & (1 << i)) {
251 if (this->local_size[i]) {
252 this->local_size[i]->merge_qualifier(q.local_size[i]);
253 } else {
254 this->local_size[i] = q.local_size[i];
255 }
256 }
257 }
258
259 this->flags.i |= q.flags.i;
260
261 if (q.flags.q.explicit_location)
262 this->location = q.location;
263
264 if (q.flags.q.explicit_index)
265 this->index = q.index;
266
267 if (q.flags.q.explicit_binding)
268 this->binding = q.binding;
269
270 if (q.flags.q.explicit_offset)
271 this->offset = q.offset;
272
273 if (q.precision != ast_precision_none)
274 this->precision = q.precision;
275
276 if (q.flags.q.explicit_image_format) {
277 this->image_format = q.image_format;
278 this->image_base_type = q.image_base_type;
279 }
280
281 return true;
282 }
283
284 bool
285 ast_type_qualifier::merge_out_qualifier(YYLTYPE *loc,
286 _mesa_glsl_parse_state *state,
287 const ast_type_qualifier &q,
288 ast_node* &node, bool create_node)
289 {
290 void *mem_ctx = state;
291 const bool r = this->merge_qualifier(loc, state, q, false);
292
293 if (state->stage == MESA_SHADER_GEOMETRY) {
294 if (q.flags.q.prim_type) {
295 /* Make sure this is a valid output primitive type. */
296 switch (q.prim_type) {
297 case GL_POINTS:
298 case GL_LINE_STRIP:
299 case GL_TRIANGLE_STRIP:
300 break;
301 default:
302 _mesa_glsl_error(loc, state, "invalid geometry shader output "
303 "primitive type");
304 break;
305 }
306 }
307
308 /* Allow future assigments of global out's stream id value */
309 this->flags.q.explicit_stream = 0;
310 } else if (state->stage == MESA_SHADER_TESS_CTRL) {
311 if (create_node) {
312 node = new(mem_ctx) ast_tcs_output_layout(*loc);
313 }
314 } else {
315 _mesa_glsl_error(loc, state, "out layout qualifiers only valid in "
316 "tessellation control or geometry shaders");
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 const ast_type_qualifier &q,
326 ast_node* &node, bool create_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 (q.flags.q.invocations) {
421 this->flags.q.invocations = 1;
422 if (this->invocations) {
423 this->invocations->merge_qualifier(q.invocations);
424 } else {
425 this->invocations = q.invocations;
426 }
427 }
428
429 if (q.flags.q.early_fragment_tests) {
430 state->fs_early_fragment_tests = true;
431 }
432
433 if (this->flags.q.vertex_spacing) {
434 if (q.flags.q.vertex_spacing &&
435 this->vertex_spacing != q.vertex_spacing) {
436 _mesa_glsl_error(loc, state,
437 "conflicting vertex spacing specified");
438 }
439 } else if (q.flags.q.vertex_spacing) {
440 this->flags.q.vertex_spacing = 1;
441 this->vertex_spacing = q.vertex_spacing;
442 }
443
444 if (this->flags.q.ordering) {
445 if (q.flags.q.ordering &&
446 this->ordering != q.ordering) {
447 _mesa_glsl_error(loc, state,
448 "conflicting ordering specified");
449 }
450 } else if (q.flags.q.ordering) {
451 this->flags.q.ordering = 1;
452 this->ordering = q.ordering;
453 }
454
455 if (this->flags.q.point_mode) {
456 if (q.flags.q.point_mode &&
457 this->point_mode != q.point_mode) {
458 _mesa_glsl_error(loc, state,
459 "conflicting point mode specified");
460 }
461 } else if (q.flags.q.point_mode) {
462 this->flags.q.point_mode = 1;
463 this->point_mode = q.point_mode;
464 }
465
466 if (create_node) {
467 if (create_gs_ast) {
468 node = new(mem_ctx) ast_gs_input_layout(*loc, q.prim_type);
469 } else if (create_cs_ast) {
470 node = new(mem_ctx) ast_cs_input_layout(*loc, q.local_size);
471 }
472 }
473
474 return true;
475 }
476
477 bool
478 ast_layout_expression::process_qualifier_constant(struct _mesa_glsl_parse_state *state,
479 const char *qual_indentifier,
480 unsigned *value,
481 bool can_be_zero)
482 {
483 int min_value = 0;
484 bool first_pass = true;
485 *value = 0;
486
487 if (!can_be_zero)
488 min_value = 1;
489
490 for (exec_node *node = layout_const_expressions.head;
491 !node->is_tail_sentinel(); node = node->next) {
492
493 exec_list dummy_instructions;
494 ast_node *const_expression = exec_node_data(ast_node, node, link);
495
496 ir_rvalue *const ir = const_expression->hir(&dummy_instructions, state);
497
498 ir_constant *const const_int = ir->constant_expression_value();
499 if (const_int == NULL || !const_int->type->is_integer()) {
500 YYLTYPE loc = const_expression->get_location();
501 _mesa_glsl_error(&loc, state, "%s must be an integral constant "
502 "expression", qual_indentifier);
503 return false;
504 }
505
506 if (const_int->value.i[0] < min_value) {
507 YYLTYPE loc = const_expression->get_location();
508 _mesa_glsl_error(&loc, state, "%s layout qualifier is invalid "
509 "(%d < %d)", qual_indentifier,
510 const_int->value.i[0], min_value);
511 return false;
512 }
513
514 if (!first_pass && *value != const_int->value.u[0]) {
515 YYLTYPE loc = const_expression->get_location();
516 _mesa_glsl_error(&loc, state, "%s layout qualifier does not "
517 "match previous declaration (%d vs %d)",
518 qual_indentifier, *value, const_int->value.i[0]);
519 return false;
520 } else {
521 first_pass = false;
522 *value = const_int->value.u[0];
523 }
524
525 /* If the location is const (and we've verified that
526 * it is) then no instructions should have been emitted
527 * when we converted it to HIR. If they were emitted,
528 * then either the location isn't const after all, or
529 * we are emitting unnecessary instructions.
530 */
531 assert(dummy_instructions.is_empty());
532 }
533
534 return true;
535 }