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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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/glut.h>
  37. #ifndef GL_EXT_gpu_program_parameters
  38. typedef void (APIENTRYP PFNGLPROGRAMLOCALPARAMETERS4FVEXTPROC)(GLenum,
  39. GLuint, GLsizei, const GLfloat *);
  40. typedef void (APIENTRYP PFNGLPROGRAMENVPARAMETERS4FVEXTPROC)(GLenum,
  41. GLuint, GLsizei, const GLfloat *);
  42. #endif
  43. static PFNGLPROGRAMLOCALPARAMETER4FVARBPROC program_local_parameter4fv = NULL;
  44. static PFNGLGETPROGRAMLOCALPARAMETERFVARBPROC get_program_local_parameterfv = NULL;
  45. static PFNGLPROGRAMENVPARAMETER4FVARBPROC program_env_parameter4fv = NULL;
  46. static PFNGLGETPROGRAMENVPARAMETERFVARBPROC get_program_env_parameterfv = NULL;
  47. static PFNGLBINDPROGRAMARBPROC bind_program = NULL;
  48. static PFNGLGETPROGRAMIVARBPROC get_program = NULL;
  49. static PFNGLPROGRAMLOCALPARAMETERS4FVEXTPROC program_local_parameters4fv = NULL;
  50. static PFNGLPROGRAMENVPARAMETERS4FVEXTPROC program_env_parameters4fv = NULL;
  51. static int Width = 400;
  52. static int Height = 200;
  53. static const GLfloat Near = 5.0, Far = 25.0;
  54. static void Display( void )
  55. {
  56. }
  57. static void Idle( void )
  58. {
  59. }
  60. static void Visible( int vis )
  61. {
  62. if ( vis == GLUT_VISIBLE ) {
  63. glutIdleFunc( Idle );
  64. }
  65. else {
  66. glutIdleFunc( NULL );
  67. }
  68. }
  69. static void Reshape( int width, int height )
  70. {
  71. GLfloat ar = (float) width / (float) height;
  72. Width = width;
  73. Height = height;
  74. glViewport( 0, 0, width, height );
  75. glMatrixMode( GL_PROJECTION );
  76. glLoadIdentity();
  77. glFrustum( -ar, ar, -1.0, 1.0, Near, Far );
  78. }
  79. static void Key( unsigned char key, int x, int y )
  80. {
  81. (void) x;
  82. (void) y;
  83. switch (key) {
  84. case 27:
  85. exit(0);
  86. break;
  87. }
  88. glutPostRedisplay();
  89. }
  90. static int set_parameter_batch( GLsizei count, GLfloat * param,
  91. const char * name,
  92. PFNGLPROGRAMLOCALPARAMETER4FVARBPROC set_parameter,
  93. PFNGLPROGRAMLOCALPARAMETERS4FVEXTPROC set_parameters,
  94. PFNGLGETPROGRAMLOCALPARAMETERFVARBPROC get_parameter
  95. )
  96. {
  97. unsigned i;
  98. int pass = 1;
  99. for ( i = 0 ; i < (4 * count) ; i++ ) {
  100. param[i] = (GLfloat) random() / (GLfloat) random();
  101. }
  102. /* Try using the "classic" interface.
  103. */
  104. printf("Testing glProgram%sParameter4fvARB (count = %u)...\n", name, count);
  105. for ( i = 0 ; i < count ; i++ ) {
  106. (*set_parameter)(GL_VERTEX_PROGRAM_ARB, i, & param[i * 4]);
  107. }
  108. for ( i = 0 ; i < count ; i++ ) {
  109. GLfloat temp[4];
  110. (*get_parameter)(GL_VERTEX_PROGRAM_ARB, i, temp);
  111. if ( (temp[0] != param[(i * 4) + 0])
  112. || (temp[1] != param[(i * 4) + 1])
  113. || (temp[2] != param[(i * 4) + 2])
  114. || (temp[3] != param[(i * 4) + 3]) ) {
  115. printf("Mismatch in glProgram%sParameter4fvARB index %u!\n", name, i);
  116. printf("Got { %f, %f, %f, %f }, expected { %f, %f, %f, %f }!\n",
  117. temp[0], temp[1],
  118. temp[2], temp[3],
  119. param[(i * 4) + 0], param[(i * 4) + 1],
  120. param[(i * 4) + 2], param[(i * 4) + 3]);
  121. pass = 0;
  122. break;
  123. }
  124. }
  125. if ( set_parameters == NULL ) {
  126. return pass;
  127. }
  128. for ( i = 0 ; i < (4 * count) ; i++ ) {
  129. param[i] = (GLfloat) random() / (GLfloat) random();
  130. }
  131. printf("Testing glProgram%sParameters4fvEXT (count = %u)...\n", name, count);
  132. (*set_parameters)(GL_VERTEX_PROGRAM_ARB, 0, count, param);
  133. for ( i = 0 ; i < count ; i++ ) {
  134. GLfloat temp[4];
  135. (*get_parameter)(GL_VERTEX_PROGRAM_ARB, i, temp);
  136. if ( (temp[0] != param[(i * 4) + 0])
  137. || (temp[1] != param[(i * 4) + 1])
  138. || (temp[2] != param[(i * 4) + 2])
  139. || (temp[3] != param[(i * 4) + 3]) ) {
  140. printf("Mismatch in glProgram%sParameters4fvEXT index %u!\n", name, i);
  141. printf("Got { %f, %f, %f, %f }, expected { %f, %f, %f, %f }!\n",
  142. temp[0], temp[1],
  143. temp[2], temp[3],
  144. param[(i * 4) + 0], param[(i * 4) + 1],
  145. param[(i * 4) + 2], param[(i * 4) + 3]);
  146. pass = 0;
  147. break;
  148. }
  149. }
  150. return pass;
  151. }
  152. static void Init( void )
  153. {
  154. const char * const ver_string = (const char * const)
  155. glGetString( GL_VERSION );
  156. int pass = 1;
  157. GLfloat * params;
  158. GLint max_program_env_parameters;
  159. GLint max_program_local_parameters;
  160. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  161. printf("GL_VERSION = %s\n\n", ver_string);
  162. if ( !glutExtensionSupported("GL_ARB_vertex_program") ) {
  163. printf("Sorry, this program requires GL_ARB_vertex_program\n");
  164. exit(2);
  165. }
  166. program_local_parameter4fv = glutGetProcAddress( "glProgramLocalParameter4fvARB" );
  167. program_env_parameter4fv = glutGetProcAddress( "glProgramEnvParameter4fvARB" );
  168. get_program_local_parameterfv = glutGetProcAddress( "glGetProgramLocalParameterfvARB" );
  169. get_program_env_parameterfv = glutGetProcAddress( "glGetProgramEnvParameterfvARB" );
  170. bind_program = glutGetProcAddress( "glBindProgramARB" );
  171. get_program = glutGetProcAddress( "glGetProgramivARB" );
  172. if ( glutExtensionSupported("GL_EXT_gpu_program_parameters") ) {
  173. printf("GL_EXT_gpu_program_parameters available, testing that path.\n");
  174. program_local_parameters4fv = glutGetProcAddress( "glProgramLocalParameters4fvEXT" );
  175. program_env_parameters4fv = glutGetProcAddress( "glProgramEnvParameters4fvEXT" );
  176. }
  177. else {
  178. printf("GL_EXT_gpu_program_parameters not available.\n");
  179. program_local_parameters4fv = NULL;
  180. program_env_parameters4fv = NULL;
  181. }
  182. /* Since the test sets program local parameters, a program must be bound.
  183. * Program source, however, is not needed.
  184. */
  185. (*bind_program)(GL_VERTEX_PROGRAM_ARB, 1);
  186. (*get_program)(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_ENV_PARAMETERS_ARB,
  187. & max_program_env_parameters);
  188. params = malloc(max_program_env_parameters * 4 * sizeof(GLfloat));
  189. pass &= set_parameter_batch(max_program_env_parameters, params, "Env",
  190. program_env_parameter4fv,
  191. program_env_parameters4fv,
  192. get_program_env_parameterfv);
  193. (*get_program)(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB,
  194. & max_program_local_parameters);
  195. if (max_program_local_parameters > max_program_env_parameters) {
  196. params = realloc(params,
  197. max_program_local_parameters * 4 * sizeof(GLfloat));
  198. }
  199. pass &= set_parameter_batch(max_program_local_parameters, params, "Local",
  200. program_local_parameter4fv,
  201. program_local_parameters4fv,
  202. get_program_local_parameterfv);
  203. free(params);
  204. if (! pass) {
  205. printf("FAIL!\n");
  206. exit(1);
  207. }
  208. printf("PASS!\n");
  209. }
  210. int main( int argc, char *argv[] )
  211. {
  212. glutInit( &argc, argv );
  213. glutInitWindowPosition( 0, 0 );
  214. glutInitWindowSize( Width, Height );
  215. glutInitDisplayMode( GLUT_RGB );
  216. glutCreateWindow( "Program Parameters Test" );
  217. glutReshapeFunc( Reshape );
  218. glutKeyboardFunc( Key );
  219. glutDisplayFunc( Display );
  220. glutVisibilityFunc( Visible );
  221. Init();
  222. return 0;
  223. }