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_assemble_typeinfo.c
31 #include "main/imports.h"
32 #include "shader/prog_instruction.h"
33 #include "slang_typeinfo.h"
34 #include "slang_compile.h"
35 #include "slang_log.h"
36 #include "slang_mem.h"
40 * Checks if a field selector is a general swizzle (an r-value swizzle
41 * with replicated components or an l-value swizzle mask) for a
42 * vector. Returns GL_TRUE if this is the case, <swz> is filled with
43 * swizzle information. Returns GL_FALSE otherwise.
46 _slang_is_swizzle(const char *field
, GLuint rows
, slang_swizzle
* swz
)
49 GLboolean xyzw
= GL_FALSE
, rgba
= GL_FALSE
, stpq
= GL_FALSE
;
52 * We rely on undefined/nil values to distinguish between
53 * regular swizzles and writemasks.
54 * For example, the swizzle ".xNNN" is the writemask ".x".
55 * That's different than the swizzle ".xxxx".
57 for (i
= 0; i
< 4; i
++)
58 swz
->swizzle
[i
] = SWIZZLE_NIL
;
60 /* the swizzle can be at most 4-component long */
61 swz
->num_components
= slang_string_length(field
);
62 if (swz
->num_components
> 4)
65 for (i
= 0; i
< swz
->num_components
; i
++) {
66 /* mark which swizzle group is used */
90 /* collect swizzle component */
114 /* check if the component is valid for given vector's row count */
115 if (rows
<= swz
->swizzle
[i
])
119 /* only one swizzle group can be used */
120 if ((xyzw
&& rgba
) || (xyzw
&& stpq
) || (rgba
&& stpq
))
129 * Checks if a general swizzle is an l-value swizzle - these swizzles
130 * do not have duplicated fields. Returns GL_TRUE if this is a
131 * swizzle mask. Returns GL_FALSE otherwise
134 _slang_is_swizzle_mask(const slang_swizzle
* swz
, GLuint rows
)
138 /* the swizzle may not be longer than the vector dim */
139 if (swz
->num_components
> rows
)
142 /* the swizzle components cannot be duplicated */
143 for (i
= 0; i
< swz
->num_components
; i
++) {
144 if ((c
& (1 << swz
->swizzle
[i
])) != 0)
146 c
|= 1 << swz
->swizzle
[i
];
154 * Combines (multiplies) two swizzles to form single swizzle.
155 * Example: "vec.wzyx.yx" --> "vec.zw".
158 _slang_multiply_swizzles(slang_swizzle
* dst
, const slang_swizzle
* left
,
159 const slang_swizzle
* right
)
163 dst
->num_components
= right
->num_components
;
164 for (i
= 0; i
< right
->num_components
; i
++)
165 dst
->swizzle
[i
] = left
->swizzle
[right
->swizzle
[i
]];
172 slang_type_specifier_type type
;
173 } type_specifier_type_name
;
175 static const type_specifier_type_name type_specifier_type_names
[] = {
176 {"void", SLANG_SPEC_VOID
},
177 {"bool", SLANG_SPEC_BOOL
},
178 {"bvec2", SLANG_SPEC_BVEC2
},
179 {"bvec3", SLANG_SPEC_BVEC3
},
180 {"bvec4", SLANG_SPEC_BVEC4
},
181 {"int", SLANG_SPEC_INT
},
182 {"ivec2", SLANG_SPEC_IVEC2
},
183 {"ivec3", SLANG_SPEC_IVEC3
},
184 {"ivec4", SLANG_SPEC_IVEC4
},
185 {"float", SLANG_SPEC_FLOAT
},
186 {"vec2", SLANG_SPEC_VEC2
},
187 {"vec3", SLANG_SPEC_VEC3
},
188 {"vec4", SLANG_SPEC_VEC4
},
189 {"mat2", SLANG_SPEC_MAT2
},
190 {"mat3", SLANG_SPEC_MAT3
},
191 {"mat4", SLANG_SPEC_MAT4
},
192 {"mat2x3", SLANG_SPEC_MAT23
},
193 {"mat3x2", SLANG_SPEC_MAT32
},
194 {"mat2x4", SLANG_SPEC_MAT24
},
195 {"mat4x2", SLANG_SPEC_MAT42
},
196 {"mat3x4", SLANG_SPEC_MAT34
},
197 {"mat4x3", SLANG_SPEC_MAT43
},
198 {"sampler1D", SLANG_SPEC_SAMPLER1D
},
199 {"sampler2D", SLANG_SPEC_SAMPLER2D
},
200 {"sampler3D", SLANG_SPEC_SAMPLER3D
},
201 {"samplerCube", SLANG_SPEC_SAMPLERCUBE
},
202 {"sampler1DShadow", SLANG_SPEC_SAMPLER1DSHADOW
},
203 {"sampler2DShadow", SLANG_SPEC_SAMPLER2DSHADOW
},
204 {"sampler2DRect", SLANG_SPEC_SAMPLER2DRECT
},
205 {"sampler2DRectShadow", SLANG_SPEC_SAMPLER2DRECTSHADOW
},
206 {NULL
, SLANG_SPEC_VOID
}
209 slang_type_specifier_type
210 slang_type_specifier_type_from_string(const char *name
)
212 const type_specifier_type_name
*p
= type_specifier_type_names
;
213 while (p
->name
!= NULL
) {
214 if (slang_string_compare(p
->name
, name
) == 0)
222 slang_type_specifier_type_to_string(slang_type_specifier_type type
)
224 const type_specifier_type_name
*p
= type_specifier_type_names
;
225 while (p
->name
!= NULL
) {
233 /* slang_fully_specified_type */
236 slang_fully_specified_type_construct(slang_fully_specified_type
* type
)
238 type
->qualifier
= SLANG_QUAL_NONE
;
239 slang_type_specifier_ctr(&type
->specifier
);
244 slang_fully_specified_type_destruct(slang_fully_specified_type
* type
)
246 slang_type_specifier_dtr(&type
->specifier
);
250 slang_fully_specified_type_copy(slang_fully_specified_type
* x
,
251 const slang_fully_specified_type
* y
)
253 slang_fully_specified_type z
;
255 if (!slang_fully_specified_type_construct(&z
))
257 z
.qualifier
= y
->qualifier
;
258 z
.precision
= y
->precision
;
259 z
.variant
= y
->variant
;
260 z
.centroid
= y
->centroid
;
261 if (!slang_type_specifier_copy(&z
.specifier
, &y
->specifier
)) {
262 slang_fully_specified_type_destruct(&z
);
265 slang_fully_specified_type_destruct(x
);
273 slang_type_specifier_ctr(slang_type_specifier
* self
)
275 self
->type
= SLANG_SPEC_VOID
;
276 self
->_struct
= NULL
;
281 slang_type_specifier_dtr(slang_type_specifier
* self
)
283 if (self
->_struct
!= NULL
) {
284 slang_struct_destruct(self
->_struct
);
285 _slang_free(self
->_struct
);
287 if (self
->_array
!= NULL
) {
288 slang_type_specifier_dtr(self
->_array
);
289 _slang_free(self
->_array
);
293 slang_type_specifier
*
294 slang_type_specifier_new(slang_type_specifier_type type
,
295 struct slang_struct_
*_struct
,
296 struct slang_type_specifier_
*_array
)
298 slang_type_specifier
*spec
=
299 (slang_type_specifier
*) _mesa_malloc(sizeof(slang_type_specifier
));
302 spec
->_struct
= _struct
;
303 spec
->_array
= _array
;
309 slang_type_specifier_copy(slang_type_specifier
* x
,
310 const slang_type_specifier
* y
)
312 slang_type_specifier z
;
314 slang_type_specifier_ctr(&z
);
316 if (z
.type
== SLANG_SPEC_STRUCT
) {
317 z
._struct
= (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
318 if (z
._struct
== NULL
) {
319 slang_type_specifier_dtr(&z
);
322 if (!slang_struct_construct(z
._struct
)) {
323 _slang_free(z
._struct
);
324 slang_type_specifier_dtr(&z
);
327 if (!slang_struct_copy(z
._struct
, y
->_struct
)) {
328 slang_type_specifier_dtr(&z
);
332 else if (z
.type
== SLANG_SPEC_ARRAY
) {
333 z
._array
= (slang_type_specifier
*)
334 _slang_alloc(sizeof(slang_type_specifier
));
335 if (z
._array
== NULL
) {
336 slang_type_specifier_dtr(&z
);
339 slang_type_specifier_ctr(z
._array
);
340 if (!slang_type_specifier_copy(z
._array
, y
->_array
)) {
341 slang_type_specifier_dtr(&z
);
345 slang_type_specifier_dtr(x
);
352 * Test if two types are equal.
355 slang_type_specifier_equal(const slang_type_specifier
* x
,
356 const slang_type_specifier
* y
)
358 if (x
->type
!= y
->type
)
360 if (x
->type
== SLANG_SPEC_STRUCT
)
361 return slang_struct_equal(x
->_struct
, y
->_struct
);
362 if (x
->type
== SLANG_SPEC_ARRAY
)
363 return slang_type_specifier_equal(x
->_array
, y
->_array
);
369 * As above, but allow float/int casting.
372 slang_type_specifier_compatible(const slang_type_specifier
* x
,
373 const slang_type_specifier
* y
)
375 /* special case: float == int */
376 if (x
->type
== SLANG_SPEC_INT
&& y
->type
== SLANG_SPEC_FLOAT
) {
379 /* XXX may need to add bool/int compatibility, etc */
381 if (x
->type
!= y
->type
)
383 if (x
->type
== SLANG_SPEC_STRUCT
)
384 return slang_struct_equal(x
->_struct
, y
->_struct
);
385 if (x
->type
== SLANG_SPEC_ARRAY
)
386 return slang_type_specifier_compatible(x
->_array
, y
->_array
);
392 slang_typeinfo_construct(slang_typeinfo
* ti
)
394 _mesa_bzero(ti
, sizeof(*ti
));
395 slang_type_specifier_ctr(&ti
->spec
);
401 slang_typeinfo_destruct(slang_typeinfo
* ti
)
403 slang_type_specifier_dtr(&ti
->spec
);
409 * Determine the return type of a function.
410 * \param a_name the function name
411 * \param param function parameters (overloading)
412 * \param num_params number of parameters to function
413 * \param space namespace to search
414 * \param spec returns the type
415 * \param funFound returns pointer to the function, or NULL if not found.
416 * \return GL_TRUE for success, GL_FALSE if failure (bad function name)
419 _slang_typeof_function(slang_atom a_name
,
420 slang_operation
* params
, GLuint num_params
,
421 const slang_name_space
* space
,
422 slang_type_specifier
* spec
,
423 slang_function
**funFound
,
424 slang_atom_pool
*atoms
, slang_info_log
*log
)
428 *funFound
= _slang_locate_function(space
->funcs
, a_name
, params
,
429 num_params
, space
, atoms
, log
, &error
);
434 return GL_TRUE
; /* yes, not false */
436 return slang_type_specifier_copy(spec
, &(*funFound
)->header
.type
.specifier
);
441 * Determine the type of a math function.
442 * \param name name of the operator, one of +,-,*,/ or unary -
443 * \param params array of function parameters
444 * \param num_params number of parameters
445 * \param space namespace to use
446 * \param spec returns the function's type
447 * \param atoms atom pool
448 * \return GL_TRUE for success, GL_FALSE if failure
451 typeof_math_call(const char *name
, slang_operation
*call
,
452 const slang_name_space
* space
,
453 slang_type_specifier
* spec
,
454 slang_atom_pool
* atoms
,
458 /* we've previously resolved this function call */
459 slang_type_specifier_copy(spec
, &call
->fun
->header
.type
.specifier
);
466 /* number of params: */
467 assert(call
->num_children
== 1 || call
->num_children
== 2);
469 atom
= slang_atom_pool_atom(atoms
, name
);
470 if (!_slang_typeof_function(atom
, call
->children
, call
->num_children
,
471 space
, spec
, &fun
, atoms
, log
))
475 /* Save pointer to save time in future */
485 * Determine the return type of an operation.
486 * \param op the operation node
487 * \param space the namespace to use
488 * \param ti the returned type
489 * \param atoms atom pool
490 * \return GL_TRUE for success, GL_FALSE if failure
493 _slang_typeof_operation_(slang_operation
* op
,
494 const slang_name_space
* space
,
496 slang_atom_pool
* atoms
,
499 ti
->can_be_referenced
= GL_FALSE
;
500 ti
->is_swizzled
= GL_FALSE
;
503 case SLANG_OPER_BLOCK_NO_NEW_SCOPE
:
504 case SLANG_OPER_BLOCK_NEW_SCOPE
:
506 case SLANG_OPER_BREAK
:
507 case SLANG_OPER_CONTINUE
:
508 case SLANG_OPER_DISCARD
:
509 case SLANG_OPER_RETURN
:
511 case SLANG_OPER_WHILE
:
514 case SLANG_OPER_VOID
:
515 ti
->spec
.type
= SLANG_SPEC_VOID
;
517 case SLANG_OPER_EXPRESSION
:
518 case SLANG_OPER_ASSIGN
:
519 case SLANG_OPER_ADDASSIGN
:
520 case SLANG_OPER_SUBASSIGN
:
521 case SLANG_OPER_MULASSIGN
:
522 case SLANG_OPER_DIVASSIGN
:
523 case SLANG_OPER_PREINCREMENT
:
524 case SLANG_OPER_PREDECREMENT
:
525 if (!_slang_typeof_operation_(op
->children
, space
, ti
, atoms
, log
))
528 case SLANG_OPER_LITERAL_BOOL
:
529 if (op
->literal_size
== 1)
530 ti
->spec
.type
= SLANG_SPEC_BOOL
;
531 else if (op
->literal_size
== 2)
532 ti
->spec
.type
= SLANG_SPEC_BVEC2
;
533 else if (op
->literal_size
== 3)
534 ti
->spec
.type
= SLANG_SPEC_BVEC3
;
535 else if (op
->literal_size
== 4)
536 ti
->spec
.type
= SLANG_SPEC_BVEC4
;
539 "Unexpected bool literal_size %d in _slang_typeof_operation()",
541 ti
->spec
.type
= SLANG_SPEC_BOOL
;
544 case SLANG_OPER_LOGICALOR
:
545 case SLANG_OPER_LOGICALXOR
:
546 case SLANG_OPER_LOGICALAND
:
547 case SLANG_OPER_EQUAL
:
548 case SLANG_OPER_NOTEQUAL
:
549 case SLANG_OPER_LESS
:
550 case SLANG_OPER_GREATER
:
551 case SLANG_OPER_LESSEQUAL
:
552 case SLANG_OPER_GREATEREQUAL
:
554 ti
->spec
.type
= SLANG_SPEC_BOOL
;
556 case SLANG_OPER_LITERAL_INT
:
557 if (op
->literal_size
== 1)
558 ti
->spec
.type
= SLANG_SPEC_INT
;
559 else if (op
->literal_size
== 2)
560 ti
->spec
.type
= SLANG_SPEC_IVEC2
;
561 else if (op
->literal_size
== 3)
562 ti
->spec
.type
= SLANG_SPEC_IVEC3
;
563 else if (op
->literal_size
== 4)
564 ti
->spec
.type
= SLANG_SPEC_IVEC4
;
567 "Unexpected int literal_size %d in _slang_typeof_operation()",
569 ti
->spec
.type
= SLANG_SPEC_INT
;
572 case SLANG_OPER_LITERAL_FLOAT
:
573 if (op
->literal_size
== 1)
574 ti
->spec
.type
= SLANG_SPEC_FLOAT
;
575 else if (op
->literal_size
== 2)
576 ti
->spec
.type
= SLANG_SPEC_VEC2
;
577 else if (op
->literal_size
== 3)
578 ti
->spec
.type
= SLANG_SPEC_VEC3
;
579 else if (op
->literal_size
== 4)
580 ti
->spec
.type
= SLANG_SPEC_VEC4
;
583 "Unexpected float literal_size %d in _slang_typeof_operation()",
585 ti
->spec
.type
= SLANG_SPEC_FLOAT
;
588 case SLANG_OPER_IDENTIFIER
:
589 case SLANG_OPER_VARIABLE_DECL
:
592 var
= _slang_locate_variable(op
->locals
, op
->a_id
, GL_TRUE
);
594 slang_info_log_error(log
, "undefined variable '%s'",
598 if (!slang_type_specifier_copy(&ti
->spec
, &var
->type
.specifier
)) {
599 slang_info_log_memory(log
);
602 ti
->can_be_referenced
= GL_TRUE
;
603 ti
->array_len
= var
->array_len
;
606 case SLANG_OPER_SEQUENCE
:
607 /* TODO: check [0] and [1] if they match */
608 if (!_slang_typeof_operation_(&op
->children
[1], space
, ti
, atoms
, log
)) {
611 ti
->can_be_referenced
= GL_FALSE
;
612 ti
->is_swizzled
= GL_FALSE
;
614 /*case SLANG_OPER_MODASSIGN: */
615 /*case SLANG_OPER_LSHASSIGN: */
616 /*case SLANG_OPER_RSHASSIGN: */
617 /*case SLANG_OPER_ORASSIGN: */
618 /*case SLANG_OPER_XORASSIGN: */
619 /*case SLANG_OPER_ANDASSIGN: */
620 case SLANG_OPER_SELECT
:
621 /* TODO: check [1] and [2] if they match */
622 if (!_slang_typeof_operation_(&op
->children
[1], space
, ti
, atoms
, log
)) {
625 ti
->can_be_referenced
= GL_FALSE
;
626 ti
->is_swizzled
= GL_FALSE
;
628 /*case SLANG_OPER_BITOR: */
629 /*case SLANG_OPER_BITXOR: */
630 /*case SLANG_OPER_BITAND: */
631 /*case SLANG_OPER_LSHIFT: */
632 /*case SLANG_OPER_RSHIFT: */
634 assert(op
->num_children
== 2);
635 if (!typeof_math_call("+", op
, space
, &ti
->spec
, atoms
, log
))
638 case SLANG_OPER_SUBTRACT
:
639 assert(op
->num_children
== 2);
640 if (!typeof_math_call("-", op
, space
, &ti
->spec
, atoms
, log
))
643 case SLANG_OPER_MULTIPLY
:
644 assert(op
->num_children
== 2);
645 if (!typeof_math_call("*", op
, space
, &ti
->spec
, atoms
, log
))
648 case SLANG_OPER_DIVIDE
:
649 assert(op
->num_children
== 2);
650 if (!typeof_math_call("/", op
, space
, &ti
->spec
, atoms
, log
))
653 /*case SLANG_OPER_MODULUS: */
654 case SLANG_OPER_PLUS
:
655 if (!_slang_typeof_operation_(op
->children
, space
, ti
, atoms
, log
))
657 ti
->can_be_referenced
= GL_FALSE
;
658 ti
->is_swizzled
= GL_FALSE
;
660 case SLANG_OPER_MINUS
:
661 assert(op
->num_children
== 1);
662 if (!typeof_math_call("-", op
, space
, &ti
->spec
, atoms
, log
))
665 /*case SLANG_OPER_COMPLEMENT: */
666 case SLANG_OPER_SUBSCRIPT
:
670 if (!slang_typeinfo_construct(&_ti
))
672 if (!_slang_typeof_operation_(op
->children
, space
, &_ti
, atoms
, log
)) {
673 slang_typeinfo_destruct(&_ti
);
676 ti
->can_be_referenced
= _ti
.can_be_referenced
;
677 if (_ti
.spec
.type
== SLANG_SPEC_ARRAY
) {
678 if (!slang_type_specifier_copy(&ti
->spec
, _ti
.spec
._array
)) {
679 slang_typeinfo_destruct(&_ti
);
684 if (!_slang_type_is_vector(_ti
.spec
.type
)
685 && !_slang_type_is_matrix(_ti
.spec
.type
)) {
686 slang_typeinfo_destruct(&_ti
);
687 slang_info_log_error(log
, "cannot index a non-array type");
690 ti
->spec
.type
= _slang_type_base(_ti
.spec
.type
);
692 slang_typeinfo_destruct(&_ti
);
695 case SLANG_OPER_CALL
:
696 if (op
->array_constructor
) {
697 /* build array typeinfo */
698 ti
->spec
.type
= SLANG_SPEC_ARRAY
;
699 ti
->spec
._array
= (slang_type_specifier
*)
700 _slang_alloc(sizeof(slang_type_specifier
));
701 slang_type_specifier_ctr(ti
->spec
._array
);
703 ti
->spec
._array
->type
=
704 slang_type_specifier_type_from_string((char *) op
->a_id
);
705 ti
->array_len
= op
->num_children
;
708 /* we've resolved this call before */
709 slang_type_specifier_copy(&ti
->spec
, &op
->fun
->header
.type
.specifier
);
713 if (!_slang_typeof_function(op
->a_id
, op
->children
, op
->num_children
,
714 space
, &ti
->spec
, &fun
, atoms
, log
))
717 /* save result for future use */
722 slang_struct_scope_find(space
->structs
, op
->a_id
, GL_TRUE
);
724 /* struct initializer */
725 ti
->spec
.type
= SLANG_SPEC_STRUCT
;
727 (slang_struct
*) _slang_alloc(sizeof(slang_struct
));
728 if (ti
->spec
._struct
== NULL
)
730 if (!slang_struct_construct(ti
->spec
._struct
)) {
731 _slang_free(ti
->spec
._struct
);
732 ti
->spec
._struct
= NULL
;
735 if (!slang_struct_copy(ti
->spec
._struct
, s
))
739 /* float, int, vec4, mat3, etc. constructor? */
741 slang_type_specifier_type type
;
743 name
= slang_atom_pool_id(atoms
, op
->a_id
);
744 type
= slang_type_specifier_type_from_string(name
);
745 if (type
== SLANG_SPEC_VOID
) {
746 slang_info_log_error(log
, "undefined function '%s'", name
);
749 ti
->spec
.type
= type
;
754 case SLANG_OPER_METHOD
:
755 /* at this time, GLSL 1.20 only has one method: array.length()
756 * which returns an integer.
758 ti
->spec
.type
= SLANG_SPEC_INT
;
760 case SLANG_OPER_FIELD
:
764 if (!slang_typeinfo_construct(&_ti
))
766 if (!_slang_typeof_operation_(op
->children
, space
, &_ti
, atoms
, log
)) {
767 slang_typeinfo_destruct(&_ti
);
770 if (_ti
.spec
.type
== SLANG_SPEC_STRUCT
) {
771 slang_variable
*field
;
773 field
= _slang_locate_variable(_ti
.spec
._struct
->fields
, op
->a_id
,
776 slang_typeinfo_destruct(&_ti
);
779 if (!slang_type_specifier_copy(&ti
->spec
, &field
->type
.specifier
)) {
780 slang_typeinfo_destruct(&_ti
);
783 ti
->can_be_referenced
= _ti
.can_be_referenced
;
788 slang_type_specifier_type base
;
790 /* determine the swizzle of the field expression */
791 if (!_slang_type_is_vector(_ti
.spec
.type
)) {
792 slang_typeinfo_destruct(&_ti
);
793 slang_info_log_error(log
, "Can't swizzle scalar expression");
796 rows
= _slang_type_dim(_ti
.spec
.type
);
797 swizzle
= slang_atom_pool_id(atoms
, op
->a_id
);
798 if (!_slang_is_swizzle(swizzle
, rows
, &ti
->swz
)) {
799 slang_typeinfo_destruct(&_ti
);
800 slang_info_log_error(log
, "bad swizzle '%s'", swizzle
);
803 ti
->is_swizzled
= GL_TRUE
;
804 ti
->can_be_referenced
= _ti
.can_be_referenced
805 && _slang_is_swizzle_mask(&ti
->swz
, rows
);
806 if (_ti
.is_swizzled
) {
809 /* swizzle the swizzle */
810 _slang_multiply_swizzles(&swz
, &_ti
.swz
, &ti
->swz
);
813 base
= _slang_type_base(_ti
.spec
.type
);
814 switch (ti
->swz
.num_components
) {
816 ti
->spec
.type
= base
;
820 case SLANG_SPEC_FLOAT
:
821 ti
->spec
.type
= SLANG_SPEC_VEC2
;
824 ti
->spec
.type
= SLANG_SPEC_IVEC2
;
826 case SLANG_SPEC_BOOL
:
827 ti
->spec
.type
= SLANG_SPEC_BVEC2
;
835 case SLANG_SPEC_FLOAT
:
836 ti
->spec
.type
= SLANG_SPEC_VEC3
;
839 ti
->spec
.type
= SLANG_SPEC_IVEC3
;
841 case SLANG_SPEC_BOOL
:
842 ti
->spec
.type
= SLANG_SPEC_BVEC3
;
850 case SLANG_SPEC_FLOAT
:
851 ti
->spec
.type
= SLANG_SPEC_VEC4
;
854 ti
->spec
.type
= SLANG_SPEC_IVEC4
;
856 case SLANG_SPEC_BOOL
:
857 ti
->spec
.type
= SLANG_SPEC_BVEC4
;
867 slang_typeinfo_destruct(&_ti
);
870 case SLANG_OPER_POSTINCREMENT
:
871 case SLANG_OPER_POSTDECREMENT
:
872 if (!_slang_typeof_operation_(op
->children
, space
, ti
, atoms
, log
))
874 ti
->can_be_referenced
= GL_FALSE
;
875 ti
->is_swizzled
= GL_FALSE
;
886 * Determine if a type is a matrix.
887 * \return GL_TRUE if is a matrix, GL_FALSE otherwise.
890 _slang_type_is_matrix(slang_type_specifier_type ty
)
893 case SLANG_SPEC_MAT2
:
894 case SLANG_SPEC_MAT3
:
895 case SLANG_SPEC_MAT4
:
896 case SLANG_SPEC_MAT23
:
897 case SLANG_SPEC_MAT32
:
898 case SLANG_SPEC_MAT24
:
899 case SLANG_SPEC_MAT42
:
900 case SLANG_SPEC_MAT34
:
901 case SLANG_SPEC_MAT43
:
910 * Determine if a type is a vector.
911 * \return GL_TRUE if is a vector, GL_FALSE otherwise.
914 _slang_type_is_vector(slang_type_specifier_type ty
)
917 case SLANG_SPEC_VEC2
:
918 case SLANG_SPEC_VEC3
:
919 case SLANG_SPEC_VEC4
:
920 case SLANG_SPEC_IVEC2
:
921 case SLANG_SPEC_IVEC3
:
922 case SLANG_SPEC_IVEC4
:
923 case SLANG_SPEC_BVEC2
:
924 case SLANG_SPEC_BVEC3
:
925 case SLANG_SPEC_BVEC4
:
934 * Determine if a type is a float, float vector or float matrix.
935 * \return GL_TRUE if so, GL_FALSE otherwise
938 _slang_type_is_float_vec_mat(slang_type_specifier_type ty
)
941 case SLANG_SPEC_FLOAT
:
942 case SLANG_SPEC_VEC2
:
943 case SLANG_SPEC_VEC3
:
944 case SLANG_SPEC_VEC4
:
945 case SLANG_SPEC_MAT2
:
946 case SLANG_SPEC_MAT3
:
947 case SLANG_SPEC_MAT4
:
948 case SLANG_SPEC_MAT23
:
949 case SLANG_SPEC_MAT32
:
950 case SLANG_SPEC_MAT24
:
951 case SLANG_SPEC_MAT42
:
952 case SLANG_SPEC_MAT34
:
953 case SLANG_SPEC_MAT43
:
962 * Given a vector type, return the type of the vector's elements.
963 * For a matrix, return the type of the columns.
965 slang_type_specifier_type
966 _slang_type_base(slang_type_specifier_type ty
)
969 case SLANG_SPEC_FLOAT
:
970 case SLANG_SPEC_VEC2
:
971 case SLANG_SPEC_VEC3
:
972 case SLANG_SPEC_VEC4
:
973 return SLANG_SPEC_FLOAT
;
975 case SLANG_SPEC_IVEC2
:
976 case SLANG_SPEC_IVEC3
:
977 case SLANG_SPEC_IVEC4
:
978 return SLANG_SPEC_INT
;
979 case SLANG_SPEC_BOOL
:
980 case SLANG_SPEC_BVEC2
:
981 case SLANG_SPEC_BVEC3
:
982 case SLANG_SPEC_BVEC4
:
983 return SLANG_SPEC_BOOL
;
984 case SLANG_SPEC_MAT2
:
985 return SLANG_SPEC_VEC2
;
986 case SLANG_SPEC_MAT3
:
987 return SLANG_SPEC_VEC3
;
988 case SLANG_SPEC_MAT4
:
989 return SLANG_SPEC_VEC4
;
990 case SLANG_SPEC_MAT23
:
991 return SLANG_SPEC_VEC3
;
992 case SLANG_SPEC_MAT32
:
993 return SLANG_SPEC_VEC2
;
994 case SLANG_SPEC_MAT24
:
995 return SLANG_SPEC_VEC4
;
996 case SLANG_SPEC_MAT42
:
997 return SLANG_SPEC_VEC2
;
998 case SLANG_SPEC_MAT34
:
999 return SLANG_SPEC_VEC4
;
1000 case SLANG_SPEC_MAT43
:
1001 return SLANG_SPEC_VEC3
;
1003 return SLANG_SPEC_VOID
;
1009 * Return the dimensionality of a vector, or for a matrix, return number
1013 _slang_type_dim(slang_type_specifier_type ty
)
1016 case SLANG_SPEC_FLOAT
:
1017 case SLANG_SPEC_INT
:
1018 case SLANG_SPEC_BOOL
:
1020 case SLANG_SPEC_VEC2
:
1021 case SLANG_SPEC_IVEC2
:
1022 case SLANG_SPEC_BVEC2
:
1023 case SLANG_SPEC_MAT2
:
1025 case SLANG_SPEC_VEC3
:
1026 case SLANG_SPEC_IVEC3
:
1027 case SLANG_SPEC_BVEC3
:
1028 case SLANG_SPEC_MAT3
:
1030 case SLANG_SPEC_VEC4
:
1031 case SLANG_SPEC_IVEC4
:
1032 case SLANG_SPEC_BVEC4
:
1033 case SLANG_SPEC_MAT4
:
1036 case SLANG_SPEC_MAT23
:
1038 case SLANG_SPEC_MAT32
:
1040 case SLANG_SPEC_MAT24
:
1042 case SLANG_SPEC_MAT42
:
1044 case SLANG_SPEC_MAT34
:
1046 case SLANG_SPEC_MAT43
:
1056 * Return the GL_* type that corresponds to a SLANG_SPEC_* type.
1059 _slang_gltype_from_specifier(const slang_type_specifier
*type
)
1061 switch (type
->type
) {
1062 case SLANG_SPEC_BOOL
:
1064 case SLANG_SPEC_BVEC2
:
1065 return GL_BOOL_VEC2
;
1066 case SLANG_SPEC_BVEC3
:
1067 return GL_BOOL_VEC3
;
1068 case SLANG_SPEC_BVEC4
:
1069 return GL_BOOL_VEC4
;
1070 case SLANG_SPEC_INT
:
1072 case SLANG_SPEC_IVEC2
:
1074 case SLANG_SPEC_IVEC3
:
1076 case SLANG_SPEC_IVEC4
:
1078 case SLANG_SPEC_FLOAT
:
1080 case SLANG_SPEC_VEC2
:
1081 return GL_FLOAT_VEC2
;
1082 case SLANG_SPEC_VEC3
:
1083 return GL_FLOAT_VEC3
;
1084 case SLANG_SPEC_VEC4
:
1085 return GL_FLOAT_VEC4
;
1086 case SLANG_SPEC_MAT2
:
1087 return GL_FLOAT_MAT2
;
1088 case SLANG_SPEC_MAT3
:
1089 return GL_FLOAT_MAT3
;
1090 case SLANG_SPEC_MAT4
:
1091 return GL_FLOAT_MAT4
;
1092 case SLANG_SPEC_MAT23
:
1093 return GL_FLOAT_MAT2x3
;
1094 case SLANG_SPEC_MAT32
:
1095 return GL_FLOAT_MAT3x2
;
1096 case SLANG_SPEC_MAT24
:
1097 return GL_FLOAT_MAT2x4
;
1098 case SLANG_SPEC_MAT42
:
1099 return GL_FLOAT_MAT4x2
;
1100 case SLANG_SPEC_MAT34
:
1101 return GL_FLOAT_MAT3x4
;
1102 case SLANG_SPEC_MAT43
:
1103 return GL_FLOAT_MAT4x3
;
1104 case SLANG_SPEC_SAMPLER1D
:
1105 return GL_SAMPLER_1D
;
1106 case SLANG_SPEC_SAMPLER2D
:
1107 return GL_SAMPLER_2D
;
1108 case SLANG_SPEC_SAMPLER3D
:
1109 return GL_SAMPLER_3D
;
1110 case SLANG_SPEC_SAMPLERCUBE
:
1111 return GL_SAMPLER_CUBE
;
1112 case SLANG_SPEC_SAMPLER1DSHADOW
:
1113 return GL_SAMPLER_1D_SHADOW
;
1114 case SLANG_SPEC_SAMPLER2DSHADOW
:
1115 return GL_SAMPLER_2D_SHADOW
;
1116 case SLANG_SPEC_SAMPLER2DRECT
:
1117 return GL_SAMPLER_2D_RECT_ARB
;
1118 case SLANG_SPEC_SAMPLER2DRECTSHADOW
:
1119 return GL_SAMPLER_2D_RECT_SHADOW_ARB
;
1120 case SLANG_SPEC_ARRAY
:
1121 return _slang_gltype_from_specifier(type
->_array
);
1122 case SLANG_SPEC_STRUCT
: