Navigation Menu

Skip to content

Commit

Permalink
Added profile-specific variable names to returned parse data.
Browse files Browse the repository at this point in the history
--HG--
branch : trunk
  • Loading branch information
icculus committed Apr 25, 2008
1 parent a9f55a9 commit 65152f6
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 25 deletions.
108 changes: 83 additions & 25 deletions mojoshader.c
Expand Up @@ -348,6 +348,7 @@ struct Context

static inline void *Malloc(const Context *ctx, const int len)
{
// !!! FIXME: just call out_of_memory() from here?
return ctx->malloc(len, ctx->malloc_data);
} // Malloc

Expand Down Expand Up @@ -4471,10 +4472,23 @@ static char *build_output(Context *ctx)
} // build_output


static char *alloc_varname(Context *ctx, const RegisterList *reg)
{
const char *varname = get_GLSL_varname(ctx, reg->regtype, reg->regnum);
const size_t len = strlen(varname) + 1;
char *retval = (char *) Malloc(ctx, len);
if (retval == NULL)
out_of_memory(ctx);
else
strcpy(retval, varname);
return retval;
} // alloc_varname


static MOJOSHADER_uniform *build_uniforms(Context *ctx)
{
MOJOSHADER_uniform *retval = (MOJOSHADER_uniform *)
Malloc(ctx, sizeof (MOJOSHADER_uniform) * ctx->uniform_count);
const size_t len = sizeof (MOJOSHADER_uniform) * ctx->uniform_count;
MOJOSHADER_uniform *retval = (MOJOSHADER_uniform *) Malloc(ctx, len);

if (retval == NULL)
out_of_memory(ctx);
Expand All @@ -4485,6 +4499,8 @@ static MOJOSHADER_uniform *build_uniforms(Context *ctx)
int index = 0;
int i;

memset(retval, '\0', len);

for (i = 0; i < ctx->uniform_count; i++)
{
if (item == NULL)
Expand Down Expand Up @@ -4530,8 +4546,9 @@ static MOJOSHADER_uniform *build_uniforms(Context *ctx)

retval[i].type = type;
retval[i].index = index;
retval[i].name = alloc_varname(ctx, item);
item = item->next;
} // while
} // for
} // else

return retval;
Expand All @@ -4540,8 +4557,8 @@ static MOJOSHADER_uniform *build_uniforms(Context *ctx)

static MOJOSHADER_sampler *build_samplers(Context *ctx)
{
MOJOSHADER_sampler *retval = (MOJOSHADER_sampler *)
Malloc(ctx, sizeof (MOJOSHADER_sampler) * ctx->sampler_count);
const size_t len = sizeof (MOJOSHADER_sampler) * ctx->sampler_count;
MOJOSHADER_sampler *retval = (MOJOSHADER_sampler *) Malloc(ctx, len);

if (retval == NULL)
out_of_memory(ctx);
Expand All @@ -4551,6 +4568,8 @@ static MOJOSHADER_sampler *build_samplers(Context *ctx)
MOJOSHADER_samplerType type = MOJOSHADER_SAMPLER_2D;
int i;

memset(retval, '\0', len);

for (i = 0; i < ctx->sampler_count; i++)
{
if (item == NULL)
Expand Down Expand Up @@ -4581,8 +4600,9 @@ static MOJOSHADER_sampler *build_samplers(Context *ctx)

retval[i].type = type;
retval[i].index = item->regnum;
retval[i].name = alloc_varname(ctx, item);
item = item->next;
} // while
} // for
} // else

return retval;
Expand All @@ -4591,13 +4611,16 @@ static MOJOSHADER_sampler *build_samplers(Context *ctx)

static MOJOSHADER_attribute *build_attributes(Context *ctx, int *_count)
{
*_count = 0;
int count = 0;

if (ctx->attribute_count == 0)
{
*_count = 0;
return NULL; // nothing to do.
} // if

MOJOSHADER_attribute *retval = (MOJOSHADER_attribute *)
Malloc(ctx, sizeof (MOJOSHADER_attribute) * ctx->attribute_count);
const size_t len = sizeof (MOJOSHADER_attribute) * ctx->attribute_count;
MOJOSHADER_attribute *retval = (MOJOSHADER_attribute *) Malloc(ctx, len);

if (retval == NULL)
out_of_memory(ctx);
Expand All @@ -4606,9 +4629,10 @@ static MOJOSHADER_attribute *build_attributes(Context *ctx, int *_count)
RegisterList *item = ctx->attributes.next;
MOJOSHADER_attribute *wptr = retval;
int is_output = 0;
int count = 0;
int i;

memset(retval, '\0', len);

for (i = 0; i < ctx->attribute_count; i++)
{
if (item == NULL)
Expand All @@ -4633,26 +4657,26 @@ static MOJOSHADER_attribute *build_attributes(Context *ctx, int *_count)

if (!is_output)
{
wptr->usage = item->usage;
wptr->index = item->index;
wptr++;
count++;
if (shader_is_pixel(ctx))
{
fail(ctx, "BUG: pixel shader with vertex attributes");
break;
} // if
else
{
wptr->usage = item->usage;
wptr->index = item->index;
wptr->name = alloc_varname(ctx, item);
wptr++;
count++;
} // else
} // if

item = item->next;
} // while

if (shader_is_pixel(ctx))
{
if (count > 0)
fail(ctx, "BUG: pixel shader shouldn't have vertex attributes");
Free(ctx, retval);
return NULL; // nothing to do for pixel shaders.
} // if

*_count = count;
} // for
} // else

