2 * Copyright © 2008 Intel Corporation
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 (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Chris Lord <chris@openedhand.com>
25 * Eric Anholt <eric@anholt.net>
29 /** @file gen-texsubimage.c
31 * Tests that the full mipmap tree is correctly updated after calling
32 * glTexSubImage() when GL_GENERATE_MIPMAP is enabled. Based on a test
36 #include "piglit-util.h"
38 int piglit_width
= 512, piglit_height
= 512;
39 int piglit_window_mode
= GLUT_RGB
| GLUT_DOUBLE
;
41 static void display_mipmaps(int start_x
, int start_y
)
45 /* Disply all the mipmap levels */
46 for (i
= 256; i
> 0; i
/= 2) {
48 glTexCoord2f(0.0, 0.0); glVertex2f(start_x
+ 0, start_y
+ 0);
49 glTexCoord2f(1.0, 0.0); glVertex2f(start_x
+ i
, start_y
+ 0);
50 glTexCoord2f(1.0, 1.0); glVertex2f(start_x
+ i
, start_y
+ i
);
51 glTexCoord2f(0.0, 1.0); glVertex2f(start_x
+ 0, start_y
+ i
);
58 static GLboolean
check_resulting_mipmaps(int x
, int y
, const GLfloat
*color
)
60 GLboolean pass
= GL_TRUE
;
63 for (i
= 256; i
> 4; i
/= 2) {
64 pass
= pass
&& piglit_probe_pixel_rgb(x
+ i
/ 2, y
+ i
/ 2,
75 GLboolean pass
= GL_TRUE
;
77 const GLfloat red
[4] = {1.0, 0.0, 0.0, 0.0};
78 const GLfloat blue
[4] = {0.0, 0.0, 1.0, 0.0};
82 glClearColor(0, 0, 0, 0);
83 glClear(GL_COLOR_BUFFER_BIT
);
85 /* Set up texture object with mipmap generation */
86 glGenTextures(1, &texture
);
87 glBindTexture(GL_TEXTURE_2D
, texture
);
89 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
90 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
91 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
92 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
93 GL_LINEAR_MIPMAP_NEAREST
);
94 glTexParameteri(GL_TEXTURE_2D
, GL_GENERATE_MIPMAP
, GL_TRUE
);
96 data
= malloc(256 * 256 * 4 * sizeof(GLfloat
));
97 for (i
= 0; i
< 4 * 256 * 256; i
+= 4) {
98 data
[i
+ 0] = blue
[0];
99 data
[i
+ 1] = blue
[1];
100 data
[i
+ 2] = blue
[2];
101 data
[i
+ 3] = blue
[3];
104 /* Initialize the texture to blue */
105 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, 256, 256, 0,
106 GL_RGBA
, GL_FLOAT
, data
);
109 /* Display the original mipmaps */
110 display_mipmaps(0, 0);
112 /* Update a square inside the texture to red */
113 data
= malloc(128 * 128 * 4 * sizeof(GLfloat
));
114 for (i
= 0; i
< 4 * 128 * 128; i
+= 4) {
115 data
[i
+ 0] = red
[0];
116 data
[i
+ 1] = red
[1];
117 data
[i
+ 2] = red
[2];
118 data
[i
+ 3] = red
[3];
120 glTexSubImage2D(GL_TEXTURE_2D
, 0, 64, 64, 128, 128,
121 GL_RGBA
, GL_FLOAT
, data
);
124 /* Display the mipmaps after subimage */
125 display_mipmaps(0, 256);
127 glDeleteTextures(1, &texture
);
132 pass
= pass
&& check_resulting_mipmaps(0, 0, blue
);
133 pass
= pass
&& check_resulting_mipmaps(0, 256, red
);
135 return pass
? PIGLIT_SUCCESS
: PIGLIT_FAILURE
;
139 piglit_init(int argc
, char **argv
)
141 piglit_require_extension("GL_SGIS_generate_mipmap");
143 piglit_ortho_projection(piglit_width
, piglit_height
, GL_FALSE
);
145 glEnable(GL_TEXTURE_2D
);