glsl: Refactor variable declaration handling.
[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 "main/core.h" /* for Elements */
27 #include "glsl_symbol_table.h"
28 #include "glsl_parser_extras.h"
29 #include "glsl_types.h"
30 #include "builtin_types.h"
31 extern "C" {
32 #include "program/hash_table.h"
33 }
34
35 hash_table *glsl_type::array_types = NULL;
36 hash_table *glsl_type::record_types = NULL;
37 void *glsl_type::mem_ctx = NULL;
38
39 void
40 glsl_type::init_talloc_type_ctx(void)
41 {
42 if (glsl_type::mem_ctx == NULL) {
43 glsl_type::mem_ctx = talloc_autofree_context();
44 assert(glsl_type::mem_ctx != NULL);
45 }
46 }
47
48 glsl_type::glsl_type(GLenum gl_type,
49 unsigned base_type, unsigned vector_elements,
50 unsigned matrix_columns, const char *name) :
51 gl_type(gl_type),
52 base_type(base_type),
53 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
54 sampler_type(0),
55 vector_elements(vector_elements), matrix_columns(matrix_columns),
56 length(0)
57 {
58 init_talloc_type_ctx();
59 this->name = talloc_strdup(this->mem_ctx, name);
60 /* Neither dimension is zero or both dimensions are zero.
61 */
62 assert((vector_elements == 0) == (matrix_columns == 0));
63 memset(& fields, 0, sizeof(fields));
64 }
65
66 glsl_type::glsl_type(GLenum gl_type,
67 enum glsl_sampler_dim dim, bool shadow, bool array,
68 unsigned type, const char *name) :
69 gl_type(gl_type),
70 base_type(GLSL_TYPE_SAMPLER),
71 sampler_dimensionality(dim), sampler_shadow(shadow),
72 sampler_array(array), sampler_type(type),
73 vector_elements(0), matrix_columns(0),
74 length(0)
75 {
76 init_talloc_type_ctx();
77 this->name = talloc_strdup(this->mem_ctx, name);
78 memset(& fields, 0, sizeof(fields));
79 }
80
81 glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
82 const char *name) :
83 base_type(GLSL_TYPE_STRUCT),
84 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
85 sampler_type(0),
86 vector_elements(0), matrix_columns(0),
87 length(num_fields)
88 {
89 unsigned int i;
90
91 init_talloc_type_ctx();
92 this->name = talloc_strdup(this->mem_ctx, name);
93 this->fields.structure = talloc_array(this->mem_ctx,
94 glsl_struct_field, length);
95 for (i = 0; i < length; i++) {
96 this->fields.structure[i].type = fields[i].type;
97 this->fields.structure[i].name = talloc_strdup(this->fields.structure,
98 fields[i].name);
99 }
100 }
101
102 static void
103 add_types_to_symbol_table(glsl_symbol_table *symtab,
104 const struct glsl_type *types,
105 unsigned num_types, bool warn)
106 {
107 (void) warn;
108
109 for (unsigned i = 0; i < num_types; i++) {
110 symtab->add_type(types[i].name, & types[i]);
111 }
112 }
113
114
115 void
116 glsl_type::generate_110_types(glsl_symbol_table *symtab)
117 {
118 add_types_to_symbol_table(symtab, builtin_core_types,
119 Elements(builtin_core_types),
120 false);
121 add_types_to_symbol_table(symtab, builtin_structure_types,
122 Elements(builtin_structure_types),
123 false);
124 add_types_to_symbol_table(symtab, builtin_110_deprecated_structure_types,
125 Elements(builtin_110_deprecated_structure_types),
126 false);
127 add_types_to_symbol_table(symtab, & void_type, 1, false);
128 }
129
130
131 void
132 glsl_type::generate_120_types(glsl_symbol_table *symtab)
133 {
134 generate_110_types(symtab);
135
136 add_types_to_symbol_table(symtab, builtin_120_types,
137 Elements(builtin_120_types), false);
138 }
139
140
141 void
142 glsl_type::generate_130_types(glsl_symbol_table *symtab)
143 {
144 generate_120_types(symtab);
145
146 add_types_to_symbol_table(symtab, builtin_130_types,
147 Elements(builtin_130_types), false);
148 generate_EXT_texture_array_types(symtab, false);
149 }
150
151
152 void
153 glsl_type::generate_ARB_texture_rectangle_types(glsl_symbol_table *symtab,
154 bool warn)
155 {
156 add_types_to_symbol_table(symtab, builtin_ARB_texture_rectangle_types,
157 Elements(builtin_ARB_texture_rectangle_types),
158 warn);
159 }
160
161
162 void
163 glsl_type::generate_EXT_texture_array_types(glsl_symbol_table *symtab,
164 bool warn)
165 {
166 add_types_to_symbol_table(symtab, builtin_EXT_texture_array_types,
167 Elements(builtin_EXT_texture_array_types),
168 warn);
169 }
170
171
172 void
173 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state)
174 {
175 switch (state->language_version) {
176 case 110:
177 glsl_type::generate_110_types(state->symbols);
178 break;
179 case 120:
180 glsl_type::generate_120_types(state->symbols);
181 break;
182 case 130:
183 glsl_type::generate_130_types(state->symbols);
184 break;
185 default:
186 /* error */
187 break;
188 }
189
190 if (state->ARB_texture_rectangle_enable) {
191 glsl_type::generate_ARB_texture_rectangle_types(state->symbols,
192 state->ARB_texture_rectangle_warn);
193 }
194
195 if (state->EXT_texture_array_enable && state->language_version < 130) {
196 // These are already included in 130; don't create twice.
197 glsl_type::generate_EXT_texture_array_types(state->symbols,
198 state->EXT_texture_array_warn);
199 }
200 }
201
202
203 const glsl_type *glsl_type::get_base_type() const
204 {
205 switch (base_type) {
206 case GLSL_TYPE_UINT:
207 return uint_type;
208 case GLSL_TYPE_INT:
209 return int_type;
210 case GLSL_TYPE_FLOAT:
211 return float_type;
212 case GLSL_TYPE_BOOL:
213 return bool_type;
214 default:
215 return error_type;
216 }
217 }
218
219
220 void
221 _mesa_glsl_release_types(void)
222 {
223 if (glsl_type::array_types != NULL) {
224 hash_table_dtor(glsl_type::array_types);
225 glsl_type::array_types = NULL;
226 }
227
228 if (glsl_type::record_types != NULL) {
229 hash_table_dtor(glsl_type::record_types);
230 glsl_type::record_types = NULL;
231 }
232 }
233
234
235 ir_function *
236 glsl_type::generate_constructor() const
237 {
238 void *ctx = (void *) this;
239
240 ir_function *const f = new(ctx) ir_function(name);
241 ir_function_signature *const sig = new(ctx) ir_function_signature(this);
242 f->add_signature(sig);
243
244 ir_variable **declarations =
245 (ir_variable **) malloc(sizeof(ir_variable *) * this->length);
246 for (unsigned i = 0; i < length; i++) {
247 char *const param_name = (char *) malloc(10);
248
249 snprintf(param_name, 10, "p%08X", i);
250
251 ir_variable *var = (this->base_type == GLSL_TYPE_ARRAY)
252 ? new(ctx) ir_variable(fields.array, param_name, ir_var_in)
253 : new(ctx) ir_variable(fields.structure[i].type, param_name, ir_var_in);
254
255 declarations[i] = var;
256 sig->parameters.push_tail(var);
257 }
258
259 /* Generate the body of the constructor. The body assigns each of the
260 * parameters to a portion of a local variable called _ret_val that has
261 * the same type as the constructor. After initializing _ret_val,
262 * _ret_val is returned.
263 */
264 ir_variable *retval = new(ctx) ir_variable(this, "_ret_val", ir_var_auto);
265 sig->body.push_tail(retval);
266
267 for (unsigned i = 0; i < length; i++) {
268 ir_dereference *const lhs = (this->base_type == GLSL_TYPE_ARRAY)
269 ? (ir_dereference *) new(ctx) ir_dereference_array(retval,
270 new(ctx) ir_constant(i))
271 : (ir_dereference *) new(ctx) ir_dereference_record(retval,
272 fields.structure[i].name);
273
274 ir_dereference *const rhs = new(ctx) ir_dereference_variable(declarations[i]);
275 ir_instruction *const assign = new(ctx) ir_assignment(lhs, rhs, NULL);
276
277 sig->body.push_tail(assign);
278 }
279
280 free(declarations);
281
282 ir_dereference *const retref = new(ctx) ir_dereference_variable(retval);
283 ir_instruction *const inst = new(ctx) ir_return(retref);
284 sig->body.push_tail(inst);
285
286 return f;
287 }
288
289
290 glsl_type::glsl_type(const glsl_type *array, unsigned length) :
291 base_type(GLSL_TYPE_ARRAY),
292 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
293 sampler_type(0),
294 vector_elements(0), matrix_columns(0),
295 name(NULL), length(length)
296 {
297 this->fields.array = array;
298 /* Inherit the gl type of the base. The GL type is used for
299 * uniform/statevar handling in Mesa and the arrayness of the type
300 * is represented by the size rather than the type.
301 */
302 this->gl_type = array->gl_type;
303
304 /* Allow a maximum of 10 characters for the array size. This is enough
305 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
306 * NUL.
307 */
308 const unsigned name_length = strlen(array->name) + 10 + 3;
309 char *const n = (char *) talloc_size(this->mem_ctx, name_length);
310
311 if (length == 0)
312 snprintf(n, name_length, "%s[]", array->name);
313 else
314 snprintf(n, name_length, "%s[%u]", array->name, length);
315
316 this->name = n;
317 }
318
319
320 const glsl_type *
321 glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
322 {
323 if (base_type == GLSL_TYPE_VOID)
324 return &void_type;
325
326 if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4))
327 return error_type;
328
329 /* Treat GLSL vectors as Nx1 matrices.
330 */
331 if (columns == 1) {
332 switch (base_type) {
333 case GLSL_TYPE_UINT:
334 return uint_type + (rows - 1);
335 case GLSL_TYPE_INT:
336 return int_type + (rows - 1);
337 case GLSL_TYPE_FLOAT:
338 return float_type + (rows - 1);
339 case GLSL_TYPE_BOOL:
340 return bool_type + (rows - 1);
341 default:
342 return error_type;
343 }
344 } else {
345 if ((base_type != GLSL_TYPE_FLOAT) || (rows == 1))
346 return error_type;
347
348 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
349 * combinations are valid:
350 *
351 * 1 2 3 4
352 * 1
353 * 2 x x x
354 * 3 x x x
355 * 4 x x x
356 */
357 #define IDX(c,r) (((c-1)*3) + (r-1))
358
359 switch (IDX(columns, rows)) {
360 case IDX(2,2): return mat2_type;
361 case IDX(2,3): return mat2x3_type;
362 case IDX(2,4): return mat2x4_type;
363 case IDX(3,2): return mat3x2_type;
364 case IDX(3,3): return mat3_type;
365 case IDX(3,4): return mat3x4_type;
366 case IDX(4,2): return mat4x2_type;
367 case IDX(4,3): return mat4x3_type;
368 case IDX(4,4): return mat4_type;
369 default: return error_type;
370 }
371 }
372
373 assert(!"Should not get here.");
374 return error_type;
375 }
376
377
378 const glsl_type *
379 glsl_type::get_array_instance(const glsl_type *base, unsigned array_size)
380 {
381
382 if (array_types == NULL) {
383 array_types = hash_table_ctor(64, hash_table_string_hash,
384 hash_table_string_compare);
385 }
386
387 /* Generate a name using the base type pointer in the key. This is
388 * done because the name of the base type may not be unique across
389 * shaders. For example, two shaders may have different record types
390 * named 'foo'.
391 */
392 char key[128];
393 snprintf(key, sizeof(key), "%p[%u]", (void *) base, array_size);
394
395 const glsl_type *t = (glsl_type *) hash_table_find(array_types, key);
396 if (t == NULL) {
397 t = new glsl_type(base, array_size);
398
399 hash_table_insert(array_types, (void *) t, talloc_strdup(mem_ctx, key));
400 }
401
402 assert(t->base_type == GLSL_TYPE_ARRAY);
403 assert(t->length == array_size);
404 assert(t->fields.array == base);
405
406 return t;
407 }
408
409
410 int
411 glsl_type::record_key_compare(const void *a, const void *b)
412 {
413 const glsl_type *const key1 = (glsl_type *) a;
414 const glsl_type *const key2 = (glsl_type *) b;
415
416 /* Return zero is the types match (there is zero difference) or non-zero
417 * otherwise.
418 */
419 if (strcmp(key1->name, key2->name) != 0)
420 return 1;
421
422 if (key1->length != key2->length)
423 return 1;
424
425 for (unsigned i = 0; i < key1->length; i++) {
426 if (key1->fields.structure[i].type != key2->fields.structure[i].type)
427 return 1;
428 if (strcmp(key1->fields.structure[i].name,
429 key2->fields.structure[i].name) != 0)
430 return 1;
431 }
432
433 return 0;
434 }
435
436
437 unsigned
438 glsl_type::record_key_hash(const void *a)
439 {
440 const glsl_type *const key = (glsl_type *) a;
441 char hash_key[128];
442 unsigned size = 0;
443
444 size = snprintf(hash_key, sizeof(hash_key), "%08x", key->length);
445
446 for (unsigned i = 0; i < key->length; i++) {
447 if (size >= sizeof(hash_key))
448 break;
449
450 size += snprintf(& hash_key[size], sizeof(hash_key) - size,
451 "%p", (void *) key->fields.structure[i].type);
452 }
453
454 return hash_table_string_hash(& hash_key);
455 }
456
457
458 const glsl_type *
459 glsl_type::get_record_instance(const glsl_struct_field *fields,
460 unsigned num_fields,
461 const char *name)
462 {
463 const glsl_type key(fields, num_fields, name);
464
465 if (record_types == NULL) {
466 record_types = hash_table_ctor(64, record_key_hash, record_key_compare);
467 }
468
469 const glsl_type *t = (glsl_type *) hash_table_find(record_types, & key);
470 if (t == NULL) {
471 t = new glsl_type(fields, num_fields, name);
472
473 hash_table_insert(record_types, (void *) t, t);
474 }
475
476 assert(t->base_type == GLSL_TYPE_STRUCT);
477 assert(t->length == num_fields);
478 assert(strcmp(t->name, name) == 0);
479
480 return t;
481 }
482
483
484 const glsl_type *
485 glsl_type::field_type(const char *name) const
486 {
487 if (this->base_type != GLSL_TYPE_STRUCT)
488 return error_type;
489
490 for (unsigned i = 0; i < this->length; i++) {
491 if (strcmp(name, this->fields.structure[i].name) == 0)
492 return this->fields.structure[i].type;
493 }
494
495 return error_type;
496 }
497
498
499 int
500 glsl_type::field_index(const char *name) const
501 {
502 if (this->base_type != GLSL_TYPE_STRUCT)
503 return -1;
504
505 for (unsigned i = 0; i < this->length; i++) {
506 if (strcmp(name, this->fields.structure[i].name) == 0)
507 return i;
508 }
509
510 return -1;
511 }
512
513
514 unsigned
515 glsl_type::component_slots() const
516 {
517 switch (this->base_type) {
518 case GLSL_TYPE_UINT:
519 case GLSL_TYPE_INT:
520 case GLSL_TYPE_FLOAT:
521 case GLSL_TYPE_BOOL:
522 return this->components();
523
524 case GLSL_TYPE_STRUCT: {
525 unsigned size = 0;
526
527 for (unsigned i = 0; i < this->length; i++)
528 size += this->fields.structure[i].type->component_slots();
529
530 return size;
531 }
532
533 case GLSL_TYPE_ARRAY:
534 return this->length * this->fields.array->component_slots();
535
536 default:
537 return 0;
538 }
539 }