Merge branch 'mesa_7_6_branch'
[mesa.git] / progs / tests / getteximage.c
1 /**
2 * Test glGetTexImage()
3 * Brian Paul
4 * 9 June 2009
5 */
6
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <math.h>
11 #include <GL/glew.h>
12 #include <GL/glut.h>
13
14 static int Win;
15
16
17 static void
18 TestGetTexImage(GLboolean npot)
19 {
20 GLuint iter;
21 GLubyte *data = (GLubyte *) malloc(1024 * 1024 * 4);
22 GLubyte *data2 = (GLubyte *) malloc(1024 * 1024 * 4);
23
24 glEnable(GL_TEXTURE_2D);
25
26 printf("glTexImage2D + glGetTexImage:\n");
27
28 for (iter = 0; iter < 8; iter++) {
29 GLint p = (iter % 8) + 3;
30 GLint w = npot ? (p * 20) : (1 << p);
31 GLint h = npot ? (p * 10) : (1 << p);
32 GLuint i;
33 GLint level = 0;
34
35 printf(" Testing %d x %d tex image\n", w, h);
36
37 /* fill data */
38 for (i = 0; i < w * h * 4; i++) {
39 data[i] = i & 0xff;
40 data2[i] = 0;
41 }
42
43 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w, h, 0,
44 GL_RGBA, GL_UNSIGNED_BYTE, data);
45
46 glBegin(GL_POINTS);
47 glVertex2f(0, 0);
48 glEnd();
49
50 /* get */
51 glGetTexImage(GL_TEXTURE_2D, level, GL_RGBA, GL_UNSIGNED_BYTE, data2);
52
53 /* compare */
54 for (i = 0; i < w * h * 4; i++) {
55 if (data2[i] != data[i]) {
56 printf("glTexImage + glGetTexImage failure!\n");
57 printf("Expected value %d, found %d\n", data[i], data2[i]);
58 abort();
59 }
60 }
61 }
62
63 printf("Passed\n");
64 glDisable(GL_TEXTURE_2D);
65 free(data);
66 free(data2);
67 }
68
69
70 static GLboolean
71 ColorsEqual(const GLubyte ref[4], const GLubyte act[4])
72 {
73 if (abs((int) ref[0] - (int) act[0]) > 1 ||
74 abs((int) ref[1] - (int) act[1]) > 1 ||
75 abs((int) ref[2] - (int) act[2]) > 1 ||
76 abs((int) ref[3] - (int) act[3]) > 1) {
77 printf("expected %d %d %d %d\n", ref[0], ref[1], ref[2], ref[3]);
78 printf("found %d %d %d %d\n", act[0], act[1], act[2], act[3]);
79 return GL_FALSE;
80 }
81 return GL_TRUE;
82 }
83
84
85 static void
86 TestGetTexImageRTT(GLboolean npot)
87 {
88 GLuint iter;
89
90 printf("Render to texture + glGetTexImage:\n");
91
92 for (iter = 0; iter < 8; iter++) {
93
94 GLuint fb, tex;
95 GLint w, h;
96 GLint level = 0;
97
98 if (npot) {
99 w = 200 + iter * 40;
100 h = 200 + iter * 12;
101 }
102 else {
103 w = 4 << iter;
104 h = 4 << iter;
105 }
106
107 glGenTextures(1, &tex);
108 glGenFramebuffersEXT(1, &fb);
109
110 glBindTexture(GL_TEXTURE_2D, tex);
111 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
112 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
113 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0,
114 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
115
116 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
117 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
118 GL_TEXTURE_2D, tex, level);
119
120 glViewport(0, 0, w, h);
121
122 printf(" Testing %d x %d tex image\n", w, h);
123 {
124 static const GLubyte blue[4] = {0, 0, 255, 255};
125 GLubyte color[4];
126 GLubyte *data2 = (GLubyte *) malloc(w * h * 4);
127 GLuint i;
128
129 /* random clear color */
130 for (i = 0; i < 4; i++) {
131 color[i] = rand() % 256;
132 }
133
134 glClearColor(color[0] / 255.0,
135 color[1] / 255.0,
136 color[2] / 255.0,
137 color[3] / 255.0);
138
139 glClear(GL_COLOR_BUFFER_BIT);
140
141 /* draw polygon over top half, in blue */
142 glColor4ubv(blue);
143 glRectf(0, 0.5, 1.0, 1.0);
144
145 /* get */
146 glGetTexImage(GL_TEXTURE_2D, level, GL_RGBA, GL_UNSIGNED_BYTE, data2);
147
148 /* compare */
149 for (i = 0; i < w * h; i += 4) {
150 if (i < w * h / 2) {
151 /* lower half */
152 if (!ColorsEqual(color, data2 + i * 4)) {
153 printf("Render to texture failure (expected clear color)!\n");
154 abort();
155 }
156 }
157 else {
158 /* upper half */
159 if (!ColorsEqual(blue, data2 + i * 4)) {
160 printf("Render to texture failure (expected blue)!\n");
161 abort();
162 }
163 }
164 }
165
166 free(data2);
167 }
168
169 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
170 glDeleteFramebuffersEXT(1, &fb);
171 glDeleteTextures(1, &tex);
172
173 }
174
175 printf("Passed\n");
176 }
177
178
179
180
181 static void
182 Draw(void)
183 {
184 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
185
186 TestGetTexImage(GL_FALSE);
187 if (glutExtensionSupported("GL_ARB_texture_non_power_of_two"))
188 TestGetTexImage(GL_TRUE);
189
190 if (glutExtensionSupported("GL_EXT_framebuffer_object") ||
191 glutExtensionSupported("GL_ARB_framebuffer_object")) {
192 TestGetTexImageRTT(GL_FALSE);
193 if (glutExtensionSupported("GL_ARB_texture_non_power_of_two"))
194 TestGetTexImageRTT(GL_TRUE);
195 }
196
197 glutDestroyWindow(Win);
198 exit(0);
199
200 glutSwapBuffers();
201 }
202
203
204 static void
205 Reshape(int width, int height)
206 {
207 glViewport(0, 0, width, height);
208 glMatrixMode(GL_PROJECTION);
209 glLoadIdentity();
210 glOrtho(0, 1, 0, 1, -1, 1);
211 glMatrixMode(GL_MODELVIEW);
212 glLoadIdentity();
213 glTranslatef(0.0, 0.0, 0.0);
214 }
215
216
217 static void
218 Key(unsigned char key, int x, int y)
219 {
220 (void) x;
221 (void) y;
222 switch (key) {
223 case 27:
224 glutDestroyWindow(Win);
225 exit(0);
226 break;
227 }
228 glutPostRedisplay();
229 }
230
231
232 static void
233 Init(void)
234 {
235 }
236
237
238 int
239 main(int argc, char *argv[])
240 {
241 glutInit(&argc, argv);
242 glutInitWindowPosition(0, 0);
243 glutInitWindowSize(400, 400);
244 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
245 Win = glutCreateWindow(argv[0]);
246 glewInit();
247 glutReshapeFunc(Reshape);
248 glutKeyboardFunc(Key);
249 glutDisplayFunc(Draw);
250 Init();
251 glutMainLoop();
252 return 0;
253 }