*_count = count;
return retval;
} // build_attributes

Expand Down Expand Up @@ -4686,10 +4710,22 @@ static MOJOSHADER_parseData *build_parsedata(Context *ctx)
// check again, in case build_output ran out of memory.
if (isfail(ctx))
{
int i;

Free(ctx, output);

for (i = 0; i < ctx->uniform_count; i++)
Free(ctx, (void *) uniforms[i].name);
Free(ctx, uniforms);

for (i = 0; i < attribute_count; i++)
Free(ctx, (void *) attributes[i].name);
Free(ctx, attributes);

for (i = 0; i < ctx->sampler_count; i++)
Free(ctx, (void *) samplers[i].name);
Free(ctx, samplers);

retval->error = ctx->failstr; // we recycle. :)
ctx->failstr = NULL; // don't let this get free()'d too soon.
} // if
Expand Down Expand Up @@ -4861,20 +4897,42 @@ void MOJOSHADER_freeParseData(const MOJOSHADER_parseData *_data)

MOJOSHADER_free f = (data->free == NULL) ? internal_free : data->free;
void *d = data->malloc_data;
int i;

// we don't f(data->profile), because that's internal static data.

if (data->output != NULL) // check for NULL in case of dumb free() impl.
f((void *) data->output, d);

if (data->uniforms != NULL)
{
for (i = 0; i < data->uniform_count; i++)
{
if (data->uniforms[i].name != NULL)
f((void *) data->uniforms[i].name, d);
} // for
f((void *) data->uniforms, d);
} // if

if (data->attributes != NULL)
{
for (i = 0; i < data->attribute_count; i++)
{
if (data->attributes[i].name != NULL)
f((void *) data->attributes[i].name, d);
} // for
f((void *) data->attributes, d);
} // if

if (data->samplers != NULL)
{
for (i = 0; i < data->sampler_count; i++)
{
if (data->samplers[i].name != NULL)
f((void *) data->samplers[i].name, d);
} // for
f((void *) data->samplers, d);
} // if

if ((data->error != NULL) && (data->error != out_of_mem_str))
f((void *) data->error, d);
Expand Down
9 changes: 9 additions & 0 deletions mojoshader.h
Expand Up @@ -66,11 +66,14 @@ typedef enum
* index==6 and type==MOJOSHADER_UNIFORM_FLOAT, that means we'd expect a
* 4-float vector to be specified for what would be register "c6" in D3D
* assembly language, before drawing with the shader.
* (name) is a profile-specific variable name; it may be NULL if it isn't
* applicable to the requested profile.
*/
typedef struct
{
MOJOSHADER_uniformType type;
int index;
const char *name;
} MOJOSHADER_uniform;

/*
Expand All @@ -90,11 +93,14 @@ typedef enum
* type==MOJOSHADER_SAMPLER_2D, that means we'd expect a regular 2D texture
* to be specified for what would be register "s6" in D3D assembly language,
* before drawing with the shader.
* (name) is a profile-specific variable name; it may be NULL if it isn't
* applicable to the requested profile.
*/
typedef struct
{
MOJOSHADER_samplerType type;
int index;
const char *name;
} MOJOSHADER_sampler;

/*
Expand Down Expand Up @@ -127,11 +133,14 @@ typedef enum
* element per-vertex. So if usage==MOJOSHADER_USAGE_COLOR and index==1, that
* means we'd expect a secondary color array to be bound to this shader
* before drawing.
* (name) is a profile-specific variable name; it may be NULL if it isn't
* applicable to the requested profile.
*/
typedef struct
{
MOJOSHADER_usage usage;
int index;
const char *name;
} MOJOSHADER_attribute;

/*
Expand Down

0 comments on commit 65152f6

Please sign in to comment.