mojoshader_compiler.c
changeset 980 83e96d285449
parent 979 f3d1c6675d97
child 981 bfcdebd39e5b
equal deleted inserted replaced
979:f3d1c6675d97 980:83e96d285449
  2146     delete_expr(ctx, *right);
  2146     delete_expr(ctx, *right);
  2147     *right = new_cast_expr(ctx, _ldatatype, new_literal_int_expr(ctx, 0));
  2147     *right = new_cast_expr(ctx, _ldatatype, new_literal_int_expr(ctx, 0));
  2148     return ldatatype;
  2148     return ldatatype;
  2149 } // add_type_coercion
  2149 } // add_type_coercion
  2150 
  2150 
  2151 static int is_swizzle_str(const char *str)
  2151 static int is_swizzle_str(const char *str, const int veclen)
  2152 {
  2152 {
  2153     int i;
  2153     int i;
  2154     int is_xyzw = 0;
  2154     int is_xyzw = 0;
  2155     int is_rgba = 0;
  2155     int is_rgba = 0;
  2156 
  2156 
  2157     assert(*str != '\0');  // can this actually happen?
  2157     assert(*str != '\0');  // can this actually happen?
  2158 
  2158 
  2159     for (i = 0; i < 4; i++, str++)
  2159     for (i = 0; i < veclen; i++, str++)
  2160     {
  2160     {
  2161         const char ch = *str;
  2161         const char ch = *str;
  2162         if (ch == '\0')
  2162         if (ch == '\0')
  2163             break;
  2163             break;
  2164         else if ((ch == 'x') || (ch == 'y') || (ch == 'z') || (ch == 'w'))
  2164         else if ((ch == 'x') || (ch == 'y') || (ch == 'z') || (ch == 'w'))
  2308             const MOJOSHADER_astDataType *reduced = reduce_datatype(ctx, datatype);
  2308             const MOJOSHADER_astDataType *reduced = reduce_datatype(ctx, datatype);
  2309 
  2309 
  2310             // Is this a swizzle and not a struct deref?
  2310             // Is this a swizzle and not a struct deref?
  2311             if (reduced->type == MOJOSHADER_AST_DATATYPE_VECTOR)
  2311             if (reduced->type == MOJOSHADER_AST_DATATYPE_VECTOR)
  2312             {
  2312             {
       
  2313                 const int veclen = reduced->vector.elements;
  2313                 ast->derefstruct.isswizzle = 1;
  2314                 ast->derefstruct.isswizzle = 1;
  2314                 if (!is_swizzle_str(member))
  2315                 if (!is_swizzle_str(member, veclen))
  2315                 {
  2316                 {
  2316                     fail(ctx, "invalid swizzle on vector");
  2317                     fail(ctx, "invalid swizzle on vector");
  2317                     // force this to be sane for further processing.
  2318                     // force this to be sane for further processing.
  2318                     const char *sane_swiz = stringcache(ctx->strcache, "xyzw");
  2319                     const char *sane_swiz = stringcache(ctx->strcache, "xyzw");
  2319                     member = ast->derefstruct.member = sane_swiz;
  2320                     member = ast->derefstruct.member = sane_swiz;
  2320                 } // if
  2321                 } // if
  2321 
  2322 
  2322                 const int swizlen = (int) strlen(member);
  2323                 const int swizlen = (int) strlen(member);
  2323                 if (swizlen == reduced->vector.elements)
  2324                 if (swizlen == veclen)
  2324                     datatype = reduced;
  2325                     datatype = reduced;
  2325                 else
  2326                 else
  2326                 {
  2327                 {
  2327                     const char *typestr = NULL;
  2328                     const char *typestr = NULL;
  2328                     switch (reduced->vector.base->type)
  2329                     switch (reduced->vector.base->type)