Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Cleanup program binding at link time.
Only bind program at link time if forced to, only do it once, and only reset
 the binding back to the original state at the end, if necessary.
  • Loading branch information
icculus committed Aug 6, 2009
1 parent 58b082d commit 04d3245
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions mojoshader_opengl.c
Expand Up @@ -1162,7 +1162,7 @@ static void fill_constant_array(GLfloat *f, const int base, const int size,


static void lookup_uniforms(MOJOSHADER_glProgram *program,
MOJOSHADER_glShader *shader)
MOJOSHADER_glShader *shader, int *bound)
{
const MOJOSHADER_parseData *pd = shader->parseData;
const MOJOSHADER_shaderType shader_type = pd->shader_type;
Expand All @@ -1183,9 +1183,12 @@ static void lookup_uniforms(MOJOSHADER_glProgram *program,
const int size = u->array_count;
GLfloat *f = (GLfloat *) alloca(sizeof (GLfloat)*(size*4));
fill_constant_array(f, base, size, pd);
ctx->profileUseProgramObject(program);
if (!(*bound))
{
ctx->profileUseProgramObject(program);
*bound = 1;
} // if
ctx->profileUniform4fv(pd, loc, size, f);
ctx->profileUseProgramObject(ctx->bound_program);
} // if
} // if

Expand All @@ -1203,7 +1206,7 @@ static void lookup_uniforms(MOJOSHADER_glProgram *program,


static void lookup_samplers(MOJOSHADER_glProgram *program,
MOJOSHADER_glShader *shader)
MOJOSHADER_glShader *shader, int *bound)
{
const MOJOSHADER_parseData *pd = shader->parseData;
const MOJOSHADER_sampler *s = pd->samplers;
Expand All @@ -1215,16 +1218,18 @@ static void lookup_samplers(MOJOSHADER_glProgram *program,
// Link up the Samplers. These never change after link time, since they
// are meant to be constant texture unit ids and not textures.

ctx->profileUseProgramObject(program);
if (!(*bound))
{
ctx->profileUseProgramObject(program);
*bound = 1;
} // if

for (i = 0; i < pd->sampler_count; i++)
{
const GLint loc = ctx->profileGetSamplerLocation(program, shader, i);
if (loc != -1) // maybe the Sampler was optimized out?
ctx->profileSetSampler(loc, s[i].index);
} // for

ctx->profileUseProgramObject(ctx->bound_program);
} // lookup_samplers


Expand Down Expand Up @@ -1284,6 +1289,8 @@ static int build_constants_lists(MOJOSHADER_glProgram *program)
MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,
MOJOSHADER_glShader *pshader)
{
int bound = 0;

if ((vshader == NULL) && (pshader == NULL))
return NULL;

Expand Down Expand Up @@ -1329,21 +1336,24 @@ MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,
lookup_attributes(retval);
} // if

lookup_uniforms(retval, vshader);
lookup_samplers(retval, vshader);
lookup_uniforms(retval, vshader, &bound);
lookup_samplers(retval, vshader, &bound);
vshader->refcount++;
} // if

if (pshader != NULL)
{
lookup_uniforms(retval, pshader);
lookup_samplers(retval, pshader);
lookup_uniforms(retval, pshader, &bound);
lookup_samplers(retval, pshader, &bound);
pshader->refcount++;
} // if

if (!build_constants_lists(retval))
goto link_program_fail;

if (bound) // reset the old binding.
ctx->profileUseProgramObject(ctx->bound_program);

return retval;

link_program_fail:
Expand All @@ -1360,6 +1370,9 @@ MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,
if (program != 0)
ctx->profileDeleteProgram(program);

if (bound)
ctx->profileUseProgramObject(ctx->bound_program);

return NULL;
} // MOJOSHADER_glLinkProgram

Expand Down

0 comments on commit 04d3245

Please sign in to comment.