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