gallium: change remaining util functions to use cso sampler views
[mesa/mesa-lb.git] / progs / tests / prog_parameter.c
blob2de7e2994af994d5ab350f784098fe03ad2035bb
1 /*
2 * (C) Copyright IBM Corporation 2006
3 * All Rights Reserved.
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:
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.
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.
25 /**
26 * \file prog_parameter.c
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.
32 * \author Ian Romanick <idr@us.ibm.com>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <math.h>
38 #include <GL/glew.h>
39 #include <GL/glut.h>
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
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;
55 static PFNGLPROGRAMLOCALPARAMETERS4FVEXTPROC program_local_parameters4fv = NULL;
56 static PFNGLPROGRAMENVPARAMETERS4FVEXTPROC program_env_parameters4fv = NULL;
58 static int Width = 400;
59 static int Height = 200;
60 static const GLfloat Near = 5.0, Far = 25.0;
63 static void Display( void )
68 static void Idle( void )
73 static void Visible( int vis )
75 if ( vis == GLUT_VISIBLE ) {
76 glutIdleFunc( Idle );
78 else {
79 glutIdleFunc( NULL );
82 static void Reshape( int width, int height )
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 );
94 static void Key( unsigned char key, int x, int y )
96 (void) x;
97 (void) y;
98 switch (key) {
99 case 27:
100 exit(0);
101 break;
103 glutPostRedisplay();
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
114 unsigned i;
115 int pass = 1;
118 for ( i = 0 ; i < (4 * count) ; i++ ) {
119 param[i] = (GLfloat) rand() / (GLfloat) rand();
122 /* Try using the "classic" interface.
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]);
129 for ( i = 0 ; i < count ; i++ ) {
130 GLfloat temp[4];
132 (*get_parameter)(GL_VERTEX_PROGRAM_ARB, i, temp);
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;
150 if ( set_parameters == NULL ) {
151 return pass;
155 for ( i = 0 ; i < (4 * count) ; i++ ) {
156 param[i] = (GLfloat) rand() / (GLfloat) rand();
159 printf("Testing glProgram%sParameters4fvEXT (count = %u)...\n", name, count);
160 (*set_parameters)(GL_VERTEX_PROGRAM_ARB, 0, count, param);
162 for ( i = 0 ; i < count ; i++ ) {
163 GLfloat temp[4];
165 (*get_parameter)(GL_VERTEX_PROGRAM_ARB, i, temp);
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;
183 return pass;
187 static void Init( void )
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;
198 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
199 printf("GL_VERSION = %s\n\n", ver_string);
201 if ( !glutExtensionSupported("GL_ARB_vertex_program") ) {
202 printf("Sorry, this program requires GL_ARB_vertex_program\n");
203 exit(2);
207 program_local_parameter4fv = (PFNGLPROGRAMLOCALPARAMETER4FVARBPROC) glutGetProcAddress( "glProgramLocalParameter4fvARB" );
208 program_env_parameter4fv = (PFNGLPROGRAMENVPARAMETER4FVARBPROC) glutGetProcAddress( "glProgramEnvParameter4fvARB" );
210 get_program_local_parameterfv = (PFNGLGETPROGRAMLOCALPARAMETERFVARBPROC) glutGetProcAddress( "glGetProgramLocalParameterfvARB" );
211 get_program_env_parameterfv = (PFNGLGETPROGRAMENVPARAMETERFVARBPROC) glutGetProcAddress( "glGetProgramEnvParameterfvARB" );
213 bind_program = (PFNGLBINDPROGRAMARBPROC) glutGetProcAddress( "glBindProgramARB" );
214 get_program = (PFNGLGETPROGRAMIVARBPROC) glutGetProcAddress( "glGetProgramivARB" );
216 if ( glutExtensionSupported("GL_EXT_gpu_program_parameters") ) {
217 printf("GL_EXT_gpu_program_parameters available, testing that path.\n");
219 program_local_parameters4fv = (PFNGLPROGRAMLOCALPARAMETERS4FVEXTPROC) glutGetProcAddress( "glProgramLocalParameters4fvEXT" );
220 program_env_parameters4fv = (PFNGLPROGRAMENVPARAMETERS4FVEXTPROC) glutGetProcAddress( "glProgramEnvParameters4fvEXT" );
222 else {
223 printf("GL_EXT_gpu_program_parameters not available.\n");
225 program_local_parameters4fv = NULL;
226 program_env_parameters4fv = NULL;
231 /* Since the test sets program local parameters, a program must be bound.
232 * Program source, however, is not needed.
234 (*bind_program)(GL_VERTEX_PROGRAM_ARB, 1);
237 (*get_program)(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_ENV_PARAMETERS_ARB,
238 & max_program_env_parameters);
240 params = malloc(max_program_env_parameters * 4 * sizeof(GLfloat));
242 for (i = 0; i < max_program_env_parameters * 4; i++) {
243 params[i] = 0.0F;
246 pass &= set_parameter_batch(max_program_env_parameters, params, "Env",
247 program_env_parameter4fv,
248 program_env_parameters4fv,
249 get_program_env_parameterfv);
252 (*get_program)(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB,
253 & max_program_local_parameters);
255 if (max_program_local_parameters > max_program_env_parameters) {
256 params = realloc(params,
257 max_program_local_parameters * 4 * sizeof(GLfloat));
260 pass &= set_parameter_batch(max_program_local_parameters, params, "Local",
261 program_local_parameter4fv,
262 program_local_parameters4fv,
263 get_program_local_parameterfv);
265 free(params);
267 if (! pass) {
268 printf("FAIL!\n");
269 exit(1);
272 printf("PASS!\n");
276 int main( int argc, char *argv[] )
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 );
289 Init();
291 return 0;