Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/compiler/glsl/ast_to_hir.cpp
4545 views
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
/**
25
* \file ast_to_hir.c
26
* Convert abstract syntax to to high-level intermediate reprensentation (HIR).
27
*
28
* During the conversion to HIR, the majority of the symantic checking is
29
* preformed on the program. This includes:
30
*
31
* * Symbol table management
32
* * Type checking
33
* * Function binding
34
*
35
* The majority of this work could be done during parsing, and the parser could
36
* probably generate HIR directly. However, this results in frequent changes
37
* to the parser code. Since we do not assume that every system this complier
38
* is built on will have Flex and Bison installed, we have to store the code
39
* generated by these tools in our version control system. In other parts of
40
* the system we've seen problems where a parser was changed but the generated
41
* code was not committed, merge conflicts where created because two developers
42
* had slightly different versions of Bison installed, etc.
43
*
44
* I have also noticed that running Bison generated parsers in GDB is very
45
* irritating. When you get a segfault on '$$ = $1->foo', you can't very
46
* well 'print $1' in GDB.
47
*
48
* As a result, my preference is to put as little C code as possible in the
49
* parser (and lexer) sources.
50
*/
51
52
#include "glsl_symbol_table.h"
53
#include "glsl_parser_extras.h"
54
#include "ast.h"
55
#include "compiler/glsl_types.h"
56
#include "util/hash_table.h"
57
#include "main/mtypes.h"
58
#include "main/macros.h"
59
#include "main/shaderobj.h"
60
#include "ir.h"
61
#include "ir_builder.h"
62
#include "builtin_functions.h"
63
64
using namespace ir_builder;
65
66
static void
67
detect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
68
exec_list *instructions);
69
static void
70
verify_subroutine_associated_funcs(struct _mesa_glsl_parse_state *state);
71
72
static void
73
remove_per_vertex_blocks(exec_list *instructions,
74
_mesa_glsl_parse_state *state, ir_variable_mode mode);
75
76
/**
77
* Visitor class that finds the first instance of any write-only variable that
78
* is ever read, if any
79
*/
80
class read_from_write_only_variable_visitor : public ir_hierarchical_visitor
81
{
82
public:
83
read_from_write_only_variable_visitor() : found(NULL)
84
{
85
}
86
87
virtual ir_visitor_status visit(ir_dereference_variable *ir)
88
{
89
if (this->in_assignee)
90
return visit_continue;
91
92
ir_variable *var = ir->variable_referenced();
93
/* We can have memory_write_only set on both images and buffer variables,
94
* but in the former there is a distinction between reads from
95
* the variable itself (write_only) and from the memory they point to
96
* (memory_write_only), while in the case of buffer variables there is
97
* no such distinction, that is why this check here is limited to
98
* buffer variables alone.
99
*/
100
if (!var || var->data.mode != ir_var_shader_storage)
101
return visit_continue;
102
103
if (var->data.memory_write_only) {
104
found = var;
105
return visit_stop;
106
}
107
108
return visit_continue;
109
}
110
111
ir_variable *get_variable() {
112
return found;
113
}
114
115
virtual ir_visitor_status visit_enter(ir_expression *ir)
116
{
117
/* .length() doesn't actually read anything */
118
if (ir->operation == ir_unop_ssbo_unsized_array_length)
119
return visit_continue_with_parent;
120
121
return visit_continue;
122
}
123
124
private:
125
ir_variable *found;
126
};
127
128
void
129
_mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
130
{
131
_mesa_glsl_initialize_variables(instructions, state);
132
133
state->symbols->separate_function_namespace = state->language_version == 110;
134
135
state->current_function = NULL;
136
137
state->toplevel_ir = instructions;
138
139
state->gs_input_prim_type_specified = false;
140
state->tcs_output_vertices_specified = false;
141
state->cs_input_local_size_specified = false;
142
143
/* Section 4.2 of the GLSL 1.20 specification states:
144
* "The built-in functions are scoped in a scope outside the global scope
145
* users declare global variables in. That is, a shader's global scope,
146
* available for user-defined functions and global variables, is nested
147
* inside the scope containing the built-in functions."
148
*
149
* Since built-in functions like ftransform() access built-in variables,
150
* it follows that those must be in the outer scope as well.
151
*
152
* We push scope here to create this nesting effect...but don't pop.
153
* This way, a shader's globals are still in the symbol table for use
154
* by the linker.
155
*/
156
state->symbols->push_scope();
157
158
foreach_list_typed (ast_node, ast, link, & state->translation_unit)
159
ast->hir(instructions, state);
160
161
verify_subroutine_associated_funcs(state);
162
detect_recursion_unlinked(state, instructions);
163
detect_conflicting_assignments(state, instructions);
164
165
state->toplevel_ir = NULL;
166
167
/* Move all of the variable declarations to the front of the IR list, and
168
* reverse the order. This has the (intended!) side effect that vertex
169
* shader inputs and fragment shader outputs will appear in the IR in the
170
* same order that they appeared in the shader code. This results in the
171
* locations being assigned in the declared order. Many (arguably buggy)
172
* applications depend on this behavior, and it matches what nearly all
173
* other drivers do.
174
*/
175
foreach_in_list_safe(ir_instruction, node, instructions) {
176
ir_variable *const var = node->as_variable();
177
178
if (var == NULL)
179
continue;
180
181
var->remove();
182
instructions->push_head(var);
183
}
184
185
/* Figure out if gl_FragCoord is actually used in fragment shader */
186
ir_variable *const var = state->symbols->get_variable("gl_FragCoord");
187
if (var != NULL)
188
state->fs_uses_gl_fragcoord = var->data.used;
189
190
/* From section 7.1 (Built-In Language Variables) of the GLSL 4.10 spec:
191
*
192
* If multiple shaders using members of a built-in block belonging to
193
* the same interface are linked together in the same program, they
194
* must all redeclare the built-in block in the same way, as described
195
* in section 4.3.7 "Interface Blocks" for interface block matching, or
196
* a link error will result.
197
*
198
* The phrase "using members of a built-in block" implies that if two
199
* shaders are linked together and one of them *does not use* any members
200
* of the built-in block, then that shader does not need to have a matching
201
* redeclaration of the built-in block.
202
*
203
* This appears to be a clarification to the behaviour established for
204
* gl_PerVertex by GLSL 1.50, therefore implement it regardless of GLSL
205
* version.
206
*
207
* The definition of "interface" in section 4.3.7 that applies here is as
208
* follows:
209
*
210
* The boundary between adjacent programmable pipeline stages: This
211
* spans all the outputs in all compilation units of the first stage
212
* and all the inputs in all compilation units of the second stage.
213
*
214
* Therefore this rule applies to both inter- and intra-stage linking.
215
*
216
* The easiest way to implement this is to check whether the shader uses
217
* gl_PerVertex right after ast-to-ir conversion, and if it doesn't, simply
218
* remove all the relevant variable declaration from the IR, so that the
219
* linker won't see them and complain about mismatches.
220
*/
221
remove_per_vertex_blocks(instructions, state, ir_var_shader_in);
222
remove_per_vertex_blocks(instructions, state, ir_var_shader_out);
223
224
/* Check that we don't have reads from write-only variables */
225
read_from_write_only_variable_visitor v;
226
v.run(instructions);
227
ir_variable *error_var = v.get_variable();
228
if (error_var) {
229
/* It would be nice to have proper location information, but for that
230
* we would need to check this as we process each kind of AST node
231
*/
232
YYLTYPE loc;
233
memset(&loc, 0, sizeof(loc));
234
_mesa_glsl_error(&loc, state, "Read from write-only variable `%s'",
235
error_var->name);
236
}
237
}
238
239
240
static ir_expression_operation
241
get_implicit_conversion_operation(const glsl_type *to, const glsl_type *from,
242
struct _mesa_glsl_parse_state *state)
243
{
244
switch (to->base_type) {
245
case GLSL_TYPE_FLOAT:
246
switch (from->base_type) {
247
case GLSL_TYPE_INT: return ir_unop_i2f;
248
case GLSL_TYPE_UINT: return ir_unop_u2f;
249
default: return (ir_expression_operation)0;
250
}
251
252
case GLSL_TYPE_UINT:
253
if (!state->has_implicit_int_to_uint_conversion())
254
return (ir_expression_operation)0;
255
switch (from->base_type) {
256
case GLSL_TYPE_INT: return ir_unop_i2u;
257
default: return (ir_expression_operation)0;
258
}
259
260
case GLSL_TYPE_DOUBLE:
261
if (!state->has_double())
262
return (ir_expression_operation)0;
263
switch (from->base_type) {
264
case GLSL_TYPE_INT: return ir_unop_i2d;
265
case GLSL_TYPE_UINT: return ir_unop_u2d;
266
case GLSL_TYPE_FLOAT: return ir_unop_f2d;
267
case GLSL_TYPE_INT64: return ir_unop_i642d;
268
case GLSL_TYPE_UINT64: return ir_unop_u642d;
269
default: return (ir_expression_operation)0;
270
}
271
272
case GLSL_TYPE_UINT64:
273
if (!state->has_int64())
274
return (ir_expression_operation)0;
275
switch (from->base_type) {
276
case GLSL_TYPE_INT: return ir_unop_i2u64;
277
case GLSL_TYPE_UINT: return ir_unop_u2u64;
278
case GLSL_TYPE_INT64: return ir_unop_i642u64;
279
default: return (ir_expression_operation)0;
280
}
281
282
case GLSL_TYPE_INT64:
283
if (!state->has_int64())
284
return (ir_expression_operation)0;
285
switch (from->base_type) {
286
case GLSL_TYPE_INT: return ir_unop_i2i64;
287
default: return (ir_expression_operation)0;
288
}
289
290
default: return (ir_expression_operation)0;
291
}
292
}
293
294
295
/**
296
* If a conversion is available, convert one operand to a different type
297
*
298
* The \c from \c ir_rvalue is converted "in place".
299
*
300
* \param to Type that the operand it to be converted to
301
* \param from Operand that is being converted
302
* \param state GLSL compiler state
303
*
304
* \return
305
* If a conversion is possible (or unnecessary), \c true is returned.
306
* Otherwise \c false is returned.
307
*/
308
static bool
309
apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
310
struct _mesa_glsl_parse_state *state)
311
{
312
void *ctx = state;
313
if (to->base_type == from->type->base_type)
314
return true;
315
316
/* Prior to GLSL 1.20, there are no implicit conversions */
317
if (!state->has_implicit_conversions())
318
return false;
319
320
/* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
321
*
322
* "There are no implicit array or structure conversions. For
323
* example, an array of int cannot be implicitly converted to an
324
* array of float.
325
*/
326
if (!to->is_numeric() || !from->type->is_numeric())
327
return false;
328
329
/* We don't actually want the specific type `to`, we want a type
330
* with the same base type as `to`, but the same vector width as
331
* `from`.
332
*/
333
to = glsl_type::get_instance(to->base_type, from->type->vector_elements,
334
from->type->matrix_columns);
335
336
ir_expression_operation op = get_implicit_conversion_operation(to, from->type, state);
337
if (op) {
338
from = new(ctx) ir_expression(op, to, from, NULL);
339
return true;
340
} else {
341
return false;
342
}
343
}
344
345
346
static const struct glsl_type *
347
arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
348
bool multiply,
349
struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
350
{
351
const glsl_type *type_a = value_a->type;
352
const glsl_type *type_b = value_b->type;
353
354
/* From GLSL 1.50 spec, page 56:
355
*
356
* "The arithmetic binary operators add (+), subtract (-),
357
* multiply (*), and divide (/) operate on integer and
358
* floating-point scalars, vectors, and matrices."
359
*/
360
if (!type_a->is_numeric() || !type_b->is_numeric()) {
361
_mesa_glsl_error(loc, state,
362
"operands to arithmetic operators must be numeric");
363
return glsl_type::error_type;
364
}
365
366
367
/* "If one operand is floating-point based and the other is
368
* not, then the conversions from Section 4.1.10 "Implicit
369
* Conversions" are applied to the non-floating-point-based operand."
370
*/
371
if (!apply_implicit_conversion(type_a, value_b, state)
372
&& !apply_implicit_conversion(type_b, value_a, state)) {
373
_mesa_glsl_error(loc, state,
374
"could not implicitly convert operands to "
375
"arithmetic operator");
376
return glsl_type::error_type;
377
}
378
type_a = value_a->type;
379
type_b = value_b->type;
380
381
/* "If the operands are integer types, they must both be signed or
382
* both be unsigned."
383
*
384
* From this rule and the preceeding conversion it can be inferred that
385
* both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
386
* The is_numeric check above already filtered out the case where either
387
* type is not one of these, so now the base types need only be tested for
388
* equality.
389
*/
390
if (type_a->base_type != type_b->base_type) {
391
_mesa_glsl_error(loc, state,
392
"base type mismatch for arithmetic operator");
393
return glsl_type::error_type;
394
}
395
396
/* "All arithmetic binary operators result in the same fundamental type
397
* (signed integer, unsigned integer, or floating-point) as the
398
* operands they operate on, after operand type conversion. After
399
* conversion, the following cases are valid
400
*
401
* * The two operands are scalars. In this case the operation is
402
* applied, resulting in a scalar."
403
*/
404
if (type_a->is_scalar() && type_b->is_scalar())
405
return type_a;
406
407
/* "* One operand is a scalar, and the other is a vector or matrix.
408
* In this case, the scalar operation is applied independently to each
409
* component of the vector or matrix, resulting in the same size
410
* vector or matrix."
411
*/
412
if (type_a->is_scalar()) {
413
if (!type_b->is_scalar())
414
return type_b;
415
} else if (type_b->is_scalar()) {
416
return type_a;
417
}
418
419
/* All of the combinations of <scalar, scalar>, <vector, scalar>,
420
* <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
421
* handled.
422
*/
423
assert(!type_a->is_scalar());
424
assert(!type_b->is_scalar());
425
426
/* "* The two operands are vectors of the same size. In this case, the
427
* operation is done component-wise resulting in the same size
428
* vector."
429
*/
430
if (type_a->is_vector() && type_b->is_vector()) {
431
if (type_a == type_b) {
432
return type_a;
433
} else {
434
_mesa_glsl_error(loc, state,
435
"vector size mismatch for arithmetic operator");
436
return glsl_type::error_type;
437
}
438
}
439
440
/* All of the combinations of <scalar, scalar>, <vector, scalar>,
441
* <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
442
* <vector, vector> have been handled. At least one of the operands must
443
* be matrix. Further, since there are no integer matrix types, the base
444
* type of both operands must be float.
445
*/
446
assert(type_a->is_matrix() || type_b->is_matrix());
447
assert(type_a->is_float() || type_a->is_double());
448
assert(type_b->is_float() || type_b->is_double());
449
450
/* "* The operator is add (+), subtract (-), or divide (/), and the
451
* operands are matrices with the same number of rows and the same
452
* number of columns. In this case, the operation is done component-
453
* wise resulting in the same size matrix."
454
* * The operator is multiply (*), where both operands are matrices or
455
* one operand is a vector and the other a matrix. A right vector
456
* operand is treated as a column vector and a left vector operand as a
457
* row vector. In all these cases, it is required that the number of
458
* columns of the left operand is equal to the number of rows of the
459
* right operand. Then, the multiply (*) operation does a linear
460
* algebraic multiply, yielding an object that has the same number of
461
* rows as the left operand and the same number of columns as the right
462
* operand. Section 5.10 "Vector and Matrix Operations" explains in
463
* more detail how vectors and matrices are operated on."
464
*/
465
if (! multiply) {
466
if (type_a == type_b)
467
return type_a;
468
} else {
469
const glsl_type *type = glsl_type::get_mul_type(type_a, type_b);
470
471
if (type == glsl_type::error_type) {
472
_mesa_glsl_error(loc, state,
473
"size mismatch for matrix multiplication");
474
}
475
476
return type;
477
}
478
479
480
/* "All other cases are illegal."
481
*/
482
_mesa_glsl_error(loc, state, "type mismatch");
483
return glsl_type::error_type;
484
}
485
486
487
static const struct glsl_type *
488
unary_arithmetic_result_type(const struct glsl_type *type,
489
struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
490
{
491
/* From GLSL 1.50 spec, page 57:
492
*
493
* "The arithmetic unary operators negate (-), post- and pre-increment
494
* and decrement (-- and ++) operate on integer or floating-point
495
* values (including vectors and matrices). All unary operators work
496
* component-wise on their operands. These result with the same type
497
* they operated on."
498
*/
499
if (!type->is_numeric()) {
500
_mesa_glsl_error(loc, state,
501
"operands to arithmetic operators must be numeric");
502
return glsl_type::error_type;
503
}
504
505
return type;
506
}
507
508
/**
509
* \brief Return the result type of a bit-logic operation.
510
*
511
* If the given types to the bit-logic operator are invalid, return
512
* glsl_type::error_type.
513
*
514
* \param value_a LHS of bit-logic op
515
* \param value_b RHS of bit-logic op
516
*/
517
static const struct glsl_type *
518
bit_logic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
519
ast_operators op,
520
struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
521
{
522
const glsl_type *type_a = value_a->type;
523
const glsl_type *type_b = value_b->type;
524
525
if (!state->check_bitwise_operations_allowed(loc)) {
526
return glsl_type::error_type;
527
}
528
529
/* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
530
*
531
* "The bitwise operators and (&), exclusive-or (^), and inclusive-or
532
* (|). The operands must be of type signed or unsigned integers or
533
* integer vectors."
534
*/
535
if (!type_a->is_integer_32_64()) {
536
_mesa_glsl_error(loc, state, "LHS of `%s' must be an integer",
537
ast_expression::operator_string(op));
538
return glsl_type::error_type;
539
}
540
if (!type_b->is_integer_32_64()) {
541
_mesa_glsl_error(loc, state, "RHS of `%s' must be an integer",
542
ast_expression::operator_string(op));
543
return glsl_type::error_type;
544
}
545
546
/* Prior to GLSL 4.0 / GL_ARB_gpu_shader5, implicit conversions didn't
547
* make sense for bitwise operations, as they don't operate on floats.
548
*
549
* GLSL 4.0 added implicit int -> uint conversions, which are relevant
550
* here. It wasn't clear whether or not we should apply them to bitwise
551
* operations. However, Khronos has decided that they should in future
552
* language revisions. Applications also rely on this behavior. We opt
553
* to apply them in general, but issue a portability warning.
554
*
555
* See https://www.khronos.org/bugzilla/show_bug.cgi?id=1405
556
*/
557
if (type_a->base_type != type_b->base_type) {
558
if (!apply_implicit_conversion(type_a, value_b, state)
559
&& !apply_implicit_conversion(type_b, value_a, state)) {
560
_mesa_glsl_error(loc, state,
561
"could not implicitly convert operands to "
562
"`%s` operator",
563
ast_expression::operator_string(op));
564
return glsl_type::error_type;
565
} else {
566
_mesa_glsl_warning(loc, state,
567
"some implementations may not support implicit "
568
"int -> uint conversions for `%s' operators; "
569
"consider casting explicitly for portability",
570
ast_expression::operator_string(op));
571
}
572
type_a = value_a->type;
573
type_b = value_b->type;
574
}
575
576
/* "The fundamental types of the operands (signed or unsigned) must
577
* match,"
578
*/
579
if (type_a->base_type != type_b->base_type) {
580
_mesa_glsl_error(loc, state, "operands of `%s' must have the same "
581
"base type", ast_expression::operator_string(op));
582
return glsl_type::error_type;
583
}
584
585
/* "The operands cannot be vectors of differing size." */
586
if (type_a->is_vector() &&
587
type_b->is_vector() &&
588
type_a->vector_elements != type_b->vector_elements) {
589
_mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of "
590
"different sizes", ast_expression::operator_string(op));
591
return glsl_type::error_type;
592
}
593
594
/* "If one operand is a scalar and the other a vector, the scalar is
595
* applied component-wise to the vector, resulting in the same type as
596
* the vector. The fundamental types of the operands [...] will be the
597
* resulting fundamental type."
598
*/
599
if (type_a->is_scalar())
600
return type_b;
601
else
602
return type_a;
603
}
604
605
static const struct glsl_type *
606
modulus_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
607
struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
608
{
609
const glsl_type *type_a = value_a->type;
610
const glsl_type *type_b = value_b->type;
611
612
if (!state->EXT_gpu_shader4_enable &&
613
!state->check_version(130, 300, loc, "operator '%%' is reserved")) {
614
return glsl_type::error_type;
615
}
616
617
/* Section 5.9 (Expressions) of the GLSL 4.00 specification says:
618
*
619
* "The operator modulus (%) operates on signed or unsigned integers or
620
* integer vectors."
621
*/
622
if (!type_a->is_integer_32_64()) {
623
_mesa_glsl_error(loc, state, "LHS of operator %% must be an integer");
624
return glsl_type::error_type;
625
}
626
if (!type_b->is_integer_32_64()) {
627
_mesa_glsl_error(loc, state, "RHS of operator %% must be an integer");
628
return glsl_type::error_type;
629
}
630
631
/* "If the fundamental types in the operands do not match, then the
632
* conversions from section 4.1.10 "Implicit Conversions" are applied
633
* to create matching types."
634
*
635
* Note that GLSL 4.00 (and GL_ARB_gpu_shader5) introduced implicit
636
* int -> uint conversion rules. Prior to that, there were no implicit
637
* conversions. So it's harmless to apply them universally - no implicit
638
* conversions will exist. If the types don't match, we'll receive false,
639
* and raise an error, satisfying the GLSL 1.50 spec, page 56:
640
*
641
* "The operand types must both be signed or unsigned."
642
*/
643
if (!apply_implicit_conversion(type_a, value_b, state) &&
644
!apply_implicit_conversion(type_b, value_a, state)) {
645
_mesa_glsl_error(loc, state,
646
"could not implicitly convert operands to "
647
"modulus (%%) operator");
648
return glsl_type::error_type;
649
}
650
type_a = value_a->type;
651
type_b = value_b->type;
652
653
/* "The operands cannot be vectors of differing size. If one operand is
654
* a scalar and the other vector, then the scalar is applied component-
655
* wise to the vector, resulting in the same type as the vector. If both
656
* are vectors of the same size, the result is computed component-wise."
657
*/
658
if (type_a->is_vector()) {
659
if (!type_b->is_vector()
660
|| (type_a->vector_elements == type_b->vector_elements))
661
return type_a;
662
} else
663
return type_b;
664
665
/* "The operator modulus (%) is not defined for any other data types
666
* (non-integer types)."
667
*/
668
_mesa_glsl_error(loc, state, "type mismatch");
669
return glsl_type::error_type;
670
}
671
672
673
static const struct glsl_type *
674
relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
675
struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
676
{
677
const glsl_type *type_a = value_a->type;
678
const glsl_type *type_b = value_b->type;
679
680
/* From GLSL 1.50 spec, page 56:
681
* "The relational operators greater than (>), less than (<), greater
682
* than or equal (>=), and less than or equal (<=) operate only on
683
* scalar integer and scalar floating-point expressions."
684
*/
685
if (!type_a->is_numeric()
686
|| !type_b->is_numeric()
687
|| !type_a->is_scalar()
688
|| !type_b->is_scalar()) {
689
_mesa_glsl_error(loc, state,
690
"operands to relational operators must be scalar and "
691
"numeric");
692
return glsl_type::error_type;
693
}
694
695
/* "Either the operands' types must match, or the conversions from
696
* Section 4.1.10 "Implicit Conversions" will be applied to the integer
697
* operand, after which the types must match."
698
*/
699
if (!apply_implicit_conversion(type_a, value_b, state)
700
&& !apply_implicit_conversion(type_b, value_a, state)) {
701
_mesa_glsl_error(loc, state,
702
"could not implicitly convert operands to "
703
"relational operator");
704
return glsl_type::error_type;
705
}
706
type_a = value_a->type;
707
type_b = value_b->type;
708
709
if (type_a->base_type != type_b->base_type) {
710
_mesa_glsl_error(loc, state, "base type mismatch");
711
return glsl_type::error_type;
712
}
713
714
/* "The result is scalar Boolean."
715
*/
716
return glsl_type::bool_type;
717
}
718
719
/**
720
* \brief Return the result type of a bit-shift operation.
721
*
722
* If the given types to the bit-shift operator are invalid, return
723
* glsl_type::error_type.
724
*
725
* \param type_a Type of LHS of bit-shift op
726
* \param type_b Type of RHS of bit-shift op
727
*/
728
static const struct glsl_type *
729
shift_result_type(const struct glsl_type *type_a,
730
const struct glsl_type *type_b,
731
ast_operators op,
732
struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
733
{
734
if (!state->check_bitwise_operations_allowed(loc)) {
735
return glsl_type::error_type;
736
}
737
738
/* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
739
*
740
* "The shift operators (<<) and (>>). For both operators, the operands
741
* must be signed or unsigned integers or integer vectors. One operand
742
* can be signed while the other is unsigned."
743
*/
744
if (!type_a->is_integer_32_64()) {
745
_mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or "
746
"integer vector", ast_expression::operator_string(op));
747
return glsl_type::error_type;
748
749
}
750
if (!type_b->is_integer_32()) {
751
_mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or "
752
"integer vector", ast_expression::operator_string(op));
753
return glsl_type::error_type;
754
}
755
756
/* "If the first operand is a scalar, the second operand has to be
757
* a scalar as well."
758
*/
759
if (type_a->is_scalar() && !type_b->is_scalar()) {
760
_mesa_glsl_error(loc, state, "if the first operand of %s is scalar, the "
761
"second must be scalar as well",
762
ast_expression::operator_string(op));
763
return glsl_type::error_type;
764
}
765
766
/* If both operands are vectors, check that they have same number of
767
* elements.
768
*/
769
if (type_a->is_vector() &&
770
type_b->is_vector() &&
771
type_a->vector_elements != type_b->vector_elements) {
772
_mesa_glsl_error(loc, state, "vector operands to operator %s must "
773
"have same number of elements",
774
ast_expression::operator_string(op));
775
return glsl_type::error_type;
776
}
777
778
/* "In all cases, the resulting type will be the same type as the left
779
* operand."
780
*/
781
return type_a;
782
}
783
784
/**
785
* Returns the innermost array index expression in an rvalue tree.
786
* This is the largest indexing level -- if an array of blocks, then
787
* it is the block index rather than an indexing expression for an
788
* array-typed member of an array of blocks.
789
*/
790
static ir_rvalue *
791
find_innermost_array_index(ir_rvalue *rv)
792
{
793
ir_dereference_array *last = NULL;
794
while (rv) {
795
if (rv->as_dereference_array()) {
796
last = rv->as_dereference_array();
797
rv = last->array;
798
} else if (rv->as_dereference_record())
799
rv = rv->as_dereference_record()->record;
800
else if (rv->as_swizzle())
801
rv = rv->as_swizzle()->val;
802
else
803
rv = NULL;
804
}
805
806
if (last)
807
return last->array_index;
808
809
return NULL;
810
}
811
812
/**
813
* Validates that a value can be assigned to a location with a specified type
814
*
815
* Validates that \c rhs can be assigned to some location. If the types are
816
* not an exact match but an automatic conversion is possible, \c rhs will be
817
* converted.
818
*
819
* \return
820
* \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
821
* Otherwise the actual RHS to be assigned will be returned. This may be
822
* \c rhs, or it may be \c rhs after some type conversion.
823
*
824
* \note
825
* In addition to being used for assignments, this function is used to
826
* type-check return values.
827
*/
828
static ir_rvalue *
829
validate_assignment(struct _mesa_glsl_parse_state *state,
830
YYLTYPE loc, ir_rvalue *lhs,
831
ir_rvalue *rhs, bool is_initializer)
832
{
833
/* If there is already some error in the RHS, just return it. Anything
834
* else will lead to an avalanche of error message back to the user.
835
*/
836
if (rhs->type->is_error())
837
return rhs;
838
839
/* In the Tessellation Control Shader:
840
* If a per-vertex output variable is used as an l-value, it is an error
841
* if the expression indicating the vertex number is not the identifier
842
* `gl_InvocationID`.
843
*/
844
if (state->stage == MESA_SHADER_TESS_CTRL && !lhs->type->is_error()) {
845
ir_variable *var = lhs->variable_referenced();
846
if (var && var->data.mode == ir_var_shader_out && !var->data.patch) {
847
ir_rvalue *index = find_innermost_array_index(lhs);
848
ir_variable *index_var = index ? index->variable_referenced() : NULL;
849
if (!index_var || strcmp(index_var->name, "gl_InvocationID") != 0) {
850
_mesa_glsl_error(&loc, state,
851
"Tessellation control shader outputs can only "
852
"be indexed by gl_InvocationID");
853
return NULL;
854
}
855
}
856
}
857
858
/* If the types are identical, the assignment can trivially proceed.
859
*/
860
if (rhs->type == lhs->type)
861
return rhs;
862
863
/* If the array element types are the same and the LHS is unsized,
864
* the assignment is okay for initializers embedded in variable
865
* declarations.
866
*
867
* Note: Whole-array assignments are not permitted in GLSL 1.10, but this
868
* is handled by ir_dereference::is_lvalue.
869
*/
870
const glsl_type *lhs_t = lhs->type;
871
const glsl_type *rhs_t = rhs->type;
872
bool unsized_array = false;
873
while(lhs_t->is_array()) {
874
if (rhs_t == lhs_t)
875
break; /* the rest of the inner arrays match so break out early */
876
if (!rhs_t->is_array()) {
877
unsized_array = false;
878
break; /* number of dimensions mismatch */
879
}
880
if (lhs_t->length == rhs_t->length) {
881
lhs_t = lhs_t->fields.array;
882
rhs_t = rhs_t->fields.array;
883
continue;
884
} else if (lhs_t->is_unsized_array()) {
885
unsized_array = true;
886
} else {
887
unsized_array = false;
888
break; /* sized array mismatch */
889
}
890
lhs_t = lhs_t->fields.array;
891
rhs_t = rhs_t->fields.array;
892
}
893
if (unsized_array) {
894
if (is_initializer) {
895
if (rhs->type->get_scalar_type() == lhs->type->get_scalar_type())
896
return rhs;
897
} else {
898
_mesa_glsl_error(&loc, state,
899
"implicitly sized arrays cannot be assigned");
900
return NULL;
901
}
902
}
903
904
/* Check for implicit conversion in GLSL 1.20 */
905
if (apply_implicit_conversion(lhs->type, rhs, state)) {
906
if (rhs->type == lhs->type)
907
return rhs;
908
}
909
910
_mesa_glsl_error(&loc, state,
911
"%s of type %s cannot be assigned to "
912
"variable of type %s",
913
is_initializer ? "initializer" : "value",
914
rhs->type->name, lhs->type->name);
915
916
return NULL;
917
}
918
919
static void
920
mark_whole_array_access(ir_rvalue *access)
921
{
922
ir_dereference_variable *deref = access->as_dereference_variable();
923
924
if (deref && deref->var) {
925
deref->var->data.max_array_access = deref->type->length - 1;
926
}
927
}
928
929
static bool
930
do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
931
const char *non_lvalue_description,
932
ir_rvalue *lhs, ir_rvalue *rhs,
933
ir_rvalue **out_rvalue, bool needs_rvalue,
934
bool is_initializer,
935
YYLTYPE lhs_loc)
936
{
937
void *ctx = state;
938
bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
939
940
ir_variable *lhs_var = lhs->variable_referenced();
941
if (lhs_var)
942
lhs_var->data.assigned = true;
943
944
bool omit_assignment = false;
945
if (!error_emitted) {
946
if (non_lvalue_description != NULL) {
947
_mesa_glsl_error(&lhs_loc, state,
948
"assignment to %s",
949
non_lvalue_description);
950
error_emitted = true;
951
} else if (lhs_var != NULL && (lhs_var->data.read_only ||
952
(lhs_var->data.mode == ir_var_shader_storage &&
953
lhs_var->data.memory_read_only))) {
954
/* We can have memory_read_only set on both images and buffer variables,
955
* but in the former there is a distinction between assignments to
956
* the variable itself (read_only) and to the memory they point to
957
* (memory_read_only), while in the case of buffer variables there is
958
* no such distinction, that is why this check here is limited to
959
* buffer variables alone.
960
*/
961
962
if (state->ignore_write_to_readonly_var)
963
omit_assignment = true;
964
else {
965
_mesa_glsl_error(&lhs_loc, state,
966
"assignment to read-only variable '%s'",
967
lhs_var->name);
968
error_emitted = true;
969
}
970
} else if (lhs->type->is_array() &&
971
!state->check_version(state->allow_glsl_120_subset_in_110 ? 110 : 120,
972
300, &lhs_loc,
973
"whole array assignment forbidden")) {
974
/* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
975
*
976
* "Other binary or unary expressions, non-dereferenced
977
* arrays, function names, swizzles with repeated fields,
978
* and constants cannot be l-values."
979
*
980
* The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
981
*/
982
error_emitted = true;
983
} else if (!lhs->is_lvalue(state)) {
984
_mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
985
error_emitted = true;
986
}
987
}
988
989
ir_rvalue *new_rhs =
990
validate_assignment(state, lhs_loc, lhs, rhs, is_initializer);
991
if (new_rhs != NULL) {
992
rhs = new_rhs;
993
994
/* If the LHS array was not declared with a size, it takes it size from
995
* the RHS. If the LHS is an l-value and a whole array, it must be a
996
* dereference of a variable. Any other case would require that the LHS
997
* is either not an l-value or not a whole array.
998
*/
999
if (lhs->type->is_unsized_array()) {
1000
ir_dereference *const d = lhs->as_dereference();
1001
1002
assert(d != NULL);
1003
1004
ir_variable *const var = d->variable_referenced();
1005
1006
assert(var != NULL);
1007
1008
if (var->data.max_array_access >= rhs->type->array_size()) {
1009
/* FINISHME: This should actually log the location of the RHS. */
1010
_mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
1011
"previous access",
1012
var->data.max_array_access);
1013
}
1014
1015
var->type = glsl_type::get_array_instance(lhs->type->fields.array,
1016
rhs->type->array_size());
1017
d->type = var->type;
1018
}
1019
if (lhs->type->is_array()) {
1020
mark_whole_array_access(rhs);
1021
mark_whole_array_access(lhs);
1022
}
1023
} else {
1024
error_emitted = true;
1025
}
1026
1027
if (omit_assignment) {
1028
*out_rvalue = needs_rvalue ? ir_rvalue::error_value(ctx) : NULL;
1029
return error_emitted;
1030
}
1031
1032
/* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
1033
* but not post_inc) need the converted assigned value as an rvalue
1034
* to handle things like:
1035
*
1036
* i = j += 1;
1037
*/
1038
if (needs_rvalue) {
1039
ir_rvalue *rvalue;
1040
if (!error_emitted) {
1041
ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
1042
ir_var_temporary);
1043
instructions->push_tail(var);
1044
instructions->push_tail(assign(var, rhs));
1045
1046
ir_dereference_variable *deref_var =
1047
new(ctx) ir_dereference_variable(var);
1048
instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var));
1049
rvalue = new(ctx) ir_dereference_variable(var);
1050
} else {
1051
rvalue = ir_rvalue::error_value(ctx);
1052
}
1053
*out_rvalue = rvalue;
1054
} else {
1055
if (!error_emitted)
1056
instructions->push_tail(new(ctx) ir_assignment(lhs, rhs));
1057
*out_rvalue = NULL;
1058
}
1059
1060
return error_emitted;
1061
}
1062
1063
static ir_rvalue *
1064
get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
1065
{
1066
void *ctx = ralloc_parent(lvalue);
1067
ir_variable *var;
1068
1069
var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
1070
ir_var_temporary);
1071
instructions->push_tail(var);
1072
1073
instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
1074
lvalue));
1075
1076
return new(ctx) ir_dereference_variable(var);
1077
}
1078
1079
1080
ir_rvalue *
1081
ast_node::hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
1082
{
1083
(void) instructions;
1084
(void) state;
1085
1086
return NULL;
1087
}
1088
1089
bool
1090
ast_node::has_sequence_subexpression() const
1091
{
1092
return false;
1093
}
1094
1095
void
1096
ast_node::set_is_lhs(bool /* new_value */)
1097
{
1098
}
1099
1100
void
1101
ast_function_expression::hir_no_rvalue(exec_list *instructions,
1102
struct _mesa_glsl_parse_state *state)
1103
{
1104
(void)hir(instructions, state);
1105
}
1106
1107
void
1108
ast_aggregate_initializer::hir_no_rvalue(exec_list *instructions,
1109
struct _mesa_glsl_parse_state *state)
1110
{
1111
(void)hir(instructions, state);
1112
}
1113
1114
static ir_rvalue *
1115
do_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1)
1116
{
1117
int join_op;
1118
ir_rvalue *cmp = NULL;
1119
1120
if (operation == ir_binop_all_equal)
1121
join_op = ir_binop_logic_and;
1122
else
1123
join_op = ir_binop_logic_or;
1124
1125
switch (op0->type->base_type) {
1126
case GLSL_TYPE_FLOAT:
1127
case GLSL_TYPE_FLOAT16:
1128
case GLSL_TYPE_UINT:
1129
case GLSL_TYPE_INT:
1130
case GLSL_TYPE_BOOL:
1131
case GLSL_TYPE_DOUBLE:
1132
case GLSL_TYPE_UINT64:
1133
case GLSL_TYPE_INT64:
1134
case GLSL_TYPE_UINT16:
1135
case GLSL_TYPE_INT16:
1136
case GLSL_TYPE_UINT8:
1137
case GLSL_TYPE_INT8:
1138
return new(mem_ctx) ir_expression(operation, op0, op1);
1139
1140
case GLSL_TYPE_ARRAY: {
1141
for (unsigned int i = 0; i < op0->type->length; i++) {
1142
ir_rvalue *e0, *e1, *result;
1143
1144
e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL),
1145
new(mem_ctx) ir_constant(i));
1146
e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL),
1147
new(mem_ctx) ir_constant(i));
1148
result = do_comparison(mem_ctx, operation, e0, e1);
1149
1150
if (cmp) {
1151
cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
1152
} else {
1153
cmp = result;
1154
}
1155
}
1156
1157
mark_whole_array_access(op0);
1158
mark_whole_array_access(op1);
1159
break;
1160
}
1161
1162
case GLSL_TYPE_STRUCT: {
1163
for (unsigned int i = 0; i < op0->type->length; i++) {
1164
ir_rvalue *e0, *e1, *result;
1165
const char *field_name = op0->type->fields.structure[i].name;
1166
1167
e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL),
1168
field_name);
1169
e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL),
1170
field_name);
1171
result = do_comparison(mem_ctx, operation, e0, e1);
1172
1173
if (cmp) {
1174
cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
1175
} else {
1176
cmp = result;
1177
}
1178
}
1179
break;
1180
}
1181
1182
case GLSL_TYPE_ERROR:
1183
case GLSL_TYPE_VOID:
1184
case GLSL_TYPE_SAMPLER:
1185
case GLSL_TYPE_IMAGE:
1186
case GLSL_TYPE_INTERFACE:
1187
case GLSL_TYPE_ATOMIC_UINT:
1188
case GLSL_TYPE_SUBROUTINE:
1189
case GLSL_TYPE_FUNCTION:
1190
/* I assume a comparison of a struct containing a sampler just
1191
* ignores the sampler present in the type.
1192
*/
1193
break;
1194
}
1195
1196
if (cmp == NULL)
1197
cmp = new(mem_ctx) ir_constant(true);
1198
1199
return cmp;
1200
}
1201
1202
/* For logical operations, we want to ensure that the operands are
1203
* scalar booleans. If it isn't, emit an error and return a constant
1204
* boolean to avoid triggering cascading error messages.
1205
*/
1206
static ir_rvalue *
1207
get_scalar_boolean_operand(exec_list *instructions,
1208
struct _mesa_glsl_parse_state *state,
1209
ast_expression *parent_expr,
1210
int operand,
1211
const char *operand_name,
1212
bool *error_emitted)
1213
{
1214
ast_expression *expr = parent_expr->subexpressions[operand];
1215
void *ctx = state;
1216
ir_rvalue *val = expr->hir(instructions, state);
1217
1218
if (val->type->is_boolean() && val->type->is_scalar())
1219
return val;
1220
1221
if (!*error_emitted) {
1222
YYLTYPE loc = expr->get_location();
1223
_mesa_glsl_error(&loc, state, "%s of `%s' must be scalar boolean",
1224
operand_name,
1225
parent_expr->operator_string(parent_expr->oper));
1226
*error_emitted = true;
1227
}
1228
1229
return new(ctx) ir_constant(true);
1230
}
1231
1232
/**
1233
* If name refers to a builtin array whose maximum allowed size is less than
1234
* size, report an error and return true. Otherwise return false.
1235
*/
1236
void
1237
check_builtin_array_max_size(const char *name, unsigned size,
1238
YYLTYPE loc, struct _mesa_glsl_parse_state *state)
1239
{
1240
if ((strcmp("gl_TexCoord", name) == 0)
1241
&& (size > state->Const.MaxTextureCoords)) {
1242
/* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1243
*
1244
* "The size [of gl_TexCoord] can be at most
1245
* gl_MaxTextureCoords."
1246
*/
1247
_mesa_glsl_error(&loc, state, "`gl_TexCoord' array size cannot "
1248
"be larger than gl_MaxTextureCoords (%u)",
1249
state->Const.MaxTextureCoords);
1250
} else if (strcmp("gl_ClipDistance", name) == 0) {
1251
state->clip_dist_size = size;
1252
if (size + state->cull_dist_size > state->Const.MaxClipPlanes) {
1253
/* From section 7.1 (Vertex Shader Special Variables) of the
1254
* GLSL 1.30 spec:
1255
*
1256
* "The gl_ClipDistance array is predeclared as unsized and
1257
* must be sized by the shader either redeclaring it with a
1258
* size or indexing it only with integral constant
1259
* expressions. ... The size can be at most
1260
* gl_MaxClipDistances."
1261
*/
1262
_mesa_glsl_error(&loc, state, "`gl_ClipDistance' array size cannot "
1263
"be larger than gl_MaxClipDistances (%u)",
1264
state->Const.MaxClipPlanes);
1265
}
1266
} else if (strcmp("gl_CullDistance", name) == 0) {
1267
state->cull_dist_size = size;
1268
if (size + state->clip_dist_size > state->Const.MaxClipPlanes) {
1269
/* From the ARB_cull_distance spec:
1270
*
1271
* "The gl_CullDistance array is predeclared as unsized and
1272
* must be sized by the shader either redeclaring it with
1273
* a size or indexing it only with integral constant
1274
* expressions. The size determines the number and set of
1275
* enabled cull distances and can be at most
1276
* gl_MaxCullDistances."
1277
*/
1278
_mesa_glsl_error(&loc, state, "`gl_CullDistance' array size cannot "
1279
"be larger than gl_MaxCullDistances (%u)",
1280
state->Const.MaxClipPlanes);
1281
}
1282
}
1283
}
1284
1285
/**
1286
* Create the constant 1, of a which is appropriate for incrementing and
1287
* decrementing values of the given GLSL type. For example, if type is vec4,
1288
* this creates a constant value of 1.0 having type float.
1289
*
1290
* If the given type is invalid for increment and decrement operators, return
1291
* a floating point 1--the error will be detected later.
1292
*/
1293
static ir_rvalue *
1294
constant_one_for_inc_dec(void *ctx, const glsl_type *type)
1295
{
1296
switch (type->base_type) {
1297
case GLSL_TYPE_UINT:
1298
return new(ctx) ir_constant((unsigned) 1);
1299
case GLSL_TYPE_INT:
1300
return new(ctx) ir_constant(1);
1301
case GLSL_TYPE_UINT64:
1302
return new(ctx) ir_constant((uint64_t) 1);
1303
case GLSL_TYPE_INT64:
1304
return new(ctx) ir_constant((int64_t) 1);
1305
default:
1306
case GLSL_TYPE_FLOAT:
1307
return new(ctx) ir_constant(1.0f);
1308
}
1309
}
1310
1311
ir_rvalue *
1312
ast_expression::hir(exec_list *instructions,
1313
struct _mesa_glsl_parse_state *state)
1314
{
1315
return do_hir(instructions, state, true);
1316
}
1317
1318
void
1319
ast_expression::hir_no_rvalue(exec_list *instructions,
1320
struct _mesa_glsl_parse_state *state)
1321
{
1322
do_hir(instructions, state, false);
1323
}
1324
1325
void
1326
ast_expression::set_is_lhs(bool new_value)
1327
{
1328
/* is_lhs is tracked only to print "variable used uninitialized" warnings,
1329
* if we lack an identifier we can just skip it.
1330
*/
1331
if (this->primary_expression.identifier == NULL)
1332
return;
1333
1334
this->is_lhs = new_value;
1335
1336
/* We need to go through the subexpressions tree to cover cases like
1337
* ast_field_selection
1338
*/
1339
if (this->subexpressions[0] != NULL)
1340
this->subexpressions[0]->set_is_lhs(new_value);
1341
}
1342
1343
ir_rvalue *
1344
ast_expression::do_hir(exec_list *instructions,
1345
struct _mesa_glsl_parse_state *state,
1346
bool needs_rvalue)
1347
{
1348
void *ctx = state;
1349
static const int operations[AST_NUM_OPERATORS] = {
1350
-1, /* ast_assign doesn't convert to ir_expression. */
1351
-1, /* ast_plus doesn't convert to ir_expression. */
1352
ir_unop_neg,
1353
ir_binop_add,
1354
ir_binop_sub,
1355
ir_binop_mul,
1356
ir_binop_div,
1357
ir_binop_mod,
1358
ir_binop_lshift,
1359
ir_binop_rshift,
1360
ir_binop_less,
1361
ir_binop_less, /* This is correct. See the ast_greater case below. */
1362
ir_binop_gequal, /* This is correct. See the ast_lequal case below. */
1363
ir_binop_gequal,
1364
ir_binop_all_equal,
1365
ir_binop_any_nequal,
1366
ir_binop_bit_and,
1367
ir_binop_bit_xor,
1368
ir_binop_bit_or,
1369
ir_unop_bit_not,
1370
ir_binop_logic_and,
1371
ir_binop_logic_xor,
1372
ir_binop_logic_or,
1373
ir_unop_logic_not,
1374
1375
/* Note: The following block of expression types actually convert
1376
* to multiple IR instructions.
1377
*/
1378
ir_binop_mul, /* ast_mul_assign */
1379
ir_binop_div, /* ast_div_assign */
1380
ir_binop_mod, /* ast_mod_assign */
1381
ir_binop_add, /* ast_add_assign */
1382
ir_binop_sub, /* ast_sub_assign */
1383
ir_binop_lshift, /* ast_ls_assign */
1384
ir_binop_rshift, /* ast_rs_assign */
1385
ir_binop_bit_and, /* ast_and_assign */
1386
ir_binop_bit_xor, /* ast_xor_assign */
1387
ir_binop_bit_or, /* ast_or_assign */
1388
1389
-1, /* ast_conditional doesn't convert to ir_expression. */
1390
ir_binop_add, /* ast_pre_inc. */
1391
ir_binop_sub, /* ast_pre_dec. */
1392
ir_binop_add, /* ast_post_inc. */
1393
ir_binop_sub, /* ast_post_dec. */
1394
-1, /* ast_field_selection doesn't conv to ir_expression. */
1395
-1, /* ast_array_index doesn't convert to ir_expression. */
1396
-1, /* ast_function_call doesn't conv to ir_expression. */
1397
-1, /* ast_identifier doesn't convert to ir_expression. */
1398
-1, /* ast_int_constant doesn't convert to ir_expression. */
1399
-1, /* ast_uint_constant doesn't conv to ir_expression. */
1400
-1, /* ast_float_constant doesn't conv to ir_expression. */
1401
-1, /* ast_bool_constant doesn't conv to ir_expression. */
1402
-1, /* ast_sequence doesn't convert to ir_expression. */
1403
-1, /* ast_aggregate shouldn't ever even get here. */
1404
};
1405
ir_rvalue *result = NULL;
1406
ir_rvalue *op[3];
1407
const struct glsl_type *type, *orig_type;
1408
bool error_emitted = false;
1409
YYLTYPE loc;
1410
1411
loc = this->get_location();
1412
1413
switch (this->oper) {
1414
case ast_aggregate:
1415
unreachable("ast_aggregate: Should never get here.");
1416
1417
case ast_assign: {
1418
this->subexpressions[0]->set_is_lhs(true);
1419
op[0] = this->subexpressions[0]->hir(instructions, state);
1420
op[1] = this->subexpressions[1]->hir(instructions, state);
1421
1422
error_emitted =
1423
do_assignment(instructions, state,
1424
this->subexpressions[0]->non_lvalue_description,
1425
op[0], op[1], &result, needs_rvalue, false,
1426
this->subexpressions[0]->get_location());
1427
break;
1428
}
1429
1430
case ast_plus:
1431
op[0] = this->subexpressions[0]->hir(instructions, state);
1432
1433
type = unary_arithmetic_result_type(op[0]->type, state, & loc);
1434
1435
error_emitted = type->is_error();
1436
1437
result = op[0];
1438
break;
1439
1440
case ast_neg:
1441
op[0] = this->subexpressions[0]->hir(instructions, state);
1442
1443
type = unary_arithmetic_result_type(op[0]->type, state, & loc);
1444
1445
error_emitted = type->is_error();
1446
1447
result = new(ctx) ir_expression(operations[this->oper], type,
1448
op[0], NULL);
1449
break;
1450
1451
case ast_add:
1452
case ast_sub:
1453
case ast_mul:
1454
case ast_div:
1455
op[0] = this->subexpressions[0]->hir(instructions, state);
1456
op[1] = this->subexpressions[1]->hir(instructions, state);
1457
1458
type = arithmetic_result_type(op[0], op[1],
1459
(this->oper == ast_mul),
1460
state, & loc);
1461
error_emitted = type->is_error();
1462
1463
result = new(ctx) ir_expression(operations[this->oper], type,
1464
op[0], op[1]);
1465
break;
1466
1467
case ast_mod:
1468
op[0] = this->subexpressions[0]->hir(instructions, state);
1469
op[1] = this->subexpressions[1]->hir(instructions, state);
1470
1471
type = modulus_result_type(op[0], op[1], state, &loc);
1472
1473
assert(operations[this->oper] == ir_binop_mod);
1474
1475
result = new(ctx) ir_expression(operations[this->oper], type,
1476
op[0], op[1]);
1477
error_emitted = type->is_error();
1478
break;
1479
1480
case ast_lshift:
1481
case ast_rshift:
1482
if (!state->check_bitwise_operations_allowed(&loc)) {
1483
error_emitted = true;
1484
}
1485
1486
op[0] = this->subexpressions[0]->hir(instructions, state);
1487
op[1] = this->subexpressions[1]->hir(instructions, state);
1488
type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1489
&loc);
1490
result = new(ctx) ir_expression(operations[this->oper], type,
1491
op[0], op[1]);
1492
error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1493
break;
1494
1495
case ast_less:
1496
case ast_greater:
1497
case ast_lequal:
1498
case ast_gequal:
1499
op[0] = this->subexpressions[0]->hir(instructions, state);
1500
op[1] = this->subexpressions[1]->hir(instructions, state);
1501
1502
type = relational_result_type(op[0], op[1], state, & loc);
1503
1504
/* The relational operators must either generate an error or result
1505
* in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1506
*/
1507
assert(type->is_error()
1508
|| (type->is_boolean() && type->is_scalar()));
1509
1510
/* Like NIR, GLSL IR does not have opcodes for > or <=. Instead, swap
1511
* the arguments and use < or >=.
1512
*/
1513
if (this->oper == ast_greater || this->oper == ast_lequal) {
1514
ir_rvalue *const tmp = op[0];
1515
op[0] = op[1];
1516
op[1] = tmp;
1517
}
1518
1519
result = new(ctx) ir_expression(operations[this->oper], type,
1520
op[0], op[1]);
1521
error_emitted = type->is_error();
1522
break;
1523
1524
case ast_nequal:
1525
case ast_equal:
1526
op[0] = this->subexpressions[0]->hir(instructions, state);
1527
op[1] = this->subexpressions[1]->hir(instructions, state);
1528
1529
/* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1530
*
1531
* "The equality operators equal (==), and not equal (!=)
1532
* operate on all types. They result in a scalar Boolean. If
1533
* the operand types do not match, then there must be a
1534
* conversion from Section 4.1.10 "Implicit Conversions"
1535
* applied to one operand that can make them match, in which
1536
* case this conversion is done."
1537
*/
1538
1539
if (op[0]->type == glsl_type::void_type || op[1]->type == glsl_type::void_type) {
1540
_mesa_glsl_error(& loc, state, "`%s': wrong operand types: "
1541
"no operation `%1$s' exists that takes a left-hand "
1542
"operand of type 'void' or a right operand of type "
1543
"'void'", (this->oper == ast_equal) ? "==" : "!=");
1544
error_emitted = true;
1545
} else if ((!apply_implicit_conversion(op[0]->type, op[1], state)
1546
&& !apply_implicit_conversion(op[1]->type, op[0], state))
1547
|| (op[0]->type != op[1]->type)) {
1548
_mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
1549
"type", (this->oper == ast_equal) ? "==" : "!=");
1550
error_emitted = true;
1551
} else if ((op[0]->type->is_array() || op[1]->type->is_array()) &&
1552
!state->check_version(120, 300, &loc,
1553
"array comparisons forbidden")) {
1554
error_emitted = true;
1555
} else if ((op[0]->type->contains_subroutine() ||
1556
op[1]->type->contains_subroutine())) {
1557
_mesa_glsl_error(&loc, state, "subroutine comparisons forbidden");
1558
error_emitted = true;
1559
} else if ((op[0]->type->contains_opaque() ||
1560
op[1]->type->contains_opaque())) {
1561
_mesa_glsl_error(&loc, state, "opaque type comparisons forbidden");
1562
error_emitted = true;
1563
}
1564
1565
if (error_emitted) {
1566
result = new(ctx) ir_constant(false);
1567
} else {
1568
result = do_comparison(ctx, operations[this->oper], op[0], op[1]);
1569
assert(result->type == glsl_type::bool_type);
1570
}
1571
break;
1572
1573
case ast_bit_and:
1574
case ast_bit_xor:
1575
case ast_bit_or:
1576
op[0] = this->subexpressions[0]->hir(instructions, state);
1577
op[1] = this->subexpressions[1]->hir(instructions, state);
1578
type = bit_logic_result_type(op[0], op[1], this->oper, state, &loc);
1579
result = new(ctx) ir_expression(operations[this->oper], type,
1580
op[0], op[1]);
1581
error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1582
break;
1583
1584
case ast_bit_not:
1585
op[0] = this->subexpressions[0]->hir(instructions, state);
1586
1587
if (!state->check_bitwise_operations_allowed(&loc)) {
1588
error_emitted = true;
1589
}
1590
1591
if (!op[0]->type->is_integer_32_64()) {
1592
_mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
1593
error_emitted = true;
1594
}
1595
1596
type = error_emitted ? glsl_type::error_type : op[0]->type;
1597
result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
1598
break;
1599
1600
case ast_logic_and: {
1601
exec_list rhs_instructions;
1602
op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1603
"LHS", &error_emitted);
1604
op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
1605
"RHS", &error_emitted);
1606
1607
if (rhs_instructions.is_empty()) {
1608
result = new(ctx) ir_expression(ir_binop_logic_and, op[0], op[1]);
1609
} else {
1610
ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
1611
"and_tmp",
1612
ir_var_temporary);
1613
instructions->push_tail(tmp);
1614
1615
ir_if *const stmt = new(ctx) ir_if(op[0]);
1616
instructions->push_tail(stmt);
1617
1618
stmt->then_instructions.append_list(&rhs_instructions);
1619
ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
1620
ir_assignment *const then_assign =
1621
new(ctx) ir_assignment(then_deref, op[1]);
1622
stmt->then_instructions.push_tail(then_assign);
1623
1624
ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
1625
ir_assignment *const else_assign =
1626
new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false));
1627
stmt->else_instructions.push_tail(else_assign);
1628
1629
result = new(ctx) ir_dereference_variable(tmp);
1630
}
1631
break;
1632
}
1633
1634
case ast_logic_or: {
1635
exec_list rhs_instructions;
1636
op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1637
"LHS", &error_emitted);
1638
op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
1639
"RHS", &error_emitted);
1640
1641
if (rhs_instructions.is_empty()) {
1642
result = new(ctx) ir_expression(ir_binop_logic_or, op[0], op[1]);
1643
} else {
1644
ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
1645
"or_tmp",
1646
ir_var_temporary);
1647
instructions->push_tail(tmp);
1648
1649
ir_if *const stmt = new(ctx) ir_if(op[0]);
1650
instructions->push_tail(stmt);
1651
1652
ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
1653
ir_assignment *const then_assign =
1654
new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true));
1655
stmt->then_instructions.push_tail(then_assign);
1656
1657
stmt->else_instructions.append_list(&rhs_instructions);
1658
ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
1659
ir_assignment *const else_assign =
1660
new(ctx) ir_assignment(else_deref, op[1]);
1661
stmt->else_instructions.push_tail(else_assign);
1662
1663
result = new(ctx) ir_dereference_variable(tmp);
1664
}
1665
break;
1666
}
1667
1668
case ast_logic_xor:
1669
/* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1670
*
1671
* "The logical binary operators and (&&), or ( | | ), and
1672
* exclusive or (^^). They operate only on two Boolean
1673
* expressions and result in a Boolean expression."
1674
*/
1675
op[0] = get_scalar_boolean_operand(instructions, state, this, 0, "LHS",
1676
&error_emitted);
1677
op[1] = get_scalar_boolean_operand(instructions, state, this, 1, "RHS",
1678
&error_emitted);
1679
1680
result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
1681
op[0], op[1]);
1682
break;
1683
1684
case ast_logic_not:
1685
op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1686
"operand", &error_emitted);
1687
1688
result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
1689
op[0], NULL);
1690
break;
1691
1692
case ast_mul_assign:
1693
case ast_div_assign:
1694
case ast_add_assign:
1695
case ast_sub_assign: {
1696
this->subexpressions[0]->set_is_lhs(true);
1697
op[0] = this->subexpressions[0]->hir(instructions, state);
1698
op[1] = this->subexpressions[1]->hir(instructions, state);
1699
1700
orig_type = op[0]->type;
1701
1702
/* Break out if operand types were not parsed successfully. */
1703
if ((op[0]->type == glsl_type::error_type ||
1704
op[1]->type == glsl_type::error_type)) {
1705
error_emitted = true;
1706
break;
1707
}
1708
1709
type = arithmetic_result_type(op[0], op[1],
1710
(this->oper == ast_mul_assign),
1711
state, & loc);
1712
1713
if (type != orig_type) {
1714
_mesa_glsl_error(& loc, state,
1715
"could not implicitly convert "
1716
"%s to %s", type->name, orig_type->name);
1717
type = glsl_type::error_type;
1718
}
1719
1720
ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1721
op[0], op[1]);
1722
1723
error_emitted =
1724
do_assignment(instructions, state,
1725
this->subexpressions[0]->non_lvalue_description,
1726
op[0]->clone(ctx, NULL), temp_rhs,
1727
&result, needs_rvalue, false,
1728
this->subexpressions[0]->get_location());
1729
1730
/* GLSL 1.10 does not allow array assignment. However, we don't have to
1731
* explicitly test for this because none of the binary expression
1732
* operators allow array operands either.
1733
*/
1734
1735
break;
1736
}
1737
1738
case ast_mod_assign: {
1739
this->subexpressions[0]->set_is_lhs(true);
1740
op[0] = this->subexpressions[0]->hir(instructions, state);
1741
op[1] = this->subexpressions[1]->hir(instructions, state);
1742
1743
orig_type = op[0]->type;
1744
type = modulus_result_type(op[0], op[1], state, &loc);
1745
1746
if (type != orig_type) {
1747
_mesa_glsl_error(& loc, state,
1748
"could not implicitly convert "
1749
"%s to %s", type->name, orig_type->name);
1750
type = glsl_type::error_type;
1751
}
1752
1753
assert(operations[this->oper] == ir_binop_mod);
1754
1755
ir_rvalue *temp_rhs;
1756
temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1757
op[0], op[1]);
1758
1759
error_emitted =
1760
do_assignment(instructions, state,
1761
this->subexpressions[0]->non_lvalue_description,
1762
op[0]->clone(ctx, NULL), temp_rhs,
1763
&result, needs_rvalue, false,
1764
this->subexpressions[0]->get_location());
1765
break;
1766
}
1767
1768
case ast_ls_assign:
1769
case ast_rs_assign: {
1770
this->subexpressions[0]->set_is_lhs(true);
1771
op[0] = this->subexpressions[0]->hir(instructions, state);
1772
op[1] = this->subexpressions[1]->hir(instructions, state);
1773
type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1774
&loc);
1775
ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1776
type, op[0], op[1]);
1777
error_emitted =
1778
do_assignment(instructions, state,
1779
this->subexpressions[0]->non_lvalue_description,
1780
op[0]->clone(ctx, NULL), temp_rhs,
1781
&result, needs_rvalue, false,
1782
this->subexpressions[0]->get_location());
1783
break;
1784
}
1785
1786
case ast_and_assign:
1787
case ast_xor_assign:
1788
case ast_or_assign: {
1789
this->subexpressions[0]->set_is_lhs(true);
1790
op[0] = this->subexpressions[0]->hir(instructions, state);
1791
op[1] = this->subexpressions[1]->hir(instructions, state);
1792
1793
orig_type = op[0]->type;
1794
type = bit_logic_result_type(op[0], op[1], this->oper, state, &loc);
1795
1796
if (type != orig_type) {
1797
_mesa_glsl_error(& loc, state,
1798
"could not implicitly convert "
1799
"%s to %s", type->name, orig_type->name);
1800
type = glsl_type::error_type;
1801
}
1802
1803
ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1804
type, op[0], op[1]);
1805
error_emitted =
1806
do_assignment(instructions, state,
1807
this->subexpressions[0]->non_lvalue_description,
1808
op[0]->clone(ctx, NULL), temp_rhs,
1809
&result, needs_rvalue, false,
1810
this->subexpressions[0]->get_location());
1811
break;
1812
}
1813
1814
case ast_conditional: {
1815
/* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1816
*
1817
* "The ternary selection operator (?:). It operates on three
1818
* expressions (exp1 ? exp2 : exp3). This operator evaluates the
1819
* first expression, which must result in a scalar Boolean."
1820
*/
1821
op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1822
"condition", &error_emitted);
1823
1824
/* The :? operator is implemented by generating an anonymous temporary
1825
* followed by an if-statement. The last instruction in each branch of
1826
* the if-statement assigns a value to the anonymous temporary. This
1827
* temporary is the r-value of the expression.
1828
*/
1829
exec_list then_instructions;
1830
exec_list else_instructions;
1831
1832
op[1] = this->subexpressions[1]->hir(&then_instructions, state);
1833
op[2] = this->subexpressions[2]->hir(&else_instructions, state);
1834
1835
/* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1836
*
1837
* "The second and third expressions can be any type, as
1838
* long their types match, or there is a conversion in
1839
* Section 4.1.10 "Implicit Conversions" that can be applied
1840
* to one of the expressions to make their types match. This
1841
* resulting matching type is the type of the entire
1842
* expression."
1843
*/
1844
if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1845
&& !apply_implicit_conversion(op[2]->type, op[1], state))
1846
|| (op[1]->type != op[2]->type)) {
1847
YYLTYPE loc = this->subexpressions[1]->get_location();
1848
1849
_mesa_glsl_error(& loc, state, "second and third operands of ?: "
1850
"operator must have matching types");
1851
error_emitted = true;
1852
type = glsl_type::error_type;
1853
} else {
1854
type = op[1]->type;
1855
}
1856
1857
/* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1858
*
1859
* "The second and third expressions must be the same type, but can
1860
* be of any type other than an array."
1861
*/
1862
if (type->is_array() &&
1863
!state->check_version(120, 300, &loc,
1864
"second and third operands of ?: operator "
1865
"cannot be arrays")) {
1866
error_emitted = true;
1867
}
1868
1869
/* From section 4.1.7 of the GLSL 4.50 spec (Opaque Types):
1870
*
1871
* "Except for array indexing, structure member selection, and
1872
* parentheses, opaque variables are not allowed to be operands in
1873
* expressions; such use results in a compile-time error."
1874
*/
1875
if (type->contains_opaque()) {
1876
if (!(state->has_bindless() && (type->is_image() || type->is_sampler()))) {
1877
_mesa_glsl_error(&loc, state, "variables of type %s cannot be "
1878
"operands of the ?: operator", type->name);
1879
error_emitted = true;
1880
}
1881
}
1882
1883
ir_constant *cond_val = op[0]->constant_expression_value(ctx);
1884
1885
if (then_instructions.is_empty()
1886
&& else_instructions.is_empty()
1887
&& cond_val != NULL) {
1888
result = cond_val->value.b[0] ? op[1] : op[2];
1889
} else {
1890
/* The copy to conditional_tmp reads the whole array. */
1891
if (type->is_array()) {
1892
mark_whole_array_access(op[1]);
1893
mark_whole_array_access(op[2]);
1894
}
1895
1896
ir_variable *const tmp =
1897
new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
1898
instructions->push_tail(tmp);
1899
1900
ir_if *const stmt = new(ctx) ir_if(op[0]);
1901
instructions->push_tail(stmt);
1902
1903
then_instructions.move_nodes_to(& stmt->then_instructions);
1904
ir_dereference *const then_deref =
1905
new(ctx) ir_dereference_variable(tmp);
1906
ir_assignment *const then_assign =
1907
new(ctx) ir_assignment(then_deref, op[1]);
1908
stmt->then_instructions.push_tail(then_assign);
1909
1910
else_instructions.move_nodes_to(& stmt->else_instructions);
1911
ir_dereference *const else_deref =
1912
new(ctx) ir_dereference_variable(tmp);
1913
ir_assignment *const else_assign =
1914
new(ctx) ir_assignment(else_deref, op[2]);
1915
stmt->else_instructions.push_tail(else_assign);
1916
1917
result = new(ctx) ir_dereference_variable(tmp);
1918
}
1919
break;
1920
}
1921
1922
case ast_pre_inc:
1923
case ast_pre_dec: {
1924
this->non_lvalue_description = (this->oper == ast_pre_inc)
1925
? "pre-increment operation" : "pre-decrement operation";
1926
1927
op[0] = this->subexpressions[0]->hir(instructions, state);
1928
op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
1929
1930
type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1931
1932
ir_rvalue *temp_rhs;
1933
temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1934
op[0], op[1]);
1935
1936
error_emitted =
1937
do_assignment(instructions, state,
1938
this->subexpressions[0]->non_lvalue_description,
1939
op[0]->clone(ctx, NULL), temp_rhs,
1940
&result, needs_rvalue, false,
1941
this->subexpressions[0]->get_location());
1942
break;
1943
}
1944
1945
case ast_post_inc:
1946
case ast_post_dec: {
1947
this->non_lvalue_description = (this->oper == ast_post_inc)
1948
? "post-increment operation" : "post-decrement operation";
1949
op[0] = this->subexpressions[0]->hir(instructions, state);
1950
op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
1951
1952
error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1953
1954
if (error_emitted) {
1955
result = ir_rvalue::error_value(ctx);
1956
break;
1957
}
1958
1959
type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1960
1961
ir_rvalue *temp_rhs;
1962
temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1963
op[0], op[1]);
1964
1965
/* Get a temporary of a copy of the lvalue before it's modified.
1966
* This may get thrown away later.
1967
*/
1968
result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
1969
1970
ir_rvalue *junk_rvalue;
1971
error_emitted =
1972
do_assignment(instructions, state,
1973
this->subexpressions[0]->non_lvalue_description,
1974
op[0]->clone(ctx, NULL), temp_rhs,
1975
&junk_rvalue, false, false,
1976
this->subexpressions[0]->get_location());
1977
1978
break;
1979
}
1980
1981
case ast_field_selection:
1982
result = _mesa_ast_field_selection_to_hir(this, instructions, state);
1983
break;
1984
1985
case ast_array_index: {
1986
YYLTYPE index_loc = subexpressions[1]->get_location();
1987
1988
/* Getting if an array is being used uninitialized is beyond what we get
1989
* from ir_value.data.assigned. Setting is_lhs as true would force to
1990
* not raise a uninitialized warning when using an array
1991
*/
1992
subexpressions[0]->set_is_lhs(true);
1993
op[0] = subexpressions[0]->hir(instructions, state);
1994
op[1] = subexpressions[1]->hir(instructions, state);
1995
1996
result = _mesa_ast_array_index_to_hir(ctx, state, op[0], op[1],
1997
loc, index_loc);
1998
1999
if (result->type->is_error())
2000
error_emitted = true;
2001
2002
break;
2003
}
2004
2005
case ast_unsized_array_dim:
2006
unreachable("ast_unsized_array_dim: Should never get here.");
2007
2008
case ast_function_call:
2009
/* Should *NEVER* get here. ast_function_call should always be handled
2010
* by ast_function_expression::hir.
2011
*/
2012
unreachable("ast_function_call: handled elsewhere ");
2013
2014
case ast_identifier: {
2015
/* ast_identifier can appear several places in a full abstract syntax
2016
* tree. This particular use must be at location specified in the grammar
2017
* as 'variable_identifier'.
2018
*/
2019
ir_variable *var =
2020
state->symbols->get_variable(this->primary_expression.identifier);
2021
2022
if (var == NULL) {
2023
/* the identifier might be a subroutine name */
2024
char *sub_name;
2025
sub_name = ralloc_asprintf(ctx, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state->stage), this->primary_expression.identifier);
2026
var = state->symbols->get_variable(sub_name);
2027
ralloc_free(sub_name);
2028
}
2029
2030
if (var != NULL) {
2031
var->data.used = true;
2032
result = new(ctx) ir_dereference_variable(var);
2033
2034
if ((var->data.mode == ir_var_auto || var->data.mode == ir_var_shader_out)
2035
&& !this->is_lhs
2036
&& result->variable_referenced()->data.assigned != true
2037
&& !is_gl_identifier(var->name)) {
2038
_mesa_glsl_warning(&loc, state, "`%s' used uninitialized",
2039
this->primary_expression.identifier);
2040
}
2041
2042
/* From the EXT_shader_framebuffer_fetch spec:
2043
*
2044
* "Unless the GL_EXT_shader_framebuffer_fetch extension has been
2045
* enabled in addition, it's an error to use gl_LastFragData if it
2046
* hasn't been explicitly redeclared with layout(noncoherent)."
2047
*/
2048
if (var->data.fb_fetch_output && var->data.memory_coherent &&
2049
!state->EXT_shader_framebuffer_fetch_enable) {
2050
_mesa_glsl_error(&loc, state,
2051
"invalid use of framebuffer fetch output not "
2052
"qualified with layout(noncoherent)");
2053
}
2054
2055
} else {
2056
_mesa_glsl_error(& loc, state, "`%s' undeclared",
2057
this->primary_expression.identifier);
2058
2059
result = ir_rvalue::error_value(ctx);
2060
error_emitted = true;
2061
}
2062
break;
2063
}
2064
2065
case ast_int_constant:
2066
result = new(ctx) ir_constant(this->primary_expression.int_constant);
2067
break;
2068
2069
case ast_uint_constant:
2070
result = new(ctx) ir_constant(this->primary_expression.uint_constant);
2071
break;
2072
2073
case ast_float_constant:
2074
result = new(ctx) ir_constant(this->primary_expression.float_constant);
2075
break;
2076
2077
case ast_bool_constant:
2078
result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
2079
break;
2080
2081
case ast_double_constant:
2082
result = new(ctx) ir_constant(this->primary_expression.double_constant);
2083
break;
2084
2085
case ast_uint64_constant:
2086
result = new(ctx) ir_constant(this->primary_expression.uint64_constant);
2087
break;
2088
2089
case ast_int64_constant:
2090
result = new(ctx) ir_constant(this->primary_expression.int64_constant);
2091
break;
2092
2093
case ast_sequence: {
2094
/* It should not be possible to generate a sequence in the AST without
2095
* any expressions in it.
2096
*/
2097
assert(!this->expressions.is_empty());
2098
2099
/* The r-value of a sequence is the last expression in the sequence. If
2100
* the other expressions in the sequence do not have side-effects (and
2101
* therefore add instructions to the instruction list), they get dropped
2102
* on the floor.
2103
*/
2104
exec_node *previous_tail = NULL;
2105
YYLTYPE previous_operand_loc = loc;
2106
2107
foreach_list_typed (ast_node, ast, link, &this->expressions) {
2108
/* If one of the operands of comma operator does not generate any
2109
* code, we want to emit a warning. At each pass through the loop
2110
* previous_tail will point to the last instruction in the stream
2111
* *before* processing the previous operand. Naturally,
2112
* instructions->get_tail_raw() will point to the last instruction in
2113
* the stream *after* processing the previous operand. If the two
2114
* pointers match, then the previous operand had no effect.
2115
*
2116
* The warning behavior here differs slightly from GCC. GCC will
2117
* only emit a warning if none of the left-hand operands have an
2118
* effect. However, it will emit a warning for each. I believe that
2119
* there are some cases in C (especially with GCC extensions) where
2120
* it is useful to have an intermediate step in a sequence have no
2121
* effect, but I don't think these cases exist in GLSL. Either way,
2122
* it would be a giant hassle to replicate that behavior.
2123
*/
2124
if (previous_tail == instructions->get_tail_raw()) {
2125
_mesa_glsl_warning(&previous_operand_loc, state,
2126
"left-hand operand of comma expression has "
2127
"no effect");
2128
}
2129
2130
/* The tail is directly accessed instead of using the get_tail()
2131
* method for performance reasons. get_tail() has extra code to
2132
* return NULL when the list is empty. We don't care about that
2133
* here, so using get_tail_raw() is fine.
2134
*/
2135
previous_tail = instructions->get_tail_raw();
2136
previous_operand_loc = ast->get_location();
2137
2138
result = ast->hir(instructions, state);
2139
}
2140
2141
/* Any errors should have already been emitted in the loop above.
2142
*/
2143
error_emitted = true;
2144
break;
2145
}
2146
}
2147
type = NULL; /* use result->type, not type. */
2148
assert(error_emitted || (result != NULL || !needs_rvalue));
2149
2150
if (result && result->type->is_error() && !error_emitted)
2151
_mesa_glsl_error(& loc, state, "type mismatch");
2152
2153
return result;
2154
}
2155
2156
bool
2157
ast_expression::has_sequence_subexpression() const
2158
{
2159
switch (this->oper) {
2160
case ast_plus:
2161
case ast_neg:
2162
case ast_bit_not:
2163
case ast_logic_not:
2164
case ast_pre_inc:
2165
case ast_pre_dec:
2166
case ast_post_inc:
2167
case ast_post_dec:
2168
return this->subexpressions[0]->has_sequence_subexpression();
2169
2170
case ast_assign:
2171
case ast_add:
2172
case ast_sub:
2173
case ast_mul:
2174
case ast_div:
2175
case ast_mod:
2176
case ast_lshift:
2177
case ast_rshift:
2178
case ast_less:
2179
case ast_greater:
2180
case ast_lequal:
2181
case ast_gequal:
2182
case ast_nequal:
2183
case ast_equal:
2184
case ast_bit_and:
2185
case ast_bit_xor:
2186
case ast_bit_or:
2187
case ast_logic_and:
2188
case ast_logic_or:
2189
case ast_logic_xor:
2190
case ast_array_index:
2191
case ast_mul_assign:
2192
case ast_div_assign:
2193
case ast_add_assign:
2194
case ast_sub_assign:
2195
case ast_mod_assign:
2196
case ast_ls_assign:
2197
case ast_rs_assign:
2198
case ast_and_assign:
2199
case ast_xor_assign:
2200
case ast_or_assign:
2201
return this->subexpressions[0]->has_sequence_subexpression() ||
2202
this->subexpressions[1]->has_sequence_subexpression();
2203
2204
case ast_conditional:
2205
return this->subexpressions[0]->has_sequence_subexpression() ||
2206
this->subexpressions[1]->has_sequence_subexpression() ||
2207
this->subexpressions[2]->has_sequence_subexpression();
2208
2209
case ast_sequence:
2210
return true;
2211
2212
case ast_field_selection:
2213
case ast_identifier:
2214
case ast_int_constant:
2215
case ast_uint_constant:
2216
case ast_float_constant:
2217
case ast_bool_constant:
2218
case ast_double_constant:
2219
case ast_int64_constant:
2220
case ast_uint64_constant:
2221
return false;
2222
2223
case ast_aggregate:
2224
return false;
2225
2226
case ast_function_call:
2227
unreachable("should be handled by ast_function_expression::hir");
2228
2229
case ast_unsized_array_dim:
2230
unreachable("ast_unsized_array_dim: Should never get here.");
2231
}
2232
2233
return false;
2234
}
2235
2236
ir_rvalue *
2237
ast_expression_statement::hir(exec_list *instructions,
2238
struct _mesa_glsl_parse_state *state)
2239
{
2240
/* It is possible to have expression statements that don't have an
2241
* expression. This is the solitary semicolon:
2242
*
2243
* for (i = 0; i < 5; i++)
2244
* ;
2245
*
2246
* In this case the expression will be NULL. Test for NULL and don't do
2247
* anything in that case.
2248
*/
2249
if (expression != NULL)
2250
expression->hir_no_rvalue(instructions, state);
2251
2252
/* Statements do not have r-values.
2253
*/
2254
return NULL;
2255
}
2256
2257
2258
ir_rvalue *
2259
ast_compound_statement::hir(exec_list *instructions,
2260
struct _mesa_glsl_parse_state *state)
2261
{
2262
if (new_scope)
2263
state->symbols->push_scope();
2264
2265
foreach_list_typed (ast_node, ast, link, &this->statements)
2266
ast->hir(instructions, state);
2267
2268
if (new_scope)
2269
state->symbols->pop_scope();
2270
2271
/* Compound statements do not have r-values.
2272
*/
2273
return NULL;
2274
}
2275
2276
/**
2277
* Evaluate the given exec_node (which should be an ast_node representing
2278
* a single array dimension) and return its integer value.
2279
*/
2280
static unsigned
2281
process_array_size(exec_node *node,
2282
struct _mesa_glsl_parse_state *state)
2283
{
2284
void *mem_ctx = state;
2285
2286
exec_list dummy_instructions;
2287
2288
ast_node *array_size = exec_node_data(ast_node, node, link);
2289
2290
/**
2291
* Dimensions other than the outermost dimension can by unsized if they
2292
* are immediately sized by a constructor or initializer.
2293
*/
2294
if (((ast_expression*)array_size)->oper == ast_unsized_array_dim)
2295
return 0;
2296
2297
ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
2298
YYLTYPE loc = array_size->get_location();
2299
2300
if (ir == NULL) {
2301
_mesa_glsl_error(& loc, state,
2302
"array size could not be resolved");
2303
return 0;
2304
}
2305
2306
if (!ir->type->is_integer_32()) {
2307
_mesa_glsl_error(& loc, state,
2308
"array size must be integer type");
2309
return 0;
2310
}
2311
2312
if (!ir->type->is_scalar()) {
2313
_mesa_glsl_error(& loc, state,
2314
"array size must be scalar type");
2315
return 0;
2316
}
2317
2318
ir_constant *const size = ir->constant_expression_value(mem_ctx);
2319
if (size == NULL ||
2320
(state->is_version(120, 300) &&
2321
array_size->has_sequence_subexpression())) {
2322
_mesa_glsl_error(& loc, state, "array size must be a "
2323
"constant valued expression");
2324
return 0;
2325
}
2326
2327
if (size->value.i[0] <= 0) {
2328
_mesa_glsl_error(& loc, state, "array size must be > 0");
2329
return 0;
2330
}
2331
2332
assert(size->type == ir->type);
2333
2334
/* If the array size is const (and we've verified that
2335
* it is) then no instructions should have been emitted
2336
* when we converted it to HIR. If they were emitted,
2337
* then either the array size isn't const after all, or
2338
* we are emitting unnecessary instructions.
2339
*/
2340
assert(dummy_instructions.is_empty());
2341
2342
return size->value.u[0];
2343
}
2344
2345
static const glsl_type *
2346
process_array_type(YYLTYPE *loc, const glsl_type *base,
2347
ast_array_specifier *array_specifier,
2348
struct _mesa_glsl_parse_state *state)
2349
{
2350
const glsl_type *array_type = base;
2351
2352
if (array_specifier != NULL) {
2353
if (base->is_array()) {
2354
2355
/* From page 19 (page 25) of the GLSL 1.20 spec:
2356
*
2357
* "Only one-dimensional arrays may be declared."
2358
*/
2359
if (!state->check_arrays_of_arrays_allowed(loc)) {
2360
return glsl_type::error_type;
2361
}
2362
}
2363
2364
for (exec_node *node = array_specifier->array_dimensions.get_tail_raw();
2365
!node->is_head_sentinel(); node = node->prev) {
2366
unsigned array_size = process_array_size(node, state);
2367
array_type = glsl_type::get_array_instance(array_type, array_size);
2368
}
2369
}
2370
2371
return array_type;
2372
}
2373
2374
static bool
2375
precision_qualifier_allowed(const glsl_type *type)
2376
{
2377
/* Precision qualifiers apply to floating point, integer and opaque
2378
* types.
2379
*
2380
* Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
2381
* "Any floating point or any integer declaration can have the type
2382
* preceded by one of these precision qualifiers [...] Literal
2383
* constants do not have precision qualifiers. Neither do Boolean
2384
* variables.
2385
*
2386
* Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
2387
* spec also says:
2388
*
2389
* "Precision qualifiers are added for code portability with OpenGL
2390
* ES, not for functionality. They have the same syntax as in OpenGL
2391
* ES."
2392
*
2393
* Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
2394
*
2395
* "uniform lowp sampler2D sampler;
2396
* highp vec2 coord;
2397
* ...
2398
* lowp vec4 col = texture2D (sampler, coord);
2399
* // texture2D returns lowp"
2400
*
2401
* From this, we infer that GLSL 1.30 (and later) should allow precision
2402
* qualifiers on sampler types just like float and integer types.
2403
*/
2404
const glsl_type *const t = type->without_array();
2405
2406
return (t->is_float() || t->is_integer_32() || t->contains_opaque()) &&
2407
!t->is_struct();
2408
}
2409
2410
const glsl_type *
2411
ast_type_specifier::glsl_type(const char **name,
2412
struct _mesa_glsl_parse_state *state) const
2413
{
2414
const struct glsl_type *type;
2415
2416
if (this->type != NULL)
2417
type = this->type;
2418
else if (structure)
2419
type = structure->type;
2420
else
2421
type = state->symbols->get_type(this->type_name);
2422
*name = this->type_name;
2423
2424
YYLTYPE loc = this->get_location();
2425
type = process_array_type(&loc, type, this->array_specifier, state);
2426
2427
return type;
2428
}
2429
2430
/**
2431
* From the OpenGL ES 3.0 spec, 4.5.4 Default Precision Qualifiers:
2432
*
2433
* "The precision statement
2434
*
2435
* precision precision-qualifier type;
2436
*
2437
* can be used to establish a default precision qualifier. The type field can
2438
* be either int or float or any of the sampler types, (...) If type is float,
2439
* the directive applies to non-precision-qualified floating point type
2440
* (scalar, vector, and matrix) declarations. If type is int, the directive
2441
* applies to all non-precision-qualified integer type (scalar, vector, signed,
2442
* and unsigned) declarations."
2443
*
2444
* We use the symbol table to keep the values of the default precisions for
2445
* each 'type' in each scope and we use the 'type' string from the precision
2446
* statement as key in the symbol table. When we want to retrieve the default
2447
* precision associated with a given glsl_type we need to know the type string
2448
* associated with it. This is what this function returns.
2449
*/
2450
static const char *
2451
get_type_name_for_precision_qualifier(const glsl_type *type)
2452
{
2453
switch (type->base_type) {
2454
case GLSL_TYPE_FLOAT:
2455
return "float";
2456
case GLSL_TYPE_UINT:
2457
case GLSL_TYPE_INT:
2458
return "int";
2459
case GLSL_TYPE_ATOMIC_UINT:
2460
return "atomic_uint";
2461
case GLSL_TYPE_IMAGE:
2462
FALLTHROUGH;
2463
case GLSL_TYPE_SAMPLER: {
2464
const unsigned type_idx =
2465
type->sampler_array + 2 * type->sampler_shadow;
2466
const unsigned offset = type->is_sampler() ? 0 : 4;
2467
assert(type_idx < 4);
2468
switch (type->sampled_type) {
2469
case GLSL_TYPE_FLOAT:
2470
switch (type->sampler_dimensionality) {
2471
case GLSL_SAMPLER_DIM_1D: {
2472
assert(type->is_sampler());
2473
static const char *const names[4] = {
2474
"sampler1D", "sampler1DArray",
2475
"sampler1DShadow", "sampler1DArrayShadow"
2476
};
2477
return names[type_idx];
2478
}
2479
case GLSL_SAMPLER_DIM_2D: {
2480
static const char *const names[8] = {
2481
"sampler2D", "sampler2DArray",
2482
"sampler2DShadow", "sampler2DArrayShadow",
2483
"image2D", "image2DArray", NULL, NULL
2484
};
2485
return names[offset + type_idx];
2486
}
2487
case GLSL_SAMPLER_DIM_3D: {
2488
static const char *const names[8] = {
2489
"sampler3D", NULL, NULL, NULL,
2490
"image3D", NULL, NULL, NULL
2491
};
2492
return names[offset + type_idx];
2493
}
2494
case GLSL_SAMPLER_DIM_CUBE: {
2495
static const char *const names[8] = {
2496
"samplerCube", "samplerCubeArray",
2497
"samplerCubeShadow", "samplerCubeArrayShadow",
2498
"imageCube", NULL, NULL, NULL
2499
};
2500
return names[offset + type_idx];
2501
}
2502
case GLSL_SAMPLER_DIM_MS: {
2503
assert(type->is_sampler());
2504
static const char *const names[4] = {
2505
"sampler2DMS", "sampler2DMSArray", NULL, NULL
2506
};
2507
return names[type_idx];
2508
}
2509
case GLSL_SAMPLER_DIM_RECT: {
2510
assert(type->is_sampler());
2511
static const char *const names[4] = {
2512
"samplerRect", NULL, "samplerRectShadow", NULL
2513
};
2514
return names[type_idx];
2515
}
2516
case GLSL_SAMPLER_DIM_BUF: {
2517
static const char *const names[8] = {
2518
"samplerBuffer", NULL, NULL, NULL,
2519
"imageBuffer", NULL, NULL, NULL
2520
};
2521
return names[offset + type_idx];
2522
}
2523
case GLSL_SAMPLER_DIM_EXTERNAL: {
2524
assert(type->is_sampler());
2525
static const char *const names[4] = {
2526
"samplerExternalOES", NULL, NULL, NULL
2527
};
2528
return names[type_idx];
2529
}
2530
default:
2531
unreachable("Unsupported sampler/image dimensionality");
2532
} /* sampler/image float dimensionality */
2533
break;
2534
case GLSL_TYPE_INT:
2535
switch (type->sampler_dimensionality) {
2536
case GLSL_SAMPLER_DIM_1D: {
2537
assert(type->is_sampler());
2538
static const char *const names[4] = {
2539
"isampler1D", "isampler1DArray", NULL, NULL
2540
};
2541
return names[type_idx];
2542
}
2543
case GLSL_SAMPLER_DIM_2D: {
2544
static const char *const names[8] = {
2545
"isampler2D", "isampler2DArray", NULL, NULL,
2546
"iimage2D", "iimage2DArray", NULL, NULL
2547
};
2548
return names[offset + type_idx];
2549
}
2550
case GLSL_SAMPLER_DIM_3D: {
2551
static const char *const names[8] = {
2552
"isampler3D", NULL, NULL, NULL,
2553
"iimage3D", NULL, NULL, NULL
2554
};
2555
return names[offset + type_idx];
2556
}
2557
case GLSL_SAMPLER_DIM_CUBE: {
2558
static const char *const names[8] = {
2559
"isamplerCube", "isamplerCubeArray", NULL, NULL,
2560
"iimageCube", NULL, NULL, NULL
2561
};
2562
return names[offset + type_idx];
2563
}
2564
case GLSL_SAMPLER_DIM_MS: {
2565
assert(type->is_sampler());
2566
static const char *const names[4] = {
2567
"isampler2DMS", "isampler2DMSArray", NULL, NULL
2568
};
2569
return names[type_idx];
2570
}
2571
case GLSL_SAMPLER_DIM_RECT: {
2572
assert(type->is_sampler());
2573
static const char *const names[4] = {
2574
"isamplerRect", NULL, "isamplerRectShadow", NULL
2575
};
2576
return names[type_idx];
2577
}
2578
case GLSL_SAMPLER_DIM_BUF: {
2579
static const char *const names[8] = {
2580
"isamplerBuffer", NULL, NULL, NULL,
2581
"iimageBuffer", NULL, NULL, NULL
2582
};
2583
return names[offset + type_idx];
2584
}
2585
default:
2586
unreachable("Unsupported isampler/iimage dimensionality");
2587
} /* sampler/image int dimensionality */
2588
break;
2589
case GLSL_TYPE_UINT:
2590
switch (type->sampler_dimensionality) {
2591
case GLSL_SAMPLER_DIM_1D: {
2592
assert(type->is_sampler());
2593
static const char *const names[4] = {
2594
"usampler1D", "usampler1DArray", NULL, NULL
2595
};
2596
return names[type_idx];
2597
}
2598
case GLSL_SAMPLER_DIM_2D: {
2599
static const char *const names[8] = {
2600
"usampler2D", "usampler2DArray", NULL, NULL,
2601
"uimage2D", "uimage2DArray", NULL, NULL
2602
};
2603
return names[offset + type_idx];
2604
}
2605
case GLSL_SAMPLER_DIM_3D: {
2606
static const char *const names[8] = {
2607
"usampler3D", NULL, NULL, NULL,
2608
"uimage3D", NULL, NULL, NULL
2609
};
2610
return names[offset + type_idx];
2611
}
2612
case GLSL_SAMPLER_DIM_CUBE: {
2613
static const char *const names[8] = {
2614
"usamplerCube", "usamplerCubeArray", NULL, NULL,
2615
"uimageCube", NULL, NULL, NULL
2616
};
2617
return names[offset + type_idx];
2618
}
2619
case GLSL_SAMPLER_DIM_MS: {
2620
assert(type->is_sampler());
2621
static const char *const names[4] = {
2622
"usampler2DMS", "usampler2DMSArray", NULL, NULL
2623
};
2624
return names[type_idx];
2625
}
2626
case GLSL_SAMPLER_DIM_RECT: {
2627
assert(type->is_sampler());
2628
static const char *const names[4] = {
2629
"usamplerRect", NULL, "usamplerRectShadow", NULL
2630
};
2631
return names[type_idx];
2632
}
2633
case GLSL_SAMPLER_DIM_BUF: {
2634
static const char *const names[8] = {
2635
"usamplerBuffer", NULL, NULL, NULL,
2636
"uimageBuffer", NULL, NULL, NULL
2637
};
2638
return names[offset + type_idx];
2639
}
2640
default:
2641
unreachable("Unsupported usampler/uimage dimensionality");
2642
} /* sampler/image uint dimensionality */
2643
break;
2644
default:
2645
unreachable("Unsupported sampler/image type");
2646
} /* sampler/image type */
2647
break;
2648
} /* GLSL_TYPE_SAMPLER/GLSL_TYPE_IMAGE */
2649
break;
2650
default:
2651
unreachable("Unsupported type");
2652
} /* base type */
2653
}
2654
2655
static unsigned
2656
select_gles_precision(unsigned qual_precision,
2657
const glsl_type *type,
2658
struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
2659
{
2660
/* Precision qualifiers do not have any meaning in Desktop GLSL.
2661
* In GLES we take the precision from the type qualifier if present,
2662
* otherwise, if the type of the variable allows precision qualifiers at
2663
* all, we look for the default precision qualifier for that type in the
2664
* current scope.
2665
*/
2666
assert(state->es_shader);
2667
2668
unsigned precision = GLSL_PRECISION_NONE;
2669
if (qual_precision) {
2670
precision = qual_precision;
2671
} else if (precision_qualifier_allowed(type)) {
2672
const char *type_name =
2673
get_type_name_for_precision_qualifier(type->without_array());
2674
assert(type_name != NULL);
2675
2676
precision =
2677
state->symbols->get_default_precision_qualifier(type_name);
2678
if (precision == ast_precision_none) {
2679
_mesa_glsl_error(loc, state,
2680
"No precision specified in this scope for type `%s'",
2681
type->name);
2682
}
2683
}
2684
2685
2686
/* Section 4.1.7.3 (Atomic Counters) of the GLSL ES 3.10 spec says:
2687
*
2688
* "The default precision of all atomic types is highp. It is an error to
2689
* declare an atomic type with a different precision or to specify the
2690
* default precision for an atomic type to be lowp or mediump."
2691
*/
2692
if (type->is_atomic_uint() && precision != ast_precision_high) {
2693
_mesa_glsl_error(loc, state,
2694
"atomic_uint can only have highp precision qualifier");
2695
}
2696
2697
return precision;
2698
}
2699
2700
const glsl_type *
2701
ast_fully_specified_type::glsl_type(const char **name,
2702
struct _mesa_glsl_parse_state *state) const
2703
{
2704
return this->specifier->glsl_type(name, state);
2705
}
2706
2707
/**
2708
* Determine whether a toplevel variable declaration declares a varying. This
2709
* function operates by examining the variable's mode and the shader target,
2710
* so it correctly identifies linkage variables regardless of whether they are
2711
* declared using the deprecated "varying" syntax or the new "in/out" syntax.
2712
*
2713
* Passing a non-toplevel variable declaration (e.g. a function parameter) to
2714
* this function will produce undefined results.
2715
*/
2716
static bool
2717
is_varying_var(ir_variable *var, gl_shader_stage target)
2718
{
2719
switch (target) {
2720
case MESA_SHADER_VERTEX:
2721
return var->data.mode == ir_var_shader_out;
2722
case MESA_SHADER_FRAGMENT:
2723
return var->data.mode == ir_var_shader_in ||
2724
(var->data.mode == ir_var_system_value &&
2725
var->data.location == SYSTEM_VALUE_FRAG_COORD);
2726
default:
2727
return var->data.mode == ir_var_shader_out || var->data.mode == ir_var_shader_in;
2728
}
2729
}
2730
2731
static bool
2732
is_allowed_invariant(ir_variable *var, struct _mesa_glsl_parse_state *state)
2733
{
2734
if (is_varying_var(var, state->stage))
2735
return true;
2736
2737
/* From Section 4.6.1 ("The Invariant Qualifier") GLSL 1.20 spec:
2738
* "Only variables output from a vertex shader can be candidates
2739
* for invariance".
2740
*/
2741
if (!state->is_version(130, 100))
2742
return false;
2743
2744
/*
2745
* Later specs remove this language - so allowed invariant
2746
* on fragment shader outputs as well.
2747
*/
2748
if (state->stage == MESA_SHADER_FRAGMENT &&
2749
var->data.mode == ir_var_shader_out)
2750
return true;
2751
return false;
2752
}
2753
2754
static void
2755
validate_component_layout_for_type(struct _mesa_glsl_parse_state *state,
2756
YYLTYPE *loc, const glsl_type *type,
2757
unsigned qual_component)
2758
{
2759
type = type->without_array();
2760
unsigned components = type->component_slots();
2761
2762
if (type->is_matrix() || type->is_struct()) {
2763
_mesa_glsl_error(loc, state, "component layout qualifier "
2764
"cannot be applied to a matrix, a structure, "
2765
"a block, or an array containing any of these.");
2766
} else if (components > 4 && type->is_64bit()) {
2767
_mesa_glsl_error(loc, state, "component layout qualifier "
2768
"cannot be applied to dvec%u.",
2769
components / 2);
2770
} else if (qual_component != 0 && (qual_component + components - 1) > 3) {
2771
_mesa_glsl_error(loc, state, "component overflow (%u > 3)",
2772
(qual_component + components - 1));
2773
} else if (qual_component == 1 && type->is_64bit()) {
2774
/* We don't bother checking for 3 as it should be caught by the
2775
* overflow check above.
2776
*/
2777
_mesa_glsl_error(loc, state, "doubles cannot begin at component 1 or 3");
2778
}
2779
}
2780
2781
/**
2782
* Matrix layout qualifiers are only allowed on certain types
2783
*/
2784
static void
2785
validate_matrix_layout_for_type(struct _mesa_glsl_parse_state *state,
2786
YYLTYPE *loc,
2787
const glsl_type *type,
2788
ir_variable *var)
2789
{
2790
if (var && !var->is_in_buffer_block()) {
2791
/* Layout qualifiers may only apply to interface blocks and fields in
2792
* them.
2793
*/
2794
_mesa_glsl_error(loc, state,
2795
"uniform block layout qualifiers row_major and "
2796
"column_major may not be applied to variables "
2797
"outside of uniform blocks");
2798
} else if (!type->without_array()->is_matrix()) {
2799
/* The OpenGL ES 3.0 conformance tests did not originally allow
2800
* matrix layout qualifiers on non-matrices. However, the OpenGL
2801
* 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
2802
* amended to specifically allow these layouts on all types. Emit
2803
* a warning so that people know their code may not be portable.
2804
*/
2805
_mesa_glsl_warning(loc, state,
2806
"uniform block layout qualifiers row_major and "
2807
"column_major applied to non-matrix types may "
2808
"be rejected by older compilers");
2809
}
2810
}
2811
2812
static bool
2813
validate_xfb_buffer_qualifier(YYLTYPE *loc,
2814
struct _mesa_glsl_parse_state *state,
2815
unsigned xfb_buffer) {
2816
if (xfb_buffer >= state->Const.MaxTransformFeedbackBuffers) {
2817
_mesa_glsl_error(loc, state,
2818
"invalid xfb_buffer specified %d is larger than "
2819
"MAX_TRANSFORM_FEEDBACK_BUFFERS - 1 (%d).",
2820
xfb_buffer,
2821
state->Const.MaxTransformFeedbackBuffers - 1);
2822
return false;
2823
}
2824
2825
return true;
2826
}
2827
2828
/* From the ARB_enhanced_layouts spec:
2829
*
2830
* "Variables and block members qualified with *xfb_offset* can be
2831
* scalars, vectors, matrices, structures, and (sized) arrays of these.
2832
* The offset must be a multiple of the size of the first component of
2833
* the first qualified variable or block member, or a compile-time error
2834
* results. Further, if applied to an aggregate containing a double,
2835
* the offset must also be a multiple of 8, and the space taken in the
2836
* buffer will be a multiple of 8.
2837
*/
2838
static bool
2839
validate_xfb_offset_qualifier(YYLTYPE *loc,
2840
struct _mesa_glsl_parse_state *state,
2841
int xfb_offset, const glsl_type *type,
2842
unsigned component_size) {
2843
const glsl_type *t_without_array = type->without_array();
2844
2845
if (xfb_offset != -1 && type->is_unsized_array()) {
2846
_mesa_glsl_error(loc, state,
2847
"xfb_offset can't be used with unsized arrays.");
2848
return false;
2849
}
2850
2851
/* Make sure nested structs don't contain unsized arrays, and validate
2852
* any xfb_offsets on interface members.
2853
*/
2854
if (t_without_array->is_struct() || t_without_array->is_interface())
2855
for (unsigned int i = 0; i < t_without_array->length; i++) {
2856
const glsl_type *member_t = t_without_array->fields.structure[i].type;
2857
2858
/* When the interface block doesn't have an xfb_offset qualifier then
2859
* we apply the component size rules at the member level.
2860
*/
2861
if (xfb_offset == -1)
2862
component_size = member_t->contains_double() ? 8 : 4;
2863
2864
int xfb_offset = t_without_array->fields.structure[i].offset;
2865
validate_xfb_offset_qualifier(loc, state, xfb_offset, member_t,
2866
component_size);
2867
}
2868
2869
/* Nested structs or interface block without offset may not have had an
2870
* offset applied yet so return.
2871
*/
2872
if (xfb_offset == -1) {
2873
return true;
2874
}
2875
2876
if (xfb_offset % component_size) {
2877
_mesa_glsl_error(loc, state,
2878
"invalid qualifier xfb_offset=%d must be a multiple "
2879
"of the first component size of the first qualified "
2880
"variable or block member. Or double if an aggregate "
2881
"that contains a double (%d).",
2882
xfb_offset, component_size);
2883
return false;
2884
}
2885
2886
return true;
2887
}
2888
2889
static bool
2890
validate_stream_qualifier(YYLTYPE *loc, struct _mesa_glsl_parse_state *state,
2891
unsigned stream)
2892
{
2893
if (stream >= state->ctx->Const.MaxVertexStreams) {
2894
_mesa_glsl_error(loc, state,
2895
"invalid stream specified %d is larger than "
2896
"MAX_VERTEX_STREAMS - 1 (%d).",
2897
stream, state->ctx->Const.MaxVertexStreams - 1);
2898
return false;
2899
}
2900
2901
return true;
2902
}
2903
2904
static void
2905
apply_explicit_binding(struct _mesa_glsl_parse_state *state,
2906
YYLTYPE *loc,
2907
ir_variable *var,
2908
const glsl_type *type,
2909
const ast_type_qualifier *qual)
2910
{
2911
if (!qual->flags.q.uniform && !qual->flags.q.buffer) {
2912
_mesa_glsl_error(loc, state,
2913
"the \"binding\" qualifier only applies to uniforms and "
2914
"shader storage buffer objects");
2915
return;
2916
}
2917
2918
unsigned qual_binding;
2919
if (!process_qualifier_constant(state, loc, "binding", qual->binding,
2920
&qual_binding)) {
2921
return;
2922
}
2923
2924
const struct gl_context *const ctx = state->ctx;
2925
unsigned elements = type->is_array() ? type->arrays_of_arrays_size() : 1;
2926
unsigned max_index = qual_binding + elements - 1;
2927
const glsl_type *base_type = type->without_array();
2928
2929
if (base_type->is_interface()) {
2930
/* UBOs. From page 60 of the GLSL 4.20 specification:
2931
* "If the binding point for any uniform block instance is less than zero,
2932
* or greater than or equal to the implementation-dependent maximum
2933
* number of uniform buffer bindings, a compilation error will occur.
2934
* When the binding identifier is used with a uniform block instanced as
2935
* an array of size N, all elements of the array from binding through
2936
* binding + N – 1 must be within this range."
2937
*
2938
* The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
2939
*/
2940
if (qual->flags.q.uniform &&
2941
max_index >= ctx->Const.MaxUniformBufferBindings) {
2942
_mesa_glsl_error(loc, state, "layout(binding = %u) for %d UBOs exceeds "
2943
"the maximum number of UBO binding points (%d)",
2944
qual_binding, elements,
2945
ctx->Const.MaxUniformBufferBindings);
2946
return;
2947
}
2948
2949
/* SSBOs. From page 67 of the GLSL 4.30 specification:
2950
* "If the binding point for any uniform or shader storage block instance
2951
* is less than zero, or greater than or equal to the
2952
* implementation-dependent maximum number of uniform buffer bindings, a
2953
* compile-time error will occur. When the binding identifier is used
2954
* with a uniform or shader storage block instanced as an array of size
2955
* N, all elements of the array from binding through binding + N – 1 must
2956
* be within this range."
2957
*/
2958
if (qual->flags.q.buffer &&
2959
max_index >= ctx->Const.MaxShaderStorageBufferBindings) {
2960
_mesa_glsl_error(loc, state, "layout(binding = %u) for %d SSBOs exceeds "
2961
"the maximum number of SSBO binding points (%d)",
2962
qual_binding, elements,
2963
ctx->Const.MaxShaderStorageBufferBindings);
2964
return;
2965
}
2966
} else if (base_type->is_sampler()) {
2967
/* Samplers. From page 63 of the GLSL 4.20 specification:
2968
* "If the binding is less than zero, or greater than or equal to the
2969
* implementation-dependent maximum supported number of units, a
2970
* compilation error will occur. When the binding identifier is used
2971
* with an array of size N, all elements of the array from binding
2972
* through binding + N - 1 must be within this range."
2973
*/
2974
unsigned limit = ctx->Const.MaxCombinedTextureImageUnits;
2975
2976
if (max_index >= limit) {
2977
_mesa_glsl_error(loc, state, "layout(binding = %d) for %d samplers "
2978
"exceeds the maximum number of texture image units "
2979
"(%u)", qual_binding, elements, limit);
2980
2981
return;
2982
}
2983
} else if (base_type->contains_atomic()) {
2984
assert(ctx->Const.MaxAtomicBufferBindings <= MAX_COMBINED_ATOMIC_BUFFERS);
2985
if (qual_binding >= ctx->Const.MaxAtomicBufferBindings) {
2986
_mesa_glsl_error(loc, state, "layout(binding = %d) exceeds the "
2987
"maximum number of atomic counter buffer bindings "
2988
"(%u)", qual_binding,
2989
ctx->Const.MaxAtomicBufferBindings);
2990
2991
return;
2992
}
2993
} else if ((state->is_version(420, 310) ||
2994
state->ARB_shading_language_420pack_enable) &&
2995
base_type->is_image()) {
2996
assert(ctx->Const.MaxImageUnits <= MAX_IMAGE_UNITS);
2997
if (max_index >= ctx->Const.MaxImageUnits) {
2998
_mesa_glsl_error(loc, state, "Image binding %d exceeds the "
2999
"maximum number of image units (%d)", max_index,
3000
ctx->Const.MaxImageUnits);
3001
return;
3002
}
3003
3004
} else {
3005
_mesa_glsl_error(loc, state,
3006
"the \"binding\" qualifier only applies to uniform "
3007
"blocks, storage blocks, opaque variables, or arrays "
3008
"thereof");
3009
return;
3010
}
3011
3012
var->data.explicit_binding = true;
3013
var->data.binding = qual_binding;
3014
3015
return;
3016
}
3017
3018
static void
3019
validate_fragment_flat_interpolation_input(struct _mesa_glsl_parse_state *state,
3020
YYLTYPE *loc,
3021
const glsl_interp_mode interpolation,
3022
const struct glsl_type *var_type,
3023
ir_variable_mode mode)
3024
{
3025
if (state->stage != MESA_SHADER_FRAGMENT ||
3026
interpolation == INTERP_MODE_FLAT ||
3027
mode != ir_var_shader_in)
3028
return;
3029
3030
/* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
3031
* so must integer vertex outputs.
3032
*
3033
* From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
3034
* "Fragment shader inputs that are signed or unsigned integers or
3035
* integer vectors must be qualified with the interpolation qualifier
3036
* flat."
3037
*
3038
* From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
3039
* "Fragment shader inputs that are, or contain, signed or unsigned
3040
* integers or integer vectors must be qualified with the
3041
* interpolation qualifier flat."
3042
*
3043
* From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
3044
* "Vertex shader outputs that are, or contain, signed or unsigned
3045
* integers or integer vectors must be qualified with the
3046
* interpolation qualifier flat."
3047
*
3048
* Note that prior to GLSL 1.50, this requirement applied to vertex
3049
* outputs rather than fragment inputs. That creates problems in the
3050
* presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
3051
* desktop GL shaders. For GLSL ES shaders, we follow the spec and
3052
* apply the restriction to both vertex outputs and fragment inputs.
3053
*
3054
* Note also that the desktop GLSL specs are missing the text "or
3055
* contain"; this is presumably an oversight, since there is no
3056
* reasonable way to interpolate a fragment shader input that contains
3057
* an integer. See Khronos bug #15671.
3058
*/
3059
if ((state->is_version(130, 300) || state->EXT_gpu_shader4_enable)
3060
&& var_type->contains_integer()) {
3061
_mesa_glsl_error(loc, state, "if a fragment input is (or contains) "
3062
"an integer, then it must be qualified with 'flat'");
3063
}
3064
3065
/* Double fragment inputs must be qualified with 'flat'.
3066
*
3067
* From the "Overview" of the ARB_gpu_shader_fp64 extension spec:
3068
* "This extension does not support interpolation of double-precision
3069
* values; doubles used as fragment shader inputs must be qualified as
3070
* "flat"."
3071
*
3072
* From section 4.3.4 ("Inputs") of the GLSL 4.00 spec:
3073
* "Fragment shader inputs that are signed or unsigned integers, integer
3074
* vectors, or any double-precision floating-point type must be
3075
* qualified with the interpolation qualifier flat."
3076
*
3077
* Note that the GLSL specs are missing the text "or contain"; this is
3078
* presumably an oversight. See Khronos bug #15671.
3079
*
3080
* The 'double' type does not exist in GLSL ES so far.
3081
*/
3082
if (state->has_double()
3083
&& var_type->contains_double()) {
3084
_mesa_glsl_error(loc, state, "if a fragment input is (or contains) "
3085
"a double, then it must be qualified with 'flat'");
3086
}
3087
3088
/* Bindless sampler/image fragment inputs must be qualified with 'flat'.
3089
*
3090
* From section 4.3.4 of the ARB_bindless_texture spec:
3091
*
3092
* "(modify last paragraph, p. 35, allowing samplers and images as
3093
* fragment shader inputs) ... Fragment inputs can only be signed and
3094
* unsigned integers and integer vectors, floating point scalars,
3095
* floating-point vectors, matrices, sampler and image types, or arrays
3096
* or structures of these. Fragment shader inputs that are signed or
3097
* unsigned integers, integer vectors, or any double-precision floating-
3098
* point type, or any sampler or image type must be qualified with the
3099
* interpolation qualifier "flat"."
3100
*/
3101
if (state->has_bindless()
3102
&& (var_type->contains_sampler() || var_type->contains_image())) {
3103
_mesa_glsl_error(loc, state, "if a fragment input is (or contains) "
3104
"a bindless sampler (or image), then it must be "
3105
"qualified with 'flat'");
3106
}
3107
}
3108
3109
static void
3110
validate_interpolation_qualifier(struct _mesa_glsl_parse_state *state,
3111
YYLTYPE *loc,
3112
const glsl_interp_mode interpolation,
3113
const struct ast_type_qualifier *qual,
3114
const struct glsl_type *var_type,
3115
ir_variable_mode mode)
3116
{
3117
/* Interpolation qualifiers can only apply to shader inputs or outputs, but
3118
* not to vertex shader inputs nor fragment shader outputs.
3119
*
3120
* From section 4.3 ("Storage Qualifiers") of the GLSL 1.30 spec:
3121
* "Outputs from a vertex shader (out) and inputs to a fragment
3122
* shader (in) can be further qualified with one or more of these
3123
* interpolation qualifiers"
3124
* ...
3125
* "These interpolation qualifiers may only precede the qualifiers in,
3126
* centroid in, out, or centroid out in a declaration. They do not apply
3127
* to the deprecated storage qualifiers varying or centroid
3128
* varying. They also do not apply to inputs into a vertex shader or
3129
* outputs from a fragment shader."
3130
*
3131
* From section 4.3 ("Storage Qualifiers") of the GLSL ES 3.00 spec:
3132
* "Outputs from a shader (out) and inputs to a shader (in) can be
3133
* further qualified with one of these interpolation qualifiers."
3134
* ...
3135
* "These interpolation qualifiers may only precede the qualifiers
3136
* in, centroid in, out, or centroid out in a declaration. They do
3137
* not apply to inputs into a vertex shader or outputs from a
3138
* fragment shader."
3139
*/
3140
if ((state->is_version(130, 300) || state->EXT_gpu_shader4_enable)
3141
&& interpolation != INTERP_MODE_NONE) {
3142
const char *i = interpolation_string(interpolation);
3143
if (mode != ir_var_shader_in && mode != ir_var_shader_out)
3144
_mesa_glsl_error(loc, state,
3145
"interpolation qualifier `%s' can only be applied to "
3146
"shader inputs or outputs.", i);
3147
3148
switch (state->stage) {
3149
case MESA_SHADER_VERTEX:
3150
if (mode == ir_var_shader_in) {
3151
_mesa_glsl_error(loc, state,
3152
"interpolation qualifier '%s' cannot be applied to "
3153
"vertex shader inputs", i);
3154
}
3155
break;
3156
case MESA_SHADER_FRAGMENT:
3157
if (mode == ir_var_shader_out) {
3158
_mesa_glsl_error(loc, state,
3159
"interpolation qualifier '%s' cannot be applied to "
3160
"fragment shader outputs", i);
3161
}
3162
break;
3163
default:
3164
break;
3165
}
3166
}
3167
3168
/* Interpolation qualifiers cannot be applied to 'centroid' and
3169
* 'centroid varying'.
3170
*
3171
* From section 4.3 ("Storage Qualifiers") of the GLSL 1.30 spec:
3172
* "interpolation qualifiers may only precede the qualifiers in,
3173
* centroid in, out, or centroid out in a declaration. They do not apply
3174
* to the deprecated storage qualifiers varying or centroid varying."
3175
*
3176
* These deprecated storage qualifiers do not exist in GLSL ES 3.00.
3177
*
3178
* GL_EXT_gpu_shader4 allows this.
3179
*/
3180
if (state->is_version(130, 0) && !state->EXT_gpu_shader4_enable
3181
&& interpolation != INTERP_MODE_NONE
3182
&& qual->flags.q.varying) {
3183
3184
const char *i = interpolation_string(interpolation);
3185
const char *s;
3186
if (qual->flags.q.centroid)
3187
s = "centroid varying";
3188
else
3189
s = "varying";
3190
3191
_mesa_glsl_error(loc, state,
3192
"qualifier '%s' cannot be applied to the "
3193
"deprecated storage qualifier '%s'", i, s);
3194
}
3195
3196
validate_fragment_flat_interpolation_input(state, loc, interpolation,
3197
var_type, mode);
3198
}
3199
3200
static glsl_interp_mode
3201
interpret_interpolation_qualifier(const struct ast_type_qualifier *qual,
3202
const struct glsl_type *var_type,
3203
ir_variable_mode mode,
3204
struct _mesa_glsl_parse_state *state,
3205
YYLTYPE *loc)
3206
{
3207
glsl_interp_mode interpolation;
3208
if (qual->flags.q.flat)
3209
interpolation = INTERP_MODE_FLAT;
3210
else if (qual->flags.q.noperspective)
3211
interpolation = INTERP_MODE_NOPERSPECTIVE;
3212
else if (qual->flags.q.smooth)
3213
interpolation = INTERP_MODE_SMOOTH;
3214
else
3215
interpolation = INTERP_MODE_NONE;
3216
3217
validate_interpolation_qualifier(state, loc,
3218
interpolation,
3219
qual, var_type, mode);
3220
3221
return interpolation;
3222
}
3223
3224
3225
static void
3226
apply_explicit_location(const struct ast_type_qualifier *qual,
3227
ir_variable *var,
3228
struct _mesa_glsl_parse_state *state,
3229
YYLTYPE *loc)
3230
{
3231
bool fail = false;
3232
3233
unsigned qual_location;
3234
if (!process_qualifier_constant(state, loc, "location", qual->location,
3235
&qual_location)) {
3236
return;
3237
}
3238
3239
/* Checks for GL_ARB_explicit_uniform_location. */
3240
if (qual->flags.q.uniform) {
3241
if (!state->check_explicit_uniform_location_allowed(loc, var))
3242
return;
3243
3244
const struct gl_context *const ctx = state->ctx;
3245
unsigned max_loc = qual_location + var->type->uniform_locations() - 1;
3246
3247
if (max_loc >= ctx->Const.MaxUserAssignableUniformLocations) {
3248
_mesa_glsl_error(loc, state, "location(s) consumed by uniform %s "
3249
">= MAX_UNIFORM_LOCATIONS (%u)", var->name,
3250
ctx->Const.MaxUserAssignableUniformLocations);
3251
return;
3252
}
3253
3254
var->data.explicit_location = true;
3255
var->data.location = qual_location;
3256
return;
3257
}
3258
3259
/* Between GL_ARB_explicit_attrib_location an
3260
* GL_ARB_separate_shader_objects, the inputs and outputs of any shader
3261
* stage can be assigned explicit locations. The checking here associates
3262
* the correct extension with the correct stage's input / output:
3263
*
3264
* input output
3265
* ----- ------
3266
* vertex explicit_loc sso
3267
* tess control sso sso
3268
* tess eval sso sso
3269
* geometry sso sso
3270
* fragment sso explicit_loc
3271
*/
3272
switch (state->stage) {
3273
case MESA_SHADER_VERTEX:
3274
if (var->data.mode == ir_var_shader_in) {
3275
if (!state->check_explicit_attrib_location_allowed(loc, var))
3276
return;
3277
3278
break;
3279
}
3280
3281
if (var->data.mode == ir_var_shader_out) {
3282
if (!state->check_separate_shader_objects_allowed(loc, var))
3283
return;
3284
3285
break;
3286
}
3287
3288
fail = true;
3289
break;
3290
3291
case MESA_SHADER_TESS_CTRL:
3292
case MESA_SHADER_TESS_EVAL:
3293
case MESA_SHADER_GEOMETRY:
3294
if (var->data.mode == ir_var_shader_in || var->data.mode == ir_var_shader_out) {
3295
if (!state->check_separate_shader_objects_allowed(loc, var))
3296
return;
3297
3298
break;
3299
}
3300
3301
fail = true;
3302
break;
3303
3304
case MESA_SHADER_FRAGMENT:
3305
if (var->data.mode == ir_var_shader_in) {
3306
if (!state->check_separate_shader_objects_allowed(loc, var))
3307
return;
3308
3309
break;
3310
}
3311
3312
if (var->data.mode == ir_var_shader_out) {
3313
if (!state->check_explicit_attrib_location_allowed(loc, var))
3314
return;
3315
3316
break;
3317
}
3318
3319
fail = true;
3320
break;
3321
3322
case MESA_SHADER_COMPUTE:
3323
_mesa_glsl_error(loc, state,
3324
"compute shader variables cannot be given "
3325
"explicit locations");
3326
return;
3327
default:
3328
fail = true;
3329
break;
3330
};
3331
3332
if (fail) {
3333
_mesa_glsl_error(loc, state,
3334
"%s cannot be given an explicit location in %s shader",
3335
mode_string(var),
3336
_mesa_shader_stage_to_string(state->stage));
3337
} else {
3338
var->data.explicit_location = true;
3339
3340
switch (state->stage) {
3341
case MESA_SHADER_VERTEX:
3342
var->data.location = (var->data.mode == ir_var_shader_in)
3343
? (qual_location + VERT_ATTRIB_GENERIC0)
3344
: (qual_location + VARYING_SLOT_VAR0);
3345
break;
3346
3347
case MESA_SHADER_TESS_CTRL:
3348
case MESA_SHADER_TESS_EVAL:
3349
case MESA_SHADER_GEOMETRY:
3350
if (var->data.patch)
3351
var->data.location = qual_location + VARYING_SLOT_PATCH0;
3352
else
3353
var->data.location = qual_location + VARYING_SLOT_VAR0;
3354
break;
3355
3356
case MESA_SHADER_FRAGMENT:
3357
var->data.location = (var->data.mode == ir_var_shader_out)
3358
? (qual_location + FRAG_RESULT_DATA0)
3359
: (qual_location + VARYING_SLOT_VAR0);
3360
break;
3361
default:
3362
assert(!"Unexpected shader type");
3363
break;
3364
}
3365
3366
/* Check if index was set for the uniform instead of the function */
3367
if (qual->flags.q.explicit_index && qual->is_subroutine_decl()) {
3368
_mesa_glsl_error(loc, state, "an index qualifier can only be "
3369
"used with subroutine functions");
3370
return;
3371
}
3372
3373
unsigned qual_index;
3374
if (qual->flags.q.explicit_index &&
3375
process_qualifier_constant(state, loc, "index", qual->index,
3376
&qual_index)) {
3377
/* From the GLSL 4.30 specification, section 4.4.2 (Output
3378
* Layout Qualifiers):
3379
*
3380
* "It is also a compile-time error if a fragment shader
3381
* sets a layout index to less than 0 or greater than 1."
3382
*
3383
* Older specifications don't mandate a behavior; we take
3384
* this as a clarification and always generate the error.
3385
*/
3386
if (qual_index > 1) {
3387
_mesa_glsl_error(loc, state,
3388
"explicit index may only be 0 or 1");
3389
} else {
3390
var->data.explicit_index = true;
3391
var->data.index = qual_index;
3392
}
3393
}
3394
}
3395
}
3396
3397
static bool
3398
validate_storage_for_sampler_image_types(ir_variable *var,
3399
struct _mesa_glsl_parse_state *state,
3400
YYLTYPE *loc)
3401
{
3402
/* From section 4.1.7 of the GLSL 4.40 spec:
3403
*
3404
* "[Opaque types] can only be declared as function
3405
* parameters or uniform-qualified variables."
3406
*
3407
* From section 4.1.7 of the ARB_bindless_texture spec:
3408
*
3409
* "Samplers may be declared as shader inputs and outputs, as uniform
3410
* variables, as temporary variables, and as function parameters."
3411
*
3412
* From section 4.1.X of the ARB_bindless_texture spec:
3413
*
3414
* "Images may be declared as shader inputs and outputs, as uniform
3415
* variables, as temporary variables, and as function parameters."
3416
*/
3417
if (state->has_bindless()) {
3418
if (var->data.mode != ir_var_auto &&
3419
var->data.mode != ir_var_uniform &&
3420
var->data.mode != ir_var_shader_in &&
3421
var->data.mode != ir_var_shader_out &&
3422
var->data.mode != ir_var_function_in &&
3423
var->data.mode != ir_var_function_out &&
3424
var->data.mode != ir_var_function_inout) {
3425
_mesa_glsl_error(loc, state, "bindless image/sampler variables may "
3426
"only be declared as shader inputs and outputs, as "
3427
"uniform variables, as temporary variables and as "
3428
"function parameters");
3429
return false;
3430
}
3431
} else {
3432
if (var->data.mode != ir_var_uniform &&
3433
var->data.mode != ir_var_function_in) {
3434
_mesa_glsl_error(loc, state, "image/sampler variables may only be "
3435
"declared as function parameters or "
3436
"uniform-qualified global variables");
3437
return false;
3438
}
3439
}
3440
return true;
3441
}
3442
3443
static bool
3444
validate_memory_qualifier_for_type(struct _mesa_glsl_parse_state *state,
3445
YYLTYPE *loc,
3446
const struct ast_type_qualifier *qual,
3447
const glsl_type *type)
3448
{
3449
/* From Section 4.10 (Memory Qualifiers) of the GLSL 4.50 spec:
3450
*
3451
* "Memory qualifiers are only supported in the declarations of image
3452
* variables, buffer variables, and shader storage blocks; it is an error
3453
* to use such qualifiers in any other declarations.
3454
*/
3455
if (!type->is_image() && !qual->flags.q.buffer) {
3456
if (qual->flags.q.read_only ||
3457
qual->flags.q.write_only ||
3458
qual->flags.q.coherent ||
3459
qual->flags.q._volatile ||
3460
qual->flags.q.restrict_flag) {
3461
_mesa_glsl_error(loc, state, "memory qualifiers may only be applied "
3462
"in the declarations of image variables, buffer "
3463
"variables, and shader storage blocks");
3464
return false;
3465
}
3466
}
3467
return true;
3468
}
3469
3470
static bool
3471
validate_image_format_qualifier_for_type(struct _mesa_glsl_parse_state *state,
3472
YYLTYPE *loc,
3473
const struct ast_type_qualifier *qual,
3474
const glsl_type *type)
3475
{
3476
/* From section 4.4.6.2 (Format Layout Qualifiers) of the GLSL 4.50 spec:
3477
*
3478
* "Format layout qualifiers can be used on image variable declarations
3479
* (those declared with a basic type having “image ” in its keyword)."
3480
*/
3481
if (!type->is_image() && qual->flags.q.explicit_image_format) {
3482
_mesa_glsl_error(loc, state, "format layout qualifiers may only be "
3483
"applied to images");
3484
return false;
3485
}
3486
return true;
3487
}
3488
3489
static void
3490
apply_image_qualifier_to_variable(const struct ast_type_qualifier *qual,
3491
ir_variable *var,
3492
struct _mesa_glsl_parse_state *state,
3493
YYLTYPE *loc)
3494
{
3495
const glsl_type *base_type = var->type->without_array();
3496
3497
if (!validate_image_format_qualifier_for_type(state, loc, qual, base_type) ||
3498
!validate_memory_qualifier_for_type(state, loc, qual, base_type))
3499
return;
3500
3501
if (!base_type->is_image())
3502
return;
3503
3504
if (!validate_storage_for_sampler_image_types(var, state, loc))
3505
return;
3506
3507
var->data.memory_read_only |= qual->flags.q.read_only;
3508
var->data.memory_write_only |= qual->flags.q.write_only;
3509
var->data.memory_coherent |= qual->flags.q.coherent;
3510
var->data.memory_volatile |= qual->flags.q._volatile;
3511
var->data.memory_restrict |= qual->flags.q.restrict_flag;
3512
3513
if (qual->flags.q.explicit_image_format) {
3514
if (var->data.mode == ir_var_function_in) {
3515
_mesa_glsl_error(loc, state, "format qualifiers cannot be used on "
3516
"image function parameters");
3517
}
3518
3519
if (qual->image_base_type != base_type->sampled_type) {
3520
_mesa_glsl_error(loc, state, "format qualifier doesn't match the base "
3521
"data type of the image");
3522
}
3523
3524
var->data.image_format = qual->image_format;
3525
} else if (state->has_image_load_formatted()) {
3526
if (var->data.mode == ir_var_uniform &&
3527
state->EXT_shader_image_load_formatted_warn) {
3528
_mesa_glsl_warning(loc, state, "GL_EXT_image_load_formatted used");
3529
}
3530
} else {
3531
if (var->data.mode == ir_var_uniform) {
3532
if (state->es_shader ||
3533
!(state->is_version(420, 310) || state->ARB_shader_image_load_store_enable)) {
3534
_mesa_glsl_error(loc, state, "all image uniforms must have a "
3535
"format layout qualifier");
3536
} else if (!qual->flags.q.write_only) {
3537
_mesa_glsl_error(loc, state, "image uniforms not qualified with "
3538
"`writeonly' must have a format layout qualifier");
3539
}
3540
}
3541
var->data.image_format = PIPE_FORMAT_NONE;
3542
}
3543
3544
/* From page 70 of the GLSL ES 3.1 specification:
3545
*
3546
* "Except for image variables qualified with the format qualifiers r32f,
3547
* r32i, and r32ui, image variables must specify either memory qualifier
3548
* readonly or the memory qualifier writeonly."
3549
*/
3550
if (state->es_shader &&
3551
var->data.image_format != PIPE_FORMAT_R32_FLOAT &&
3552
var->data.image_format != PIPE_FORMAT_R32_SINT &&
3553
var->data.image_format != PIPE_FORMAT_R32_UINT &&
3554
!var->data.memory_read_only &&
3555
!var->data.memory_write_only) {
3556
_mesa_glsl_error(loc, state, "image variables of format other than r32f, "
3557
"r32i or r32ui must be qualified `readonly' or "
3558
"`writeonly'");
3559
}
3560
}
3561
3562
static inline const char*
3563
get_layout_qualifier_string(bool origin_upper_left, bool pixel_center_integer)
3564
{
3565
if (origin_upper_left && pixel_center_integer)
3566
return "origin_upper_left, pixel_center_integer";
3567
else if (origin_upper_left)
3568
return "origin_upper_left";
3569
else if (pixel_center_integer)
3570
return "pixel_center_integer";
3571
else
3572
return " ";
3573
}
3574
3575
static inline bool
3576
is_conflicting_fragcoord_redeclaration(struct _mesa_glsl_parse_state *state,
3577
const struct ast_type_qualifier *qual)
3578
{
3579
/* If gl_FragCoord was previously declared, and the qualifiers were
3580
* different in any way, return true.
3581
*/
3582
if (state->fs_redeclares_gl_fragcoord) {
3583
return (state->fs_pixel_center_integer != qual->flags.q.pixel_center_integer
3584
|| state->fs_origin_upper_left != qual->flags.q.origin_upper_left);
3585
}
3586
3587
return false;
3588
}
3589
3590
static inline bool
3591
is_conflicting_layer_redeclaration(struct _mesa_glsl_parse_state *state,
3592
const struct ast_type_qualifier *qual)
3593
{
3594
if (state->redeclares_gl_layer) {
3595
return state->layer_viewport_relative != qual->flags.q.viewport_relative;
3596
}
3597
return false;
3598
}
3599
3600
static inline void
3601
validate_array_dimensions(const glsl_type *t,
3602
struct _mesa_glsl_parse_state *state,
3603
YYLTYPE *loc) {
3604
if (t->is_array()) {
3605
t = t->fields.array;
3606
while (t->is_array()) {
3607
if (t->is_unsized_array()) {
3608
_mesa_glsl_error(loc, state,
3609
"only the outermost array dimension can "
3610
"be unsized",
3611
t->name);
3612
break;
3613
}
3614
t = t->fields.array;
3615
}
3616
}
3617
}
3618
3619
static void
3620
apply_bindless_qualifier_to_variable(const struct ast_type_qualifier *qual,
3621
ir_variable *var,
3622
struct _mesa_glsl_parse_state *state,
3623
YYLTYPE *loc)
3624
{
3625
bool has_local_qualifiers = qual->flags.q.bindless_sampler ||
3626
qual->flags.q.bindless_image ||
3627
qual->flags.q.bound_sampler ||
3628
qual->flags.q.bound_image;
3629
3630
/* The ARB_bindless_texture spec says:
3631
*
3632
* "Modify Section 4.4.6 Opaque-Uniform Layout Qualifiers of the GLSL 4.30
3633
* spec"
3634
*
3635
* "If these layout qualifiers are applied to other types of default block
3636
* uniforms, or variables with non-uniform storage, a compile-time error
3637
* will be generated."
3638
*/
3639
if (has_local_qualifiers && !qual->flags.q.uniform) {
3640
_mesa_glsl_error(loc, state, "ARB_bindless_texture layout qualifiers "
3641
"can only be applied to default block uniforms or "
3642
"variables with uniform storage");
3643
return;
3644
}
3645
3646
/* The ARB_bindless_texture spec doesn't state anything in this situation,
3647
* but it makes sense to only allow bindless_sampler/bound_sampler for
3648
* sampler types, and respectively bindless_image/bound_image for image
3649
* types.
3650
*/
3651
if ((qual->flags.q.bindless_sampler || qual->flags.q.bound_sampler) &&
3652
!var->type->contains_sampler()) {
3653
_mesa_glsl_error(loc, state, "bindless_sampler or bound_sampler can only "
3654
"be applied to sampler types");
3655
return;
3656
}
3657
3658
if ((qual->flags.q.bindless_image || qual->flags.q.bound_image) &&
3659
!var->type->contains_image()) {
3660
_mesa_glsl_error(loc, state, "bindless_image or bound_image can only be "
3661
"applied to image types");
3662
return;
3663
}
3664
3665
/* The bindless_sampler/bindless_image (and respectively
3666
* bound_sampler/bound_image) layout qualifiers can be set at global and at
3667
* local scope.
3668
*/
3669
if (var->type->contains_sampler() || var->type->contains_image()) {
3670
var->data.bindless = qual->flags.q.bindless_sampler ||
3671
qual->flags.q.bindless_image ||
3672
state->bindless_sampler_specified ||
3673
state->bindless_image_specified;
3674
3675
var->data.bound = qual->flags.q.bound_sampler ||
3676
qual->flags.q.bound_image ||
3677
state->bound_sampler_specified ||
3678
state->bound_image_specified;
3679
}
3680
}
3681
3682
static void
3683
apply_layout_qualifier_to_variable(const struct ast_type_qualifier *qual,
3684
ir_variable *var,
3685
struct _mesa_glsl_parse_state *state,
3686
YYLTYPE *loc)
3687
{
3688
if (var->name != NULL && strcmp(var->name, "gl_FragCoord") == 0) {
3689
3690
/* Section 4.3.8.1, page 39 of GLSL 1.50 spec says:
3691
*
3692
* "Within any shader, the first redeclarations of gl_FragCoord
3693
* must appear before any use of gl_FragCoord."
3694
*
3695
* Generate a compiler error if above condition is not met by the
3696
* fragment shader.
3697
*/
3698
ir_variable *earlier = state->symbols->get_variable("gl_FragCoord");
3699
if (earlier != NULL &&
3700
earlier->data.used &&
3701
!state->fs_redeclares_gl_fragcoord) {
3702
_mesa_glsl_error(loc, state,
3703
"gl_FragCoord used before its first redeclaration "
3704
"in fragment shader");
3705
}
3706
3707
/* Make sure all gl_FragCoord redeclarations specify the same layout
3708
* qualifiers.
3709
*/
3710
if (is_conflicting_fragcoord_redeclaration(state, qual)) {
3711
const char *const qual_string =
3712
get_layout_qualifier_string(qual->flags.q.origin_upper_left,
3713
qual->flags.q.pixel_center_integer);
3714
3715
const char *const state_string =
3716
get_layout_qualifier_string(state->fs_origin_upper_left,
3717
state->fs_pixel_center_integer);
3718
3719
_mesa_glsl_error(loc, state,
3720
"gl_FragCoord redeclared with different layout "
3721
"qualifiers (%s) and (%s) ",
3722
state_string,
3723
qual_string);
3724
}
3725
state->fs_origin_upper_left = qual->flags.q.origin_upper_left;
3726
state->fs_pixel_center_integer = qual->flags.q.pixel_center_integer;
3727
state->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers =
3728
!qual->flags.q.origin_upper_left && !qual->flags.q.pixel_center_integer;
3729
state->fs_redeclares_gl_fragcoord =
3730
state->fs_origin_upper_left ||
3731
state->fs_pixel_center_integer ||
3732
state->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers;
3733
}
3734
3735
if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer)
3736
&& (strcmp(var->name, "gl_FragCoord") != 0)) {
3737
const char *const qual_string = (qual->flags.q.origin_upper_left)
3738
? "origin_upper_left" : "pixel_center_integer";
3739
3740
_mesa_glsl_error(loc, state,
3741
"layout qualifier `%s' can only be applied to "
3742
"fragment shader input `gl_FragCoord'",
3743
qual_string);
3744
}
3745
3746
if (qual->flags.q.explicit_location) {
3747
apply_explicit_location(qual, var, state, loc);
3748
3749
if (qual->flags.q.explicit_component) {
3750
unsigned qual_component;
3751
if (process_qualifier_constant(state, loc, "component",
3752
qual->component, &qual_component)) {
3753
validate_component_layout_for_type(state, loc, var->type,
3754
qual_component);
3755
var->data.explicit_component = true;
3756
var->data.location_frac = qual_component;
3757
}
3758
}
3759
} else if (qual->flags.q.explicit_index) {
3760
if (!qual->subroutine_list)
3761
_mesa_glsl_error(loc, state,
3762
"explicit index requires explicit location");
3763
} else if (qual->flags.q.explicit_component) {
3764
_mesa_glsl_error(loc, state,
3765
"explicit component requires explicit location");
3766
}
3767
3768
if (qual->flags.q.explicit_binding) {
3769
apply_explicit_binding(state, loc, var, var->type, qual);
3770
}
3771
3772
if (state->stage == MESA_SHADER_GEOMETRY &&
3773
qual->flags.q.out && qual->flags.q.stream) {
3774
unsigned qual_stream;
3775
if (process_qualifier_constant(state, loc, "stream", qual->stream,
3776
&qual_stream) &&
3777
validate_stream_qualifier(loc, state, qual_stream)) {
3778
var->data.stream = qual_stream;
3779
}
3780
}
3781
3782
if (qual->flags.q.out && qual->flags.q.xfb_buffer) {
3783
unsigned qual_xfb_buffer;
3784
if (process_qualifier_constant(state, loc, "xfb_buffer",
3785
qual->xfb_buffer, &qual_xfb_buffer) &&
3786
validate_xfb_buffer_qualifier(loc, state, qual_xfb_buffer)) {
3787
var->data.xfb_buffer = qual_xfb_buffer;
3788
if (qual->flags.q.explicit_xfb_buffer)
3789
var->data.explicit_xfb_buffer = true;
3790
}
3791
}
3792
3793
if (qual->flags.q.explicit_xfb_offset) {
3794
unsigned qual_xfb_offset;
3795
unsigned component_size = var->type->contains_double() ? 8 : 4;
3796
3797
if (process_qualifier_constant(state, loc, "xfb_offset",
3798
qual->offset, &qual_xfb_offset) &&
3799
validate_xfb_offset_qualifier(loc, state, (int) qual_xfb_offset,
3800
var->type, component_size)) {
3801
var->data.offset = qual_xfb_offset;
3802
var->data.explicit_xfb_offset = true;
3803
}
3804
}
3805
3806
if (qual->flags.q.explicit_xfb_stride) {
3807
unsigned qual_xfb_stride;
3808
if (process_qualifier_constant(state, loc, "xfb_stride",
3809
qual->xfb_stride, &qual_xfb_stride)) {
3810
var->data.xfb_stride = qual_xfb_stride;
3811
var->data.explicit_xfb_stride = true;
3812
}
3813
}
3814
3815
if (var->type->contains_atomic()) {
3816
if (var->data.mode == ir_var_uniform) {
3817
if (var->data.explicit_binding) {
3818
unsigned *offset =
3819
&state->atomic_counter_offsets[var->data.binding];
3820
3821
if (*offset % ATOMIC_COUNTER_SIZE)
3822
_mesa_glsl_error(loc, state,
3823
"misaligned atomic counter offset");
3824
3825
var->data.offset = *offset;
3826
*offset += var->type->atomic_size();
3827
3828
} else {
3829
_mesa_glsl_error(loc, state,
3830
"atomic counters require explicit binding point");
3831
}
3832
} else if (var->data.mode != ir_var_function_in) {
3833
_mesa_glsl_error(loc, state, "atomic counters may only be declared as "
3834
"function parameters or uniform-qualified "
3835
"global variables");
3836
}
3837
}
3838
3839
if (var->type->contains_sampler() &&
3840
!validate_storage_for_sampler_image_types(var, state, loc))
3841
return;
3842
3843
/* Is the 'layout' keyword used with parameters that allow relaxed checking.
3844
* Many implementations of GL_ARB_fragment_coord_conventions_enable and some
3845
* implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
3846
* allowed the layout qualifier to be used with 'varying' and 'attribute'.
3847
* These extensions and all following extensions that add the 'layout'
3848
* keyword have been modified to require the use of 'in' or 'out'.
3849
*
3850
* The following extension do not allow the deprecated keywords:
3851
*
3852
* GL_AMD_conservative_depth
3853
* GL_ARB_conservative_depth
3854
* GL_ARB_gpu_shader5
3855
* GL_ARB_separate_shader_objects
3856
* GL_ARB_tessellation_shader
3857
* GL_ARB_transform_feedback3
3858
* GL_ARB_uniform_buffer_object
3859
*
3860
* It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
3861
* allow layout with the deprecated keywords.
3862
*/
3863
const bool relaxed_layout_qualifier_checking =
3864
state->ARB_fragment_coord_conventions_enable;
3865
3866
const bool uses_deprecated_qualifier = qual->flags.q.attribute
3867
|| qual->flags.q.varying;
3868
if (qual->has_layout() && uses_deprecated_qualifier) {
3869
if (relaxed_layout_qualifier_checking) {
3870
_mesa_glsl_warning(loc, state,
3871
"`layout' qualifier may not be used with "
3872
"`attribute' or `varying'");
3873
} else {
3874
_mesa_glsl_error(loc, state,
3875
"`layout' qualifier may not be used with "
3876
"`attribute' or `varying'");
3877
}
3878
}
3879
3880
/* Layout qualifiers for gl_FragDepth, which are enabled by extension
3881
* AMD_conservative_depth.
3882
*/
3883
if (qual->flags.q.depth_type
3884
&& !state->is_version(420, 0)
3885
&& !state->AMD_conservative_depth_enable
3886
&& !state->ARB_conservative_depth_enable) {
3887
_mesa_glsl_error(loc, state,
3888
"extension GL_AMD_conservative_depth or "
3889
"GL_ARB_conservative_depth must be enabled "
3890
"to use depth layout qualifiers");
3891
} else if (qual->flags.q.depth_type
3892
&& strcmp(var->name, "gl_FragDepth") != 0) {
3893
_mesa_glsl_error(loc, state,
3894
"depth layout qualifiers can be applied only to "
3895
"gl_FragDepth");
3896
}
3897
3898
switch (qual->depth_type) {
3899
case ast_depth_any:
3900
var->data.depth_layout = ir_depth_layout_any;
3901
break;
3902
case ast_depth_greater:
3903
var->data.depth_layout = ir_depth_layout_greater;
3904
break;
3905
case ast_depth_less:
3906
var->data.depth_layout = ir_depth_layout_less;
3907
break;
3908
case ast_depth_unchanged:
3909
var->data.depth_layout = ir_depth_layout_unchanged;
3910
break;
3911
default:
3912
var->data.depth_layout = ir_depth_layout_none;
3913
break;
3914
}
3915
3916
if (qual->flags.q.std140 ||
3917
qual->flags.q.std430 ||
3918
qual->flags.q.packed ||
3919
qual->flags.q.shared) {
3920
_mesa_glsl_error(loc, state,
3921
"uniform and shader storage block layout qualifiers "
3922
"std140, std430, packed, and shared can only be "
3923
"applied to uniform or shader storage blocks, not "
3924
"members");
3925
}
3926
3927
if (qual->flags.q.row_major || qual->flags.q.column_major) {
3928
validate_matrix_layout_for_type(state, loc, var->type, var);
3929
}
3930
3931
/* From section 4.4.1.3 of the GLSL 4.50 specification (Fragment Shader
3932
* Inputs):
3933
*
3934
* "Fragment shaders also allow the following layout qualifier on in only
3935
* (not with variable declarations)
3936
* layout-qualifier-id
3937
* early_fragment_tests
3938
* [...]"
3939
*/
3940
if (qual->flags.q.early_fragment_tests) {
3941
_mesa_glsl_error(loc, state, "early_fragment_tests layout qualifier only "
3942
"valid in fragment shader input layout declaration.");
3943
}
3944
3945
if (qual->flags.q.inner_coverage) {
3946
_mesa_glsl_error(loc, state, "inner_coverage layout qualifier only "
3947
"valid in fragment shader input layout declaration.");
3948
}
3949
3950
if (qual->flags.q.post_depth_coverage) {
3951
_mesa_glsl_error(loc, state, "post_depth_coverage layout qualifier only "
3952
"valid in fragment shader input layout declaration.");
3953
}
3954
3955
if (state->has_bindless())
3956
apply_bindless_qualifier_to_variable(qual, var, state, loc);
3957
3958
if (qual->flags.q.pixel_interlock_ordered ||
3959
qual->flags.q.pixel_interlock_unordered ||
3960
qual->flags.q.sample_interlock_ordered ||
3961
qual->flags.q.sample_interlock_unordered) {
3962
_mesa_glsl_error(loc, state, "interlock layout qualifiers: "
3963
"pixel_interlock_ordered, pixel_interlock_unordered, "
3964
"sample_interlock_ordered and sample_interlock_unordered, "
3965
"only valid in fragment shader input layout declaration.");
3966
}
3967
3968
if (var->name != NULL && strcmp(var->name, "gl_Layer") == 0) {
3969
if (is_conflicting_layer_redeclaration(state, qual)) {
3970
_mesa_glsl_error(loc, state, "gl_Layer redeclaration with "
3971
"different viewport_relative setting than earlier");
3972
}
3973
state->redeclares_gl_layer = 1;
3974
if (qual->flags.q.viewport_relative) {
3975
state->layer_viewport_relative = 1;
3976
}
3977
} else if (qual->flags.q.viewport_relative) {
3978
_mesa_glsl_error(loc, state,
3979
"viewport_relative qualifier "
3980
"can only be applied to gl_Layer.");
3981
}
3982
}
3983
3984
static void
3985
apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
3986
ir_variable *var,
3987
struct _mesa_glsl_parse_state *state,
3988
YYLTYPE *loc,
3989
bool is_parameter)
3990
{
3991
STATIC_ASSERT(sizeof(qual->flags.q) <= sizeof(qual->flags.i));
3992
3993
if (qual->flags.q.invariant) {
3994
if (var->data.used) {
3995
_mesa_glsl_error(loc, state,
3996
"variable `%s' may not be redeclared "
3997
"`invariant' after being used",
3998
var->name);
3999
} else {
4000
var->data.explicit_invariant = true;
4001
var->data.invariant = true;
4002
}
4003
}
4004
4005
if (qual->flags.q.precise) {
4006
if (var->data.used) {
4007
_mesa_glsl_error(loc, state,
4008
"variable `%s' may not be redeclared "
4009
"`precise' after being used",
4010
var->name);
4011
} else {
4012
var->data.precise = 1;
4013
}
4014
}
4015
4016
if (qual->is_subroutine_decl() && !qual->flags.q.uniform) {
4017
_mesa_glsl_error(loc, state,
4018
"`subroutine' may only be applied to uniforms, "
4019
"subroutine type declarations, or function definitions");
4020
}
4021
4022
if (qual->flags.q.constant || qual->flags.q.attribute
4023
|| qual->flags.q.uniform
4024
|| (qual->flags.q.varying && (state->stage == MESA_SHADER_FRAGMENT)))
4025
var->data.read_only = 1;
4026
4027
if (qual->flags.q.centroid)
4028
var->data.centroid = 1;
4029
4030
if (qual->flags.q.sample)
4031
var->data.sample = 1;
4032
4033
/* Precision qualifiers do not hold any meaning in Desktop GLSL */
4034
if (state->es_shader) {
4035
var->data.precision =
4036
select_gles_precision(qual->precision, var->type, state, loc);
4037
}
4038
4039
if (qual->flags.q.patch)
4040
var->data.patch = 1;
4041
4042
if (qual->flags.q.attribute && state->stage != MESA_SHADER_VERTEX) {
4043
var->type = glsl_type::error_type;
4044
_mesa_glsl_error(loc, state,
4045
"`attribute' variables may not be declared in the "
4046
"%s shader",
4047
_mesa_shader_stage_to_string(state->stage));
4048
}
4049
4050
/* Disallow layout qualifiers which may only appear on layout declarations. */
4051
if (qual->flags.q.prim_type) {
4052
_mesa_glsl_error(loc, state,
4053
"Primitive type may only be specified on GS input or output "
4054
"layout declaration, not on variables.");
4055
}
4056
4057
/* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
4058
*
4059
* "However, the const qualifier cannot be used with out or inout."
4060
*
4061
* The same section of the GLSL 4.40 spec further clarifies this saying:
4062
*
4063
* "The const qualifier cannot be used with out or inout, or a
4064
* compile-time error results."
4065
*/
4066
if (is_parameter && qual->flags.q.constant && qual->flags.q.out) {
4067
_mesa_glsl_error(loc, state,
4068
"`const' may not be applied to `out' or `inout' "
4069
"function parameters");
4070
}
4071
4072
/* If there is no qualifier that changes the mode of the variable, leave
4073
* the setting alone.
4074
*/
4075
assert(var->data.mode != ir_var_temporary);
4076
if (qual->flags.q.in && qual->flags.q.out)
4077
var->data.mode = is_parameter ? ir_var_function_inout : ir_var_shader_out;
4078
else if (qual->flags.q.in)
4079
var->data.mode = is_parameter ? ir_var_function_in : ir_var_shader_in;
4080
else if (qual->flags.q.attribute
4081
|| (qual->flags.q.varying && (state->stage == MESA_SHADER_FRAGMENT)))
4082
var->data.mode = ir_var_shader_in;
4083
else if (qual->flags.q.out)
4084
var->data.mode = is_parameter ? ir_var_function_out : ir_var_shader_out;
4085
else if (qual->flags.q.varying && (state->stage == MESA_SHADER_VERTEX))
4086
var->data.mode = ir_var_shader_out;
4087
else if (qual->flags.q.uniform)
4088
var->data.mode = ir_var_uniform;
4089
else if (qual->flags.q.buffer)
4090
var->data.mode = ir_var_shader_storage;
4091
else if (qual->flags.q.shared_storage)
4092
var->data.mode = ir_var_shader_shared;
4093
4094
if (!is_parameter && state->has_framebuffer_fetch() &&
4095
state->stage == MESA_SHADER_FRAGMENT) {
4096
if (state->is_version(130, 300))
4097
var->data.fb_fetch_output = qual->flags.q.in && qual->flags.q.out;
4098
else
4099
var->data.fb_fetch_output = (strcmp(var->name, "gl_LastFragData") == 0);
4100
}
4101
4102
if (var->data.fb_fetch_output) {
4103
var->data.assigned = true;
4104
var->data.memory_coherent = !qual->flags.q.non_coherent;
4105
4106
/* From the EXT_shader_framebuffer_fetch spec:
4107
*
4108
* "It is an error to declare an inout fragment output not qualified
4109
* with layout(noncoherent) if the GL_EXT_shader_framebuffer_fetch
4110
* extension hasn't been enabled."
4111
*/
4112
if (var->data.memory_coherent &&
4113
!state->EXT_shader_framebuffer_fetch_enable)
4114
_mesa_glsl_error(loc, state,
4115
"invalid declaration of framebuffer fetch output not "
4116
"qualified with layout(noncoherent)");
4117
4118
} else {
4119
/* From the EXT_shader_framebuffer_fetch spec:
4120
*
4121
* "Fragment outputs declared inout may specify the following layout
4122
* qualifier: [...] noncoherent"
4123
*/
4124
if (qual->flags.q.non_coherent)
4125
_mesa_glsl_error(loc, state,
4126
"invalid layout(noncoherent) qualifier not part of "
4127
"framebuffer fetch output declaration");
4128
}
4129
4130
if (!is_parameter && is_varying_var(var, state->stage)) {
4131
/* User-defined ins/outs are not permitted in compute shaders. */
4132
if (state->stage == MESA_SHADER_COMPUTE) {
4133
_mesa_glsl_error(loc, state,
4134
"user-defined input and output variables are not "
4135
"permitted in compute shaders");
4136
}
4137
4138
/* This variable is being used to link data between shader stages (in
4139
* pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
4140
* that is allowed for such purposes.
4141
*
4142
* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
4143
*
4144
* "The varying qualifier can be used only with the data types
4145
* float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
4146
* these."
4147
*
4148
* This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
4149
* page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
4150
*
4151
* "Fragment inputs can only be signed and unsigned integers and
4152
* integer vectors, float, floating-point vectors, matrices, or
4153
* arrays of these. Structures cannot be input.
4154
*
4155
* Similar text exists in the section on vertex shader outputs.
4156
*
4157
* Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
4158
* 3.00 spec allows structs as well. Varying structs are also allowed
4159
* in GLSL 1.50.
4160
*
4161
* From section 4.3.4 of the ARB_bindless_texture spec:
4162
*
4163
* "(modify third paragraph of the section to allow sampler and image
4164
* types) ... Vertex shader inputs can only be float,
4165
* single-precision floating-point scalars, single-precision
4166
* floating-point vectors, matrices, signed and unsigned integers
4167
* and integer vectors, sampler and image types."
4168
*
4169
* From section 4.3.6 of the ARB_bindless_texture spec:
4170
*
4171
* "Output variables can only be floating-point scalars,
4172
* floating-point vectors, matrices, signed or unsigned integers or
4173
* integer vectors, sampler or image types, or arrays or structures
4174
* of any these."
4175
*/
4176
switch (var->type->without_array()->base_type) {
4177
case GLSL_TYPE_FLOAT:
4178
/* Ok in all GLSL versions */
4179
break;
4180
case GLSL_TYPE_UINT:
4181
case GLSL_TYPE_INT:
4182
if (state->is_version(130, 300) || state->EXT_gpu_shader4_enable)
4183
break;
4184
_mesa_glsl_error(loc, state,
4185
"varying variables must be of base type float in %s",
4186
state->get_version_string());
4187
break;
4188
case GLSL_TYPE_STRUCT:
4189
if (state->is_version(150, 300))
4190
break;
4191
_mesa_glsl_error(loc, state,
4192
"varying variables may not be of type struct");
4193
break;
4194
case GLSL_TYPE_DOUBLE:
4195
case GLSL_TYPE_UINT64:
4196
case GLSL_TYPE_INT64:
4197
break;
4198
case GLSL_TYPE_SAMPLER:
4199
case GLSL_TYPE_IMAGE:
4200
if (state->has_bindless())
4201
break;
4202
FALLTHROUGH;
4203
default:
4204
_mesa_glsl_error(loc, state, "illegal type for a varying variable");
4205
break;
4206
}
4207
}
4208
4209
if (state->all_invariant && var->data.mode == ir_var_shader_out) {
4210
var->data.explicit_invariant = true;
4211
var->data.invariant = true;
4212
}
4213
4214
var->data.interpolation =
4215
interpret_interpolation_qualifier(qual, var->type,
4216
(ir_variable_mode) var->data.mode,
4217
state, loc);
4218
4219
/* Does the declaration use the deprecated 'attribute' or 'varying'
4220
* keywords?
4221
*/
4222
const bool uses_deprecated_qualifier = qual->flags.q.attribute
4223
|| qual->flags.q.varying;
4224
4225
4226
/* Validate auxiliary storage qualifiers */
4227
4228
/* From section 4.3.4 of the GLSL 1.30 spec:
4229
* "It is an error to use centroid in in a vertex shader."
4230
*
4231
* From section 4.3.4 of the GLSL ES 3.00 spec:
4232
* "It is an error to use centroid in or interpolation qualifiers in
4233
* a vertex shader input."
4234
*/
4235
4236
/* Section 4.3.6 of the GLSL 1.30 specification states:
4237
* "It is an error to use centroid out in a fragment shader."
4238
*
4239
* The GL_ARB_shading_language_420pack extension specification states:
4240
* "It is an error to use auxiliary storage qualifiers or interpolation
4241
* qualifiers on an output in a fragment shader."
4242
*/
4243
if (qual->flags.q.sample && (!is_varying_var(var, state->stage) || uses_deprecated_qualifier)) {
4244
_mesa_glsl_error(loc, state,
4245
"sample qualifier may only be used on `in` or `out` "
4246
"variables between shader stages");
4247
}
4248
if (qual->flags.q.centroid && !is_varying_var(var, state->stage)) {
4249
_mesa_glsl_error(loc, state,
4250
"centroid qualifier may only be used with `in', "
4251
"`out' or `varying' variables between shader stages");
4252
}
4253
4254
if (qual->flags.q.shared_storage && state->stage != MESA_SHADER_COMPUTE) {
4255
_mesa_glsl_error(loc, state,
4256
"the shared storage qualifiers can only be used with "
4257
"compute shaders");
4258
}
4259
4260
apply_image_qualifier_to_variable(qual, var, state, loc);
4261
}
4262
4263
/**
4264
* Get the variable that is being redeclared by this declaration or if it
4265
* does not exist, the current declared variable.
4266
*
4267
* Semantic checks to verify the validity of the redeclaration are also
4268
* performed. If semantic checks fail, compilation error will be emitted via
4269
* \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
4270
*
4271
* \returns
4272
* A pointer to an existing variable in the current scope if the declaration
4273
* is a redeclaration, current variable otherwise. \c is_declared boolean
4274
* will return \c true if the declaration is a redeclaration, \c false
4275
* otherwise.
4276
*/
4277
static ir_variable *
4278
get_variable_being_redeclared(ir_variable **var_ptr, YYLTYPE loc,
4279
struct _mesa_glsl_parse_state *state,
4280
bool allow_all_redeclarations,
4281
bool *is_redeclaration)
4282
{
4283
ir_variable *var = *var_ptr;
4284
4285
/* Check if this declaration is actually a re-declaration, either to
4286
* resize an array or add qualifiers to an existing variable.
4287
*
4288
* This is allowed for variables in the current scope, or when at
4289
* global scope (for built-ins in the implicit outer scope).
4290
*/
4291
ir_variable *earlier = state->symbols->get_variable(var->name);
4292
if (earlier == NULL ||
4293
(state->current_function != NULL &&
4294
!state->symbols->name_declared_this_scope(var->name))) {
4295
*is_redeclaration = false;
4296
return var;
4297
}
4298
4299
*is_redeclaration = true;
4300
4301
if (earlier->data.how_declared == ir_var_declared_implicitly) {
4302
/* Verify that the redeclaration of a built-in does not change the
4303
* storage qualifier. There are a couple special cases.
4304
*
4305
* 1. Some built-in variables that are defined as 'in' in the
4306
* specification are implemented as system values. Allow
4307
* ir_var_system_value -> ir_var_shader_in.
4308
*
4309
* 2. gl_LastFragData is implemented as a ir_var_shader_out, but the
4310
* specification requires that redeclarations omit any qualifier.
4311
* Allow ir_var_shader_out -> ir_var_auto for this one variable.
4312
*/
4313
if (earlier->data.mode != var->data.mode &&
4314
!(earlier->data.mode == ir_var_system_value &&
4315
var->data.mode == ir_var_shader_in) &&
4316
!(strcmp(var->name, "gl_LastFragData") == 0 &&
4317
var->data.mode == ir_var_auto)) {
4318
_mesa_glsl_error(&loc, state,
4319
"redeclaration cannot change qualification of `%s'",
4320
var->name);
4321
}
4322
}
4323
4324
/* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
4325
*
4326
* "It is legal to declare an array without a size and then
4327
* later re-declare the same name as an array of the same
4328
* type and specify a size."
4329
*/
4330
if (earlier->type->is_unsized_array() && var->type->is_array()
4331
&& (var->type->fields.array == earlier->type->fields.array)) {
4332
const int size = var->type->array_size();
4333
check_builtin_array_max_size(var->name, size, loc, state);
4334
if ((size > 0) && (size <= earlier->data.max_array_access)) {
4335
_mesa_glsl_error(& loc, state, "array size must be > %u due to "
4336
"previous access",
4337
earlier->data.max_array_access);
4338
}
4339
4340
earlier->type = var->type;
4341
delete var;
4342
var = NULL;
4343
*var_ptr = NULL;
4344
} else if (earlier->type != var->type) {
4345
_mesa_glsl_error(&loc, state,
4346
"redeclaration of `%s' has incorrect type",
4347
var->name);
4348
} else if ((state->ARB_fragment_coord_conventions_enable ||
4349
state->is_version(150, 0))
4350
&& strcmp(var->name, "gl_FragCoord") == 0) {
4351
/* Allow redeclaration of gl_FragCoord for ARB_fcc layout
4352
* qualifiers.
4353
*
4354
* We don't really need to do anything here, just allow the
4355
* redeclaration. Any error on the gl_FragCoord is handled on the ast
4356
* level at apply_layout_qualifier_to_variable using the
4357
* ast_type_qualifier and _mesa_glsl_parse_state, or later at
4358
* linker.cpp.
4359
*/
4360
/* According to section 4.3.7 of the GLSL 1.30 spec,
4361
* the following built-in varaibles can be redeclared with an
4362
* interpolation qualifier:
4363
* * gl_FrontColor
4364
* * gl_BackColor
4365
* * gl_FrontSecondaryColor
4366
* * gl_BackSecondaryColor
4367
* * gl_Color
4368
* * gl_SecondaryColor
4369
*/
4370
} else if (state->is_version(130, 0)
4371
&& (strcmp(var->name, "gl_FrontColor") == 0
4372
|| strcmp(var->name, "gl_BackColor") == 0
4373
|| strcmp(var->name, "gl_FrontSecondaryColor") == 0
4374
|| strcmp(var->name, "gl_BackSecondaryColor") == 0
4375
|| strcmp(var->name, "gl_Color") == 0
4376
|| strcmp(var->name, "gl_SecondaryColor") == 0)) {
4377
earlier->data.interpolation = var->data.interpolation;
4378
4379
/* Layout qualifiers for gl_FragDepth. */
4380
} else if ((state->is_version(420, 0) ||
4381
state->AMD_conservative_depth_enable ||
4382
state->ARB_conservative_depth_enable)
4383
&& strcmp(var->name, "gl_FragDepth") == 0) {
4384
4385
/** From the AMD_conservative_depth spec:
4386
* Within any shader, the first redeclarations of gl_FragDepth
4387
* must appear before any use of gl_FragDepth.
4388
*/
4389
if (earlier->data.used) {
4390
_mesa_glsl_error(&loc, state,
4391
"the first redeclaration of gl_FragDepth "
4392
"must appear before any use of gl_FragDepth");
4393
}
4394
4395
/* Prevent inconsistent redeclaration of depth layout qualifier. */
4396
if (earlier->data.depth_layout != ir_depth_layout_none
4397
&& earlier->data.depth_layout != var->data.depth_layout) {
4398
_mesa_glsl_error(&loc, state,
4399
"gl_FragDepth: depth layout is declared here "
4400
"as '%s, but it was previously declared as "
4401
"'%s'",
4402
depth_layout_string(var->data.depth_layout),
4403
depth_layout_string(earlier->data.depth_layout));
4404
}
4405
4406
earlier->data.depth_layout = var->data.depth_layout;
4407
4408
} else if (state->has_framebuffer_fetch() &&
4409
strcmp(var->name, "gl_LastFragData") == 0 &&
4410
var->data.mode == ir_var_auto) {
4411
/* According to the EXT_shader_framebuffer_fetch spec:
4412
*
4413
* "By default, gl_LastFragData is declared with the mediump precision
4414
* qualifier. This can be changed by redeclaring the corresponding
4415
* variables with the desired precision qualifier."
4416
*
4417
* "Fragment shaders may specify the following layout qualifier only for
4418
* redeclaring the built-in gl_LastFragData array [...]: noncoherent"
4419
*/
4420
earlier->data.precision = var->data.precision;
4421
earlier->data.memory_coherent = var->data.memory_coherent;
4422
4423
} else if (state->NV_viewport_array2_enable &&
4424
strcmp(var->name, "gl_Layer") == 0 &&
4425
earlier->data.how_declared == ir_var_declared_implicitly) {
4426
/* No need to do anything, just allow it. Qualifier is stored in state */
4427
4428
} else if (state->is_version(0, 300) &&
4429
state->has_separate_shader_objects() &&
4430
(strcmp(var->name, "gl_Position") == 0 ||
4431
strcmp(var->name, "gl_PointSize") == 0)) {
4432
4433
/* EXT_separate_shader_objects spec says:
4434
*
4435
* "The following vertex shader outputs may be redeclared
4436
* at global scope to specify a built-in output interface,
4437
* with or without special qualifiers:
4438
*
4439
* gl_Position
4440
* gl_PointSize
4441
*
4442
* When compiling shaders using either of the above variables,
4443
* both such variables must be redeclared prior to use."
4444
*/
4445
if (earlier->data.used) {
4446
_mesa_glsl_error(&loc, state, "the first redeclaration of "
4447
"%s must appear before any use", var->name);
4448
}
4449
} else if ((earlier->data.how_declared == ir_var_declared_implicitly &&
4450
state->allow_builtin_variable_redeclaration) ||
4451
allow_all_redeclarations) {
4452
/* Allow verbatim redeclarations of built-in variables. Not explicitly
4453
* valid, but some applications do it.
4454
*/
4455
} else {
4456
_mesa_glsl_error(&loc, state, "`%s' redeclared", var->name);
4457
}
4458
4459
return earlier;
4460
}
4461
4462
/**
4463
* Generate the IR for an initializer in a variable declaration
4464
*/
4465
static ir_rvalue *
4466
process_initializer(ir_variable *var, ast_declaration *decl,
4467
ast_fully_specified_type *type,
4468
exec_list *initializer_instructions,
4469
struct _mesa_glsl_parse_state *state)
4470
{
4471
void *mem_ctx = state;
4472
ir_rvalue *result = NULL;
4473
4474
YYLTYPE initializer_loc = decl->initializer->get_location();
4475
4476
/* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
4477
*
4478
* "All uniform variables are read-only and are initialized either
4479
* directly by an application via API commands, or indirectly by
4480
* OpenGL."
4481
*/
4482
if (var->data.mode == ir_var_uniform) {
4483
state->check_version(120, 0, &initializer_loc,
4484
"cannot initialize uniform %s",
4485
var->name);
4486
}
4487
4488
/* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
4489
*
4490
* "Buffer variables cannot have initializers."
4491
*/
4492
if (var->data.mode == ir_var_shader_storage) {
4493
_mesa_glsl_error(&initializer_loc, state,
4494
"cannot initialize buffer variable %s",
4495
var->name);
4496
}
4497
4498
/* From section 4.1.7 of the GLSL 4.40 spec:
4499
*
4500
* "Opaque variables [...] are initialized only through the
4501
* OpenGL API; they cannot be declared with an initializer in a
4502
* shader."
4503
*
4504
* From section 4.1.7 of the ARB_bindless_texture spec:
4505
*
4506
* "Samplers may be declared as shader inputs and outputs, as uniform
4507
* variables, as temporary variables, and as function parameters."
4508
*
4509
* From section 4.1.X of the ARB_bindless_texture spec:
4510
*
4511
* "Images may be declared as shader inputs and outputs, as uniform
4512
* variables, as temporary variables, and as function parameters."
4513
*/
4514
if (var->type->contains_atomic() ||
4515
(!state->has_bindless() && var->type->contains_opaque())) {
4516
_mesa_glsl_error(&initializer_loc, state,
4517
"cannot initialize %s variable %s",
4518
var->name, state->has_bindless() ? "atomic" : "opaque");
4519
}
4520
4521
if ((var->data.mode == ir_var_shader_in) && (state->current_function == NULL)) {
4522
_mesa_glsl_error(&initializer_loc, state,
4523
"cannot initialize %s shader input / %s %s",
4524
_mesa_shader_stage_to_string(state->stage),
4525
(state->stage == MESA_SHADER_VERTEX)
4526
? "attribute" : "varying",
4527
var->name);
4528
}
4529
4530
if (var->data.mode == ir_var_shader_out && state->current_function == NULL) {
4531
_mesa_glsl_error(&initializer_loc, state,
4532
"cannot initialize %s shader output %s",
4533
_mesa_shader_stage_to_string(state->stage),
4534
var->name);
4535
}
4536
4537
/* If the initializer is an ast_aggregate_initializer, recursively store
4538
* type information from the LHS into it, so that its hir() function can do
4539
* type checking.
4540
*/
4541
if (decl->initializer->oper == ast_aggregate)
4542
_mesa_ast_set_aggregate_type(var->type, decl->initializer);
4543
4544
ir_dereference *const lhs = new(state) ir_dereference_variable(var);
4545
ir_rvalue *rhs = decl->initializer->hir(initializer_instructions, state);
4546
4547
/* Calculate the constant value if this is a const or uniform
4548
* declaration.
4549
*
4550
* Section 4.3 (Storage Qualifiers) of the GLSL ES 1.00.17 spec says:
4551
*
4552
* "Declarations of globals without a storage qualifier, or with
4553
* just the const qualifier, may include initializers, in which case
4554
* they will be initialized before the first line of main() is
4555
* executed. Such initializers must be a constant expression."
4556
*
4557
* The same section of the GLSL ES 3.00.4 spec has similar language.
4558
*/
4559
if (type->qualifier.flags.q.constant
4560
|| type->qualifier.flags.q.uniform
4561
|| (state->es_shader && state->current_function == NULL)) {
4562
ir_rvalue *new_rhs = validate_assignment(state, initializer_loc,
4563
lhs, rhs, true);
4564
if (new_rhs != NULL) {
4565
rhs = new_rhs;
4566
4567
/* Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec
4568
* says:
4569
*
4570
* "A constant expression is one of
4571
*
4572
* ...
4573
*
4574
* - an expression formed by an operator on operands that are
4575
* all constant expressions, including getting an element of
4576
* a constant array, or a field of a constant structure, or
4577
* components of a constant vector. However, the sequence
4578
* operator ( , ) and the assignment operators ( =, +=, ...)
4579
* are not included in the operators that can create a
4580
* constant expression."
4581
*
4582
* Section 12.43 (Sequence operator and constant expressions) says:
4583
*
4584
* "Should the following construct be allowed?
4585
*
4586
* float a[2,3];
4587
*
4588
* The expression within the brackets uses the sequence operator
4589
* (',') and returns the integer 3 so the construct is declaring
4590
* a single-dimensional array of size 3. In some languages, the
4591
* construct declares a two-dimensional array. It would be
4592
* preferable to make this construct illegal to avoid confusion.
4593
*
4594
* One possibility is to change the definition of the sequence
4595
* operator so that it does not return a constant-expression and
4596
* hence cannot be used to declare an array size.
4597
*
4598
* RESOLUTION: The result of a sequence operator is not a
4599
* constant-expression."
4600
*
4601
* Section 4.3.3 (Constant Expressions) of the GLSL 4.30.9 spec
4602
* contains language almost identical to the section 4.3.3 in the
4603
* GLSL ES 3.00.4 spec. This is a new limitation for these GLSL
4604
* versions.
4605
*/
4606
ir_constant *constant_value =
4607
rhs->constant_expression_value(mem_ctx);
4608
4609
if (!constant_value ||
4610
(state->is_version(430, 300) &&
4611
decl->initializer->has_sequence_subexpression())) {
4612
const char *const variable_mode =
4613
(type->qualifier.flags.q.constant)
4614
? "const"
4615
: ((type->qualifier.flags.q.uniform) ? "uniform" : "global");
4616
4617
/* If ARB_shading_language_420pack is enabled, initializers of
4618
* const-qualified local variables do not have to be constant
4619
* expressions. Const-qualified global variables must still be
4620
* initialized with constant expressions.
4621
*/
4622
if (!state->has_420pack()
4623
|| state->current_function == NULL) {
4624
_mesa_glsl_error(& initializer_loc, state,
4625
"initializer of %s variable `%s' must be a "
4626
"constant expression",
4627
variable_mode,
4628
decl->identifier);
4629
if (var->type->is_numeric()) {
4630
/* Reduce cascading errors. */
4631
var->constant_value = type->qualifier.flags.q.constant
4632
? ir_constant::zero(state, var->type) : NULL;
4633
}
4634
}
4635
} else {
4636
rhs = constant_value;
4637
var->constant_value = type->qualifier.flags.q.constant
4638
? constant_value : NULL;
4639
}
4640
} else {
4641
if (var->type->is_numeric()) {
4642
/* Reduce cascading errors. */
4643
rhs = var->constant_value = type->qualifier.flags.q.constant
4644
? ir_constant::zero(state, var->type) : NULL;
4645
}
4646
}
4647
}
4648
4649
if (rhs && !rhs->type->is_error()) {
4650
bool temp = var->data.read_only;
4651
if (type->qualifier.flags.q.constant)
4652
var->data.read_only = false;
4653
4654
/* Never emit code to initialize a uniform.
4655
*/
4656
const glsl_type *initializer_type;
4657
bool error_emitted = false;
4658
if (!type->qualifier.flags.q.uniform) {
4659
error_emitted =
4660
do_assignment(initializer_instructions, state,
4661
NULL, lhs, rhs,
4662
&result, true, true,
4663
type->get_location());
4664
initializer_type = result->type;
4665
} else
4666
initializer_type = rhs->type;
4667
4668
if (!error_emitted) {
4669
var->constant_initializer = rhs->constant_expression_value(mem_ctx);
4670
var->data.has_initializer = true;
4671
var->data.is_implicit_initializer = false;
4672
4673
/* If the declared variable is an unsized array, it must inherrit
4674
* its full type from the initializer. A declaration such as
4675
*
4676
* uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
4677
*
4678
* becomes
4679
*
4680
* uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
4681
*
4682
* The assignment generated in the if-statement (below) will also
4683
* automatically handle this case for non-uniforms.
4684
*
4685
* If the declared variable is not an array, the types must
4686
* already match exactly. As a result, the type assignment
4687
* here can be done unconditionally. For non-uniforms the call
4688
* to do_assignment can change the type of the initializer (via
4689
* the implicit conversion rules). For uniforms the initializer
4690
* must be a constant expression, and the type of that expression
4691
* was validated above.
4692
*/
4693
var->type = initializer_type;
4694
}
4695
4696
var->data.read_only = temp;
4697
}
4698
4699
return result;
4700
}
4701
4702
static void
4703
validate_layout_qualifier_vertex_count(struct _mesa_glsl_parse_state *state,
4704
YYLTYPE loc, ir_variable *var,
4705
unsigned num_vertices,
4706
unsigned *size,
4707
const char *var_category)
4708
{
4709
if (var->type->is_unsized_array()) {
4710
/* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
4711
*
4712
* All geometry shader input unsized array declarations will be
4713
* sized by an earlier input layout qualifier, when present, as per
4714
* the following table.
4715
*
4716
* Followed by a table mapping each allowed input layout qualifier to
4717
* the corresponding input length.
4718
*
4719
* Similarly for tessellation control shader outputs.
4720
*/
4721
if (num_vertices != 0)
4722
var->type = glsl_type::get_array_instance(var->type->fields.array,
4723
num_vertices);
4724
} else {
4725
/* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
4726
* includes the following examples of compile-time errors:
4727
*
4728
* // code sequence within one shader...
4729
* in vec4 Color1[]; // size unknown
4730
* ...Color1.length()...// illegal, length() unknown
4731
* in vec4 Color2[2]; // size is 2
4732
* ...Color1.length()...// illegal, Color1 still has no size
4733
* in vec4 Color3[3]; // illegal, input sizes are inconsistent
4734
* layout(lines) in; // legal, input size is 2, matching
4735
* in vec4 Color4[3]; // illegal, contradicts layout
4736
* ...
4737
*
4738
* To detect the case illustrated by Color3, we verify that the size of
4739
* an explicitly-sized array matches the size of any previously declared
4740
* explicitly-sized array. To detect the case illustrated by Color4, we
4741
* verify that the size of an explicitly-sized array is consistent with
4742
* any previously declared input layout.
4743
*/
4744
if (num_vertices != 0 && var->type->length != num_vertices) {
4745
_mesa_glsl_error(&loc, state,
4746
"%s size contradicts previously declared layout "
4747
"(size is %u, but layout requires a size of %u)",
4748
var_category, var->type->length, num_vertices);
4749
} else if (*size != 0 && var->type->length != *size) {
4750
_mesa_glsl_error(&loc, state,
4751
"%s sizes are inconsistent (size is %u, but a "
4752
"previous declaration has size %u)",
4753
var_category, var->type->length, *size);
4754
} else {
4755
*size = var->type->length;
4756
}
4757
}
4758
}
4759
4760
static void
4761
handle_tess_ctrl_shader_output_decl(struct _mesa_glsl_parse_state *state,
4762
YYLTYPE loc, ir_variable *var)
4763
{
4764
unsigned num_vertices = 0;
4765
4766
if (state->tcs_output_vertices_specified) {
4767
if (!state->out_qualifier->vertices->
4768
process_qualifier_constant(state, "vertices",
4769
&num_vertices, false)) {
4770
return;
4771
}
4772
4773
if (num_vertices > state->Const.MaxPatchVertices) {
4774
_mesa_glsl_error(&loc, state, "vertices (%d) exceeds "
4775
"GL_MAX_PATCH_VERTICES", num_vertices);
4776
return;
4777
}
4778
}
4779
4780
if (!var->type->is_array() && !var->data.patch) {
4781
_mesa_glsl_error(&loc, state,
4782
"tessellation control shader outputs must be arrays");
4783
4784
/* To avoid cascading failures, short circuit the checks below. */
4785
return;
4786
}
4787
4788
if (var->data.patch)
4789
return;
4790
4791
validate_layout_qualifier_vertex_count(state, loc, var, num_vertices,
4792
&state->tcs_output_size,
4793
"tessellation control shader output");
4794
}
4795
4796
/**
4797
* Do additional processing necessary for tessellation control/evaluation shader
4798
* input declarations. This covers both interface block arrays and bare input
4799
* variables.
4800
*/
4801
static void
4802
handle_tess_shader_input_decl(struct _mesa_glsl_parse_state *state,
4803
YYLTYPE loc, ir_variable *var)
4804
{
4805
if (!var->type->is_array() && !var->data.patch) {
4806
_mesa_glsl_error(&loc, state,
4807
"per-vertex tessellation shader inputs must be arrays");
4808
/* Avoid cascading failures. */
4809
return;
4810
}
4811
4812
if (var->data.patch)
4813
return;
4814
4815
/* The ARB_tessellation_shader spec says:
4816
*
4817
* "Declaring an array size is optional. If no size is specified, it
4818
* will be taken from the implementation-dependent maximum patch size
4819
* (gl_MaxPatchVertices). If a size is specified, it must match the
4820
* maximum patch size; otherwise, a compile or link error will occur."
4821
*
4822
* This text appears twice, once for TCS inputs, and again for TES inputs.
4823
*/
4824
if (var->type->is_unsized_array()) {
4825
var->type = glsl_type::get_array_instance(var->type->fields.array,
4826
state->Const.MaxPatchVertices);
4827
} else if (var->type->length != state->Const.MaxPatchVertices) {
4828
_mesa_glsl_error(&loc, state,
4829
"per-vertex tessellation shader input arrays must be "
4830
"sized to gl_MaxPatchVertices (%d).",
4831
state->Const.MaxPatchVertices);
4832
}
4833
}
4834
4835
4836
/**
4837
* Do additional processing necessary for geometry shader input declarations
4838
* (this covers both interface blocks arrays and bare input variables).
4839
*/
4840
static void
4841
handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state *state,
4842
YYLTYPE loc, ir_variable *var)
4843
{
4844
unsigned num_vertices = 0;
4845
4846
if (state->gs_input_prim_type_specified) {
4847
num_vertices = vertices_per_prim(state->in_qualifier->prim_type);
4848
}
4849
4850
/* Geometry shader input variables must be arrays. Caller should have
4851
* reported an error for this.
4852
*/
4853
if (!var->type->is_array()) {
4854
assert(state->error);
4855
4856
/* To avoid cascading failures, short circuit the checks below. */
4857
return;
4858
}
4859
4860
validate_layout_qualifier_vertex_count(state, loc, var, num_vertices,
4861
&state->gs_input_size,
4862
"geometry shader input");
4863
}
4864
4865
static void
4866
validate_identifier(const char *identifier, YYLTYPE loc,
4867
struct _mesa_glsl_parse_state *state)
4868
{
4869
/* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
4870
*
4871
* "Identifiers starting with "gl_" are reserved for use by
4872
* OpenGL, and may not be declared in a shader as either a
4873
* variable or a function."
4874
*/
4875
if (is_gl_identifier(identifier)) {
4876
_mesa_glsl_error(&loc, state,
4877
"identifier `%s' uses reserved `gl_' prefix",
4878
identifier);
4879
} else if (strstr(identifier, "__")) {
4880
/* From page 14 (page 20 of the PDF) of the GLSL 1.10
4881
* spec:
4882
*
4883
* "In addition, all identifiers containing two
4884
* consecutive underscores (__) are reserved as
4885
* possible future keywords."
4886
*
4887
* The intention is that names containing __ are reserved for internal
4888
* use by the implementation, and names prefixed with GL_ are reserved
4889
* for use by Khronos. Names simply containing __ are dangerous to use,
4890
* but should be allowed.
4891
*
4892
* A future version of the GLSL specification will clarify this.
4893
*/
4894
_mesa_glsl_warning(&loc, state,
4895
"identifier `%s' uses reserved `__' string",
4896
identifier);
4897
}
4898
}
4899
4900
ir_rvalue *
4901
ast_declarator_list::hir(exec_list *instructions,
4902
struct _mesa_glsl_parse_state *state)
4903
{
4904
void *ctx = state;
4905
const struct glsl_type *decl_type;
4906
const char *type_name = NULL;
4907
ir_rvalue *result = NULL;
4908
YYLTYPE loc = this->get_location();
4909
4910
/* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
4911
*
4912
* "To ensure that a particular output variable is invariant, it is
4913
* necessary to use the invariant qualifier. It can either be used to
4914
* qualify a previously declared variable as being invariant
4915
*
4916
* invariant gl_Position; // make existing gl_Position be invariant"
4917
*
4918
* In these cases the parser will set the 'invariant' flag in the declarator
4919
* list, and the type will be NULL.
4920
*/
4921
if (this->invariant) {
4922
assert(this->type == NULL);
4923
4924
if (state->current_function != NULL) {
4925
_mesa_glsl_error(& loc, state,
4926
"all uses of `invariant' keyword must be at global "
4927
"scope");
4928
}
4929
4930
foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
4931
assert(decl->array_specifier == NULL);
4932
assert(decl->initializer == NULL);
4933
4934
ir_variable *const earlier =
4935
state->symbols->get_variable(decl->identifier);
4936
if (earlier == NULL) {
4937
_mesa_glsl_error(& loc, state,
4938
"undeclared variable `%s' cannot be marked "
4939
"invariant", decl->identifier);
4940
} else if (!is_allowed_invariant(earlier, state)) {
4941
_mesa_glsl_error(&loc, state,
4942
"`%s' cannot be marked invariant; interfaces between "
4943
"shader stages only.", decl->identifier);
4944
} else if (earlier->data.used) {
4945
_mesa_glsl_error(& loc, state,
4946
"variable `%s' may not be redeclared "
4947
"`invariant' after being used",
4948
earlier->name);
4949
} else {
4950
earlier->data.explicit_invariant = true;
4951
earlier->data.invariant = true;
4952
}
4953
}
4954
4955
/* Invariant redeclarations do not have r-values.
4956
*/
4957
return NULL;
4958
}
4959
4960
if (this->precise) {
4961
assert(this->type == NULL);
4962
4963
foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
4964
assert(decl->array_specifier == NULL);
4965
assert(decl->initializer == NULL);
4966
4967
ir_variable *const earlier =
4968
state->symbols->get_variable(decl->identifier);
4969
if (earlier == NULL) {
4970
_mesa_glsl_error(& loc, state,
4971
"undeclared variable `%s' cannot be marked "
4972
"precise", decl->identifier);
4973
} else if (state->current_function != NULL &&
4974
!state->symbols->name_declared_this_scope(decl->identifier)) {
4975
/* Note: we have to check if we're in a function, since
4976
* builtins are treated as having come from another scope.
4977
*/
4978
_mesa_glsl_error(& loc, state,
4979
"variable `%s' from an outer scope may not be "
4980
"redeclared `precise' in this scope",
4981
earlier->name);
4982
} else if (earlier->data.used) {
4983
_mesa_glsl_error(& loc, state,
4984
"variable `%s' may not be redeclared "
4985
"`precise' after being used",
4986
earlier->name);
4987
} else {
4988
earlier->data.precise = true;
4989
}
4990
}
4991
4992
/* Precise redeclarations do not have r-values either. */
4993
return NULL;
4994
}
4995
4996
assert(this->type != NULL);
4997
assert(!this->invariant);
4998
assert(!this->precise);
4999
5000
/* GL_EXT_shader_image_load_store base type uses GLSL_TYPE_VOID as a special value to
5001
* indicate that it needs to be updated later (see glsl_parser.yy).
5002
* This is done here, based on the layout qualifier and the type of the image var
5003
*/
5004
if (this->type->qualifier.flags.q.explicit_image_format &&
5005
this->type->specifier->type->is_image() &&
5006
this->type->qualifier.image_base_type == GLSL_TYPE_VOID) {
5007
/* "The ARB_shader_image_load_store says:
5008
* If both extensions are enabled in the shading language, the "size*" layout
5009
* qualifiers are treated as format qualifiers, and are mapped to equivalent
5010
* format qualifiers in the table below, according to the type of image
5011
* variable.
5012
* image* iimage* uimage*
5013
* -------- -------- --------
5014
* size1x8 n/a r8i r8ui
5015
* size1x16 r16f r16i r16ui
5016
* size1x32 r32f r32i r32ui
5017
* size2x32 rg32f rg32i rg32ui
5018
* size4x32 rgba32f rgba32i rgba32ui"
5019
*/
5020
if (strncmp(this->type->specifier->type_name, "image", strlen("image")) == 0) {
5021
switch (this->type->qualifier.image_format) {
5022
case PIPE_FORMAT_R8_SINT:
5023
/* The GL_EXT_shader_image_load_store spec says:
5024
* A layout of "size1x8" is illegal for image variables associated
5025
* with floating-point data types.
5026
*/
5027
_mesa_glsl_error(& loc, state,
5028
"size1x8 is illegal for image variables "
5029
"with floating-point data types.");
5030
return NULL;
5031
case PIPE_FORMAT_R16_SINT:
5032
this->type->qualifier.image_format = PIPE_FORMAT_R16_FLOAT;
5033
break;
5034
case PIPE_FORMAT_R32_SINT:
5035
this->type->qualifier.image_format = PIPE_FORMAT_R32_FLOAT;
5036
break;
5037
case PIPE_FORMAT_R32G32_SINT:
5038
this->type->qualifier.image_format = PIPE_FORMAT_R32G32_FLOAT;
5039
break;
5040
case PIPE_FORMAT_R32G32B32A32_SINT:
5041
this->type->qualifier.image_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
5042
break;
5043
default:
5044
unreachable("Unknown image format");
5045
}
5046
this->type->qualifier.image_base_type = GLSL_TYPE_FLOAT;
5047
} else if (strncmp(this->type->specifier->type_name, "uimage", strlen("uimage")) == 0) {
5048
switch (this->type->qualifier.image_format) {
5049
case PIPE_FORMAT_R8_SINT:
5050
this->type->qualifier.image_format = PIPE_FORMAT_R8_UINT;
5051
break;
5052
case PIPE_FORMAT_R16_SINT:
5053
this->type->qualifier.image_format = PIPE_FORMAT_R16_UINT;
5054
break;
5055
case PIPE_FORMAT_R32_SINT:
5056
this->type->qualifier.image_format = PIPE_FORMAT_R32_UINT;
5057
break;
5058
case PIPE_FORMAT_R32G32_SINT:
5059
this->type->qualifier.image_format = PIPE_FORMAT_R32G32_UINT;
5060
break;
5061
case PIPE_FORMAT_R32G32B32A32_SINT:
5062
this->type->qualifier.image_format = PIPE_FORMAT_R32G32B32A32_UINT;
5063
break;
5064
default:
5065
unreachable("Unknown image format");
5066
}
5067
this->type->qualifier.image_base_type = GLSL_TYPE_UINT;
5068
} else if (strncmp(this->type->specifier->type_name, "iimage", strlen("iimage")) == 0) {
5069
this->type->qualifier.image_base_type = GLSL_TYPE_INT;
5070
} else {
5071
assert(false);
5072
}
5073
}
5074
5075
/* The type specifier may contain a structure definition. Process that
5076
* before any of the variable declarations.
5077
*/
5078
(void) this->type->specifier->hir(instructions, state);
5079
5080
decl_type = this->type->glsl_type(& type_name, state);
5081
5082
/* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
5083
* "Buffer variables may only be declared inside interface blocks
5084
* (section 4.3.9 “Interface Blocks”), which are then referred to as
5085
* shader storage blocks. It is a compile-time error to declare buffer
5086
* variables at global scope (outside a block)."
5087
*/
5088
if (type->qualifier.flags.q.buffer && !decl_type->is_interface()) {
5089
_mesa_glsl_error(&loc, state,
5090
"buffer variables cannot be declared outside "
5091
"interface blocks");
5092
}
5093
5094
/* An offset-qualified atomic counter declaration sets the default
5095
* offset for the next declaration within the same atomic counter
5096
* buffer.
5097
*/
5098
if (decl_type && decl_type->contains_atomic()) {
5099
if (type->qualifier.flags.q.explicit_binding &&
5100
type->qualifier.flags.q.explicit_offset) {
5101
unsigned qual_binding;
5102
unsigned qual_offset;
5103
if (process_qualifier_constant(state, &loc, "binding",
5104
type->qualifier.binding,
5105
&qual_binding)
5106
&& process_qualifier_constant(state, &loc, "offset",
5107
type->qualifier.offset,
5108
&qual_offset)) {
5109
if (qual_binding < ARRAY_SIZE(state->atomic_counter_offsets))
5110
state->atomic_counter_offsets[qual_binding] = qual_offset;
5111
}
5112
}
5113
5114
ast_type_qualifier allowed_atomic_qual_mask;
5115
allowed_atomic_qual_mask.flags.i = 0;
5116
allowed_atomic_qual_mask.flags.q.explicit_binding = 1;
5117
allowed_atomic_qual_mask.flags.q.explicit_offset = 1;
5118
allowed_atomic_qual_mask.flags.q.uniform = 1;
5119
5120
type->qualifier.validate_flags(&loc, state, allowed_atomic_qual_mask,
5121
"invalid layout qualifier for",
5122
"atomic_uint");
5123
}
5124
5125
if (this->declarations.is_empty()) {
5126
/* If there is no structure involved in the program text, there are two
5127
* possible scenarios:
5128
*
5129
* - The program text contained something like 'vec4;'. This is an
5130
* empty declaration. It is valid but weird. Emit a warning.
5131
*
5132
* - The program text contained something like 'S;' and 'S' is not the
5133
* name of a known structure type. This is both invalid and weird.
5134
* Emit an error.
5135
*
5136
* - The program text contained something like 'mediump float;'
5137
* when the programmer probably meant 'precision mediump
5138
* float;' Emit a warning with a description of what they
5139
* probably meant to do.
5140
*
5141
* Note that if decl_type is NULL and there is a structure involved,
5142
* there must have been some sort of error with the structure. In this
5143
* case we assume that an error was already generated on this line of
5144
* code for the structure. There is no need to generate an additional,
5145
* confusing error.
5146
*/
5147
assert(this->type->specifier->structure == NULL || decl_type != NULL
5148
|| state->error);
5149
5150
if (decl_type == NULL) {
5151
_mesa_glsl_error(&loc, state,
5152
"invalid type `%s' in empty declaration",
5153
type_name);
5154
} else {
5155
if (decl_type->is_array()) {
5156
/* From Section 13.22 (Array Declarations) of the GLSL ES 3.2
5157
* spec:
5158
*
5159
* "... any declaration that leaves the size undefined is
5160
* disallowed as this would add complexity and there are no
5161
* use-cases."
5162
*/
5163
if (state->es_shader && decl_type->is_unsized_array()) {
5164
_mesa_glsl_error(&loc, state, "array size must be explicitly "
5165
"or implicitly defined");
5166
}
5167
5168
/* From Section 4.12 (Empty Declarations) of the GLSL 4.5 spec:
5169
*
5170
* "The combinations of types and qualifiers that cause
5171
* compile-time or link-time errors are the same whether or not
5172
* the declaration is empty."
5173
*/
5174
validate_array_dimensions(decl_type, state, &loc);
5175
}
5176
5177
if (decl_type->is_atomic_uint()) {
5178
/* Empty atomic counter declarations are allowed and useful
5179
* to set the default offset qualifier.
5180
*/
5181
return NULL;
5182
} else if (this->type->qualifier.precision != ast_precision_none) {
5183
if (this->type->specifier->structure != NULL) {
5184
_mesa_glsl_error(&loc, state,
5185
"precision qualifiers can't be applied "
5186
"to structures");
5187
} else {
5188
static const char *const precision_names[] = {
5189
"highp",
5190
"highp",
5191
"mediump",
5192
"lowp"
5193
};
5194
5195
_mesa_glsl_warning(&loc, state,
5196
"empty declaration with precision "
5197
"qualifier, to set the default precision, "
5198
"use `precision %s %s;'",
5199
precision_names[this->type->
5200
qualifier.precision],
5201
type_name);
5202
}
5203
} else if (this->type->specifier->structure == NULL) {
5204
_mesa_glsl_warning(&loc, state, "empty declaration");
5205
}
5206
}
5207
}
5208
5209
foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
5210
const struct glsl_type *var_type;
5211
ir_variable *var;
5212
const char *identifier = decl->identifier;
5213
/* FINISHME: Emit a warning if a variable declaration shadows a
5214
* FINISHME: declaration at a higher scope.
5215
*/
5216
5217
if ((decl_type == NULL) || decl_type->is_void()) {
5218
if (type_name != NULL) {
5219
_mesa_glsl_error(& loc, state,
5220
"invalid type `%s' in declaration of `%s'",
5221
type_name, decl->identifier);
5222
} else {
5223
_mesa_glsl_error(& loc, state,
5224
"invalid type in declaration of `%s'",
5225
decl->identifier);
5226
}
5227
continue;
5228
}
5229
5230
if (this->type->qualifier.is_subroutine_decl()) {
5231
const glsl_type *t;
5232
const char *name;
5233
5234
t = state->symbols->get_type(this->type->specifier->type_name);
5235
if (!t)
5236
_mesa_glsl_error(& loc, state,
5237
"invalid type in declaration of `%s'",
5238
decl->identifier);
5239
name = ralloc_asprintf(ctx, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state->stage), decl->identifier);
5240
5241
identifier = name;
5242
5243
}
5244
var_type = process_array_type(&loc, decl_type, decl->array_specifier,
5245
state);
5246
5247
var = new(ctx) ir_variable(var_type, identifier, ir_var_auto);
5248
5249
/* The 'varying in' and 'varying out' qualifiers can only be used with
5250
* ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
5251
* yet.
5252
*/
5253
if (this->type->qualifier.flags.q.varying) {
5254
if (this->type->qualifier.flags.q.in) {
5255
_mesa_glsl_error(& loc, state,
5256
"`varying in' qualifier in declaration of "
5257
"`%s' only valid for geometry shaders using "
5258
"ARB_geometry_shader4 or EXT_geometry_shader4",
5259
decl->identifier);
5260
} else if (this->type->qualifier.flags.q.out) {
5261
_mesa_glsl_error(& loc, state,
5262
"`varying out' qualifier in declaration of "
5263
"`%s' only valid for geometry shaders using "
5264
"ARB_geometry_shader4 or EXT_geometry_shader4",
5265
decl->identifier);
5266
}
5267
}
5268
5269
/* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
5270
*
5271
* "Global variables can only use the qualifiers const,
5272
* attribute, uniform, or varying. Only one may be
5273
* specified.
5274
*
5275
* Local variables can only use the qualifier const."
5276
*
5277
* This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
5278
* any extension that adds the 'layout' keyword.
5279
*/
5280
if (!state->is_version(130, 300)
5281
&& !state->has_explicit_attrib_location()
5282
&& !state->has_separate_shader_objects()
5283
&& !state->ARB_fragment_coord_conventions_enable) {
5284
/* GL_EXT_gpu_shader4 only allows "varying out" on fragment shader
5285
* outputs. (the varying flag is not set by the parser)
5286
*/
5287
if (this->type->qualifier.flags.q.out &&
5288
(!state->EXT_gpu_shader4_enable ||
5289
state->stage != MESA_SHADER_FRAGMENT)) {
5290
_mesa_glsl_error(& loc, state,
5291
"`out' qualifier in declaration of `%s' "
5292
"only valid for function parameters in %s",
5293
decl->identifier, state->get_version_string());
5294
}
5295
if (this->type->qualifier.flags.q.in) {
5296
_mesa_glsl_error(& loc, state,
5297
"`in' qualifier in declaration of `%s' "
5298
"only valid for function parameters in %s",
5299
decl->identifier, state->get_version_string());
5300
}
5301
/* FINISHME: Test for other invalid qualifiers. */
5302
}
5303
5304
apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
5305
& loc, false);
5306
apply_layout_qualifier_to_variable(&this->type->qualifier, var, state,
5307
&loc);
5308
5309
if ((state->zero_init & (1u << var->data.mode)) &&
5310
(var->type->is_numeric() || var->type->is_boolean())) {
5311
const ir_constant_data data = { { 0 } };
5312
var->data.has_initializer = true;
5313
var->data.is_implicit_initializer = true;
5314
var->constant_initializer = new(var) ir_constant(var->type, &data);
5315
}
5316
5317
if (this->type->qualifier.flags.q.invariant) {
5318
if (!is_allowed_invariant(var, state)) {
5319
_mesa_glsl_error(&loc, state,
5320
"`%s' cannot be marked invariant; interfaces between "
5321
"shader stages only", var->name);
5322
}
5323
}
5324
5325
if (state->current_function != NULL) {
5326
const char *mode = NULL;
5327
const char *extra = "";
5328
5329
/* There is no need to check for 'inout' here because the parser will
5330
* only allow that in function parameter lists.
5331
*/
5332
if (this->type->qualifier.flags.q.attribute) {
5333
mode = "attribute";
5334
} else if (this->type->qualifier.is_subroutine_decl()) {
5335
mode = "subroutine uniform";
5336
} else if (this->type->qualifier.flags.q.uniform) {
5337
mode = "uniform";
5338
} else if (this->type->qualifier.flags.q.varying) {
5339
mode = "varying";
5340
} else if (this->type->qualifier.flags.q.in) {
5341
mode = "in";
5342
extra = " or in function parameter list";
5343
} else if (this->type->qualifier.flags.q.out) {
5344
mode = "out";
5345
extra = " or in function parameter list";
5346
}
5347
5348
if (mode) {
5349
_mesa_glsl_error(& loc, state,
5350
"%s variable `%s' must be declared at "
5351
"global scope%s",
5352
mode, var->name, extra);
5353
}
5354
} else if (var->data.mode == ir_var_shader_in) {
5355
var->data.read_only = true;
5356
5357
if (state->stage == MESA_SHADER_VERTEX) {
5358
/* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
5359
*
5360
* "Vertex shader inputs can only be float, floating-point
5361
* vectors, matrices, signed and unsigned integers and integer
5362
* vectors. Vertex shader inputs can also form arrays of these
5363
* types, but not structures."
5364
*
5365
* From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
5366
*
5367
* "Vertex shader inputs can only be float, floating-point
5368
* vectors, matrices, signed and unsigned integers and integer
5369
* vectors. They cannot be arrays or structures."
5370
*
5371
* From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
5372
*
5373
* "The attribute qualifier can be used only with float,
5374
* floating-point vectors, and matrices. Attribute variables
5375
* cannot be declared as arrays or structures."
5376
*
5377
* From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
5378
*
5379
* "Vertex shader inputs can only be float, floating-point
5380
* vectors, matrices, signed and unsigned integers and integer
5381
* vectors. Vertex shader inputs cannot be arrays or
5382
* structures."
5383
*
5384
* From section 4.3.4 of the ARB_bindless_texture spec:
5385
*
5386
* "(modify third paragraph of the section to allow sampler and
5387
* image types) ... Vertex shader inputs can only be float,
5388
* single-precision floating-point scalars, single-precision
5389
* floating-point vectors, matrices, signed and unsigned
5390
* integers and integer vectors, sampler and image types."
5391
*/
5392
const glsl_type *check_type = var->type->without_array();
5393
5394
bool error = false;
5395
switch (check_type->base_type) {
5396
case GLSL_TYPE_FLOAT:
5397
break;
5398
case GLSL_TYPE_UINT64:
5399
case GLSL_TYPE_INT64:
5400
break;
5401
case GLSL_TYPE_UINT:
5402
case GLSL_TYPE_INT:
5403
error = !state->is_version(120, 300) && !state->EXT_gpu_shader4_enable;
5404
break;
5405
case GLSL_TYPE_DOUBLE:
5406
error = !state->is_version(410, 0) && !state->ARB_vertex_attrib_64bit_enable;
5407
break;
5408
case GLSL_TYPE_SAMPLER:
5409
case GLSL_TYPE_IMAGE:
5410
error = !state->has_bindless();
5411
break;
5412
default:
5413
error = true;
5414
}
5415
5416
if (error) {
5417
_mesa_glsl_error(& loc, state,
5418
"vertex shader input / attribute cannot have "
5419
"type %s`%s'",
5420
var->type->is_array() ? "array of " : "",
5421
check_type->name);
5422
} else if (var->type->is_array() &&
5423
!state->check_version(150, 0, &loc,
5424
"vertex shader input / attribute "
5425
"cannot have array type")) {
5426
}
5427
} else if (state->stage == MESA_SHADER_GEOMETRY) {
5428
/* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
5429
*
5430
* Geometry shader input variables get the per-vertex values
5431
* written out by vertex shader output variables of the same
5432
* names. Since a geometry shader operates on a set of
5433
* vertices, each input varying variable (or input block, see
5434
* interface blocks below) needs to be declared as an array.
5435
*/
5436
if (!var->type->is_array()) {
5437
_mesa_glsl_error(&loc, state,
5438
"geometry shader inputs must be arrays");
5439
}
5440
5441
handle_geometry_shader_input_decl(state, loc, var);
5442
} else if (state->stage == MESA_SHADER_FRAGMENT) {
5443
/* From section 4.3.4 (Input Variables) of the GLSL ES 3.10 spec:
5444
*
5445
* It is a compile-time error to declare a fragment shader
5446
* input with, or that contains, any of the following types:
5447
*
5448
* * A boolean type
5449
* * An opaque type
5450
* * An array of arrays
5451
* * An array of structures
5452
* * A structure containing an array
5453
* * A structure containing a structure
5454
*/
5455
if (state->es_shader) {
5456
const glsl_type *check_type = var->type->without_array();
5457
if (check_type->is_boolean() ||
5458
check_type->contains_opaque()) {
5459
_mesa_glsl_error(&loc, state,
5460
"fragment shader input cannot have type %s",
5461
check_type->name);
5462
}
5463
if (var->type->is_array() &&
5464
var->type->fields.array->is_array()) {
5465
_mesa_glsl_error(&loc, state,
5466
"%s shader output "
5467
"cannot have an array of arrays",
5468
_mesa_shader_stage_to_string(state->stage));
5469
}
5470
if (var->type->is_array() &&
5471
var->type->fields.array->is_struct()) {
5472
_mesa_glsl_error(&loc, state,
5473
"fragment shader input "
5474
"cannot have an array of structs");
5475
}
5476
if (var->type->is_struct()) {
5477
for (unsigned i = 0; i < var->type->length; i++) {
5478
if (var->type->fields.structure[i].type->is_array() ||
5479
var->type->fields.structure[i].type->is_struct())
5480
_mesa_glsl_error(&loc, state,
5481
"fragment shader input cannot have "
5482
"a struct that contains an "
5483
"array or struct");
5484
}
5485
}
5486
}
5487
} else if (state->stage == MESA_SHADER_TESS_CTRL ||
5488
state->stage == MESA_SHADER_TESS_EVAL) {
5489
handle_tess_shader_input_decl(state, loc, var);
5490
}
5491
} else if (var->data.mode == ir_var_shader_out) {
5492
const glsl_type *check_type = var->type->without_array();
5493
5494
/* From section 4.3.6 (Output variables) of the GLSL 4.40 spec:
5495
*
5496
* It is a compile-time error to declare a fragment shader output
5497
* that contains any of the following:
5498
*
5499
* * A Boolean type (bool, bvec2 ...)
5500
* * A double-precision scalar or vector (double, dvec2 ...)
5501
* * An opaque type
5502
* * Any matrix type
5503
* * A structure
5504
*/
5505
if (state->stage == MESA_SHADER_FRAGMENT) {
5506
if (check_type->is_struct() || check_type->is_matrix())
5507
_mesa_glsl_error(&loc, state,
5508
"fragment shader output "
5509
"cannot have struct or matrix type");
5510
switch (check_type->base_type) {
5511
case GLSL_TYPE_UINT:
5512
case GLSL_TYPE_INT:
5513
case GLSL_TYPE_FLOAT:
5514
break;
5515
default:
5516
_mesa_glsl_error(&loc, state,
5517
"fragment shader output cannot have "
5518
"type %s", check_type->name);
5519
}
5520
}
5521
5522
/* From section 4.3.6 (Output Variables) of the GLSL ES 3.10 spec:
5523
*
5524
* It is a compile-time error to declare a vertex shader output
5525
* with, or that contains, any of the following types:
5526
*
5527
* * A boolean type
5528
* * An opaque type
5529
* * An array of arrays
5530
* * An array of structures
5531
* * A structure containing an array
5532
* * A structure containing a structure
5533
*
5534
* It is a compile-time error to declare a fragment shader output
5535
* with, or that contains, any of the following types:
5536
*
5537
* * A boolean type
5538
* * An opaque type
5539
* * A matrix
5540
* * A structure
5541
* * An array of array
5542
*
5543
* ES 3.20 updates this to apply to tessellation and geometry shaders
5544
* as well. Because there are per-vertex arrays in the new stages,
5545
* it strikes the "array of..." rules and replaces them with these:
5546
*
5547
* * For per-vertex-arrayed variables (applies to tessellation
5548
* control, tessellation evaluation and geometry shaders):
5549
*
5550
* * Per-vertex-arrayed arrays of arrays
5551
* * Per-vertex-arrayed arrays of structures
5552
*
5553
* * For non-per-vertex-arrayed variables:
5554
*
5555
* * An array of arrays
5556
* * An array of structures
5557
*
5558
* which basically says to unwrap the per-vertex aspect and apply
5559
* the old rules.
5560
*/
5561
if (state->es_shader) {
5562
if (var->type->is_array() &&
5563
var->type->fields.array->is_array()) {
5564
_mesa_glsl_error(&loc, state,
5565
"%s shader output "
5566
"cannot have an array of arrays",
5567
_mesa_shader_stage_to_string(state->stage));
5568
}
5569
if (state->stage <= MESA_SHADER_GEOMETRY) {
5570
const glsl_type *type = var->type;
5571
5572
if (state->stage == MESA_SHADER_TESS_CTRL &&
5573
!var->data.patch && var->type->is_array()) {
5574
type = var->type->fields.array;
5575
}
5576
5577
if (type->is_array() && type->fields.array->is_struct()) {
5578
_mesa_glsl_error(&loc, state,
5579
"%s shader output cannot have "
5580
"an array of structs",
5581
_mesa_shader_stage_to_string(state->stage));
5582
}
5583
if (type->is_struct()) {
5584
for (unsigned i = 0; i < type->length; i++) {
5585
if (type->fields.structure[i].type->is_array() ||
5586
type->fields.structure[i].type->is_struct())
5587
_mesa_glsl_error(&loc, state,
5588
"%s shader output cannot have a "
5589
"struct that contains an "
5590
"array or struct",
5591
_mesa_shader_stage_to_string(state->stage));
5592
}
5593
}
5594
}
5595
}
5596
5597
if (state->stage == MESA_SHADER_TESS_CTRL) {
5598
handle_tess_ctrl_shader_output_decl(state, loc, var);
5599
}
5600
} else if (var->type->contains_subroutine()) {
5601
/* declare subroutine uniforms as hidden */
5602
var->data.how_declared = ir_var_hidden;
5603
}
5604
5605
/* From section 4.3.4 of the GLSL 4.00 spec:
5606
* "Input variables may not be declared using the patch in qualifier
5607
* in tessellation control or geometry shaders."
5608
*
5609
* From section 4.3.6 of the GLSL 4.00 spec:
5610
* "It is an error to use patch out in a vertex, tessellation
5611
* evaluation, or geometry shader."
5612
*
5613
* This doesn't explicitly forbid using them in a fragment shader, but
5614
* that's probably just an oversight.
5615
*/
5616
if (state->stage != MESA_SHADER_TESS_EVAL
5617
&& this->type->qualifier.flags.q.patch
5618
&& this->type->qualifier.flags.q.in) {
5619
5620
_mesa_glsl_error(&loc, state, "'patch in' can only be used in a "
5621
"tessellation evaluation shader");
5622
}
5623
5624
if (state->stage != MESA_SHADER_TESS_CTRL
5625
&& this->type->qualifier.flags.q.patch
5626
&& this->type->qualifier.flags.q.out) {
5627
5628
_mesa_glsl_error(&loc, state, "'patch out' can only be used in a "
5629
"tessellation control shader");
5630
}
5631
5632
/* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
5633
*/
5634
if (this->type->qualifier.precision != ast_precision_none) {
5635
state->check_precision_qualifiers_allowed(&loc);
5636
}
5637
5638
if (this->type->qualifier.precision != ast_precision_none &&
5639
!precision_qualifier_allowed(var->type)) {
5640
_mesa_glsl_error(&loc, state,
5641
"precision qualifiers apply only to floating point"
5642
", integer and opaque types");
5643
}
5644
5645
/* From section 4.1.7 of the GLSL 4.40 spec:
5646
*
5647
* "[Opaque types] can only be declared as function
5648
* parameters or uniform-qualified variables."
5649
*
5650
* From section 4.1.7 of the ARB_bindless_texture spec:
5651
*
5652
* "Samplers may be declared as shader inputs and outputs, as uniform
5653
* variables, as temporary variables, and as function parameters."
5654
*
5655
* From section 4.1.X of the ARB_bindless_texture spec:
5656
*
5657
* "Images may be declared as shader inputs and outputs, as uniform
5658
* variables, as temporary variables, and as function parameters."
5659
*/
5660
if (!this->type->qualifier.flags.q.uniform &&
5661
(var_type->contains_atomic() ||
5662
(!state->has_bindless() && var_type->contains_opaque()))) {
5663
_mesa_glsl_error(&loc, state,
5664
"%s variables must be declared uniform",
5665
state->has_bindless() ? "atomic" : "opaque");
5666
}
5667
5668
/* Process the initializer and add its instructions to a temporary
5669
* list. This list will be added to the instruction stream (below) after
5670
* the declaration is added. This is done because in some cases (such as
5671
* redeclarations) the declaration may not actually be added to the
5672
* instruction stream.
5673
*/
5674
exec_list initializer_instructions;
5675
5676
/* Examine var name here since var may get deleted in the next call */
5677
bool var_is_gl_id = is_gl_identifier(var->name);
5678
5679
bool is_redeclaration;
5680
var = get_variable_being_redeclared(&var, decl->get_location(), state,
5681
false /* allow_all_redeclarations */,
5682
&is_redeclaration);
5683
if (is_redeclaration) {
5684
if (var_is_gl_id &&
5685
var->data.how_declared == ir_var_declared_in_block) {
5686
_mesa_glsl_error(&loc, state,
5687
"`%s' has already been redeclared using "
5688
"gl_PerVertex", var->name);
5689
}
5690
var->data.how_declared = ir_var_declared_normally;
5691
}
5692
5693
if (decl->initializer != NULL) {
5694
result = process_initializer(var,
5695
decl, this->type,
5696
&initializer_instructions, state);
5697
} else {
5698
validate_array_dimensions(var_type, state, &loc);
5699
}
5700
5701
/* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
5702
*
5703
* "It is an error to write to a const variable outside of
5704
* its declaration, so they must be initialized when
5705
* declared."
5706
*/
5707
if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) {
5708
_mesa_glsl_error(& loc, state,
5709
"const declaration of `%s' must be initialized",
5710
decl->identifier);
5711
}
5712
5713
if (state->es_shader) {
5714
const glsl_type *const t = var->type;
5715
5716
/* Skip the unsized array check for TCS/TES/GS inputs & TCS outputs.
5717
*
5718
* The GL_OES_tessellation_shader spec says about inputs:
5719
*
5720
* "Declaring an array size is optional. If no size is specified,
5721
* it will be taken from the implementation-dependent maximum
5722
* patch size (gl_MaxPatchVertices)."
5723
*
5724
* and about TCS outputs:
5725
*
5726
* "If no size is specified, it will be taken from output patch
5727
* size declared in the shader."
5728
*
5729
* The GL_OES_geometry_shader spec says:
5730
*
5731
* "All geometry shader input unsized array declarations will be
5732
* sized by an earlier input primitive layout qualifier, when
5733
* present, as per the following table."
5734
*/
5735
const bool implicitly_sized =
5736
(var->data.mode == ir_var_shader_in &&
5737
state->stage >= MESA_SHADER_TESS_CTRL &&
5738
state->stage <= MESA_SHADER_GEOMETRY) ||
5739
(var->data.mode == ir_var_shader_out &&
5740
state->stage == MESA_SHADER_TESS_CTRL);
5741
5742
if (t->is_unsized_array() && !implicitly_sized)
5743
/* Section 10.17 of the GLSL ES 1.00 specification states that
5744
* unsized array declarations have been removed from the language.
5745
* Arrays that are sized using an initializer are still explicitly
5746
* sized. However, GLSL ES 1.00 does not allow array
5747
* initializers. That is only allowed in GLSL ES 3.00.
5748
*
5749
* Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
5750
*
5751
* "An array type can also be formed without specifying a size
5752
* if the definition includes an initializer:
5753
*
5754
* float x[] = float[2] (1.0, 2.0); // declares an array of size 2
5755
* float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
5756
*
5757
* float a[5];
5758
* float b[] = a;"
5759
*/
5760
_mesa_glsl_error(& loc, state,
5761
"unsized array declarations are not allowed in "
5762
"GLSL ES");
5763
}
5764
5765
/* Section 4.4.6.1 Atomic Counter Layout Qualifiers of the GLSL 4.60 spec:
5766
*
5767
* "It is a compile-time error to declare an unsized array of
5768
* atomic_uint"
5769
*/
5770
if (var->type->is_unsized_array() &&
5771
var->type->without_array()->base_type == GLSL_TYPE_ATOMIC_UINT) {
5772
_mesa_glsl_error(& loc, state,
5773
"Unsized array of atomic_uint is not allowed");
5774
}
5775
5776
/* If the declaration is not a redeclaration, there are a few additional
5777
* semantic checks that must be applied. In addition, variable that was
5778
* created for the declaration should be added to the IR stream.
5779
*/
5780
if (!is_redeclaration) {
5781
validate_identifier(decl->identifier, loc, state);
5782
5783
/* Add the variable to the symbol table. Note that the initializer's
5784
* IR was already processed earlier (though it hasn't been emitted
5785
* yet), without the variable in scope.
5786
*
5787
* This differs from most C-like languages, but it follows the GLSL
5788
* specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
5789
* spec:
5790
*
5791
* "Within a declaration, the scope of a name starts immediately
5792
* after the initializer if present or immediately after the name
5793
* being declared if not."
5794
*/
5795
if (!state->symbols->add_variable(var)) {
5796
YYLTYPE loc = this->get_location();
5797
_mesa_glsl_error(&loc, state, "name `%s' already taken in the "
5798
"current scope", decl->identifier);
5799
continue;
5800
}
5801
5802
/* Push the variable declaration to the top. It means that all the
5803
* variable declarations will appear in a funny last-to-first order,
5804
* but otherwise we run into trouble if a function is prototyped, a
5805
* global var is decled, then the function is defined with usage of
5806
* the global var. See glslparsertest's CorrectModule.frag.
5807
*/
5808
instructions->push_head(var);
5809
}
5810
5811
instructions->append_list(&initializer_instructions);
5812
}
5813
5814
5815
/* Generally, variable declarations do not have r-values. However,
5816
* one is used for the declaration in
5817
*
5818
* while (bool b = some_condition()) {
5819
* ...
5820
* }
5821
*
5822
* so we return the rvalue from the last seen declaration here.
5823
*/
5824
return result;
5825
}
5826
5827
5828
ir_rvalue *
5829
ast_parameter_declarator::hir(exec_list *instructions,
5830
struct _mesa_glsl_parse_state *state)
5831
{
5832
void *ctx = state;
5833
const struct glsl_type *type;
5834
const char *name = NULL;
5835
YYLTYPE loc = this->get_location();
5836
5837
type = this->type->glsl_type(& name, state);
5838
5839
if (type == NULL) {
5840
if (name != NULL) {
5841
_mesa_glsl_error(& loc, state,
5842
"invalid type `%s' in declaration of `%s'",
5843
name, this->identifier);
5844
} else {
5845
_mesa_glsl_error(& loc, state,
5846
"invalid type in declaration of `%s'",
5847
this->identifier);
5848
}
5849
5850
type = glsl_type::error_type;
5851
}
5852
5853
/* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
5854
*
5855
* "Functions that accept no input arguments need not use void in the
5856
* argument list because prototypes (or definitions) are required and
5857
* therefore there is no ambiguity when an empty argument list "( )" is
5858
* declared. The idiom "(void)" as a parameter list is provided for
5859
* convenience."
5860
*
5861
* Placing this check here prevents a void parameter being set up
5862
* for a function, which avoids tripping up checks for main taking
5863
* parameters and lookups of an unnamed symbol.
5864
*/
5865
if (type->is_void()) {
5866
if (this->identifier != NULL)
5867
_mesa_glsl_error(& loc, state,
5868
"named parameter cannot have type `void'");
5869
5870
is_void = true;
5871
return NULL;
5872
}
5873
5874
if (formal_parameter && (this->identifier == NULL)) {
5875
_mesa_glsl_error(& loc, state, "formal parameter lacks a name");
5876
return NULL;
5877
}
5878
5879
/* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
5880
* call already handled the "vec4[..] foo" case.
5881
*/
5882
type = process_array_type(&loc, type, this->array_specifier, state);
5883
5884
if (!type->is_error() && type->is_unsized_array()) {
5885
_mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
5886
"a declared size");
5887
type = glsl_type::error_type;
5888
}
5889
5890
is_void = false;
5891
ir_variable *var = new(ctx)
5892
ir_variable(type, this->identifier, ir_var_function_in);
5893
5894
/* Apply any specified qualifiers to the parameter declaration. Note that
5895
* for function parameters the default mode is 'in'.
5896
*/
5897
apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc,
5898
true);
5899
5900
if (((1u << var->data.mode) & state->zero_init) &&
5901
(var->type->is_numeric() || var->type->is_boolean())) {
5902
const ir_constant_data data = { { 0 } };
5903
var->data.has_initializer = true;
5904
var->data.is_implicit_initializer = true;
5905
var->constant_initializer = new(var) ir_constant(var->type, &data);
5906
}
5907
5908
/* From section 4.1.7 of the GLSL 4.40 spec:
5909
*
5910
* "Opaque variables cannot be treated as l-values; hence cannot
5911
* be used as out or inout function parameters, nor can they be
5912
* assigned into."
5913
*
5914
* From section 4.1.7 of the ARB_bindless_texture spec:
5915
*
5916
* "Samplers can be used as l-values, so can be assigned into and used
5917
* as "out" and "inout" function parameters."
5918
*
5919
* From section 4.1.X of the ARB_bindless_texture spec:
5920
*
5921
* "Images can be used as l-values, so can be assigned into and used as
5922
* "out" and "inout" function parameters."
5923
*/
5924
if ((var->data.mode == ir_var_function_inout || var->data.mode == ir_var_function_out)
5925
&& (type->contains_atomic() ||
5926
(!state->has_bindless() && type->contains_opaque()))) {
5927
_mesa_glsl_error(&loc, state, "out and inout parameters cannot "
5928
"contain %s variables",
5929
state->has_bindless() ? "atomic" : "opaque");
5930
type = glsl_type::error_type;
5931
}
5932
5933
/* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
5934
*
5935
* "When calling a function, expressions that do not evaluate to
5936
* l-values cannot be passed to parameters declared as out or inout."
5937
*
5938
* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
5939
*
5940
* "Other binary or unary expressions, non-dereferenced arrays,
5941
* function names, swizzles with repeated fields, and constants
5942
* cannot be l-values."
5943
*
5944
* So for GLSL 1.10, passing an array as an out or inout parameter is not
5945
* allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
5946
*/
5947
if ((var->data.mode == ir_var_function_inout || var->data.mode == ir_var_function_out)
5948
&& type->is_array()
5949
&& !state->check_version(120, 100, &loc,
5950
"arrays cannot be out or inout parameters")) {
5951
type = glsl_type::error_type;
5952
}
5953
5954
instructions->push_tail(var);
5955
5956
/* Parameter declarations do not have r-values.
5957
*/
5958
return NULL;
5959
}
5960
5961
5962
void
5963
ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
5964
bool formal,
5965
exec_list *ir_parameters,
5966
_mesa_glsl_parse_state *state)
5967
{
5968
ast_parameter_declarator *void_param = NULL;
5969
unsigned count = 0;
5970
5971
foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
5972
param->formal_parameter = formal;
5973
param->hir(ir_parameters, state);
5974
5975
if (param->is_void)
5976
void_param = param;
5977
5978
count++;
5979
}
5980
5981
if ((void_param != NULL) && (count > 1)) {
5982
YYLTYPE loc = void_param->get_location();
5983
5984
_mesa_glsl_error(& loc, state,
5985
"`void' parameter must be only parameter");
5986
}
5987
}
5988
5989
5990
void
5991
emit_function(_mesa_glsl_parse_state *state, ir_function *f)
5992
{
5993
/* IR invariants disallow function declarations or definitions
5994
* nested within other function definitions. But there is no
5995
* requirement about the relative order of function declarations
5996
* and definitions with respect to one another. So simply insert
5997
* the new ir_function block at the end of the toplevel instruction
5998
* list.
5999
*/
6000
state->toplevel_ir->push_tail(f);
6001
}
6002
6003
6004
ir_rvalue *
6005
ast_function::hir(exec_list *instructions,
6006
struct _mesa_glsl_parse_state *state)
6007
{
6008
void *ctx = state;
6009
ir_function *f = NULL;
6010
ir_function_signature *sig = NULL;
6011
exec_list hir_parameters;
6012
YYLTYPE loc = this->get_location();
6013
6014
const char *const name = identifier;
6015
6016
/* New functions are always added to the top-level IR instruction stream,
6017
* so this instruction list pointer is ignored. See also emit_function
6018
* (called below).
6019
*/
6020
(void) instructions;
6021
6022
/* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
6023
*
6024
* "Function declarations (prototypes) cannot occur inside of functions;
6025
* they must be at global scope, or for the built-in functions, outside
6026
* the global scope."
6027
*
6028
* From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
6029
*
6030
* "User defined functions may only be defined within the global scope."
6031
*
6032
* Note that this language does not appear in GLSL 1.10.
6033
*/
6034
if ((state->current_function != NULL) &&
6035
state->is_version(120, 100)) {
6036
YYLTYPE loc = this->get_location();
6037
_mesa_glsl_error(&loc, state,
6038
"declaration of function `%s' not allowed within "
6039
"function body", name);
6040
}
6041
6042
validate_identifier(name, this->get_location(), state);
6043
6044
/* Convert the list of function parameters to HIR now so that they can be
6045
* used below to compare this function's signature with previously seen
6046
* signatures for functions with the same name.
6047
*/
6048
ast_parameter_declarator::parameters_to_hir(& this->parameters,
6049
is_definition,
6050
& hir_parameters, state);
6051
6052
const char *return_type_name;
6053
const glsl_type *return_type =
6054
this->return_type->glsl_type(& return_type_name, state);
6055
6056
if (!return_type) {
6057
YYLTYPE loc = this->get_location();
6058
_mesa_glsl_error(&loc, state,
6059
"function `%s' has undeclared return type `%s'",
6060
name, return_type_name);
6061
return_type = glsl_type::error_type;
6062
}
6063
6064
/* ARB_shader_subroutine states:
6065
* "Subroutine declarations cannot be prototyped. It is an error to prepend
6066
* subroutine(...) to a function declaration."
6067
*/
6068
if (this->return_type->qualifier.subroutine_list && !is_definition) {
6069
YYLTYPE loc = this->get_location();
6070
_mesa_glsl_error(&loc, state,
6071
"function declaration `%s' cannot have subroutine prepended",
6072
name);
6073
}
6074
6075
/* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
6076
* "No qualifier is allowed on the return type of a function."
6077
*/
6078
if (this->return_type->has_qualifiers(state)) {
6079
YYLTYPE loc = this->get_location();
6080
_mesa_glsl_error(& loc, state,
6081
"function `%s' return type has qualifiers", name);
6082
}
6083
6084
/* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
6085
*
6086
* "Arrays are allowed as arguments and as the return type. In both
6087
* cases, the array must be explicitly sized."
6088
*/
6089
if (return_type->is_unsized_array()) {
6090
YYLTYPE loc = this->get_location();
6091
_mesa_glsl_error(& loc, state,
6092
"function `%s' return type array must be explicitly "
6093
"sized", name);
6094
}
6095
6096
/* From Section 6.1 (Function Definitions) of the GLSL 1.00 spec:
6097
*
6098
* "Arrays are allowed as arguments, but not as the return type. [...]
6099
* The return type can also be a structure if the structure does not
6100
* contain an array."
6101
*/
6102
if (state->language_version == 100 && return_type->contains_array()) {
6103
YYLTYPE loc = this->get_location();
6104
_mesa_glsl_error(& loc, state,
6105
"function `%s' return type contains an array", name);
6106
}
6107
6108
/* From section 4.1.7 of the GLSL 4.40 spec:
6109
*
6110
* "[Opaque types] can only be declared as function parameters
6111
* or uniform-qualified variables."
6112
*
6113
* The ARB_bindless_texture spec doesn't clearly state this, but as it says
6114
* "Replace Section 4.1.7 (Samplers), p. 25" and, "Replace Section 4.1.X,
6115
* (Images)", this should be allowed.
6116
*/
6117
if (return_type->contains_atomic() ||
6118
(!state->has_bindless() && return_type->contains_opaque())) {
6119
YYLTYPE loc = this->get_location();
6120
_mesa_glsl_error(&loc, state,
6121
"function `%s' return type can't contain an %s type",
6122
name, state->has_bindless() ? "atomic" : "opaque");
6123
}
6124
6125
/**/
6126
if (return_type->is_subroutine()) {
6127
YYLTYPE loc = this->get_location();
6128
_mesa_glsl_error(&loc, state,
6129
"function `%s' return type can't be a subroutine type",
6130
name);
6131
}
6132
6133
/* Get the precision for the return type */
6134
unsigned return_precision;
6135
6136
if (state->es_shader) {
6137
YYLTYPE loc = this->get_location();
6138
return_precision =
6139
select_gles_precision(this->return_type->qualifier.precision,
6140
return_type,
6141
state,
6142
&loc);
6143
} else {
6144
return_precision = GLSL_PRECISION_NONE;
6145
}
6146
6147
/* Create an ir_function if one doesn't already exist. */
6148
f = state->symbols->get_function(name);
6149
if (f == NULL) {
6150
f = new(ctx) ir_function(name);
6151
if (!this->return_type->qualifier.is_subroutine_decl()) {
6152
if (!state->symbols->add_function(f)) {
6153
/* This function name shadows a non-function use of the same name. */
6154
YYLTYPE loc = this->get_location();
6155
_mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
6156
"non-function", name);
6157
return NULL;
6158
}
6159
}
6160
emit_function(state, f);
6161
}
6162
6163
/* From GLSL ES 3.0 spec, chapter 6.1 "Function Definitions", page 71:
6164
*
6165
* "A shader cannot redefine or overload built-in functions."
6166
*
6167
* While in GLSL ES 1.0 specification, chapter 8 "Built-in Functions":
6168
*
6169
* "User code can overload the built-in functions but cannot redefine
6170
* them."
6171
*/
6172
if (state->es_shader) {
6173
/* Local shader has no exact candidates; check the built-ins. */
6174
if (state->language_version >= 300 &&
6175
_mesa_glsl_has_builtin_function(state, name)) {
6176
YYLTYPE loc = this->get_location();
6177
_mesa_glsl_error(& loc, state,
6178
"A shader cannot redefine or overload built-in "
6179
"function `%s' in GLSL ES 3.00", name);
6180
return NULL;
6181
}
6182
6183
if (state->language_version == 100) {
6184
ir_function_signature *sig =
6185
_mesa_glsl_find_builtin_function(state, name, &hir_parameters);
6186
if (sig && sig->is_builtin()) {
6187
_mesa_glsl_error(& loc, state,
6188
"A shader cannot redefine built-in "
6189
"function `%s' in GLSL ES 1.00", name);
6190
}
6191
}
6192
}
6193
6194
/* Verify that this function's signature either doesn't match a previously
6195
* seen signature for a function with the same name, or, if a match is found,
6196
* that the previously seen signature does not have an associated definition.
6197
*/
6198
if (state->es_shader || f->has_user_signature()) {
6199
sig = f->exact_matching_signature(state, &hir_parameters);
6200
if (sig != NULL) {
6201
const char *badvar = sig->qualifiers_match(&hir_parameters);
6202
if (badvar != NULL) {
6203
YYLTYPE loc = this->get_location();
6204
6205
_mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
6206
"qualifiers don't match prototype", name, badvar);
6207
}
6208
6209
if (sig->return_type != return_type) {
6210
YYLTYPE loc = this->get_location();
6211
6212
_mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
6213
"match prototype", name);
6214
}
6215
6216
if (sig->return_precision != return_precision) {
6217
YYLTYPE loc = this->get_location();
6218
6219
_mesa_glsl_error(&loc, state, "function `%s' return type precision "
6220
"doesn't match prototype", name);
6221
}
6222
6223
if (sig->is_defined) {
6224
if (is_definition) {
6225
YYLTYPE loc = this->get_location();
6226
_mesa_glsl_error(& loc, state, "function `%s' redefined", name);
6227
} else {
6228
/* We just encountered a prototype that exactly matches a
6229
* function that's already been defined. This is redundant,
6230
* and we should ignore it.
6231
*/
6232
return NULL;
6233
}
6234
} else if (state->language_version == 100 && !is_definition) {
6235
/* From the GLSL 1.00 spec, section 4.2.7:
6236
*
6237
* "A particular variable, structure or function declaration
6238
* may occur at most once within a scope with the exception
6239
* that a single function prototype plus the corresponding
6240
* function definition are allowed."
6241
*/
6242
YYLTYPE loc = this->get_location();
6243
_mesa_glsl_error(&loc, state, "function `%s' redeclared", name);
6244
}
6245
}
6246
}
6247
6248
/* Verify the return type of main() */
6249
if (strcmp(name, "main") == 0) {
6250
if (! return_type->is_void()) {
6251
YYLTYPE loc = this->get_location();
6252
6253
_mesa_glsl_error(& loc, state, "main() must return void");
6254
}
6255
6256
if (!hir_parameters.is_empty()) {
6257
YYLTYPE loc = this->get_location();
6258
6259
_mesa_glsl_error(& loc, state, "main() must not take any parameters");
6260
}
6261
}
6262
6263
/* Finish storing the information about this new function in its signature.
6264
*/
6265
if (sig == NULL) {
6266
sig = new(ctx) ir_function_signature(return_type);
6267
sig->return_precision = return_precision;
6268
f->add_signature(sig);
6269
}
6270
6271
sig->replace_parameters(&hir_parameters);
6272
signature = sig;
6273
6274
if (this->return_type->qualifier.subroutine_list) {
6275
int idx;
6276
6277
if (this->return_type->qualifier.flags.q.explicit_index) {
6278
unsigned qual_index;
6279
if (process_qualifier_constant(state, &loc, "index",
6280
this->return_type->qualifier.index,
6281
&qual_index)) {
6282
if (!state->has_explicit_uniform_location()) {
6283
_mesa_glsl_error(&loc, state, "subroutine index requires "
6284
"GL_ARB_explicit_uniform_location or "
6285
"GLSL 4.30");
6286
} else if (qual_index >= MAX_SUBROUTINES) {
6287
_mesa_glsl_error(&loc, state,
6288
"invalid subroutine index (%d) index must "
6289
"be a number between 0 and "
6290
"GL_MAX_SUBROUTINES - 1 (%d)", qual_index,
6291
MAX_SUBROUTINES - 1);
6292
} else {
6293
f->subroutine_index = qual_index;
6294
}
6295
}
6296
}
6297
6298
f->num_subroutine_types = this->return_type->qualifier.subroutine_list->declarations.length();
6299
f->subroutine_types = ralloc_array(state, const struct glsl_type *,
6300
f->num_subroutine_types);
6301
idx = 0;
6302
foreach_list_typed(ast_declaration, decl, link, &this->return_type->qualifier.subroutine_list->declarations) {
6303
const struct glsl_type *type;
6304
/* the subroutine type must be already declared */
6305
type = state->symbols->get_type(decl->identifier);
6306
if (!type) {
6307
_mesa_glsl_error(& loc, state, "unknown type '%s' in subroutine function definition", decl->identifier);
6308
}
6309
6310
for (int i = 0; i < state->num_subroutine_types; i++) {
6311
ir_function *fn = state->subroutine_types[i];
6312
ir_function_signature *tsig = NULL;
6313
6314
if (strcmp(fn->name, decl->identifier))
6315
continue;
6316
6317
tsig = fn->matching_signature(state, &sig->parameters,
6318
false);
6319
if (!tsig) {
6320
_mesa_glsl_error(& loc, state, "subroutine type mismatch '%s' - signatures do not match\n", decl->identifier);
6321
} else {
6322
if (tsig->return_type != sig->return_type) {
6323
_mesa_glsl_error(& loc, state, "subroutine type mismatch '%s' - return types do not match\n", decl->identifier);
6324
}
6325
}
6326
}
6327
f->subroutine_types[idx++] = type;
6328
}
6329
state->subroutines = (ir_function **)reralloc(state, state->subroutines,
6330
ir_function *,
6331
state->num_subroutines + 1);
6332
state->subroutines[state->num_subroutines] = f;
6333
state->num_subroutines++;
6334
6335
}
6336
6337
if (this->return_type->qualifier.is_subroutine_decl()) {
6338
if (!state->symbols->add_type(this->identifier, glsl_type::get_subroutine_instance(this->identifier))) {
6339
_mesa_glsl_error(& loc, state, "type '%s' previously defined", this->identifier);
6340
return NULL;
6341
}
6342
state->subroutine_types = (ir_function **)reralloc(state, state->subroutine_types,
6343
ir_function *,
6344
state->num_subroutine_types + 1);
6345
state->subroutine_types[state->num_subroutine_types] = f;
6346
state->num_subroutine_types++;
6347
6348
f->is_subroutine = true;
6349
}
6350
6351
/* Function declarations (prototypes) do not have r-values.
6352
*/
6353
return NULL;
6354
}
6355
6356
6357
ir_rvalue *
6358
ast_function_definition::hir(exec_list *instructions,
6359
struct _mesa_glsl_parse_state *state)
6360
{
6361
prototype->is_definition = true;
6362
prototype->hir(instructions, state);
6363
6364
ir_function_signature *signature = prototype->signature;
6365
if (signature == NULL)
6366
return NULL;
6367
6368
assert(state->current_function == NULL);
6369
state->current_function = signature;
6370
state->found_return = false;
6371
state->found_begin_interlock = false;
6372
state->found_end_interlock = false;
6373
6374
/* Duplicate parameters declared in the prototype as concrete variables.
6375
* Add these to the symbol table.
6376
*/
6377
state->symbols->push_scope();
6378
foreach_in_list(ir_variable, var, &signature->parameters) {
6379
assert(var->as_variable() != NULL);
6380
6381
/* The only way a parameter would "exist" is if two parameters have
6382
* the same name.
6383
*/
6384
if (state->symbols->name_declared_this_scope(var->name)) {
6385
YYLTYPE loc = this->get_location();
6386
6387
_mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
6388
} else {
6389
state->symbols->add_variable(var);
6390
}
6391
}
6392
6393
/* Convert the body of the function to HIR. */
6394
this->body->hir(&signature->body, state);
6395
signature->is_defined = true;
6396
6397
state->symbols->pop_scope();
6398
6399
assert(state->current_function == signature);
6400
state->current_function = NULL;
6401
6402
if (!signature->return_type->is_void() && !state->found_return) {
6403
YYLTYPE loc = this->get_location();
6404
_mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
6405
"%s, but no return statement",
6406
signature->function_name(),
6407
signature->return_type->name);
6408
}
6409
6410
/* Function definitions do not have r-values.
6411
*/
6412
return NULL;
6413
}
6414
6415
6416
ir_rvalue *
6417
ast_jump_statement::hir(exec_list *instructions,
6418
struct _mesa_glsl_parse_state *state)
6419
{
6420
void *ctx = state;
6421
6422
switch (mode) {
6423
case ast_return: {
6424
ir_return *inst;
6425
assert(state->current_function);
6426
6427
if (opt_return_value) {
6428
ir_rvalue *ret = opt_return_value->hir(instructions, state);
6429
6430
/* The value of the return type can be NULL if the shader says
6431
* 'return foo();' and foo() is a function that returns void.
6432
*
6433
* NOTE: The GLSL spec doesn't say that this is an error. The type
6434
* of the return value is void. If the return type of the function is
6435
* also void, then this should compile without error. Seriously.
6436
*/
6437
const glsl_type *const ret_type =
6438
(ret == NULL) ? glsl_type::void_type : ret->type;
6439
6440
/* Implicit conversions are not allowed for return values prior to
6441
* ARB_shading_language_420pack.
6442
*/
6443
if (state->current_function->return_type != ret_type) {
6444
YYLTYPE loc = this->get_location();
6445
6446
if (state->has_420pack()) {
6447
if (!apply_implicit_conversion(state->current_function->return_type,
6448
ret, state)
6449
|| (ret->type != state->current_function->return_type)) {
6450
_mesa_glsl_error(& loc, state,
6451
"could not implicitly convert return value "
6452
"to %s, in function `%s'",
6453
state->current_function->return_type->name,
6454
state->current_function->function_name());
6455
}
6456
} else {
6457
_mesa_glsl_error(& loc, state,
6458
"`return' with wrong type %s, in function `%s' "
6459
"returning %s",
6460
ret_type->name,
6461
state->current_function->function_name(),
6462
state->current_function->return_type->name);
6463
}
6464
} else if (state->current_function->return_type->base_type ==
6465
GLSL_TYPE_VOID) {
6466
YYLTYPE loc = this->get_location();
6467
6468
/* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
6469
* specs add a clarification:
6470
*
6471
* "A void function can only use return without a return argument, even if
6472
* the return argument has void type. Return statements only accept values:
6473
*
6474
* void func1() { }
6475
* void func2() { return func1(); } // illegal return statement"
6476
*/
6477
_mesa_glsl_error(& loc, state,
6478
"void functions can only use `return' without a "
6479
"return argument");
6480
}
6481
6482
inst = new(ctx) ir_return(ret);
6483
} else {
6484
if (state->current_function->return_type->base_type !=
6485
GLSL_TYPE_VOID) {
6486
YYLTYPE loc = this->get_location();
6487
6488
_mesa_glsl_error(& loc, state,
6489
"`return' with no value, in function %s returning "
6490
"non-void",
6491
state->current_function->function_name());
6492
}
6493
inst = new(ctx) ir_return;
6494
}
6495
6496
state->found_return = true;
6497
instructions->push_tail(inst);
6498
break;
6499
}
6500
6501
case ast_discard:
6502
if (state->stage != MESA_SHADER_FRAGMENT) {
6503
YYLTYPE loc = this->get_location();
6504
6505
_mesa_glsl_error(& loc, state,
6506
"`discard' may only appear in a fragment shader");
6507
}
6508
instructions->push_tail(new(ctx) ir_discard);
6509
break;
6510
6511
case ast_break:
6512
case ast_continue:
6513
if (mode == ast_continue &&
6514
state->loop_nesting_ast == NULL) {
6515
YYLTYPE loc = this->get_location();
6516
6517
_mesa_glsl_error(& loc, state, "continue may only appear in a loop");
6518
} else if (mode == ast_break &&
6519
state->loop_nesting_ast == NULL &&
6520
state->switch_state.switch_nesting_ast == NULL) {
6521
YYLTYPE loc = this->get_location();
6522
6523
_mesa_glsl_error(& loc, state,
6524
"break may only appear in a loop or a switch");
6525
} else {
6526
/* For a loop, inline the for loop expression again, since we don't
6527
* know where near the end of the loop body the normal copy of it is
6528
* going to be placed. Same goes for the condition for a do-while
6529
* loop.
6530
*/
6531
if (state->loop_nesting_ast != NULL &&
6532
mode == ast_continue && !state->switch_state.is_switch_innermost) {
6533
if (state->loop_nesting_ast->rest_expression) {
6534
state->loop_nesting_ast->rest_expression->hir(instructions,
6535
state);
6536
}
6537
if (state->loop_nesting_ast->mode ==
6538
ast_iteration_statement::ast_do_while) {
6539
state->loop_nesting_ast->condition_to_hir(instructions, state);
6540
}
6541
}
6542
6543
if (state->switch_state.is_switch_innermost &&
6544
mode == ast_continue) {
6545
/* Set 'continue_inside' to true. */
6546
ir_rvalue *const true_val = new (ctx) ir_constant(true);
6547
ir_dereference_variable *deref_continue_inside_var =
6548
new(ctx) ir_dereference_variable(state->switch_state.continue_inside);
6549
instructions->push_tail(new(ctx) ir_assignment(deref_continue_inside_var,
6550
true_val));
6551
6552
/* Break out from the switch, continue for the loop will
6553
* be called right after switch. */
6554
ir_loop_jump *const jump =
6555
new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
6556
instructions->push_tail(jump);
6557
6558
} else if (state->switch_state.is_switch_innermost &&
6559
mode == ast_break) {
6560
/* Force break out of switch by inserting a break. */
6561
ir_loop_jump *const jump =
6562
new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
6563
instructions->push_tail(jump);
6564
} else {
6565
ir_loop_jump *const jump =
6566
new(ctx) ir_loop_jump((mode == ast_break)
6567
? ir_loop_jump::jump_break
6568
: ir_loop_jump::jump_continue);
6569
instructions->push_tail(jump);
6570
}
6571
}
6572
6573
break;
6574
}
6575
6576
/* Jump instructions do not have r-values.
6577
*/
6578
return NULL;
6579
}
6580
6581
6582
ir_rvalue *
6583
ast_demote_statement::hir(exec_list *instructions,
6584
struct _mesa_glsl_parse_state *state)
6585
{
6586
void *ctx = state;
6587
6588
if (state->stage != MESA_SHADER_FRAGMENT) {
6589
YYLTYPE loc = this->get_location();
6590
6591
_mesa_glsl_error(& loc, state,
6592
"`demote' may only appear in a fragment shader");
6593
}
6594
6595
instructions->push_tail(new(ctx) ir_demote);
6596
6597
return NULL;
6598
}
6599
6600
6601
ir_rvalue *
6602
ast_selection_statement::hir(exec_list *instructions,
6603
struct _mesa_glsl_parse_state *state)
6604
{
6605
void *ctx = state;
6606
6607
ir_rvalue *const condition = this->condition->hir(instructions, state);
6608
6609
/* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
6610
*
6611
* "Any expression whose type evaluates to a Boolean can be used as the
6612
* conditional expression bool-expression. Vector types are not accepted
6613
* as the expression to if."
6614
*
6615
* The checks are separated so that higher quality diagnostics can be
6616
* generated for cases where both rules are violated.
6617
*/
6618
if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
6619
YYLTYPE loc = this->condition->get_location();
6620
6621
_mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
6622
"boolean");
6623
}
6624
6625
ir_if *const stmt = new(ctx) ir_if(condition);
6626
6627
if (then_statement != NULL) {
6628
state->symbols->push_scope();
6629
then_statement->hir(& stmt->then_instructions, state);
6630
state->symbols->pop_scope();
6631
}
6632
6633
if (else_statement != NULL) {
6634
state->symbols->push_scope();
6635
else_statement->hir(& stmt->else_instructions, state);
6636
state->symbols->pop_scope();
6637
}
6638
6639
instructions->push_tail(stmt);
6640
6641
/* if-statements do not have r-values.
6642
*/
6643
return NULL;
6644
}
6645
6646
6647
struct case_label {
6648
/** Value of the case label. */
6649
unsigned value;
6650
6651
/** Does this label occur after the default? */
6652
bool after_default;
6653
6654
/**
6655
* AST for the case label.
6656
*
6657
* This is only used to generate error messages for duplicate labels.
6658
*/
6659
ast_expression *ast;
6660
};
6661
6662
/* Used for detection of duplicate case values, compare
6663
* given contents directly.
6664
*/
6665
static bool
6666
compare_case_value(const void *a, const void *b)
6667
{
6668
return ((struct case_label *) a)->value == ((struct case_label *) b)->value;
6669
}
6670
6671
6672
/* Used for detection of duplicate case values, just
6673
* returns key contents as is.
6674
*/
6675
static unsigned
6676
key_contents(const void *key)
6677
{
6678
return ((struct case_label *) key)->value;
6679
}
6680
6681
void
6682
ast_switch_statement::eval_test_expression(exec_list *instructions,
6683
struct _mesa_glsl_parse_state *state)
6684
{
6685
if (test_val == NULL)
6686
test_val = this->test_expression->hir(instructions, state);
6687
}
6688
6689
ir_rvalue *
6690
ast_switch_statement::hir(exec_list *instructions,
6691
struct _mesa_glsl_parse_state *state)
6692
{
6693
void *ctx = state;
6694
6695
this->eval_test_expression(instructions, state);
6696
6697
/* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
6698
*
6699
* "The type of init-expression in a switch statement must be a
6700
* scalar integer."
6701
*/
6702
if (!test_val->type->is_scalar() ||
6703
!test_val->type->is_integer_32()) {
6704
YYLTYPE loc = this->test_expression->get_location();
6705
6706
_mesa_glsl_error(& loc,
6707
state,
6708
"switch-statement expression must be scalar "
6709
"integer");
6710
return NULL;
6711
}
6712
6713
/* Track the switch-statement nesting in a stack-like manner.
6714
*/
6715
struct glsl_switch_state saved = state->switch_state;
6716
6717
state->switch_state.is_switch_innermost = true;
6718
state->switch_state.switch_nesting_ast = this;
6719
state->switch_state.labels_ht =
6720
_mesa_hash_table_create(NULL, key_contents,
6721
compare_case_value);
6722
state->switch_state.previous_default = NULL;
6723
6724
/* Initalize is_fallthru state to false.
6725
*/
6726
ir_rvalue *const is_fallthru_val = new (ctx) ir_constant(false);
6727
state->switch_state.is_fallthru_var =
6728
new(ctx) ir_variable(glsl_type::bool_type,
6729
"switch_is_fallthru_tmp",
6730
ir_var_temporary);
6731
instructions->push_tail(state->switch_state.is_fallthru_var);
6732
6733
ir_dereference_variable *deref_is_fallthru_var =
6734
new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
6735
instructions->push_tail(new(ctx) ir_assignment(deref_is_fallthru_var,
6736
is_fallthru_val));
6737
6738
/* Initialize continue_inside state to false.
6739
*/
6740
state->switch_state.continue_inside =
6741
new(ctx) ir_variable(glsl_type::bool_type,
6742
"continue_inside_tmp",
6743
ir_var_temporary);
6744
instructions->push_tail(state->switch_state.continue_inside);
6745
6746
ir_rvalue *const false_val = new (ctx) ir_constant(false);
6747
ir_dereference_variable *deref_continue_inside_var =
6748
new(ctx) ir_dereference_variable(state->switch_state.continue_inside);
6749
instructions->push_tail(new(ctx) ir_assignment(deref_continue_inside_var,
6750
false_val));
6751
6752
state->switch_state.run_default =
6753
new(ctx) ir_variable(glsl_type::bool_type,
6754
"run_default_tmp",
6755
ir_var_temporary);
6756
instructions->push_tail(state->switch_state.run_default);
6757
6758
/* Loop around the switch is used for flow control. */
6759
ir_loop * loop = new(ctx) ir_loop();
6760
instructions->push_tail(loop);
6761
6762
/* Cache test expression.
6763
*/
6764
test_to_hir(&loop->body_instructions, state);
6765
6766
/* Emit code for body of switch stmt.
6767
*/
6768
body->hir(&loop->body_instructions, state);
6769
6770
/* Insert a break at the end to exit loop. */
6771
ir_loop_jump *jump = new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
6772
loop->body_instructions.push_tail(jump);
6773
6774
/* If we are inside loop, check if continue got called inside switch. */
6775
if (state->loop_nesting_ast != NULL) {
6776
ir_dereference_variable *deref_continue_inside =
6777
new(ctx) ir_dereference_variable(state->switch_state.continue_inside);
6778
ir_if *irif = new(ctx) ir_if(deref_continue_inside);
6779
ir_loop_jump *jump = new(ctx) ir_loop_jump(ir_loop_jump::jump_continue);
6780
6781
if (state->loop_nesting_ast != NULL) {
6782
if (state->loop_nesting_ast->rest_expression) {
6783
state->loop_nesting_ast->rest_expression->hir(&irif->then_instructions,
6784
state);
6785
}
6786
if (state->loop_nesting_ast->mode ==
6787
ast_iteration_statement::ast_do_while) {
6788
state->loop_nesting_ast->condition_to_hir(&irif->then_instructions, state);
6789
}
6790
}
6791
irif->then_instructions.push_tail(jump);
6792
instructions->push_tail(irif);
6793
}
6794
6795
_mesa_hash_table_destroy(state->switch_state.labels_ht, NULL);
6796
6797
state->switch_state = saved;
6798
6799
/* Switch statements do not have r-values. */
6800
return NULL;
6801
}
6802
6803
6804
void
6805
ast_switch_statement::test_to_hir(exec_list *instructions,
6806
struct _mesa_glsl_parse_state *state)
6807
{
6808
void *ctx = state;
6809
6810
/* set to true to avoid a duplicate "use of uninitialized variable" warning
6811
* on the switch test case. The first one would be already raised when
6812
* getting the test_expression at ast_switch_statement::hir
6813
*/
6814
test_expression->set_is_lhs(true);
6815
/* Cache value of test expression. */
6816
this->eval_test_expression(instructions, state);
6817
6818
state->switch_state.test_var = new(ctx) ir_variable(test_val->type,
6819
"switch_test_tmp",
6820
ir_var_temporary);
6821
ir_dereference_variable *deref_test_var =
6822
new(ctx) ir_dereference_variable(state->switch_state.test_var);
6823
6824
instructions->push_tail(state->switch_state.test_var);
6825
instructions->push_tail(new(ctx) ir_assignment(deref_test_var, test_val));
6826
}
6827
6828
6829
ir_rvalue *
6830
ast_switch_body::hir(exec_list *instructions,
6831
struct _mesa_glsl_parse_state *state)
6832
{
6833
if (stmts != NULL)
6834
stmts->hir(instructions, state);
6835
6836
/* Switch bodies do not have r-values. */
6837
return NULL;
6838
}
6839
6840
ir_rvalue *
6841
ast_case_statement_list::hir(exec_list *instructions,
6842
struct _mesa_glsl_parse_state *state)
6843
{
6844
exec_list default_case, after_default, tmp;
6845
6846
foreach_list_typed (ast_case_statement, case_stmt, link, & this->cases) {
6847
case_stmt->hir(&tmp, state);
6848
6849
/* Default case. */
6850
if (state->switch_state.previous_default && default_case.is_empty()) {
6851
default_case.append_list(&tmp);
6852
continue;
6853
}
6854
6855
/* If default case found, append 'after_default' list. */
6856
if (!default_case.is_empty())
6857
after_default.append_list(&tmp);
6858
else
6859
instructions->append_list(&tmp);
6860
}
6861
6862
/* Handle the default case. This is done here because default might not be
6863
* the last case. We need to add checks against following cases first to see
6864
* if default should be chosen or not.
6865
*/
6866
if (!default_case.is_empty()) {
6867
ir_factory body(instructions, state);
6868
6869
ir_expression *cmp = NULL;
6870
6871
hash_table_foreach(state->switch_state.labels_ht, entry) {
6872
const struct case_label *const l = (struct case_label *) entry->data;
6873
6874
/* If the switch init-value is the value of one of the labels that
6875
* occurs after the default case, disable execution of the default
6876
* case.
6877
*/
6878
if (l->after_default) {
6879
ir_constant *const cnst =
6880
state->switch_state.test_var->type->base_type == GLSL_TYPE_UINT
6881
? body.constant(unsigned(l->value))
6882
: body.constant(int(l->value));
6883
6884
cmp = cmp == NULL
6885
? equal(cnst, state->switch_state.test_var)
6886
: logic_or(cmp, equal(cnst, state->switch_state.test_var));
6887
}
6888
}
6889
6890
if (cmp != NULL)
6891
body.emit(assign(state->switch_state.run_default, logic_not(cmp)));
6892
else
6893
body.emit(assign(state->switch_state.run_default, body.constant(true)));
6894
6895
/* Append default case and all cases after it. */
6896
instructions->append_list(&default_case);
6897
instructions->append_list(&after_default);
6898
}
6899
6900
/* Case statements do not have r-values. */
6901
return NULL;
6902
}
6903
6904
ir_rvalue *
6905
ast_case_statement::hir(exec_list *instructions,
6906
struct _mesa_glsl_parse_state *state)
6907
{
6908
labels->hir(instructions, state);
6909
6910
/* Guard case statements depending on fallthru state. */
6911
ir_dereference_variable *const deref_fallthru_guard =
6912
new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
6913
ir_if *const test_fallthru = new(state) ir_if(deref_fallthru_guard);
6914
6915
foreach_list_typed (ast_node, stmt, link, & this->stmts)
6916
stmt->hir(& test_fallthru->then_instructions, state);
6917
6918
instructions->push_tail(test_fallthru);
6919
6920
/* Case statements do not have r-values. */
6921
return NULL;
6922
}
6923
6924
6925
ir_rvalue *
6926
ast_case_label_list::hir(exec_list *instructions,
6927
struct _mesa_glsl_parse_state *state)
6928
{
6929
foreach_list_typed (ast_case_label, label, link, & this->labels)
6930
label->hir(instructions, state);
6931
6932
/* Case labels do not have r-values. */
6933
return NULL;
6934
}
6935
6936
ir_rvalue *
6937
ast_case_label::hir(exec_list *instructions,
6938
struct _mesa_glsl_parse_state *state)
6939
{
6940
ir_factory body(instructions, state);
6941
6942
ir_variable *const fallthru_var = state->switch_state.is_fallthru_var;
6943
6944
/* If not default case, ... */
6945
if (this->test_value != NULL) {
6946
/* Conditionally set fallthru state based on
6947
* comparison of cached test expression value to case label.
6948
*/
6949
ir_rvalue *const label_rval = this->test_value->hir(instructions, state);
6950
ir_constant *label_const =
6951
label_rval->constant_expression_value(body.mem_ctx);
6952
6953
if (!label_const) {
6954
YYLTYPE loc = this->test_value->get_location();
6955
6956
_mesa_glsl_error(& loc, state,
6957
"switch statement case label must be a "
6958
"constant expression");
6959
6960
/* Stuff a dummy value in to allow processing to continue. */
6961
label_const = body.constant(0);
6962
} else {
6963
hash_entry *entry =
6964
_mesa_hash_table_search(state->switch_state.labels_ht,
6965
&label_const->value.u[0]);
6966
6967
if (entry) {
6968
const struct case_label *const l =
6969
(struct case_label *) entry->data;
6970
const ast_expression *const previous_label = l->ast;
6971
YYLTYPE loc = this->test_value->get_location();
6972
6973
_mesa_glsl_error(& loc, state, "duplicate case value");
6974
6975
loc = previous_label->get_location();
6976
_mesa_glsl_error(& loc, state, "this is the previous case label");
6977
} else {
6978
struct case_label *l = ralloc(state->switch_state.labels_ht,
6979
struct case_label);
6980
6981
l->value = label_const->value.u[0];
6982
l->after_default = state->switch_state.previous_default != NULL;
6983
l->ast = this->test_value;
6984
6985
_mesa_hash_table_insert(state->switch_state.labels_ht,
6986
&label_const->value.u[0],
6987
l);
6988
}
6989
}
6990
6991
/* Create an r-value version of the ir_constant label here (after we may
6992
* have created a fake one in error cases) that can be passed to
6993
* apply_implicit_conversion below.
6994
*/
6995
ir_rvalue *label = label_const;
6996
6997
ir_rvalue *deref_test_var =
6998
new(body.mem_ctx) ir_dereference_variable(state->switch_state.test_var);
6999
7000
/*
7001
* From GLSL 4.40 specification section 6.2 ("Selection"):
7002
*
7003
* "The type of the init-expression value in a switch statement must
7004
* be a scalar int or uint. The type of the constant-expression value
7005
* in a case label also must be a scalar int or uint. When any pair
7006
* of these values is tested for "equal value" and the types do not
7007
* match, an implicit conversion will be done to convert the int to a
7008
* uint (see section 4.1.10 “Implicit Conversions”) before the compare
7009
* is done."
7010
*/
7011
if (label->type != state->switch_state.test_var->type) {
7012
YYLTYPE loc = this->test_value->get_location();
7013
7014
const glsl_type *type_a = label->type;
7015
const glsl_type *type_b = state->switch_state.test_var->type;
7016
7017
/* Check if int->uint implicit conversion is supported. */
7018
bool integer_conversion_supported =
7019
glsl_type::int_type->can_implicitly_convert_to(glsl_type::uint_type,
7020
state);
7021
7022
if ((!type_a->is_integer_32() || !type_b->is_integer_32()) ||
7023
!integer_conversion_supported) {
7024
_mesa_glsl_error(&loc, state, "type mismatch with switch "
7025
"init-expression and case label (%s != %s)",
7026
type_a->name, type_b->name);
7027
} else {
7028
/* Conversion of the case label. */
7029
if (type_a->base_type == GLSL_TYPE_INT) {
7030
if (!apply_implicit_conversion(glsl_type::uint_type,
7031
label, state))
7032
_mesa_glsl_error(&loc, state, "implicit type conversion error");
7033
} else {
7034
/* Conversion of the init-expression value. */
7035
if (!apply_implicit_conversion(glsl_type::uint_type,
7036
deref_test_var, state))
7037
_mesa_glsl_error(&loc, state, "implicit type conversion error");
7038
}
7039
}
7040
7041
/* If the implicit conversion was allowed, the types will already be
7042
* the same. If the implicit conversion wasn't allowed, smash the
7043
* type of the label anyway. This will prevent the expression
7044
* constructor (below) from failing an assertion.
7045
*/
7046
label->type = deref_test_var->type;
7047
}
7048
7049
body.emit(assign(fallthru_var,
7050
logic_or(fallthru_var, equal(label, deref_test_var))));
7051
} else { /* default case */
7052
if (state->switch_state.previous_default) {
7053
YYLTYPE loc = this->get_location();
7054
_mesa_glsl_error(& loc, state,
7055
"multiple default labels in one switch");
7056
7057
loc = state->switch_state.previous_default->get_location();
7058
_mesa_glsl_error(& loc, state, "this is the first default label");
7059
}
7060
state->switch_state.previous_default = this;
7061
7062
/* Set fallthru condition on 'run_default' bool. */
7063
body.emit(assign(fallthru_var,
7064
logic_or(fallthru_var,
7065
state->switch_state.run_default)));
7066
}
7067
7068
/* Case statements do not have r-values. */
7069
return NULL;
7070
}
7071
7072
void
7073
ast_iteration_statement::condition_to_hir(exec_list *instructions,
7074
struct _mesa_glsl_parse_state *state)
7075
{
7076
void *ctx = state;
7077
7078
if (condition != NULL) {
7079
ir_rvalue *const cond =
7080
condition->hir(instructions, state);
7081
7082
if ((cond == NULL)
7083
|| !cond->type->is_boolean() || !cond->type->is_scalar()) {
7084
YYLTYPE loc = condition->get_location();
7085
7086
_mesa_glsl_error(& loc, state,
7087
"loop condition must be scalar boolean");
7088
} else {
7089
/* As the first code in the loop body, generate a block that looks
7090
* like 'if (!condition) break;' as the loop termination condition.
7091
*/
7092
ir_rvalue *const not_cond =
7093
new(ctx) ir_expression(ir_unop_logic_not, cond);
7094
7095
ir_if *const if_stmt = new(ctx) ir_if(not_cond);
7096
7097
ir_jump *const break_stmt =
7098
new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
7099
7100
if_stmt->then_instructions.push_tail(break_stmt);
7101
instructions->push_tail(if_stmt);
7102
}
7103
}
7104
}
7105
7106
7107
ir_rvalue *
7108
ast_iteration_statement::hir(exec_list *instructions,
7109
struct _mesa_glsl_parse_state *state)
7110
{
7111
void *ctx = state;
7112
7113
/* For-loops and while-loops start a new scope, but do-while loops do not.
7114
*/
7115
if (mode != ast_do_while)
7116
state->symbols->push_scope();
7117
7118
if (init_statement != NULL)
7119
init_statement->hir(instructions, state);
7120
7121
ir_loop *const stmt = new(ctx) ir_loop();
7122
instructions->push_tail(stmt);
7123
7124
/* Track the current loop nesting. */
7125
ast_iteration_statement *nesting_ast = state->loop_nesting_ast;
7126
7127
state->loop_nesting_ast = this;
7128
7129
/* Likewise, indicate that following code is closest to a loop,
7130
* NOT closest to a switch.
7131
*/
7132
bool saved_is_switch_innermost = state->switch_state.is_switch_innermost;
7133
state->switch_state.is_switch_innermost = false;
7134
7135
if (mode != ast_do_while)
7136
condition_to_hir(&stmt->body_instructions, state);
7137
7138
if (body != NULL)
7139
body->hir(& stmt->body_instructions, state);
7140
7141
if (rest_expression != NULL)
7142
rest_expression->hir(& stmt->body_instructions, state);
7143
7144
if (mode == ast_do_while)
7145
condition_to_hir(&stmt->body_instructions, state);
7146
7147
if (mode != ast_do_while)
7148
state->symbols->pop_scope();
7149
7150
/* Restore previous nesting before returning. */
7151
state->loop_nesting_ast = nesting_ast;
7152
state->switch_state.is_switch_innermost = saved_is_switch_innermost;
7153
7154
/* Loops do not have r-values.
7155
*/
7156
return NULL;
7157
}
7158
7159
7160
/**
7161
* Determine if the given type is valid for establishing a default precision
7162
* qualifier.
7163
*
7164
* From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
7165
*
7166
* "The precision statement
7167
*
7168
* precision precision-qualifier type;
7169
*
7170
* can be used to establish a default precision qualifier. The type field
7171
* can be either int or float or any of the sampler types, and the
7172
* precision-qualifier can be lowp, mediump, or highp."
7173
*
7174
* GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
7175
* qualifiers on sampler types, but this seems like an oversight (since the
7176
* intention of including these in GLSL 1.30 is to allow compatibility with ES
7177
* shaders). So we allow int, float, and all sampler types regardless of GLSL
7178
* version.
7179
*/
7180
static bool
7181
is_valid_default_precision_type(const struct glsl_type *const type)
7182
{
7183
if (type == NULL)
7184
return false;
7185
7186
switch (type->base_type) {
7187
case GLSL_TYPE_INT:
7188
case GLSL_TYPE_FLOAT:
7189
/* "int" and "float" are valid, but vectors and matrices are not. */
7190
return type->vector_elements == 1 && type->matrix_columns == 1;
7191
case GLSL_TYPE_SAMPLER:
7192
case GLSL_TYPE_IMAGE:
7193
case GLSL_TYPE_ATOMIC_UINT:
7194
return true;
7195
default:
7196
return false;
7197
}
7198
}
7199
7200
7201
ir_rvalue *
7202
ast_type_specifier::hir(exec_list *instructions,
7203
struct _mesa_glsl_parse_state *state)
7204
{
7205
if (this->default_precision == ast_precision_none && this->structure == NULL)
7206
return NULL;
7207
7208
YYLTYPE loc = this->get_location();
7209
7210
/* If this is a precision statement, check that the type to which it is
7211
* applied is either float or int.
7212
*
7213
* From section 4.5.3 of the GLSL 1.30 spec:
7214
* "The precision statement
7215
* precision precision-qualifier type;
7216
* can be used to establish a default precision qualifier. The type
7217
* field can be either int or float [...]. Any other types or
7218
* qualifiers will result in an error.
7219
*/
7220
if (this->default_precision != ast_precision_none) {
7221
if (!state->check_precision_qualifiers_allowed(&loc))
7222
return NULL;
7223
7224
if (this->structure != NULL) {
7225
_mesa_glsl_error(&loc, state,
7226
"precision qualifiers do not apply to structures");
7227
return NULL;
7228
}
7229
7230
if (this->array_specifier != NULL) {
7231
_mesa_glsl_error(&loc, state,
7232
"default precision statements do not apply to "
7233
"arrays");
7234
return NULL;
7235
}
7236
7237
const struct glsl_type *const type =
7238
state->symbols->get_type(this->type_name);
7239
if (!is_valid_default_precision_type(type)) {
7240
_mesa_glsl_error(&loc, state,
7241
"default precision statements apply only to "
7242
"float, int, and opaque types");
7243
return NULL;
7244
}
7245
7246
if (state->es_shader) {
7247
/* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
7248
* spec says:
7249
*
7250
* "Non-precision qualified declarations will use the precision
7251
* qualifier specified in the most recent precision statement
7252
* that is still in scope. The precision statement has the same
7253
* scoping rules as variable declarations. If it is declared
7254
* inside a compound statement, its effect stops at the end of
7255
* the innermost statement it was declared in. Precision
7256
* statements in nested scopes override precision statements in
7257
* outer scopes. Multiple precision statements for the same basic
7258
* type can appear inside the same scope, with later statements
7259
* overriding earlier statements within that scope."
7260
*
7261
* Default precision specifications follow the same scope rules as
7262
* variables. So, we can track the state of the default precision
7263
* qualifiers in the symbol table, and the rules will just work. This
7264
* is a slight abuse of the symbol table, but it has the semantics
7265
* that we want.
7266
*/
7267
state->symbols->add_default_precision_qualifier(this->type_name,
7268
this->default_precision);
7269
}
7270
7271
/* FINISHME: Translate precision statements into IR. */
7272
return NULL;
7273
}
7274
7275
/* _mesa_ast_set_aggregate_type() sets the <structure> field so that
7276
* process_record_constructor() can do type-checking on C-style initializer
7277
* expressions of structs, but ast_struct_specifier should only be translated
7278
* to HIR if it is declaring the type of a structure.
7279
*
7280
* The ->is_declaration field is false for initializers of variables
7281
* declared separately from the struct's type definition.
7282
*
7283
* struct S { ... }; (is_declaration = true)
7284
* struct T { ... } t = { ... }; (is_declaration = true)
7285
* S s = { ... }; (is_declaration = false)
7286
*/
7287
if (this->structure != NULL && this->structure->is_declaration)
7288
return this->structure->hir(instructions, state);
7289
7290
return NULL;
7291
}
7292
7293
7294
/**
7295
* Process a structure or interface block tree into an array of structure fields
7296
*
7297
* After parsing, where there are some syntax differnces, structures and
7298
* interface blocks are almost identical. They are similar enough that the
7299
* AST for each can be processed the same way into a set of
7300
* \c glsl_struct_field to describe the members.
7301
*
7302
* If we're processing an interface block, var_mode should be the type of the
7303
* interface block (ir_var_shader_in, ir_var_shader_out, ir_var_uniform or
7304
* ir_var_shader_storage). If we're processing a structure, var_mode should be
7305
* ir_var_auto.
7306
*
7307
* \return
7308
* The number of fields processed. A pointer to the array structure fields is
7309
* stored in \c *fields_ret.
7310
*/
7311
static unsigned
7312
ast_process_struct_or_iface_block_members(exec_list *instructions,
7313
struct _mesa_glsl_parse_state *state,
7314
exec_list *declarations,
7315
glsl_struct_field **fields_ret,
7316
bool is_interface,
7317
enum glsl_matrix_layout matrix_layout,
7318
bool allow_reserved_names,
7319
ir_variable_mode var_mode,
7320
ast_type_qualifier *layout,
7321
unsigned block_stream,
7322
unsigned block_xfb_buffer,
7323
unsigned block_xfb_offset,
7324
unsigned expl_location,
7325
unsigned expl_align)
7326
{
7327
unsigned decl_count = 0;
7328
unsigned next_offset = 0;
7329
7330
/* Make an initial pass over the list of fields to determine how
7331
* many there are. Each element in this list is an ast_declarator_list.
7332
* This means that we actually need to count the number of elements in the
7333
* 'declarations' list in each of the elements.
7334
*/
7335
foreach_list_typed (ast_declarator_list, decl_list, link, declarations) {
7336
decl_count += decl_list->declarations.length();
7337
}
7338
7339
/* Allocate storage for the fields and process the field
7340
* declarations. As the declarations are processed, try to also convert
7341
* the types to HIR. This ensures that structure definitions embedded in
7342
* other structure definitions or in interface blocks are processed.
7343
*/
7344
glsl_struct_field *const fields = rzalloc_array(state, glsl_struct_field,
7345
decl_count);
7346
7347
bool first_member = true;
7348
bool first_member_has_explicit_location = false;
7349
7350
unsigned i = 0;
7351
foreach_list_typed (ast_declarator_list, decl_list, link, declarations) {
7352
const char *type_name;
7353
YYLTYPE loc = decl_list->get_location();
7354
7355
decl_list->type->specifier->hir(instructions, state);
7356
7357
/* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
7358
*
7359
* "Anonymous structures are not supported; so embedded structures
7360
* must have a declarator. A name given to an embedded struct is
7361
* scoped at the same level as the struct it is embedded in."
7362
*
7363
* The same section of the GLSL 1.20 spec says:
7364
*
7365
* "Anonymous structures are not supported. Embedded structures are
7366
* not supported."
7367
*
7368
* The GLSL ES 1.00 and 3.00 specs have similar langauge. So, we allow
7369
* embedded structures in 1.10 only.
7370
*/
7371
if (state->language_version != 110 &&
7372
decl_list->type->specifier->structure != NULL)
7373
_mesa_glsl_error(&loc, state,
7374
"embedded structure declarations are not allowed");
7375
7376
const glsl_type *decl_type =
7377
decl_list->type->glsl_type(& type_name, state);
7378
7379
const struct ast_type_qualifier *const qual =
7380
&decl_list->type->qualifier;
7381
7382
/* From section 4.3.9 of the GLSL 4.40 spec:
7383
*
7384
* "[In interface blocks] opaque types are not allowed."
7385
*
7386
* It should be impossible for decl_type to be NULL here. Cases that
7387
* might naturally lead to decl_type being NULL, especially for the
7388
* is_interface case, will have resulted in compilation having
7389
* already halted due to a syntax error.
7390
*/
7391
assert(decl_type);
7392
7393
if (is_interface) {
7394
/* From section 4.3.7 of the ARB_bindless_texture spec:
7395
*
7396
* "(remove the following bullet from the last list on p. 39,
7397
* thereby permitting sampler types in interface blocks; image
7398
* types are also permitted in blocks by this extension)"
7399
*
7400
* * sampler types are not allowed
7401
*/
7402
if (decl_type->contains_atomic() ||
7403
(!state->has_bindless() && decl_type->contains_opaque())) {
7404
_mesa_glsl_error(&loc, state, "uniform/buffer in non-default "
7405
"interface block contains %s variable",
7406
state->has_bindless() ? "atomic" : "opaque");
7407
}
7408
} else {
7409
if (decl_type->contains_atomic()) {
7410
/* From section 4.1.7.3 of the GLSL 4.40 spec:
7411
*
7412
* "Members of structures cannot be declared as atomic counter
7413
* types."
7414
*/
7415
_mesa_glsl_error(&loc, state, "atomic counter in structure");
7416
}
7417
7418
if (!state->has_bindless() && decl_type->contains_image()) {
7419
/* FINISHME: Same problem as with atomic counters.
7420
* FINISHME: Request clarification from Khronos and add
7421
* FINISHME: spec quotation here.
7422
*/
7423
_mesa_glsl_error(&loc, state, "image in structure");
7424
}
7425
}
7426
7427
if (qual->flags.q.explicit_binding) {
7428
_mesa_glsl_error(&loc, state,
7429
"binding layout qualifier cannot be applied "
7430
"to struct or interface block members");
7431
}
7432
7433
if (is_interface) {
7434
if (!first_member) {
7435
if (!layout->flags.q.explicit_location &&
7436
((first_member_has_explicit_location &&
7437
!qual->flags.q.explicit_location) ||
7438
(!first_member_has_explicit_location &&
7439
qual->flags.q.explicit_location))) {
7440
_mesa_glsl_error(&loc, state,
7441
"when block-level location layout qualifier "
7442
"is not supplied either all members must "
7443
"have a location layout qualifier or all "
7444
"members must not have a location layout "
7445
"qualifier");
7446
}
7447
} else {
7448
first_member = false;
7449
first_member_has_explicit_location =
7450
qual->flags.q.explicit_location;
7451
}
7452
}
7453
7454
if (qual->flags.q.std140 ||
7455
qual->flags.q.std430 ||
7456
qual->flags.q.packed ||
7457
qual->flags.q.shared) {
7458
_mesa_glsl_error(&loc, state,
7459
"uniform/shader storage block layout qualifiers "
7460
"std140, std430, packed, and shared can only be "
7461
"applied to uniform/shader storage blocks, not "
7462
"members");
7463
}
7464
7465
if (qual->flags.q.constant) {
7466
_mesa_glsl_error(&loc, state,
7467
"const storage qualifier cannot be applied "
7468
"to struct or interface block members");
7469
}
7470
7471
validate_memory_qualifier_for_type(state, &loc, qual, decl_type);
7472
validate_image_format_qualifier_for_type(state, &loc, qual, decl_type);
7473
7474
/* From Section 4.4.2.3 (Geometry Outputs) of the GLSL 4.50 spec:
7475
*
7476
* "A block member may be declared with a stream identifier, but
7477
* the specified stream must match the stream associated with the
7478
* containing block."
7479
*/
7480
if (qual->flags.q.explicit_stream) {
7481
unsigned qual_stream;
7482
if (process_qualifier_constant(state, &loc, "stream",
7483
qual->stream, &qual_stream) &&
7484
qual_stream != block_stream) {
7485
_mesa_glsl_error(&loc, state, "stream layout qualifier on "
7486
"interface block member does not match "
7487
"the interface block (%u vs %u)", qual_stream,
7488
block_stream);
7489
}
7490
}
7491
7492
int xfb_buffer;
7493
unsigned explicit_xfb_buffer = 0;
7494
if (qual->flags.q.explicit_xfb_buffer) {
7495
unsigned qual_xfb_buffer;
7496
if (process_qualifier_constant(state, &loc, "xfb_buffer",
7497
qual->xfb_buffer, &qual_xfb_buffer)) {
7498
explicit_xfb_buffer = 1;
7499
if (qual_xfb_buffer != block_xfb_buffer)
7500
_mesa_glsl_error(&loc, state, "xfb_buffer layout qualifier on "
7501
"interface block member does not match "
7502
"the interface block (%u vs %u)",
7503
qual_xfb_buffer, block_xfb_buffer);
7504
}
7505
xfb_buffer = (int) qual_xfb_buffer;
7506
} else {
7507
if (layout)
7508
explicit_xfb_buffer = layout->flags.q.explicit_xfb_buffer;
7509
xfb_buffer = (int) block_xfb_buffer;
7510
}
7511
7512
int xfb_stride = -1;
7513
if (qual->flags.q.explicit_xfb_stride) {
7514
unsigned qual_xfb_stride;
7515
if (process_qualifier_constant(state, &loc, "xfb_stride",
7516
qual->xfb_stride, &qual_xfb_stride)) {
7517
xfb_stride = (int) qual_xfb_stride;
7518
}
7519
}
7520
7521
if (qual->flags.q.uniform && qual->has_interpolation()) {
7522
_mesa_glsl_error(&loc, state,
7523
"interpolation qualifiers cannot be used "
7524
"with uniform interface blocks");
7525
}
7526
7527
if ((qual->flags.q.uniform || !is_interface) &&
7528
qual->has_auxiliary_storage()) {
7529
_mesa_glsl_error(&loc, state,
7530
"auxiliary storage qualifiers cannot be used "
7531
"in uniform blocks or structures.");
7532
}
7533
7534
if (qual->flags.q.row_major || qual->flags.q.column_major) {
7535
if (!qual->flags.q.uniform && !qual->flags.q.buffer) {
7536
_mesa_glsl_error(&loc, state,
7537
"row_major and column_major can only be "
7538
"applied to interface blocks");
7539
} else
7540
validate_matrix_layout_for_type(state, &loc, decl_type, NULL);
7541
}
7542
7543
foreach_list_typed (ast_declaration, decl, link,
7544
&decl_list->declarations) {
7545
YYLTYPE loc = decl->get_location();
7546
7547
if (!allow_reserved_names)
7548
validate_identifier(decl->identifier, loc, state);
7549
7550
const struct glsl_type *field_type =
7551
process_array_type(&loc, decl_type, decl->array_specifier, state);
7552
validate_array_dimensions(field_type, state, &loc);
7553
fields[i].type = field_type;
7554
fields[i].name = decl->identifier;
7555
fields[i].interpolation =
7556
interpret_interpolation_qualifier(qual, field_type,
7557
var_mode, state, &loc);
7558
fields[i].centroid = qual->flags.q.centroid ? 1 : 0;
7559
fields[i].sample = qual->flags.q.sample ? 1 : 0;
7560
fields[i].patch = qual->flags.q.patch ? 1 : 0;
7561
fields[i].offset = -1;
7562
fields[i].explicit_xfb_buffer = explicit_xfb_buffer;
7563
fields[i].xfb_buffer = xfb_buffer;
7564
fields[i].xfb_stride = xfb_stride;
7565
7566
if (qual->flags.q.explicit_location) {
7567
unsigned qual_location;
7568
if (process_qualifier_constant(state, &loc, "location",
7569
qual->location, &qual_location)) {
7570
fields[i].location = qual_location +
7571
(fields[i].patch ? VARYING_SLOT_PATCH0 : VARYING_SLOT_VAR0);
7572
expl_location = fields[i].location +
7573
fields[i].type->count_attribute_slots(false);
7574
}
7575
} else {
7576
if (layout && layout->flags.q.explicit_location) {
7577
fields[i].location = expl_location;
7578
expl_location += fields[i].type->count_attribute_slots(false);
7579
} else {
7580
fields[i].location = -1;
7581
}
7582
}
7583
7584
if (qual->flags.q.explicit_component) {
7585
unsigned qual_component;
7586
if (process_qualifier_constant(state, &loc, "component",
7587
qual->component, &qual_component)) {
7588
validate_component_layout_for_type(state, &loc, fields[i].type,
7589
qual_component);
7590
fields[i].component = qual_component;
7591
}
7592
} else {
7593
fields[i].component = -1;
7594
}
7595
7596
/* Offset can only be used with std430 and std140 layouts an initial
7597
* value of 0 is used for error detection.
7598
*/
7599
unsigned align = 0;
7600
unsigned size = 0;
7601
if (layout) {
7602
bool row_major;
7603
if (qual->flags.q.row_major ||
7604
matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
7605
row_major = true;
7606
} else {
7607
row_major = false;
7608
}
7609
7610
if(layout->flags.q.std140) {
7611
align = field_type->std140_base_alignment(row_major);
7612
size = field_type->std140_size(row_major);
7613
} else if (layout->flags.q.std430) {
7614
align = field_type->std430_base_alignment(row_major);
7615
size = field_type->std430_size(row_major);
7616
}
7617
}
7618
7619
if (qual->flags.q.explicit_offset) {
7620
unsigned qual_offset;
7621
if (process_qualifier_constant(state, &loc, "offset",
7622
qual->offset, &qual_offset)) {
7623
if (align != 0 && size != 0) {
7624
if (next_offset > qual_offset)
7625
_mesa_glsl_error(&loc, state, "layout qualifier "
7626
"offset overlaps previous member");
7627
7628
if (qual_offset % align) {
7629
_mesa_glsl_error(&loc, state, "layout qualifier offset "
7630
"must be a multiple of the base "
7631
"alignment of %s", field_type->name);
7632
}
7633
fields[i].offset = qual_offset;
7634
next_offset = qual_offset + size;
7635
} else {
7636
_mesa_glsl_error(&loc, state, "offset can only be used "
7637
"with std430 and std140 layouts");
7638
}
7639
}
7640
}
7641
7642
if (qual->flags.q.explicit_align || expl_align != 0) {
7643
unsigned offset = fields[i].offset != -1 ? fields[i].offset :
7644
next_offset;
7645
if (align == 0 || size == 0) {
7646
_mesa_glsl_error(&loc, state, "align can only be used with "
7647
"std430 and std140 layouts");
7648
} else if (qual->flags.q.explicit_align) {
7649
unsigned member_align;
7650
if (process_qualifier_constant(state, &loc, "align",
7651
qual->align, &member_align)) {
7652
if (member_align == 0 ||
7653
member_align & (member_align - 1)) {
7654
_mesa_glsl_error(&loc, state, "align layout qualifier "
7655
"is not a power of 2");
7656
} else {
7657
fields[i].offset = glsl_align(offset, member_align);
7658
next_offset = fields[i].offset + size;
7659
}
7660
}
7661
} else {
7662
fields[i].offset = glsl_align(offset, expl_align);
7663
next_offset = fields[i].offset + size;
7664
}
7665
} else if (!qual->flags.q.explicit_offset) {
7666
if (align != 0 && size != 0)
7667
next_offset = glsl_align(next_offset, align) + size;
7668
}
7669
7670
/* From the ARB_enhanced_layouts spec:
7671
*
7672
* "The given offset applies to the first component of the first
7673
* member of the qualified entity. Then, within the qualified
7674
* entity, subsequent components are each assigned, in order, to
7675
* the next available offset aligned to a multiple of that
7676
* component's size. Aggregate types are flattened down to the
7677
* component level to get this sequence of components."
7678
*/
7679
if (qual->flags.q.explicit_xfb_offset) {
7680
unsigned xfb_offset;
7681
if (process_qualifier_constant(state, &loc, "xfb_offset",
7682
qual->offset, &xfb_offset)) {
7683
fields[i].offset = xfb_offset;
7684
block_xfb_offset = fields[i].offset +
7685
4 * field_type->component_slots();
7686
}
7687
} else {
7688
if (layout && layout->flags.q.explicit_xfb_offset) {
7689
unsigned align = field_type->is_64bit() ? 8 : 4;
7690
fields[i].offset = glsl_align(block_xfb_offset, align);
7691
block_xfb_offset += 4 * field_type->component_slots();
7692
}
7693
}
7694
7695
/* Propogate row- / column-major information down the fields of the
7696
* structure or interface block. Structures need this data because
7697
* the structure may contain a structure that contains ... a matrix
7698
* that need the proper layout.
7699
*/
7700
if (is_interface && layout &&
7701
(layout->flags.q.uniform || layout->flags.q.buffer) &&
7702
(field_type->without_array()->is_matrix()
7703
|| field_type->without_array()->is_struct())) {
7704
/* If no layout is specified for the field, inherit the layout
7705
* from the block.
7706
*/
7707
fields[i].matrix_layout = matrix_layout;
7708
7709
if (qual->flags.q.row_major)
7710
fields[i].matrix_layout = GLSL_MATRIX_LAYOUT_ROW_MAJOR;
7711
else if (qual->flags.q.column_major)
7712
fields[i].matrix_layout = GLSL_MATRIX_LAYOUT_COLUMN_MAJOR;
7713
7714
/* If we're processing an uniform or buffer block, the matrix
7715
* layout must be decided by this point.
7716
*/
7717
assert(fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR
7718
|| fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR);
7719
}
7720
7721
/* Memory qualifiers are allowed on buffer and image variables, while
7722
* the format qualifier is only accepted for images.
7723
*/
7724
if (var_mode == ir_var_shader_storage ||
7725
field_type->without_array()->is_image()) {
7726
/* For readonly and writeonly qualifiers the field definition,
7727
* if set, overwrites the layout qualifier.
7728
*/
7729
if (qual->flags.q.read_only || qual->flags.q.write_only) {
7730
fields[i].memory_read_only = qual->flags.q.read_only;
7731
fields[i].memory_write_only = qual->flags.q.write_only;
7732
} else {
7733
fields[i].memory_read_only =
7734
layout ? layout->flags.q.read_only : 0;
7735
fields[i].memory_write_only =
7736
layout ? layout->flags.q.write_only : 0;
7737
}
7738
7739
/* For other qualifiers, we set the flag if either the layout
7740
* qualifier or the field qualifier are set
7741
*/
7742
fields[i].memory_coherent = qual->flags.q.coherent ||
7743
(layout && layout->flags.q.coherent);
7744
fields[i].memory_volatile = qual->flags.q._volatile ||
7745
(layout && layout->flags.q._volatile);
7746
fields[i].memory_restrict = qual->flags.q.restrict_flag ||
7747
(layout && layout->flags.q.restrict_flag);
7748
7749
if (field_type->without_array()->is_image()) {
7750
if (qual->flags.q.explicit_image_format) {
7751
if (qual->image_base_type !=
7752
field_type->without_array()->sampled_type) {
7753
_mesa_glsl_error(&loc, state, "format qualifier doesn't "
7754
"match the base data type of the image");
7755
}
7756
7757
fields[i].image_format = qual->image_format;
7758
} else {
7759
if (!qual->flags.q.write_only) {
7760
_mesa_glsl_error(&loc, state, "image not qualified with "
7761
"`writeonly' must have a format layout "
7762
"qualifier");
7763
}
7764
7765
fields[i].image_format = PIPE_FORMAT_NONE;
7766
}
7767
}
7768
}
7769
7770
/* Precision qualifiers do not hold any meaning in Desktop GLSL */
7771
if (state->es_shader) {
7772
fields[i].precision = select_gles_precision(qual->precision,
7773
field_type,
7774
state,
7775
&loc);
7776
} else {
7777
fields[i].precision = qual->precision;
7778
}
7779
7780
i++;
7781
}
7782
}
7783
7784
assert(i == decl_count);
7785
7786
*fields_ret = fields;
7787
return decl_count;
7788
}
7789
7790
7791
ir_rvalue *
7792
ast_struct_specifier::hir(exec_list *instructions,
7793
struct _mesa_glsl_parse_state *state)
7794
{
7795
YYLTYPE loc = this->get_location();
7796
7797
unsigned expl_location = 0;
7798
if (layout && layout->flags.q.explicit_location) {
7799
if (!process_qualifier_constant(state, &loc, "location",
7800
layout->location, &expl_location)) {
7801
return NULL;
7802
} else {
7803
expl_location = VARYING_SLOT_VAR0 + expl_location;
7804
}
7805
}
7806
7807
glsl_struct_field *fields;
7808
unsigned decl_count =
7809
ast_process_struct_or_iface_block_members(instructions,
7810
state,
7811
&this->declarations,
7812
&fields,
7813
false,
7814
GLSL_MATRIX_LAYOUT_INHERITED,
7815
false /* allow_reserved_names */,
7816
ir_var_auto,
7817
layout,
7818
0, /* for interface only */
7819
0, /* for interface only */
7820
0, /* for interface only */
7821
expl_location,
7822
0 /* for interface only */);
7823
7824
validate_identifier(this->name, loc, state);
7825
7826
type = glsl_type::get_struct_instance(fields, decl_count, this->name);
7827
7828
if (!type->is_anonymous() && !state->symbols->add_type(name, type)) {
7829
const glsl_type *match = state->symbols->get_type(name);
7830
/* allow struct matching for desktop GL - older UE4 does this */
7831
if (match != NULL && state->is_version(130, 0) && match->record_compare(type, true, false))
7832
_mesa_glsl_warning(& loc, state, "struct `%s' previously defined", name);
7833
else
7834
_mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
7835
} else {
7836
const glsl_type **s = reralloc(state, state->user_structures,
7837
const glsl_type *,
7838
state->num_user_structures + 1);
7839
if (s != NULL) {
7840
s[state->num_user_structures] = type;
7841
state->user_structures = s;
7842
state->num_user_structures++;
7843
}
7844
}
7845
7846
/* Structure type definitions do not have r-values.
7847
*/
7848
return NULL;
7849
}
7850
7851
7852
/**
7853
* Visitor class which detects whether a given interface block has been used.
7854
*/
7855
class interface_block_usage_visitor : public ir_hierarchical_visitor
7856
{
7857
public:
7858
interface_block_usage_visitor(ir_variable_mode mode, const glsl_type *block)
7859
: mode(mode), block(block), found(false)
7860
{
7861
}
7862
7863
virtual ir_visitor_status visit(ir_dereference_variable *ir)
7864
{
7865
if (ir->var->data.mode == mode && ir->var->get_interface_type() == block) {
7866
found = true;
7867
return visit_stop;
7868
}
7869
return visit_continue;
7870
}
7871
7872
bool usage_found() const
7873
{
7874
return this->found;
7875
}
7876
7877
private:
7878
ir_variable_mode mode;
7879
const glsl_type *block;
7880
bool found;
7881
};
7882
7883
static bool
7884
is_unsized_array_last_element(ir_variable *v)
7885
{
7886
const glsl_type *interface_type = v->get_interface_type();
7887
int length = interface_type->length;
7888
7889
assert(v->type->is_unsized_array());
7890
7891
/* Check if it is the last element of the interface */
7892
if (strcmp(interface_type->fields.structure[length-1].name, v->name) == 0)
7893
return true;
7894
return false;
7895
}
7896
7897
static void
7898
apply_memory_qualifiers(ir_variable *var, glsl_struct_field field)
7899
{
7900
var->data.memory_read_only = field.memory_read_only;
7901
var->data.memory_write_only = field.memory_write_only;
7902
var->data.memory_coherent = field.memory_coherent;
7903
var->data.memory_volatile = field.memory_volatile;
7904
var->data.memory_restrict = field.memory_restrict;
7905
}
7906
7907
ir_rvalue *
7908
ast_interface_block::hir(exec_list *instructions,
7909
struct _mesa_glsl_parse_state *state)
7910
{
7911
YYLTYPE loc = this->get_location();
7912
7913
/* Interface blocks must be declared at global scope */
7914
if (state->current_function != NULL) {
7915
_mesa_glsl_error(&loc, state,
7916
"Interface block `%s' must be declared "
7917
"at global scope",
7918
this->block_name);
7919
}
7920
7921
/* Validate qualifiers:
7922
*
7923
* - Layout Qualifiers as per the table in Section 4.4
7924
* ("Layout Qualifiers") of the GLSL 4.50 spec.
7925
*
7926
* - Memory Qualifiers as per Section 4.10 ("Memory Qualifiers") of the
7927
* GLSL 4.50 spec:
7928
*
7929
* "Additionally, memory qualifiers may also be used in the declaration
7930
* of shader storage blocks"
7931
*
7932
* Note the table in Section 4.4 says std430 is allowed on both uniform and
7933
* buffer blocks however Section 4.4.5 (Uniform and Shader Storage Block
7934
* Layout Qualifiers) of the GLSL 4.50 spec says:
7935
*
7936
* "The std430 qualifier is supported only for shader storage blocks;
7937
* using std430 on a uniform block will result in a compile-time error."
7938
*/
7939
ast_type_qualifier allowed_blk_qualifiers;
7940
allowed_blk_qualifiers.flags.i = 0;
7941
if (this->layout.flags.q.buffer || this->layout.flags.q.uniform) {
7942
allowed_blk_qualifiers.flags.q.shared = 1;
7943
allowed_blk_qualifiers.flags.q.packed = 1;
7944
allowed_blk_qualifiers.flags.q.std140 = 1;
7945
allowed_blk_qualifiers.flags.q.row_major = 1;
7946
allowed_blk_qualifiers.flags.q.column_major = 1;
7947
allowed_blk_qualifiers.flags.q.explicit_align = 1;
7948
allowed_blk_qualifiers.flags.q.explicit_binding = 1;
7949
if (this->layout.flags.q.buffer) {
7950
allowed_blk_qualifiers.flags.q.buffer = 1;
7951
allowed_blk_qualifiers.flags.q.std430 = 1;
7952
allowed_blk_qualifiers.flags.q.coherent = 1;
7953
allowed_blk_qualifiers.flags.q._volatile = 1;
7954
allowed_blk_qualifiers.flags.q.restrict_flag = 1;
7955
allowed_blk_qualifiers.flags.q.read_only = 1;
7956
allowed_blk_qualifiers.flags.q.write_only = 1;
7957
} else {
7958
allowed_blk_qualifiers.flags.q.uniform = 1;
7959
}
7960
} else {
7961
/* Interface block */
7962
assert(this->layout.flags.q.in || this->layout.flags.q.out);
7963
7964
allowed_blk_qualifiers.flags.q.explicit_location = 1;
7965
if (this->layout.flags.q.out) {
7966
allowed_blk_qualifiers.flags.q.out = 1;
7967
if (state->stage == MESA_SHADER_GEOMETRY ||
7968
state->stage == MESA_SHADER_TESS_CTRL ||
7969
state->stage == MESA_SHADER_TESS_EVAL ||
7970
state->stage == MESA_SHADER_VERTEX ) {
7971
allowed_blk_qualifiers.flags.q.explicit_xfb_offset = 1;
7972
allowed_blk_qualifiers.flags.q.explicit_xfb_buffer = 1;
7973
allowed_blk_qualifiers.flags.q.xfb_buffer = 1;
7974
allowed_blk_qualifiers.flags.q.explicit_xfb_stride = 1;
7975
allowed_blk_qualifiers.flags.q.xfb_stride = 1;
7976
if (state->stage == MESA_SHADER_GEOMETRY) {
7977
allowed_blk_qualifiers.flags.q.stream = 1;
7978
allowed_blk_qualifiers.flags.q.explicit_stream = 1;
7979
}
7980
if (state->stage == MESA_SHADER_TESS_CTRL) {
7981
allowed_blk_qualifiers.flags.q.patch = 1;
7982
}
7983
}
7984
} else {
7985
allowed_blk_qualifiers.flags.q.in = 1;
7986
if (state->stage == MESA_SHADER_TESS_EVAL) {
7987
allowed_blk_qualifiers.flags.q.patch = 1;
7988
}
7989
}
7990
}
7991
7992
this->layout.validate_flags(&loc, state, allowed_blk_qualifiers,
7993
"invalid qualifier for block",
7994
this->block_name);
7995
7996
enum glsl_interface_packing packing;
7997
if (this->layout.flags.q.std140) {
7998
packing = GLSL_INTERFACE_PACKING_STD140;
7999
} else if (this->layout.flags.q.packed) {
8000
packing = GLSL_INTERFACE_PACKING_PACKED;
8001
} else if (this->layout.flags.q.std430) {
8002
packing = GLSL_INTERFACE_PACKING_STD430;
8003
} else {
8004
/* The default layout is shared.
8005
*/
8006
packing = GLSL_INTERFACE_PACKING_SHARED;
8007
}
8008
8009
ir_variable_mode var_mode;
8010
const char *iface_type_name;
8011
if (this->layout.flags.q.in) {
8012
var_mode = ir_var_shader_in;
8013
iface_type_name = "in";
8014
} else if (this->layout.flags.q.out) {
8015
var_mode = ir_var_shader_out;
8016
iface_type_name = "out";
8017
} else if (this->layout.flags.q.uniform) {
8018
var_mode = ir_var_uniform;
8019
iface_type_name = "uniform";
8020
} else if (this->layout.flags.q.buffer) {
8021
var_mode = ir_var_shader_storage;
8022
iface_type_name = "buffer";
8023
} else {
8024
var_mode = ir_var_auto;
8025
iface_type_name = "UNKNOWN";
8026
assert(!"interface block layout qualifier not found!");
8027
}
8028
8029
enum glsl_matrix_layout matrix_layout = GLSL_MATRIX_LAYOUT_INHERITED;
8030
if (this->layout.flags.q.row_major)
8031
matrix_layout = GLSL_MATRIX_LAYOUT_ROW_MAJOR;
8032
else if (this->layout.flags.q.column_major)
8033
matrix_layout = GLSL_MATRIX_LAYOUT_COLUMN_MAJOR;
8034
8035
bool redeclaring_per_vertex = strcmp(this->block_name, "gl_PerVertex") == 0;
8036
exec_list declared_variables;
8037
glsl_struct_field *fields;
8038
8039
/* For blocks that accept memory qualifiers (i.e. shader storage), verify
8040
* that we don't have incompatible qualifiers
8041
*/
8042
if (this->layout.flags.q.read_only && this->layout.flags.q.write_only) {
8043
_mesa_glsl_error(&loc, state,
8044
"Interface block sets both readonly and writeonly");
8045
}
8046
8047
unsigned qual_stream;
8048
if (!process_qualifier_constant(state, &loc, "stream", this->layout.stream,
8049
&qual_stream) ||
8050
!validate_stream_qualifier(&loc, state, qual_stream)) {
8051
/* If the stream qualifier is invalid it doesn't make sense to continue
8052
* on and try to compare stream layouts on member variables against it
8053
* so just return early.
8054
*/
8055
return NULL;
8056
}
8057
8058
unsigned qual_xfb_buffer;
8059
if (!process_qualifier_constant(state, &loc, "xfb_buffer",
8060
layout.xfb_buffer, &qual_xfb_buffer) ||
8061
!validate_xfb_buffer_qualifier(&loc, state, qual_xfb_buffer)) {
8062
return NULL;
8063
}
8064
8065
unsigned qual_xfb_offset = 0;
8066
if (layout.flags.q.explicit_xfb_offset) {
8067
if (!process_qualifier_constant(state, &loc, "xfb_offset",
8068
layout.offset, &qual_xfb_offset)) {
8069
return NULL;
8070
}
8071
}
8072
8073
unsigned qual_xfb_stride = 0;
8074
if (layout.flags.q.explicit_xfb_stride) {
8075
if (!process_qualifier_constant(state, &loc, "xfb_stride",
8076
layout.xfb_stride, &qual_xfb_stride)) {
8077
return NULL;
8078
}
8079
}
8080
8081
unsigned expl_location = 0;
8082
if (layout.flags.q.explicit_location) {
8083
if (!process_qualifier_constant(state, &loc, "location",
8084
layout.location, &expl_location)) {
8085
return NULL;
8086
} else {
8087
expl_location += this->layout.flags.q.patch ? VARYING_SLOT_PATCH0
8088
: VARYING_SLOT_VAR0;
8089
}
8090
}
8091
8092
unsigned expl_align = 0;
8093
if (layout.flags.q.explicit_align) {
8094
if (!process_qualifier_constant(state, &loc, "align",
8095
layout.align, &expl_align)) {
8096
return NULL;
8097
} else {
8098
if (expl_align == 0 || expl_align & (expl_align - 1)) {
8099
_mesa_glsl_error(&loc, state, "align layout qualifier is not a "
8100
"power of 2.");
8101
return NULL;
8102
}
8103
}
8104
}
8105
8106
unsigned int num_variables =
8107
ast_process_struct_or_iface_block_members(&declared_variables,
8108
state,
8109
&this->declarations,
8110
&fields,
8111
true,
8112
matrix_layout,
8113
redeclaring_per_vertex,
8114
var_mode,
8115
&this->layout,
8116
qual_stream,
8117
qual_xfb_buffer,
8118
qual_xfb_offset,
8119
expl_location,
8120
expl_align);
8121
8122
if (!redeclaring_per_vertex) {
8123
validate_identifier(this->block_name, loc, state);
8124
8125
/* From section 4.3.9 ("Interface Blocks") of the GLSL 4.50 spec:
8126
*
8127
* "Block names have no other use within a shader beyond interface
8128
* matching; it is a compile-time error to use a block name at global
8129
* scope for anything other than as a block name."
8130
*/
8131
ir_variable *var = state->symbols->get_variable(this->block_name);
8132
if (var && !var->type->is_interface()) {
8133
_mesa_glsl_error(&loc, state, "Block name `%s' is "
8134
"already used in the scope.",
8135
this->block_name);
8136
}
8137
}
8138
8139
const glsl_type *earlier_per_vertex = NULL;
8140
if (redeclaring_per_vertex) {
8141
/* Find the previous declaration of gl_PerVertex. If we're redeclaring
8142
* the named interface block gl_in, we can find it by looking at the
8143
* previous declaration of gl_in. Otherwise we can find it by looking
8144
* at the previous decalartion of any of the built-in outputs,
8145
* e.g. gl_Position.
8146
*
8147
* Also check that the instance name and array-ness of the redeclaration
8148
* are correct.
8149
*/
8150
switch (var_mode) {
8151
case ir_var_shader_in:
8152
if (ir_variable *earlier_gl_in =
8153
state->symbols->get_variable("gl_in")) {
8154
earlier_per_vertex = earlier_gl_in->get_interface_type();
8155
} else {
8156
_mesa_glsl_error(&loc, state,
8157
"redeclaration of gl_PerVertex input not allowed "
8158
"in the %s shader",
8159
_mesa_shader_stage_to_string(state->stage));
8160
}
8161
if (this->instance_name == NULL ||
8162
strcmp(this->instance_name, "gl_in") != 0 || this->array_specifier == NULL ||
8163
!this->array_specifier->is_single_dimension()) {
8164
_mesa_glsl_error(&loc, state,
8165
"gl_PerVertex input must be redeclared as "
8166
"gl_in[]");
8167
}
8168
break;
8169
case ir_var_shader_out:
8170
if (ir_variable *earlier_gl_Position =
8171
state->symbols->get_variable("gl_Position")) {
8172
earlier_per_vertex = earlier_gl_Position->get_interface_type();
8173
} else if (ir_variable *earlier_gl_out =
8174
state->symbols->get_variable("gl_out")) {
8175
earlier_per_vertex = earlier_gl_out->get_interface_type();
8176
} else {
8177
_mesa_glsl_error(&loc, state,
8178
"redeclaration of gl_PerVertex output not "
8179
"allowed in the %s shader",
8180
_mesa_shader_stage_to_string(state->stage));
8181
}
8182
if (state->stage == MESA_SHADER_TESS_CTRL) {
8183
if (this->instance_name == NULL ||
8184
strcmp(this->instance_name, "gl_out") != 0 || this->array_specifier == NULL) {
8185
_mesa_glsl_error(&loc, state,
8186
"gl_PerVertex output must be redeclared as "
8187
"gl_out[]");
8188
}
8189
} else {
8190
if (this->instance_name != NULL) {
8191
_mesa_glsl_error(&loc, state,
8192
"gl_PerVertex output may not be redeclared with "
8193
"an instance name");
8194
}
8195
}
8196
break;
8197
default:
8198
_mesa_glsl_error(&loc, state,
8199
"gl_PerVertex must be declared as an input or an "
8200
"output");
8201
break;
8202
}
8203
8204
if (earlier_per_vertex == NULL) {
8205
/* An error has already been reported. Bail out to avoid null
8206
* dereferences later in this function.
8207
*/
8208
return NULL;
8209
}
8210
8211
/* Copy locations from the old gl_PerVertex interface block. */
8212
for (unsigned i = 0; i < num_variables; i++) {
8213
int j = earlier_per_vertex->field_index(fields[i].name);
8214
if (j == -1) {
8215
_mesa_glsl_error(&loc, state,
8216
"redeclaration of gl_PerVertex must be a subset "
8217
"of the built-in members of gl_PerVertex");
8218
} else {
8219
fields[i].location =
8220
earlier_per_vertex->fields.structure[j].location;
8221
fields[i].offset =
8222
earlier_per_vertex->fields.structure[j].offset;
8223
fields[i].interpolation =
8224
earlier_per_vertex->fields.structure[j].interpolation;
8225
fields[i].centroid =
8226
earlier_per_vertex->fields.structure[j].centroid;
8227
fields[i].sample =
8228
earlier_per_vertex->fields.structure[j].sample;
8229
fields[i].patch =
8230
earlier_per_vertex->fields.structure[j].patch;
8231
fields[i].precision =
8232
earlier_per_vertex->fields.structure[j].precision;
8233
fields[i].explicit_xfb_buffer =
8234
earlier_per_vertex->fields.structure[j].explicit_xfb_buffer;
8235
fields[i].xfb_buffer =
8236
earlier_per_vertex->fields.structure[j].xfb_buffer;
8237
fields[i].xfb_stride =
8238
earlier_per_vertex->fields.structure[j].xfb_stride;
8239
}
8240
}
8241
8242
/* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
8243
* spec:
8244
*
8245
* If a built-in interface block is redeclared, it must appear in
8246
* the shader before any use of any member included in the built-in
8247
* declaration, or a compilation error will result.
8248
*
8249
* This appears to be a clarification to the behaviour established for
8250
* gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
8251
* regardless of GLSL version.
8252
*/
8253
interface_block_usage_visitor v(var_mode, earlier_per_vertex);
8254
v.run(instructions);
8255
if (v.usage_found()) {
8256
_mesa_glsl_error(&loc, state,
8257
"redeclaration of a built-in interface block must "
8258
"appear before any use of any member of the "
8259
"interface block");
8260
}
8261
}
8262
8263
const glsl_type *block_type =
8264
glsl_type::get_interface_instance(fields,
8265
num_variables,
8266
packing,
8267
matrix_layout ==
8268
GLSL_MATRIX_LAYOUT_ROW_MAJOR,
8269
this->block_name);
8270
8271
unsigned component_size = block_type->contains_double() ? 8 : 4;
8272
int xfb_offset =
8273
layout.flags.q.explicit_xfb_offset ? (int) qual_xfb_offset : -1;
8274
validate_xfb_offset_qualifier(&loc, state, xfb_offset, block_type,
8275
component_size);
8276
8277
if (!state->symbols->add_interface(block_type->name, block_type, var_mode)) {
8278
YYLTYPE loc = this->get_location();
8279
_mesa_glsl_error(&loc, state, "interface block `%s' with type `%s' "
8280
"already taken in the current scope",
8281
this->block_name, iface_type_name);
8282
}
8283
8284
/* Since interface blocks cannot contain statements, it should be
8285
* impossible for the block to generate any instructions.
8286
*/
8287
assert(declared_variables.is_empty());
8288
8289
/* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
8290
*
8291
* Geometry shader input variables get the per-vertex values written
8292
* out by vertex shader output variables of the same names. Since a
8293
* geometry shader operates on a set of vertices, each input varying
8294
* variable (or input block, see interface blocks below) needs to be
8295
* declared as an array.
8296
*/
8297
if (state->stage == MESA_SHADER_GEOMETRY && this->array_specifier == NULL &&
8298
var_mode == ir_var_shader_in) {
8299
_mesa_glsl_error(&loc, state, "geometry shader inputs must be arrays");
8300
} else if ((state->stage == MESA_SHADER_TESS_CTRL ||
8301
state->stage == MESA_SHADER_TESS_EVAL) &&
8302
!this->layout.flags.q.patch &&
8303
this->array_specifier == NULL &&
8304
var_mode == ir_var_shader_in) {
8305
_mesa_glsl_error(&loc, state, "per-vertex tessellation shader inputs must be arrays");
8306
} else if (state->stage == MESA_SHADER_TESS_CTRL &&
8307
!this->layout.flags.q.patch &&
8308
this->array_specifier == NULL &&
8309
var_mode == ir_var_shader_out) {
8310
_mesa_glsl_error(&loc, state, "tessellation control shader outputs must be arrays");
8311
}
8312
8313
8314
/* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
8315
* says:
8316
*
8317
* "If an instance name (instance-name) is used, then it puts all the
8318
* members inside a scope within its own name space, accessed with the
8319
* field selector ( . ) operator (analogously to structures)."
8320
*/
8321
if (this->instance_name) {
8322
if (redeclaring_per_vertex) {
8323
/* When a built-in in an unnamed interface block is redeclared,
8324
* get_variable_being_redeclared() calls
8325
* check_builtin_array_max_size() to make sure that built-in array
8326
* variables aren't redeclared to illegal sizes. But we're looking
8327
* at a redeclaration of a named built-in interface block. So we
8328
* have to manually call check_builtin_array_max_size() for all parts
8329
* of the interface that are arrays.
8330
*/
8331
for (unsigned i = 0; i < num_variables; i++) {
8332
if (fields[i].type->is_array()) {
8333
const unsigned size = fields[i].type->array_size();
8334
check_builtin_array_max_size(fields[i].name, size, loc, state);
8335
}
8336
}
8337
} else {
8338
validate_identifier(this->instance_name, loc, state);
8339
}
8340
8341
ir_variable *var;
8342
8343
if (this->array_specifier != NULL) {
8344
const glsl_type *block_array_type =
8345
process_array_type(&loc, block_type, this->array_specifier, state);
8346
8347
/* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
8348
*
8349
* For uniform blocks declared an array, each individual array
8350
* element corresponds to a separate buffer object backing one
8351
* instance of the block. As the array size indicates the number
8352
* of buffer objects needed, uniform block array declarations
8353
* must specify an array size.
8354
*
8355
* And a few paragraphs later:
8356
*
8357
* Geometry shader input blocks must be declared as arrays and
8358
* follow the array declaration and linking rules for all
8359
* geometry shader inputs. All other input and output block
8360
* arrays must specify an array size.
8361
*
8362
* The same applies to tessellation shaders.
8363
*
8364
* The upshot of this is that the only circumstance where an
8365
* interface array size *doesn't* need to be specified is on a
8366
* geometry shader input, tessellation control shader input,
8367
* tessellation control shader output, and tessellation evaluation
8368
* shader input.
8369
*/
8370
if (block_array_type->is_unsized_array()) {
8371
bool allow_inputs = state->stage == MESA_SHADER_GEOMETRY ||
8372
state->stage == MESA_SHADER_TESS_CTRL ||
8373
state->stage == MESA_SHADER_TESS_EVAL;
8374
bool allow_outputs = state->stage == MESA_SHADER_TESS_CTRL;
8375
8376
if (this->layout.flags.q.in) {
8377
if (!allow_inputs)
8378
_mesa_glsl_error(&loc, state,
8379
"unsized input block arrays not allowed in "
8380
"%s shader",
8381
_mesa_shader_stage_to_string(state->stage));
8382
} else if (this->layout.flags.q.out) {
8383
if (!allow_outputs)
8384
_mesa_glsl_error(&loc, state,
8385
"unsized output block arrays not allowed in "
8386
"%s shader",
8387
_mesa_shader_stage_to_string(state->stage));
8388
} else {
8389
/* by elimination, this is a uniform block array */
8390
_mesa_glsl_error(&loc, state,
8391
"unsized uniform block arrays not allowed in "
8392
"%s shader",
8393
_mesa_shader_stage_to_string(state->stage));
8394
}
8395
}
8396
8397
/* From section 4.3.9 (Interface Blocks) of the GLSL ES 3.10 spec:
8398
*
8399
* * Arrays of arrays of blocks are not allowed
8400
*/
8401
if (state->es_shader && block_array_type->is_array() &&
8402
block_array_type->fields.array->is_array()) {
8403
_mesa_glsl_error(&loc, state,
8404
"arrays of arrays interface blocks are "
8405
"not allowed");
8406
}
8407
8408
var = new(state) ir_variable(block_array_type,
8409
this->instance_name,
8410
var_mode);
8411
} else {
8412
var = new(state) ir_variable(block_type,
8413
this->instance_name,
8414
var_mode);
8415
}
8416
8417
var->data.matrix_layout = matrix_layout == GLSL_MATRIX_LAYOUT_INHERITED
8418
? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR : matrix_layout;
8419
8420
if (var_mode == ir_var_shader_in || var_mode == ir_var_uniform)
8421
var->data.read_only = true;
8422
8423
var->data.patch = this->layout.flags.q.patch;
8424
8425
if (state->stage == MESA_SHADER_GEOMETRY && var_mode == ir_var_shader_in)
8426
handle_geometry_shader_input_decl(state, loc, var);
8427
else if ((state->stage == MESA_SHADER_TESS_CTRL ||
8428
state->stage == MESA_SHADER_TESS_EVAL) && var_mode == ir_var_shader_in)
8429
handle_tess_shader_input_decl(state, loc, var);
8430
else if (state->stage == MESA_SHADER_TESS_CTRL && var_mode == ir_var_shader_out)
8431
handle_tess_ctrl_shader_output_decl(state, loc, var);
8432
8433
for (unsigned i = 0; i < num_variables; i++) {
8434
if (var->data.mode == ir_var_shader_storage)
8435
apply_memory_qualifiers(var, fields[i]);
8436
}
8437
8438
if (ir_variable *earlier =
8439
state->symbols->get_variable(this->instance_name)) {
8440
if (!redeclaring_per_vertex) {
8441
_mesa_glsl_error(&loc, state, "`%s' redeclared",
8442
this->instance_name);
8443
}
8444
earlier->data.how_declared = ir_var_declared_normally;
8445
earlier->type = var->type;
8446
earlier->reinit_interface_type(block_type);
8447
delete var;
8448
} else {
8449
if (this->layout.flags.q.explicit_binding) {
8450
apply_explicit_binding(state, &loc, var, var->type,
8451
&this->layout);
8452
}
8453
8454
var->data.stream = qual_stream;
8455
if (layout.flags.q.explicit_location) {
8456
var->data.location = expl_location;
8457
var->data.explicit_location = true;
8458
}
8459
8460
state->symbols->add_variable(var);
8461
instructions->push_tail(var);
8462
}
8463
} else {
8464
/* In order to have an array size, the block must also be declared with
8465
* an instance name.
8466
*/
8467
assert(this->array_specifier == NULL);
8468
8469
for (unsigned i = 0; i < num_variables; i++) {
8470
ir_variable *var =
8471
new(state) ir_variable(fields[i].type,
8472
ralloc_strdup(state, fields[i].name),
8473
var_mode);
8474
var->data.interpolation = fields[i].interpolation;
8475
var->data.centroid = fields[i].centroid;
8476
var->data.sample = fields[i].sample;
8477
var->data.patch = fields[i].patch;
8478
var->data.stream = qual_stream;
8479
var->data.location = fields[i].location;
8480
8481
if (fields[i].location != -1)
8482
var->data.explicit_location = true;
8483
8484
var->data.explicit_xfb_buffer = fields[i].explicit_xfb_buffer;
8485
var->data.xfb_buffer = fields[i].xfb_buffer;
8486
8487
if (fields[i].offset != -1)
8488
var->data.explicit_xfb_offset = true;
8489
var->data.offset = fields[i].offset;
8490
8491
var->init_interface_type(block_type);
8492
8493
if (var_mode == ir_var_shader_in || var_mode == ir_var_uniform)
8494
var->data.read_only = true;
8495
8496
/* Precision qualifiers do not have any meaning in Desktop GLSL */
8497
if (state->es_shader) {
8498
var->data.precision =
8499
select_gles_precision(fields[i].precision, fields[i].type,
8500
state, &loc);
8501
}
8502
8503
if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_INHERITED) {
8504
var->data.matrix_layout = matrix_layout == GLSL_MATRIX_LAYOUT_INHERITED
8505
? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR : matrix_layout;
8506
} else {
8507
var->data.matrix_layout = fields[i].matrix_layout;
8508
}
8509
8510
if (var->data.mode == ir_var_shader_storage)
8511
apply_memory_qualifiers(var, fields[i]);
8512
8513
/* Examine var name here since var may get deleted in the next call */
8514
bool var_is_gl_id = is_gl_identifier(var->name);
8515
8516
if (redeclaring_per_vertex) {
8517
bool is_redeclaration;
8518
var =
8519
get_variable_being_redeclared(&var, loc, state,
8520
true /* allow_all_redeclarations */,
8521
&is_redeclaration);
8522
if (!var_is_gl_id || !is_redeclaration) {
8523
_mesa_glsl_error(&loc, state,
8524
"redeclaration of gl_PerVertex can only "
8525
"include built-in variables");
8526
} else if (var->data.how_declared == ir_var_declared_normally) {
8527
_mesa_glsl_error(&loc, state,
8528
"`%s' has already been redeclared",
8529
var->name);
8530
} else {
8531
var->data.how_declared = ir_var_declared_in_block;
8532
var->reinit_interface_type(block_type);
8533
}
8534
continue;
8535
}
8536
8537
if (state->symbols->get_variable(var->name) != NULL)
8538
_mesa_glsl_error(&loc, state, "`%s' redeclared", var->name);
8539
8540
/* Propagate the "binding" keyword into this UBO/SSBO's fields.
8541
* The UBO declaration itself doesn't get an ir_variable unless it
8542
* has an instance name. This is ugly.
8543
*/
8544
if (this->layout.flags.q.explicit_binding) {
8545
apply_explicit_binding(state, &loc, var,
8546
var->get_interface_type(), &this->layout);
8547
}
8548
8549
if (var->type->is_unsized_array()) {
8550
if (var->is_in_shader_storage_block() &&
8551
is_unsized_array_last_element(var)) {
8552
var->data.from_ssbo_unsized_array = true;
8553
} else {
8554
/* From GLSL ES 3.10 spec, section 4.1.9 "Arrays":
8555
*
8556
* "If an array is declared as the last member of a shader storage
8557
* block and the size is not specified at compile-time, it is
8558
* sized at run-time. In all other cases, arrays are sized only
8559
* at compile-time."
8560
*
8561
* In desktop GLSL it is allowed to have unsized-arrays that are
8562
* not last, as long as we can determine that they are implicitly
8563
* sized.
8564
*/
8565
if (state->es_shader) {
8566
_mesa_glsl_error(&loc, state, "unsized array `%s' "
8567
"definition: only last member of a shader "
8568
"storage block can be defined as unsized "
8569
"array", fields[i].name);
8570
}
8571
}
8572
}
8573
8574
state->symbols->add_variable(var);
8575
instructions->push_tail(var);
8576
}
8577
8578
if (redeclaring_per_vertex && block_type != earlier_per_vertex) {
8579
/* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
8580
*
8581
* It is also a compilation error ... to redeclare a built-in
8582
* block and then use a member from that built-in block that was
8583
* not included in the redeclaration.
8584
*
8585
* This appears to be a clarification to the behaviour established
8586
* for gl_PerVertex by GLSL 1.50, therefore we implement this
8587
* behaviour regardless of GLSL version.
8588
*
8589
* To prevent the shader from using a member that was not included in
8590
* the redeclaration, we disable any ir_variables that are still
8591
* associated with the old declaration of gl_PerVertex (since we've
8592
* already updated all of the variables contained in the new
8593
* gl_PerVertex to point to it).
8594
*
8595
* As a side effect this will prevent
8596
* validate_intrastage_interface_blocks() from getting confused and
8597
* thinking there are conflicting definitions of gl_PerVertex in the
8598
* shader.
8599
*/
8600
foreach_in_list_safe(ir_instruction, node, instructions) {
8601
ir_variable *const var = node->as_variable();
8602
if (var != NULL &&
8603
var->get_interface_type() == earlier_per_vertex &&
8604
var->data.mode == var_mode) {
8605
if (var->data.how_declared == ir_var_declared_normally) {
8606
_mesa_glsl_error(&loc, state,
8607
"redeclaration of gl_PerVertex cannot "
8608
"follow a redeclaration of `%s'",
8609
var->name);
8610
}
8611
state->symbols->disable_variable(var->name);
8612
var->remove();
8613
}
8614
}
8615
}
8616
}
8617
8618
return NULL;
8619
}
8620
8621
8622
ir_rvalue *
8623
ast_tcs_output_layout::hir(exec_list *instructions,
8624
struct _mesa_glsl_parse_state *state)
8625
{
8626
YYLTYPE loc = this->get_location();
8627
8628
unsigned num_vertices;
8629
if (!state->out_qualifier->vertices->
8630
process_qualifier_constant(state, "vertices", &num_vertices,
8631
false)) {
8632
/* return here to stop cascading incorrect error messages */
8633
return NULL;
8634
}
8635
8636
/* If any shader outputs occurred before this declaration and specified an
8637
* array size, make sure the size they specified is consistent with the
8638
* primitive type.
8639
*/
8640
if (state->tcs_output_size != 0 && state->tcs_output_size != num_vertices) {
8641
_mesa_glsl_error(&loc, state,
8642
"this tessellation control shader output layout "
8643
"specifies %u vertices, but a previous output "
8644
"is declared with size %u",
8645
num_vertices, state->tcs_output_size);
8646
return NULL;
8647
}
8648
8649
state->tcs_output_vertices_specified = true;
8650
8651
/* If any shader outputs occurred before this declaration and did not
8652
* specify an array size, their size is determined now.
8653
*/
8654
foreach_in_list (ir_instruction, node, instructions) {
8655
ir_variable *var = node->as_variable();
8656
if (var == NULL || var->data.mode != ir_var_shader_out)
8657
continue;
8658
8659
/* Note: Not all tessellation control shader output are arrays. */
8660
if (!var->type->is_unsized_array() || var->data.patch)
8661
continue;
8662
8663
if (var->data.max_array_access >= (int)num_vertices) {
8664
_mesa_glsl_error(&loc, state,
8665
"this tessellation control shader output layout "
8666
"specifies %u vertices, but an access to element "
8667
"%u of output `%s' already exists", num_vertices,
8668
var->data.max_array_access, var->name);
8669
} else {
8670
var->type = glsl_type::get_array_instance(var->type->fields.array,
8671
num_vertices);
8672
}
8673
}
8674
8675
return NULL;
8676
}
8677
8678
8679
ir_rvalue *
8680
ast_gs_input_layout::hir(exec_list *instructions,
8681
struct _mesa_glsl_parse_state *state)
8682
{
8683
YYLTYPE loc = this->get_location();
8684
8685
/* Should have been prevented by the parser. */
8686
assert(!state->gs_input_prim_type_specified
8687
|| state->in_qualifier->prim_type == this->prim_type);
8688
8689
/* If any shader inputs occurred before this declaration and specified an
8690
* array size, make sure the size they specified is consistent with the
8691
* primitive type.
8692
*/
8693
unsigned num_vertices = vertices_per_prim(this->prim_type);
8694
if (state->gs_input_size != 0 && state->gs_input_size != num_vertices) {
8695
_mesa_glsl_error(&loc, state,
8696
"this geometry shader input layout implies %u vertices"
8697
" per primitive, but a previous input is declared"
8698
" with size %u", num_vertices, state->gs_input_size);
8699
return NULL;
8700
}
8701
8702
state->gs_input_prim_type_specified = true;
8703
8704
/* If any shader inputs occurred before this declaration and did not
8705
* specify an array size, their size is determined now.
8706
*/
8707
foreach_in_list(ir_instruction, node, instructions) {
8708
ir_variable *var = node->as_variable();
8709
if (var == NULL || var->data.mode != ir_var_shader_in)
8710
continue;
8711
8712
/* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
8713
* array; skip it.
8714
*/
8715
8716
if (var->type->is_unsized_array()) {
8717
if (var->data.max_array_access >= (int)num_vertices) {
8718
_mesa_glsl_error(&loc, state,
8719
"this geometry shader input layout implies %u"
8720
" vertices, but an access to element %u of input"
8721
" `%s' already exists", num_vertices,
8722
var->data.max_array_access, var->name);
8723
} else {
8724
var->type = glsl_type::get_array_instance(var->type->fields.array,
8725
num_vertices);
8726
}
8727
}
8728
}
8729
8730
return NULL;
8731
}
8732
8733
8734
ir_rvalue *
8735
ast_cs_input_layout::hir(exec_list *instructions,
8736
struct _mesa_glsl_parse_state *state)
8737
{
8738
YYLTYPE loc = this->get_location();
8739
8740
/* From the ARB_compute_shader specification:
8741
*
8742
* If the local size of the shader in any dimension is greater
8743
* than the maximum size supported by the implementation for that
8744
* dimension, a compile-time error results.
8745
*
8746
* It is not clear from the spec how the error should be reported if
8747
* the total size of the work group exceeds
8748
* MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to
8749
* report it at compile time as well.
8750
*/
8751
GLuint64 total_invocations = 1;
8752
unsigned qual_local_size[3];
8753
for (int i = 0; i < 3; i++) {
8754
8755
char *local_size_str = ralloc_asprintf(NULL, "invalid local_size_%c",
8756
'x' + i);
8757
/* Infer a local_size of 1 for unspecified dimensions */
8758
if (this->local_size[i] == NULL) {
8759
qual_local_size[i] = 1;
8760
} else if (!this->local_size[i]->
8761
process_qualifier_constant(state, local_size_str,
8762
&qual_local_size[i], false)) {
8763
ralloc_free(local_size_str);
8764
return NULL;
8765
}
8766
ralloc_free(local_size_str);
8767
8768
if (qual_local_size[i] > state->ctx->Const.MaxComputeWorkGroupSize[i]) {
8769
_mesa_glsl_error(&loc, state,
8770
"local_size_%c exceeds MAX_COMPUTE_WORK_GROUP_SIZE"
8771
" (%d)", 'x' + i,
8772
state->ctx->Const.MaxComputeWorkGroupSize[i]);
8773
break;
8774
}
8775
total_invocations *= qual_local_size[i];
8776
if (total_invocations >
8777
state->ctx->Const.MaxComputeWorkGroupInvocations) {
8778
_mesa_glsl_error(&loc, state,
8779
"product of local_sizes exceeds "
8780
"MAX_COMPUTE_WORK_GROUP_INVOCATIONS (%d)",
8781
state->ctx->Const.MaxComputeWorkGroupInvocations);
8782
break;
8783
}
8784
}
8785
8786
/* If any compute input layout declaration preceded this one, make sure it
8787
* was consistent with this one.
8788
*/
8789
if (state->cs_input_local_size_specified) {
8790
for (int i = 0; i < 3; i++) {
8791
if (state->cs_input_local_size[i] != qual_local_size[i]) {
8792
_mesa_glsl_error(&loc, state,
8793
"compute shader input layout does not match"
8794
" previous declaration");
8795
return NULL;
8796
}
8797
}
8798
}
8799
8800
/* The ARB_compute_variable_group_size spec says:
8801
*
8802
* If a compute shader including a *local_size_variable* qualifier also
8803
* declares a fixed local group size using the *local_size_x*,
8804
* *local_size_y*, or *local_size_z* qualifiers, a compile-time error
8805
* results
8806
*/
8807
if (state->cs_input_local_size_variable_specified) {
8808
_mesa_glsl_error(&loc, state,
8809
"compute shader can't include both a variable and a "
8810
"fixed local group size");
8811
return NULL;
8812
}
8813
8814
state->cs_input_local_size_specified = true;
8815
for (int i = 0; i < 3; i++)
8816
state->cs_input_local_size[i] = qual_local_size[i];
8817
8818
/* We may now declare the built-in constant gl_WorkGroupSize (see
8819
* builtin_variable_generator::generate_constants() for why we didn't
8820
* declare it earlier).
8821
*/
8822
ir_variable *var = new(state->symbols)
8823
ir_variable(glsl_type::uvec3_type, "gl_WorkGroupSize", ir_var_auto);
8824
var->data.how_declared = ir_var_declared_implicitly;
8825
var->data.read_only = true;
8826
instructions->push_tail(var);
8827
state->symbols->add_variable(var);
8828
ir_constant_data data;
8829
memset(&data, 0, sizeof(data));
8830
for (int i = 0; i < 3; i++)
8831
data.u[i] = qual_local_size[i];
8832
var->constant_value = new(var) ir_constant(glsl_type::uvec3_type, &data);
8833
var->constant_initializer =
8834
new(var) ir_constant(glsl_type::uvec3_type, &data);
8835
var->data.has_initializer = true;
8836
var->data.is_implicit_initializer = false;
8837
8838
return NULL;
8839
}
8840
8841
8842
static void
8843
detect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
8844
exec_list *instructions)
8845
{
8846
bool gl_FragColor_assigned = false;
8847
bool gl_FragData_assigned = false;
8848
bool gl_FragSecondaryColor_assigned = false;
8849
bool gl_FragSecondaryData_assigned = false;
8850
bool user_defined_fs_output_assigned = false;
8851
ir_variable *user_defined_fs_output = NULL;
8852
8853
/* It would be nice to have proper location information. */
8854
YYLTYPE loc;
8855
memset(&loc, 0, sizeof(loc));
8856
8857
foreach_in_list(ir_instruction, node, instructions) {
8858
ir_variable *var = node->as_variable();
8859
8860
if (!var || !var->data.assigned)
8861
continue;
8862
8863
if (strcmp(var->name, "gl_FragColor") == 0) {
8864
gl_FragColor_assigned = true;
8865
if (!var->constant_initializer && state->zero_init) {
8866
const ir_constant_data data = { { 0 } };
8867
var->data.has_initializer = true;
8868
var->data.is_implicit_initializer = true;
8869
var->constant_initializer = new(var) ir_constant(var->type, &data);
8870
}
8871
}
8872
else if (strcmp(var->name, "gl_FragData") == 0)
8873
gl_FragData_assigned = true;
8874
else if (strcmp(var->name, "gl_SecondaryFragColorEXT") == 0)
8875
gl_FragSecondaryColor_assigned = true;
8876
else if (strcmp(var->name, "gl_SecondaryFragDataEXT") == 0)
8877
gl_FragSecondaryData_assigned = true;
8878
else if (!is_gl_identifier(var->name)) {
8879
if (state->stage == MESA_SHADER_FRAGMENT &&
8880
var->data.mode == ir_var_shader_out) {
8881
user_defined_fs_output_assigned = true;
8882
user_defined_fs_output = var;
8883
}
8884
}
8885
}
8886
8887
/* From the GLSL 1.30 spec:
8888
*
8889
* "If a shader statically assigns a value to gl_FragColor, it
8890
* may not assign a value to any element of gl_FragData. If a
8891
* shader statically writes a value to any element of
8892
* gl_FragData, it may not assign a value to
8893
* gl_FragColor. That is, a shader may assign values to either
8894
* gl_FragColor or gl_FragData, but not both. Multiple shaders
8895
* linked together must also consistently write just one of
8896
* these variables. Similarly, if user declared output
8897
* variables are in use (statically assigned to), then the
8898
* built-in variables gl_FragColor and gl_FragData may not be
8899
* assigned to. These incorrect usages all generate compile
8900
* time errors."
8901
*/
8902
if (gl_FragColor_assigned && gl_FragData_assigned) {
8903
_mesa_glsl_error(&loc, state, "fragment shader writes to both "
8904
"`gl_FragColor' and `gl_FragData'");
8905
} else if (gl_FragColor_assigned && user_defined_fs_output_assigned) {
8906
_mesa_glsl_error(&loc, state, "fragment shader writes to both "
8907
"`gl_FragColor' and `%s'",
8908
user_defined_fs_output->name);
8909
} else if (gl_FragSecondaryColor_assigned && gl_FragSecondaryData_assigned) {
8910
_mesa_glsl_error(&loc, state, "fragment shader writes to both "
8911
"`gl_FragSecondaryColorEXT' and"
8912
" `gl_FragSecondaryDataEXT'");
8913
} else if (gl_FragColor_assigned && gl_FragSecondaryData_assigned) {
8914
_mesa_glsl_error(&loc, state, "fragment shader writes to both "
8915
"`gl_FragColor' and"
8916
" `gl_FragSecondaryDataEXT'");
8917
} else if (gl_FragData_assigned && gl_FragSecondaryColor_assigned) {
8918
_mesa_glsl_error(&loc, state, "fragment shader writes to both "
8919
"`gl_FragData' and"
8920
" `gl_FragSecondaryColorEXT'");
8921
} else if (gl_FragData_assigned && user_defined_fs_output_assigned) {
8922
_mesa_glsl_error(&loc, state, "fragment shader writes to both "
8923
"`gl_FragData' and `%s'",
8924
user_defined_fs_output->name);
8925
}
8926
8927
if ((gl_FragSecondaryColor_assigned || gl_FragSecondaryData_assigned) &&
8928
!state->EXT_blend_func_extended_enable) {
8929
_mesa_glsl_error(&loc, state,
8930
"Dual source blending requires EXT_blend_func_extended");
8931
}
8932
}
8933
8934
static void
8935
verify_subroutine_associated_funcs(struct _mesa_glsl_parse_state *state)
8936
{
8937
YYLTYPE loc;
8938
memset(&loc, 0, sizeof(loc));
8939
8940
/* Section 6.1.2 (Subroutines) of the GLSL 4.00 spec says:
8941
*
8942
* "A program will fail to compile or link if any shader
8943
* or stage contains two or more functions with the same
8944
* name if the name is associated with a subroutine type."
8945
*/
8946
8947
for (int i = 0; i < state->num_subroutines; i++) {
8948
unsigned definitions = 0;
8949
ir_function *fn = state->subroutines[i];
8950
/* Calculate number of function definitions with the same name */
8951
foreach_in_list(ir_function_signature, sig, &fn->signatures) {
8952
if (sig->is_defined) {
8953
if (++definitions > 1) {
8954
_mesa_glsl_error(&loc, state,
8955
"%s shader contains two or more function "
8956
"definitions with name `%s', which is "
8957
"associated with a subroutine type.\n",
8958
_mesa_shader_stage_to_string(state->stage),
8959
fn->name);
8960
return;
8961
}
8962
}
8963
}
8964
}
8965
}
8966
8967
static void
8968
remove_per_vertex_blocks(exec_list *instructions,
8969
_mesa_glsl_parse_state *state, ir_variable_mode mode)
8970
{
8971
/* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
8972
* if it exists in this shader type.
8973
*/
8974
const glsl_type *per_vertex = NULL;
8975
switch (mode) {
8976
case ir_var_shader_in:
8977
if (ir_variable *gl_in = state->symbols->get_variable("gl_in"))
8978
per_vertex = gl_in->get_interface_type();
8979
break;
8980
case ir_var_shader_out:
8981
if (ir_variable *gl_Position =
8982
state->symbols->get_variable("gl_Position")) {
8983
per_vertex = gl_Position->get_interface_type();
8984
}
8985
break;
8986
default:
8987
assert(!"Unexpected mode");
8988
break;
8989
}
8990
8991
/* If we didn't find a built-in gl_PerVertex interface block, then we don't
8992
* need to do anything.
8993
*/
8994
if (per_vertex == NULL)
8995
return;
8996
8997
/* If the interface block is used by the shader, then we don't need to do
8998
* anything.
8999
*/
9000
interface_block_usage_visitor v(mode, per_vertex);
9001
v.run(instructions);
9002
if (v.usage_found())
9003
return;
9004
9005
/* Remove any ir_variable declarations that refer to the interface block
9006
* we're removing.
9007
*/
9008
foreach_in_list_safe(ir_instruction, node, instructions) {
9009
ir_variable *const var = node->as_variable();
9010
if (var != NULL && var->get_interface_type() == per_vertex &&
9011
var->data.mode == mode) {
9012
state->symbols->disable_variable(var->name);
9013
var->remove();
9014
}
9015
}
9016
}
9017
9018
ir_rvalue *
9019
ast_warnings_toggle::hir(exec_list *,
9020
struct _mesa_glsl_parse_state *state)
9021
{
9022
state->warnings_enabled = enable;
9023
return NULL;
9024
}
9025
9026