Skip to content

Commit

Permalink
Cleanups for building as C++ code.
Browse files Browse the repository at this point in the history
--HG--
branch : trunk
  • Loading branch information
icculus committed Apr 29, 2008
1 parent 6f83593 commit cce0ae4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
13 changes: 7 additions & 6 deletions mojoshader.c
Expand Up @@ -1476,7 +1476,7 @@ static void emit_PASSTHROUGH_start(Context *ctx)
{
// just copy the whole token stream and make all other emitters no-ops.
ctx->output_len = (ctx->tokencount * sizeof (uint32));
ctx->output_bytes = Malloc(ctx, ctx->output_len);
ctx->output_bytes = (uint8 *) Malloc(ctx, ctx->output_len);
if (ctx->output_bytes != NULL)
memcpy(ctx->output_bytes, ctx->tokens, ctx->output_len);
} // emit_PASSTHROUGH_start
Expand Down Expand Up @@ -2337,7 +2337,7 @@ static void emit_GLSL_LIT_helper(Context *ctx)
if (ctx->flags & CTX_FLAGS_GLSL_LIT_OPCODE)
return;

ctx->flags |= CTX_FLAGS_GLSL_LIT_OPCODE;
ctx->flags = (ContextFlags) (ctx->flags | CTX_FLAGS_GLSL_LIT_OPCODE);

push_output(ctx, &ctx->helpers);
output_line(ctx, "const vec4 LIT(const vec4 src)");
Expand Down Expand Up @@ -4347,7 +4347,7 @@ static Context *build_context(const char *profile,
if (m == NULL) m = internal_malloc;
if (f == NULL) f = internal_free;

Context *ctx = m(sizeof (Context), d);
Context *ctx = (Context *) m(sizeof (Context), d);
if (ctx == NULL)
return NULL;

Expand Down Expand Up @@ -4654,10 +4654,11 @@ static MOJOSHADER_parseData *build_parsedata(Context *ctx)
MOJOSHADER_uniform *uniforms = NULL;
MOJOSHADER_attribute *attributes = NULL;
MOJOSHADER_sampler *samplers = NULL;
MOJOSHADER_parseData *retval;
MOJOSHADER_parseData *retval = NULL;
int attribute_count = 0;

if ((retval = Malloc(ctx, sizeof (MOJOSHADER_parseData))) == NULL)
retval = (MOJOSHADER_parseData*) Malloc(ctx, sizeof(MOJOSHADER_parseData));
if (retval == NULL)
return &out_of_mem_data;

memset(retval, '\0', sizeof (MOJOSHADER_parseData));
Expand Down Expand Up @@ -4754,7 +4755,7 @@ static void process_definitions(Context *ctx)
// Apparently this is an attribute that wasn't DCL'd.
// Add it to the attribute list; deal with it later.
add_attribute_register(ctx, item->regtype, item->regnum,
0, 0, 0xF);
MOJOSHADER_USAGE_UNKNOWN, 0, 0xF);
break;

case REG_TYPE_ADDRESS:
Expand Down
39 changes: 22 additions & 17 deletions mojoshader_opengl.c
Expand Up @@ -61,7 +61,7 @@ struct MOJOSHADER_glProgram
};

// Entry points in base OpenGL that lack function pointer prototypes...
typedef WINGDIAPI const GLubyte * (APIENTRYP PFNGLGETSTRINGPROC) (GLenum name);
typedef const GLubyte * (APIENTRYP PFNGLGETSTRINGPROC) (GLenum name);

