[3] | 1 | #include "vs1.0_inst_list.h" |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | #include <stdio.h> |
---|
| 4 | #include <ctype.h> |
---|
| 5 | #include <string> |
---|
| 6 | #include "nvparse_errors.h" |
---|
| 7 | #include "nvparse_externs.h" |
---|
| 8 | #include <string.h> |
---|
| 9 | #include <OgreGLPrerequisites.h> |
---|
| 10 | #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE |
---|
| 11 | # include <OpenGL/glu.h> |
---|
| 12 | #else |
---|
| 13 | # include <GL/glu.h> |
---|
| 14 | #endif |
---|
| 15 | |
---|
| 16 | using namespace std; |
---|
| 17 | |
---|
| 18 | extern string vs10_transstring; |
---|
| 19 | |
---|
| 20 | #define MAX_NUM_INSTRUCTIONS 128 |
---|
| 21 | #define INSTRUCTION_LIST_INC 128 |
---|
| 22 | |
---|
| 23 | VS10InstList::VS10InstList() |
---|
| 24 | { |
---|
| 25 | size = 0; |
---|
| 26 | max = INSTRUCTION_LIST_INC; |
---|
| 27 | list = new VS10Inst[max]; |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | VS10InstList::~VS10InstList() |
---|
| 31 | { |
---|
| 32 | delete [] list; |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | int VS10InstList::Size() |
---|
| 36 | { |
---|
| 37 | return size; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | VS10InstList& VS10InstList::operator+=(VS10InstPtr t) |
---|
| 41 | { |
---|
| 42 | if (size == max) { |
---|
| 43 | // Extend list size by increment amount. |
---|
| 44 | VS10InstPtr newlist; |
---|
| 45 | max += INSTRUCTION_LIST_INC; |
---|
| 46 | newlist = new VS10Inst[max]; |
---|
| 47 | for ( int i = 0; i < size; i++ ) |
---|
| 48 | newlist[i] = list[i]; |
---|
| 49 | delete [] list; |
---|
| 50 | list = newlist; |
---|
| 51 | } |
---|
| 52 | list[size++] = *t; |
---|
| 53 | return *this; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | void VS10InstList::Translate() |
---|
| 57 | { |
---|
| 58 | int ntranslated = 0; |
---|
| 59 | |
---|
| 60 | vs10_transstring.append( "!!VP1.0\n" ); |
---|
| 61 | for (int i = 0; i < size; i++) |
---|
| 62 | { |
---|
| 63 | ntranslated += list[i].Translate(); |
---|
| 64 | } |
---|
| 65 | vs10_transstring.append( "END\n" ); |
---|
| 66 | |
---|
| 67 | if ( ntranslated > 128 ) |
---|
| 68 | { |
---|
| 69 | char str[256]; |
---|
| 70 | sprintf( str, "Vertex Shader had more than 128 instructions. (Converted to: %d)\n", ntranslated ); |
---|
| 71 | errors.set( str ); |
---|
| 72 | } |
---|
| 73 | //fprintf( stderr, "Converted vertex shader to vertex program with %d instructions.\n\n", ntranslated ); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | void VS10InstList::Validate() |
---|
| 77 | { |
---|
| 78 | int vsflag = 0; |
---|
| 79 | for ( int i = 0; i < size; i++ ) |
---|
| 80 | { |
---|
| 81 | list[i].Validate( vsflag ); |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | namespace |
---|
| 88 | { |
---|
| 89 | void LoadProgram( GLenum target, GLuint id, char *instring ); |
---|
| 90 | void StrToUpper(char * string); |
---|
| 91 | int vpid; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | bool is_vs10(const char *s) |
---|
| 97 | { |
---|
| 98 | int len; |
---|
| 99 | char *temp; |
---|
| 100 | bool vshader_flag; |
---|
| 101 | |
---|
| 102 | temp = NULL; |
---|
| 103 | len = strlen(s); |
---|
| 104 | if ( len > 0 ) |
---|
| 105 | temp = new char [len+1]; |
---|
| 106 | for ( int k = 0; k < len; k++ ) |
---|
| 107 | { |
---|
| 108 | temp[k] = (char) tolower( (char) s[k] ); |
---|
| 109 | } |
---|
| 110 | if ( len == 0 ) |
---|
| 111 | vshader_flag = false; |
---|
| 112 | else |
---|
| 113 | { |
---|
| 114 | vshader_flag = ( NULL != strstr(temp, "vs.1.0") ) || |
---|
| 115 | ( NULL != strstr(temp, "vs.1.1") ); |
---|
| 116 | delete [] temp; |
---|
| 117 | } |
---|
| 118 | return vshader_flag; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | bool vs10_init_more() |
---|
| 122 | { |
---|
| 123 | static bool vpinit = false; |
---|
| 124 | if (vpinit == false ) |
---|
| 125 | { |
---|
| 126 | /* |
---|
| 127 | if(! glh_init_extensions("GL_NV_vertex_program")) |
---|
| 128 | { |
---|
| 129 | errors.set("unable to initialize GL_NV_vertex_program"); |
---|
| 130 | return false; |
---|
| 131 | } |
---|
| 132 | else |
---|
| 133 | { |
---|
| 134 | */ |
---|
| 135 | vpinit = true; |
---|
| 136 | /* |
---|
| 137 | } |
---|
| 138 | */ |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | glGetIntegerv( GL_VERTEX_PROGRAM_BINDING_NV, &vpid ); |
---|
| 142 | |
---|
| 143 | if ( vpid == 0 ) |
---|
| 144 | { |
---|
| 145 | char str[128]; |
---|
| 146 | sprintf( str, "No vertex program id bound for nvparse() invocation. Bound id = %d\n", vpid ); |
---|
| 147 | errors.set( str ); |
---|
| 148 | return false; |
---|
| 149 | } |
---|
| 150 | errors.reset(); |
---|
| 151 | line_number = 1; |
---|
| 152 | vs10_transstring = ""; |
---|
| 153 | return true; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | void vs10_load_program() |
---|
| 157 | { |
---|
| 158 | // Only load the program if no errors occurred. |
---|
| 159 | if ( errors.get_num_errors() == 0 ) |
---|
| 160 | LoadProgram( GL_VERTEX_PROGRAM_NV, vpid, (char *) vs10_transstring.c_str() ); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | |
---|
| 164 | namespace |
---|
| 165 | { |
---|
| 166 | //.----------------------------------------------------------------------------. |
---|
| 167 | //| Function : LoadProgram | |
---|
| 168 | //| Description: Load a program into GL, and report any errors encountered. | |
---|
| 169 | //.----------------------------------------------------------------------------. |
---|
| 170 | void LoadProgram( GLenum target, GLuint id, char *instring ) |
---|
| 171 | { |
---|
| 172 | GLint errPos; |
---|
| 173 | GLenum errCode; |
---|
| 174 | const GLubyte *errString; |
---|
| 175 | |
---|
| 176 | int len = strlen(instring); |
---|
| 177 | glLoadProgramNV( target, id, len, (const GLubyte *) instring ); |
---|
| 178 | if ( (errCode = glGetError()) != GL_NO_ERROR ) |
---|
| 179 | { |
---|
| 180 | errString = gluErrorString( errCode ); |
---|
| 181 | |
---|
| 182 | glGetIntegerv( GL_PROGRAM_ERROR_POSITION_NV, &errPos ); |
---|
| 183 | |
---|
| 184 | int nlines = 1; |
---|
| 185 | int nchar = 1; |
---|
| 186 | int i; |
---|
| 187 | for ( i = 0; i < errPos; i++ ) |
---|
| 188 | { |
---|
| 189 | if ( instring[i] == '\n' ) |
---|
| 190 | { |
---|
| 191 | nlines++; |
---|
| 192 | nchar = 1; |
---|
| 193 | } |
---|
| 194 | else |
---|
| 195 | { |
---|
| 196 | nchar++; |
---|
| 197 | } |
---|
| 198 | } |
---|
| 199 | int start; |
---|
| 200 | int end; |
---|
| 201 | int flag = ((instring[errPos]==';') | (instring[errPos-1]==';')) ? 1 : 0; |
---|
| 202 | for ( i = errPos; i >= 0; i-- ) |
---|
| 203 | { |
---|
| 204 | start = i; |
---|
| 205 | if ( flag && (start >= errPos-1) ) |
---|
| 206 | continue; |
---|
| 207 | if ( instring[i] == ';' ) |
---|
| 208 | { |
---|
| 209 | if ( !flag ) |
---|
| 210 | { |
---|
| 211 | start = i+1; |
---|
| 212 | if ( instring[start] == '\n' ) |
---|
| 213 | start++; |
---|
| 214 | } |
---|
| 215 | break; |
---|
| 216 | } |
---|
| 217 | } |
---|
| 218 | for ( i = errPos; i < len; i++ ) |
---|
| 219 | { |
---|
| 220 | end = i; |
---|
| 221 | if ( instring[i] == ';' && end > start) |
---|
| 222 | { |
---|
| 223 | break; |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | if ( errPos - start > 30 ) |
---|
| 227 | { |
---|
| 228 | start = errPos - 30; |
---|
| 229 | } |
---|
| 230 | if ( end - errPos > 30 ) |
---|
| 231 | { |
---|
| 232 | end = errPos + 30; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | char substring[96]; |
---|
| 236 | memset( substring, 0, 96 ); |
---|
| 237 | strncpy( substring, &(instring[start]), end-start+1 ); |
---|
| 238 | char str[256]; |
---|
| 239 | //sprintf( str, "error at line %d character %d\n \"%s\"\n", nlines, nchar, substring ); |
---|
| 240 | sprintf( str, "error at line %d character %d\n\"%s\"\n", nlines, nchar, substring ); |
---|
| 241 | int width = errPos-start; |
---|
| 242 | for ( i = 0; i < width; i++ ) |
---|
| 243 | { |
---|
| 244 | strcat( str, " " ); |
---|
| 245 | } |
---|
| 246 | strcat( str, "|\n" ); |
---|
| 247 | for ( i = 0; i < width; i++ ) |
---|
| 248 | { |
---|
| 249 | strcat( str, " " ); |
---|
| 250 | } |
---|
| 251 | strcat( str, "^\n" ); |
---|
| 252 | |
---|
| 253 | errors.set( str ); |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | |
---|
| 258 | //.----------------------------------------------------------------------------. |
---|
| 259 | //| Function : StrToUpper | |
---|
| 260 | //| Description: Converts all lowercase chars in a string to uppercase. | |
---|
| 261 | //.----------------------------------------------------------------------------. |
---|
| 262 | void StrToUpper(char *string) |
---|
| 263 | { |
---|
| 264 | for (unsigned int i = 0; i < strlen(string); i++) |
---|
| 265 | string[i] = toupper(string[i]); |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | /* |
---|
| 271 | else if ( is_vs10(instring) ) |
---|
| 272 | { |
---|
| 273 | if (vpinit == 0 ) |
---|
| 274 | { |
---|
| 275 | if(! glh_init_extensions("GL_NV_vertex_program")) |
---|
| 276 | { |
---|
| 277 | errors.set("unable to initialize GL_NV_vertex_program"); |
---|
| 278 | free(instring); |
---|
| 279 | return; |
---|
| 280 | } |
---|
| 281 | else |
---|
| 282 | { |
---|
| 283 | vpinit = 1; |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | if ( glGetError() != GL_NO_ERROR ) |
---|
| 288 | { |
---|
| 289 | errors.set( "Previous GL_ERROR prior to vertex shader parsing.\n" ); |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | int vpid; |
---|
| 293 | glGetIntegerv( GL_VERTEX_PROGRAM_BINDING_NV, &vpid ); |
---|
| 294 | |
---|
| 295 | if ( vpid == 0 ) |
---|
| 296 | { |
---|
| 297 | char str[128]; |
---|
| 298 | sprintf( str, "No vertex program id bound for nvparse() invocation. Bound id = %d\n", vpid ); |
---|
| 299 | errors.set( str ); |
---|
| 300 | } |
---|
| 301 | else |
---|
| 302 | { |
---|
| 303 | errors.reset(); |
---|
| 304 | line_number = 1; |
---|
| 305 | vs10_init(instring); |
---|
| 306 | vs10_transstring = ""; |
---|
| 307 | vs10_parse(); |
---|
| 308 | //fprintf( stderr, "Converted text:\n%s\n\n\n", vs10_transstring.c_str() ); |
---|
| 309 | LoadProgram( GL_VERTEX_PROGRAM_NV, vpid, (char *) vs10_transstring.c_str() ); |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | */ |
---|