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"
31 #include "hash_table.h"
34 hash_table
*glsl_type::array_types
= NULL
;
37 add_types_to_symbol_table(glsl_symbol_table
*symtab
,
38 const struct glsl_type
*types
,
39 unsigned num_types
, bool warn
)
43 for (unsigned i
= 0; i
< num_types
; i
++) {
44 symtab
->add_type(types
[i
].name
, & types
[i
]);
50 generate_110_types(glsl_symbol_table
*symtab
)
52 add_types_to_symbol_table(symtab
, builtin_core_types
,
53 Elements(builtin_core_types
),
55 add_types_to_symbol_table(symtab
, builtin_structure_types
,
56 Elements(builtin_structure_types
),
58 add_types_to_symbol_table(symtab
, builtin_110_deprecated_structure_types
,
59 Elements(builtin_110_deprecated_structure_types
),
61 add_types_to_symbol_table(symtab
, & void_type
, 1, false);
66 generate_120_types(glsl_symbol_table
*symtab
)
68 generate_110_types(symtab
);
70 add_types_to_symbol_table(symtab
, builtin_120_types
,
71 Elements(builtin_120_types
), false);
76 generate_130_types(glsl_symbol_table
*symtab
)
78 generate_120_types(symtab
);
80 add_types_to_symbol_table(symtab
, builtin_130_types
,
81 Elements(builtin_130_types
), false);
86 generate_ARB_texture_rectangle_types(glsl_symbol_table
*symtab
, bool warn
)
88 add_types_to_symbol_table(symtab
, builtin_ARB_texture_rectangle_types
,
89 Elements(builtin_ARB_texture_rectangle_types
),
95 generate_EXT_texture_array_types(glsl_symbol_table
*symtab
, bool warn
)
97 add_types_to_symbol_table(symtab
, builtin_EXT_texture_array_types
,
98 Elements(builtin_EXT_texture_array_types
),
104 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*state
)
106 switch (state
->language_version
) {
108 generate_110_types(state
->symbols
);
111 generate_120_types(state
->symbols
);
114 generate_130_types(state
->symbols
);
121 if (state
->ARB_texture_rectangle_enable
) {
122 generate_ARB_texture_rectangle_types(state
->symbols
,
123 state
->ARB_texture_rectangle_warn
);
126 if (state
->EXT_texture_array_enable
&& state
->language_version
< 130) {
127 // These are already included in 130; don't create twice.
128 generate_EXT_texture_array_types(state
->symbols
,
129 state
->EXT_texture_array_warn
);
134 const glsl_type
*glsl_type::get_base_type() const
141 case GLSL_TYPE_FLOAT
:
152 glsl_type::generate_constructor(glsl_symbol_table
*symtab
) const
156 /* Generate the function name and add it to the symbol table.
158 ir_function
*const f
= new(ctx
) ir_function(name
);
160 bool added
= symtab
->add_function(name
, f
);
163 ir_function_signature
*const sig
= new(ctx
) ir_function_signature(this);
164 f
->add_signature(sig
);
166 ir_variable
**declarations
=
167 (ir_variable
**) malloc(sizeof(ir_variable
*) * this->length
);
168 for (unsigned i
= 0; i
< length
; i
++) {
169 char *const param_name
= (char *) malloc(10);
171 snprintf(param_name
, 10, "p%08X", i
);
173 ir_variable
*var
= (this->base_type
== GLSL_TYPE_ARRAY
)
174 ? new(ctx
) ir_variable(fields
.array
, param_name
)
175 : new(ctx
) ir_variable(fields
.structure
[i
].type
, param_name
);
177 var
->mode
= ir_var_in
;
178 declarations
[i
] = var
;
179 sig
->parameters
.push_tail(var
);
182 /* Generate the body of the constructor. The body assigns each of the
183 * parameters to a portion of a local variable called __retval that has
184 * the same type as the constructor. After initializing __retval,
185 * __retval is returned.
187 ir_variable
*retval
= new(ctx
) ir_variable(this, "__retval");
188 sig
->body
.push_tail(retval
);
190 for (unsigned i
= 0; i
< length
; i
++) {
191 ir_dereference
*const lhs
= (this->base_type
== GLSL_TYPE_ARRAY
)
192 ? (ir_dereference
*) new(ctx
) ir_dereference_array(retval
,
193 new(ctx
) ir_constant(i
))
194 : (ir_dereference
*) new(ctx
) ir_dereference_record(retval
,
195 fields
.structure
[i
].name
);
197 ir_dereference
*const rhs
= new(ctx
) ir_dereference_variable(declarations
[i
]);
198 ir_instruction
*const assign
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
200 sig
->body
.push_tail(assign
);
205 ir_dereference
*const retref
= new(ctx
) ir_dereference_variable(retval
);
206 ir_instruction
*const inst
= new(ctx
) ir_return(retref
);
207 sig
->body
.push_tail(inst
);
214 * Generate the function intro for a constructor
216 * \param type Data type to be constructed
217 * \param count Number of parameters to this concrete constructor. Most
218 * types have at least two constructors. One will take a
219 * single scalar parameter and the other will take "N"
221 * \param parameters Storage for the list of parameters. These are
222 * typically stored in an \c ir_function_signature.
223 * \param declarations Pointers to the variable declarations for the function
224 * parameters. These are used later to avoid having to use
227 static ir_function_signature
*
228 generate_constructor_intro(void *ctx
,
229 const glsl_type
*type
, unsigned parameter_count
,
230 ir_variable
**declarations
)
232 /* Names of parameters used in vector and matrix constructors
234 static const char *const names
[] = {
235 "a", "b", "c", "d", "e", "f", "g", "h",
236 "i", "j", "k", "l", "m", "n", "o", "p",
239 assert(parameter_count
<= Elements(names
));
241 const glsl_type
*const parameter_type
= type
->get_base_type();
243 ir_function_signature
*const signature
= new(ctx
) ir_function_signature(type
);
245 for (unsigned i
= 0; i
< parameter_count
; i
++) {
246 ir_variable
*var
= new(ctx
) ir_variable(parameter_type
, names
[i
]);
248 var
->mode
= ir_var_in
;
249 signature
->parameters
.push_tail(var
);
251 declarations
[i
] = var
;
254 ir_variable
*retval
= new(ctx
) ir_variable(type
, "__retval");
255 signature
->body
.push_tail(retval
);
257 declarations
[16] = retval
;
263 * Generate the body of a vector constructor that takes a single scalar
266 generate_vec_body_from_scalar(void *ctx
,
267 exec_list
*instructions
,
268 ir_variable
**declarations
)
270 ir_instruction
*inst
;
272 /* Generate a single assignment of the parameter to __retval.x and return
273 * __retval.xxxx for however many vector components there are.
275 ir_dereference
*const lhs_ref
=
276 new(ctx
) ir_dereference_variable(declarations
[16]);
277 ir_dereference
*const rhs
= new(ctx
) ir_dereference_variable(declarations
[0]);
279 ir_swizzle
*lhs
= new(ctx
) ir_swizzle(lhs_ref
, 0, 0, 0, 0, 1);
281 inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
282 instructions
->push_tail(inst
);
284 ir_dereference
*const retref
= new(ctx
) ir_dereference_variable(declarations
[16]);
286 ir_swizzle
*retval
= new(ctx
) ir_swizzle(retref
, 0, 0, 0, 0,
287 declarations
[16]->type
->vector_elements
);
289 inst
= new(ctx
) ir_return(retval
);
290 instructions
->push_tail(inst
);
295 * Generate the body of a vector constructor that takes multiple scalars
298 generate_vec_body_from_N_scalars(void *ctx
,
299 exec_list
*instructions
,
300 ir_variable
**declarations
)
302 ir_instruction
*inst
;
303 const glsl_type
*const vec_type
= declarations
[16]->type
;
305 /* Generate an assignment of each parameter to a single component of
306 * __retval.x and return __retval.
308 for (unsigned i
= 0; i
< vec_type
->vector_elements
; i
++) {
309 ir_dereference
*const lhs_ref
=
310 new(ctx
) ir_dereference_variable(declarations
[16]);
311 ir_dereference
*const rhs
= new(ctx
) ir_dereference_variable(declarations
[i
]);
313 ir_swizzle
*lhs
= new(ctx
) ir_swizzle(lhs_ref
, i
, 0, 0, 0, 1);
315 inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
316 instructions
->push_tail(inst
);
319 ir_dereference
*retval
= new(ctx
) ir_dereference_variable(declarations
[16]);
321 inst
= new(ctx
) ir_return(retval
);
322 instructions
->push_tail(inst
);
327 * Generate the body of a matrix constructor that takes a single scalar
330 generate_mat_body_from_scalar(void *ctx
,
331 exec_list
*instructions
,
332 ir_variable
**declarations
)
334 ir_instruction
*inst
;
336 /* Generate an assignment of the parameter to the X component of a
337 * temporary vector. Set the remaining fields of the vector to 0. The
338 * size of the vector is equal to the number of rows of the matrix.
340 * Set each column of the matrix to a successive "rotation" of the
341 * temporary vector. This fills the matrix with 0s, but writes the single
342 * scalar along the matrix's diagonal.
344 * For a mat4x3, this is equivalent to:
351 * __retval[0] = tmp.xyy;
352 * __retval[1] = tmp.yxy;
353 * __retval[2] = tmp.yyx;
354 * __retval[3] = tmp.yyy;
356 const glsl_type
*const column_type
= declarations
[16]->type
->column_type();
357 const glsl_type
*const row_type
= declarations
[16]->type
->row_type();
359 ir_variable
*const column
= new(ctx
) ir_variable(column_type
, "v");
361 instructions
->push_tail(column
);
363 ir_dereference
*const lhs_ref
= new(ctx
) ir_dereference_variable(column
);
364 ir_dereference
*const rhs
= new(ctx
) ir_dereference_variable(declarations
[0]);
366 ir_swizzle
*lhs
= new(ctx
) ir_swizzle(lhs_ref
, 0, 0, 0, 0, 1);
368 inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
369 instructions
->push_tail(inst
);
371 for (unsigned i
= 1; i
< column_type
->vector_elements
; i
++) {
372 ir_dereference
*const lhs_ref
= new(ctx
) ir_dereference_variable(column
);
373 ir_constant
*const zero
= new(ctx
) ir_constant(0.0f
);
375 ir_swizzle
*lhs
= new(ctx
) ir_swizzle(lhs_ref
, i
, 0, 0, 0, 1);
377 inst
= new(ctx
) ir_assignment(lhs
, zero
, NULL
);
378 instructions
->push_tail(inst
);
382 for (unsigned i
= 0; i
< row_type
->vector_elements
; i
++) {
383 static const unsigned swiz
[] = { 1, 1, 1, 0, 1, 1, 1 };
384 ir_dereference
*const rhs_ref
= new(ctx
) ir_dereference_variable(column
);
386 /* This will be .xyyy when i=0, .yxyy when i=1, etc.
388 ir_swizzle
*rhs
= new(ctx
) ir_swizzle(rhs_ref
, swiz
[3 - i
], swiz
[4 - i
],
389 swiz
[5 - i
], swiz
[6 - i
],
390 column_type
->vector_elements
);
392 ir_constant
*const idx
= new(ctx
) ir_constant(int(i
));
393 ir_dereference
*const lhs
=
394 new(ctx
) ir_dereference_array(declarations
[16], idx
);
396 inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
397 instructions
->push_tail(inst
);
400 ir_dereference
*const retval
= new(ctx
) ir_dereference_variable(declarations
[16]);
401 inst
= new(ctx
) ir_return(retval
);
402 instructions
->push_tail(inst
);
407 * Generate the body of a vector constructor that takes multiple scalars
410 generate_mat_body_from_N_scalars(void *ctx
,
411 exec_list
*instructions
,
412 ir_variable
**declarations
)
414 ir_instruction
*inst
;
415 const glsl_type
*const row_type
= declarations
[16]->type
->row_type();
416 const glsl_type
*const column_type
= declarations
[16]->type
->column_type();
418 /* Generate an assignment of each parameter to a single component of
419 * of a particular column of __retval and return __retval.
421 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++) {
422 for (unsigned j
= 0; j
< row_type
->vector_elements
; j
++) {
423 ir_constant
*row_index
= new(ctx
) ir_constant(int(i
));
424 ir_dereference
*const row_access
=
425 new(ctx
) ir_dereference_array(declarations
[16], row_index
);
427 ir_swizzle
*component_access
= new(ctx
) ir_swizzle(row_access
,
430 const unsigned param
= (i
* row_type
->vector_elements
) + j
;
431 ir_dereference
*const rhs
=
432 new(ctx
) ir_dereference_variable(declarations
[param
]);
434 inst
= new(ctx
) ir_assignment(component_access
, rhs
, NULL
);
435 instructions
->push_tail(inst
);
439 ir_dereference
*retval
= new(ctx
) ir_dereference_variable(declarations
[16]);
441 inst
= new(ctx
) ir_return(retval
);
442 instructions
->push_tail(inst
);
447 * Generate the constructors for a set of GLSL types
449 * Constructor implementations are added to \c instructions, and the symbols
450 * are added to \c symtab.
453 generate_constructor(glsl_symbol_table
*symtab
, const struct glsl_type
*types
,
454 unsigned num_types
, exec_list
*instructions
)
457 ir_variable
*declarations
[17];
459 for (unsigned i
= 0; i
< num_types
; i
++) {
460 /* Only numeric and boolean vectors and matrices get constructors here.
461 * Structures need to be handled elsewhere. It is expected that scalar
462 * constructors are never actually called, so they are not generated.
464 if (!types
[i
].is_numeric() && !types
[i
].is_boolean())
467 if (types
[i
].is_scalar())
470 /* Generate the function block, add it to the symbol table, and emit it.
472 ir_function
*const f
= new(ctx
) ir_function(types
[i
].name
);
474 bool added
= symtab
->add_function(types
[i
].name
, f
);
477 instructions
->push_tail(f
);
479 /* Each type has several basic constructors. The total number of forms
480 * depends on the derived type.
482 * Vectors: 1 scalar, N scalars
483 * Matrices: 1 scalar, NxM scalars
485 * Several possible types of constructors are not included in this list.
487 * Scalar constructors are not included. The expectation is that the
488 * IR generator won't actually generate these as constructor calls. The
489 * expectation is that it will just generate the necessary type
492 * Matrix contructors from matrices are also not included. The
493 * expectation is that the IR generator will generate a call to the
494 * appropriate from-scalars constructor.
496 ir_function_signature
*const sig
=
497 generate_constructor_intro(ctx
, &types
[i
], 1, declarations
);
498 f
->add_signature(sig
);
500 if (types
[i
].is_vector()) {
501 generate_vec_body_from_scalar(ctx
, &sig
->body
, declarations
);
503 ir_function_signature
*const vec_sig
=
504 generate_constructor_intro(ctx
,
505 &types
[i
], types
[i
].vector_elements
,
507 f
->add_signature(vec_sig
);
509 generate_vec_body_from_N_scalars(ctx
, &vec_sig
->body
, declarations
);
511 assert(types
[i
].is_matrix());
513 generate_mat_body_from_scalar(ctx
, &sig
->body
, declarations
);
515 ir_function_signature
*const mat_sig
=
516 generate_constructor_intro(ctx
,
518 (types
[i
].vector_elements
519 * types
[i
].matrix_columns
),
521 f
->add_signature(mat_sig
);
523 generate_mat_body_from_N_scalars(ctx
, &mat_sig
->body
, declarations
);
530 generate_110_constructors(glsl_symbol_table
*symtab
, exec_list
*instructions
)
532 generate_constructor(symtab
, builtin_core_types
,
533 Elements(builtin_core_types
), instructions
);
538 generate_120_constructors(glsl_symbol_table
*symtab
, exec_list
*instructions
)
540 generate_110_constructors(symtab
, instructions
);
542 generate_constructor(symtab
, builtin_120_types
,
543 Elements(builtin_120_types
), instructions
);
548 generate_130_constructors(glsl_symbol_table
*symtab
, exec_list
*instructions
)
550 generate_120_constructors(symtab
, instructions
);
552 generate_constructor(symtab
, builtin_130_types
,
553 Elements(builtin_130_types
), instructions
);
558 _mesa_glsl_initialize_constructors(exec_list
*instructions
,
559 struct _mesa_glsl_parse_state
*state
)
561 switch (state
->language_version
) {
563 generate_110_constructors(state
->symbols
, instructions
);
566 generate_120_constructors(state
->symbols
, instructions
);
569 generate_130_constructors(state
->symbols
, instructions
);
578 glsl_type::glsl_type(void *ctx
, const glsl_type
*array
, unsigned length
) :
579 base_type(GLSL_TYPE_ARRAY
),
580 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
582 vector_elements(0), matrix_columns(0),
583 name(NULL
), length(length
)
585 this->fields
.array
= array
;
587 /* Allow a maximum of 10 characters for the array size. This is enough
588 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
591 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
592 char *const n
= (char *) talloc_size(ctx
, name_length
);
595 snprintf(n
, name_length
, "%s[]", array
->name
);
597 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
604 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
606 if (base_type
== GLSL_TYPE_VOID
)
609 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
612 /* Treat GLSL vectors as Nx1 matrices.
617 return uint_type
+ (rows
- 1);
619 return int_type
+ (rows
- 1);
620 case GLSL_TYPE_FLOAT
:
621 return float_type
+ (rows
- 1);
623 return bool_type
+ (rows
- 1);
628 if ((base_type
!= GLSL_TYPE_FLOAT
) || (rows
== 1))
631 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
632 * combinations are valid:
640 #define IDX(c,r) (((c-1)*3) + (r-1))
642 switch (IDX(columns
, rows
)) {
643 case IDX(2,2): return mat2_type
;
644 case IDX(2,3): return mat2x3_type
;
645 case IDX(2,4): return mat2x4_type
;
646 case IDX(3,2): return mat3x2_type
;
647 case IDX(3,3): return mat3_type
;
648 case IDX(3,4): return mat3x4_type
;
649 case IDX(4,2): return mat4x2_type
;
650 case IDX(4,3): return mat4x3_type
;
651 case IDX(4,4): return mat4_type
;
652 default: return error_type
;
656 assert(!"Should not get here.");
662 glsl_type::array_key_compare(const void *a
, const void *b
)
664 const glsl_type
*const key1
= (glsl_type
*) a
;
665 const glsl_type
*const key2
= (glsl_type
*) b
;
667 /* Return zero is the types match (there is zero difference) or non-zero
670 return ((key1
->fields
.array
== key2
->fields
.array
)
671 && (key1
->length
== key2
->length
)) ? 0 : 1;
676 glsl_type::array_key_hash(const void *a
)
678 const glsl_type
*const key
= (glsl_type
*) a
;
690 return hash_table_string_hash(& hash_key
);
695 glsl_type::get_array_instance(void *ctx
, const glsl_type
*base
,
698 const glsl_type
key(ctx
, base
, array_size
);
700 if (array_types
== NULL
) {
701 array_types
= hash_table_ctor(64, array_key_hash
, array_key_compare
);
704 const glsl_type
*t
= (glsl_type
*) hash_table_find(array_types
, & key
);
706 t
= new(ctx
) glsl_type(ctx
, base
, array_size
);
708 hash_table_insert(array_types
, (void *) t
, t
);
711 assert(t
->base_type
== GLSL_TYPE_ARRAY
);
712 assert(t
->length
== array_size
);
713 assert(t
->fields
.array
== base
);
720 glsl_type::field_type(const char *name
) const
722 if (this->base_type
!= GLSL_TYPE_STRUCT
)
725 for (unsigned i
= 0; i
< this->length
; i
++) {
726 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
727 return this->fields
.structure
[i
].type
;
735 glsl_type::field_index(const char *name
) const
737 if (this->base_type
!= GLSL_TYPE_STRUCT
)
740 for (unsigned i
= 0; i
< this->length
; i
++) {
741 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
750 glsl_type::component_slots() const
752 switch (this->base_type
) {
755 case GLSL_TYPE_FLOAT
:
757 return this->components();
759 case GLSL_TYPE_STRUCT
: {
762 for (unsigned i
= 0; i
< this->length
; i
++)
763 size
+= this->fields
.structure
[i
].type
->component_slots();
768 case GLSL_TYPE_ARRAY
:
769 return this->length
* this->fields
.array
->component_slots();