mojoshader.c
changeset 1055 3295380ce6fc
parent 1054 63dd1a46ce13
child 1058 dcbe39bffedc
equal deleted inserted replaced
1054:63dd1a46ce13 1055:3295380ce6fc
  7964 
  7964 
  7965     *_count = count;
  7965     *_count = count;
  7966     return retval;
  7966     return retval;
  7967 } // build_attributes
  7967 } // build_attributes
  7968 
  7968 
       
  7969 static MOJOSHADER_attribute *build_outputs(Context *ctx, int *_count)
       
  7970 {
       
  7971     int count = 0;
       
  7972 
       
  7973     if (ctx->attribute_count == 0)
       
  7974     {
       
  7975         *_count = 0;
       
  7976         return NULL;  // nothing to do.
       
  7977     } // if
       
  7978 
       
  7979     const size_t len = sizeof (MOJOSHADER_attribute) * ctx->attribute_count;
       
  7980     MOJOSHADER_attribute *retval = (MOJOSHADER_attribute *) Malloc(ctx, len);
       
  7981 
       
  7982     if (retval != NULL)
       
  7983     {
       
  7984         RegisterList *item = ctx->attributes.next;
       
  7985         MOJOSHADER_attribute *wptr = retval;
       
  7986         int i;
       
  7987 
       
  7988         memset(retval, '\0', len);
       
  7989 
       
  7990         for (i = 0; i < ctx->attribute_count; i++)
       
  7991         {
       
  7992             if (item == NULL)
       
  7993             {
       
  7994                 fail(ctx, "BUG: mismatched attribute list and count");
       
  7995                 break;
       
  7996             } // if
       
  7997 
       
  7998             switch (item->regtype)
       
  7999             {
       
  8000                 case REG_TYPE_RASTOUT:
       
  8001                 case REG_TYPE_ATTROUT:
       
  8002                 case REG_TYPE_TEXCRDOUT:
       
  8003                 case REG_TYPE_COLOROUT:
       
  8004                 case REG_TYPE_DEPTHOUT:
       
  8005                     wptr->usage = item->usage;
       
  8006                     wptr->index = item->index;
       
  8007                     wptr->name = alloc_varname(ctx, item);
       
  8008                     wptr++;
       
  8009                     count++;
       
  8010                     break;
       
  8011                 default:
       
  8012                     break;
       
  8013             } // switch
       
  8014 
       
  8015 
       
  8016             item = item->next;
       
  8017         } // for
       
  8018     } // if
       
  8019 
       
  8020     *_count = count;
       
  8021     return retval;
       
  8022 } // build_outputs
       
  8023 
  7969 
  8024 
  7970 static MOJOSHADER_parseData *build_parsedata(Context *ctx)
  8025 static MOJOSHADER_parseData *build_parsedata(Context *ctx)
  7971 {
  8026 {
  7972     char *output = NULL;
  8027     char *output = NULL;
  7973     MOJOSHADER_constant *constants = NULL;
  8028     MOJOSHADER_constant *constants = NULL;
  7974     MOJOSHADER_uniform *uniforms = NULL;
  8029     MOJOSHADER_uniform *uniforms = NULL;
  7975     MOJOSHADER_attribute *attributes = NULL;
  8030     MOJOSHADER_attribute *attributes = NULL;
       
  8031     MOJOSHADER_attribute *outputs = NULL;
  7976     MOJOSHADER_sampler *samplers = NULL;
  8032     MOJOSHADER_sampler *samplers = NULL;
  7977     MOJOSHADER_swizzle *swizzles = NULL;
  8033     MOJOSHADER_swizzle *swizzles = NULL;
  7978     MOJOSHADER_error *errors = NULL;
  8034     MOJOSHADER_error *errors = NULL;
  7979     MOJOSHADER_parseData *retval = NULL;
  8035     MOJOSHADER_parseData *retval = NULL;
  7980     size_t output_len = 0;
  8036     size_t output_len = 0;
  7981     int attribute_count = 0;
  8037     int attribute_count = 0;
       
  8038     int output_count = 0;
  7982 
  8039 
  7983     if (ctx->out_of_memory)
  8040     if (ctx->out_of_memory)
  7984         return &MOJOSHADER_out_of_mem_data;
  8041         return &MOJOSHADER_out_of_mem_data;
  7985 
  8042 
  7986     retval = (MOJOSHADER_parseData*) Malloc(ctx, sizeof(MOJOSHADER_parseData));
  8043     retval = (MOJOSHADER_parseData*) Malloc(ctx, sizeof(MOJOSHADER_parseData));
  7998     if (!isfail(ctx))
  8055     if (!isfail(ctx))
  7999         uniforms = build_uniforms(ctx);
  8056         uniforms = build_uniforms(ctx);
  8000 
  8057 
  8001     if (!isfail(ctx))
  8058     if (!isfail(ctx))
  8002         attributes = build_attributes(ctx, &attribute_count);
  8059         attributes = build_attributes(ctx, &attribute_count);
       
  8060 
       
  8061     if (!isfail(ctx))
       
  8062         outputs = build_outputs(ctx, &output_count);
  8003 
  8063 
  8004     if (!isfail(ctx))
  8064     if (!isfail(ctx))
  8005         samplers = build_samplers(ctx);
  8065         samplers = build_samplers(ctx);
  8006 
  8066 
  8007     const int error_count = errorlist_count(ctx->errors);
  8067     const int error_count = errorlist_count(ctx->errors);
  8037         if (attributes != NULL)
  8097         if (attributes != NULL)
  8038         {
  8098         {
  8039             for (i = 0; i < attribute_count; i++)
  8099             for (i = 0; i < attribute_count; i++)
  8040                 Free(ctx, (void *) attributes[i].name);
  8100                 Free(ctx, (void *) attributes[i].name);
  8041             Free(ctx, attributes);
  8101             Free(ctx, attributes);
       
  8102         } // if
       
  8103 
       
  8104         if (outputs != NULL)
       
  8105         {
       
  8106             for (i = 0; i < output_count; i++)
       
  8107                 Free(ctx, (void *) outputs[i].name);
       
  8108             Free(ctx, outputs);
  8042         } // if
  8109         } // if
  8043 
  8110 
  8044         if (samplers != NULL)
  8111         if (samplers != NULL)
  8045         {
  8112         {
  8046             for (i = 0; i < ctx->sampler_count; i++)
  8113             for (i = 0; i < ctx->sampler_count; i++)
  8075         retval->constants = constants;
  8142         retval->constants = constants;
  8076         retval->sampler_count = ctx->sampler_count;
  8143         retval->sampler_count = ctx->sampler_count;
  8077         retval->samplers = samplers;
  8144         retval->samplers = samplers;
  8078         retval->attribute_count = attribute_count;
  8145         retval->attribute_count = attribute_count;
  8079         retval->attributes = attributes;
  8146         retval->attributes = attributes;
       
  8147         retval->output_count = output_count;
       
  8148         retval->outputs = outputs;
  8080         retval->swizzle_count = ctx->swizzles_count;
  8149         retval->swizzle_count = ctx->swizzles_count;
  8081         retval->swizzles = swizzles;
  8150         retval->swizzles = swizzles;
  8082         retval->symbol_count = ctx->ctab.symbol_count;
  8151         retval->symbol_count = ctx->ctab.symbol_count;
  8083         retval->symbols = ctx->ctab.symbols;
  8152         retval->symbols = ctx->ctab.symbols;
  8084         retval->preshader = ctx->preshader;
  8153         retval->preshader = ctx->preshader;
  8384 
  8453 
  8385     for (i = 0; i < data->attribute_count; i++)
  8454     for (i = 0; i < data->attribute_count; i++)
  8386         f((void *) data->attributes[i].name, d);
  8455         f((void *) data->attributes[i].name, d);
  8387     f((void *) data->attributes, d);
  8456     f((void *) data->attributes, d);
  8388 
  8457 
       
  8458     for (i = 0; i < data->output_count; i++)
       
  8459         f((void *) data->outputs[i].name, d);
       
  8460     f((void *) data->outputs, d);
       
  8461 
  8389     for (i = 0; i < data->sampler_count; i++)
  8462     for (i = 0; i < data->sampler_count; i++)
  8390         f((void *) data->samplers[i].name, d);
  8463         f((void *) data->samplers[i].name, d);
  8391     f((void *) data->samplers, d);
  8464     f((void *) data->samplers, d);
  8392 
  8465 
  8393     free_symbols(f, d, data->symbols, data->symbol_count);
  8466     free_symbols(f, d, data->symbols, data->symbol_count);