-
-#include "glheader.h"
-#include "context.h"
-#include "hash.h"
-#include "imports.h"
-#include "macros.h"
-#include "mtypes.h"
-#include "nvfragparse.h"
-#include "nvfragprog.h"
-#include "nvvertexec.h"
-#include "nvvertparse.h"
-#include "nvvertprog.h"
-#include "nvprogram.h"
-
-
-/**
- * Set the vertex/fragment program error state (position and error string).
- * This is generally called from within the parsers.
- */
-void
-_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
-{
- ctx->Program.ErrorPos = pos;
- _mesa_free((void *) ctx->Program.ErrorString);
- if (!string)
- string = "";
- ctx->Program.ErrorString = _mesa_strdup(string);
-}
-
-
-/**
- * Find the line number and column for 'pos' within 'string'.
- * Return a copy of the line which contains 'pos'. Free the line with
- * _mesa_free().
- * \param string the program string
- * \param pos the position within the string
- * \param line returns the line number corresponding to 'pos'.
- * \param col returns the column number corresponding to 'pos'.
- * \return copy of the line containing 'pos'.
- */
-const GLubyte *
-_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
- GLint *line, GLint *col)
-{
- const GLubyte *lineStart = string;
- const GLubyte *p = string;
- GLubyte *s;
- int len;
-
- *line = 1;
-
- while (p != pos) {
- if (*p == (GLubyte) '\n') {
- (*line)++;
- lineStart = p + 1;
- }
- p++;
- }
-
- *col = (pos - lineStart) + 1;
-
- /* return copy of this line */
- while (*p != 0 && *p != '\n')
- p++;
- len = p - lineStart;
- s = (GLubyte *) _mesa_malloc(len + 1);
- _mesa_memcpy(s, lineStart, len);
- s[len] = 0;
-
- return s;
-}
-
-
-
-/**
- * Allocate and initialize a new fragment/vertex program object
- * \param ctx context
- * \param id program id/number
- * \param target program target/type
- * \return pointer to new program object
- */
-struct program *
-_mesa_alloc_program(GLcontext *ctx, GLenum target, GLuint id)
-{
- struct program *prog;
-
- if (target == GL_VERTEX_PROGRAM_NV
- || target == GL_VERTEX_PROGRAM_ARB) {
- struct vertex_program *vprog = CALLOC_STRUCT(vertex_program);
- if (!vprog) {
- return NULL;
- }
- prog = &(vprog->Base);
- }
- else if (target == GL_FRAGMENT_PROGRAM_NV
- || target == GL_FRAGMENT_PROGRAM_ARB) {
- struct fragment_program *fprog = CALLOC_STRUCT(fragment_program);
- if (!fprog) {
- return NULL;
- }
- prog = &(fprog->Base);
- }
- else {
- _mesa_problem(ctx, "bad target in _mesa_alloc_program");
- return NULL;
- }
- prog->Id = id;
- prog->Target = target;
- prog->Resident = GL_TRUE;
- prog->RefCount = 1;
- return prog;
-}
-
-
-/**
- * Delete a program and remove it from the hash table, ignoring the
- * reference count.
- * \note Called from the GL API dispatcher.
- */
-void
-_mesa_delete_program(GLcontext *ctx, struct program *prog)
-{
- ASSERT(prog);
-
- if (prog->String)
- _mesa_free(prog->String);
- if (prog->Target == GL_VERTEX_PROGRAM_NV ||
- prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
- struct vertex_program *vprog = (struct vertex_program *) prog;
- if (vprog->Instructions)
- _mesa_free(vprog->Instructions);
- }
- else if (prog->Target == GL_FRAGMENT_PROGRAM_NV) {
- struct fragment_program *fprog = (struct fragment_program *) prog;
- if (fprog->Instructions)
- _mesa_free(fprog->Instructions);
- if (fprog->Parameters) {
- GLuint i;
- for (i = 0; i < fprog->NumParameters; i++) {
- _mesa_free((void *) fprog->Parameters[i].Name);
- }
- _mesa_free(fprog->Parameters);
- }
- }
- _mesa_free(prog);
-}
-
-
-/**
- * Bind a program (make it current)
- * \note Called from the GL API dispatcher by both glBindProgramNV
- * and glBindProgramARB.
- */
-void
-_mesa_BindProgramNV(GLenum target, GLuint id)
-{
- struct program *prog;
- GET_CURRENT_CONTEXT(ctx);
- ASSERT_OUTSIDE_BEGIN_END(ctx);
-
- if ((target == GL_VERTEX_PROGRAM_NV
- && ctx->Extensions.NV_vertex_program) ||
- (target == GL_VERTEX_PROGRAM_ARB
- && ctx->Extensions.ARB_vertex_program)) {
- if (ctx->VertexProgram.Current &&
- ctx->VertexProgram.Current->Base.Id == id)
- return;
- /* decrement refcount on previously bound vertex program */
- if (ctx->VertexProgram.Current) {
- ctx->VertexProgram.Current->Base.RefCount--;
- /* and delete if refcount goes below one */
- if (ctx->VertexProgram.Current->Base.RefCount <= 0) {
- _mesa_delete_program(ctx, &(ctx->VertexProgram.Current->Base));
- _mesa_HashRemove(ctx->Shared->Programs, id);
- }
- }
- }
- else if ((target == GL_FRAGMENT_PROGRAM_NV
- && ctx->Extensions.NV_fragment_program) ||
- (target == GL_FRAGMENT_PROGRAM_ARB
- && ctx->Extensions.ARB_fragment_program)) {
- if (ctx->FragmentProgram.Current &&
- ctx->FragmentProgram.Current->Base.Id == id)
- return;
- /* decrement refcount on previously bound fragment program */
- if (ctx->FragmentProgram.Current) {
- ctx->FragmentProgram.Current->Base.RefCount--;
- /* and delete if refcount goes below one */
- if (ctx->FragmentProgram.Current->Base.RefCount <= 0) {
- _mesa_delete_program(ctx, &(ctx->FragmentProgram.Current->Base));
- _mesa_HashRemove(ctx->Shared->Programs, id);
- }
- }
- }
- else {
- _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
- return;
- }
-
- /* NOTE: binding to a non-existant program is not an error.
- * That's supposed to be caught in glBegin.
- */
- if (id == 0) {
- /* default program */
- prog = NULL;
- if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB)
- prog = ctx->Shared->DefaultVertexProgram;
- else
- prog = ctx->Shared->DefaultFragmentProgram;
- }
- else {
- prog = (struct program *) _mesa_HashLookup(ctx->Shared->Programs, id);
- if (prog) {
- if (prog->Target == 0) {
- /* prog was allocated with glGenProgramsNV */
- prog->Target = target;
- }
- else if (prog->Target != target) {
- _mesa_error(ctx, GL_INVALID_OPERATION,
- "glBindProgramNV/ARB(target mismatch)");
- return;
- }
- }
- else {
- /* allocate a new program now */
- prog = _mesa_alloc_program(ctx, target, id);
- if (!prog) {
- _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
- return;
- }
- prog->Id = id;
- prog->Target = target;
- prog->Resident = GL_TRUE;
- prog->RefCount = 1;
- _mesa_HashInsert(ctx->Shared->Programs, id, prog);
- }
- }
-
- /* bind now */
- if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB) {
- ctx->VertexProgram.Current = (struct vertex_program *) prog;
- }
- else if (target == GL_FRAGMENT_PROGRAM_NV || target == GL_FRAGMENT_PROGRAM_ARB) {
- ctx->FragmentProgram.Current = (struct fragment_program *) prog;
- }
-
- if (prog)
- prog->RefCount++;
-}
-
-
-/**
- * Delete a list of programs.
- * \note Not compiled into display lists.
- * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB.