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 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state)
96 switch (state->language_version) {
98 generate_110_types(state->symbols);
101 generate_120_types(state->symbols);
104 generate_130_types(state->symbols);
111 if (state->ARB_texture_rectangle_enable) {
112 generate_ARB_texture_rectangle_types(state->symbols,
113 state->ARB_texture_rectangle_warn);
118 const glsl_type *glsl_type::get_base_type() const
125 case GLSL_TYPE_FLOAT:
136 glsl_type::generate_constructor(glsl_symbol_table *symtab) const
138 /* Generate the function name and add it to the symbol table.
140 ir_function *const f = new ir_function(name);
142 bool added = symtab->add_function(name, f);
145 ir_function_signature *const sig = new ir_function_signature(this);
146 f->add_signature(sig);
148 ir_variable **declarations =
149 (ir_variable **) malloc(sizeof(ir_variable *) * this->length);
150 for (unsigned i = 0; i < length; i++) {
151 char *const param_name = (char *) malloc(10);
153 snprintf(param_name, 10, "p%08X", i);
155 ir_variable *var = (this->base_type == GLSL_TYPE_ARRAY)
156 ? new ir_variable(fields.array, param_name)
157 : new ir_variable(fields.structure[i].type, param_name);
159 var->mode = ir_var_in;
160 declarations[i] = var;
161 sig->parameters.push_tail(var);
164 /* Generate the body of the constructor. The body assigns each of the
165 * parameters to a portion of a local variable called __retval that has
166 * the same type as the constructor. After initializing __retval,
167 * __retval is returned.
169 ir_variable *retval = new ir_variable(this, "__retval");
170 sig->body.push_tail(retval);
172 for (unsigned i = 0; i < length; i++) {
173 ir_dereference *const lhs = (this->base_type == GLSL_TYPE_ARRAY)
174 ? (ir_dereference *) new ir_dereference_array(retval, new ir_constant(i))
175 : (ir_dereference *) new ir_dereference_record(retval, fields.structure[i].name);
177 ir_dereference *const rhs = new ir_dereference_variable(declarations[i]);
178 ir_instruction *const assign = new ir_assignment(lhs, rhs, NULL);
180 sig->body.push_tail(assign);
185 ir_dereference *const retref = new ir_dereference_variable(retval);
186 ir_instruction *const inst = new ir_return(retref);
187 sig->body.push_tail(inst);
194 * Generate the function intro for a constructor
196 * \param type Data type to be constructed
197 * \param count Number of parameters to this concrete constructor. Most
198 * types have at least two constructors. One will take a
199 * single scalar parameter and the other will take "N"
201 * \param parameters Storage for the list of parameters. These are
202 * typically stored in an \c ir_function_signature.
203 * \param declarations Pointers to the variable declarations for the function
204 * parameters. These are used later to avoid having to use
207 static ir_function_signature *
208 generate_constructor_intro(const glsl_type *type, unsigned parameter_count,
209 ir_variable **declarations)
211 /* Names of parameters used in vector and matrix constructors
213 static const char *const names[] = {
214 "a", "b", "c", "d", "e", "f", "g", "h",
215 "i", "j", "k", "l", "m", "n", "o", "p",
218 assert(parameter_count <= Elements(names));
220 const glsl_type *const parameter_type = type->get_base_type();
222 ir_function_signature *const signature = new ir_function_signature(type);
224 for (unsigned i = 0; i < parameter_count; i++) {
225 ir_variable *var = new ir_variable(parameter_type, names[i]);
227 var->mode = ir_var_in;
228 signature->parameters.push_tail(var);
230 declarations[i] = var;
233 ir_variable *retval = new ir_variable(type, "__retval");
234 signature->body.push_tail(retval);
236 declarations[16] = retval;
242 * Generate the body of a vector constructor that takes a single scalar
245 generate_vec_body_from_scalar(exec_list *instructions,
246 ir_variable **declarations)
248 ir_instruction *inst;
250 /* Generate a single assignment of the parameter to __retval.x and return
251 * __retval.xxxx for however many vector components there are.
253 ir_dereference *const lhs_ref =
254 new ir_dereference_variable(declarations[16]);
255 ir_dereference *const rhs = new ir_dereference_variable(declarations[0]);
257 ir_swizzle *lhs = new ir_swizzle(lhs_ref, 0, 0, 0, 0, 1);
259 inst = new ir_assignment(lhs, rhs, NULL);
260 instructions->push_tail(inst);
262 ir_dereference *const retref = new ir_dereference_variable(declarations[16]);
264 ir_swizzle *retval = new ir_swizzle(retref, 0, 0, 0, 0,
265 declarations[16]->type->vector_elements);
267 inst = new ir_return(retval);
268 instructions->push_tail(inst);
273 * Generate the body of a vector constructor that takes multiple scalars
276 generate_vec_body_from_N_scalars(exec_list *instructions,
277 ir_variable **declarations)
279 ir_instruction *inst;
280 const glsl_type *const vec_type = declarations[16]->type;
283 /* Generate an assignment of each parameter to a single component of
284 * __retval.x and return __retval.
286 for (unsigned i = 0; i < vec_type->vector_elements; i++) {
287 ir_dereference *const lhs_ref =
288 new ir_dereference_variable(declarations[16]);
289 ir_dereference *const rhs = new ir_dereference_variable(declarations[i]);
291 ir_swizzle *lhs = new ir_swizzle(lhs_ref, i, 0, 0, 0, 1);
293 inst = new ir_assignment(lhs, rhs, NULL);
294 instructions->push_tail(inst);
297 ir_dereference *retval = new ir_dereference_variable(declarations[16]);
299 inst = new ir_return(retval);
300 instructions->push_tail(inst);
305 * Generate the body of a matrix constructor that takes a single scalar
308 generate_mat_body_from_scalar(exec_list *instructions,
309 ir_variable **declarations)
311 ir_instruction *inst;
313 /* Generate an assignment of the parameter to the X component of a
314 * temporary vector. Set the remaining fields of the vector to 0. The
315 * size of the vector is equal to the number of rows of the matrix.
317 * Set each column of the matrix to a successive "rotation" of the
318 * temporary vector. This fills the matrix with 0s, but writes the single
319 * scalar along the matrix's diagonal.
321 * For a mat4x3, this is equivalent to:
328 * __retval[0] = tmp.xyy;
329 * __retval[1] = tmp.yxy;
330 * __retval[2] = tmp.yyx;
331 * __retval[3] = tmp.yyy;
333 const glsl_type *const column_type = declarations[16]->type->column_type();
334 const glsl_type *const row_type = declarations[16]->type->row_type();
335 ir_variable *const column = new ir_variable(column_type, "v");
337 instructions->push_tail(column);
339 ir_dereference *const lhs_ref = new ir_dereference_variable(column);
340 ir_dereference *const rhs = new ir_dereference_variable(declarations[0]);
342 ir_swizzle *lhs = new ir_swizzle(lhs_ref, 0, 0, 0, 0, 1);
344 inst = new ir_assignment(lhs, rhs, NULL);
345 instructions->push_tail(inst);
347 const float z = 0.0f;
348 ir_constant *const zero = new ir_constant(glsl_type::float_type, &z);
350 for (unsigned i = 1; i < column_type->vector_elements; i++) {
351 ir_dereference *const lhs_ref = new ir_dereference_variable(column);
353 ir_swizzle *lhs = new ir_swizzle(lhs_ref, i, 0, 0, 0, 1);
355 inst = new ir_assignment(lhs, zero, NULL);
356 instructions->push_tail(inst);
360 for (unsigned i = 0; i < row_type->vector_elements; i++) {
361 static const unsigned swiz[] = { 1, 1, 1, 0, 1, 1, 1 };
362 ir_dereference *const rhs_ref = new ir_dereference_variable(column);
364 /* This will be .xyyy when i=0, .yxyy when i=1, etc.
366 ir_swizzle *rhs = new ir_swizzle(rhs_ref, swiz[3 - i], swiz[4 - i],
367 swiz[5 - i], swiz[6 - i],
368 column_type->vector_elements);
370 ir_constant *const idx = new ir_constant(glsl_type::int_type, &i);
371 ir_dereference *const lhs =
372 new ir_dereference_array(declarations[16], idx);
374 inst = new ir_assignment(lhs, rhs, NULL);
375 instructions->push_tail(inst);
378 ir_dereference *const retval = new ir_dereference_variable(declarations[16]);
379 inst = new ir_return(retval);
380 instructions->push_tail(inst);
385 * Generate the body of a vector constructor that takes multiple scalars
388 generate_mat_body_from_N_scalars(exec_list *instructions,
389 ir_variable **declarations)
391 ir_instruction *inst;
392 const glsl_type *const row_type = declarations[16]->type->row_type();
393 const glsl_type *const column_type = declarations[16]->type->column_type();
396 /* Generate an assignment of each parameter to a single component of
397 * of a particular column of __retval and return __retval.
399 for (unsigned i = 0; i < column_type->vector_elements; i++) {
400 for (unsigned j = 0; j < row_type->vector_elements; j++) {
401 ir_constant *row_index = new ir_constant(glsl_type::int_type, &i);
402 ir_dereference *const row_access =
403 new ir_dereference_array(declarations[16], row_index);
405 ir_swizzle *component_access = new ir_swizzle(row_access,
408 const unsigned param = (i * row_type->vector_elements) + j;
409 ir_dereference *const rhs =
410 new ir_dereference_variable(declarations[param]);
412 inst = new ir_assignment(component_access, rhs, NULL);
413 instructions->push_tail(inst);
417 ir_dereference *retval = new ir_dereference_variable(declarations[16]);
419 inst = new ir_return(retval);
420 instructions->push_tail(inst);
425 * Generate the constructors for a set of GLSL types
427 * Constructor implementations are added to \c instructions, and the symbols
428 * are added to \c symtab.
431 generate_constructor(glsl_symbol_table *symtab, const struct glsl_type *types,
432 unsigned num_types, exec_list *instructions)
434 ir_variable *declarations[17];
436 for (unsigned i = 0; i < num_types; i++) {
437 /* Only numeric and boolean vectors and matrices get constructors here.
438 * Structures need to be handled elsewhere. It is expected that scalar
439 * constructors are never actually called, so they are not generated.
441 if (!types[i].is_numeric() && !types[i].is_boolean())
444 if (types[i].is_scalar())
447 /* Generate the function block, add it to the symbol table, and emit it.
449 ir_function *const f = new ir_function(types[i].name);
451 bool added = symtab->add_function(types[i].name, f);
454 instructions->push_tail(f);
456 /* Each type has several basic constructors. The total number of forms
457 * depends on the derived type.
459 * Vectors: 1 scalar, N scalars
460 * Matrices: 1 scalar, NxM scalars
462 * Several possible types of constructors are not included in this list.
464 * Scalar constructors are not included. The expectation is that the
465 * IR generator won't actually generate these as constructor calls. The
466 * expectation is that it will just generate the necessary type
469 * Matrix contructors from matrices are also not included. The
470 * expectation is that the IR generator will generate a call to the
471 * appropriate from-scalars constructor.
473 ir_function_signature *const sig =
474 generate_constructor_intro(&types[i], 1, declarations);
475 f->add_signature(sig);
477 if (types[i].is_vector()) {
478 generate_vec_body_from_scalar(&sig->body, declarations);
480 ir_function_signature *const vec_sig =
481 generate_constructor_intro(&types[i], types[i].vector_elements,
483 f->add_signature(vec_sig);
485 generate_vec_body_from_N_scalars(&vec_sig->body, declarations);
487 assert(types[i].is_matrix());
489 generate_mat_body_from_scalar(&sig->body, declarations);
491 ir_function_signature *const mat_sig =
492 generate_constructor_intro(&types[i],
493 (types[i].vector_elements
494 * types[i].matrix_columns),
496 f->add_signature(mat_sig);
498 generate_mat_body_from_N_scalars(&mat_sig->body, declarations);
505 generate_110_constructors(glsl_symbol_table *symtab, exec_list *instructions)
507 generate_constructor(symtab, builtin_core_types,
508 Elements(builtin_core_types), instructions);
513 generate_120_constructors(glsl_symbol_table *symtab, exec_list *instructions)
515 generate_110_constructors(symtab, instructions);
517 generate_constructor(symtab, builtin_120_types,
518 Elements(builtin_120_types), instructions);
523 generate_130_constructors(glsl_symbol_table *symtab, exec_list *instructions)
525 generate_120_constructors(symtab, instructions);
527 generate_constructor(symtab, builtin_130_types,
528 Elements(builtin_130_types), instructions);
533 _mesa_glsl_initialize_constructors(exec_list *instructions,
534 struct _mesa_glsl_parse_state *state)
536 switch (state->language_version) {
538 generate_110_constructors(state->symbols, instructions);
541 generate_120_constructors(state->symbols, instructions);
544 generate_130_constructors(state->symbols, instructions);
553 glsl_type::glsl_type(const glsl_type *array, unsigned length) :
554 base_type(GLSL_TYPE_ARRAY),
555 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
557 vector_elements(0), matrix_columns(0),
558 name(NULL), length(length)
560 this->fields.array = array;
562 /* Allow a maximum of 10 characters for the array size. This is enough
563 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
566 const unsigned name_length = strlen(array->name) + 10 + 3;
567 char *const n = (char *) malloc(name_length);
570 snprintf(n, name_length, "%s[]", array->name);
572 snprintf(n, name_length, "%s[%u]", array->name, length);
579 glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
581 if (base_type == GLSL_TYPE_VOID)
584 if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4))
587 /* Treat GLSL vectors as Nx1 matrices.
592 return uint_type + (rows - 1);
594 return int_type + (rows - 1);
595 case GLSL_TYPE_FLOAT:
596 return float_type + (rows - 1);
598 return bool_type + (rows - 1);
603 if ((base_type != GLSL_TYPE_FLOAT) || (rows == 1))
606 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
607 * combinations are valid:
615 #define IDX(c,r) (((c-1)*3) + (r-1))
617 switch (IDX(columns, rows)) {
618 case IDX(2,2): return mat2_type;
619 case IDX(2,3): return mat2x3_type;
620 case IDX(2,4): return mat2x4_type;
621 case IDX(3,2): return mat3x2_type;
622 case IDX(3,3): return mat3_type;
623 case IDX(3,4): return mat3x4_type;
624 case IDX(4,2): return mat4x2_type;
625 case IDX(4,3): return mat4x3_type;
626 case IDX(4,4): return mat4_type;
627 default: return error_type;
631 assert(!"Should not get here.");
637 glsl_type::array_key_compare(const void *a, const void *b)
639 const glsl_type *const key1 = (glsl_type *) a;
640 const glsl_type *const key2 = (glsl_type *) b;
642 /* Return zero is the types match (there is zero difference) or non-zero
645 return ((key1->fields.array == key2->fields.array)
646 && (key1->length == key2->length)) ? 0 : 1;
651 glsl_type::array_key_hash(const void *a)
653 const glsl_type *const key = (glsl_type *) a;
665 return hash_table_string_hash(& hash_key);
670 glsl_type::get_array_instance(const glsl_type *base, unsigned array_size)
672 const glsl_type key(base, array_size);
674 if (array_types == NULL) {
675 array_types = hash_table_ctor(64, array_key_hash, array_key_compare);
678 const glsl_type *t = (glsl_type *) hash_table_find(array_types, & key);
680 t = new glsl_type(base, array_size);
682 hash_table_insert(array_types, (void *) t, t);
685 assert(t->base_type == GLSL_TYPE_ARRAY);
686 assert(t->length == array_size);
687 assert(t->fields.array == base);
694 glsl_type::field_type(const char *name) const
696 if (this->base_type != GLSL_TYPE_STRUCT)
699 for (unsigned i = 0; i < this->length; i++) {
700 if (strcmp(name, this->fields.structure[i].name) == 0)
701 return this->fields.structure[i].type;