}
if (!prog->LocalParams) {
- prog->LocalParams = calloc(maxParams, sizeof(float[4]));
+ prog->LocalParams = rzalloc_array_size(prog, sizeof(float[4]),
+ maxParams);
+
if (!prog->LocalParams)
return GL_FALSE;
}
/* double the size */
p->max_inst *= 2;
- newInst = _mesa_alloc_instructions(p->max_inst);
+ newInst =
+ rzalloc_array(p->program, struct prog_instruction, p->max_inst);
if (!newInst) {
_mesa_error(NULL, GL_OUT_OF_MEMORY, "vertex program build");
return;
_mesa_copy_instructions(newInst, p->program->Instructions,
p->program->NumInstructions);
- _mesa_free_instructions(p->program->Instructions,
- p->program->NumInstructions);
+ ralloc_free(p->program->Instructions);
p->program->Instructions = newInst;
}
* If we need more, we'll grow the instruction array as needed.
*/
p.max_inst = 32;
- p.program->Instructions = _mesa_alloc_instructions(p.max_inst);
+ p.program->Instructions = rzalloc_array(program, struct prog_instruction,
+ p.max_inst);
p.program->String = NULL;
p.program->NumInstructions =
p.program->NumTemporaries =
memset(&prog, 0, sizeof(prog));
memset(&state, 0, sizeof(state));
state.prog = &prog;
+ state.mem_ctx = program;
if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len,
&state)) {
+ ralloc_free(prog.Instructions);
+ ralloc_free(prog.String);
_mesa_error(ctx, GL_INVALID_OPERATION, "glProgramString(bad program)");
return;
}
if ((ctx->_Shader->Flags & GLSL_NO_OPT) == 0)
- _mesa_optimize_program(ctx, &prog);
+ _mesa_optimize_program(ctx, &prog, program);
- free(program->String);
+ ralloc_free(program->String);
/* Copy the relevant contents of the arb_program struct into the
* vertex_program struct.
program->IsPositionInvariant = (state.option.PositionInvariant)
? GL_TRUE : GL_FALSE;
- free(program->Instructions);
+ ralloc_free(program->Instructions);
program->Instructions = prog.Instructions;
if (program->Parameters)
_mesa_reference_program(ctx, &shader->Program, prog);
if ((ctx->_Shader->Flags & GLSL_NO_OPT) == 0) {
- _mesa_optimize_program(ctx, prog);
+ _mesa_optimize_program(ctx, prog, prog);
}
/* This has to be done last. Any operation that can cause
}
-/**
- * Allocate an array of program instructions.
- * \param numInst number of instructions
- * \return pointer to instruction memory
- */
-struct prog_instruction *
-_mesa_alloc_instructions(GLuint numInst)
-{
- return
- calloc(numInst, sizeof(struct prog_instruction));
-}
-
-
/**
* Copy an array of program instructions.
* \param dest pointer to destination.
}
-/**
- * Free an array of instructions
- */
-void
-_mesa_free_instructions(struct prog_instruction *inst, GLuint count)
-{
- free(inst);
-}
-
-
/**
* Basic info about each instruction
*/
extern "C" {
#endif
+struct gl_program;
+
extern void
_mesa_init_instructions(struct prog_instruction *inst, GLuint count);
-extern struct prog_instruction *
-_mesa_alloc_instructions(GLuint numInst);
-
extern struct prog_instruction *
_mesa_copy_instructions(struct prog_instruction *dest,
const struct prog_instruction *src, GLuint n);
-extern void
-_mesa_free_instructions(struct prog_instruction *inst, GLuint count);
-
extern GLuint
_mesa_num_inst_src_regs(enum prog_opcode opcode);
* \return number of instructions removed
*/
static GLuint
-remove_instructions(struct gl_program *prog, const GLboolean *removeFlags)
+remove_instructions(struct gl_program *prog, const GLboolean *removeFlags,
+ void *mem_ctx)
{
GLint i, removeEnd = 0, removeCount = 0;
GLuint totalRemoved = 0;
*/
if (removeCount > 0) {
GLint removeStart = removeEnd - removeCount + 1;
- _mesa_delete_instructions(prog, removeStart, removeCount);
+ _mesa_delete_instructions(prog, removeStart, removeCount, mem_ctx);
removeStart = removeCount = 0; /* reset removal info */
}
}
/* Finish removing if the first instruction was to be removed. */
if (removeCount > 0) {
GLint removeStart = removeEnd - removeCount + 1;
- _mesa_delete_instructions(prog, removeStart, removeCount);
+ _mesa_delete_instructions(prog, removeStart, removeCount, mem_ctx);
}
return totalRemoved;
}
* write to such registers. Be careful with condition code setters.
*/
static GLboolean
-_mesa_remove_dead_code_global(struct gl_program *prog)
+_mesa_remove_dead_code_global(struct gl_program *prog, void *mem_ctx)
{
GLboolean tempRead[REG_ALLOCATE_MAX_PROGRAM_TEMPS][4];
GLboolean *removeInst; /* per-instruction removal flag */
}
/* now remove the instructions which aren't needed */
- rem = remove_instructions(prog, removeInst);
+ rem = remove_instructions(prog, removeInst, mem_ctx);
if (dbg) {
printf("Optimize: End dead code removal.\n");
* with a proper control flow graph
*/
static GLboolean
-_mesa_remove_dead_code_local(struct gl_program *prog)
+_mesa_remove_dead_code_local(struct gl_program *prog, void *mem_ctx)
{
GLboolean *removeInst;
GLuint i, arg, rem = 0;
removeInst[i] = GL_TRUE;
}
- rem = remove_instructions(prog, removeInst);
+ rem = remove_instructions(prog, removeInst, mem_ctx);
done:
free(removeInst);
* Try to remove extraneous MOV instructions from the given program.
*/
static GLboolean
-_mesa_remove_extra_moves(struct gl_program *prog)
+_mesa_remove_extra_moves(struct gl_program *prog, void *mem_ctx)
{
GLboolean *removeInst; /* per-instruction removal flag */
GLuint i, rem = 0, nesting = 0;
}
/* now remove the instructions which aren't needed */
- rem = remove_instructions(prog, removeInst);
+ rem = remove_instructions(prog, removeInst, mem_ctx);
free(removeInst);
* instructions, temp regs, etc.
*/
void
-_mesa_optimize_program(struct gl_context *ctx, struct gl_program *program)
+_mesa_optimize_program(struct gl_context *ctx, struct gl_program *program,
+ void *mem_ctx)
{
GLboolean any_change;
do {
any_change = GL_FALSE;
_mesa_remove_extra_move_use(program);
- if (_mesa_remove_dead_code_global(program))
+ if (_mesa_remove_dead_code_global(program, mem_ctx))
any_change = GL_TRUE;
- if (_mesa_remove_extra_moves(program))
+ if (_mesa_remove_extra_moves(program, mem_ctx))
any_change = GL_TRUE;
- if (_mesa_remove_dead_code_local(program))
+ if (_mesa_remove_dead_code_local(program, mem_ctx))
any_change = GL_TRUE;
any_change = _mesa_constant_fold(program) || any_change;
GLint intEnd[MAX_PROGRAM_TEMPS]);
extern void
-_mesa_optimize_program(struct gl_context *ctx, struct gl_program *program);
+_mesa_optimize_program(struct gl_context *ctx, struct gl_program *program,
+ void *mem_ctx);
extern GLboolean
_mesa_constant_fold(struct gl_program *prog);
case STATE_LOCAL:
if (!ctx->FragmentProgram.Current->LocalParams) {
ctx->FragmentProgram.Current->LocalParams =
- calloc(MAX_PROGRAM_LOCAL_PARAMS, sizeof(float[4]));
+ rzalloc_array_size(ctx->FragmentProgram.Current,
+ sizeof(float[4]),
+ MAX_PROGRAM_LOCAL_PARAMS);
if (!ctx->FragmentProgram.Current->LocalParams)
return;
}
case STATE_LOCAL:
if (!ctx->VertexProgram.Current->LocalParams) {
ctx->VertexProgram.Current->LocalParams =
- calloc(MAX_PROGRAM_LOCAL_PARAMS, sizeof(float[4]));
+ rzalloc_array_size(ctx->VertexProgram.Current,
+ sizeof(float[4]),
+ MAX_PROGRAM_LOCAL_PARAMS);
if (!ctx->VertexProgram.Current->LocalParams)
return;
}
if (prog == &_mesa_DummyProgram)
return;
- free(prog->String);
- free(prog->LocalParams);
-
- if (prog->Instructions) {
- _mesa_free_instructions(prog->Instructions, prog->NumInstructions);
- }
if (prog->Parameters) {
_mesa_free_parameter_list(prog->Parameters);
}
}
/* Alloc storage for new instructions */
- newInst = _mesa_alloc_instructions(newLen);
+ newInst = rzalloc_array(prog, struct prog_instruction, newLen);
if (!newInst) {
return GL_FALSE;
}
origLen - start);
/* free old instructions */
- _mesa_free_instructions(prog->Instructions, origLen);
+ ralloc_free(prog->Instructions);
/* install new instructions */
prog->Instructions = newInst;
* Adjust branch targets accordingly.
*/
GLboolean
-_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count)
+_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count,
+ void *mem_ctx)
{
const GLuint origLen = prog->NumInstructions;
const GLuint newLen = origLen - count;
}
/* Alloc storage for new instructions */
- newInst = _mesa_alloc_instructions(newLen);
+ newInst = rzalloc_array(mem_ctx, struct prog_instruction, newLen);
if (!newInst) {
return GL_FALSE;
}
newLen - start);
/* free old instructions */
- _mesa_free_instructions(prog->Instructions, origLen);
+ ralloc_free(prog->Instructions);
/* install new instructions */
prog->Instructions = newInst;
_mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count);
extern GLboolean
-_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count);
+_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count,
+ void *mem_ctx);
extern void
_mesa_find_used_registers(const struct gl_program *prog,
/* Make a copy of the program string and force it to be NUL-terminated.
*/
- strz = (GLubyte *) malloc(len + 1);
+ strz = (GLubyte *) ralloc_size(state->mem_ctx, len + 1);
if (strz == NULL) {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
return GL_FALSE;
/* Add one instruction to store the "END" instruction.
*/
state->prog->Instructions =
- _mesa_alloc_instructions(state->prog->NumInstructions + 1);
+ rzalloc_array(state->mem_ctx, struct prog_instruction,
+ state->prog->NumInstructions + 1);
if (state->prog->Instructions == NULL) {
goto error;
struct gl_context *ctx;
struct gl_program *prog;
+ /** Memory context to attach instructions to. */
+ void *mem_ctx;
+
/**
* Per-program target limits
*/
}
/* Alloc storage for new instructions */
- newInst = _mesa_alloc_instructions(newLen);
+ newInst = rzalloc_array(vprog, struct prog_instruction, newLen);
if (!newInst) {
_mesa_error(ctx, GL_OUT_OF_MEMORY,
"glProgramString(inserting position_invariant code)");
_mesa_copy_instructions (newInst + 4, vprog->Instructions, origLen);
/* free old instructions */
- _mesa_free_instructions(vprog->Instructions, origLen);
+ ralloc_free(vprog->Instructions);
/* install new instructions */
vprog->Instructions = newInst;
}
/* Alloc storage for new instructions */
- newInst = _mesa_alloc_instructions(newLen);
+ newInst = rzalloc_array(vprog, struct prog_instruction, newLen);
if (!newInst) {
_mesa_error(ctx, GL_OUT_OF_MEMORY,
"glProgramString(inserting position_invariant code)");
_mesa_copy_instructions (newInst + 4, vprog->Instructions, origLen);
/* free old instructions */
- _mesa_free_instructions(vprog->Instructions, origLen);
+ ralloc_free(vprog->Instructions);
/* install new instructions */
vprog->Instructions = newInst;
}
/* Alloc storage for new instructions */
- newInst = _mesa_alloc_instructions(newLen);
+ newInst = rzalloc_array(fprog, struct prog_instruction, newLen);
if (!newInst) {
_mesa_error(ctx, GL_OUT_OF_MEMORY,
"glProgramString(inserting fog_option code)");
inst++;
/* free old instructions */
- _mesa_free_instructions(fprog->Instructions, origLen);
+ ralloc_free(fprog->Instructions);
/* install new instructions */
fprog->Instructions = newInst;