| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- #include "vs1.0_inst_list.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <string>
- #include "nvparse_errors.h"
- #include "nvparse_externs.h"
- #include <string.h>
- #if CM_PLATFORM == CM_PLATFORM_APPLE
- # include <OpenGL/glu.h>
- #else
- # include <GL/glu.h>
- #endif
- using namespace std;
- extern string vs10_transstring;
- #define MAX_NUM_INSTRUCTIONS 128
- #define INSTRUCTION_LIST_INC 128
- VS10InstList::VS10InstList()
- {
- size = 0;
- max = INSTRUCTION_LIST_INC;
- list = new VS10Inst[max];
- }
- VS10InstList::~VS10InstList()
- {
- delete [] list;
- }
- int VS10InstList::Size()
- {
- return size;
- }
- VS10InstList& VS10InstList::operator+=(VS10InstPtr t)
- {
- if (size == max) {
- // Extend list size by increment amount.
- VS10InstPtr newlist;
- max += INSTRUCTION_LIST_INC;
- newlist = new VS10Inst[max];
- for ( int i = 0; i < size; i++ )
- newlist[i] = list[i];
- delete [] list;
- list = newlist;
- }
- list[size++] = *t;
- return *this;
- }
- void VS10InstList::Translate()
- {
- int ntranslated = 0;
- vs10_transstring.append( "!!VP1.0\n" );
- for (int i = 0; i < size; i++)
- {
- ntranslated += list[i].Translate();
- }
- vs10_transstring.append( "END\n" );
- if ( ntranslated > 128 )
- {
- char str[256];
- sprintf( str, "Vertex Shader had more than 128 instructions. (Converted to: %d)\n", ntranslated );
- errors.set( str );
- }
- //fprintf( stderr, "Converted vertex shader to vertex program with %d instructions.\n\n", ntranslated );
- }
- void VS10InstList::Validate()
- {
- int vsflag = 0;
- for ( int i = 0; i < size; i++ )
- {
- list[i].Validate( vsflag );
- }
- }
- namespace
- {
- void LoadProgram( GLenum target, GLuint id, char *instring );
- void StrToUpper(char * string);
- GLint vpid;
- }
- bool is_vs10(const char *s)
- {
- int len;
- char *temp;
- bool vshader_flag;
- temp = NULL;
- len = strlen(s);
- if ( len > 0 )
- temp = new char [len+1];
- for ( int k = 0; k < len; k++ )
- {
- temp[k] = (char) tolower( (char) s[k] );
- }
- if ( len == 0 )
- vshader_flag = false;
- else
- {
- vshader_flag = ( NULL != strstr(temp, "vs.1.0") ) ||
- ( NULL != strstr(temp, "vs.1.1") );
- delete [] temp;
- }
- return vshader_flag;
- }
- bool vs10_init_more()
- {
- static bool vpinit = false;
- if (vpinit == false )
- {
- /*
- if(! glh_init_extensions("GL_NV_vertex_program"))
- {
- errors.set("unable to initialize GL_NV_vertex_program");
- return false;
- }
- else
- {
- */
- vpinit = true;
- /*
- }
- */
- }
-
- glGetIntegerv( GL_VERTEX_PROGRAM_BINDING_NV, &vpid );
-
- if ( vpid == 0 )
- {
- char str[128];
- sprintf( str, "No vertex program id bound for nvparse() invocation. Bound id = %d\n", (int)vpid );
- errors.set( str );
- return false;
- }
- errors.reset();
- line_number = 1;
- vs10_transstring = "";
- return true;
- }
- void vs10_load_program()
- {
- // Only load the program if no errors occurred.
- if ( errors.get_num_errors() == 0 )
- LoadProgram( GL_VERTEX_PROGRAM_NV, vpid, (char *) vs10_transstring.c_str() );
- }
- namespace
- {
- //.----------------------------------------------------------------------------.
- //| Function : LoadProgram |
- //| Description: Load a program into GL, and report any errors encountered. |
- //.----------------------------------------------------------------------------.
- void LoadProgram( GLenum target, GLuint id, char *instring )
- {
- GLint errPos;
- GLenum errCode;
- const GLubyte *errString;
-
- int len = strlen(instring);
- glLoadProgramNV( target, id, len, (const GLubyte *) instring );
- if ( (errCode = glGetError()) != GL_NO_ERROR )
- {
- errString = gluErrorString( errCode );
-
- glGetIntegerv( GL_PROGRAM_ERROR_POSITION_NV, &errPos );
-
- int nlines = 1;
- int nchar = 1;
- int i;
- for ( i = 0; i < errPos; i++ )
- {
- if ( instring[i] == '\n' )
- {
- nlines++;
- nchar = 1;
- }
- else
- {
- nchar++;
- }
- }
- int start = 0;
- int end = 0;
- int flag = ((instring[errPos]==';') | (instring[errPos-1]==';')) ? 1 : 0;
- for ( i = errPos; i >= 0; i-- )
- {
- start = i;
- if ( flag && (start >= errPos-1) )
- continue;
- if ( instring[i] == ';' )
- {
- if ( !flag )
- {
- start = i+1;
- if ( instring[start] == '\n' )
- start++;
- }
- break;
- }
- }
- for ( i = errPos; i < len; i++ )
- {
- end = i;
- if ( instring[i] == ';' && end > start)
- {
- break;
- }
- }
- if ( errPos - start > 30 )
- {
- start = errPos - 30;
- }
- if ( end - errPos > 30 )
- {
- end = errPos + 30;
- }
-
- char substring[96];
- memset( substring, 0, 96 );
- strncpy( substring, &(instring[start]), end-start+1 );
- char str[256];
- //sprintf( str, "error at line %d character %d\n \"%s\"\n", nlines, nchar, substring );
- sprintf( str, "error at line %d character %d\n\"%s\"\n", nlines, nchar, substring );
- int width = errPos-start;
- for ( i = 0; i < width; i++ )
- {
- strcat( str, " " );
- }
- strcat( str, "|\n" );
- for ( i = 0; i < width; i++ )
- {
- strcat( str, " " );
- }
- strcat( str, "^\n" );
-
- errors.set( str );
- }
- }
-
-
- //.----------------------------------------------------------------------------.
- //| Function : StrToUpper |
- //| Description: Converts all lowercase chars in a string to uppercase. |
- //.----------------------------------------------------------------------------.
- void StrToUpper(char *string)
- {
- for (unsigned int i = 0; i < strlen(string); i++)
- string[i] = toupper(string[i]);
- }
-
- }
- /*
- else if ( is_vs10(instring) )
- {
- if (vpinit == 0 )
- {
- if(! glh_init_extensions("GL_NV_vertex_program"))
- {
- errors.set("unable to initialize GL_NV_vertex_program");
- free(instring);
- return;
- }
- else
- {
- vpinit = 1;
- }
- }
- if ( glGetError() != GL_NO_ERROR )
- {
- errors.set( "Previous GL_ERROR prior to vertex shader parsing.\n" );
- }
- int vpid;
- glGetIntegerv( GL_VERTEX_PROGRAM_BINDING_NV, &vpid );
-
- if ( vpid == 0 )
- {
- char str[128];
- sprintf( str, "No vertex program id bound for nvparse() invocation. Bound id = %d\n", vpid );
- errors.set( str );
- }
- else
- {
- errors.reset();
- line_number = 1;
- vs10_init(instring);
- vs10_transstring = "";
- vs10_parse();
- //fprintf( stderr, "Converted text:\n%s\n\n\n", vs10_transstring.c_str() );
- LoadProgram( GL_VERTEX_PROGRAM_NV, vpid, (char *) vs10_transstring.c_str() );
- }
-
- }
- */
|