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