mojoshader_compiler.c
changeset 985 bea4dafa2782
parent 983 665e2d8bd7de
child 986 805aa6eb75af
equal deleted inserted replaced
984:b75637d50945 985:bea4dafa2782
  2291 
  2291 
  2292     return ident->datatype;
  2292     return ident->datatype;
  2293 } // match_func_to_call
  2293 } // match_func_to_call
  2294 
  2294 
  2295 
  2295 
       
  2296 static const MOJOSHADER_astDataType *vectype_from_base(Context *ctx,
       
  2297                                             const MOJOSHADER_astDataTypeType base,
       
  2298                                             const int len)
       
  2299 {
       
  2300     assert(len > 0);
       
  2301     assert(len <= 4);
       
  2302 
       
  2303     const char *typestr = NULL;
       
  2304     switch (base)
       
  2305     {
       
  2306         case MOJOSHADER_AST_DATATYPE_BOOL: typestr = "bool"; break;
       
  2307         case MOJOSHADER_AST_DATATYPE_INT: typestr = "int"; break;
       
  2308         case MOJOSHADER_AST_DATATYPE_UINT: typestr = "uint"; break;
       
  2309         case MOJOSHADER_AST_DATATYPE_HALF: typestr = "half"; break;
       
  2310         case MOJOSHADER_AST_DATATYPE_FLOAT: typestr = "float"; break;
       
  2311         case MOJOSHADER_AST_DATATYPE_DOUBLE: typestr = "double"; break;
       
  2312         default: assert(0 && "This shouldn't happen"); break;
       
  2313     } // switch
       
  2314 
       
  2315     char buf[32];
       
  2316     snprintf(buf, sizeof (buf), "%s%d", typestr, len);
       
  2317     const MOJOSHADER_astDataType *datatype = get_usertype(ctx, buf);
       
  2318     assert(datatype != NULL);
       
  2319     return datatype;
       
  2320 } // vectype_from_base
       
  2321 
       
  2322 
  2296 // Go through the AST and make sure all datatypes check out okay. For datatypes
  2323 // Go through the AST and make sure all datatypes check out okay. For datatypes
  2297 //  that are compatible but are relying on an implicit cast, we add explicit
  2324 //  that are compatible but are relying on an implicit cast, we add explicit
  2298 //  casts to the AST here, so further processing doesn't have to worry about
  2325 //  casts to the AST here, so further processing doesn't have to worry about
  2299 //  type coercion.
  2326 //  type coercion.
  2300 // For things that are incompatible, we generate errors and
  2327 // For things that are incompatible, we generate errors and
  2334             return datatype;
  2361             return datatype;
  2335 
  2362 
  2336         case MOJOSHADER_AST_OP_DEREF_ARRAY:
  2363         case MOJOSHADER_AST_OP_DEREF_ARRAY:
  2337             datatype = type_check_ast(ctx, ast->binary.left);
  2364             datatype = type_check_ast(ctx, ast->binary.left);
  2338             datatype2 = type_check_ast(ctx, ast->binary.right);
  2365             datatype2 = type_check_ast(ctx, ast->binary.right);
  2339             require_array_datatype(ctx, datatype);
       
  2340             require_integer_datatype(ctx, datatype2);
  2366             require_integer_datatype(ctx, datatype2);
  2341             add_type_coercion(ctx, NULL, &ctx->dt_int, &ast->binary.right, datatype2);
  2367             add_type_coercion(ctx, NULL, &ctx->dt_int, &ast->binary.right, datatype2);
  2342             ast->binary.datatype = array_element_datatype(ctx, datatype);
  2368 
       
  2369             datatype = reduce_datatype(ctx, datatype);
       
  2370             if (datatype->type == MOJOSHADER_AST_DATATYPE_VECTOR)
       
  2371                 ast->binary.datatype = datatype->vector.base;
       
  2372             else if (datatype->type == MOJOSHADER_AST_DATATYPE_MATRIX)
       
  2373                 ast->binary.datatype = vectype_from_base(ctx, datatype->matrix.base->type, datatype->matrix.columns);  // !!! FIXME: rows?
       
  2374             else
       
  2375             {
       
  2376                 require_array_datatype(ctx, datatype);
       
  2377                 ast->binary.datatype = array_element_datatype(ctx, datatype);
       
  2378             } // else
       
  2379 
  2343             return ast->binary.datatype;
  2380             return ast->binary.datatype;
  2344 
  2381 
  2345         case MOJOSHADER_AST_OP_DEREF_STRUCT:
  2382         case MOJOSHADER_AST_OP_DEREF_STRUCT:
  2346         {
  2383         {
  2347             const char *member = ast->derefstruct.member;
  2384             const char *member = ast->derefstruct.member;
  2361                     member = ast->derefstruct.member = sane_swiz;
  2398                     member = ast->derefstruct.member = sane_swiz;
  2362                 } // if
  2399                 } // if
  2363 
  2400 
  2364                 const int swizlen = (int) strlen(member);
  2401                 const int swizlen = (int) strlen(member);
  2365                 if (swizlen != veclen)
  2402                 if (swizlen != veclen)
  2366                 {
  2403                     datatype = vectype_from_base(ctx, reduced->vector.base->type, swizlen);
  2367                     const char *typestr = NULL;
       
  2368                     switch (reduced->vector.base->type)
       
  2369                     {
       
  2370                         case MOJOSHADER_AST_DATATYPE_BOOL: typestr = "bool"; break;
       
  2371                         case MOJOSHADER_AST_DATATYPE_INT: typestr = "int"; break;
       
  2372                         case MOJOSHADER_AST_DATATYPE_UINT: typestr = "uint"; break;
       
  2373                         case MOJOSHADER_AST_DATATYPE_HALF: typestr = "half"; break;
       
  2374                         case MOJOSHADER_AST_DATATYPE_FLOAT: typestr = "float"; break;
       
  2375                         case MOJOSHADER_AST_DATATYPE_DOUBLE: typestr = "double"; break;
       
  2376                         default: assert(0 && "This shouldn't happen"); break;
       
  2377                     } // switch
       
  2378 
       
  2379                     char buf[32];
       
  2380                     snprintf(buf, sizeof (buf), "%s%d", typestr, swizlen);
       
  2381                     datatype = get_usertype(ctx, buf);
       
  2382                     assert(datatype != NULL);
       
  2383                 } // if
       
  2384 
  2404 
  2385                 ast->derefstruct.datatype = datatype;
  2405                 ast->derefstruct.datatype = datatype;
  2386                 return ast->derefstruct.datatype;
  2406                 return ast->derefstruct.datatype;
  2387             } // if
  2407             } // if
  2388 
  2408