gallium: change remaining util functions to use cso sampler views
[mesa/mesa-lb.git] / progs / tests / bug_3195.c
blob3574c1f7176661102d9aa3a4422a1f0876408a63
1 /*
2 * Copyright (C) 2000 Brian Paul All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 /**
23 * \file bug_3195.c
25 * Simple regression test for bug #3195. A bug in the i180 driver caused
26 * a segfault (inside the driver) when the LOD bias is adjusted and no texture
27 * is enabled. This test, which is based on progs/demos/lodbias.c, sets up
28 * all the texturing, disables all textures, adjusts the LOD bias, then
29 * re-enables \c GL_TEXTURE_2D.
31 * \author Brian Paul
32 * \author Ian Romanick <idr@us.ibm.com>
35 #include <assert.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <math.h>
39 #include <GL/glew.h>
40 #include <GL/glut.h>
42 #include "readtex.h"
44 #define TEXTURE_FILE "../images/girl.rgb"
46 static GLfloat Xrot = 0, Yrot = -30, Zrot = 0;
47 static GLint Bias = 0, BiasStepSign = +1; /* ints avoid fp precision problem */
48 static GLint BiasMin = -400, BiasMax = 400;
52 static void
53 PrintString(const char *s)
55 while (*s) {
56 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
57 s++;
61 static void Idle( void )
63 static int lastTime = 0;
64 int time = glutGet(GLUT_ELAPSED_TIME);
65 int step;
67 if (lastTime == 0)
68 lastTime = time;
69 else if (time - lastTime < 10)
70 return;
72 step = (time - lastTime) / 10 * BiasStepSign;
73 lastTime = time;
75 Bias += step;
76 if (Bias < BiasMin) {
77 exit(0);
79 else if (Bias > BiasMax) {
80 Bias = BiasMax;
81 BiasStepSign = -1;
84 glutPostRedisplay();
88 static void Display( void )
90 char str[100];
92 glClear( GL_COLOR_BUFFER_BIT );
94 glMatrixMode( GL_PROJECTION );
95 glLoadIdentity();
96 glOrtho(-1, 1, -1, 1, -1, 1);
97 glMatrixMode( GL_MODELVIEW );
98 glLoadIdentity();
100 glDisable(GL_TEXTURE_2D);
101 glColor3f(1,1,1);
102 glRasterPos3f(-0.9, -0.9, 0.0);
103 sprintf(str, "Texture LOD Bias = %4.1f", Bias * 0.01);
104 PrintString(str);
106 glMatrixMode( GL_PROJECTION );
107 glLoadIdentity();
108 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
109 glMatrixMode( GL_MODELVIEW );
110 glLoadIdentity();
111 glTranslatef( 0.0, 0.0, -8.0 );
113 glPushMatrix();
114 glRotatef(Xrot, 1, 0, 0);
115 glRotatef(Yrot, 0, 1, 0);
116 glRotatef(Zrot, 0, 0, 1);
118 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0.01 * Bias);
119 glEnable(GL_TEXTURE_2D);
121 glBegin(GL_POLYGON);
122 glTexCoord2f(0, 0); glVertex2f(-1, -1);
123 glTexCoord2f(2, 0); glVertex2f( 1, -1);
124 glTexCoord2f(2, 2); glVertex2f( 1, 1);
125 glTexCoord2f(0, 2); glVertex2f(-1, 1);
126 glEnd();
128 glPopMatrix();
130 glutSwapBuffers();
134 static void Reshape( int width, int height )
136 glViewport( 0, 0, width, height );
140 static void Key( unsigned char key, int x, int y )
142 (void) x;
143 (void) y;
144 switch (key) {
145 case 27:
146 exit(0);
147 break;
149 glutPostRedisplay();
153 static void SpecialKey( int key, int x, int y )
155 const GLfloat step = 3.0;
156 (void) x;
157 (void) y;
158 switch (key) {
159 case GLUT_KEY_UP:
160 Xrot -= step;
161 break;
162 case GLUT_KEY_DOWN:
163 Xrot += step;
164 break;
165 case GLUT_KEY_LEFT:
166 Yrot -= step;
167 break;
168 case GLUT_KEY_RIGHT:
169 Yrot += step;
170 break;
172 glutPostRedisplay();
176 static void Init( void )
178 GLfloat maxBias;
179 const char * const ver_string = (const char * const)
180 glGetString( GL_VERSION );
182 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
183 printf("GL_VERSION = %s\n", ver_string);
185 printf("\nThis program should function nearly identically to Mesa's lodbias demo.\n"
186 "It should cycle through the complet LOD bias range once and exit. If bug\n"
187 "#3195 still exists, the demo should crash almost immediatly.\n");
188 printf("This is a regression test for bug #3195.\n");
189 printf("https://bugs.freedesktop.org/show_bug.cgi?id=3195\n");
191 if (!glutExtensionSupported("GL_EXT_texture_lod_bias")) {
192 printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
193 exit(1);
196 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
198 if (glutExtensionSupported("GL_SGIS_generate_mipmap")) {
199 /* test auto mipmap generation */
200 GLint width, height, i;
201 GLenum format;
202 GLubyte *image = LoadRGBImage(TEXTURE_FILE, &width, &height, &format);
203 if (!image) {
204 printf("Error: could not load texture image %s\n", TEXTURE_FILE);
205 exit(1);
207 /* resize to 256 x 256 */
208 if (width != 256 || height != 256) {
209 GLubyte *newImage = malloc(256 * 256 * 4);
210 gluScaleImage(format, width, height, GL_UNSIGNED_BYTE, image,
211 256, 256, GL_UNSIGNED_BYTE, newImage);
212 free(image);
213 image = newImage;
215 printf("Using GL_SGIS_generate_mipmap\n");
216 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
217 glTexImage2D(GL_TEXTURE_2D, 0, format, 256, 256, 0,
218 format, GL_UNSIGNED_BYTE, image);
219 free(image);
221 /* make sure mipmap was really generated correctly */
222 width = height = 256;
223 for (i = 0; i < 9; i++) {
224 GLint w, h;
225 glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_WIDTH, &w);
226 glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_HEIGHT, &h);
227 printf("Level %d size: %d x %d\n", i, w, h);
228 assert(w == width);
229 assert(h == height);
230 width /= 2;
231 height /= 2;
235 else if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
236 printf("Error: could not load texture image %s\n", TEXTURE_FILE);
237 exit(1);
240 /* mipmapping required for this extension */
241 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
242 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
243 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
245 glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS_EXT, &maxBias);
246 printf("LOD bias range: [%g, %g]\n", -maxBias, maxBias);
247 BiasMin = -100 * maxBias;
248 BiasMax = 100 * maxBias;
250 /* Since we have (about) 8 mipmap levels, no need to bias beyond
251 * the range [-1, +8].
253 if (BiasMin < -100)
254 BiasMin = -100;
255 if (BiasMax > 800)
256 BiasMax = 800;
260 int main( int argc, char *argv[] )
262 glutInit( &argc, argv );
263 glutInitWindowPosition( 0, 0 );
264 glutInitWindowSize( 350, 350 );
265 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
266 glutCreateWindow( "Bug #3195 Test" );
267 glewInit();
268 glutReshapeFunc( Reshape );
269 glutKeyboardFunc( Key );
270 glutSpecialFunc( SpecialKey );
271 glutDisplayFunc( Display );
272 glutIdleFunc(Idle);
273 Init();
274 glutMainLoop();
275 return 0;