2 * Copyright © 2009 Intel Corporation
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:
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
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.
26 #include "glsl_symbol_table.h"
27 #include "glsl_parser_extras.h"
28 #include "glsl_types.h"
29 #include "builtin_types.h"
30 #include "hash_table.h"
33 hash_table
*glsl_type::array_types
= NULL
;
36 add_types_to_symbol_table(glsl_symbol_table
*symtab
,
37 const struct glsl_type
*types
,
38 unsigned num_types
, bool warn
)
42 for (unsigned i
= 0; i
< num_types
; i
++) {
43 symtab
->add_type(types
[i
].name
, & types
[i
]);
49 generate_110_types(glsl_symbol_table
*symtab
)
51 add_types_to_symbol_table(symtab
, builtin_core_types
,
52 Elements(builtin_core_types
),
54 add_types_to_symbol_table(symtab
, builtin_structure_types
,
55 Elements(builtin_structure_types
),
57 add_types_to_symbol_table(symtab
, builtin_110_deprecated_structure_types
,
58 Elements(builtin_110_deprecated_structure_types
),
60 add_types_to_symbol_table(symtab
, & void_type
, 1, false);
65 generate_120_types(glsl_symbol_table
*symtab
)
67 generate_110_types(symtab
);
69 add_types_to_symbol_table(symtab
, builtin_120_types
,
70 Elements(builtin_120_types
), false);
75 generate_130_types(glsl_symbol_table
*symtab
)
77 generate_120_types(symtab
);
79 add_types_to_symbol_table(symtab
, builtin_130_types
,
80 Elements(builtin_130_types
), false);
85 generate_ARB_texture_rectangle_types(glsl_symbol_table
*symtab
, bool warn
)
87 add_types_to_symbol_table(symtab
, builtin_ARB_texture_rectangle_types
,
88 Elements(builtin_ARB_texture_rectangle_types
),
94 generate_EXT_texture_array_types(glsl_symbol_table
*symtab
, bool warn
)
96 add_types_to_symbol_table(symtab
, builtin_EXT_texture_array_types
,
97 Elements(builtin_EXT_texture_array_types
),
103 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*state
)
105 switch (state
->language_version
) {
107 generate_110_types(state
->symbols
);
110 generate_120_types(state
->symbols
);
113 generate_130_types(state
->symbols
);
120 if (state
->ARB_texture_rectangle_enable
) {
121 generate_ARB_texture_rectangle_types(state
->symbols
,
122 state
->ARB_texture_rectangle_warn
);
125 if (state
->EXT_texture_array_enable
&& state
->language_version
< 130) {
126 // These are already included in 130; don't create twice.
127 generate_EXT_texture_array_types(state
->symbols
,
128 state
->EXT_texture_array_warn
);
133 const glsl_type
*glsl_type::get_base_type() const
140 case GLSL_TYPE_FLOAT
:
151 glsl_type::generate_constructor(glsl_symbol_table
*symtab
) const
155 /* Generate the function name and add it to the symbol table.
157 ir_function
*const f
= new(ctx
) ir_function(name
);
159 bool added
= symtab
->add_function(name
, f
);
162 ir_function_signature
*const sig
= new(ctx
) ir_function_signature(this);
163 f
->add_signature(sig
);
165 ir_variable
**declarations
=
166 (ir_variable
**) malloc(sizeof(ir_variable
*) * this->length
);
167 for (unsigned i
= 0; i
< length
; i
++) {
168 char *const param_name
= (char *) malloc(10);
170 snprintf(param_name
, 10, "p%08X", i
);
172 ir_variable
*var
= (this->base_type
== GLSL_TYPE_ARRAY
)
173 ? new(ctx
) ir_variable(fields
.array
, param_name
)
174 : new(ctx
) ir_variable(fields
.structure
[i
].type
, param_name
);
176 var
->mode
= ir_var_in
;
177 declarations
[i
] = var
;
178 sig
->parameters
.push_tail(var
);
181 /* Generate the body of the constructor. The body assigns each of the
182 * parameters to a portion of a local variable called __retval that has
183 * the same type as the constructor. After initializing __retval,
184 * __retval is returned.
186 ir_variable
*retval
= new(ctx
) ir_variable(this, "__retval");
187 sig
->body
.push_tail(retval
);
189 for (unsigned i
= 0; i
< length
; i
++) {
190 ir_dereference
*const lhs
= (this->base_type
== GLSL_TYPE_ARRAY
)
191 ? (ir_dereference
*) new(ctx
) ir_dereference_array(retval
,
192 new(ctx
) ir_constant(i
))
193 : (ir_dereference
*) new(ctx
) ir_dereference_record(retval
,
194 fields
.structure
[i
].name
);
196 ir_dereference
*const rhs
= new(ctx
) ir_dereference_variable(declarations
[i
]);
197 ir_instruction
*const assign
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
199 sig
->body
.push_tail(assign
);
204 ir_dereference
*const retref
= new(ctx
) ir_dereference_variable(retval
);
205 ir_instruction
*const inst
= new(ctx
) ir_return(retref
);
206 sig
->body
.push_tail(inst
);
213 * Generate the function intro for a constructor
215 * \param type Data type to be constructed
216 * \param count Number of parameters to this concrete constructor. Most
217 * types have at least two constructors. One will take a
218 * single scalar parameter and the other will take "N"
220 * \param parameters Storage for the list of parameters. These are
221 * typically stored in an \c ir_function_signature.
222 * \param declarations Pointers to the variable declarations for the function
223 * parameters. These are used later to avoid having to use
226 static ir_function_signature
*
227 generate_constructor_intro(void *ctx
,
228 const glsl_type
*type
, unsigned parameter_count
,
229 ir_variable
**declarations
)
231 /* Names of parameters used in vector and matrix constructors
233 static const char *const names
[] = {
234 "a", "b", "c", "d", "e", "f", "g", "h",
235 "i", "j", "k", "l", "m", "n", "o", "p",
238 assert(parameter_count
<= Elements(names
));
240 const glsl_type
*const parameter_type
= type
->get_base_type();
242 ir_function_signature
*const signature
= new(ctx
) ir_function_signature(type
);
244 for (unsigned i
= 0; i
< parameter_count
; i
++) {
245 ir_variable
*var
= new(ctx
) ir_variable(parameter_type
, names
[i
]);
247 var
->mode
= ir_var_in
;
248 signature
->parameters
.push_tail(var
);
250 declarations
[i
] = var
;
253 ir_variable
*retval
= new(ctx
) ir_variable(type
, "__retval");
254 signature
->body
.push_tail(retval
);
256 declarations
[16] = retval
;
262 * Generate the body of a vector constructor that takes a single scalar
265 generate_vec_body_from_scalar(void *ctx
,
266 exec_list
*instructions
,
267 ir_variable
**declarations
)
269 ir_instruction
*inst
;
271 /* Generate a single assignment of the parameter to __retval.x and return
272 * __retval.xxxx for however many vector components there are.
274 ir_dereference
*const lhs_ref
=
275 new(ctx
) ir_dereference_variable(declarations
[16]);
276 ir_dereference
*const rhs
= new(ctx
) ir_dereference_variable(declarations
[0]);
278 ir_swizzle
*lhs
= new(ctx
) ir_swizzle(lhs_ref
, 0, 0, 0, 0, 1);
280 inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
281 instructions
->push_tail(inst
);
283 ir_dereference
*const retref
= new(ctx
) ir_dereference_variable(declarations
[16]);
285 ir_swizzle
*retval
= new(ctx
) ir_swizzle(retref
, 0, 0, 0, 0,
286 declarations
[16]->type
->vector_elements
);
288 inst
= new(ctx
) ir_return(retval
);
289 instructions
->push_tail(inst
);
294 * Generate the body of a vector constructor that takes multiple scalars
297 generate_vec_body_from_N_scalars(void *ctx
,
298 exec_list
*instructions
,
299 ir_variable
**declarations
)
301 ir_instruction
*inst
;
302 const glsl_type
*const vec_type
= declarations
[16]->type
;
304 /* Generate an assignment of each parameter to a single component of
305 * __retval.x and return __retval.
307 for (unsigned i
= 0; i
< vec_type
->vector_elements
; i
++) {
308 ir_dereference
*const lhs_ref
=
309 new(ctx
) ir_dereference_variable(declarations
[16]);
310 ir_dereference
*const rhs
= new(ctx
) ir_dereference_variable(declarations
[i
]);
312 ir_swizzle
*lhs
= new(ctx
) ir_swizzle(lhs_ref
, i
, 0, 0, 0, 1);
314 inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
315 instructions
->push_tail(inst
);
318 ir_dereference
*retval
= new(ctx
) ir_dereference_variable(declarations
[16]);
320 inst
= new(ctx
) ir_return(retval
);
321 instructions
->push_tail(inst
);
326 * Generate the body of a matrix constructor that takes a single scalar
329 generate_mat_body_from_scalar(void *ctx
,
330 exec_list
*instructions
,
331 ir_variable
**declarations
)
333 ir_instruction
*inst
;
335 /* Generate an assignment of the parameter to the X component of a
336 * temporary vector. Set the remaining fields of the vector to 0. The
337 * size of the vector is equal to the number of rows of the matrix.
339 * Set each column of the matrix to a successive "rotation" of the
340 * temporary vector. This fills the matrix with 0s, but writes the single
341 * scalar along the matrix's diagonal.
343 * For a mat4x3, this is equivalent to:
350 * __retval[0] = tmp.xyy;
351 * __retval[1] = tmp.yxy;
352 * __retval[2] = tmp.yyx;
353 * __retval[3] = tmp.yyy;
355 const glsl_type
*const column_type
= declarations
[16]->type
->column_type();
356 const glsl_type
*const row_type
= declarations
[16]->type
->row_type();
358 ir_variable
*const column
= new(ctx
) ir_variable(column_type
, "v");
360 instructions
->push_tail(column
);
362 ir_dereference
*const lhs_ref
= new(ctx
) ir_dereference_variable(column
);
363 ir_dereference
*const rhs
= new(ctx
) ir_dereference_variable(declarations
[0]);
365 ir_swizzle
*lhs
= new(ctx
) ir_swizzle(lhs_ref
, 0, 0, 0, 0, 1);
367 inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
368 instructions
->push_tail(inst
);
370 for (unsigned i
= 1; i
< column_type
->vector_elements
; i
++) {
371 ir_dereference
*const lhs_ref
= new(ctx
) ir_dereference_variable(column
);
372 ir_constant
*const zero
= new(ctx
) ir_constant(0.0f
);
374 ir_swizzle
*lhs
= new(ctx
) ir_swizzle(lhs_ref
, i
, 0, 0, 0, 1);
376 inst
= new(ctx
) ir_assignment(lhs
, zero
, NULL
);
377 instructions
->push_tail(inst
);
381 for (unsigned i
= 0; i
< row_type
->vector_elements
; i
++) {
382 static const unsigned swiz
[] = { 1, 1, 1, 0, 1, 1, 1 };
383 ir_dereference
*const rhs_ref
= new(ctx
) ir_dereference_variable(column
);
385 /* This will be .xyyy when i=0, .yxyy when i=1, etc.
387 ir_swizzle
*rhs
= new(ctx
) ir_swizzle(rhs_ref
, swiz
[3 - i
], swiz
[4 - i
],
388 swiz
[5 - i
], swiz
[6 - i
],
389 column_type
->vector_elements
);
391 ir_constant
*const idx
= new(ctx
) ir_constant(int(i
));
392 ir_dereference
*const lhs
=
393 new(ctx
) ir_dereference_array(declarations
[16], idx
);
395 inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
396 instructions
->push_tail(inst
);
399 ir_dereference
*const retval
= new(ctx
) ir_dereference_variable(declarations
[16]);
400 inst
= new(ctx
) ir_return(retval
);
401 instructions
->push_tail(inst
);
406 * Generate the body of a vector constructor that takes multiple scalars
409 generate_mat_body_from_N_scalars(void *ctx
,
410 exec_list
*instructions
,
411 ir_variable
**declarations
)
413 ir_instruction
*inst
;
414 const glsl_type
*const row_type
= declarations
[16]->type
->row_type();
415 const glsl_type
*const column_type
= declarations
[16]->type
->column_type();
417 /* Generate an assignment of each parameter to a single component of
418 * of a particular column of __retval and return __retval.
420 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++) {
421 for (unsigned j
= 0; j
< row_type
->vector_elements
; j
++) {
422 ir_constant
*row_index
= new(ctx
) ir_constant(int(i
));
423 ir_dereference
*const row_access
=
424 new(ctx
) ir_dereference_array(declarations
[16], row_index
);
426 ir_swizzle
*component_access
= new(ctx
) ir_swizzle(row_access
,
429 const unsigned param
= (i
* row_type
->vector_elements
) + j
;
430 ir_dereference
*const rhs
=
431 new(ctx
) ir_dereference_variable(declarations
[param
]);
433 inst
= new(ctx
) ir_assignment(component_access
, rhs
, NULL
);
434 instructions
->push_tail(inst
);
438 ir_dereference
*retval
= new(ctx
) ir_dereference_variable(declarations
[16]);
440 inst
= new(ctx
) ir_return(retval
);
441 instructions
->push_tail(inst
);
446 * Generate the constructors for a set of GLSL types
448 * Constructor implementations are added to \c instructions, and the symbols
449 * are added to \c symtab.
452 generate_constructor(glsl_symbol_table
*symtab
, const struct glsl_type
*types
,
453 unsigned num_types
, exec_list
*instructions
)
456 ir_variable
*declarations
[17];
458 for (unsigned i
= 0; i
< num_types
; i
++) {
459 /* Only numeric and boolean vectors and matrices get constructors here.
460 * Structures need to be handled elsewhere. It is expected that scalar
461 * constructors are never actually called, so they are not generated.
463 if (!types
[i
].is_numeric() && !types
[i
].is_boolean())
466 if (types
[i
].is_scalar())
469 /* Generate the function block, add it to the symbol table, and emit it.
471 ir_function
*const f
= new(ctx
) ir_function(types
[i
].name
);
473 bool added
= symtab
->add_function(types
[i
].name
, f
);
476 instructions
->push_tail(f
);
478 /* Each type has several basic constructors. The total number of forms
479 * depends on the derived type.
481 * Vectors: 1 scalar, N scalars
482 * Matrices: 1 scalar, NxM scalars
484 * Several possible types of constructors are not included in this list.
486 * Scalar constructors are not included. The expectation is that the
487 * IR generator won't actually generate these as constructor calls. The
488 * expectation is that it will just generate the necessary type
491 * Matrix contructors from matrices are also not included. The
492 * expectation is that the IR generator will generate a call to the
493 * appropriate from-scalars constructor.
495 ir_function_signature
*const sig
=
496 generate_constructor_intro(ctx
, &types
[i
], 1, declarations
);
497 f
->add_signature(sig
);
499 if (types
[i
].is_vector()) {
500 generate_vec_body_from_scalar(ctx
, &sig
->body
, declarations
);
502 ir_function_signature
*const vec_sig
=
503 generate_constructor_intro(ctx
,
504 &types
[i
], types
[i
].vector_elements
,
506 f
->add_signature(vec_sig
);
508 generate_vec_body_from_N_scalars(ctx
, &vec_sig
->body
, declarations
);
510 assert(types
[i
].is_matrix());
512 generate_mat_body_from_scalar(ctx
, &sig
->body
, declarations
);
514 ir_function_signature
*const mat_sig
=
515 generate_constructor_intro(ctx
,
517 (types
[i
].vector_elements
518 * types
[i
].matrix_columns
),
520 f
->add_signature(mat_sig
);
522 generate_mat_body_from_N_scalars(ctx
, &mat_sig
->body
, declarations
);
529 generate_110_constructors(glsl_symbol_table
*symtab
, exec_list
*instructions
)
531 generate_constructor(symtab
, builtin_core_types
,
532 Elements(builtin_core_types
), instructions
);
537 generate_120_constructors(glsl_symbol_table
*symtab
, exec_list
*instructions
)
539 generate_110_constructors(symtab
, instructions
);
541 generate_constructor(symtab
, builtin_120_types
,
542 Elements(builtin_120_types
), instructions
);
547 generate_130_constructors(glsl_symbol_table
*symtab
, exec_list
*instructions
)
549 generate_120_constructors(symtab
, instructions
);
551 generate_constructor(symtab
, builtin_130_types
,
552 Elements(builtin_130_types
), instructions
);
557 _mesa_glsl_initialize_constructors(exec_list
*instructions
,
558 struct _mesa_glsl_parse_state
*state
)
560 switch (state
->language_version
) {
562 generate_110_constructors(state
->symbols
, instructions
);
565 generate_120_constructors(state
->symbols
, instructions
);
568 generate_130_constructors(state
->symbols
, instructions
);
577 glsl_type::glsl_type(void *ctx
, const glsl_type
*array
, unsigned length
) :
578 base_type(GLSL_TYPE_ARRAY
),
579 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
581 vector_elements(0), matrix_columns(0),
582 name(NULL
), length(length
)
584 this->fields
.array
= array
;
586 /* Allow a maximum of 10 characters for the array size. This is enough
587 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
590 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
591 char *const n
= (char *) talloc_size(ctx
, name_length
);
594 snprintf(n
, name_length
, "%s[]", array
->name
);
596 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
603 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
605 if (base_type
== GLSL_TYPE_VOID
)
608 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
611 /* Treat GLSL vectors as Nx1 matrices.
616 return uint_type
+ (rows
- 1);
618 return int_type
+ (rows
- 1);
619 case GLSL_TYPE_FLOAT
:
620 return float_type
+ (rows
- 1);
622 return bool_type
+ (rows
- 1);
627 if ((base_type
!= GLSL_TYPE_FLOAT
) || (rows
== 1))
630 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
631 * combinations are valid:
639 #define IDX(c,r) (((c-1)*3) + (r-1))
641 switch (IDX(columns
, rows
)) {
642 case IDX(2,2): return mat2_type
;
643 case IDX(2,3): return mat2x3_type
;
644 case IDX(2,4): return mat2x4_type
;
645 case IDX(3,2): return mat3x2_type
;
646 case IDX(3,3): return mat3_type
;
647 case IDX(3,4): return mat3x4_type
;
648 case IDX(4,2): return mat4x2_type
;
649 case IDX(4,3): return mat4x3_type
;
650 case IDX(4,4): return mat4_type
;
651 default: return error_type
;
655 assert(!"Should not get here.");
661 glsl_type::array_key_compare(const void *a
, const void *b
)
663 const glsl_type
*const key1
= (glsl_type
*) a
;
664 const glsl_type
*const key2
= (glsl_type
*) b
;
666 /* Return zero is the types match (there is zero difference) or non-zero
669 return ((key1
->fields
.array
== key2
->fields
.array
)
670 && (key1
->length
== key2
->length
)) ? 0 : 1;
675 glsl_type::array_key_hash(const void *a
)
677 const glsl_type
*const key
= (glsl_type
*) a
;
689 return hash_table_string_hash(& hash_key
);
694 glsl_type::get_array_instance(void *ctx
, const glsl_type
*base
,
697 const glsl_type
key(ctx
, base
, array_size
);
699 if (array_types
== NULL
) {
700 array_types
= hash_table_ctor(64, array_key_hash
, array_key_compare
);
703 const glsl_type
*t
= (glsl_type
*) hash_table_find(array_types
, & key
);
705 t
= new(ctx
) glsl_type(ctx
, base
, array_size
);
707 hash_table_insert(array_types
, (void *) t
, t
);
710 assert(t
->base_type
== GLSL_TYPE_ARRAY
);
711 assert(t
->length
== array_size
);
712 assert(t
->fields
.array
== base
);
719 glsl_type::field_type(const char *name
) const
721 if (this->base_type
!= GLSL_TYPE_STRUCT
)
724 for (unsigned i
= 0; i
< this->length
; i
++) {
725 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
726 return this->fields
.structure
[i
].type
;
734 glsl_type::field_index(const char *name
) const
736 if (this->base_type
!= GLSL_TYPE_STRUCT
)
739 for (unsigned i
= 0; i
< this->length
; i
++) {
740 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
749 glsl_type::component_slots() const
751 switch (this->base_type
) {
754 case GLSL_TYPE_FLOAT
:
756 return this->components();
758 case GLSL_TYPE_STRUCT
: {
761 for (unsigned i
= 0; i
< this->length
; i
++)
762 size
+= this->fields
.structure
[i
].type
->component_slots();
767 case GLSL_TYPE_ARRAY
:
768 return this->length
* this->fields
.array
->component_slots();