glsl_type: Vector, matrix, and sampler type constructors are private
[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 glsl_type::glsl_type(GLenum gl_type,
37 unsigned base_type, unsigned vector_elements,
38 unsigned matrix_columns, const char *name) :
39 gl_type(gl_type),
40 base_type(base_type),
41 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
42 sampler_type(0),
43 vector_elements(vector_elements), matrix_columns(matrix_columns),
44 name(name),
45 length(0)
46 {
47 /* Neither dimension is zero or both dimensions are zero.
48 */
49 assert((vector_elements == 0) == (matrix_columns == 0));
50 memset(& fields, 0, sizeof(fields));
51 }
52
53 glsl_type::glsl_type(GLenum gl_type,
54 enum glsl_sampler_dim dim, bool shadow, bool array,
55 unsigned type, const char *name) :
56 gl_type(gl_type),
57 base_type(GLSL_TYPE_SAMPLER),
58 sampler_dimensionality(dim), sampler_shadow(shadow),
59 sampler_array(array), sampler_type(type),
60 vector_elements(0), matrix_columns(0),
61 name(name),
62 length(0)
63 {
64 memset(& fields, 0, sizeof(fields));
65 }
66
67 static void
68 add_types_to_symbol_table(glsl_symbol_table *symtab,
69 const struct glsl_type *types,
70 unsigned num_types, bool warn)
71 {
72 (void) warn;
73
74 for (unsigned i = 0; i < num_types; i++) {
75 symtab->add_type(types[i].name, & types[i]);
76 }
77 }
78
79
80 void
81 glsl_type::generate_110_types(glsl_symbol_table *symtab)
82 {
83 add_types_to_symbol_table(symtab, builtin_core_types,
84 Elements(builtin_core_types),
85 false);
86 add_types_to_symbol_table(symtab, builtin_structure_types,
87 Elements(builtin_structure_types),
88 false);
89 add_types_to_symbol_table(symtab, builtin_110_deprecated_structure_types,
90 Elements(builtin_110_deprecated_structure_types),
91 false);
92 add_types_to_symbol_table(symtab, & void_type, 1, false);
93 }
94
95
96 void
97 glsl_type::generate_120_types(glsl_symbol_table *symtab)
98 {
99 generate_110_types(symtab);
100
101 add_types_to_symbol_table(symtab, builtin_120_types,
102 Elements(builtin_120_types), false);
103 }
104
105
106 void
107 glsl_type::generate_130_types(glsl_symbol_table *symtab)
108 {
109 generate_120_types(symtab);
110
111 add_types_to_symbol_table(symtab, builtin_130_types,
112 Elements(builtin_130_types), false);
113 }
114
115
116 void
117 glsl_type::generate_ARB_texture_rectangle_types(glsl_symbol_table *symtab,
118 bool warn)
119 {
120 add_types_to_symbol_table(symtab, builtin_ARB_texture_rectangle_types,
121 Elements(builtin_ARB_texture_rectangle_types),
122 warn);
123 }
124
125
126 void
127 glsl_type::generate_EXT_texture_array_types(glsl_symbol_table *symtab,
128 bool warn)
129 {
130 add_types_to_symbol_table(symtab, builtin_EXT_texture_array_types,
131 Elements(builtin_EXT_texture_array_types),
132 warn);
133 }
134
135
136 void
137 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state)
138 {
139 switch (state->language_version) {
140 case 110:
141 glsl_type::generate_110_types(state->symbols);
142 break;
143 case 120:
144 glsl_type::generate_120_types(state->symbols);
145 break;
146 case 130:
147 glsl_type::generate_130_types(state->symbols);
148 break;
149 default:
150 /* error */
151 break;
152 }
153
154 if (state->ARB_texture_rectangle_enable) {
155 glsl_type::generate_ARB_texture_rectangle_types(state->symbols,
156 state->ARB_texture_rectangle_warn);
157 }
158
159 if (state->EXT_texture_array_enable && state->language_version < 130) {
160 // These are already included in 130; don't create twice.
161 glsl_type::generate_EXT_texture_array_types(state->symbols,
162 state->EXT_texture_array_warn);
163 }
164 }
165
166
167 const glsl_type *glsl_type::get_base_type() const
168 {
169 switch (base_type) {
170 case GLSL_TYPE_UINT:
171 return uint_type;
172 case GLSL_TYPE_INT:
173 return int_type;
174 case GLSL_TYPE_FLOAT:
175 return float_type;
176 case GLSL_TYPE_BOOL:
177 return bool_type;
178 default:
179 return error_type;
180 }
181 }
182
183
184 ir_function *
185 glsl_type::generate_constructor(glsl_symbol_table *symtab) const
186 {
187 void *ctx = symtab;
188
189 /* Generate the function name and add it to the symbol table.
190 */
191 ir_function *const f = new(ctx) ir_function(name);
192
193 bool added = symtab->add_function(name, f);
194 assert(added);
195
196 ir_function_signature *const sig = new(ctx) ir_function_signature(this);
197 f->add_signature(sig);
198
199 ir_variable **declarations =
200 (ir_variable **) malloc(sizeof(ir_variable *) * this->length);
201 for (unsigned i = 0; i < length; i++) {
202 char *const param_name = (char *) malloc(10);
203
204 snprintf(param_name, 10, "p%08X", i);
205
206 ir_variable *var = (this->base_type == GLSL_TYPE_ARRAY)
207 ? new(ctx) ir_variable(fields.array, param_name)
208 : new(ctx) ir_variable(fields.structure[i].type, param_name);
209
210 var->mode = ir_var_in;
211 declarations[i] = var;
212 sig->parameters.push_tail(var);
213 }
214
215 /* Generate the body of the constructor. The body assigns each of the
216 * parameters to a portion of a local variable called __retval that has
217 * the same type as the constructor. After initializing __retval,
218 * __retval is returned.
219 */
220 ir_variable *retval = new(ctx) ir_variable(this, "__retval");
221 sig->body.push_tail(retval);
222
223 for (unsigned i = 0; i < length; i++) {
224 ir_dereference *const lhs = (this->base_type == GLSL_TYPE_ARRAY)
225 ? (ir_dereference *) new(ctx) ir_dereference_array(retval,
226 new(ctx) ir_constant(i))
227 : (ir_dereference *) new(ctx) ir_dereference_record(retval,
228 fields.structure[i].name);
229
230 ir_dereference *const rhs = new(ctx) ir_dereference_variable(declarations[i]);
231 ir_instruction *const assign = new(ctx) ir_assignment(lhs, rhs, NULL);
232
233 sig->body.push_tail(assign);
234 }
235
236 free(declarations);
237
238 ir_dereference *const retref = new(ctx) ir_dereference_variable(retval);
239 ir_instruction *const inst = new(ctx) ir_return(retref);
240 sig->body.push_tail(inst);
241
242 return f;
243 }
244
245
246 glsl_type::glsl_type(void *ctx, const glsl_type *array, unsigned length) :
247 base_type(GLSL_TYPE_ARRAY),
248 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
249 sampler_type(0),
250 vector_elements(0), matrix_columns(0),
251 name(NULL), length(length)
252 {
253 this->fields.array = array;
254
255 /* Allow a maximum of 10 characters for the array size. This is enough
256 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
257 * NUL.
258 */
259 const unsigned name_length = strlen(array->name) + 10 + 3;
260 char *const n = (char *) talloc_size(ctx, name_length);
261
262 if (length == 0)
263 snprintf(n, name_length, "%s[]", array->name);
264 else
265 snprintf(n, name_length, "%s[%u]", array->name, length);
266
267 this->name = n;
268 }
269
270
271 const glsl_type *
272 glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
273 {
274 if (base_type == GLSL_TYPE_VOID)
275 return &void_type;
276
277 if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4))
278 return error_type;
279
280 /* Treat GLSL vectors as Nx1 matrices.
281 */
282 if (columns == 1) {
283 switch (base_type) {
284 case GLSL_TYPE_UINT:
285 return uint_type + (rows - 1);
286 case GLSL_TYPE_INT:
287 return int_type + (rows - 1);
288 case GLSL_TYPE_FLOAT:
289 return float_type + (rows - 1);
290 case GLSL_TYPE_BOOL:
291 return bool_type + (rows - 1);
292 default:
293 return error_type;
294 }
295 } else {
296 if ((base_type != GLSL_TYPE_FLOAT) || (rows == 1))
297 return error_type;
298
299 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
300 * combinations are valid:
301 *
302 * 1 2 3 4
303 * 1
304 * 2 x x x
305 * 3 x x x
306 * 4 x x x
307 */
308 #define IDX(c,r) (((c-1)*3) + (r-1))
309
310 switch (IDX(columns, rows)) {
311 case IDX(2,2): return mat2_type;
312 case IDX(2,3): return mat2x3_type;
313 case IDX(2,4): return mat2x4_type;
314 case IDX(3,2): return mat3x2_type;
315 case IDX(3,3): return mat3_type;
316 case IDX(3,4): return mat3x4_type;
317 case IDX(4,2): return mat4x2_type;
318 case IDX(4,3): return mat4x3_type;
319 case IDX(4,4): return mat4_type;
320 default: return error_type;
321 }
322 }
323
324 assert(!"Should not get here.");
325 return error_type;
326 }
327
328
329 int
330 glsl_type::array_key_compare(const void *a, const void *b)
331 {
332 const glsl_type *const key1 = (glsl_type *) a;
333 const glsl_type *const key2 = (glsl_type *) b;
334
335 /* Return zero is the types match (there is zero difference) or non-zero
336 * otherwise.
337 */
338 return ((key1->fields.array == key2->fields.array)
339 && (key1->length == key2->length)) ? 0 : 1;
340 }
341
342
343 unsigned
344 glsl_type::array_key_hash(const void *a)
345 {
346 const glsl_type *const key = (glsl_type *) a;
347
348 const struct {
349 const glsl_type *t;
350 unsigned l;
351 char nul;
352 } hash_key = {
353 key->fields.array,
354 key->length,
355 '\0'
356 };
357
358 return hash_table_string_hash(& hash_key);
359 }
360
361
362 const glsl_type *
363 glsl_type::get_array_instance(void *ctx, const glsl_type *base,
364 unsigned array_size)
365 {
366 const glsl_type key(ctx, base, array_size);
367
368 if (array_types == NULL) {
369 array_types = hash_table_ctor(64, array_key_hash, array_key_compare);
370 }
371
372 const glsl_type *t = (glsl_type *) hash_table_find(array_types, & key);
373 if (t == NULL) {
374 t = new(ctx) glsl_type(ctx, base, array_size);
375
376 hash_table_insert(array_types, (void *) t, t);
377 }
378
379 assert(t->base_type == GLSL_TYPE_ARRAY);
380 assert(t->length == array_size);
381 assert(t->fields.array == base);
382
383 return t;
384 }
385
386
387 const glsl_type *
388 glsl_type::field_type(const char *name) const
389 {
390 if (this->base_type != GLSL_TYPE_STRUCT)
391 return error_type;
392
393 for (unsigned i = 0; i < this->length; i++) {
394 if (strcmp(name, this->fields.structure[i].name) == 0)
395 return this->fields.structure[i].type;
396 }
397
398 return error_type;
399 }
400
401
402 int
403 glsl_type::field_index(const char *name) const
404 {
405 if (this->base_type != GLSL_TYPE_STRUCT)
406 return -1;
407
408 for (unsigned i = 0; i < this->length; i++) {
409 if (strcmp(name, this->fields.structure[i].name) == 0)
410 return i;
411 }
412
413 return -1;
414 }
415
416
417 unsigned
418 glsl_type::component_slots() const
419 {
420 switch (this->base_type) {
421 case GLSL_TYPE_UINT:
422 case GLSL_TYPE_INT:
423 case GLSL_TYPE_FLOAT:
424 case GLSL_TYPE_BOOL:
425 return this->components();
426
427 case GLSL_TYPE_STRUCT: {
428 unsigned size = 0;
429
430 for (unsigned i = 0; i < this->length; i++)
431 size += this->fields.structure[i].type->component_slots();
432
433 return size;
434 }
435
436 case GLSL_TYPE_ARRAY:
437 return this->length * this->fields.array->component_slots();
438
439 default:
440 return 0;
441 }
442 }