summaryrefslogtreecommitdiffstats
path: root/progs/tests/prog_parameter.c
blob: 6dd956c4023570aa143b2c5e16de6be88ddf916c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * (C) Copyright IBM Corporation 2006
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

/**
 * \file prog_parameter.c
 * 
 * Test various aspects of setting (and getting) low-level program parameters.
 * This is primarilly intended as a test for GL_EXT_gpu_program_parameters,
 * but it turns out that it hits some other functionality along the way.
 * 
 * \author Ian Romanick <idr@us.ibm.com>
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glew.h>
#include <GL/glut.h>

#ifndef GL_EXT_gpu_program_parameters
typedef void (APIENTRYP PFNGLPROGRAMLOCALPARAMETERS4FVEXTPROC)(GLenum,
    GLuint, GLsizei, const GLfloat *);
typedef void (APIENTRYP PFNGLPROGRAMENVPARAMETERS4FVEXTPROC)(GLenum,
    GLuint, GLsizei, const GLfloat *);
#endif

static PFNGLPROGRAMLOCALPARAMETER4FVARBPROC program_local_parameter4fv = NULL;
static PFNGLGETPROGRAMLOCALPARAMETERFVARBPROC get_program_local_parameterfv = NULL;
static PFNGLPROGRAMENVPARAMETER4FVARBPROC program_env_parameter4fv = NULL;
static PFNGLGETPROGRAMENVPARAMETERFVARBPROC get_program_env_parameterfv = NULL;
static PFNGLBINDPROGRAMARBPROC bind_program = NULL;
static PFNGLGETPROGRAMIVARBPROC get_program = NULL;

static PFNGLPROGRAMLOCALPARAMETERS4FVEXTPROC program_local_parameters4fv = NULL;
static PFNGLPROGRAMENVPARAMETERS4FVEXTPROC program_env_parameters4fv = NULL;

static int Width = 400;
static int Height = 200;
static const GLfloat Near = 5.0, Far = 25.0;


static void Display( void )
{
}


static void Idle( void )
{
}


static void Visible( int vis )
{
   if ( vis == GLUT_VISIBLE ) {
      glutIdleFunc( Idle );
   }
   else {
      glutIdleFunc( NULL );
   }
}
static void Reshape( int width, int height )
{
   GLfloat ar = (float) width / (float) height;
   Width = width;
   Height = height;
   glViewport( 0, 0, width, height );
   glMatrixMode( GL_PROJECTION );
   glLoadIdentity();
   glFrustum( -ar, ar, -1.0, 1.0, Near, Far );
}


static void Key( unsigned char key, int x, int y )
{
   (void) x;
   (void) y;
   switch (key) {
      case 27:
         exit(0);
         break;
   }
   glutPostRedisplay();
}


static int set_parameter_batch( GLsizei count, GLfloat * param,
				const char * name,
				PFNGLPROGRAMLOCALPARAMETER4FVARBPROC set_parameter,
				PFNGLPROGRAMLOCALPARAMETERS4FVEXTPROC set_parameters,
				PFNGLGETPROGRAMLOCALPARAMETERFVARBPROC get_parameter
				)
{
   unsigned i;
   int pass = 1;


   for ( i = 0 ; i < (4 * count) ; i++ ) {
      param[i] = (GLfloat) random() / (GLfloat) random();
   }

   /* Try using the "classic" interface.
    */
   printf("Testing glProgram%sParameter4fvARB (count = %u)...\n", name, count);
   for ( i = 0 ; i < count ; i++ ) {
      (*set_parameter)(GL_VERTEX_PROGRAM_ARB, i, & param[i * 4]);
   }

   for ( i = 0 ; i < count ; i++ ) {
      GLfloat temp[4];

      (*get_parameter)(GL_VERTEX_PROGRAM_ARB, i, temp);

      if ( (temp[0] != param[(i * 4) + 0])
	   || (temp[1] != param[(i * 4) + 1])
	   || (temp[2] != param[(i * 4) + 2])
	   || (temp[3] != param[(i * 4) + 3]) ) {
	 printf("Mismatch in glProgram%sParameter4fvARB index %u!\n", name, i);
	 printf("Got { %f, %f, %f, %f }, expected { %f, %f, %f, %f }!\n",
		temp[0],            temp[1],
		temp[2],            temp[3],
		param[(i * 4) + 0], param[(i * 4) + 1],
		param[(i * 4) + 2], param[(i * 4) + 3]);
	 pass = 0;
	 break;
      }
   }


   if ( set_parameters == NULL ) {
      return pass;
   }


   for ( i = 0 ; i < (4 * count) ; i++ ) {
      param[i] = (GLfloat) random() / (GLfloat) random();
   }

   printf("Testing glProgram%sParameters4fvEXT (count = %u)...\n", name, count);
   (*set_parameters)(GL_VERTEX_PROGRAM_ARB, 0, count, param);

   for ( i = 0 ; i < count ; i++ ) {
      GLfloat temp[4];

      (*get_parameter)(GL_VERTEX_PROGRAM_ARB, i, temp);

      if ( (temp[0] != param[(i * 4) + 0])
	   || (temp[1] != param[(i * 4) + 1])
	   || (temp[2] != param[(i * 4) + 2])
	   || (temp[3] != param[(i * 4) + 3]) ) {
	 printf("Mismatch in glProgram%sParameters4fvEXT index %u!\n", name, i);
	 printf("Got { %f, %f, %f, %f }, expected { %f, %f, %f, %f }!\n",
		temp[0],            temp[1],
		temp[2],            temp[3],
		param[(i * 4) + 0], param[(i * 4) + 1],
		param[(i * 4) + 2], param[(i * 4) + 3]);
	 pass = 0;
	 break;
      }
   }


   return pass;
}


