vs1.0_inst_list.cpp 7.1 KB

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