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.

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