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