-
-#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;
-}
-
-
-
-
-/**
- * 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, GLuint id)
-{
- struct program *prog = (struct program *)
- _mesa_HashLookup(ctx->Shared->Programs, id);
-
- if (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_HashRemove(ctx->Shared->Programs, id);
- _mesa_free(prog);
- }
-}
-
-
-/**
- * Bind a program (make it current)
- * \note Called from the GL API dispatcher.
- */
-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) {
- 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.Id);
- }
- }
- else if (target == GL_FRAGMENT_PROGRAM_NV) {
- 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.Id);
- }
- }
- else {
- _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV");
- return;
- }
-
- /* NOTE: binding to a non-existant program is not an error.
- * That's supposed to be caught in glBegin.
- */
- if (id == 0) {
- /* OK, the null program object */
- prog = NULL;
- }
- else {
- prog = (struct program *) _mesa_HashLookup(ctx->Shared->Programs, id);
-
- if (!prog && id > 0){
- /* allocate new program */
- if (target == GL_VERTEX_PROGRAM_NV) {
- struct vertex_program *vprog = CALLOC_STRUCT(vertex_program);
- if (!vprog) {
- _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV");
- return;
- }
- prog = &(vprog->Base);
- }
- else if (target == GL_FRAGMENT_PROGRAM_NV) {
- struct fragment_program *fprog = CALLOC_STRUCT(fragment_program);
- if (!fprog) {
- _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV");
- return;
- }
- prog = &(fprog->Base);
- }
- else {
- _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV(target)");
- 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) {
- ctx->VertexProgram.Current = (struct vertex_program *) prog;
- }
- else if (target == GL_FRAGMENT_PROGRAM_NV) {
- ctx->FragmentProgram.Current = (struct fragment_program *) prog;
- }
-
- if (prog)
- prog->RefCount++;
-}
-
-
-/**
- * Delete a list of programs.
- * \note Not compiled into display lists.
- * \note Called from the GL API dispatcher.