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
153 /* Generate the function name and add it to the symbol table.
155 ir_function
*const f
= new ir_function(name
);
157 bool added
= symtab
->add_function(name
, f
);
160 ir_function_signature
*const sig
= new ir_function_signature(this);
161 f
->add_signature(sig
);
163 ir_variable
**declarations
=
164 (ir_variable
**) malloc(sizeof(ir_variable
*) * this->length
);
165 for (unsigned i
= 0; i
< length
; i
++) {
166 char *const param_name
= (char *) malloc(10);
168 snprintf(param_name
, 10, "p%08X", i
);
170 ir_variable
*var
= (this->base_type
== GLSL_TYPE_ARRAY
)
171 ? new ir_variable(fields
.array
, param_name
)
172 : new ir_variable(fields
.structure
[i
].type
, param_name
);
174 var
->mode
= ir_var_in
;
175 declarations
[i
] = var
;
176 sig
->parameters
.push_tail(var
);
179 /* Generate the body of the constructor. The body assigns each of the
180 * parameters to a portion of a local variable called __retval that has
181 * the same type as the constructor. After initializing __retval,
182 * __retval is returned.
184 ir_variable
*retval
= new ir_variable(this, "__retval");
185 sig
->body
.push_tail(retval
);
187 for (unsigned i
= 0; i
< length
; i
++) {
188 ir_dereference
*const lhs
= (this->base_type
== GLSL_TYPE_ARRAY
)
189 ? (ir_dereference
*) new ir_dereference_array(retval
, new ir_constant(i
))
190 : (ir_dereference
*) new ir_dereference_record(retval
, fields
.structure
[i
].name
);
192 ir_dereference
*const rhs
= new ir_dereference_variable(declarations
[i
]);
193 ir_instruction
*const assign
= new ir_assignment(lhs
, rhs
, NULL
);
195 sig
->body
.push_tail(assign
);
200 ir_dereference
*const retref
= new ir_dereference_variable(retval
);
201 ir_instruction
*const inst
= new ir_return(retref
);
202 sig
->body
.push_tail(inst
);
209 * Generate the function intro for a constructor
211 * \param type Data type to be constructed
212 * \param count Number of parameters to this concrete constructor. Most
213 * types have at least two constructors. One will take a
214 * single scalar parameter and the other will take "N"
216 * \param parameters Storage for the list of parameters. These are
217 * typically stored in an \c ir_function_signature.
218 * \param declarations Pointers to the variable declarations for the function
219 * parameters. These are used later to avoid having to use
222 static ir_function_signature
*
223 generate_constructor_intro(const glsl_type
*type
, unsigned parameter_count
,
224 ir_variable
**declarations
)
226 /* Names of parameters used in vector and matrix constructors
228 static const char *const names
[] = {
229 "a", "b", "c", "d", "e", "f", "g", "h",
230 "i", "j", "k", "l", "m", "n", "o", "p",
233 assert(parameter_count
<= Elements(names
));
235 const glsl_type
*const parameter_type
= type
->get_base_type();
237 ir_function_signature
*const signature
= new ir_function_signature(type
);
239 for (unsigned i
= 0; i
< parameter_count
; i
++) {
240 ir_variable
*var
= new ir_variable(parameter_type
, names
[i
]);
242 var
->mode
= ir_var_in
;
243 signature
->parameters
.push_tail(var
);
245 declarations
[i
] = var
;
248 ir_variable
*retval
= new ir_variable(type
, "__retval");
249 signature
->body
.push_tail(retval
);
251 declarations
[16] = retval
;
257 * Generate the body of a vector constructor that takes a single scalar
260 generate_vec_body_from_scalar(exec_list
*instructions
,
261 ir_variable
**declarations
)
263 ir_instruction
*inst
;
265 /* Generate a single assignment of the parameter to __retval.x and return
266 * __retval.xxxx for however many vector components there are.
268 ir_dereference
*const lhs_ref
=
269 new ir_dereference_variable(declarations
[16]);
270 ir_dereference
*const rhs
= new ir_dereference_variable(declarations
[0]);
272 ir_swizzle
*lhs
= new ir_swizzle(lhs_ref
, 0, 0, 0, 0, 1);
274 inst
= new ir_assignment(lhs
, rhs
, NULL
);
275 instructions
->push_tail(inst
);
277 ir_dereference
*const retref
= new ir_dereference_variable(declarations
[16]);
279 ir_swizzle
*retval
= new ir_swizzle(retref
, 0, 0, 0, 0,
280 declarations
[16]->type
->vector_elements
);
282 inst
= new ir_return(retval
);
283 instructions
->push_tail(inst
);
288 * Generate the body of a vector constructor that takes multiple scalars
291 generate_vec_body_from_N_scalars(exec_list
*instructions
,
292 ir_variable
**declarations
)
294 ir_instruction
*inst
;
295 const glsl_type
*const vec_type
= declarations
[16]->type
;
298 /* Generate an assignment of each parameter to a single component of
299 * __retval.x and return __retval.
301 for (unsigned i
= 0; i
< vec_type
->vector_elements
; i
++) {
302 ir_dereference
*const lhs_ref
=
303 new ir_dereference_variable(declarations
[16]);
304 ir_dereference
*const rhs
= new ir_dereference_variable(declarations
[i
]);
306 ir_swizzle
*lhs
= new ir_swizzle(lhs_ref
, i
, 0, 0, 0, 1);
308 inst
= new ir_assignment(lhs
, rhs
, NULL
);
309 instructions
->push_tail(inst
);
312 ir_dereference
*retval
= new ir_dereference_variable(declarations
[16]);
314 inst
= new ir_return(retval
);
315 instructions
->push_tail(inst
);
320 * Generate the body of a matrix constructor that takes a single scalar
323 generate_mat_body_from_scalar(exec_list
*instructions
,
324 ir_variable
**declarations
)
326 ir_instruction
*inst
;
328 /* Generate an assignment of the parameter to the X component of a
329 * temporary vector. Set the remaining fields of the vector to 0. The
330 * size of the vector is equal to the number of rows of the matrix.
332 * Set each column of the matrix to a successive "rotation" of the
333 * temporary vector. This fills the matrix with 0s, but writes the single
334 * scalar along the matrix's diagonal.
336 * For a mat4x3, this is equivalent to:
343 * __retval[0] = tmp.xyy;
344 * __retval[1] = tmp.yxy;
345 * __retval[2] = tmp.yyx;
346 * __retval[3] = tmp.yyy;
348 const glsl_type
*const column_type
= declarations
[16]->type
->column_type();
349 const glsl_type
*const row_type
= declarations
[16]->type
->row_type();
350 ir_variable
*const column
= new ir_variable(column_type
, "v");
352 instructions
->push_tail(column
);
354 ir_dereference
*const lhs_ref
= new ir_dereference_variable(column
);
355 ir_dereference
*const rhs
= new ir_dereference_variable(declarations
[0]);
357 ir_swizzle
*lhs
= new ir_swizzle(lhs_ref
, 0, 0, 0, 0, 1);
359 inst
= new ir_assignment(lhs
, rhs
, NULL
);
360 instructions
->push_tail(inst
);
362 ir_constant
*const zero
= new ir_constant(0.0f
);
364 for (unsigned i
= 1; i
< column_type
->vector_elements
; i
++) {
365 ir_dereference
*const lhs_ref
= new ir_dereference_variable(column
);
367 ir_swizzle
*lhs
= new ir_swizzle(lhs_ref
, i
, 0, 0, 0, 1);
369 inst
= new ir_assignment(lhs
, zero
, NULL
);
370 instructions
->push_tail(inst
);
374 for (unsigned i
= 0; i
< row_type
->vector_elements
; i
++) {
375 static const unsigned swiz
[] = { 1, 1, 1, 0, 1, 1, 1 };
376 ir_dereference
*const rhs_ref
= new ir_dereference_variable(column
);
378 /* This will be .xyyy when i=0, .yxyy when i=1, etc.
380 ir_swizzle
*rhs
= new ir_swizzle(rhs_ref
, swiz
[3 - i
], swiz
[4 - i
],
381 swiz
[5 - i
], swiz
[6 - i
],
382 column_type
->vector_elements
);
384 ir_constant
*const idx
= new ir_constant(int(i
));
385 ir_dereference
*const lhs
=
386 new ir_dereference_array(declarations
[16], idx
);
388 inst
= new ir_assignment(lhs
, rhs
, NULL
);
389 instructions
->push_tail(inst
);
392 ir_dereference
*const retval
= new ir_dereference_variable(declarations
[16]);
393 inst
= new ir_return(retval
);
394 instructions
->push_tail(inst
);
399 * Generate the body of a vector constructor that takes multiple scalars
402 generate_mat_body_from_N_scalars(exec_list
*instructions
,
403 ir_variable
**declarations
)
405 ir_instruction
*inst
;
406 const glsl_type
*const row_type
= declarations
[16]->type
->row_type();
407 const glsl_type
*const column_type
= declarations
[16]->type
->column_type();
410 /* Generate an assignment of each parameter to a single component of
411 * of a particular column of __retval and return __retval.
413 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++) {
414 for (unsigned j
= 0; j
< row_type
->vector_elements
; j
++) {
415 ir_constant
*row_index
= new ir_constant(int(i
));
416 ir_dereference
*const row_access
=
417 new ir_dereference_array(declarations
[16], row_index
);
419 ir_swizzle
*component_access
= new ir_swizzle(row_access
,
422 const unsigned param
= (i
* row_type
->vector_elements
) + j
;
423 ir_dereference
*const rhs
=
424 new ir_dereference_variable(declarations
[param
]);
426 inst
= new ir_assignment(component_access
, rhs
, NULL
);
427 instructions
->push_tail(inst
);
431 ir_dereference
*retval
= new ir_dereference_variable(declarations
[16]);
433 inst
= new ir_return(retval
);
434 instructions
->push_tail(inst
);
439 * Generate the constructors for a set of GLSL types
441 * Constructor implementations are added to \c instructions, and the symbols
442 * are added to \c symtab.
445 generate_constructor(glsl_symbol_table
*symtab
, const struct glsl_type
*types
,
446 unsigned num_types
, exec_list
*instructions
)
448 ir_variable
*declarations
[17];
450 for (unsigned i
= 0; i
< num_types
; i
++) {
451 /* Only numeric and boolean vectors and matrices get constructors here.
452 * Structures need to be handled elsewhere. It is expected that scalar
453 * constructors are never actually called, so they are not generated.
455 if (!types
[i
].is_numeric() && !types
[i
].is_boolean())
458 if (types
[i
].is_scalar())
461 /* Generate the function block, add it to the symbol table, and emit it.
463 ir_function
*const f
= new ir_function(types
[i
].name
);
465 bool added
= symtab
->add_function(types
[i
].name
, f
);
468 instructions
->push_tail(f
);
470 /* Each type has several basic constructors. The total number of forms
471 * depends on the derived type.
473 * Vectors: 1 scalar, N scalars
474 * Matrices: 1 scalar, NxM scalars
476 * Several possible types of constructors are not included in this list.
478 * Scalar constructors are not included. The expectation is that the
479 * IR generator won't actually generate these as constructor calls. The
480 * expectation is that it will just generate the necessary type
483 * Matrix contructors from matrices are also not included. The
484 * expectation is that the IR generator will generate a call to the
485 * appropriate from-scalars constructor.
487 ir_function_signature
*const sig
=
488 generate_constructor_intro(&types
[i
], 1, declarations
);
489 f
->add_signature(sig
);
491 if (types
[i
].is_vector()) {
492 generate_vec_body_from_scalar(&sig
->body
, declarations
);
494 ir_function_signature
*const vec_sig
=
495 generate_constructor_intro(&types
[i
], types
[i
].vector_elements
,
497 f
->add_signature(vec_sig
);
499 generate_vec_body_from_N_scalars(&vec_sig
->body
, declarations
);
501 assert(types
[i
].is_matrix());
503 generate_mat_body_from_scalar(&sig
->body
, declarations
);
505 ir_function_signature
*const mat_sig
=
506 generate_constructor_intro(&types
[i
],
507 (types
[i
].vector_elements
508 * types
[i
].matrix_columns
),
510 f
->add_signature(mat_sig
);
512 generate_mat_body_from_N_scalars(&mat_sig
->body
, declarations
);
519 generate_110_constructors(glsl_symbol_table
*symtab
, exec_list
*instructions
)
521 generate_constructor(symtab
, builtin_core_types
,
522 Elements(builtin_core_types
), instructions
);
527 generate_120_constructors(glsl_symbol_table
*symtab
, exec_list
*instructions
)
529 generate_110_constructors(symtab
, instructions
);
531 generate_constructor(symtab
, builtin_120_types
,
532 Elements(builtin_120_types
), instructions
);
537 generate_130_constructors(glsl_symbol_table
*symtab
, exec_list
*instructions
)
539 generate_120_constructors(symtab
, instructions
);
541 generate_constructor(symtab
, builtin_130_types
,
542 Elements(builtin_130_types
), instructions
);
547 _mesa_glsl_initialize_constructors(exec_list
*instructions
,
548 struct _mesa_glsl_parse_state
*state
)
550 switch (state
->language_version
) {
552 generate_110_constructors(state
->symbols
, instructions
);
555 generate_120_constructors(state
->symbols
, instructions
);
558 generate_130_constructors(state
->symbols
, instructions
);
567 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
568 base_type(GLSL_TYPE_ARRAY
),
569 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
571 vector_elements(0), matrix_columns(0),
572 name(NULL
), length(length
)
574 this->fields
.array
= array
;
576 /* Allow a maximum of 10 characters for the array size. This is enough
577 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
580 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
581 char *const n
= (char *) malloc(name_length
);
584 snprintf(n
, name_length
, "%s[]", array
->name
);
586 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
593 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
595 if (base_type
== GLSL_TYPE_VOID
)
598 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
601 /* Treat GLSL vectors as Nx1 matrices.
606 return uint_type
+ (rows
- 1);
608 return int_type
+ (rows
- 1);
609 case GLSL_TYPE_FLOAT
:
610 return float_type
+ (rows
- 1);
612 return bool_type
+ (rows
- 1);
617 if ((base_type
!= GLSL_TYPE_FLOAT
) || (rows
== 1))
620 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
621 * combinations are valid:
629 #define IDX(c,r) (((c-1)*3) + (r-1))
631 switch (IDX(columns
, rows
)) {
632 case IDX(2,2): return mat2_type
;
633 case IDX(2,3): return mat2x3_type
;
634 case IDX(2,4): return mat2x4_type
;
635 case IDX(3,2): return mat3x2_type
;
636 case IDX(3,3): return mat3_type
;
637 case IDX(3,4): return mat3x4_type
;
638 case IDX(4,2): return mat4x2_type
;
639 case IDX(4,3): return mat4x3_type
;
640 case IDX(4,4): return mat4_type
;
641 default: return error_type
;
645 assert(!"Should not get here.");
651 glsl_type::array_key_compare(const void *a
, const void *b
)
653 const glsl_type
*const key1
= (glsl_type
*) a
;
654 const glsl_type
*const key2
= (glsl_type
*) b
;
656 /* Return zero is the types match (there is zero difference) or non-zero
659 return ((key1
->fields
.array
== key2
->fields
.array
)
660 && (key1
->length
== key2
->length
)) ? 0 : 1;
665 glsl_type::array_key_hash(const void *a
)
667 const glsl_type
*const key
= (glsl_type
*) a
;
679 return hash_table_string_hash(& hash_key
);
684 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
686 const glsl_type
key(base
, array_size
);
688 if (array_types
== NULL
) {
689 array_types
= hash_table_ctor(64, array_key_hash
, array_key_compare
);
692 const glsl_type
*t
= (glsl_type
*) hash_table_find(array_types
, & key
);
694 t
= new glsl_type(base
, array_size
);
696 hash_table_insert(array_types
, (void *) t
, t
);
699 assert(t
->base_type
== GLSL_TYPE_ARRAY
);
700 assert(t
->length
== array_size
);
701 assert(t
->fields
.array
== base
);
708 glsl_type::field_type(const char *name
) const
710 if (this->base_type
!= GLSL_TYPE_STRUCT
)
713 for (unsigned i
= 0; i
< this->length
; i
++) {
714 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
715 return this->fields
.structure
[i
].type
;
723 glsl_type::field_index(const char *name
) const
725 if (this->base_type
!= GLSL_TYPE_STRUCT
)
728 for (unsigned i
= 0; i
< this->length
; i
++) {
729 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)