2 * Mesa 3-D graphics library
5 * Copyright (C) 2005-2006 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * \file slang_compile_variable.c
27 * slang front-end compiler
32 #include "slang_compile.h"
34 /* slang_type_specifier_type */
39 slang_type_specifier_type type
;
40 } type_specifier_type_name
;
42 static type_specifier_type_name type_specifier_type_names
[] = {
43 { "void", slang_spec_void
},
44 { "bool", slang_spec_bool
},
45 { "bvec2", slang_spec_bvec2
},
46 { "bvec3", slang_spec_bvec3
},
47 { "bvec4", slang_spec_bvec4
},
48 { "int", slang_spec_int
},
49 { "ivec2", slang_spec_ivec2
},
50 { "ivec3", slang_spec_ivec3
},
51 { "ivec4", slang_spec_ivec4
},
52 { "float", slang_spec_float
},
53 { "vec2", slang_spec_vec2
},
54 { "vec3", slang_spec_vec3
},
55 { "vec4", slang_spec_vec4
},
56 { "mat2", slang_spec_mat2
},
57 { "mat3", slang_spec_mat3
},
58 { "mat4", slang_spec_mat4
},
59 { "sampler1D", slang_spec_sampler1D
},
60 { "sampler2D", slang_spec_sampler2D
},
61 { "sampler3D", slang_spec_sampler3D
},
62 { "samplerCube", slang_spec_samplerCube
},
63 { "sampler1DShadow", slang_spec_sampler1DShadow
},
64 { "sampler2DShadow", slang_spec_sampler2DShadow
},
65 { NULL
, slang_spec_void
}
68 slang_type_specifier_type
slang_type_specifier_type_from_string (const char *name
)
70 type_specifier_type_name
*p
= type_specifier_type_names
;
71 while (p
->name
!= NULL
)
73 if (slang_string_compare (p
->name
, name
) == 0)
80 const char *slang_type_specifier_type_to_string (slang_type_specifier_type type
)
82 type_specifier_type_name
*p
= type_specifier_type_names
;
83 while (p
->name
!= NULL
)
92 /* slang_fully_specified_type */
94 int slang_fully_specified_type_construct (slang_fully_specified_type
*type
)
96 type
->qualifier
= slang_qual_none
;
97 slang_type_specifier_ctr (&type
->specifier
);
101 void slang_fully_specified_type_destruct (slang_fully_specified_type
*type
)
103 slang_type_specifier_dtr (&type
->specifier
);
106 int slang_fully_specified_type_copy (slang_fully_specified_type
*x
, const slang_fully_specified_type
*y
)
108 slang_fully_specified_type z
;
110 if (!slang_fully_specified_type_construct (&z
))
112 z
.qualifier
= y
->qualifier
;
113 if (!slang_type_specifier_copy (&z
.specifier
, &y
->specifier
))
115 slang_fully_specified_type_destruct (&z
);
118 slang_fully_specified_type_destruct (x
);
123 /* slang_variable_scope */
125 int slang_variable_scope_construct (slang_variable_scope
*scope
)
127 scope
->variables
= NULL
;
128 scope
->num_variables
= 0;
129 scope
->outer_scope
= NULL
;
133 void slang_variable_scope_destruct (slang_variable_scope
*scope
)
137 for (i
= 0; i
< scope
->num_variables
; i
++)
138 slang_variable_destruct (scope
->variables
+ i
);
139 slang_alloc_free (scope
->variables
);
140 /* do not free scope->outer_scope */
143 int slang_variable_scope_copy (slang_variable_scope
*x
, const slang_variable_scope
*y
)
145 slang_variable_scope z
;
148 if (!slang_variable_scope_construct (&z
))
150 z
.variables
= (slang_variable
*) slang_alloc_malloc (y
->num_variables
* sizeof (slang_variable
));
151 if (z
.variables
== NULL
)
153 slang_variable_scope_destruct (&z
);
156 for (z
.num_variables
= 0; z
.num_variables
< y
->num_variables
; z
.num_variables
++)
157 if (!slang_variable_construct (&z
.variables
[z
.num_variables
]))
159 slang_variable_scope_destruct (&z
);
162 for (i
= 0; i
< z
.num_variables
; i
++)
163 if (!slang_variable_copy (&z
.variables
[i
], &y
->variables
[i
]))
165 slang_variable_scope_destruct (&z
);
168 z
.outer_scope
= y
->outer_scope
;
169 slang_variable_scope_destruct (x
);
176 int slang_variable_construct (slang_variable
*var
)
178 if (!slang_fully_specified_type_construct (&var
->type
))
180 var
->a_name
= SLANG_ATOM_NULL
;
182 var
->initializer
= NULL
;
189 void slang_variable_destruct (slang_variable
*var
)
191 slang_fully_specified_type_destruct (&var
->type
);
192 if (var
->initializer
!= NULL
)
194 slang_operation_destruct (var
->initializer
);
195 slang_alloc_free (var
->initializer
);
199 int slang_variable_copy (slang_variable
*x
, const slang_variable
*y
)
203 if (!slang_variable_construct (&z
))
205 if (!slang_fully_specified_type_copy (&z
.type
, &y
->type
))
207 slang_variable_destruct (&z
);
210 z
.a_name
= y
->a_name
;
211 z
.array_len
= y
->array_len
;
212 if (y
->initializer
!= NULL
)
214 z
.initializer
= (slang_operation
*) slang_alloc_malloc (sizeof (slang_operation
));
215 if (z
.initializer
== NULL
)
217 slang_variable_destruct (&z
);
220 if (!slang_operation_construct (z
.initializer
))
222 slang_alloc_free (z
.initializer
);
223 slang_variable_destruct (&z
);
226 if (!slang_operation_copy (z
.initializer
, y
->initializer
))
228 slang_variable_destruct (&z
);
232 z
.address
= y
->address
;
234 z
.global
= y
->global
;
235 slang_variable_destruct (x
);
240 slang_variable
*_slang_locate_variable (slang_variable_scope
*scope
, slang_atom a_name
, GLboolean all
)
244 for (i
= 0; i
< scope
->num_variables
; i
++)
245 if (a_name
== scope
->variables
[i
].a_name
)
246 return &scope
->variables
[i
];
247 if (all
&& scope
->outer_scope
!= NULL
)
248 return _slang_locate_variable (scope
->outer_scope
, a_name
, 1);
253 * _slang_build_export_data_table()
256 static GLenum
gl_type_from_specifier (const slang_type_specifier
*type
)
260 case slang_spec_bool
:
262 case slang_spec_bvec2
:
263 return GL_BOOL_VEC2_ARB
;
264 case slang_spec_bvec3
:
265 return GL_BOOL_VEC3_ARB
;
266 case slang_spec_bvec4
:
267 return GL_BOOL_VEC4_ARB
;
270 case slang_spec_ivec2
:
271 return GL_INT_VEC2_ARB
;
272 case slang_spec_ivec3
:
273 return GL_INT_VEC3_ARB
;
274 case slang_spec_ivec4
:
275 return GL_INT_VEC4_ARB
;
276 case slang_spec_float
:
278 case slang_spec_vec2
:
279 return GL_FLOAT_VEC2_ARB
;
280 case slang_spec_vec3
:
281 return GL_FLOAT_VEC3_ARB
;
282 case slang_spec_vec4
:
283 return GL_FLOAT_VEC4_ARB
;
284 case slang_spec_mat2
:
285 return GL_FLOAT_MAT2_ARB
;
286 case slang_spec_mat3
:
287 return GL_FLOAT_MAT3_ARB
;
288 case slang_spec_mat4
:
289 return GL_FLOAT_MAT4_ARB
;
290 case slang_spec_sampler1D
:
291 return GL_SAMPLER_1D_ARB
;
292 case slang_spec_sampler2D
:
293 return GL_SAMPLER_2D_ARB
;
294 case slang_spec_sampler3D
:
295 return GL_SAMPLER_3D_ARB
;
296 case slang_spec_samplerCube
:
297 return GL_SAMPLER_CUBE_ARB
;
298 case slang_spec_sampler1DShadow
:
299 return GL_SAMPLER_1D_SHADOW_ARB
;
300 case slang_spec_sampler2DShadow
:
301 return GL_SAMPLER_2D_SHADOW_ARB
;
302 case slang_spec_array
:
303 return gl_type_from_specifier (type
->_array
);
309 static GLboolean
build_quant (slang_export_data_quant
*q
, slang_variable
*var
)
311 slang_type_specifier
*spec
= &var
->type
.specifier
;
313 q
->name
= var
->a_name
;
315 if (spec
->type
== slang_spec_array
)
317 q
->array_len
= var
->array_len
;
318 q
->size
/= var
->array_len
;
321 if (spec
->type
== slang_spec_struct
)
325 q
->u
.field_count
= spec
->_struct
->fields
->num_variables
;
326 q
->structure
= (slang_export_data_quant
*) slang_alloc_malloc (
327 q
->u
.field_count
* sizeof (slang_export_data_quant
));
328 if (q
->structure
== NULL
)
331 for (i
= 0; i
< q
->u
.field_count
; i
++)
332 slang_export_data_quant_ctr (&q
->structure
[i
]);
333 for (i
= 0; i
< q
->u
.field_count
; i
++)
334 if (!build_quant (&q
->structure
[i
], &spec
->_struct
->fields
->variables
[i
]))
338 q
->u
.basic_type
= gl_type_from_specifier (spec
);
342 GLboolean
_slang_build_export_data_table (slang_export_data_table
*tbl
, slang_variable_scope
*vars
)
346 for (i
= 0; i
< vars
->num_variables
; i
++)
348 slang_variable
*var
= &vars
->variables
[i
];
349 slang_export_data_entry
*e
;
351 e
= slang_export_data_table_add (tbl
);
354 if (!build_quant (&e
->quant
, var
))
356 if (var
->type
.qualifier
== slang_qual_uniform
)
357 e
->access
= slang_exp_uniform
;
358 else if (var
->type
.qualifier
== slang_qual_attribute
)
359 e
->access
= slang_exp_attribute
;
361 e
->access
= slang_exp_varying
;
362 e
->address
= var
->address
;
365 if (vars
->outer_scope
!= NULL
)
366 return _slang_build_export_data_table (tbl
, vars
->outer_scope
);