nir: include nir_instr_set.h in the tarball
[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 }
90
91 bool
92 ast_type_qualifier::has_auxiliary_storage() const
93 {
94 return this->flags.q.centroid
95 || this->flags.q.sample
96 || this->flags.q.patch;
97 }
98
99 const char*
100 ast_type_qualifier::interpolation_string() const
101 {
102 if (this->flags.q.smooth)
103 return "smooth";
104 else if (this->flags.q.flat)
105 return "flat";
106 else if (this->flags.q.noperspective)
107 return "noperspective";
108 else
109 return NULL;
110 }
111
112 bool
113 ast_type_qualifier::merge_qualifier(YYLTYPE *loc,
114 _mesa_glsl_parse_state *state,
115 ast_type_qualifier q)
116 {
117 ast_type_qualifier ubo_mat_mask;
118 ubo_mat_mask.flags.i = 0;
119 ubo_mat_mask.flags.q.row_major = 1;
120 ubo_mat_mask.flags.q.column_major = 1;
121
122 ast_type_qualifier ubo_layout_mask;
123 ubo_layout_mask.flags.i = 0;
124 ubo_layout_mask.flags.q.std140 = 1;
125 ubo_layout_mask.flags.q.packed = 1;
126 ubo_layout_mask.flags.q.shared = 1;
127 ubo_layout_mask.flags.q.std430 = 1;
128
129 ast_type_qualifier ubo_binding_mask;
130 ubo_binding_mask.flags.i = 0;
131 ubo_binding_mask.flags.q.explicit_binding = 1;
132 ubo_binding_mask.flags.q.explicit_offset = 1;
133
134 ast_type_qualifier stream_layout_mask;
135 stream_layout_mask.flags.i = 0;
136 stream_layout_mask.flags.q.stream = 1;
137
138 /* Uniform block layout qualifiers get to overwrite each
139 * other (rightmost having priority), while all other
140 * qualifiers currently don't allow duplicates.
141 */
142 ast_type_qualifier allowed_duplicates_mask;
143 allowed_duplicates_mask.flags.i =
144 ubo_mat_mask.flags.i |
145 ubo_layout_mask.flags.i |
146 ubo_binding_mask.flags.i;
147
148 /* Geometry shaders can have several layout qualifiers
149 * assigning different stream values.
150 */
151 if (state->stage == MESA_SHADER_GEOMETRY)
152 allowed_duplicates_mask.flags.i |=
153 stream_layout_mask.flags.i;
154
155 if ((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->flags.q.max_vertices && this->max_vertices != q.max_vertices) {
172 _mesa_glsl_error(loc, state,
173 "geometry shader set conflicting max_vertices "
174 "(%d and %d)", this->max_vertices, q.max_vertices);
175 return false;
176 }
177 this->max_vertices = q.max_vertices;
178 }
179
180 if (q.flags.q.invocations) {
181 if (this->flags.q.invocations && this->invocations != q.invocations) {
182 _mesa_glsl_error(loc, state,
183 "geometry shader set conflicting invocations "
184 "(%d and %d)", this->invocations, q.invocations);
185 return false;
186 }
187 this->invocations = q.invocations;
188 }
189
190 if (state->stage == MESA_SHADER_GEOMETRY &&
191 state->has_explicit_attrib_stream()) {
192 if (q.flags.q.stream && q.stream >= state->ctx->Const.MaxVertexStreams) {
193 _mesa_glsl_error(loc, state,
194 "`stream' value is larger than MAX_VERTEX_STREAMS - 1 "
195 "(%d > %d)",
196 q.stream, state->ctx->Const.MaxVertexStreams - 1);
197 }
198 if (this->flags.q.explicit_stream &&
199 this->stream >= state->ctx->Const.MaxVertexStreams) {
200 _mesa_glsl_error(loc, state,
201 "`stream' value is larger than MAX_VERTEX_STREAMS - 1 "
202 "(%d > %d)",
203 this->stream, state->ctx->Const.MaxVertexStreams - 1);
204 }
205
206 if (!this->flags.q.explicit_stream) {
207 if (q.flags.q.stream) {
208 this->flags.q.stream = 1;
209 this->stream = q.stream;
210 } else if (!this->flags.q.stream && this->flags.q.out) {
211 /* Assign default global stream value */
212 this->flags.q.stream = 1;
213 this->stream = state->out_qualifier->stream;
214 }
215 } else {
216 if (q.flags.q.explicit_stream) {
217 _mesa_glsl_error(loc, state,
218 "duplicate layout `stream' qualifier");
219 }
220 }
221 }
222
223 if (q.flags.q.vertices) {
224 if (this->flags.q.vertices && this->vertices != q.vertices) {
225 _mesa_glsl_error(loc, state,
226 "tessellation control shader set conflicting "
227 "vertices (%d and %d)",
228 this->vertices, q.vertices);
229 return false;
230 }
231 this->vertices = q.vertices;
232 }
233
234 if (q.flags.q.vertex_spacing) {
235 if (this->flags.q.vertex_spacing && this->vertex_spacing != q.vertex_spacing) {
236 _mesa_glsl_error(loc, state,
237 "conflicting vertex spacing used");
238 return false;
239 }
240 this->vertex_spacing = q.vertex_spacing;
241 }
242
243 if (q.flags.q.ordering) {
244 if (this->flags.q.ordering && this->ordering != q.ordering) {
245 _mesa_glsl_error(loc, state,
246 "conflicting ordering used");
247 return false;
248 }
249 this->ordering = q.ordering;
250 }
251
252 if (q.flags.q.point_mode) {
253 if (this->flags.q.point_mode && this->point_mode != q.point_mode) {
254 _mesa_glsl_error(loc, state,
255 "conflicting point mode used");
256 return false;
257 }
258 this->point_mode = q.point_mode;
259 }
260
261 if ((q.flags.i & ubo_mat_mask.flags.i) != 0)
262 this->flags.i &= ~ubo_mat_mask.flags.i;
263 if ((q.flags.i & ubo_layout_mask.flags.i) != 0)
264 this->flags.i &= ~ubo_layout_mask.flags.i;
265
266 for (int i = 0; i < 3; i++) {
267 if (q.flags.q.local_size & (1 << i)) {
268 if ((this->flags.q.local_size & (1 << i)) &&
269 this->local_size[i] != q.local_size[i]) {
270 _mesa_glsl_error(loc, state,
271 "compute shader set conflicting values for "
272 "local_size_%c (%d and %d)", 'x' + i,
273 this->local_size[i], q.local_size[i]);
274 return false;
275 }
276 this->local_size[i] = q.local_size[i];
277 }
278 }
279
280 this->flags.i |= q.flags.i;
281
282 if (q.flags.q.explicit_location)
283 this->location = q.location;
284
285 if (q.flags.q.explicit_index)
286 this->index = q.index;
287
288 if (q.flags.q.explicit_binding)
289 this->binding = q.binding;
290
291 if (q.flags.q.explicit_offset)
292 this->offset = q.offset;
293
294 if (q.precision != ast_precision_none)
295 this->precision = q.precision;
296
297 if (q.flags.q.explicit_image_format) {
298 this->image_format = q.image_format;
299 this->image_base_type = q.image_base_type;
300 }
301
302 return true;
303 }
304
305 bool
306 ast_type_qualifier::merge_out_qualifier(YYLTYPE *loc,
307 _mesa_glsl_parse_state *state,
308 ast_type_qualifier q,
309 ast_node* &node)
310 {
311 void *mem_ctx = state;
312 const bool r = this->merge_qualifier(loc, state, q);
313
314 if (state->stage == MESA_SHADER_TESS_CTRL) {
315 node = new(mem_ctx) ast_tcs_output_layout(*loc, q.vertices);
316 }
317
318 return r;
319 }
320
321 bool
322 ast_type_qualifier::merge_in_qualifier(YYLTYPE *loc,
323 _mesa_glsl_parse_state *state,
324 ast_type_qualifier q,
325 ast_node* &node)
326 {
327 void *mem_ctx = state;
328 bool create_gs_ast = false;
329 bool create_cs_ast = false;
330 ast_type_qualifier valid_in_mask;
331 valid_in_mask.flags.i = 0;
332
333 switch (state->stage) {
334 case MESA_SHADER_TESS_EVAL:
335 if (q.flags.q.prim_type) {
336 /* Make sure this is a valid input primitive type. */
337 switch (q.prim_type) {
338 case GL_TRIANGLES:
339 case GL_QUADS:
340 case GL_ISOLINES:
341 break;
342 default:
343 _mesa_glsl_error(loc, state,
344 "invalid tessellation evaluation "
345 "shader input primitive type");
346 break;
347 }
348 }
349
350 valid_in_mask.flags.q.prim_type = 1;
351 valid_in_mask.flags.q.vertex_spacing = 1;
352 valid_in_mask.flags.q.ordering = 1;
353 valid_in_mask.flags.q.point_mode = 1;
354 break;
355 case MESA_SHADER_GEOMETRY:
356 if (q.flags.q.prim_type) {
357 /* Make sure this is a valid input primitive type. */
358 switch (q.prim_type) {
359 case GL_POINTS:
360 case GL_LINES:
361 case GL_LINES_ADJACENCY:
362 case GL_TRIANGLES:
363 case GL_TRIANGLES_ADJACENCY:
364 break;
365 default:
366 _mesa_glsl_error(loc, state,
367 "invalid geometry shader input primitive type");
368 break;
369 }
370 }
371
372 create_gs_ast |=
373 q.flags.q.prim_type &&
374 !state->in_qualifier->flags.q.prim_type;
375
376 valid_in_mask.flags.q.prim_type = 1;
377 valid_in_mask.flags.q.invocations = 1;
378 break;
379 case MESA_SHADER_FRAGMENT:
380 valid_in_mask.flags.q.early_fragment_tests = 1;
381 break;
382 case MESA_SHADER_COMPUTE:
383 create_cs_ast |=
384 q.flags.q.local_size != 0 &&
385 state->in_qualifier->flags.q.local_size == 0;
386
387 valid_in_mask.flags.q.local_size = 7;
388 break;
389 default:
390 _mesa_glsl_error(loc, state,
391 "input layout qualifiers only valid in "
392 "geometry, fragment and compute shaders");
393 break;
394 }
395
396 /* Generate an error when invalid input layout qualifiers are used. */
397 if ((q.flags.i & ~valid_in_mask.flags.i) != 0) {
398 _mesa_glsl_error(loc, state,
399 "invalid input layout qualifiers used");
400 return false;
401 }
402
403 /* Input layout qualifiers can be specified multiple
404 * times in separate declarations, as long as they match.
405 */
406 if (this->flags.q.prim_type) {
407 if (q.flags.q.prim_type &&
408 this->prim_type != q.prim_type) {
409 _mesa_glsl_error(loc, state,
410 "conflicting input primitive %s specified",
411 state->stage == MESA_SHADER_GEOMETRY ?
412 "type" : "mode");
413 }
414 } else if (q.flags.q.prim_type) {
415 state->in_qualifier->flags.q.prim_type = 1;
416 state->in_qualifier->prim_type = q.prim_type;
417 }
418
419 if (this->flags.q.invocations &&
420 q.flags.q.invocations &&
421 this->invocations != q.invocations) {
422 _mesa_glsl_error(loc, state,
423 "conflicting invocations counts specified");
424 return false;
425 } else if (q.flags.q.invocations) {
426 this->flags.q.invocations = 1;
427 this->invocations = q.invocations;
428 }
429
430 if (q.flags.q.early_fragment_tests) {
431 state->fs_early_fragment_tests = true;
432 }
433
434 if (this->flags.q.vertex_spacing) {
435 if (q.flags.q.vertex_spacing &&
436 this->vertex_spacing != q.vertex_spacing) {
437 _mesa_glsl_error(loc, state,
438 "conflicting vertex spacing specified");
439 }
440 } else if (q.flags.q.vertex_spacing) {
441 this->flags.q.vertex_spacing = 1;
442 this->vertex_spacing = q.vertex_spacing;
443 }
444
445 if (this->flags.q.ordering) {
446 if (q.flags.q.ordering &&
447 this->ordering != q.ordering) {
448 _mesa_glsl_error(loc, state,
449 "conflicting ordering specified");
450 }
451 } else if (q.flags.q.ordering) {
452 this->flags.q.ordering = 1;
453 this->ordering = q.ordering;
454 }
455
456 if (this->flags.q.point_mode) {
457 if (q.flags.q.point_mode &&
458 this->point_mode != q.point_mode) {
459 _mesa_glsl_error(loc, state,
460 "conflicting point mode specified");
461 }
462 } else if (q.flags.q.point_mode) {
463 this->flags.q.point_mode = 1;
464 this->point_mode = q.point_mode;
465 }
466
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 /* Infer a local_size of 1 for every unspecified dimension */
471 unsigned local_size[3];
472 for (int i = 0; i < 3; i++) {
473 if (q.flags.q.local_size & (1 << i))
474 local_size[i] = q.local_size[i];
475 else
476 local_size[i] = 1;
477 }
478 node = new(mem_ctx) ast_cs_input_layout(*loc, local_size);
479 }
480
481 return true;
482 }