static void Init( void )
{
   const char * const ver_string = (const char * const)
       glGetString( GL_VERSION );
   int pass = 1;
   GLfloat * params;
   GLint max_program_env_parameters;
   GLint max_program_local_parameters;


   printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
   printf("GL_VERSION = %s\n\n", ver_string);

   if ( !glutExtensionSupported("GL_ARB_vertex_program") ) {
      printf("Sorry, this program requires GL_ARB_vertex_program\n");
      exit(2);
   }


   program_local_parameter4fv = glutGetProcAddress( "glProgramLocalParameter4fvARB" );
   program_env_parameter4fv = glutGetProcAddress( "glProgramEnvParameter4fvARB" );

   get_program_local_parameterfv = glutGetProcAddress( "glGetProgramLocalParameterfvARB" );
   get_program_env_parameterfv = glutGetProcAddress( "glGetProgramEnvParameterfvARB" );

   bind_program = glutGetProcAddress( "glBindProgramARB" );
   get_program = glutGetProcAddress( "glGetProgramivARB" );

   if ( glutExtensionSupported("GL_EXT_gpu_program_parameters") ) {
      printf("GL_EXT_gpu_program_parameters available, testing that path.\n");

      program_local_parameters4fv = glutGetProcAddress( "glProgramLocalParameters4fvEXT" );
      program_env_parameters4fv = glutGetProcAddress( "glProgramEnvParameters4fvEXT" );
   }
   else {
      printf("GL_EXT_gpu_program_parameters not available.\n");

      program_local_parameters4fv = NULL;
      program_env_parameters4fv = NULL;
   }



   /* Since the test sets program local parameters, a program must be bound.
    * Program source, however, is not needed.
    */
   (*bind_program)(GL_VERTEX_PROGRAM_ARB, 1);


   (*get_program)(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_ENV_PARAMETERS_ARB,
		  & max_program_env_parameters);

   params = malloc(max_program_env_parameters * 4 * sizeof(GLfloat));

   pass &= set_parameter_batch(max_program_env_parameters, params, "Env",
			       program_env_parameter4fv,
			       program_env_parameters4fv,
			       get_program_env_parameterfv);


   (*get_program)(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB,
		  & max_program_local_parameters);

   if (max_program_local_parameters > max_program_env_parameters) {
      params = realloc(params,
		       max_program_local_parameters * 4 * sizeof(GLfloat));
   }

   pass &= set_parameter_batch(max_program_local_parameters, params, "Local",
			       program_local_parameter4fv,
			       program_local_parameters4fv,
			       get_program_local_parameterfv);

   free(params);

   if (! pass) {
      printf("FAIL!\n");
      exit(1);
   }
   
   printf("PASS!\n");
}


int main( int argc, char *argv[] )
{
   glutInit( &argc, argv );
   glutInitWindowPosition( 0, 0 );
   glutInitWindowSize( Width, Height );
   glutInitDisplayMode( GLUT_RGB );
   glutCreateWindow( "Program Parameters Test" );
   glewInit();
   glutReshapeFunc( Reshape );
   glutKeyboardFunc( Key );
   glutDisplayFunc( Display );
   glutVisibilityFunc( Visible );

   Init();

   return 0;
}