progs/tests: also test stencil INCR_WRAP mode if supported
[mesa.git] / progs / tests / prog_parameter.c
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>
38 #include <GL/glew.h>
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) rand() / (GLfloat) rand();
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) rand() / (GLfloat) rand();
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 int i;
196
197
198 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
199 printf("GL_VERSION = %s\n\n", ver_string);
200
201 if ( !glutExtensionSupported("GL_ARB_vertex_program") ) {
202 printf("Sorry, this program requires GL_ARB_vertex_program\n");
203 exit(2);
204 }
205
206
207 program_local_parameter4fv = (PFNGLPROGRAMLOCALPARAMETER4FVARBPROC) glutGetProcAddress( "glProgramLocalParameter4fvARB" );
208 program_env_parameter4fv = (PFNGLPROGRAMENVPARAMETER4FVARBPROC) glutGetProcAddress( "glProgramEnvParameter4fvARB" );
209
210 get_program_local_parameterfv = (PFNGLGETPROGRAMLOCALPARAMETERFVARBPROC) glutGetProcAddress( "glGetProgramLocalParameterfvARB" );
211 get_program_env_parameterfv = (PFNGLGETPROGRAMENVPARAMETERFVARBPROC) glutGetProcAddress( "glGetProgramEnvParameterfvARB" );
212
213 bind_program = (PFNGLBINDPROGRAMARBPROC) glutGetProcAddress( "glBindProgramARB" );
214 get_program = (PFNGLGETPROGRAMIVARBPROC) glutGetProcAddress( "glGetProgramivARB" );
215
216 if ( glutExtensionSupported("GL_EXT_gpu_program_parameters") ) {
217 printf("GL_EXT_gpu_program_parameters available, testing that path.\n");
218
219 program_local_parameters4fv = (PFNGLPROGRAMLOCALPARAMETERS4FVEXTPROC) glutGetProcAddress( "glProgramLocalParameters4fvEXT" );
220 program_env_parameters4fv = (PFNGLPROGRAMENVPARAMETERS4FVEXTPROC) glutGetProcAddress( "glProgramEnvParameters4fvEXT" );
221 }
222 else {
223 printf("GL_EXT_gpu_program_parameters not available.\n");
224
225 program_local_parameters4fv = NULL;
226 program_env_parameters4fv = NULL;
227 }
228
229
230
231 /* Since the test sets program local parameters, a program must be bound.
232 * Program source, however, is not needed.
233 */
234 (*bind_program)(GL_VERTEX_PROGRAM_ARB, 1);
235
236
237 (*get_program)(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_ENV_PARAMETERS_ARB,
238 & max_program_env_parameters);
239
240 params = malloc(max_program_env_parameters * 4 * sizeof(GLfloat));
241
242 for (i = 0; i < max_program_env_parameters * 4; i++) {
243 params[i] = 0.0F;
244 }
245
246 pass &= set_parameter_batch(max_program_env_parameters, params, "Env",
247 program_env_parameter4fv,
248 program_env_parameters4fv,
249 get_program_env_parameterfv);
250
251
252 (*get_program)(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB,
253 & max_program_local_parameters);
254
255 if (max_program_local_parameters > max_program_env_parameters) {
256 params = realloc(params,
257 max_program_local_parameters * 4 * sizeof(GLfloat));
258 }
259
260 pass &= set_parameter_batch(max_program_local_parameters, params, "Local",
261 program_local_parameter4fv,
262 program_local_parameters4fv,
263 get_program_local_parameterfv);
264
265 free(params);
266
267 if (! pass) {
268 printf("FAIL!\n");
269 exit(1);
270 }
271
272 printf("PASS!\n");
273 }
274
275
276 int main( int argc, char *argv[] )
277 {
278 glutInit( &argc, argv );
279 glutInitWindowPosition( 0, 0 );
280 glutInitWindowSize( Width, Height );
281 glutInitDisplayMode( GLUT_RGB );
282 glutCreateWindow( "Program Parameters Test" );
283 glewInit();
284 glutReshapeFunc( Reshape );
285 glutKeyboardFunc( Key );
286 glutDisplayFunc( Display );
287 glutVisibilityFunc( Visible );
288
289 Init();
290
291 return 0;
292 }