Skip to content

Commit

Permalink
Report shader outputs in MOJOSHADER_parseData.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Jun 20, 2011
1 parent f47794a commit 1b1b92b
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 26 deletions.
73 changes: 73 additions & 0 deletions mojoshader.c
Expand Up @@ -7966,19 +7966,76 @@ 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)
{
char *output = NULL;
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;
Expand All @@ -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);

Expand Down Expand Up @@ -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++)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions mojoshader.h
Expand Up @@ -532,23 +532,38 @@ 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.
* This can be NULL on error or if (attribute_count) is zero.
*/
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
Expand Down Expand Up @@ -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,
Expand Down
60 changes: 34 additions & 26 deletions utils/testparse.c
Expand Up @@ -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)
{
Expand All @@ -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)
Expand Down

0 comments on commit 1b1b92b

Please sign in to comment.