nvparse.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #ifdef _WIN32
  2. # define WIN32_LEAN_AND_MEAN
  3. # if !defined(NOMINMAX) && defined(_MSC_VER)
  4. # define NOMINMAX // required to stop windows.h messing up std::min
  5. # endif
  6. # include <windows.h>
  7. #else
  8. #include <stdarg.h>
  9. #define strnicmp strncasecmp
  10. #endif
  11. #include <stdio.h>
  12. #if defined(__APPLE__) && defined(__GNUC__)
  13. #include <OpenGL/glu.h>
  14. #else
  15. #include <GL/glu.h>
  16. #endif
  17. #include <string>
  18. #include <vector>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include "nvparse.h"
  22. #include "nvparse_errors.h"
  23. //void yyinit(char*);
  24. //int yyparse(void);
  25. // RC1.0 -- register combiners 1.0 configuration
  26. bool rc10_init(char *);
  27. int rc10_parse();
  28. bool is_rc10(const char *);
  29. // TS1.0 -- texture shader 1.0 configuration
  30. bool ts10_init(char *);
  31. int ts10_parse();
  32. bool is_ts10(const char *);
  33. // ARBvp1.0 -- ARB vertex program
  34. bool avp10_init(char *);
  35. int avp10_parse();
  36. bool is_avp10(const char *);
  37. #if !defined(__APPLE__)
  38. // VP1.0 -- vertex program
  39. bool vp10_init(char *);
  40. int vp10_parse();
  41. bool is_vp10(const char *);
  42. // VSP1.0 -- vertex state program
  43. bool vsp10_init(char *);
  44. int vsp10_parse(int vspid);
  45. bool is_vsp10(const char *);
  46. // VCP1.0 -- vertex constant program
  47. bool vcp10_init(char *);
  48. int vcp10_parse();
  49. bool is_vcp10(const char *);
  50. // DX8 stuff
  51. // PS1.0 -- DX8 Pixel Shader 1.0 configuration
  52. bool ps10_init(char *);
  53. int ps10_parse();
  54. bool ps10_set_map(const std::vector<int>& argv);
  55. bool is_ps10(const char *);
  56. const int* ps10_get_info(int* pcount);
  57. // VS1.0 -- DX8 Vertex Shader 1.0
  58. bool vs10_init(char *);
  59. int vs10_parse();
  60. bool is_vs10(const char *);
  61. void vs10_load_program();
  62. #endif
  63. nvparse_errors errors;
  64. int line_number;
  65. char * myin = 0;
  66. void nvparse(const char * input_string, int argc /* = 0 */,...)
  67. {
  68. if (NULL == input_string)
  69. {
  70. errors.set("NULL string passed to nvparse");
  71. return;
  72. }
  73. char * instring = strdup(input_string);
  74. // register combiners (1 and 2)
  75. if(is_rc10(instring))
  76. {
  77. if(rc10_init(instring))
  78. {
  79. rc10_parse();
  80. }
  81. }
  82. // texture shader
  83. else if(is_ts10(instring))
  84. {
  85. if(ts10_init(instring))
  86. {
  87. ts10_parse();
  88. }
  89. }
  90. // vertex program
  91. else if(is_avp10(instring))
  92. {
  93. if(avp10_init(instring))
  94. {
  95. avp10_parse();
  96. }
  97. }
  98. #if !defined(__APPLE__)
  99. // vertex constant program
  100. else if(is_vcp10(instring))
  101. {
  102. if(vcp10_init(instring))
  103. {
  104. vcp10_parse();
  105. }
  106. }
  107. // vertex state program
  108. else if(is_vsp10(instring))
  109. {
  110. if(vsp10_init(instring))
  111. {
  112. vsp10_parse(argc);
  113. }
  114. }
  115. // vertex program
  116. else if(is_vp10(instring))
  117. {
  118. if(vp10_init(instring))
  119. {
  120. vp10_parse();
  121. }
  122. }
  123. // DX8 vertex shader
  124. else if ( is_vs10(instring) )
  125. {
  126. if(vs10_init(instring))
  127. {
  128. vs10_parse();
  129. vs10_load_program();
  130. }
  131. }
  132. else if (is_ps10(instring))
  133. {
  134. if(ps10_init(instring))
  135. {
  136. va_list ap;
  137. std::vector<int> argv;
  138. va_start(ap,argc);
  139. for (int i=0;i<argc;++i)
  140. {
  141. int arg = va_arg(ap,int);
  142. argv.push_back(arg);
  143. }
  144. va_end(ap);
  145. if (!ps10_set_map(argv))
  146. return;
  147. ps10_parse();
  148. }
  149. }
  150. #endif
  151. else
  152. {
  153. errors.set("invalid string.\n "
  154. "first characters must be: !!ARBvp1.0 or !!VP1.0 or !!VSP1.0 or !!RC1.0 or !!TS1.0\n "
  155. "or it must be a valid DirectX 8.0 Vertex Shader");
  156. }
  157. free(instring);
  158. }
  159. char * const * const nvparse_get_errors()
  160. {
  161. return errors.get_errors();
  162. }
  163. char * const * const nvparse_print_errors(FILE * errfp)
  164. {
  165. for (char * const * ep = nvparse_get_errors(); *ep; ep++)
  166. {
  167. const char * errstr = *ep;
  168. fprintf(errfp, "%s\n", errstr);
  169. }
  170. return nvparse_get_errors();
  171. }
  172. const int* nvparse_get_info(const char* input_string, int* pcount)
  173. {
  174. #if !defined(__APPLE__)
  175. if (NULL == input_string)
  176. {
  177. errors.set("NULL string passed to nvparse_get_info");
  178. return 0;
  179. }
  180. if (is_ps10(input_string))
  181. {
  182. return ps10_get_info(pcount);
  183. }
  184. #endif
  185. return 0;
  186. }