Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vstest.c 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * GL_ARB_vertex_shader test application. Feeds a vertex shader with attributes that
  3. * that have magic values and check if the values received by the shader are the same.
  4. *
  5. * Requires specific support on the GL implementation side. A special function printMESA()
  6. * must be supported in the language that prints variable's current value of generic type
  7. * to the appropriate shader's info log, and optionally to the screen.
  8. *
  9. * Perfectly valid behaviour produces output that does not have a line
  10. * beginning with three stars (***).
  11. *
  12. * Author: Michal Krol
  13. */
  14. #include "framework.h"
  15. #define EPSILON 0.0001f
  16. static GLhandleARB vert = 0;
  17. static GLhandleARB prog = 0;
  18. enum SUBMIT_MODE
  19. {
  20. SM_IM,
  21. SM_VA,
  22. SM_IM_DL,
  23. SM_VA_DL,
  24. SM_MAX
  25. };
  26. static enum SUBMIT_MODE submit_method = SM_IM;
  27. #define C 0
  28. #define S 1
  29. #define N 2
  30. #define V 3
  31. #define T 4
  32. #define F 5
  33. #define A 6
  34. struct ATTRIB_DATA
  35. {
  36. const char *name;
  37. GLuint dispatch;
  38. GLint index;
  39. GLint bind;
  40. GLuint size;
  41. GLfloat data[4];
  42. };
  43. static struct ATTRIB_DATA attribs[] = {
  44. { "gl_Color", C, -1, -1, 4, { 4.2f, 0.56f, -2.1f, 0.29f } },
  45. { "gl_SecondaryColor", S, -1, -1, 4, { 0.38f, 2.0f, 0.99f, 1.0f } },
  46. { "gl_Normal", N, -1, -1, 3, { 54.0f, 77.0f, 1.15f, 0.0f } },
  47. { "gl_MultiTexCoord0", T, 0, -1, 4, { 11.1f, 11.2f, 11.3f, 11.4f } },
  48. { "gl_MultiTexCoord1", T, 1, -1, 4, { 22.1f, 22.2f, 22.3f, 22.4f } },
  49. { "gl_MultiTexCoord2", T, 2, -1, 4, { 33.1f, 33.2f, 33.3f, 33.4f } },
  50. { "gl_MultiTexCoord3", T, 3, -1, 4, { 44.1f, 44.2f, 44.3f, 44.4f } },
  51. { "gl_MultiTexCoord4", T, 4, -1, 4, { 55.1f, 55.2f, 55.3f, 55.4f } },
  52. { "gl_MultiTexCoord5", T, 5, -1, 4, { 66.1f, 66.2f, 66.3f, 66.4f } },
  53. { "gl_MultiTexCoord6", T, 6, -1, 4, { 77.1f, 77.2f, 77.3f, 77.4f } },
  54. { "gl_MultiTexCoord7", T, 7, -1, 4, { 88.1f, 88.2f, 88.3f, 88.4f } },
  55. { "gl_FogCoord", F, -1, -1, 1, { 0.63f, 0.0f, 0.0f, 0.0f } },
  56. { "Attribute1", A, 1, 1, 4, { 1.11f, 1.22f, 1.33f, 1.44f } },
  57. { "Attribute2", A, 2, 2, 4, { 2.11f, 2.22f, 2.33f, 2.44f } },
  58. { "Attribute3", A, 3, 3, 4, { 3.11f, 3.22f, 3.33f, 3.44f } },
  59. { "Attribute4", A, 4, 4, 1, { 4.11f, 0.0f, 0.0f, 0.0f } },
  60. { "Attribute5", A, 5, 5, 2, { 5.11f, 5.22f, 0.0f, 0.0f } },
  61. { "Attribute6", A, 6, 6, 3, { 6.11f, 6.22f, 6.33f, 0.0f } },
  62. { "Attribute7", A, 7, 7, 2, { 7.11f, 7.22f, 0.0f, 0.0f } },
  63. { "Attribute7", A, 8, -1, 2, { 8.11f, 8.22f, 0.0f, 0.0f } },
  64. { "Attribute9", A, 9, 9, 3, { 9.11f, 9.22f, 9.33f, 0.0f } },
  65. { "Attribute9", A, 10, -1, 3, { 10.11f, 10.22f, 10.33f, 0.0f } },
  66. { "Attribute9", A, 11, -1, 3, { 11.11f, 11.22f, 11.33f, 0.0f } },
  67. { "Attribute12", A, 12, 12, 4, { 12.11f, 12.22f, 12.33f, 12.44f } },
  68. { "Attribute12", A, 13, -1, 4, { 13.11f, 13.22f, 13.33f, 13.44f } },
  69. { "Attribute12", A, 14, -1, 4, { 14.11f, 14.22f, 14.33f, 14.44f } },
  70. { "Attribute12", A, 15, -1, 4, { 15.11f, 15.22f, 15.33f, 15.44f } },
  71. { "gl_Vertex", V, 16, -1, 4, { 0.25f, -0.14f, 0.01f, 1.0f } }
  72. };
  73. static void im_render ()
  74. {
  75. GLint i;
  76. glBegin (GL_POINTS);
  77. for (i = 0; i < sizeof (attribs) / sizeof (*attribs); i++) {
  78. struct ATTRIB_DATA *att = &attribs[i];
  79. switch (att->dispatch)
  80. {
  81. case C:
  82. glColor4fv (att->data);
  83. break;
  84. case S:
  85. glSecondaryColor3fvEXT (att->data);
  86. break;
  87. case N:
  88. glNormal3fv (att->data);
  89. break;
  90. case V:
  91. glVertex4fv (att->data);
  92. break;
  93. case T:
  94. assert (att->index >= 0 && att->index < 8);
  95. glMultiTexCoord4fvARB (GL_TEXTURE0_ARB + att->index, att->data);
  96. break;
  97. case F:
  98. glFogCoordfvEXT (att->data);
  99. break;
  100. case A:
  101. assert (att->index > 0 && att->index < 16);
  102. glVertexAttrib4fvARB (att->index, att->data);
  103. break;
  104. default:
  105. assert (0);
  106. }
  107. }
  108. glEnd ();
  109. }
  110. static void va_render ()
  111. {
  112. GLint i;
  113. for (i = 0; i < sizeof (attribs) / sizeof (*attribs); i++) {
  114. struct ATTRIB_DATA *att = &attribs[i];
  115. switch (att->dispatch)
  116. {
  117. case C:
  118. glColorPointer (4, GL_FLOAT, 0, att->data);
  119. glEnable (GL_COLOR_ARRAY);
  120. break;
  121. case S:
  122. glSecondaryColorPointerEXT (4, GL_FLOAT, 0, att->data);
  123. glEnable (GL_SECONDARY_COLOR_ARRAY_EXT);
  124. break;
  125. case N:
  126. glNormalPointer (GL_FLOAT, 0, att->data);
  127. glEnable (GL_NORMAL_ARRAY);
  128. break;
  129. case V:
  130. glVertexPointer (4, GL_FLOAT, 0, att->data);
  131. glEnable (GL_VERTEX_ARRAY);
  132. break;
  133. case T:
  134. assert (att->index >= 0 && att->index < 8);
  135. glClientActiveTextureARB (GL_TEXTURE0_ARB + att->index);
  136. glTexCoordPointer (4, GL_FLOAT, 0, att->data);
  137. glEnable (GL_TEXTURE_COORD_ARRAY);
  138. break;
  139. case F:
  140. glFogCoordPointerEXT (GL_FLOAT, 0, att->data);
  141. glEnable (GL_FOG_COORDINATE_ARRAY_EXT);
  142. break;
  143. case A:
  144. assert (att->index > 0 && att->index < 16);
  145. glVertexAttribPointerARB (att->index, 4, GL_FLOAT, GL_FALSE, 0, att->data);
  146. glEnableVertexAttribArrayARB (att->index);
  147. break;
  148. default:
  149. assert (0);
  150. }
  151. }
  152. glDrawArrays (GL_POINTS, 0, 1);
  153. for (i = 0; i < sizeof (attribs) / sizeof (*attribs); i++) {
  154. struct ATTRIB_DATA *att = &attribs[i];
  155. switch (att->dispatch)
  156. {
  157. case C:
  158. glDisable (GL_COLOR_ARRAY);
  159. break;
  160. case S:
  161. glDisable (GL_SECONDARY_COLOR_ARRAY_EXT);
  162. break;
  163. case N:
  164. glDisable (GL_NORMAL_ARRAY);
  165. break;
  166. case V:
  167. glDisable (GL_VERTEX_ARRAY);
  168. break;
  169. case T:
  170. glClientActiveTextureARB (GL_TEXTURE0_ARB + att->index);
  171. glDisable (GL_TEXTURE_COORD_ARRAY);
  172. break;
  173. case F:
  174. glDisable (GL_FOG_COORDINATE_ARRAY_EXT);
  175. break;
  176. case A:
  177. glDisableVertexAttribArrayARB (att->index);
  178. break;
  179. default:
  180. assert (0);
  181. }
  182. }
  183. }
  184. static void dl_start ()
  185. {
  186. glNewList (GL_COMPILE, 1);
  187. }
  188. static void dl_end ()
  189. {
  190. glEndList ();
  191. glCallList (1);
  192. }
  193. static void load_test_file (const char *filename)
  194. {
  195. FILE *f;
  196. GLint size;
  197. char *code;
  198. GLint i;
  199. f = fopen (filename, "r");
  200. if (f == NULL)
  201. return;
  202. fseek (f, 0, SEEK_END);
  203. size = ftell (f);
  204. fseek (f, 0, SEEK_SET);
  205. code = (char *) (malloc (size));
  206. if (code == NULL) {
  207. fclose (f);
  208. return;
  209. }
  210. size = fread (code, 1, size, f);
  211. fclose (f);
  212. glShaderSourceARB (vert, 1, (const GLcharARB **) (&code), &size);
  213. glCompileShaderARB (vert);
  214. if (!CheckObjectStatus (vert))
  215. exit (0);
  216. for (i = 0; i < sizeof (attribs) / sizeof (*attribs); i++)
  217. if (attribs[i].dispatch == A && attribs[i].bind != -1)
  218. glBindAttribLocationARB (prog, attribs[i].bind, attribs[i].name);
  219. }
  220. void InitScene (void)
  221. {
  222. prog = glCreateProgramObjectARB ();
  223. vert = glCreateShaderObjectARB (GL_VERTEX_SHADER_ARB);
  224. glAttachObjectARB (prog, vert);
  225. glDeleteObjectARB (vert);
  226. load_test_file ("vstest.txt");
  227. glLinkProgramARB (prog);
  228. if (!CheckObjectStatus (prog))
  229. exit (0);
  230. glUseProgramObjectARB (prog);
  231. }
  232. void RenderScene (void)
  233. {
  234. GLint info_length, length;
  235. char output[65000], *p;
  236. GLint i;
  237. if (submit_method == SM_MAX)
  238. exit (0);
  239. /*
  240. * Get the current size of the info log. Any text output produced by executed
  241. * shader will be appended to the end of log.
  242. */
  243. glGetObjectParameterivARB (vert, GL_OBJECT_INFO_LOG_LENGTH_ARB, &info_length);
  244. switch (submit_method)
  245. {
  246. case SM_IM:
  247. printf ("\n--- TESTING IMMEDIATE MODE\n");
  248. im_render ();
  249. break;
  250. case SM_VA:
  251. printf ("\n--- TESTING VERTEX ARRAY MODE\n");
  252. va_render ();
  253. break;
  254. case SM_IM_DL:
  255. printf ("\n--- TESTING IMMEDIATE + DISPLAY LIST MODE\n");
  256. dl_start ();
  257. im_render ();
  258. dl_end ();
  259. break;
  260. case SM_VA_DL:
  261. printf ("\n--- TESTING VERTEX ARRAY + DISPLAY LIST MODE\n");
  262. dl_start ();
  263. va_render ();
  264. dl_end ();
  265. break;
  266. default:
  267. assert (0);
  268. }
  269. glFlush ();
  270. /*
  271. * Get the info log and set the pointer to the beginning of the output.
  272. */
  273. glGetInfoLogARB (vert, sizeof (output), &length, output);
  274. p = output + info_length - 1;
  275. for (i = 0; i < sizeof (attribs) / sizeof (*attribs); i++) {
  276. GLuint j;
  277. for (j = 0; j < attribs[i].size; j++) {
  278. GLfloat value;
  279. if (p == NULL) {
  280. printf ("*** %s\n", "I/O error");
  281. break;
  282. }
  283. if (strncmp (p, "true", 4) == 0)
  284. value = 1.0f;
  285. else if (strncmp (p, "false", 5) == 0)
  286. value = 0.0f;
  287. else if (sscanf (p, "%f", &value) != 1) {
  288. printf ("*** %s\n", "I/O error");
  289. p = NULL;
  290. break;
  291. }
  292. if (fabs (value - attribs[i].data[j]) > EPSILON)
  293. printf ("*** %s, is %f, should be %f\n", "Values are different", value, attribs[i].data[j]);
  294. p = strchr (p, '\n');
  295. if (p != NULL)
  296. p++;
  297. }
  298. if (p == NULL)
  299. break;
  300. }
  301. submit_method++;
  302. }
  303. int main (int argc, char *argv[])
  304. {
  305. InitFramework (&argc, argv);
  306. return 0;
  307. }