glsl: move to compiler/
[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 const char*
106 ast_type_qualifier::interpolation_string() const
107 {
108 if (this->flags.q.smooth)
109 return "smooth";
110 else if (this->flags.q.flat)
111 return "flat";
112 else if (this->flags.q.noperspective)
113 return "noperspective";
114 else
115 return NULL;
116 }
117
118 /**
119 * This function merges both duplicate identifies within a single layout and
120 * multiple layout qualifiers on a single variable declaration. The
121 * is_single_layout_merge param is used differentiate between the two.
122 */
123 bool
124 ast_type_qualifier::merge_qualifier(YYLTYPE *loc,
125 _mesa_glsl_parse_state *state,
126 const ast_type_qualifier &q,
127 bool is_single_layout_merge)
128 {
129 ast_type_qualifier ubo_mat_mask;
130 ubo_mat_mask.flags.i = 0;
131 ubo_mat_mask.flags.q.row_major = 1;
132 ubo_mat_mask.flags.q.column_major = 1;
133
134 ast_type_qualifier ubo_layout_mask;
135 ubo_layout_mask.flags.i = 0;
136 ubo_layout_mask.flags.q.std140 = 1;
137 ubo_layout_mask.flags.q.packed = 1;
138 ubo_layout_mask.flags.q.shared = 1;
139 ubo_layout_mask.flags.q.std430 = 1;
140
141 ast_type_qualifier ubo_binding_mask;
142 ubo_binding_mask.flags.i = 0;
143 ubo_binding_mask.flags.q.explicit_binding = 1;
144 ubo_binding_mask.flags.q.explicit_offset = 1;
145
146 ast_type_qualifier stream_layout_mask;
147 stream_layout_mask.flags.i = 0;
148 stream_layout_mask.flags.q.stream = 1;
149
150 /* Uniform block layout qualifiers get to overwrite each
151 * other (rightmost having priority), while all other
152 * qualifiers currently don't allow duplicates.
153 */
154 ast_type_qualifier allowed_duplicates_mask;
155 allowed_duplicates_mask.flags.i =
156 ubo_mat_mask.flags.i |
157 ubo_layout_mask.flags.i |
158 ubo_binding_mask.flags.i;
159
160 /* Geometry shaders can have several layout qualifiers
161 * assigning different stream values.
162 */
163 if (state->stage == MESA_SHADER_GEOMETRY)
164 allowed_duplicates_mask.flags.i |=
165 stream_layout_mask.flags.i;
166
167 if (is_single_layout_merge && !state->has_enhanced_layouts() &&
168 (this->flags.i & q.flags.i & ~allowed_duplicates_mask.flags.i) != 0) {
169 _mesa_glsl_error(loc, state,
170 "duplicate layout qualifiers used");
171 return false;
172 }
173
174 if (q.flags.q.prim_type) {
175 if (this->flags.q.prim_type && this->prim_type != q.prim_type) {
176 _mesa_glsl_error(loc, state,
177 "conflicting primitive type qualifiers used");
178 return false;
179 }
180 this->prim_type = q.prim_type;
181 }
182
183 if (q.flags.q.max_vertices) {
184 if (this->max_vertices) {
185 this->max_vertices->merge_qualifier(q.max_vertices);
186 } else {
187 this->max_vertices = q.max_vertices;
188 }
189 }
190
191 if (q.flags.q.subroutine_def) {
192 if (this->flags.q.subroutine_def) {
193 _mesa_glsl_error(loc, state,
194 "conflicting subroutine qualifiers used");
195 } else {
196 this->subroutine_list = q.subroutine_list;
197 }
198 }
199
200 if (q.flags.q.invocations) {
201 if (this->invocations) {
202 this->invocations->merge_qualifier(q.invocations);
203 } else {
204 this->invocations = q.invocations;
205 }
206 }
207
208 if (state->stage == MESA_SHADER_GEOMETRY &&
209 state->has_explicit_attrib_stream()) {
210 if (!this->flags.q.explicit_stream) {
211 if (q.flags.q.stream) {
212 this->flags.q.stream = 1;
213 this->stream = q.stream;
214 } else if (!this->flags.q.stream && this->flags.q.out) {
215 /* Assign default global stream value */
216 this->flags.q.stream = 1;
217 this->stream = state->out_qualifier->stream;
218 }
219 }
220 }
221
222 if (q.flags.q.vertices) {
223 if (this->vertices) {
224 this->vertices->merge_qualifier(q.vertices);
225 } else {
226 this->vertices = q.vertices;
227 }
228 }
229
230 if (q.flags.q.vertex_spacing) {
231 if (this->flags.q.vertex_spacing && this->vertex_spacing != q.vertex_spacing) {
232 _mesa_glsl_error(loc, state,
233 "conflicting vertex spacing used");
234 return false;
235 }
236 this->vertex_spacing = q.vertex_spacing;
237 }
238
239 if (q.flags.q.ordering) {
240 if (this->flags.q.ordering && this->ordering != q.ordering) {
241 _mesa_glsl_error(loc, state,
242 "conflicting ordering used");
243 return false;
244 }
245 this->ordering = q.ordering;
246 }
247
248 if (q.flags.q.point_mode) {
249 if (this->flags.q.point_mode && this->point_mode != q.point_mode) {
250 _mesa_glsl_error(loc, state,
251 "conflicting point mode used");
252 return false;
253 }
254 this->point_mode = q.point_mode;
255 }
256
257 if ((q.flags.i & ubo_mat_mask.flags.i) != 0)
258 this->flags.i &= ~ubo_mat_mask.flags.i;
259 if ((q.flags.i & ubo_layout_mask.flags.i) != 0)
260 this->flags.i &= ~ubo_layout_mask.flags.i;
261
262 for (int i = 0; i < 3; i++) {
263 if (q.flags.q.local_size & (1 << i)) {
264 if (this->local_size[i]) {
265 this->local_size[i]->merge_qualifier(q.local_size[i]);
266 } else {
267 this->local_size[i] = q.local_size[i];
268 }
269 }
270 }
271
272 this->flags.i |= q.flags.i;
273
274 if (q.flags.q.explicit_location)
275 this->location = q.location;
276
277 if (q.flags.q.explicit_index)
278 this->index = q.index;
279
280 if (q.flags.q.explicit_binding)
281 this->binding = q.binding;
282
283 if (q.flags.q.explicit_offset)
284 this->offset = q.offset;
285
286 if (q.precision != ast_precision_none)
287 this->precision = q.precision;
288
289 if (q.flags.q.explicit_image_format) {
290 this->image_format = q.image_format;
291 this->image_base_type = q.image_base_type;
292 }
293
294 return true;
295 }
296
297 bool
298 ast_type_qualifier::merge_out_qualifier(YYLTYPE *loc,
299 _mesa_glsl_parse_state *state,
300 const ast_type_qualifier &q,
301 ast_node* &node, bool create_node)
302 {
303 void *mem_ctx = state;
304 const bool r = this->merge_qualifier(loc, state, q, false);
305
306 if (state->stage == MESA_SHADER_GEOMETRY) {
307 if (q.flags.q.prim_type) {
308 /* Make sure this is a valid output primitive type. */
309 switch (q.prim_type) {
310 case GL_POINTS:
311 case GL_LINE_STRIP:
312 case GL_TRIANGLE_STRIP:
313 break;
314 default:
315 _mesa_glsl_error(loc, state, "invalid geometry shader output "
316 "primitive type");
317 break;
318 }
319 }
320
321 /* Allow future assigments of global out's stream id value */
322 this->flags.q.explicit_stream = 0;
323 } else if (state->stage == MESA_SHADER_TESS_CTRL) {
324 if (create_node) {
325 node = new(mem_ctx) ast_tcs_output_layout(*loc);
326 }
327 } else {
328 _mesa_glsl_error(loc, state, "out layout qualifiers only valid in "
329 "tessellation control or geometry shaders");
330 }
331
332 return r;
333 }
334
335 bool
336 ast_type_qualifier::merge_in_qualifier(YYLTYPE *loc,
337 _mesa_glsl_parse_state *state,
338 const ast_type_qualifier &q,
339 ast_node* &node, bool create_node)
340 {
341 void *mem_ctx = state;
342 bool create_gs_ast = false;
343 bool create_cs_ast = false;
344 ast_type_qualifier valid_in_mask;
345 valid_in_mask.flags.i = 0;
346
347 switch (state->stage) {
348 case MESA_SHADER_TESS_EVAL:
349 if (q.flags.q.prim_type) {
350 /* Make sure this is a valid input primitive type. */
351 switch (q.prim_type) {
352 case GL_TRIANGLES:
353 case GL_QUADS:
354 case GL_ISOLINES:
355 break;
356 default:
357 _mesa_glsl_error(loc, state,
358 "invalid tessellation evaluation "
359 "shader input primitive type");
360 break;
361 }
362 }
363
364 valid_in_mask.flags.q.prim_type = 1;
365 valid_in_mask.flags.q.vertex_spacing = 1;
366 valid_in_mask.flags.q.ordering = 1;
367 valid_in_mask.flags.q.point_mode = 1;
368 break;
369 case MESA_SHADER_GEOMETRY:
370 if (q.flags.q.prim_type) {
371 /* Make sure this is a valid input primitive type. */
372 switch (q.prim_type) {
373 case GL_POINTS:
374 case GL_LINES:
375 case GL_LINES_ADJACENCY:
376 case GL_TRIANGLES:
377 case GL_TRIANGLES_ADJACENCY:
378 break;
379 default:
380 _mesa_glsl_error(loc, state,
381 "invalid geometry shader input primitive type");
382 break;
383 }
384 }
385
386 create_gs_ast |=
387 q.flags.q.prim_type &&
388 !state->in_qualifier->flags.q.prim_type;
389
390 valid_in_mask.flags.q.prim_type = 1;
391 valid_in_mask.flags.q.invocations = 1;
392 break;
393 case MESA_SHADER_FRAGMENT:
394 valid_in_mask.flags.q.early_fragment_tests = 1;
395 break;
396 case MESA_SHADER_COMPUTE:
397 create_cs_ast |=
398 q.flags.q.local_size != 0 &&
399 state->in_qualifier->flags.q.local_size == 0;
400
401 valid_in_mask.flags.q.local_size = 7;
402 break;
403 default:
404 _mesa_glsl_error(loc, state,
405 "input layout qualifiers only valid in "
406 "geometry, fragment and compute shaders");
407 break;
408 }
409
410 /* Generate an error when invalid input layout qualifiers are used. */
411 if ((q.flags.i & ~valid_in_mask.flags.i) != 0) {
412 _mesa_glsl_error(loc, state,
413 "invalid input layout qualifiers used");
414 return false;
415 }
416
417 /* Input layout qualifiers can be specified multiple
418 * times in separate declarations, as long as they match.
419 */
420 if (this->flags.q.prim_type) {
421 if (q.flags.q.prim_type &&
422 this->prim_type != q.prim_type) {
423 _mesa_glsl_error(loc, state,
424 "conflicting input primitive %s specified",
425 state->stage == MESA_SHADER_GEOMETRY ?
426 "type" : "mode");
427 }
428 } else if (q.flags.q.prim_type) {
429 state->in_qualifier->flags.q.prim_type = 1;
430 state->in_qualifier->prim_type = q.prim_type;
431 }
432
433 if (q.flags.q.invocations) {
434 this->flags.q.invocations = 1;
435 if (this->invocations) {
436 this->invocations->merge_qualifier(q.invocations);
437 } else {
438 this->invocations = q.invocations;
439 }
440 }
441
442 if (q.flags.q.early_fragment_tests) {
443 state->fs_early_fragment_tests = true;
444 }
445
446 if (this->flags.q.vertex_spacing) {
447 if (q.flags.q.vertex_spacing &&
448 this->vertex_spacing != q.vertex_spacing) {
449 _mesa_glsl_error(loc, state,
450 "conflicting vertex spacing specified");
451 }
452 } else if (q.flags.q.vertex_spacing) {
453 this->flags.q.vertex_spacing = 1;
454 this->vertex_spacing = q.vertex_spacing;
455 }
456
457 if (this->flags.q.ordering) {
458 if (q.flags.q.ordering &&
459 this->ordering != q.ordering) {
460 _mesa_glsl_error(loc, state,
461 "conflicting ordering specified");
462 }
463 } else if (q.flags.q.ordering) {
464 this->flags.q.ordering = 1;
465 this->ordering = q.ordering;
466 }
467
468 if (this->flags.q.point_mode) {
469 if (q.flags.q.point_mode &&
470 this->point_mode != q.point_mode) {
471 _mesa_glsl_error(loc, state,
472 "conflicting point mode specified");
473 }
474 } else if (q.flags.q.point_mode) {
475 this->flags.q.point_mode = 1;
476 this->point_mode = q.point_mode;
477 }
478
479 if (create_node) {
480 if (create_gs_ast) {
481 node = new(mem_ctx) ast_gs_input_layout(*loc, q.prim_type);
482 } else if (create_cs_ast) {
483 node = new(mem_ctx) ast_cs_input_layout(*loc, q.local_size);
484 }
485 }
486
487 return true;
488 }
489
490 bool
491 ast_layout_expression::process_qualifier_constant(struct _mesa_glsl_parse_state *state,
492 const char *qual_indentifier,
493 unsigned *value,
494 bool can_be_zero)
495 {
496 int min_value = 0;
497 bool first_pass = true;
498 *value = 0;
499
500 if (!can_be_zero)
501 min_value = 1;
502
503 for (exec_node *node = layout_const_expressions.head;
504 !node->is_tail_sentinel(); node = node->next) {
505
506 exec_list dummy_instructions;
507 ast_node *const_expression = exec_node_data(ast_node, node, link);
508
509 ir_rvalue *const ir = const_expression->hir(&dummy_instructions, state);
510
511 ir_constant *const const_int = ir->constant_expression_value();
512 if (const_int == NULL || !const_int->type->is_integer()) {
513 YYLTYPE loc = const_expression->get_location();
514 _mesa_glsl_error(&loc, state, "%s must be an integral constant "
515 "expression", qual_indentifier);
516 return false;
517 }
518
519 if (const_int->value.i[0] < min_value) {
520 YYLTYPE loc = const_expression->get_location();
521 _mesa_glsl_error(&loc, state, "%s layout qualifier is invalid "
522 "(%d < %d)", qual_indentifier,
523 const_int->value.i[0], min_value);
524 return false;
525 }
526
527 if (!first_pass && *value != const_int->value.u[0]) {
528 YYLTYPE loc = const_expression->get_location();
529 _mesa_glsl_error(&loc, state, "%s layout qualifier does not "
530 "match previous declaration (%d vs %d)",
531 qual_indentifier, *value, const_int->value.i[0]);
532 return false;
533 } else {
534 first_pass = false;
535 *value = const_int->value.u[0];
536 }
537
538 /* If the location is const (and we've verified that
539 * it is) then no instructions should have been emitted
540 * when we converted it to HIR. If they were emitted,
541 * then either the location isn't const after all, or
542 * we are emitting unnecessary instructions.
543 */
544 assert(dummy_instructions.is_empty());
545 }
546
547 return true;
548 }