⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.c

📁 winNT技术操作系统,国外开放的原代码和LIUX一样
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
 * Mesa 3-D graphics library
 * Version:  6.2
 *
 * Copyright (C) 1999-2004  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * \file program.c
 * Vertex and fragment program support functions.
 * \author Brian Paul
 */


#include "glheader.h"
#include "context.h"
#include "hash.h"
#include "imports.h"
#include "macros.h"
#include "mtypes.h"
#include "program.h"
#include "nvfragparse.h"
#include "nvfragprog.h"
#include "nvvertparse.h"
#include "nvvertprog.h"


/**********************************************************************/
/* Utility functions                                                  */
/**********************************************************************/


/* A pointer to this dummy program is put into the hash table when
 * glGenPrograms is called.
 */
struct program _mesa_DummyProgram;


/**
 * Init context's vertex/fragment program state
 */
void
_mesa_init_program(GLcontext *ctx)
{
   GLuint i;

   ctx->Program.ErrorPos = -1;
   ctx->Program.ErrorString = _mesa_strdup("");

#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
   ctx->VertexProgram.Enabled = GL_FALSE;
   ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
   ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
   ctx->VertexProgram.Current = (struct vertex_program *) ctx->Shared->DefaultVertexProgram;
   assert(ctx->VertexProgram.Current);
   ctx->VertexProgram.Current->Base.RefCount++;
   for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
      ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
      ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
   }
#endif

#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
   ctx->FragmentProgram.Enabled = GL_FALSE;
   ctx->FragmentProgram.Current = (struct fragment_program *) ctx->Shared->DefaultFragmentProgram;
   assert(ctx->FragmentProgram.Current);
   ctx->FragmentProgram.Current->Base.RefCount++;
#endif

#if FEATURE_ATI_fragment_shader
   ctx->ATIFragmentShader.Enabled = GL_FALSE;
   ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
   assert(ctx->ATIFragmentShader.Current);
   ctx->ATIFragmentShader.Current->Base.RefCount++;
#endif
}


/**
 * Free a context's vertex/fragment program state
 */
void
_mesa_free_program_data(GLcontext *ctx)
{
#if FEATURE_NV_vertex_program
   if (ctx->VertexProgram.Current) {
      ctx->VertexProgram.Current->Base.RefCount--;
      if (ctx->VertexProgram.Current->Base.RefCount <= 0)
         ctx->Driver.DeleteProgram(ctx, &(ctx->VertexProgram.Current->Base));
   }
#endif
#if FEATURE_NV_fragment_program
   if (ctx->FragmentProgram.Current) {
      ctx->FragmentProgram.Current->Base.RefCount--;
      if (ctx->FragmentProgram.Current->Base.RefCount <= 0)
         ctx->Driver.DeleteProgram(ctx, &(ctx->FragmentProgram.Current->Base));
   }
#endif
#if FEATURE_ATI_fragment_shader
   if (ctx->ATIFragmentShader.Current) {
      ctx->ATIFragmentShader.Current->Base.RefCount--;
      if (ctx->ATIFragmentShader.Current->Base.RefCount <= 0)
	ctx->Driver.DeleteProgram(ctx, &(ctx->ATIFragmentShader.Current->Base));
   }
#endif
   _mesa_free((void *) ctx->Program.ErrorString);
}




/**
 * 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;
}


/**
 * Initialize a new vertex/fragment program object.
 */
static struct program *
_mesa_init_program_struct( GLcontext *ctx, struct program *prog,
                           GLenum target, GLuint id)
{
   (void) ctx;
   if (prog) {
      prog->Id = id;
      prog->Target = target;
      prog->Resident = GL_TRUE;
      prog->RefCount = 1;
   }

   return prog;
}


/**
 * Initialize a new fragment program object.
 */
struct program *
_mesa_init_fragment_program( GLcontext *ctx, struct fragment_program *prog,
                             GLenum target, GLuint id)
{
   if (prog) 
      return _mesa_init_program_struct( ctx, &prog->Base, target, id );
   else
      return NULL;
}


/**
 * Initialize a new vertex program object.
 */
struct program *
_mesa_init_vertex_program( GLcontext *ctx, struct vertex_program *prog,
                           GLenum target, GLuint id)
{
   if (prog) 
      return _mesa_init_program_struct( ctx, &prog->Base, target, id );
   else
      return NULL;
}

/**
 * Initialize a new ATI fragment shader object.
 */
struct program *
_mesa_init_ati_fragment_shader( GLcontext *ctx,
                                struct ati_fragment_shader *prog,
                                GLenum target, GLuint id )
{
   if (prog) 
      return _mesa_init_program_struct( ctx, &prog->Base, target, id );
   else
      return NULL;
}



/**
 * Allocate and initialize a new fragment/vertex program object but
 * don't put it into the program hash table.  Called via
 * ctx->Driver.NewProgram.  May be overridden (ie. replaced) by a
 * device driver function to implement OO deriviation with additional
 * types not understood by this function.
 * 
 * \param ctx  context
 * \param id   program id/number
 * \param target  program target/type
 * \return  pointer to new program object
 */
