nir/spirv: Handle decorations after assigning variable locations
[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 return true;
301 }
302
303 bool
304 ast_type_qualifier::merge_out_qualifier(YYLTYPE *loc,
305 _mesa_glsl_parse_state *state,
306 ast_type_qualifier q,
307 ast_node* &node)
308 {
309 void *mem_ctx = state;
310 const bool r = this->merge_qualifier(loc, state, q);
311
312 if (state->stage == MESA_SHADER_TESS_CTRL) {
313 node = new(mem_ctx) ast_tcs_output_layout(*loc, q.vertices);
314 }
315
316 return r;
317 }
318
319 bool
320 ast_type_qualifier::merge_in_qualifier(YYLTYPE *loc,
321 _mesa_glsl_parse_state *state,
322 ast_type_qualifier q,
323 ast_node* &node)
324 {
325 void *mem_ctx = state;
326 bool create_gs_ast = false;
327 bool create_cs_ast = false;
328 ast_type_qualifier valid_in_mask;
329 valid_in_mask.flags.i = 0;
330
331 switch (state->stage) {
332 case MESA_SHADER_TESS_EVAL:
333 if (q.flags.q.prim_type) {
334 /* Make sure this is a valid input primitive type. */
335 switch (q.prim_type) {
336 case GL_TRIANGLES:
337 case GL_QUADS:
338 case GL_ISOLINES:
339 break;
340 default:
341 _mesa_glsl_error(loc, state,
342 "invalid tessellation evaluation "
343 "shader input primitive type");
344 break;
345 }
346 }
347
348 valid_in_mask.flags.q.prim_type = 1;
349 valid_in_mask.flags.q.vertex_spacing = 1;
350 valid_in_mask.flags.q.ordering = 1;
351 valid_in_mask.flags.q.point_mode = 1;
352 break;
353 case MESA_SHADER_GEOMETRY:
354 if (q.flags.q.prim_type) {
355 /* Make sure this is a valid input primitive type. */
356 switch (q.prim_type) {
357 case GL_POINTS:
358 case GL_LINES:
359 case GL_LINES_ADJACENCY:
360 case GL_TRIANGLES:
361 case GL_TRIANGLES_ADJACENCY:
362 break;
363 default:
364 _mesa_glsl_error(loc, state,
365 "invalid geometry shader input primitive type");
366 break;
367 }
368 }
369
370 create_gs_ast |=
371 q.flags.q.prim_type &&
372 !state->in_qualifier->flags.q.prim_type;
373
374 valid_in_mask.flags.q.prim_type = 1;
375 valid_in_mask.flags.q.invocations = 1;
376 break;
377 case MESA_SHADER_FRAGMENT:
378 valid_in_mask.flags.q.early_fragment_tests = 1;
379 break;
380 case MESA_SHADER_COMPUTE:
381 create_cs_ast |=
382 q.flags.q.local_size != 0 &&
383 state->in_qualifier->flags.q.local_size == 0;
384
385 valid_in_mask.flags.q.local_size = 7;
386 break;
387 default:
388 _mesa_glsl_error(loc, state,
389 "input layout qualifiers only valid in "
390 "geometry, fragment and compute shaders");
391 break;
392 }
393
394 /* Generate an error when invalid input layout qualifiers are used. */
395 if ((q.flags.i & ~valid_in_mask.flags.i) != 0) {
396 _mesa_glsl_error(loc, state,
397 "invalid input layout qualifiers used");
398 return false;
399 }
400
401 /* Input layout qualifiers can be specified multiple
402 * times in separate declarations, as long as they match.
403 */
404 if (this->flags.q.prim_type) {
405 if (q.flags.q.prim_type &&
406 this->prim_type != q.prim_type) {
407 _mesa_glsl_error(loc, state,
408 "conflicting input primitive %s specified",
409 state->stage == MESA_SHADER_GEOMETRY ?
410 "type" : "mode");
411 }
412 } else if (q.flags.q.prim_type) {
413 state->in_qualifier->flags.q.prim_type = 1;
414 state->in_qualifier->prim_type = q.prim_type;
415 }
416
417 if (this->flags.q.invocations &&
418 q.flags.q.invocations &&
419 this->invocations != q.invocations) {
420 _mesa_glsl_error(loc, state,
421 "conflicting invocations counts specified");
422 return false;
423 } else if (q.flags.q.invocations) {
424 this->flags.q.invocations = 1;
425 this->invocations = q.invocations;
426 }
427
428 if (q.flags.q.early_fragment_tests) {
429 state->fs_early_fragment_tests = true;
430 }
431
432 if (this->flags.q.vertex_spacing) {
433 if (q.flags.q.vertex_spacing &&
434 this->vertex_spacing != q.vertex_spacing) {
435 _mesa_glsl_error(loc, state,
436 "conflicting vertex spacing specified");
437 }
438 } else if (q.flags.q.vertex_spacing) {
439 this->flags.q.vertex_spacing = 1;
440 this->vertex_spacing = q.vertex_spacing;
441 }
442
443 if (this->flags.q.ordering) {
444 if (q.flags.q.ordering &&
445 this->ordering != q.ordering) {
446 _mesa_glsl_error(loc, state,
447 "conflicting ordering specified");
448 }
449 } else if (q.flags.q.ordering) {
450 this->flags.q.ordering = 1;
451 this->ordering = q.ordering;
452 }
453
454 if (this->flags.q.point_mode) {
455 if (q.flags.q.point_mode &&
456 this->point_mode != q.point_mode) {
457 _mesa_glsl_error(loc, state,
458 "conflicting point mode specified");
459 }
460 } else if (q.flags.q.point_mode) {
461 this->flags.q.point_mode = 1;
462 this->point_mode = q.point_mode;
463 }
464
465 if (create_gs_ast) {
466 node = new(mem_ctx) ast_gs_input_layout(*loc, q.prim_type);
467 } else if (create_cs_ast) {
468 /* Infer a local_size of 1 for every unspecified dimension */
469 unsigned local_size[3];
470 for (int i = 0; i < 3; i++) {
471 if (q.flags.q.local_size & (1 << i))
472 local_size[i] = q.local_size[i];
473 else
474 local_size[i] = 1;
475 }
476 node = new(mem_ctx) ast_cs_input_layout(*loc, local_size);
477 }
478
479 return true;
480 }