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.

prog_parameter.c 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * (C) Copyright IBM Corporation 2006
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * on the rights to use, copy, modify, merge, publish, distribute, sub
  9. * license, and/or sell copies of the Software, and to permit persons to whom
  10. * the Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  19. * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. /**
  25. * \file prog_parameter.c
  26. *
  27. * Test various aspects of setting (and getting) low-level program parameters.
  28. * This is primarilly intended as a test for GL_EXT_gpu_program_parameters,
  29. * but it turns out that it hits some other functionality along the way.
  30. *
  31. * \author Ian Romanick <idr@us.ibm.com>
  32. */
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <math.h>
  36. #include <GL/glew.h>
  37. #include <GL/glut.h>
  38. #ifndef GL_EXT_gpu_program_parameters
  39. typedef void (APIENTRYP PFNGLPROGRAMLOCALPARAMETERS4FVEXTPROC)(GLenum,
  40. GLuint, GLsizei, const GLfloat *);
  41. typedef void (APIENTRYP PFNGLPROGRAMENVPARAMETERS4FVEXTPROC)(GLenum,
  42. GLuint, GLsizei, const GLfloat *);
  43. #endif
  44. static PFNGLPROGRAMLOCALPARAMETER4FVARBPROC program_local_parameter4fv = NULL;
  45. static PFNGLGETPROGRAMLOCALPARAMETERFVARBPROC get_program_local_parameterfv = NULL;
  46. static PFNGLPROGRAMENVPARAMETER4FVARBPROC program_env_parameter4fv = NULL;
  47. static PFNGLGETPROGRAMENVPARAMETERFVARBPROC get_program_env_parameterfv = NULL;
  48. static PFNGLBINDPROGRAMARBPROC bind_program = NULL;
  49. static PFNGLGETPROGRAMIVARBPROC get_program = NULL;
  50. static PFNGLPROGRAMLOCALPARAMETERS4FVEXTPROC program_local_parameters4fv = NULL;
  51. static PFNGLPROGRAMENVPARAMETERS4FVEXTPROC program_env_parameters4fv = NULL;
  52. static int Width = 400;
  53. static int Height = 200;
  54. static const GLfloat Near = 5.0, Far = 25.0;
  55. static void Display( void )
  56. {
  57. }
  58. static void Idle( void )
  59. {
  60. }
  61. static void Visible( int vis )
  62. {
  63. if ( vis == GLUT_VISIBLE ) {
  64. glutIdleFunc( Idle );
  65. }
  66. else {
  67. glutIdleFunc( NULL );
  68. }
  69. }
  70. static void Reshape( int width, int height )
  71. {
  72. GLfloat ar = (float) width / (float) height;
  73. Width = width;
  74. Height = height;
  75. glViewport( 0, 0, width, height );
  76. glMatrixMode( GL_PROJECTION );
  77. glLoadIdentity();
  78. glFrustum( -ar, ar, -1.0, 1.0, Near, Far );
  79. }
  80. static void Key( unsigned char key, int x, int y )
  81. {
  82. (void) x;
  83. (void) y;
  84. switch (key) {
  85. case 27:
  86. exit(0);
  87. break;
  88. }
  89. glutPostRedisplay();
  90. }
  91. static int set_parameter_batch( GLsizei count, GLfloat * param,
  92. const char * name,
  93. PFNGLPROGRAMLOCALPARAMETER4FVARBPROC set_parameter,
  94. PFNGLPROGRAMLOCALPARAMETERS4FVEXTPROC set_parameters,
  95. PFNGLGETPROGRAMLOCALPARAMETERFVARBPROC get_parameter
  96. )
  97. {
  98. unsigned i;
  99. int pass = 1;
  100. for ( i = 0 ; i < (4 * count) ; i++ ) {
  101. param[i] = (GLfloat) rand() / (GLfloat) rand();
  102. }
  103. /* Try using the "classic" interface.
  104. */
  105. printf("Testing glProgram%sParameter4fvARB (count = %u)...\n", name, count);
  106. for ( i = 0 ; i < count ; i++ ) {
  107. (*set_parameter)(GL_VERTEX_PROGRAM_ARB, i, & param[i * 4]);
  108. }
  109. for ( i = 0 ; i < count ; i++ ) {
  110. GLfloat temp[4];
  111. (*get_parameter)(GL_VERTEX_PROGRAM_ARB, i, temp);
  112. if ( (temp[0] != param[(i * 4) + 0])
  113. || (temp[1] != param[(i * 4) + 1])
  114. || (temp[2] != param[(i * 4) + 2])
  115. || (temp[3] != param[(i * 4) + 3]) ) {
  116. printf("Mismatch in glProgram%sParameter4fvARB index %u!\n", name, i);
  117. printf("Got { %f, %f, %f, %f }, expected { %f, %f, %f, %f }!\n",
  118. temp[0], temp[1],
  119. temp[2], temp[3],
  120. param[(i * 4) + 0], param[(i * 4) + 1],
  121. param[(i * 4) + 2], param[(i * 4) + 3]);
  122. pass = 0;
  123. break;
  124. }
  125. }
  126. if ( set_parameters == NULL ) {
  127. return pass;
  128. }
  129. for ( i = 0 ; i < (4 * count) ; i++ ) {
  130. param[i] = (GLfloat) rand() / (GLfloat) rand();
  131. }
  132. printf("Testing glProgram%sParameters4fvEXT (count = %u)...\n", name, count);
  133. (*set_parameters)(GL_VERTEX_PROGRAM_ARB, 0, count, param);
  134. for ( i = 0 ; i < count ; i++ ) {
  135. GLfloat temp[4];
  136. (*get_parameter)(GL_VERTEX_PROGRAM_ARB, i, temp);
  137. if ( (temp[0] != param[(i * 4) + 0])
  138. || (temp[1] != param[(i * 4) + 1])
  139. || (temp[2] != param[(i * 4) + 2])
  140. || (temp[3] != param[(i * 4) + 3]) ) {
  141. printf("Mismatch in glProgram%sParameters4fvEXT index %u!\n", name, i);
  142. printf("Got { %f, %f, %f, %f }, expected { %f, %f, %f, %f }!\n",
  143. temp[0], temp[1],
  144. temp[2], temp[3],
  145. param[(i * 4) + 0], param[(i * 4) + 1],
  146. param[(i * 4) + 2], param[(i * 4) + 3]);
  147. pass = 0;
  148. break;
  149. }
  150. }
  151. return pass;
  152. }
  153. static void Init( void )
  154. {
  155. const char * const ver_string = (const char * const)
  156. glGetString( GL_VERSION );
  157. int pass = 1;
  158. GLfloat * params;
  159. GLint max_program_env_parameters;
  160. GLint max_program_local_parameters;
  161. int i;
  162. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  163. printf("GL_VERSION = %s\n\n", ver_string);
  164. if ( !glutExtensionSupported("GL_ARB_vertex_program") ) {
  165. printf("Sorry, this program requires GL_ARB_vertex_program\n");
  166. exit(2);
  167. }
  168. program_local_parameter4fv = (PFNGLPROGRAMLOCALPARAMETER4FVARBPROC) glutGetProcAddress( "glProgramLocalParameter4fvARB" );
  169. program_env_parameter4fv = (PFNGLPROGRAMENVPARAMETER4FVARBPROC) glutGetProcAddress( "glProgramEnvParameter4fvARB" );
  170. get_program_local_parameterfv = (PFNGLGETPROGRAMLOCALPARAMETERFVARBPROC) glutGetProcAddress( "glGetProgramLocalParameterfvARB" );
  171. get_program_env_parameterfv = (PFNGLGETPROGRAMENVPARAMETERFVARBPROC) glutGetProcAddress( "glGetProgramEnvParameterfvARB" );
  172. bind_program = (PFNGLBINDPROGRAMARBPROC) glutGetProcAddress( "glBindProgramARB" );
  173. get_program = (PFNGLGETPROGRAMIVARBPROC) glutGetProcAddress( "glGetProgramivARB" );
  174. if ( glutExtensionSupported("GL_EXT_gpu_program_parameters") ) {
  175. printf("GL_EXT_gpu_program_parameters available, testing that path.\n");
  176. program_local_parameters4fv = (PFNGLPROGRAMLOCALPARAMETERS4FVEXTPROC) glutGetProcAddress( "glProgramLocalParameters4fvEXT" );
  177. program_env_parameters4fv = (PFNGLPROGRAMENVPARAMETERS4FVEXTPROC) glutGetProcAddress( "glProgramEnvParameters4fvEXT" );
  178. }
  179. else {
  180. printf("GL_EXT_gpu_program_parameters not available.\n");
  181. program_local_parameters4fv = NULL;
  182. program_env_parameters4fv = NULL;
  183. }
  184. /* Since the test sets program local parameters, a program must be bound.
  185. * Program source, however, is not needed.
  186. */
  187. (*bind_program)(GL_VERTEX_PROGRAM_ARB, 1);
  188. (*get_program)(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_ENV_PARAMETERS_ARB,
  189. & max_program_env_parameters);
  190. params = malloc(max_program_env_parameters * 4 * sizeof(GLfloat));
  191. for (i = 0; i < max_program_env_parameters * 4; i++) {
  192. params[i] = 0.0F;
  193. }
  194. pass &= set_parameter_batch(max_program_env_parameters, params, "Env",
  195. program_env_parameter4fv,
  196. program_env_parameters4fv,
  197. get_program_env_parameterfv);
  198. (*get_program)(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB,
  199. & max_program_local_parameters);
  200. if (max_program_local_parameters > max_program_env_parameters) {
  201. params = realloc(params,
  202. max_program_local_parameters * 4 * sizeof(GLfloat));
  203. }
  204. pass &= set_parameter_batch(max_program_local_parameters, params, "Local",
  205. program_local_parameter4fv,
  206. program_local_parameters4fv,
  207. get_program_local_parameterfv);
  208. free(params);
  209. if (! pass) {
  210. printf("FAIL!\n");
  211. exit(1);
  212. }
  213. printf("PASS!\n");
  214. }
  215. int main( int argc, char *argv[] )
  216. {
  217. glutInit( &argc, argv );
  218. glutInitWindowPosition( 0, 0 );
  219. glutInitWindowSize( Width, Height );
  220. glutInitDisplayMode( GLUT_RGB );
  221. glutCreateWindow( "Program Parameters Test" );
  222. glewInit();
  223. glutReshapeFunc( Reshape );
  224. glutKeyboardFunc( Key );
  225. glutDisplayFunc( Display );
  226. glutVisibilityFunc( Visible );
  227. Init();
  228. return 0;
  229. }