Ian Romanick | 5208d93 | 2006-08-15 16:44:32 +0000 | [diff] [blame] | 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 | /** |
| 26 | * \file prog_parameter.c |
| 27 | * |
| 28 | * Test various aspects of setting (and getting) low-level program parameters. |
| 29 | * This is primarilly intended as a test for GL_EXT_gpu_program_parameters, |
| 30 | * but it turns out that it hits some other functionality along the way. |
| 31 | * |
| 32 | * \author Ian Romanick <idr@us.ibm.com> |
| 33 | */ |
| 34 | |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <math.h> |
Keith Whitwell | a58065d | 2009-03-10 13:11:23 +0000 | [diff] [blame^] | 38 | #include <GL/glew.h> |
Ian Romanick | 5208d93 | 2006-08-15 16:44:32 +0000 | [diff] [blame] | 39 | #include <GL/glut.h> |
| 40 | |
| 41 | #ifndef GL_EXT_gpu_program_parameters |
| 42 | typedef void (APIENTRYP PFNGLPROGRAMLOCALPARAMETERS4FVEXTPROC)(GLenum, |
| 43 | GLuint, GLsizei, const GLfloat *); |
| 44 | typedef void (APIENTRYP PFNGLPROGRAMENVPARAMETERS4FVEXTPROC)(GLenum, |
| 45 | GLuint, GLsizei, const GLfloat *); |
| 46 | #endif |
| 47 | |
| 48 | static PFNGLPROGRAMLOCALPARAMETER4FVARBPROC program_local_parameter4fv = NULL; |
| 49 | static PFNGLGETPROGRAMLOCALPARAMETERFVARBPROC get_program_local_parameterfv = NULL; |
| 50 | static PFNGLPROGRAMENVPARAMETER4FVARBPROC program_env_parameter4fv = NULL; |
| 51 | static PFNGLGETPROGRAMENVPARAMETERFVARBPROC get_program_env_parameterfv = NULL; |
| 52 | static PFNGLBINDPROGRAMARBPROC bind_program = NULL; |
| 53 | static PFNGLGETPROGRAMIVARBPROC get_program = NULL; |
| 54 | |
| 55 | static PFNGLPROGRAMLOCALPARAMETERS4FVEXTPROC program_local_parameters4fv = NULL; |
| 56 | static PFNGLPROGRAMENVPARAMETERS4FVEXTPROC program_env_parameters4fv = NULL; |
| 57 | |
| 58 | static int Width = 400; |
| 59 | static int Height = 200; |
| 60 | static const GLfloat Near = 5.0, Far = 25.0; |
| 61 | |
| 62 | |
| 63 | static void Display( void ) |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | |
| 68 | static void Idle( void ) |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | |
| 73 | static void Visible( int vis ) |
| 74 | { |
| 75 | if ( vis == GLUT_VISIBLE ) { |
| 76 | glutIdleFunc( Idle ); |
| 77 | } |
| 78 | else { |
| 79 | glutIdleFunc( NULL ); |
| 80 | } |
| 81 | } |
| 82 | static void Reshape( int width, int height ) |
| 83 | { |
| 84 | GLfloat ar = (float) width / (float) height; |
| 85 | Width = width; |
| 86 | Height = height; |
| 87 | glViewport( 0, 0, width, height ); |
| 88 | glMatrixMode( GL_PROJECTION ); |
| 89 | glLoadIdentity(); |
| 90 | glFrustum( -ar, ar, -1.0, 1.0, Near, Far ); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | static void Key( unsigned char key, int x, int y ) |
| 95 | { |
| 96 | (void) x; |
| 97 | (void) y; |
| 98 | switch (key) { |
| 99 | case 27: |
| 100 | exit(0); |
| 101 | break; |
| 102 | } |
| 103 | glutPostRedisplay(); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | static int set_parameter_batch( GLsizei count, GLfloat * param, |
| 108 | const char * name, |
| 109 | PFNGLPROGRAMLOCALPARAMETER4FVARBPROC set_parameter, |
| 110 | PFNGLPROGRAMLOCALPARAMETERS4FVEXTPROC set_parameters, |
| 111 | PFNGLGETPROGRAMLOCALPARAMETERFVARBPROC get_parameter |
| 112 | ) |
| 113 | { |
| 114 | unsigned i; |
| 115 | int pass = 1; |
| 116 | |
| 117 | |
| 118 | for ( i = 0 ; i < (4 * count) ; i++ ) { |
| 119 | param[i] = (GLfloat) random() / (GLfloat) random(); |
| 120 | } |
| 121 | |
| 122 | /* Try using the "classic" interface. |
| 123 | */ |
| 124 | printf("Testing glProgram%sParameter4fvARB (count = %u)...\n", name, count); |
| 125 | for ( i = 0 ; i < count ; i++ ) { |
| 126 | (*set_parameter)(GL_VERTEX_PROGRAM_ARB, i, & param[i * 4]); |
| 127 | } |
| 128 | |
| 129 | for ( i = 0 ; i < count ; i++ ) { |
| 130 | GLfloat temp[4]; |
| 131 | |
| 132 | (*get_parameter)(GL_VERTEX_PROGRAM_ARB, i, temp); |
| 133 | |
| 134 | if ( (temp[0] != param[(i * 4) + 0]) |
| 135 | || (temp[1] != param[(i * 4) + 1]) |
| 136 | || (temp[2] != param[(i * 4) + 2]) |
| 137 | || (temp[3] != param[(i * 4) + 3]) ) { |
| 138 | printf("Mismatch in glProgram%sParameter4fvARB index %u!\n", name, i); |
| 139 | printf("Got { %f, %f, %f, %f }, expected { %f, %f, %f, %f }!\n", |
| 140 | temp[0], temp[1], |
| 141 | temp[2], temp[3], |
| 142 | param[(i * 4) + 0], param[(i * 4) + 1], |
| 143 | param[(i * 4) + 2], param[(i * 4) + 3]); |
| 144 | pass = 0; |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | |
| 150 | if ( set_parameters == NULL ) { |
| 151 | return pass; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | for ( i = 0 ; i < (4 * count) ; i++ ) { |
| 156 | param[i] = (GLfloat) random() / (GLfloat) random(); |
| 157 | } |
| 158 | |
| 159 | printf("Testing glProgram%sParameters4fvEXT (count = %u)...\n", name, count); |
| 160 | (*set_parameters)(GL_VERTEX_PROGRAM_ARB, 0, count, param); |
| 161 | |
| 162 | for ( i = 0 ; i < count ; i++ ) { |
| 163 | GLfloat temp[4]; |
| 164 | |
| 165 | (*get_parameter)(GL_VERTEX_PROGRAM_ARB, i, temp); |
| 166 | |
| 167 | if ( (temp[0] != param[(i * 4) + 0]) |
| 168 | || (temp[1] != param[(i * 4) + 1]) |
| 169 | || (temp[2] != param[(i * 4) + 2]) |
| 170 | || (temp[3] != param[(i * 4) + 3]) ) { |
| 171 | printf("Mismatch in glProgram%sParameters4fvEXT index %u!\n", name, i); |
| 172 | printf("Got { %f, %f, %f, %f }, expected { %f, %f, %f, %f }!\n", |
| 173 | temp[0], temp[1], |
| 174 | temp[2], temp[3], |
| 175 | param[(i * 4) + 0], param[(i * 4) + 1], |
| 176 | param[(i * 4) + 2], param[(i * 4) + 3]); |
| 177 | pass = 0; |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | |
| 183 | return pass; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | static void Init( void ) |
| 188 | { |
| 189 | const char * const ver_string = (const char * const) |
| 190 | glGetString( GL_VERSION ); |
| 191 | int pass = 1; |
| 192 | GLfloat * params; |
| 193 | GLint max_program_env_parameters; |
| 194 | GLint max_program_local_parameters; |
| 195 | |
| 196 | |
| 197 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 198 | printf("GL_VERSION = %s\n\n", ver_string); |
| 199 | |
| 200 | if ( !glutExtensionSupported("GL_ARB_vertex_program") ) { |
| 201 | printf("Sorry, this program requires GL_ARB_vertex_program\n"); |
| 202 | exit(2); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | program_local_parameter4fv = glutGetProcAddress( "glProgramLocalParameter4fvARB" ); |
| 207 | program_env_parameter4fv = glutGetProcAddress( "glProgramEnvParameter4fvARB" ); |
| 208 | |
| 209 | get_program_local_parameterfv = glutGetProcAddress( "glGetProgramLocalParameterfvARB" ); |
| 210 | get_program_env_parameterfv = glutGetProcAddress( "glGetProgramEnvParameterfvARB" ); |
| 211 | |
| 212 | bind_program = glutGetProcAddress( "glBindProgramARB" ); |
| 213 | get_program = glutGetProcAddress( "glGetProgramivARB" ); |
| 214 | |
| 215 | if ( glutExtensionSupported("GL_EXT_gpu_program_parameters") ) { |
| 216 | printf("GL_EXT_gpu_program_parameters available, testing that path.\n"); |
| 217 | |
| 218 | program_local_parameters4fv = glutGetProcAddress( "glProgramLocalParameters4fvEXT" ); |
| 219 | program_env_parameters4fv = glutGetProcAddress( "glProgramEnvParameters4fvEXT" ); |
| 220 | } |
| 221 | else { |
| 222 | printf("GL_EXT_gpu_program_parameters not available.\n"); |
| 223 | |
| 224 | program_local_parameters4fv = NULL; |
| 225 | program_env_parameters4fv = NULL; |
| 226 | } |
| 227 | |
| 228 | |
| 229 | |
| 230 | /* Since the test sets program local parameters, a program must be bound. |
| 231 | * Program source, however, is not needed. |
| 232 | */ |
| 233 | (*bind_program)(GL_VERTEX_PROGRAM_ARB, 1); |
| 234 | |
| 235 | |
| 236 | (*get_program)(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_ENV_PARAMETERS_ARB, |
| 237 | & max_program_env_parameters); |
| 238 | |
| 239 | params = malloc(max_program_env_parameters * 4 * sizeof(GLfloat)); |
| 240 | |
| 241 | pass &= set_parameter_batch(max_program_env_parameters, params, "Env", |
| 242 | program_env_parameter4fv, |
| 243 | program_env_parameters4fv, |
| 244 | get_program_env_parameterfv); |
| 245 | |
| 246 | |
| 247 | (*get_program)(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB, |
| 248 | & max_program_local_parameters); |
| 249 | |
| 250 | if (max_program_local_parameters > max_program_env_parameters) { |
| 251 | params = realloc(params, |
| 252 | max_program_local_parameters * 4 * sizeof(GLfloat)); |
| 253 | } |
| 254 | |
| 255 | pass &= set_parameter_batch(max_program_local_parameters, params, "Local", |
| 256 | program_local_parameter4fv, |
| 257 | program_local_parameters4fv, |
| 258 | get_program_local_parameterfv); |
| 259 | |
| 260 | free(params); |
| 261 | |
| 262 | if (! pass) { |
| 263 | printf("FAIL!\n"); |
| 264 | exit(1); |
| 265 | } |
| 266 | |
| 267 | printf("PASS!\n"); |
| 268 | } |
| 269 | |
| 270 | |
| 271 | int main( int argc, char *argv[] ) |
| 272 | { |
| 273 | glutInit( &argc, argv ); |
| 274 | glutInitWindowPosition( 0, 0 ); |
| 275 | glutInitWindowSize( Width, Height ); |
| 276 | glutInitDisplayMode( GLUT_RGB ); |
| 277 | glutCreateWindow( "Program Parameters Test" ); |
Keith Whitwell | a58065d | 2009-03-10 13:11:23 +0000 | [diff] [blame^] | 278 | glewInit(); |
Ian Romanick | 5208d93 | 2006-08-15 16:44:32 +0000 | [diff] [blame] | 279 | glutReshapeFunc( Reshape ); |
| 280 | glutKeyboardFunc( Key ); |
| 281 | glutDisplayFunc( Display ); |
| 282 | glutVisibilityFunc( Visible ); |
| 283 | |
| 284 | Init(); |
| 285 | |
| 286 | return 0; |
| 287 | } |