Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Cache attribute locations for GLSL programs at link time.
--HG--
branch : trunk
  • Loading branch information
icculus committed Apr 27, 2008
1 parent 28b005b commit 5fcff69
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions mojoshader_opengl.c
Expand Up @@ -93,13 +93,21 @@ typedef struct
GLint location;
} UniformMap;

typedef struct
{
MOJOSHADER_attribute *attribute;
GLint location;
} AttributeMap;

struct MOJOSHADER_glProgram
{
const MOJOSHADER_glShader *vertex;
const MOJOSHADER_glShader *fragment;
GLhandleARB handle;
uint32 uniform_count;
UniformMap uniforms;
uint32 attribute_count;
AttributeMap attributes;
uint32 refcount;
};

Expand Down Expand Up @@ -248,6 +256,7 @@ static void program_unref(MOJOSHADER_glProgram *program)
pglDeleteObjectARB(program->handle);
shader_unref(program->vertex);
shader_unref(program->fragment);
Free(program->attributes);
Free(program->uniforms);
Free(program);
} // else
Expand Down Expand Up @@ -278,6 +287,26 @@ static void lookup_uniforms(MOJOSHADER_glProgram *program,
} // lookup_uniforms


static void lookup_attributes(MOJOSHADER_glProgram *program)
{
int i;
const MOJOSHADER_parseData *pd = program->vertex->parseData;
const MOJOSHADER_attributes *a = pd->attributes;

for (i = 0; i < pd->attribute_count; i++)
{
const GLint loc = pglGetAttribLocationARB(program->handle, a->name);
if (loc != -1) // maybe the Attribute was optimized out?
{
AttributeMap *map = &program->attributes[program->attribute_count];
map->attribute = &a[i];
map->location = loc;
program->attribute_count++;
} // if
} // for
} // lookup_attributes


MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,
MOJOSHADER_glShader *pshader)
{
Expand Down Expand Up @@ -323,6 +352,12 @@ MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,

if (vshader != NULL)
{
retval->attributes = (AttributeMap *) Malloc(sizeof (AttributeMap) *
vshader->parseData->attribute_count);
if (retval->attributes == NULL)
goto link_program_fail;

lookup_attributes(retval);
lookup_uniforms(retval, vshader);
vshader->refcount++;
} // if
Expand All @@ -338,8 +373,8 @@ MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,
link_program_fail:
if (retval != NULL)
{
if (retval->uniforms != NULL)
Free(retval->uniforms);
Free(retval->uniforms);
Free(retval->attributes);
Free(retval);
} // if

Expand Down

0 comments on commit 5fcff69

Please sign in to comment.