struct program *
_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
{
   switch (target) {
   case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
      return _mesa_init_vertex_program( ctx, CALLOC_STRUCT(vertex_program),
					target, id );
   case GL_FRAGMENT_PROGRAM_NV:
   case GL_FRAGMENT_PROGRAM_ARB:
      return _mesa_init_fragment_program( ctx, CALLOC_STRUCT(fragment_program),
					  target, id );
   case GL_FRAGMENT_SHADER_ATI:
      return _mesa_init_ati_fragment_shader( ctx, CALLOC_STRUCT(ati_fragment_shader),
					  target, id );

   default:
      _mesa_problem(ctx, "bad target in _mesa_new_program");
      return NULL;
   }
}


/**
 * Delete a program and remove it from the hash table, ignoring the
 * reference count.
 * Called via ctx->Driver.DeleteProgram.  May be wrapped (OO deriviation)
 * by a device driver function.
 */
void
_mesa_delete_program(GLcontext *ctx, struct program *prog)
{
   (void) ctx;
   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) {
         GLuint i;
         for (i = 0; i < vprog->Base.NumInstructions; i++) {
            if (vprog->Instructions[i].Data)
               _mesa_free(vprog->Instructions[i].Data);
         }
         _mesa_free(vprog->Instructions);
      }
      if (vprog->Parameters)
         _mesa_free_parameter_list(vprog->Parameters);
   }
   else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
            prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
      struct fragment_program *fprog = (struct fragment_program *) prog;
      if (fprog->Instructions) {
         GLuint i;
         for (i = 0; i < fprog->Base.NumInstructions; i++) {
            if (fprog->Instructions[i].Data)
               _mesa_free(fprog->Instructions[i].Data);
         }
         _mesa_free(fprog->Instructions);
      }
      if (fprog->Parameters)
         _mesa_free_parameter_list(fprog->Parameters);
   }
   else if (prog->Target == GL_FRAGMENT_SHADER_ATI) {
      struct ati_fragment_shader *atifs = (struct ati_fragment_shader *)prog;
      if (atifs->Instructions)
	 _mesa_free(atifs->Instructions);
   }

   _mesa_free(prog);
}



/**********************************************************************/
/* Program parameter functions                                        */
/**********************************************************************/

struct program_parameter_list *
_mesa_new_parameter_list(void)
{
   return (struct program_parameter_list *)
      _mesa_calloc(sizeof(struct program_parameter_list));
}


/**
 * Free a parameter list and all its parameters
 */
void
_mesa_free_parameter_list(struct program_parameter_list *paramList)
{
   _mesa_free_parameters(paramList);
   _mesa_free(paramList->Parameters);
   if (paramList->ParameterValues)
      ALIGN_FREE(paramList->ParameterValues);
   _mesa_free(paramList);
}


/**
 * Free all the parameters in the given list, but don't free the
 * paramList structure itself.
 */
void
_mesa_free_parameters(struct program_parameter_list *paramList)
{
   GLuint i;
   for (i = 0; i < paramList->NumParameters; i++) {
      if (paramList->Parameters[i].Name)
	 _mesa_free((void *) paramList->Parameters[i].Name);
   }
   paramList->NumParameters = 0;
}


/**
 * Helper function used by the functions below.
 */
static GLint
add_parameter(struct program_parameter_list *paramList,
              const char *name, const GLfloat values[4],
              enum parameter_type type)
{
   const GLuint n = paramList->NumParameters;

   if (n == paramList->Size) {
      GLfloat (*tmp)[4];

      paramList->Size *= 2;
      if (!paramList->Size)
	 paramList->Size = 8;

      paramList->Parameters = (struct program_parameter *)
	 _mesa_realloc(paramList->Parameters,
		       n * sizeof(struct program_parameter),
		       paramList->Size * sizeof(struct program_parameter));

      tmp = paramList->ParameterValues;
      paramList->ParameterValues = ALIGN_MALLOC(paramList->Size * 4 * sizeof(GLfloat), 16);
      if (tmp) {
	 _mesa_memcpy(paramList->ParameterValues, tmp, 
		      n * 4 * sizeof(GLfloat));
	 ALIGN_FREE(tmp);
      }
   }

   if (!paramList->Parameters ||
       !paramList->ParameterValues) {
      /* out of memory */
      paramList->NumParameters = 0;
      paramList->Size = 0;
      return -1;
   }
   else {
      paramList->NumParameters = n + 1;

      _mesa_memset(&paramList->Parameters[n], 0, 
		   sizeof(struct program_parameter));

      paramList->Parameters[n].Name = name ? _mesa_strdup(name) : NULL;
      paramList->Parameters[n].Type = type;
      if (values)
         COPY_4V(paramList->ParameterValues[n], values);
      return (GLint) n;
   }
}


/**
 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
 * \return index of the new entry in the parameter list
 */
GLint
_mesa_add_named_parameter(struct program_parameter_list *paramList,
                          const char *name, const GLfloat values[4])
{
   return add_parameter(paramList, name, values, NAMED_PARAMETER);
}


/**
 * Add a new unnamed constant to the parameter list.
 * \param paramList - the parameter list
 * \param values - four float values
 * \return index of the new parameter.
 */
GLint
_mesa_add_named_constant(struct program_parameter_list *paramList,
                         const char *name, const GLfloat values[4])
{
   return add_parameter(paramList, name, values, CONSTANT);
}


/**
 * Add a new unnamed constant to the parameter list.
 * \param paramList - the parameter list
 * \param values - four float values
 * \return index of the new parameter.
 */
GLint
_mesa_add_unnamed_constant(struct program_parameter_list *paramList,
                           const GLfloat values[4])
{
   return add_parameter(paramList, NULL, values, CONSTANT);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -