From 1b1b92b603059be67f684a18c534b930b62c16ad Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Mon, 20 Jun 2011 15:47:16 -0400 Subject: [PATCH] Report shader outputs in MOJOSHADER_parseData. --- mojoshader.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++ mojoshader.h | 17 +++++++++++ utils/testparse.c | 60 +++++++++++++++++++++----------------- 3 files changed, 124 insertions(+), 26 deletions(-) diff --git a/mojoshader.c b/mojoshader.c index 9e969573..dc0b82a5 100644 --- a/mojoshader.c +++ b/mojoshader.c @@ -7966,6 +7966,61 @@ static MOJOSHADER_attribute *build_attributes(Context *ctx, int *_count) return retval; } // build_attributes +static MOJOSHADER_attribute *build_outputs(Context *ctx, int *_count) +{ + int count = 0; + + if (ctx->attribute_count == 0) + { + *_count = 0; + return NULL; // nothing to do. + } // if + + const size_t len = sizeof (MOJOSHADER_attribute) * ctx->attribute_count; + MOJOSHADER_attribute *retval = (MOJOSHADER_attribute *) Malloc(ctx, len); + + if (retval != NULL) + { + RegisterList *item = ctx->attributes.next; + MOJOSHADER_attribute *wptr = retval; + int i; + + memset(retval, '\0', len); + + for (i = 0; i < ctx->attribute_count; i++) + { + if (item == NULL) + { + fail(ctx, "BUG: mismatched attribute list and count"); + break; + } // if + + switch (item->regtype) + { + case REG_TYPE_RASTOUT: + case REG_TYPE_ATTROUT: + case REG_TYPE_TEXCRDOUT: + case REG_TYPE_COLOROUT: + case REG_TYPE_DEPTHOUT: + wptr->usage = item->usage; + wptr->index = item->index; + wptr->name = alloc_varname(ctx, item); + wptr++; + count++; + break; + default: + break; + } // switch + + + item = item->next; + } // for + } // if + + *_count = count; + return retval; +} // build_outputs + static MOJOSHADER_parseData *build_parsedata(Context *ctx) { @@ -7973,12 +8028,14 @@ static MOJOSHADER_parseData *build_parsedata(Context *ctx) MOJOSHADER_constant *constants = NULL; MOJOSHADER_uniform *uniforms = NULL; MOJOSHADER_attribute *attributes = NULL; + MOJOSHADER_attribute *outputs = NULL; MOJOSHADER_sampler *samplers = NULL; MOJOSHADER_swizzle *swizzles = NULL; MOJOSHADER_error *errors = NULL; MOJOSHADER_parseData *retval = NULL; size_t output_len = 0; int attribute_count = 0; + int output_count = 0; if (ctx->out_of_memory) return &MOJOSHADER_out_of_mem_data; @@ -8001,6 +8058,9 @@ static MOJOSHADER_parseData *build_parsedata(Context *ctx) if (!isfail(ctx)) attributes = build_attributes(ctx, &attribute_count); + if (!isfail(ctx)) + outputs = build_outputs(ctx, &output_count); + if (!isfail(ctx)) samplers = build_samplers(ctx); @@ -8041,6 +8101,13 @@ static MOJOSHADER_parseData *build_parsedata(Context *ctx) Free(ctx, attributes); } // if + if (outputs != NULL) + { + for (i = 0; i < output_count; i++) + Free(ctx, (void *) outputs[i].name); + Free(ctx, outputs); + } // if + if (samplers != NULL) { for (i = 0; i < ctx->sampler_count; i++) @@ -8077,6 +8144,8 @@ static MOJOSHADER_parseData *build_parsedata(Context *ctx) retval->samplers = samplers; retval->attribute_count = attribute_count; retval->attributes = attributes; + retval->output_count = output_count; + retval->outputs = outputs; retval->swizzle_count = ctx->swizzles_count; retval->swizzles = swizzles; retval->symbol_count = ctx->ctab.symbol_count; @@ -8386,6 +8455,10 @@ void MOJOSHADER_freeParseData(const MOJOSHADER_parseData *_data) f((void *) data->attributes[i].name, d); f((void *) data->attributes, d); + for (i = 0; i < data->output_count; i++) + f((void *) data->outputs[i].name, d); + f((void *) data->outputs, d); + for (i = 0; i < data->sampler_count; i++) f((void *) data->samplers[i].name, d); f((void *) data->samplers, d); diff --git a/mojoshader.h b/mojoshader.h index 4d4a0bdf..60df73ea 100644 --- a/mojoshader.h +++ b/mojoshader.h @@ -532,11 +532,13 @@ typedef struct MOJOSHADER_parseData */ MOJOSHADER_sampler *samplers; + /* !!! FIXME: this should probably be "input" and not "attribute" */ /* * The number of elements pointed to by (attributes). */ int attribute_count; + /* !!! FIXME: this should probably be "input" and not "attribute" */ /* * (attribute_count) elements of data that specify Attributes to be set * for this shader. See discussion on MOJOSHADER_attribute for details. @@ -544,11 +546,24 @@ typedef struct MOJOSHADER_parseData */ MOJOSHADER_attribute *attributes; + /* + * The number of elements pointed to by (outputs). + */ + int output_count; + + /* + * (output_count) elements of data that specify outputs this shader + * writes to. See discussion on MOJOSHADER_attribute for details. + * This can be NULL on error or if (output_count) is zero. + */ + MOJOSHADER_attribute *outputs; + /* * The number of elements pointed to by (swizzles). */ int swizzle_count; + /* !!! FIXME: this should probably be "input" and not "attribute" */ /* * (swizzle_count) elements of data that specify swizzles the shader will * apply to incoming attributes. This is a copy of what was passed to @@ -2996,6 +3011,8 @@ void MOJOSHADER_glGetPixelShaderUniformB(unsigned int idx, int *data, * * Vertex attributes are not shared between contexts. */ + /* !!! FIXME: this should probably be "input" and not "attribute" */ + /* !!! FIXME: or maybe "vertex array" or something. */ void MOJOSHADER_glSetVertexAttribute(MOJOSHADER_usage usage, int index, unsigned int size, MOJOSHADER_attributeType type, diff --git a/utils/testparse.c b/utils/testparse.c index a40bd93f..7dee4b15 100644 --- a/utils/testparse.c +++ b/utils/testparse.c @@ -234,6 +234,38 @@ static void print_preshader(const MOJOSHADER_preshader *preshader, } // print_preshader +static void print_attrs(const char *category, const int count, + const MOJOSHADER_attribute *attributes, + const int indent) +{ + INDENT(); printf("%s:", category); + if (count == 0) + printf(" (none.)\n"); + else + { + int i; + printf("\n"); + for (i = 0; i < count; i++) + { + static const char *usagenames[] = { + "position", "blendweight", "blendindices", "normal", + "psize", "texcoord", "tangent", "binormal", "tessfactor", + "positiont", "color", "fog", "depth", "sample" + }; + const MOJOSHADER_attribute *a = &attributes[i]; + char numstr[16] = { 0 }; + if (a->index != 0) + snprintf(numstr, sizeof (numstr), "%d", a->index); + INDENT(); + printf(" * %s%s", usagenames[(int) a->usage], numstr); + if (a->name != NULL) + printf(" (\"%s\")", a->name); + printf("\n"); + } // for + } // else +} // print_attrs + + static void print_shader(const char *fname, const MOJOSHADER_parseData *pd, unsigned int indent) { @@ -255,32 +287,8 @@ static void print_shader(const char *fname, const MOJOSHADER_parseData *pd, INDENT(); printf("SHADER TYPE: %s\n", shader_type(pd->shader_type)); INDENT(); printf("VERSION: %d.%d\n", pd->major_ver, pd->minor_ver); INDENT(); printf("INSTRUCTION COUNT: %d\n", (int) pd->instruction_count); - - INDENT(); printf("ATTRIBUTES:"); - if (pd->attribute_count == 0) - printf(" (none.)\n"); - else - { - int i; - printf("\n"); - for (i = 0; i < pd->attribute_count; i++) - { - static const char *usagenames[] = { - "position", "blendweight", "blendindices", "normal", - "psize", "texcoord", "tangent", "binormal", "tessfactor", - "positiont", "color", "fog", "depth", "sample" - }; - const MOJOSHADER_attribute *a = &pd->attributes[i]; - char numstr[16] = { 0 }; - if (a->index != 0) - snprintf(numstr, sizeof (numstr), "%d", a->index); - INDENT(); - printf(" * %s%s", usagenames[(int) a->usage], numstr); - if (a->name != NULL) - printf(" (\"%s\")", a->name); - printf("\n"); - } // for - } // else + print_attrs("INPUTS", pd->attribute_count, pd->attributes, indent); + print_attrs("OUTPUTS", pd->output_count, pd->outputs, indent); INDENT(); printf("CONSTANTS:"); if (pd->constant_count == 0)