Skip to content

Commit

Permalink
OpenGL glue now handles Uniform arrays.
Browse files Browse the repository at this point in the history
Completed untested and sort of crappy.

--HG--
branch : trunk
  • Loading branch information
icculus committed May 6, 2008
1 parent 0f94868 commit 3e0f4aa
Showing 1 changed file with 106 additions and 1 deletion.
107 changes: 106 additions & 1 deletion mojoshader_opengl.c
Expand Up @@ -70,6 +70,8 @@ struct MOJOSHADER_glProgram
MOJOSHADER_glShader *vertex;
MOJOSHADER_glShader *fragment;
GLhandleARB handle;
uint32 constant_count; // !!! FIXME: misnamed.
GLfloat *constants; // !!! FIXME: misnamed.
uint32 uniform_count;
UniformMap *uniforms;
uint32 attribute_count;
Expand Down Expand Up @@ -524,6 +526,7 @@ static void program_unref(MOJOSHADER_glProgram *program)
shader_unref(program->fragment);
Free(program->attributes);
Free(program->uniforms);
Free(program->constants);
Free(program);
} // else
} // if
Expand Down Expand Up @@ -614,8 +617,12 @@ MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,
retval->fragment = pshader;
retval->refcount = 1;

uint32 const_count = 0;

if (vshader != NULL)
{
if (const_count < vshader->parseData->constant_count)
const_count = vshader->parseData->constant_count;
retval->attributes = (AttributeMap *) Malloc(sizeof (AttributeMap) *
vshader->parseData->attribute_count);
if (retval->attributes == NULL)
Expand All @@ -628,17 +635,29 @@ MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,

if (pshader != NULL)
{
if (const_count < pshader->parseData->constant_count)
const_count = pshader->parseData->constant_count;

lookup_uniforms(retval, pshader);
pshader->refcount++;
} // if

if (const_count > 0)
{
retval->constants = (GLfloat *) Malloc(sizeof (GLfloat) * const_count);
if (retval->constants == NULL)
goto link_program_fail;
retval->constant_count = const_count;
} // if

return retval;

link_program_fail:
if (retval != NULL)
{
Free(retval->uniforms);
Free(retval->attributes);
Free(retval->constants);
Free(retval);
} // if

Expand Down Expand Up @@ -853,9 +872,95 @@ void MOJOSHADER_glProgramReady(void)
const MOJOSHADER_uniformType type = u->type;
const MOJOSHADER_shaderType shader_type = map->shader_type;
const int index = u->index;
const int size = u->array_count;
const GLint location = map->location;

if (shader_type == MOJOSHADER_TYPE_VERTEX)
// only use arrays for 'c' registers.
assert((size == 0) || (type == MOJOSHADER_UNIFORM_FLOAT));

if (size != 0) // !!! FIXME: this code sucks.
{
// !!! FIXME: calculate this all at link time.
if (shader_type == MOJOSHADER_TYPE_VERTEX)
{
const MOJOSHADER_constant *c = ctx->bound_program->vertex->parseData->constants;
int hi = ctx->bound_program->vertex->parseData->constant_count;

int j;
GLfloat *ptr = ctx->bound_program->constants;

for (j = 0; j < hi; j++)
{
if (c[j].type != MOJOSHADER_UNIFORM_FLOAT)
continue;

const int idx = c[j].index;
if ( (idx >= index) && (idx < (index + size)) )
{
memcpy(ptr, &ctx->vs_reg_file_f[idx * 4], 16); // !!! FIXME: 16
memcpy(&ctx->vs_reg_file_f[idx * 4], &c->value.f, 16); // !!! FIXME: 16
ptr += 4;
} // if
} // for

ctx->glUniform4fv(location, size, &ctx->vs_reg_file_f[index * 4]);

ptr = ctx->bound_program->constants;
for (j = 0; j < hi; j++)
{
if (c[j].type != MOJOSHADER_UNIFORM_FLOAT)
continue;

const int idx = c[j].index;
if ( (idx >= index) && (idx < (index + size)) )
{
memcpy(&ctx->vs_reg_file_f[idx * 4], ptr, 16); // !!! FIXME: 16
ptr += 4;
} // if
} // for
} // if

else if (shader_type == MOJOSHADER_TYPE_PIXEL)
{
const MOJOSHADER_constant *c = ctx->bound_program->fragment->parseData->constants;
int hi = ctx->bound_program->fragment->parseData->constant_count;

int j;
GLfloat *ptr = ctx->bound_program->constants;

for (j = 0; j < hi; j++)
{
if (c[j].type != MOJOSHADER_UNIFORM_FLOAT)
continue;

const int idx = c[j].index;
if ( (idx >= index) && (idx < (index + size)) )
{
memcpy(ptr, &ctx->ps_reg_file_f[idx * 4], 16); // !!! FIXME: 16
memcpy(&ctx->ps_reg_file_f[idx * 4], &c->value.f, 16); // !!! FIXME: 16
ptr += 4;
} // if
} // for

ctx->glUniform4fv(location, size, &ctx->ps_reg_file_f[index * 4]);

ptr = ctx->bound_program->constants;
for (j = 0; j < hi; j++)
{
if (c[j].type != MOJOSHADER_UNIFORM_FLOAT)
continue;

const int idx = c[j].index;
if ( (idx >= index) && (idx < (index + size)) )
{
memcpy(&ctx->ps_reg_file_f[idx * 4], ptr, 16); // !!! FIXME: 16
ptr += 4;
} // if
} // for
} // else if
} // if

else if (shader_type == MOJOSHADER_TYPE_VERTEX)
{
if (type == MOJOSHADER_UNIFORM_FLOAT)
ctx->glUniform4fv(location, 1, &ctx->vs_reg_file_f[index * 4]);
Expand Down

0 comments on commit 3e0f4aa

Please sign in to comment.