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