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_prototype(glsl_symbol_table
*symtab
) const
138 /* FINISHME: Add support for non-array types. */
139 assert(base_type
== GLSL_TYPE_ARRAY
);
141 /* Generate the function name and add it to the symbol table.
143 ir_function
*const f
= new ir_function(name
);
145 bool added
= symtab
->add_function(name
, f
);
148 ir_function_signature
*const sig
= new ir_function_signature(this);
149 f
->add_signature(sig
);
151 for (unsigned i
= 0; i
< length
; i
++) {
152 char *const param_name
= (char *) malloc(10);
154 snprintf(param_name
, 10, "p%08X", i
);
156 ir_variable
*var
= new ir_variable(fields
.array
, param_name
);
158 var
->mode
= ir_var_in
;
159 sig
->parameters
.push_tail(var
);
167 * Generate the function intro for a constructor
169 * \param type Data type to be constructed
170 * \param count Number of parameters to this concrete constructor. Most
171 * types have at least two constructors. One will take a
172 * single scalar parameter and the other will take "N"
174 * \param parameters Storage for the list of parameters. These are
175 * typically stored in an \c ir_function_signature.
176 * \param declarations Pointers to the variable declarations for the function
177 * parameters. These are used later to avoid having to use
180 static ir_function_signature
*
181 generate_constructor_intro(const glsl_type
*type
, unsigned parameter_count
,
182 ir_variable
**declarations
)
184 /* Names of parameters used in vector and matrix constructors
186 static const char *const names
[] = {
187 "a", "b", "c", "d", "e", "f", "g", "h",
188 "i", "j", "k", "l", "m", "n", "o", "p",
191 assert(parameter_count
<= Elements(names
));
193 const glsl_type
*const parameter_type
= type
->get_base_type();
195 ir_function_signature
*const signature
= new ir_function_signature(type
);
197 for (unsigned i
= 0; i
< parameter_count
; i
++) {
198 ir_variable
*var
= new ir_variable(parameter_type
, names
[i
]);
200 var
->mode
= ir_var_in
;
201 signature
->parameters
.push_tail(var
);
203 declarations
[i
] = var
;
206 ir_variable
*retval
= new ir_variable(type
, "__retval");
207 signature
->body
.push_tail(retval
);
209 declarations
[16] = retval
;
215 * Generate the body of a vector constructor that takes a single scalar
218 generate_vec_body_from_scalar(exec_list
*instructions
,
219 ir_variable
**declarations
)
221 ir_instruction
*inst
;
223 /* Generate a single assignment of the parameter to __retval.x and return
224 * __retval.xxxx for however many vector components there are.
226 ir_dereference
*const lhs_ref
= new ir_dereference(declarations
[16]);
227 ir_dereference
*const rhs
= new ir_dereference(declarations
[0]);
229 ir_swizzle
*lhs
= new ir_swizzle(lhs_ref
, 0, 0, 0, 0, 1);
231 inst
= new ir_assignment(lhs
, rhs
, NULL
);
232 instructions
->push_tail(inst
);
234 ir_dereference
*const retref
= new ir_dereference(declarations
[16]);
236 ir_swizzle
*retval
= new ir_swizzle(retref
, 0, 0, 0, 0,
237 declarations
[16]->type
->vector_elements
);
239 inst
= new ir_return(retval
);
240 instructions
->push_tail(inst
);
245 * Generate the body of a vector constructor that takes multiple scalars
248 generate_vec_body_from_N_scalars(exec_list
*instructions
,
249 ir_variable
**declarations
)
251 ir_instruction
*inst
;
252 const glsl_type
*const vec_type
= declarations
[16]->type
;
255 /* Generate an assignment of each parameter to a single component of
256 * __retval.x and return __retval.
258 for (unsigned i
= 0; i
< vec_type
->vector_elements
; i
++) {
259 ir_dereference
*const lhs_ref
= new ir_dereference(declarations
[16]);
260 ir_dereference
*const rhs
= new ir_dereference(declarations
[i
]);
262 ir_swizzle
*lhs
= new ir_swizzle(lhs_ref
, 1, 0, 0, 0, 1);
264 inst
= new ir_assignment(lhs
, rhs
, NULL
);
265 instructions
->push_tail(inst
);
268 ir_dereference
*retval
= new ir_dereference(declarations
[16]);
270 inst
= new ir_return(retval
);
271 instructions
->push_tail(inst
);
276 * Generate the body of a matrix constructor that takes a single scalar
279 generate_mat_body_from_scalar(exec_list
*instructions
,
280 ir_variable
**declarations
)
282 ir_instruction
*inst
;
284 /* Generate an assignment of the parameter to the X component of a
285 * temporary vector. Set the remaining fields of the vector to 0. The
286 * size of the vector is equal to the number of rows of the matrix.
288 * Set each column of the matrix to a successive "rotation" of the
289 * temporary vector. This fills the matrix with 0s, but writes the single
290 * scalar along the matrix's diagonal.
292 * For a mat4x3, this is equivalent to:
299 * __retval[0] = tmp.xyy;
300 * __retval[1] = tmp.yxy;
301 * __retval[2] = tmp.yyx;
302 * __retval[3] = tmp.yyy;
304 const glsl_type
*const column_type
= declarations
[16]->type
->column_type();
305 const glsl_type
*const row_type
= declarations
[16]->type
->row_type();
306 ir_variable
*const column
= new ir_variable(column_type
, "v");
308 instructions
->push_tail(column
);
310 ir_dereference
*const lhs_ref
= new ir_dereference(column
);
311 ir_dereference
*const rhs
= new ir_dereference(declarations
[0]);
313 ir_swizzle
*lhs
= new ir_swizzle(lhs_ref
, 0, 0, 0, 0, 1);
315 inst
= new ir_assignment(lhs
, rhs
, NULL
);
316 instructions
->push_tail(inst
);
318 const float z
= 0.0f
;
319 ir_constant
*const zero
= new ir_constant(glsl_type::float_type
, &z
);
321 for (unsigned i
= 1; i
< column_type
->vector_elements
; i
++) {
322 ir_dereference
*const lhs_ref
= new ir_dereference(column
);
324 ir_swizzle
*lhs
= new ir_swizzle(lhs_ref
, i
, 0, 0, 0, 1);
326 inst
= new ir_assignment(lhs
, zero
, NULL
);
327 instructions
->push_tail(inst
);
331 for (unsigned i
= 0; i
< row_type
->vector_elements
; i
++) {
332 static const unsigned swiz
[] = { 1, 1, 1, 0, 1, 1, 1 };
333 ir_dereference
*const rhs_ref
= new ir_dereference(column
);
335 /* This will be .xyyy when i=0, .yxyy when i=1, etc.
337 ir_swizzle
*rhs
= new ir_swizzle(rhs_ref
, swiz
[3 - i
], swiz
[4 - i
],
338 swiz
[5 - i
], swiz
[6 - i
],
339 column_type
->vector_elements
);
341 ir_constant
*const idx
= new ir_constant(glsl_type::int_type
, &i
);
342 ir_dereference
*const lhs
= new ir_dereference(declarations
[16], idx
);
344 inst
= new ir_assignment(lhs
, rhs
, NULL
);
345 instructions
->push_tail(inst
);
348 ir_dereference
*const retval
= new ir_dereference(declarations
[16]);
349 inst
= new ir_return(retval
);
350 instructions
->push_tail(inst
);
355 * Generate the body of a vector constructor that takes multiple scalars
358 generate_mat_body_from_N_scalars(exec_list
*instructions
,
359 ir_variable
**declarations
)
361 ir_instruction
*inst
;
362 const glsl_type
*const row_type
= declarations
[16]->type
->row_type();
363 const glsl_type
*const column_type
= declarations
[16]->type
->column_type();
366 /* Generate an assignment of each parameter to a single component of
367 * of a particular column of __retval and return __retval.
369 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++) {
370 for (unsigned j
= 0; j
< row_type
->vector_elements
; j
++) {
371 ir_constant
*row_index
= new ir_constant(glsl_type::int_type
, &i
);
372 ir_dereference
*const row_access
=
373 new ir_dereference(declarations
[16], row_index
);
375 ir_swizzle
*component_access
= new ir_swizzle(row_access
,
378 const unsigned param
= (i
* row_type
->vector_elements
) + j
;
379 ir_dereference
*const rhs
= new ir_dereference(declarations
[param
]);
381 inst
= new ir_assignment(component_access
, rhs
, NULL
);
382 instructions
->push_tail(inst
);
386 ir_dereference
*retval
= new ir_dereference(declarations
[16]);
388 inst
= new ir_return(retval
);
389 instructions
->push_tail(inst
);
394 * Generate the constructors for a set of GLSL types
396 * Constructor implementations are added to \c instructions, and the symbols
397 * are added to \c symtab.
400 generate_constructor(glsl_symbol_table
*symtab
, const struct glsl_type
*types
,
401 unsigned num_types
, exec_list
*instructions
)
403 ir_variable
*declarations
[17];
405 for (unsigned i
= 0; i
< num_types
; i
++) {
406 /* Only numeric and boolean vectors and matrices get constructors here.
407 * Structures need to be handled elsewhere. It is expected that scalar
408 * constructors are never actually called, so they are not generated.
410 if (!types
[i
].is_numeric() && !types
[i
].is_boolean())
413 if (types
[i
].is_scalar())
416 /* Generate the function block, add it to the symbol table, and emit it.
418 ir_function
*const f
= new ir_function(types
[i
].name
);
420 bool added
= symtab
->add_function(types
[i
].name
, f
);
423 instructions
->push_tail(f
);
425 /* Each type has several basic constructors. The total number of forms
426 * depends on the derived type.
428 * Vectors: 1 scalar, N scalars
429 * Matrices: 1 scalar, NxM scalars
431 * Several possible types of constructors are not included in this list.
433 * Scalar constructors are not included. The expectation is that the
434 * IR generator won't actually generate these as constructor calls. The
435 * expectation is that it will just generate the necessary type
438 * Matrix contructors from matrices are also not included. The
439 * expectation is that the IR generator will generate a call to the
440 * appropriate from-scalars constructor.
442 ir_function_signature
*const sig
=
443 generate_constructor_intro(&types
[i
], 1, declarations
);
444 f
->add_signature(sig
);
446 if (types
[i
].is_vector()) {
447 generate_vec_body_from_scalar(&sig
->body
, declarations
);
449 ir_function_signature
*const vec_sig
=
450 generate_constructor_intro(&types
[i
], types
[i
].vector_elements
,
452 f
->add_signature(vec_sig
);
454 generate_vec_body_from_N_scalars(&vec_sig
->body
, declarations
);
456 assert(types
[i
].is_matrix());
458 generate_mat_body_from_scalar(&sig
->body
, declarations
);
460 ir_function_signature
*const mat_sig
=
461 generate_constructor_intro(&types
[i
],
462 (types
[i
].vector_elements
463 * types
[i
].matrix_columns
),
465 f
->add_signature(mat_sig
);
467 generate_mat_body_from_N_scalars(&mat_sig
->body
, declarations
);
474 generate_110_constructors(glsl_symbol_table
*symtab
, exec_list
*instructions
)
476 generate_constructor(symtab
, builtin_core_types
,
477 Elements(builtin_core_types
), instructions
);
482 generate_120_constructors(glsl_symbol_table
*symtab
, exec_list
*instructions
)
484 generate_110_constructors(symtab
, instructions
);
486 generate_constructor(symtab
, builtin_120_types
,
487 Elements(builtin_120_types
), instructions
);
492 generate_130_constructors(glsl_symbol_table
*symtab
, exec_list
*instructions
)
494 generate_120_constructors(symtab
, instructions
);
496 generate_constructor(symtab
, builtin_130_types
,
497 Elements(builtin_130_types
), instructions
);
502 _mesa_glsl_initialize_constructors(exec_list
*instructions
,
503 struct _mesa_glsl_parse_state
*state
)
505 switch (state
->language_version
) {
507 generate_110_constructors(state
->symbols
, instructions
);
510 generate_120_constructors(state
->symbols
, instructions
);
513 generate_130_constructors(state
->symbols
, instructions
);
522 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
523 base_type(GLSL_TYPE_ARRAY
),
524 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
526 vector_elements(0), matrix_columns(0),
527 name(NULL
), length(length
)
529 this->fields
.array
= array
;
531 /* Allow a maximum of 10 characters for the array size. This is enough
532 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
535 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
536 char *const n
= (char *) malloc(name_length
);
539 snprintf(n
, name_length
, "%s[]", array
->name
);
541 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
548 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
550 if (base_type
== GLSL_TYPE_VOID
)
553 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
556 /* Treat GLSL vectors as Nx1 matrices.
561 return uint_type
+ (rows
- 1);
563 return int_type
+ (rows
- 1);
564 case GLSL_TYPE_FLOAT
:
565 return float_type
+ (rows
- 1);
567 return bool_type
+ (rows
- 1);
572 if ((base_type
!= GLSL_TYPE_FLOAT
) || (rows
== 1))
575 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
576 * combinations are valid:
584 #define IDX(c,r) (((c-1)*3) + (r-1))
586 switch (IDX(columns
, rows
)) {
587 case IDX(2,2): return mat2_type
;
588 case IDX(2,3): return mat2x3_type
;
589 case IDX(2,4): return mat2x4_type
;
590 case IDX(3,2): return mat3x2_type
;
591 case IDX(3,3): return mat3_type
;
592 case IDX(3,4): return mat3x4_type
;
593 case IDX(4,2): return mat4x2_type
;
594 case IDX(4,3): return mat4x3_type
;
595 case IDX(4,4): return mat4_type
;
596 default: return error_type
;
600 assert(!"Should not get here.");
606 glsl_type::array_key_compare(const void *a
, const void *b
)
608 const glsl_type
*const key1
= (glsl_type
*) a
;
609 const glsl_type
*const key2
= (glsl_type
*) b
;
611 /* Return zero is the types match (there is zero difference) or non-zero
614 return ((key1
->fields
.array
== key2
->fields
.array
)
615 && (key1
->length
== key2
->length
)) ? 0 : 1;
620 glsl_type::array_key_hash(const void *a
)
622 const glsl_type
*const key
= (glsl_type
*) a
;
634 return hash_table_string_hash(& hash_key
);
639 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
641 const glsl_type
key(base
, array_size
);
643 if (array_types
== NULL
) {
644 array_types
= hash_table_ctor(64, array_key_hash
, array_key_compare
);
647 const glsl_type
*t
= (glsl_type
*) hash_table_find(array_types
, & key
);
649 t
= new glsl_type(base
, array_size
);
651 hash_table_insert(array_types
, (void *) t
, t
);
654 assert(t
->base_type
== GLSL_TYPE_ARRAY
);
655 assert(t
->length
== array_size
);
656 assert(t
->fields
.array
== base
);