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
;
170 * Generate the body of a vector constructor that takes a single scalar
173 generate_vec_body_from_scalar(exec_list
*instructions
,
174 ir_variable
**declarations
)
176 ir_instruction
*inst
;
178 /* Generate a single assignment of the parameter to __retval.x and return
179 * __retval.xxxx for however many vector components there are.
181 ir_dereference
*const lhs_ref
= new ir_dereference(declarations
[16]);
182 ir_dereference
*const rhs
= new ir_dereference(declarations
[0]);
184 ir_swizzle
*lhs
= new ir_swizzle(lhs_ref
, 0, 0, 0, 0, 1);
186 inst
= new ir_assignment(lhs
, rhs
, NULL
);
187 instructions
->push_tail(inst
);
189 ir_dereference
*const retref
= new ir_dereference(declarations
[16]);
191 ir_swizzle
*retval
= new ir_swizzle(retref
, 0, 0, 0, 0,
192 declarations
[16]->type
->vector_elements
);
194 inst
= new ir_return(retval
);
195 instructions
->push_tail(inst
);
200 * Generate the body of a vector constructor that takes multiple scalars
203 generate_vec_body_from_N_scalars(exec_list
*instructions
,
204 ir_variable
**declarations
)
206 ir_instruction
*inst
;
207 const glsl_type
*const vec_type
= declarations
[16]->type
;
210 /* Generate an assignment of each parameter to a single component of
211 * __retval.x and return __retval.
213 for (unsigned i
= 0; i
< vec_type
->vector_elements
; i
++) {
214 ir_dereference
*const lhs_ref
= new ir_dereference(declarations
[16]);
215 ir_dereference
*const rhs
= new ir_dereference(declarations
[i
]);
217 ir_swizzle
*lhs
= new ir_swizzle(lhs_ref
, 1, 0, 0, 0, 1);
219 inst
= new ir_assignment(lhs
, rhs
, NULL
);
220 instructions
->push_tail(inst
);
223 ir_dereference
*retval
= new ir_dereference(declarations
[16]);
225 inst
= new ir_return(retval
);
226 instructions
->push_tail(inst
);
231 * Generate the body of a matrix constructor that takes a single scalar
234 generate_mat_body_from_scalar(exec_list
*instructions
,
235 ir_variable
**declarations
)
237 ir_instruction
*inst
;
239 /* Generate an assignment of the parameter to the X component of a
240 * temporary vector. Set the remaining fields of the vector to 0. The
241 * size of the vector is equal to the number of rows of the matrix.
243 * Set each column of the matrix to a successive "rotation" of the
244 * temporary vector. This fills the matrix with 0s, but writes the single
245 * scalar along the matrix's diagonal.
247 * For a mat4x3, this is equivalent to:
254 * __retval[0] = tmp.xyy;
255 * __retval[1] = tmp.yxy;
256 * __retval[2] = tmp.yyx;
257 * __retval[3] = tmp.yyy;
259 const glsl_type
*const column_type
= declarations
[16]->type
->column_type();
260 const glsl_type
*const row_type
= declarations
[16]->type
->row_type();
261 ir_variable
*const column
= new ir_variable(column_type
, "v");
263 instructions
->push_tail(column
);
265 ir_dereference
*const lhs_ref
= new ir_dereference(column
);
266 ir_dereference
*const rhs
= new ir_dereference(declarations
[0]);
268 ir_swizzle
*lhs
= new ir_swizzle(lhs_ref
, 0, 0, 0, 0, 1);
270 inst
= new ir_assignment(lhs
, rhs
, NULL
);
271 instructions
->push_tail(inst
);
273 const float z
= 0.0f
;
274 ir_constant
*const zero
= new ir_constant(glsl_type::float_type
, &z
);
276 for (unsigned i
= 1; i
< column_type
->vector_elements
; i
++) {
277 ir_dereference
*const lhs_ref
= new ir_dereference(column
);
279 ir_swizzle
*lhs
= new ir_swizzle(lhs_ref
, i
, 0, 0, 0, 1);
281 inst
= new ir_assignment(lhs
, zero
, NULL
);
282 instructions
->push_tail(inst
);
286 for (unsigned i
= 0; i
< row_type
->vector_elements
; i
++) {
287 static const unsigned swiz
[] = { 1, 1, 1, 0, 1, 1, 1 };
288 ir_dereference
*const rhs_ref
= new ir_dereference(column
);
290 /* This will be .xyyy when i=0, .yxyy when i=1, etc.
292 ir_swizzle
*rhs
= new ir_swizzle(rhs_ref
, swiz
[3 - i
], swiz
[4 - i
],
293 swiz
[5 - i
], swiz
[6 - i
],
294 column_type
->vector_elements
);
296 ir_constant
*const idx
= new ir_constant(glsl_type::int_type
, &i
);
297 ir_dereference
*const lhs
= new ir_dereference(declarations
[16], idx
);
299 inst
= new ir_assignment(lhs
, rhs
, NULL
);
300 instructions
->push_tail(inst
);
303 ir_dereference
*const retval
= new ir_dereference(declarations
[16]);
304 inst
= new ir_return(retval
);
305 instructions
->push_tail(inst
);
310 * Generate the body of a vector constructor that takes multiple scalars
313 generate_mat_body_from_N_scalars(exec_list
*instructions
,
314 ir_variable
**declarations
)
316 ir_instruction
*inst
;
317 const glsl_type
*const row_type
= declarations
[16]->type
->row_type();
318 const glsl_type
*const column_type
= declarations
[16]->type
->column_type();
321 /* Generate an assignment of each parameter to a single component of
322 * of a particular column of __retval and return __retval.
324 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++) {
325 for (unsigned j
= 0; j
< row_type
->vector_elements
; j
++) {
326 ir_constant
*row_index
= new ir_constant(glsl_type::int_type
, &i
);
327 ir_dereference
*const row_access
=
328 new ir_dereference(declarations
[16], row_index
);
330 ir_dereference
*const component_access_ref
=
331 new ir_dereference(row_access
);
333 ir_swizzle
*component_access
= new ir_swizzle(component_access_ref
,
336 const unsigned param
= (i
* row_type
->vector_elements
) + j
;
337 ir_dereference
*const rhs
= new ir_dereference(declarations
[param
]);
339 inst
= new ir_assignment(component_access
, rhs
, NULL
);
340 instructions
->push_tail(inst
);
344 ir_dereference
*retval
= new ir_dereference(declarations
[16]);
346 inst
= new ir_return(retval
);
347 instructions
->push_tail(inst
);
352 * Generate the constructors for a set of GLSL types
354 * Constructor implementations are added to \c instructions, and the symbols
355 * are added to \c symtab.
358 generate_constructor(glsl_symbol_table
*symtab
, const struct glsl_type
*types
,
359 unsigned num_types
, exec_list
*instructions
)
361 ir_variable
*declarations
[17];
363 for (unsigned i
= 0; i
< num_types
; i
++) {
364 /* Only numeric and boolean vectors and matrices get constructors here.
365 * Structures need to be handled elsewhere. It is expected that scalar
366 * constructors are never actually called, so they are not generated.
368 if (!types
[i
].is_numeric() && !types
[i
].is_boolean())
371 if (types
[i
].is_scalar())
374 /* Generate the function name and add it to the symbol table.
376 ir_function
*const f
= new ir_function(types
[i
].name
);
378 bool added
= symtab
->add_function(types
[i
].name
, f
);
382 /* Each type has several basic constructors. The total number of forms
383 * depends on the derived type.
385 * Vectors: 1 scalar, N scalars
386 * Matrices: 1 scalar, NxM scalars
388 * Several possible types of constructors are not included in this list.
390 * Scalar constructors are not included. The expectation is that the
391 * IR generator won't actually generate these as constructor calls. The
392 * expectation is that it will just generate the necessary type
395 * Matrix contructors from matrices are also not included. The
396 * expectation is that the IR generator will generate a call to the
397 * appropriate from-scalars constructor.
399 ir_function_signature
*const sig
= new ir_function_signature(& types
[i
]);
400 f
->signatures
.push_tail(sig
);
402 generate_constructor_intro(& types
[i
], 1, & sig
->parameters
,
403 instructions
, declarations
);
405 if (types
[i
].is_vector()) {
406 generate_vec_body_from_scalar(instructions
, declarations
);
408 ir_function_signature
*const vec_sig
=
409 new ir_function_signature(& types
[i
]);
410 f
->signatures
.push_tail(vec_sig
);
412 generate_constructor_intro(& types
[i
], types
[i
].vector_elements
,
413 & vec_sig
->parameters
, instructions
,
415 generate_vec_body_from_N_scalars(instructions
, declarations
);
417 assert(types
[i
].is_matrix());
419 generate_mat_body_from_scalar(instructions
, declarations
);
421 ir_function_signature
*const mat_sig
=
422 new ir_function_signature(& types
[i
]);
423 f
->signatures
.push_tail(mat_sig
);
425 generate_constructor_intro(& types
[i
],
426 (types
[i
].vector_elements
427 * types
[i
].matrix_columns
),
428 & mat_sig
->parameters
, instructions
,
430 generate_mat_body_from_N_scalars(instructions
, declarations
);
437 generate_110_constructors(glsl_symbol_table
*symtab
, exec_list
*instructions
)
439 generate_constructor(symtab
, builtin_core_types
,
440 Elements(builtin_core_types
), instructions
);
445 generate_120_constructors(glsl_symbol_table
*symtab
, exec_list
*instructions
)
447 generate_110_constructors(symtab
, instructions
);
449 generate_constructor(symtab
, builtin_120_types
,
450 Elements(builtin_120_types
), instructions
);
455 generate_130_constructors(glsl_symbol_table
*symtab
, exec_list
*instructions
)
457 generate_120_constructors(symtab
, instructions
);
459 generate_constructor(symtab
, builtin_130_types
,
460 Elements(builtin_130_types
), instructions
);
465 _mesa_glsl_initialize_constructors(exec_list
*instructions
,
466 struct _mesa_glsl_parse_state
*state
)
468 switch (state
->language_version
) {
470 generate_110_constructors(state
->symbols
, instructions
);
473 generate_120_constructors(state
->symbols
, instructions
);
476 generate_130_constructors(state
->symbols
, instructions
);
486 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
488 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
492 /* Treat GLSL vectors as Nx1 matrices.
497 return uint_type
+ (rows
- 1);
499 return int_type
+ (rows
- 1);
500 case GLSL_TYPE_FLOAT
:
501 return float_type
+ (rows
- 1);
503 return bool_type
+ (rows
- 1);
508 if ((base_type
!= GLSL_TYPE_FLOAT
) || (rows
== 1))
511 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
512 * combinations are valid:
520 #define IDX(c,r) (((c-1)*3) + (r-1))
522 switch (IDX(columns
, rows
)) {
523 case IDX(2,2): return mat2_type
;
524 case IDX(2,3): return mat2x3_type
;
525 case IDX(2,4): return mat2x4_type
;
526 case IDX(3,2): return mat3x2_type
;
527 case IDX(3,3): return mat3_type
;
528 case IDX(3,4): return mat3x4_type
;
529 case IDX(4,2): return mat4x2_type
;
530 case IDX(4,3): return mat4x3_type
;
531 case IDX(4,4): return mat4_type
;
532 default: return error_type
;
536 assert(!"Should not get here.");