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