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.
26 #include "glsl_symbol_table.h"
27 #include "glsl_parser_extras.h"
28 #include "glsl_types.h"
29 #include "builtin_types.h"
31 #include "hash_table.h"
34 hash_table
*glsl_type::array_types
= NULL
;
36 glsl_type::glsl_type(GLenum gl_type
,
37 unsigned base_type
, unsigned vector_elements
,
38 unsigned matrix_columns
, const char *name
) :
41 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
43 vector_elements(vector_elements
), matrix_columns(matrix_columns
),
47 /* Neither dimension is zero or both dimensions are zero.
49 assert((vector_elements
== 0) == (matrix_columns
== 0));
50 memset(& fields
, 0, sizeof(fields
));
53 glsl_type::glsl_type(GLenum gl_type
,
54 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
55 unsigned type
, const char *name
) :
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),
64 memset(& fields
, 0, sizeof(fields
));
68 add_types_to_symbol_table(glsl_symbol_table
*symtab
,
69 const struct glsl_type
*types
,
70 unsigned num_types
, bool warn
)
74 for (unsigned i
= 0; i
< num_types
; i
++) {
75 symtab
->add_type(types
[i
].name
, & types
[i
]);
81 glsl_type::generate_110_types(glsl_symbol_table
*symtab
)
83 add_types_to_symbol_table(symtab
, builtin_core_types
,
84 Elements(builtin_core_types
),
86 add_types_to_symbol_table(symtab
, builtin_structure_types
,
87 Elements(builtin_structure_types
),
89 add_types_to_symbol_table(symtab
, builtin_110_deprecated_structure_types
,
90 Elements(builtin_110_deprecated_structure_types
),
92 add_types_to_symbol_table(symtab
, & void_type
, 1, false);
97 glsl_type::generate_120_types(glsl_symbol_table
*symtab
)
99 generate_110_types(symtab
);
101 add_types_to_symbol_table(symtab
, builtin_120_types
,
102 Elements(builtin_120_types
), false);
107 glsl_type::generate_130_types(glsl_symbol_table
*symtab
)
109 generate_120_types(symtab
);
111 add_types_to_symbol_table(symtab
, builtin_130_types
,
112 Elements(builtin_130_types
), false);
117 glsl_type::generate_ARB_texture_rectangle_types(glsl_symbol_table
*symtab
,
120 add_types_to_symbol_table(symtab
, builtin_ARB_texture_rectangle_types
,
121 Elements(builtin_ARB_texture_rectangle_types
),
127 glsl_type::generate_EXT_texture_array_types(glsl_symbol_table
*symtab
,
130 add_types_to_symbol_table(symtab
, builtin_EXT_texture_array_types
,
131 Elements(builtin_EXT_texture_array_types
),
137 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*state
)
139 switch (state
->language_version
) {
141 glsl_type::generate_110_types(state
->symbols
);
144 glsl_type::generate_120_types(state
->symbols
);
147 glsl_type::generate_130_types(state
->symbols
);
154 if (state
->ARB_texture_rectangle_enable
) {
155 glsl_type::generate_ARB_texture_rectangle_types(state
->symbols
,
156 state
->ARB_texture_rectangle_warn
);
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
);
167 const glsl_type
*glsl_type::get_base_type() const
174 case GLSL_TYPE_FLOAT
:
185 glsl_type::generate_constructor(glsl_symbol_table
*symtab
) const
189 /* Generate the function name and add it to the symbol table.
191 ir_function
*const f
= new(ctx
) ir_function(name
);
193 bool added
= symtab
->add_function(name
, f
);
196 ir_function_signature
*const sig
= new(ctx
) ir_function_signature(this);
197 f
->add_signature(sig
);
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);
204 snprintf(param_name
, 10, "p%08X", i
);
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
);
210 var
->mode
= ir_var_in
;
211 declarations
[i
] = var
;
212 sig
->parameters
.push_tail(var
);
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.
220 ir_variable
*retval
= new(ctx
) ir_variable(this, "__retval");
221 sig
->body
.push_tail(retval
);
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
);
230 ir_dereference
*const rhs
= new(ctx
) ir_dereference_variable(declarations
[i
]);
231 ir_instruction
*const assign
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
233 sig
->body
.push_tail(assign
);
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
);
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),
250 vector_elements(0), matrix_columns(0),
251 name(NULL
), length(length
)
253 this->fields
.array
= array
;
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
259 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
260 char *const n
= (char *) talloc_size(ctx
, name_length
);
263 snprintf(n
, name_length
, "%s[]", array
->name
);
265 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
272 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
274 if (base_type
== GLSL_TYPE_VOID
)
277 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
280 /* Treat GLSL vectors as Nx1 matrices.
285 return uint_type
+ (rows
- 1);
287 return int_type
+ (rows
- 1);
288 case GLSL_TYPE_FLOAT
:
289 return float_type
+ (rows
- 1);
291 return bool_type
+ (rows
- 1);
296 if ((base_type
!= GLSL_TYPE_FLOAT
) || (rows
== 1))
299 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
300 * combinations are valid:
308 #define IDX(c,r) (((c-1)*3) + (r-1))
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
;
324 assert(!"Should not get here.");
330 glsl_type::array_key_compare(const void *a
, const void *b
)
332 const glsl_type
*const key1
= (glsl_type
*) a
;
333 const glsl_type
*const key2
= (glsl_type
*) b
;
335 /* Return zero is the types match (there is zero difference) or non-zero
338 return ((key1
->fields
.array
== key2
->fields
.array
)
339 && (key1
->length
== key2
->length
)) ? 0 : 1;
344 glsl_type::array_key_hash(const void *a
)
346 const glsl_type
*const key
= (glsl_type
*) a
;
358 return hash_table_string_hash(& hash_key
);
363 glsl_type::get_array_instance(void *ctx
, const glsl_type
*base
,
366 const glsl_type
key(ctx
, base
, array_size
);
368 if (array_types
== NULL
) {
369 array_types
= hash_table_ctor(64, array_key_hash
, array_key_compare
);
372 const glsl_type
*t
= (glsl_type
*) hash_table_find(array_types
, & key
);
374 t
= new(ctx
) glsl_type(ctx
, base
, array_size
);
376 hash_table_insert(array_types
, (void *) t
, t
);
379 assert(t
->base_type
== GLSL_TYPE_ARRAY
);
380 assert(t
->length
== array_size
);
381 assert(t
->fields
.array
== base
);
388 glsl_type::field_type(const char *name
) const
390 if (this->base_type
!= GLSL_TYPE_STRUCT
)
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
;
403 glsl_type::field_index(const char *name
) const
405 if (this->base_type
!= GLSL_TYPE_STRUCT
)
408 for (unsigned i
= 0; i
< this->length
; i
++) {
409 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
418 glsl_type::component_slots() const
420 switch (this->base_type
) {
423 case GLSL_TYPE_FLOAT
:
425 return this->components();
427 case GLSL_TYPE_STRUCT
: {
430 for (unsigned i
= 0; i
< this->length
; i
++)
431 size
+= this->fields
.structure
[i
].type
->component_slots();
436 case GLSL_TYPE_ARRAY
:
437 return this->length
* this->fields
.array
->component_slots();