struct MOJOSHADER_glContext
{
Expand All @@ -72,12 +72,12 @@ struct MOJOSHADER_glContext

// The constant register files...
// Man, it kills me how much memory this takes...
float vs_reg_file_f[8192 * 4];
int vs_reg_file_i[2047 * 4];
uint8 vs_reg_file_b[2047];
float ps_reg_file_f[8192 * 4];
int ps_reg_file_i[2047 * 4];
uint8 ps_reg_file_b[2047];
GLfloat vs_reg_file_f[8192 * 4];
GLint vs_reg_file_i[2047 * 4];
GLint vs_reg_file_b[2047];
GLfloat ps_reg_file_f[8192 * 4];
GLint ps_reg_file_i[2047 * 4];
GLint ps_reg_file_b[2047];

// GL stuff...
int opengl_major;
Expand Down Expand Up @@ -302,6 +302,7 @@ MOJOSHADER_glContext *MOJOSHADER_glCreateContext(const char *_profile,
MOJOSHADER_malloc m, MOJOSHADER_free f,
void *d)
{
MOJOSHADER_glContext *retval = NULL;
MOJOSHADER_glContext *current_ctx = ctx;
ctx = NULL;

Expand All @@ -315,8 +316,6 @@ MOJOSHADER_glContext *MOJOSHADER_glCreateContext(const char *_profile,
goto init_fail;
} // if

MOJOSHADER_glContext *retval = ctx;

memset(ctx, '\0', sizeof (MOJOSHADER_glContext));
ctx->malloc_fn = m;
ctx->free_fn = f;
Expand All @@ -335,6 +334,7 @@ MOJOSHADER_glContext *MOJOSHADER_glCreateContext(const char *_profile,

MOJOSHADER_glBindProgram(NULL);

retval = ctx;
ctx = current_ctx;
return retval;

Expand All @@ -361,6 +361,10 @@ MOJOSHADER_glShader *MOJOSHADER_glCompileShader(const unsigned char *tokenbuf,
bufsize, ctx->malloc_fn,
ctx->free_fn,
ctx->malloc_data);
GLint ok = 0;
const GLenum shader_type = (pd->shader_type == MOJOSHADER_TYPE_PIXEL) ? GL_FRAGMENT_SHADER : GL_VERTEX_SHADER;
GLint shaderlen = (GLint) pd->output_len;

if (pd->error != NULL)
{
set_error(pd->error);
Expand All @@ -371,9 +375,6 @@ MOJOSHADER_glShader *MOJOSHADER_glCompileShader(const unsigned char *tokenbuf,
if (retval == NULL)
goto compile_shader_fail;

GLint ok = 0;
const GLenum shader_type = (pd->shader_type == MOJOSHADER_TYPE_PIXEL) ? GL_FRAGMENT_SHADER : GL_VERTEX_SHADER;
GLint shaderlen = (GLint) pd->output_len;
shader = ctx->glCreateShaderObject(shader_type);

ctx->glShaderSource(shader, 1, (const GLchar **) &pd->output, &shaderlen);
Expand Down Expand Up @@ -496,6 +497,7 @@ MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,

MOJOSHADER_glProgram *retval = NULL;
const GLhandleARB program = ctx->glCreateProgramObject();
int numregs = 0;

if (vshader != NULL) ctx->glAttachObject(program, vshader->handle);
if (pshader != NULL) ctx->glAttachObject(program, pshader->handle);
Expand All @@ -516,7 +518,6 @@ MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,
goto link_program_fail;
memset(retval, '\0', sizeof (MOJOSHADER_glProgram));

int numregs = 0;
if (vshader != NULL) numregs += vshader->parseData->uniform_count;
if (pshader != NULL) numregs += pshader->parseData->uniform_count;
retval->uniforms = (UniformMap *) Malloc(sizeof (UniformMap) * numregs);
Expand Down Expand Up @@ -607,6 +608,7 @@ void MOJOSHADER_glSetVertexShaderUniformF(unsigned int idx, const float *data,
const uint maxregs = STATICARRAYLEN(ctx->vs_reg_file_f) / 4;
if (idx < maxregs)
{
assert(sizeof (GLfloat) == sizeof (float));
const uint cpy = (maxuint(maxregs - idx, vec4n) * sizeof (*data)) * 4;
memcpy(ctx->vs_reg_file_f + (idx * 4), data, cpy);
} // if
Expand All @@ -619,6 +621,7 @@ void MOJOSHADER_glSetVertexShaderUniformI(unsigned int idx, const int *data,
const uint maxregs = STATICARRAYLEN(ctx->vs_reg_file_i) / 4;
if (idx < maxregs)
{
assert(sizeof (GLint) == sizeof (int));
const uint cpy = (maxuint(maxregs - idx, ivec4n) * sizeof (*data)) * 4;
memcpy(ctx->vs_reg_file_i + (idx * 4), data, cpy);
} // if
Expand All @@ -631,8 +634,8 @@ void MOJOSHADER_glSetVertexShaderUniformB(unsigned int idx, const int *data,
const uint maxregs = STATICARRAYLEN(ctx->vs_reg_file_f) / 4;
if (idx < maxregs)
{
uint8 *wptr = ctx->vs_reg_file_b + idx;
uint8 *endptr = wptr + maxuint(maxregs - idx, bcount);
GLint *wptr = ctx->vs_reg_file_b + idx;
GLint *endptr = wptr + maxuint(maxregs - idx, bcount);
while (wptr != endptr)
*(wptr++) = *(data++) ? 1 : 0;
} // if
Expand All @@ -645,6 +648,7 @@ void MOJOSHADER_glSetPixelShaderUniformF(unsigned int idx, const float *data,
const uint maxregs = STATICARRAYLEN(ctx->ps_reg_file_f) / 4;
if (idx < maxregs)
{
assert(sizeof (GLfloat) == sizeof (float));
const uint cpy = (maxuint(maxregs - idx, vec4n) * sizeof (*data)) * 4;
memcpy(ctx->ps_reg_file_f + (idx * 4), data, cpy);
} // if
Expand All @@ -657,6 +661,7 @@ void MOJOSHADER_glSetPixelShaderUniformI(unsigned int idx, const int *data,
const uint maxregs = STATICARRAYLEN(ctx->ps_reg_file_i) / 4;
if (idx < maxregs)
{
assert(sizeof (GLint) == sizeof (int));
const uint cpy = (maxuint(maxregs - idx, ivec4n) * sizeof (*data)) * 4;
memcpy(ctx->ps_reg_file_i + (idx * 4), data, cpy);
} // if
Expand All @@ -669,8 +674,8 @@ void MOJOSHADER_glSetPixelShaderUniformB(unsigned int idx, const int *data,
const uint maxregs = STATICARRAYLEN(ctx->ps_reg_file_f) / 4;
if (idx < maxregs)
{
uint8 *wptr = ctx->ps_reg_file_b + idx;
uint8 *endptr = wptr + maxuint(maxregs - idx, bcount);
GLint *wptr = ctx->ps_reg_file_b + idx;
GLint *endptr = wptr + maxuint(maxregs - idx, bcount);
while (wptr != endptr)
*(wptr++) = *(data++) ? 1 : 0;
} // if
Expand Down

0 comments on commit cce0ae4

Please sign in to comment.