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.
25 #include "glsl_symbol_table.h"
26 #include "glsl_parser_extras.h"
27 #include "glsl_types.h"
28 #include "builtin_types.h"
32 add_types_to_symbol_table(glsl_symbol_table
*symtab
,
33 const struct glsl_type
*types
,
38 for (i
= 0; i
< num_types
; i
++) {
39 symtab
->add_type(types
[i
].name
, & types
[i
]);
45 generate_110_types(glsl_symbol_table
*symtab
)
47 add_types_to_symbol_table(symtab
, builtin_core_types
,
48 Elements(builtin_core_types
));
49 add_types_to_symbol_table(symtab
, builtin_structure_types
,
50 Elements(builtin_structure_types
));
51 add_types_to_symbol_table(symtab
, builtin_110_deprecated_structure_types
,
52 Elements(builtin_110_deprecated_structure_types
));
53 add_types_to_symbol_table(symtab
, & void_type
, 1);
58 generate_120_types(glsl_symbol_table
*symtab
)
60 generate_110_types(symtab
);
62 add_types_to_symbol_table(symtab
, builtin_120_types
,
63 Elements(builtin_120_types
));
68 generate_130_types(glsl_symbol_table
*symtab
)
70 generate_120_types(symtab
);
72 add_types_to_symbol_table(symtab
, builtin_130_types
,
73 Elements(builtin_130_types
));
78 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*state
)
80 switch (state
->language_version
) {
82 generate_110_types(state
->symbols
);
85 generate_120_types(state
->symbols
);
88 generate_130_types(state
->symbols
);
97 const glsl_type
*glsl_type::get_base_type() const
104 case GLSL_TYPE_FLOAT
:
115 * Generate the function intro for a constructor
117 * \param type Data type to be constructed
118 * \param count Number of parameters to this concrete constructor. Most
119 * types have at least two constructors. One will take a
120 * single scalar parameter and the other will take "N"
122 * \param parameters Storage for the list of parameters. These are
123 * typically stored in an \c ir_function_signature.
124 * \param instructions Storage for the preamble and body of the function.
125 * \param declarations Pointers to the variable declarations for the function
126 * parameters. These are used later to avoid having to use
130 generate_constructor_intro(const glsl_type
*type
, unsigned parameter_count
,
131 exec_list
*parameters
, exec_list
*instructions
,
132 ir_variable
**declarations
)
134 /* Names of parameters used in vector and matrix constructors
136 static const char *const names
[] = {
137 "a", "b", "c", "d", "e", "f", "g", "h",
138 "i", "j", "k", "l", "m", "n", "o", "p",
141 assert(parameter_count
<= Elements(names
));
143 const glsl_type
*const parameter_type
= type
->get_base_type();
145 ir_label
*const label
= new ir_label(type
->name
);
146 instructions
->push_tail(label
);
148 for (unsigned i
= 0; i
< parameter_count
; i
++) {
149 ir_variable
*var
= new ir_variable(parameter_type
, names
[i
]);
151 var
->mode
= ir_var_in
;
152 parameters
->push_tail(var
);
154 var
= new ir_variable(parameter_type
, names
[i
]);
156 var
->mode
= ir_var_in
;
157 instructions
->push_tail(var
);
159 declarations
[i
] = var
;
162 ir_variable
*retval
= new ir_variable(type
, "__retval");
163 instructions
->push_tail(retval
);
165 declarations
[16] = retval
;
171 * Generate the body of a vector constructor that takes a single scalar
174 generate_vec_body_from_scalar(exec_list
*instructions
,
175 ir_variable
**declarations
)
177 ir_instruction
*inst
;
179 /* Generate a single assignment of the parameter to __retval.x and return
180 * __retval.xxxx for however many vector components there are.
182 ir_dereference
*const lhs_ref
= new ir_dereference(declarations
[16]);
183 ir_dereference
*const rhs
= new ir_dereference(declarations
[0]);
185 ir_swizzle
*lhs
= new ir_swizzle(lhs_ref
, 0, 0, 0, 0, 1);
187 inst
= new ir_assignment(lhs
, rhs
, NULL
);
188 instructions
->push_tail(inst
);
190 ir_dereference
*const retref
= new ir_dereference(declarations
[16]);
192 ir_swizzle
*retval
= new ir_swizzle(retref
, 0, 0, 0, 0,
193 declarations
[16]->type
->vector_elements
);
195 inst
= new ir_return(retval
);
196 instructions
->push_tail(inst
);
201 * Generate the body of a vector constructor that takes multiple scalars
204 generate_vec_body_from_N_scalars(exec_list
*instructions
,
205 ir_variable
**declarations
)
207 ir_instruction
*inst
;
208 const glsl_type
*const vec_type
= declarations
[16]->type
;
211 /* Generate an assignment of each parameter to a single component of
212 * __retval.x and return __retval.
214 for (unsigned i
= 0; i
< vec_type
->vector_elements
; i
++) {
215 ir_dereference
*const lhs_ref
= new ir_dereference(declarations
[16]);
216 ir_dereference
*const rhs
= new ir_dereference(declarations
[i
]);
218 ir_swizzle
*lhs
= new ir_swizzle(lhs_ref
, 1, 0, 0, 0, 1);
220 inst
= new ir_assignment(lhs
, rhs
, NULL
);
221 instructions
->push_tail(inst
);
224 ir_dereference
*retval
= new ir_dereference(declarations
[16]);
226 inst
= new ir_return(retval
);
227 instructions
->push_tail(inst
);
232 * Generate the body of a matrix constructor that takes a single scalar
235 generate_mat_body_from_scalar(exec_list
*instructions
,
236 ir_variable
**declarations
)
238 ir_instruction
*inst
;
240 /* Generate an assignment of the parameter to the X component of a
241 * temporary vector. Set the remaining fields of the vector to 0. The
242 * size of the vector is equal to the number of rows of the matrix.
244 * Set each column of the matrix to a successive "rotation" of the
245 * temporary vector. This fills the matrix with 0s, but writes the single
246 * scalar along the matrix's diagonal.
248 * For a mat4x3, this is equivalent to:
255 * __retval[0] = tmp.xyy;
256 * __retval[1] = tmp.yxy;
257 * __retval[2] = tmp.yyx;
258 * __retval[3] = tmp.yyy;
260 const glsl_type
*const column_type
= declarations
[16]->type
->column_type();
261 const glsl_type
*const row_type
= declarations
[16]->type
->row_type();
262 ir_variable
*const column
= new ir_variable(column_type
, "v");
264 instructions
->push_tail(column
);
266 ir_dereference
*const lhs_ref
= new ir_dereference(column
);
267 ir_dereference
*const rhs
= new ir_dereference(declarations
[0]);
269 ir_swizzle
*lhs
= new ir_swizzle(lhs_ref
, 0, 0, 0, 0, 1);
271 inst
= new ir_assignment(lhs
, rhs
, NULL
);
272 instructions
->push_tail(inst
);
274 const float z
= 0.0f
;
275 ir_constant
*const zero
= new ir_constant(glsl_type::float_type
, &z
);
277 for (unsigned i
= 1; i
< column_type
->vector_elements
; i
++) {
278 ir_dereference
*const lhs_ref
= new ir_dereference(column
);
280 ir_swizzle
*lhs
= new ir_swizzle(lhs_ref
, i
, 0, 0, 0, 1);
282 inst
= new ir_assignment(lhs
, zero
, NULL
);
283 instructions
->push_tail(inst
);
287 for (unsigned i
= 0; i
< row_type
->vector_elements
; i
++) {
288 static const unsigned swiz
[] = { 1, 1, 1, 0, 1, 1, 1 };
289 ir_dereference
*const rhs_ref
= new ir_dereference(column
);
291 /* This will be .xyyy when i=0, .yxyy when i=1, etc.
293 ir_swizzle
*rhs
= new ir_swizzle(rhs_ref
, swiz
[3 - i
], swiz
[4 - i
],
294 swiz
[5 - i
], swiz
[6 - i
],
295 column_type
->vector_elements
);
297 ir_constant
*const idx
= new ir_constant(glsl_type::int_type
, &i
);
298 ir_dereference
*const lhs
= new ir_dereference(declarations
[16], idx
);
300 inst
= new ir_assignment(lhs
, rhs
, NULL
);
301 instructions
->push_tail(inst
);
304 ir_dereference
*const retval
= new ir_dereference(declarations
[16]);
305 inst
= new ir_return(retval
);
306 instructions
->push_tail(inst
);
311 * Generate the body of a vector constructor that takes multiple scalars
314 generate_mat_body_from_N_scalars(exec_list
*instructions
,
315 ir_variable
**declarations
)
317 ir_instruction
*inst
;
318 const glsl_type
*const row_type
= declarations
[16]->type
->row_type();
319 const glsl_type
*const column_type
= declarations
[16]->type
->column_type();
322 /* Generate an assignment of each parameter to a single component of
323 * of a particular column of __retval and return __retval.
325 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++) {
326 for (unsigned j
= 0; j
< row_type
->vector_elements
; j
++) {
327 ir_constant
*row_index
= new ir_constant(glsl_type::int_type
, &i
);
328 ir_dereference
*const row_access
=
329 new ir_dereference(declarations
[16], row_index
);
331 ir_dereference
*const component_access_ref
=
332 new ir_dereference(row_access
);
334 ir_swizzle
*component_access
= new ir_swizzle(component_access_ref
,
337 const unsigned param
= (i
* row_type
->vector_elements
) + j
;
338 ir_dereference
*const rhs
= new ir_dereference(declarations
[param
]);
340 inst
= new ir_assignment(component_access
, rhs
, NULL
);
341 instructions
->push_tail(inst
);
345 ir_dereference
*retval
= new ir_dereference(declarations
[16]);
347 inst
= new ir_return(retval
);
348 instructions
->push_tail(inst
);
353 * Generate the constructors for a set of GLSL types
355 * Constructor implementations are added to \c instructions, and the symbols
356 * are added to \c symtab.
359 generate_constructor(glsl_symbol_table
*symtab
, const struct glsl_type
*types
,
360 unsigned num_types
, exec_list
*instructions
)
362 ir_variable
*declarations
[17];
364 for (unsigned i
= 0; i
< num_types
; i
++) {
365 /* Only numeric and boolean vectors and matrices get constructors here.
366 * Structures need to be handled elsewhere. It is expected that scalar
367 * constructors are never actually called, so they are not generated.
369 if (!types
[i
].is_numeric() && !types
[i
].is_boolean())
372 if (types
[i
].is_scalar())
375 /* Generate the function name and add it to the symbol table.
377 ir_function
*const f
= new ir_function(types
[i
].name
);
379 bool added
= symtab
->add_function(types
[i
].name
, f
);
383 /* Each type has several basic constructors. The total number of forms
384 * depends on the derived type.
386 * Vectors: 1 scalar, N scalars
387 * Matrices: 1 scalar, NxM scalars
389 * Several possible types of constructors are not included in this list.
391 * Scalar constructors are not included. The expectation is that the
392 * IR generator won't actually generate these as constructor calls. The
393 * expectation is that it will just generate the necessary type
396 * Matrix contructors from matrices are also not included. The
397 * expectation is that the IR generator will generate a call to the
398 * appropriate from-scalars constructor.
400 ir_function_signature
*const sig
= new ir_function_signature(& types
[i
]);
401 f
->signatures
.push_tail(sig
);
404 generate_constructor_intro(& types
[i
], 1, & sig
->parameters
,
405 instructions
, declarations
);
407 if (types
[i
].is_vector()) {
408 generate_vec_body_from_scalar(instructions
, declarations
);
410 ir_function_signature
*const vec_sig
=
411 new ir_function_signature(& types
[i
]);
412 f
->signatures
.push_tail(vec_sig
);
414 vec_sig
->definition
=
415 generate_constructor_intro(& types
[i
], types
[i
].vector_elements
,
416 & vec_sig
->parameters
, instructions
,
418 generate_vec_body_from_N_scalars(instructions
, declarations
);
420 assert(types
[i
].is_matrix());
422 generate_mat_body_from_scalar(instructions
, declarations
);
424 ir_function_signature
*const mat_sig
=
425 new ir_function_signature(& types
[i
]);
426 f
->signatures
.push_tail(mat_sig
);
428 mat_sig
->definition
=
429 generate_constructor_intro(& types
[i
],
430 (types
[i
].vector_elements
431 * types
[i
].matrix_columns
),
432 & mat_sig
->parameters
, instructions
,
434 generate_mat_body_from_N_scalars(instructions
, declarations
);
441 generate_110_constructors(glsl_symbol_table
*symtab
, exec_list
*instructions
)
443 generate_constructor(symtab
, builtin_core_types
,
444 Elements(builtin_core_types
), instructions
);
449 generate_120_constructors(glsl_symbol_table
*symtab
, exec_list
*instructions
)
451 generate_110_constructors(symtab
, instructions
);
453 generate_constructor(symtab
, builtin_120_types
,
454 Elements(builtin_120_types
), instructions
);
459 generate_130_constructors(glsl_symbol_table
*symtab
, exec_list
*instructions
)
461 generate_120_constructors(symtab
, instructions
);
463 generate_constructor(symtab
, builtin_130_types
,
464 Elements(builtin_130_types
), instructions
);
469 _mesa_glsl_initialize_constructors(exec_list
*instructions
,
470 struct _mesa_glsl_parse_state
*state
)
472 switch (state
->language_version
) {
474 generate_110_constructors(state
->symbols
, instructions
);
477 generate_120_constructors(state
->symbols
, instructions
);
480 generate_130_constructors(state
->symbols
, instructions
);
490 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
492 if (base_type
== GLSL_TYPE_VOID
)
495 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
498 /* Treat GLSL vectors as Nx1 matrices.
503 return uint_type
+ (rows
- 1);
505 return int_type
+ (rows
- 1);
506 case GLSL_TYPE_FLOAT
:
507 return float_type
+ (rows
- 1);
509 return bool_type
+ (rows
- 1);
514 if ((base_type
!= GLSL_TYPE_FLOAT
) || (rows
== 1))
517 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
518 * combinations are valid:
526 #define IDX(c,r) (((c-1)*3) + (r-1))
528 switch (IDX(columns
, rows
)) {
529 case IDX(2,2): return mat2_type
;
530 case IDX(2,3): return mat2x3_type
;
531 case IDX(2,4): return mat2x4_type
;
532 case IDX(3,2): return mat3x2_type
;
533 case IDX(3,3): return mat3_type
;
534 case IDX(3,4): return mat3x4_type
;
535 case IDX(4,2): return mat4x2_type
;
536 case IDX(4,3): return mat4x3_type
;
537 case IDX(4,4): return mat4_type
;
538 default: return error_type
;
542 assert(!"Should not get here.");