glsl2: Wrap includes of C interfaces with extern "C".
[mesa.git] / src / glsl / glsl_types.cpp
1 /*
2 * Copyright © 2009 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 #include <cstdio>
25 #include <stdlib.h>
26 #include "glsl_symbol_table.h"
27 #include "glsl_parser_extras.h"
28 #include "glsl_types.h"
29 #include "builtin_types.h"
30 extern "C" {
31 #include "hash_table.h"
32 }
33
34 hash_table *glsl_type::array_types = NULL;
35
36 static void
37 add_types_to_symbol_table(glsl_symbol_table *symtab,
38 const struct glsl_type *types,
39 unsigned num_types, bool warn)
40 {
41 (void) warn;
42
43 for (unsigned i = 0; i < num_types; i++) {
44 symtab->add_type(types[i].name, & types[i]);
45 }
46 }
47
48
49 static void
50 generate_110_types(glsl_symbol_table *symtab)
51 {
52 add_types_to_symbol_table(symtab, builtin_core_types,
53 Elements(builtin_core_types),
54 false);
55 add_types_to_symbol_table(symtab, builtin_structure_types,
56 Elements(builtin_structure_types),
57 false);
58 add_types_to_symbol_table(symtab, builtin_110_deprecated_structure_types,
59 Elements(builtin_110_deprecated_structure_types),
60 false);
61 add_types_to_symbol_table(symtab, & void_type, 1, false);
62 }
63
64
65 static void
66 generate_120_types(glsl_symbol_table *symtab)
67 {
68 generate_110_types(symtab);
69
70 add_types_to_symbol_table(symtab, builtin_120_types,
71 Elements(builtin_120_types), false);
72 }
73
74
75 static void
76 generate_130_types(glsl_symbol_table *symtab)
77 {
78 generate_120_types(symtab);
79
80 add_types_to_symbol_table(symtab, builtin_130_types,
81 Elements(builtin_130_types), false);
82 }
83
84
85 static void
86 generate_ARB_texture_rectangle_types(glsl_symbol_table *symtab, bool warn)
87 {
88 add_types_to_symbol_table(symtab, builtin_ARB_texture_rectangle_types,
89 Elements(builtin_ARB_texture_rectangle_types),
90 warn);
91 }
92
93
94 static void
95 generate_EXT_texture_array_types(glsl_symbol_table *symtab, bool warn)
96 {
97 add_types_to_symbol_table(symtab, builtin_EXT_texture_array_types,
98 Elements(builtin_EXT_texture_array_types),
99 warn);
100 }
101
102
103 void
104 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state)
105 {
106 switch (state->language_version) {
107 case 110:
108 generate_110_types(state->symbols);
109 break;
110 case 120:
111 generate_120_types(state->symbols);
112 break;
113 case 130:
114 generate_130_types(state->symbols);
115 break;
116 default:
117 /* error */
118 break;
119 }
120
121 if (state->ARB_texture_rectangle_enable) {
122 generate_ARB_texture_rectangle_types(state->symbols,
123 state->ARB_texture_rectangle_warn);
124 }
125
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);
130 }
131 }
132
133
134 const glsl_type *glsl_type::get_base_type() const
135 {
136 switch (base_type) {
137 case GLSL_TYPE_UINT:
138 return uint_type;
139 case GLSL_TYPE_INT:
140 return int_type;
141 case GLSL_TYPE_FLOAT:
142 return float_type;
143 case GLSL_TYPE_BOOL:
144 return bool_type;
145 default:
146 return error_type;
147 }
148 }
149
150
151 ir_function *
152 glsl_type::generate_constructor(glsl_symbol_table *symtab) const
153 {
154 void *ctx = symtab;
155
156 /* Generate the function name and add it to the symbol table.
157 */
158 ir_function *const f = new(ctx) ir_function(name);
159
160 bool added = symtab->add_function(name, f);
161 assert(added);
162
163 ir_function_signature *const sig = new(ctx) ir_function_signature(this);
164 f->add_signature(sig);
165
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);
170
171 snprintf(param_name, 10, "p%08X", i);
172
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);
176
177 var->mode = ir_var_in;
178 declarations[i] = var;
179 sig->parameters.push_tail(var);
180 }
181
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.
186 */
187 ir_variable *retval = new(ctx) ir_variable(this, "__retval");
188 sig->body.push_tail(retval);
189
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);
196
197 ir_dereference *const rhs = new(ctx) ir_dereference_variable(declarations[i]);
198 ir_instruction *const assign = new(ctx) ir_assignment(lhs, rhs, NULL);
199
200 sig->body.push_tail(assign);
201 }
202
203 free(declarations);
204
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);
208
209 return f;
210 }
211
212
213 /**
214 * Generate the function intro for a constructor
215 *
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"
220 * scalar parameters.
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
225 * the symbol table.
226 */
227 static ir_function_signature *
228 generate_constructor_intro(void *ctx,
229 const glsl_type *type, unsigned parameter_count,
230 ir_variable **declarations)
231 {
232 /* Names of parameters used in vector and matrix constructors
233 */
234 static const char *const names[] = {
235 "a", "b", "c", "d", "e", "f", "g", "h",
236 "i", "j", "k", "l", "m", "n", "o", "p",
237 };
238
239 assert(parameter_count <= Elements(names));
240
241 const glsl_type *const parameter_type = type->get_base_type();
242
243 ir_function_signature *const signature = new(ctx) ir_function_signature(type);
244
245 for (unsigned i = 0; i < parameter_count; i++) {
246 ir_variable *var = new(ctx) ir_variable(parameter_type, names[i]);
247
248 var->mode = ir_var_in;
249 signature->parameters.push_tail(var);
250
251 declarations[i] = var;
252 }
253
254 ir_variable *retval = new(ctx) ir_variable(type, "__retval");
255 signature->body.push_tail(retval);
256
257 declarations[16] = retval;
258 return signature;
259 }
260
261
262 /**
263 * Generate the body of a vector constructor that takes a single scalar
264 */
265 static void
266 generate_vec_body_from_scalar(void *ctx,
267 exec_list *instructions,
268 ir_variable **declarations)
269 {
270 ir_instruction *inst;
271
272 /* Generate a single assignment of the parameter to __retval.x and return
273 * __retval.xxxx for however many vector components there are.
274 */
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]);
278
279 ir_swizzle *lhs = new(ctx) ir_swizzle(lhs_ref, 0, 0, 0, 0, 1);
280
281 inst = new(ctx) ir_assignment(lhs, rhs, NULL);
282 instructions->push_tail(inst);
283
284 ir_dereference *const retref = new(ctx) ir_dereference_variable(declarations[16]);
285
286 ir_swizzle *retval = new(ctx) ir_swizzle(retref, 0, 0, 0, 0,
287 declarations[16]->type->vector_elements);
288
289 inst = new(ctx) ir_return(retval);
290 instructions->push_tail(inst);
291 }
292
293
294 /**
295 * Generate the body of a vector constructor that takes multiple scalars
296 */
297 static void
298 generate_vec_body_from_N_scalars(void *ctx,
299 exec_list *instructions,
300 ir_variable **declarations)
301 {
302 ir_instruction *inst;
303 const glsl_type *const vec_type = declarations[16]->type;
304
305 /* Generate an assignment of each parameter to a single component of
306 * __retval.x and return __retval.
307 */
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]);
312
313 ir_swizzle *lhs = new(ctx) ir_swizzle(lhs_ref, i, 0, 0, 0, 1);
314
315 inst = new(ctx) ir_assignment(lhs, rhs, NULL);
316 instructions->push_tail(inst);
317 }
318
319 ir_dereference *retval = new(ctx) ir_dereference_variable(declarations[16]);
320
321 inst = new(ctx) ir_return(retval);
322 instructions->push_tail(inst);
323 }
324
325
326 /**
327 * Generate the body of a matrix constructor that takes a single scalar
328 */
329 static void
330 generate_mat_body_from_scalar(void *ctx,
331 exec_list *instructions,
332 ir_variable **declarations)
333 {
334 ir_instruction *inst;
335
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.
339 *
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.
343 *
344 * For a mat4x3, this is equivalent to:
345 *
346 * vec3 tmp;
347 * mat4x3 __retval;
348 * tmp.x = a;
349 * tmp.y = 0.0;
350 * tmp.z = 0.0;
351 * __retval[0] = tmp.xyy;
352 * __retval[1] = tmp.yxy;
353 * __retval[2] = tmp.yyx;
354 * __retval[3] = tmp.yyy;
355 */
356 const glsl_type *const column_type = declarations[16]->type->column_type();
357 const glsl_type *const row_type = declarations[16]->type->row_type();
358
359 ir_variable *const column = new(ctx) ir_variable(column_type, "v");
360
361 instructions->push_tail(column);
362
363 ir_dereference *const lhs_ref = new(ctx) ir_dereference_variable(column);
364 ir_dereference *const rhs = new(ctx) ir_dereference_variable(declarations[0]);
365
366 ir_swizzle *lhs = new(ctx) ir_swizzle(lhs_ref, 0, 0, 0, 0, 1);
367
368 inst = new(ctx) ir_assignment(lhs, rhs, NULL);
369 instructions->push_tail(inst);
370
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);
374
375 ir_swizzle *lhs = new(ctx) ir_swizzle(lhs_ref, i, 0, 0, 0, 1);
376
377 inst = new(ctx) ir_assignment(lhs, zero, NULL);
378 instructions->push_tail(inst);
379 }
380
381
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);
385
386 /* This will be .xyyy when i=0, .yxyy when i=1, etc.
387 */
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);
391
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);
395
396 inst = new(ctx) ir_assignment(lhs, rhs, NULL);
397 instructions->push_tail(inst);
398 }
399
400 ir_dereference *const retval = new(ctx) ir_dereference_variable(declarations[16]);
401 inst = new(ctx) ir_return(retval);
402 instructions->push_tail(inst);
403 }
404
405
406 /**
407 * Generate the body of a vector constructor that takes multiple scalars
408 */
409 static void
410 generate_mat_body_from_N_scalars(void *ctx,
411 exec_list *instructions,
412 ir_variable **declarations)
413 {
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();
417
418 /* Generate an assignment of each parameter to a single component of
419 * of a particular column of __retval and return __retval.
420 */
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);
426
427 ir_swizzle *component_access = new(ctx) ir_swizzle(row_access,
428 j, 0, 0, 0, 1);
429
430 const unsigned param = (i * row_type->vector_elements) + j;
431 ir_dereference *const rhs =
432 new(ctx) ir_dereference_variable(declarations[param]);
433
434 inst = new(ctx) ir_assignment(component_access, rhs, NULL);
435 instructions->push_tail(inst);
436 }
437 }
438
439 ir_dereference *retval = new(ctx) ir_dereference_variable(declarations[16]);
440
441 inst = new(ctx) ir_return(retval);
442 instructions->push_tail(inst);
443 }
444
445
446 /**
447 * Generate the constructors for a set of GLSL types
448 *
449 * Constructor implementations are added to \c instructions, and the symbols
450 * are added to \c symtab.
451 */
452 static void
453 generate_constructor(glsl_symbol_table *symtab, const struct glsl_type *types,
454 unsigned num_types, exec_list *instructions)
455 {
456 void *ctx = symtab;
457 ir_variable *declarations[17];
458
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.
463 */
464 if (!types[i].is_numeric() && !types[i].is_boolean())
465 continue;
466
467 if (types[i].is_scalar())
468 continue;
469
470 /* Generate the function block, add it to the symbol table, and emit it.
471 */
472 ir_function *const f = new(ctx) ir_function(types[i].name);
473
474 bool added = symtab->add_function(types[i].name, f);
475 assert(added);
476
477 instructions->push_tail(f);
478
479 /* Each type has several basic constructors. The total number of forms
480 * depends on the derived type.
481 *
482 * Vectors: 1 scalar, N scalars
483 * Matrices: 1 scalar, NxM scalars
484 *
485 * Several possible types of constructors are not included in this list.
486 *
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
490 * conversion.
491 *
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.
495 */
496 ir_function_signature *const sig =
497 generate_constructor_intro(ctx, &types[i], 1, declarations);
498 f->add_signature(sig);
499
500 if (types[i].is_vector()) {
501 generate_vec_body_from_scalar(ctx, &sig->body, declarations);
502
503 ir_function_signature *const vec_sig =
504 generate_constructor_intro(ctx,
505 &types[i], types[i].vector_elements,
506 declarations);
507 f->add_signature(vec_sig);
508
509 generate_vec_body_from_N_scalars(ctx, &vec_sig->body, declarations);
510 } else {
511 assert(types[i].is_matrix());
512
513 generate_mat_body_from_scalar(ctx, &sig->body, declarations);
514
515 ir_function_signature *const mat_sig =
516 generate_constructor_intro(ctx,
517 &types[i],
518 (types[i].vector_elements
519 * types[i].matrix_columns),
520 declarations);
521 f->add_signature(mat_sig);
522
523 generate_mat_body_from_N_scalars(ctx, &mat_sig->body, declarations);
524 }
525 }
526 }
527
528
529 void
530 generate_110_constructors(glsl_symbol_table *symtab, exec_list *instructions)
531 {
532 generate_constructor(symtab, builtin_core_types,
533 Elements(builtin_core_types), instructions);
534 }
535
536
537 void
538 generate_120_constructors(glsl_symbol_table *symtab, exec_list *instructions)
539 {
540 generate_110_constructors(symtab, instructions);
541
542 generate_constructor(symtab, builtin_120_types,
543 Elements(builtin_120_types), instructions);
544 }
545
546
547 void
548 generate_130_constructors(glsl_symbol_table *symtab, exec_list *instructions)
549 {
550 generate_120_constructors(symtab, instructions);
551
552 generate_constructor(symtab, builtin_130_types,
553 Elements(builtin_130_types), instructions);
554 }
555
556
557 void
558 _mesa_glsl_initialize_constructors(exec_list *instructions,
559 struct _mesa_glsl_parse_state *state)
560 {
561 switch (state->language_version) {
562 case 110:
563 generate_110_constructors(state->symbols, instructions);
564 break;
565 case 120:
566 generate_120_constructors(state->symbols, instructions);
567 break;
568 case 130:
569 generate_130_constructors(state->symbols, instructions);
570 break;
571 default:
572 /* error */
573 break;
574 }
575 }
576
577
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),
581 sampler_type(0),
582 vector_elements(0), matrix_columns(0),
583 name(NULL), length(length)
584 {
585 this->fields.array = array;
586
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
589 * NUL.
590 */
591 const unsigned name_length = strlen(array->name) + 10 + 3;
592 char *const n = (char *) talloc_size(ctx, name_length);
593
594 if (length == 0)
595 snprintf(n, name_length, "%s[]", array->name);
596 else
597 snprintf(n, name_length, "%s[%u]", array->name, length);
598
599 this->name = n;
600 }
601
602
603 const glsl_type *
604 glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
605 {
606 if (base_type == GLSL_TYPE_VOID)
607 return &void_type;
608
609 if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4))
610 return error_type;
611
612 /* Treat GLSL vectors as Nx1 matrices.
613 */
614 if (columns == 1) {
615 switch (base_type) {
616 case GLSL_TYPE_UINT:
617 return uint_type + (rows - 1);
618 case GLSL_TYPE_INT:
619 return int_type + (rows - 1);
620 case GLSL_TYPE_FLOAT:
621 return float_type + (rows - 1);
622 case GLSL_TYPE_BOOL:
623 return bool_type + (rows - 1);
624 default:
625 return error_type;
626 }
627 } else {
628 if ((base_type != GLSL_TYPE_FLOAT) || (rows == 1))
629 return error_type;
630
631 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
632 * combinations are valid:
633 *
634 * 1 2 3 4
635 * 1
636 * 2 x x x
637 * 3 x x x
638 * 4 x x x
639 */
640 #define IDX(c,r) (((c-1)*3) + (r-1))
641
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;
653 }
654 }
655
656 assert(!"Should not get here.");
657 return error_type;
658 }
659
660
661 int
662 glsl_type::array_key_compare(const void *a, const void *b)
663 {
664 const glsl_type *const key1 = (glsl_type *) a;
665 const glsl_type *const key2 = (glsl_type *) b;
666
667 /* Return zero is the types match (there is zero difference) or non-zero
668 * otherwise.
669 */
670 return ((key1->fields.array == key2->fields.array)
671 && (key1->length == key2->length)) ? 0 : 1;
672 }
673
674
675 unsigned
676 glsl_type::array_key_hash(const void *a)
677 {
678 const glsl_type *const key = (glsl_type *) a;
679
680 const struct {
681 const glsl_type *t;
682 unsigned l;
683 char nul;
684 } hash_key = {
685 key->fields.array,
686 key->length,
687 '\0'
688 };
689
690 return hash_table_string_hash(& hash_key);
691 }
692
693
694 const glsl_type *
695 glsl_type::get_array_instance(void *ctx, const glsl_type *base,
696 unsigned array_size)
697 {
698 const glsl_type key(ctx, base, array_size);
699
700 if (array_types == NULL) {
701 array_types = hash_table_ctor(64, array_key_hash, array_key_compare);
702 }
703
704 const glsl_type *t = (glsl_type *) hash_table_find(array_types, & key);
705 if (t == NULL) {
706 t = new(ctx) glsl_type(ctx, base, array_size);
707
708 hash_table_insert(array_types, (void *) t, t);
709 }
710
711 assert(t->base_type == GLSL_TYPE_ARRAY);
712 assert(t->length == array_size);
713 assert(t->fields.array == base);
714
715 return t;
716 }
717
718
719 const glsl_type *
720 glsl_type::field_type(const char *name) const
721 {
722 if (this->base_type != GLSL_TYPE_STRUCT)
723 return error_type;
724
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;
728 }
729
730 return error_type;
731 }
732
733
734 int
735 glsl_type::field_index(const char *name) const
736 {
737 if (this->base_type != GLSL_TYPE_STRUCT)
738 return -1;
739
740 for (unsigned i = 0; i < this->length; i++) {
741 if (strcmp(name, this->fields.structure[i].name) == 0)
742 return i;
743 }
744
745 return -1;
746 }
747
748
749 unsigned
750 glsl_type::component_slots() const
751 {
752 switch (this->base_type) {
753 case GLSL_TYPE_UINT:
754 case GLSL_TYPE_INT:
755 case GLSL_TYPE_FLOAT:
756 case GLSL_TYPE_BOOL:
757 return this->components();
758
759 case GLSL_TYPE_STRUCT: {
760 unsigned size = 0;
761
762 for (unsigned i = 0; i < this->length; i++)
763 size += this->fields.structure[i].type->component_slots();
764
765 return size;
766 }
767
768 case GLSL_TYPE_ARRAY:
769 return this->length * this->fields.array->component_slots();
770
771 default:
772 return 0;
773 }
774 }