Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bug_3195.c 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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:
  10. *
  11. * The above copyright notice and this permission notice shall be included
  12. * in all copies or substantial portions of the Software.
  13. *
  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.
  20. */
  21. /**
  22. * \file bug_3195.c
  23. *
  24. * Simple regression test for bug #3195. A bug in the i180 driver caused
  25. * a segfault (inside the driver) when the LOD bias is adjusted and no texture
  26. * is enabled. This test, which is based on progs/demos/lodbias.c, sets up
  27. * all the texturing, disables all textures, adjusts the LOD bias, then
  28. * re-enables \c GL_TEXTURE_2D.
  29. *
  30. * \author Brian Paul
  31. * \author Ian Romanick <idr@us.ibm.com>
  32. */
  33. #include <assert.h>
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <math.h>
  37. #include <GL/glew.h>
  38. #include <GL/glut.h>
  39. #include <GL/glext.h>
  40. #include "readtex.h"
  41. #define TEXTURE_FILE "../images/girl.rgb"
  42. static GLfloat Xrot = 0, Yrot = -30, Zrot = 0;
  43. static GLint Bias = 0, BiasStepSign = +1; /* ints avoid fp precision problem */
  44. static GLint BiasMin = -400, BiasMax = 400;
  45. static void
  46. PrintString(const char *s)
  47. {
  48. while (*s) {
  49. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  50. s++;
  51. }
  52. }
  53. static void Idle( void )
  54. {
  55. static int lastTime = 0;
  56. int time = glutGet(GLUT_ELAPSED_TIME);
  57. int step;
  58. if (lastTime == 0)
  59. lastTime = time;
  60. else if (time - lastTime < 10)
  61. return;
  62. step = (time - lastTime) / 10 * BiasStepSign;
  63. lastTime = time;
  64. Bias += step;
  65. if (Bias < BiasMin) {
  66. exit(0);
  67. }
  68. else if (Bias > BiasMax) {
  69. Bias = BiasMax;
  70. BiasStepSign = -1;
  71. }
  72. glutPostRedisplay();
  73. }
  74. static void Display( void )
  75. {
  76. char str[100];
  77. glClear( GL_COLOR_BUFFER_BIT );
  78. glMatrixMode( GL_PROJECTION );
  79. glLoadIdentity();
  80. glOrtho(-1, 1, -1, 1, -1, 1);
  81. glMatrixMode( GL_MODELVIEW );
  82. glLoadIdentity();
  83. glDisable(GL_TEXTURE_2D);
  84. glColor3f(1,1,1);
  85. glRasterPos3f(-0.9, -0.9, 0.0);
  86. sprintf(str, "Texture LOD Bias = %4.1f", Bias * 0.01);
  87. PrintString(str);
  88. glMatrixMode( GL_PROJECTION );
  89. glLoadIdentity();
  90. glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
  91. glMatrixMode( GL_MODELVIEW );
  92. glLoadIdentity();
  93. glTranslatef( 0.0, 0.0, -8.0 );
  94. glPushMatrix();
  95. glRotatef(Xrot, 1, 0, 0);
  96. glRotatef(Yrot, 0, 1, 0);
  97. glRotatef(Zrot, 0, 0, 1);
  98. glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0.01 * Bias);
  99. glEnable(GL_TEXTURE_2D);
  100. glBegin(GL_POLYGON);
  101. glTexCoord2f(0, 0); glVertex2f(-1, -1);
  102. glTexCoord2f(2, 0); glVertex2f( 1, -1);
  103. glTexCoord2f(2, 2); glVertex2f( 1, 1);
  104. glTexCoord2f(0, 2); glVertex2f(-1, 1);
  105. glEnd();
  106. glPopMatrix();
  107. glutSwapBuffers();
  108. }
  109. static void Reshape( int width, int height )
  110. {
  111. glViewport( 0, 0, width, height );
  112. }
  113. static void Key( unsigned char key, int x, int y )
  114. {
  115. (void) x;
  116. (void) y;
  117. switch (key) {
  118. case 27:
  119. exit(0);
  120. break;
  121. }
  122. glutPostRedisplay();
  123. }
  124. static void SpecialKey( int key, int x, int y )
  125. {
  126. const GLfloat step = 3.0;
  127. (void) x;
  128. (void) y;
  129. switch (key) {
  130. case GLUT_KEY_UP:
  131. Xrot -= step;
  132. break;
  133. case GLUT_KEY_DOWN:
  134. Xrot += step;
  135. break;
  136. case GLUT_KEY_LEFT:
  137. Yrot -= step;
  138. break;
  139. case GLUT_KEY_RIGHT:
  140. Yrot += step;
  141. break;
  142. }
  143. glutPostRedisplay();
  144. }
  145. static void Init( void )
  146. {
  147. GLfloat maxBias;
  148. const char * const ver_string = (const char * const)
  149. glGetString( GL_VERSION );
  150. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  151. printf("GL_VERSION = %s\n", ver_string);
  152. printf("\nThis program should function nearly identically to Mesa's lodbias demo.\n"
  153. "It should cycle through the complet LOD bias range once and exit. If bug\n"
  154. "#3195 still exists, the demo should crash almost immediatly.\n");
  155. printf("This is a regression test for bug #3195.\n");
  156. printf("https://bugs.freedesktop.org/show_bug.cgi?id=3195\n");
  157. if (!glutExtensionSupported("GL_EXT_texture_lod_bias")) {
  158. printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
  159. exit(1);
  160. }
  161. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  162. if (glutExtensionSupported("GL_SGIS_generate_mipmap")) {
  163. /* test auto mipmap generation */
  164. GLint width, height, i;
  165. GLenum format;
  166. GLubyte *image = LoadRGBImage(TEXTURE_FILE, &width, &height, &format);
  167. if (!image) {
  168. printf("Error: could not load texture image %s\n", TEXTURE_FILE);
  169. exit(1);
  170. }
  171. /* resize to 256 x 256 */
  172. if (width != 256 || height != 256) {
  173. GLubyte *newImage = malloc(256 * 256 * 4);
  174. gluScaleImage(format, width, height, GL_UNSIGNED_BYTE, image,
  175. 256, 256, GL_UNSIGNED_BYTE, newImage);
  176. free(image);
  177. image = newImage;
  178. }
  179. printf("Using GL_SGIS_generate_mipmap\n");
  180. glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
  181. glTexImage2D(GL_TEXTURE_2D, 0, format, 256, 256, 0,
  182. format, GL_UNSIGNED_BYTE, image);
  183. free(image);
  184. /* make sure mipmap was really generated correctly */
  185. width = height = 256;
  186. for (i = 0; i < 9; i++) {
  187. GLint w, h;
  188. glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_WIDTH, &w);
  189. glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_HEIGHT, &h);
  190. printf("Level %d size: %d x %d\n", i, w, h);
  191. assert(w == width);
  192. assert(h == height);
  193. width /= 2;
  194. height /= 2;
  195. }
  196. }
  197. else if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
  198. printf("Error: could not load texture image %s\n", TEXTURE_FILE);
  199. exit(1);
  200. }
  201. /* mipmapping required for this extension */
  202. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  203. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  204. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  205. glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS_EXT, &maxBias);
  206. printf("LOD bias range: [%g, %g]\n", -maxBias, maxBias);
  207. BiasMin = -100 * maxBias;
  208. BiasMax = 100 * maxBias;
  209. /* Since we have (about) 8 mipmap levels, no need to bias beyond
  210. * the range [-1, +8].
  211. */
  212. if (BiasMin < -100)
  213. BiasMin = -100;
  214. if (BiasMax > 800)
  215. BiasMax = 800;
  216. }
  217. int main( int argc, char *argv[] )
  218. {
  219. glutInit( &argc, argv );
  220. glutInitWindowPosition( 0, 0 );
  221. glutInitWindowSize( 350, 350 );
  222. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  223. glutCreateWindow( "Bug #3195 Test" );
  224. glewInit();
  225. glutReshapeFunc( Reshape );
  226. glutKeyboardFunc( Key );
  227. glutSpecialFunc( SpecialKey );
  228. glutDisplayFunc( Display );
  229. glutIdleFunc(Idle);
  230. Init();
  231. glutMainLoop();
  232. return 0;
  233. }