89ab8eaa7ff2e1c4435bbfe7d6789e42b0a3a530
[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 (!this->flags.q.explicit_stream) {
194 if (q.flags.q.stream) {
195 this->flags.q.stream = 1;
196 this->stream = q.stream;
197 } else if (!this->flags.q.stream && this->flags.q.out) {
198 /* Assign default global stream value */
199 this->flags.q.stream = 1;
200 this->stream = state->out_qualifier->stream;
201 }
202 } else {
203 if (q.flags.q.explicit_stream) {
204 _mesa_glsl_error(loc, state,
205 "duplicate layout `stream' qualifier");
206 }
207 }
208 }
209
210 if (q.flags.q.vertices) {
211 if (this->flags.q.vertices && this->vertices != q.vertices) {
212 _mesa_glsl_error(loc, state,
213 "tessellation control shader set conflicting "
214 "vertices (%d and %d)",
215 this->vertices, q.vertices);
216 return false;
217 }
218 this->vertices = q.vertices;
219 }
220
221 if (q.flags.q.vertex_spacing) {
222 if (this->flags.q.vertex_spacing && this->vertex_spacing != q.vertex_spacing) {
223 _mesa_glsl_error(loc, state,
224 "conflicting vertex spacing used");
225 return false;
226 }
227 this->vertex_spacing = q.vertex_spacing;
228 }
229
230 if (q.flags.q.ordering) {
231 if (this->flags.q.ordering && this->ordering != q.ordering) {
232 _mesa_glsl_error(loc, state,
233 "conflicting ordering used");
234 return false;
235 }
236 this->ordering = q.ordering;
237 }
238
239 if (q.flags.q.point_mode) {
240 if (this->flags.q.point_mode && this->point_mode != q.point_mode) {
241 _mesa_glsl_error(loc, state,
242 "conflicting point mode used");
243 return false;
244 }
245 this->point_mode = q.point_mode;
246 }
247
248 if ((q.flags.i & ubo_mat_mask.flags.i) != 0)
249 this->flags.i &= ~ubo_mat_mask.flags.i;
250 if ((q.flags.i & ubo_layout_mask.flags.i) != 0)
251 this->flags.i &= ~ubo_layout_mask.flags.i;
252
253 for (int i = 0; i < 3; i++) {
254 if (q.flags.q.local_size & (1 << i)) {
255 if ((this->flags.q.local_size & (1 << i)) &&
256 this->local_size[i] != q.local_size[i]) {
257 _mesa_glsl_error(loc, state,
258 "compute shader set conflicting values for "
259 "local_size_%c (%d and %d)", 'x' + i,
260 this->local_size[i], q.local_size[i]);
261 return false;
262 }
263 this->local_size[i] = q.local_size[i];
264 }
265 }
266
267 this->flags.i |= q.flags.i;
268
269 if (q.flags.q.explicit_location)
270 this->location = q.location;
271
272 if (q.flags.q.explicit_index)
273 this->index = q.index;
274
275 if (q.flags.q.explicit_binding)
276 this->binding = q.binding;
277
278 if (q.flags.q.explicit_offset)
279 this->offset = q.offset;
280
281 if (q.precision != ast_precision_none)
282 this->precision = q.precision;
283
284 if (q.flags.q.explicit_image_format) {
285 this->image_format = q.image_format;
286 this->image_base_type = q.image_base_type;
287 }
288
289 return true;
290 }
291
292 bool
293 ast_type_qualifier::merge_out_qualifier(YYLTYPE *loc,
294 _mesa_glsl_parse_state *state,
295 ast_type_qualifier q,
296 ast_node* &node)
297 {
298 void *mem_ctx = state;
299 const bool r = this->merge_qualifier(loc, state, q);
300
301 if (state->stage == MESA_SHADER_TESS_CTRL) {
302 node = new(mem_ctx) ast_tcs_output_layout(*loc, q.vertices);
303 }
304
305 return r;
306 }
307
308 bool
309 ast_type_qualifier::merge_in_qualifier(YYLTYPE *loc,
310 _mesa_glsl_parse_state *state,
311 ast_type_qualifier q,
312 ast_node* &node)
313 {
314 void *mem_ctx = state;
315 bool create_gs_ast = false;
316 bool create_cs_ast = false;
317 ast_type_qualifier valid_in_mask;
318 valid_in_mask.flags.i = 0;
319
320 switch (state->stage) {
321 case MESA_SHADER_TESS_EVAL:
322 if (q.flags.q.prim_type) {
323 /* Make sure this is a valid input primitive type. */
324 switch (q.prim_type) {
325 case GL_TRIANGLES:
326 case GL_QUADS:
327 case GL_ISOLINES:
328 break;
329 default:
330 _mesa_glsl_error(loc, state,
331 "invalid tessellation evaluation "
332 "shader input primitive type");
333 break;
334 }
335 }
336
337 valid_in_mask.flags.q.prim_type = 1;
338 valid_in_mask.flags.q.vertex_spacing = 1;
339 valid_in_mask.flags.q.ordering = 1;
340 valid_in_mask.flags.q.point_mode = 1;
341 break;
342 case MESA_SHADER_GEOMETRY:
343 if (q.flags.q.prim_type) {
344 /* Make sure this is a valid input primitive type. */
345 switch (q.prim_type) {
346 case GL_POINTS:
347 case GL_LINES:
348 case GL_LINES_ADJACENCY:
349 case GL_TRIANGLES:
350 case GL_TRIANGLES_ADJACENCY:
351 break;
352 default:
353 _mesa_glsl_error(loc, state,
354 "invalid geometry shader input primitive type");
355 break;
356 }
357 }
358
359 create_gs_ast |=
360 q.flags.q.prim_type &&
361 !state->in_qualifier->flags.q.prim_type;
362
363 valid_in_mask.flags.q.prim_type = 1;
364 valid_in_mask.flags.q.invocations = 1;
365 break;
366 case MESA_SHADER_FRAGMENT:
367 valid_in_mask.flags.q.early_fragment_tests = 1;
368 break;
369 case MESA_SHADER_COMPUTE:
370 create_cs_ast |=
371 q.flags.q.local_size != 0 &&
372 state->in_qualifier->flags.q.local_size == 0;
373
374 valid_in_mask.flags.q.local_size = 7;
375 break;
376 default:
377 _mesa_glsl_error(loc, state,
378 "input layout qualifiers only valid in "
379 "geometry, fragment and compute shaders");
380 break;
381 }
382
383 /* Generate an error when invalid input layout qualifiers are used. */
384 if ((q.flags.i & ~valid_in_mask.flags.i) != 0) {
385 _mesa_glsl_error(loc, state,
386 "invalid input layout qualifiers used");
387 return false;
388 }
389
390 /* Input layout qualifiers can be specified multiple
391 * times in separate declarations, as long as they match.
392 */
393 if (this->flags.q.prim_type) {
394 if (q.flags.q.prim_type &&
395 this->prim_type != q.prim_type) {
396 _mesa_glsl_error(loc, state,
397 "conflicting input primitive %s specified",
398 state->stage == MESA_SHADER_GEOMETRY ?
399 "type" : "mode");
400 }
401 } else if (q.flags.q.prim_type) {
402 state->in_qualifier->flags.q.prim_type = 1;
403 state->in_qualifier->prim_type = q.prim_type;
404 }
405
406 if (this->flags.q.invocations &&
407 q.flags.q.invocations &&
408 this->invocations != q.invocations) {
409 _mesa_glsl_error(loc, state,
410 "conflicting invocations counts specified");
411 return false;
412 } else if (q.flags.q.invocations) {
413 this->flags.q.invocations = 1;
414 this->invocations = q.invocations;
415 }
416
417 if (q.flags.q.early_fragment_tests) {
418 state->fs_early_fragment_tests = true;
419 }
420
421 if (this->flags.q.vertex_spacing) {
422 if (q.flags.q.vertex_spacing &&
423 this->vertex_spacing != q.vertex_spacing) {
424 _mesa_glsl_error(loc, state,
425 "conflicting vertex spacing specified");
426 }
427 } else if (q.flags.q.vertex_spacing) {
428 this->flags.q.vertex_spacing = 1;
429 this->vertex_spacing = q.vertex_spacing;
430 }
431
432 if (this->flags.q.ordering) {
433 if (q.flags.q.ordering &&
434 this->ordering != q.ordering) {
435 _mesa_glsl_error(loc, state,
436 "conflicting ordering specified");
437 }
438 } else if (q.flags.q.ordering) {
439 this->flags.q.ordering = 1;
440 this->ordering = q.ordering;
441 }
442
443 if (this->flags.q.point_mode) {
444 if (q.flags.q.point_mode &&
445 this->point_mode != q.point_mode) {
446 _mesa_glsl_error(loc, state,
447 "conflicting point mode specified");
448 }
449 } else if (q.flags.q.point_mode) {
450 this->flags.q.point_mode = 1;
451 this->point_mode = q.point_mode;
452 }
453
454 if (create_gs_ast) {
455 node = new(mem_ctx) ast_gs_input_layout(*loc, q.prim_type);
456 } else if (create_cs_ast) {
457 /* Infer a local_size of 1 for every unspecified dimension */
458 unsigned local_size[3];
459 for (int i = 0; i < 3; i++) {
460 if (q.flags.q.local_size & (1 << i))
461 local_size[i] = q.local_size[i];
462 else
463 local_size[i] = 1;
464 }
465 node = new(mem_ctx) ast_cs_input_layout(*loc, local_size);
466 }
467
468 return true;
469 }
470
471 bool
472 ast_layout_expression::process_qualifier_constant(struct _mesa_glsl_parse_state *state,
473 const char *qual_indentifier,
474 unsigned *value,
475 bool can_be_zero)
476 {
477 int min_value = 0;
478 bool first_pass = true;
479 *value = 0;
480
481 if (!can_be_zero)
482 min_value = 1;
483
484 for (exec_node *node = layout_const_expressions.head;
485 !node->is_tail_sentinel(); node = node->next) {
486
487 exec_list dummy_instructions;
488 ast_node *const_expression = exec_node_data(ast_node, node, link);
489
490 ir_rvalue *const ir = const_expression->hir(&dummy_instructions, state);
491
492 ir_constant *const const_int = ir->constant_expression_value();
493 if (const_int == NULL || !const_int->type->is_integer()) {
494 YYLTYPE loc = const_expression->get_location();
495 _mesa_glsl_error(&loc, state, "%s must be an integral constant "
496 "expression", qual_indentifier);
497 return false;
498 }
499
500 if (const_int->value.i[0] < min_value) {
501 YYLTYPE loc = const_expression->get_location();
502 _mesa_glsl_error(&loc, state, "%s layout qualifier is invalid "
503 "(%d < %d)", qual_indentifier,
504 const_int->value.i[0], min_value);
505 return false;
506 }
507
508 if (!first_pass && *value != const_int->value.u[0]) {
509 YYLTYPE loc = const_expression->get_location();
510 _mesa_glsl_error(&loc, state, "%s layout qualifier does not "
511 "match previous declaration (%d vs %d)",
512 qual_indentifier, *value, const_int->value.i[0]);
513 return false;
514 } else {
515 first_pass = false;
516 *value = const_int->value.u[0];
517 }
518
519 /* If the location is const (and we've verified that
520 * it is) then no instructions should have been emitted
521 * when we converted it to HIR. If they were emitted,
522 * then either the location isn't const after all, or
523 * we are emitting unnecessary instructions.
524 */
525 assert(dummy_instructions.is_empty());
526 }
527
528 return true;
529 }