Merge branch 'mesa_7_5_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(void)
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 = (1 << p);
31 GLint h = (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(void)
87 {
88 GLuint iter;
89 GLuint fb, tex;
90 GLint w = 512;
91 GLint h = 256;
92 GLint level = 0;
93
94 glGenTextures(1, &tex);
95 glGenFramebuffersEXT(1, &fb);
96
97 glBindTexture(GL_TEXTURE_2D, tex);
98 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
99 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
100 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0,
101 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
102
103 glBindFramebuffer(GL_FRAMEBUFFER_EXT, fb);
104 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
105 GL_TEXTURE_2D, tex, level);
106
107 printf("Render to texture + glGetTexImage:\n");
108 printf(" Testing %d x %d tex image\n", w, h);
109 for (iter = 0; iter < 8; iter++) {
110 GLubyte color[4];
111 GLubyte *data2 = (GLubyte *) malloc(w * h * 4);
112 GLuint i;
113
114 /* random clear color */
115 for (i = 0; i < 4; i++) {
116 color[i] = rand() % 256;
117 }
118
119 glClearColor(color[0] / 255.0,
120 color[1] / 255.0,
121 color[2] / 255.0,
122 color[3] / 255.0);
123
124 glClear(GL_COLOR_BUFFER_BIT);
125
126 /* get */
127 glGetTexImage(GL_TEXTURE_2D, level, GL_RGBA, GL_UNSIGNED_BYTE, data2);
128
129 /* compare */
130 for (i = 0; i < w * h; i += 4) {
131 if (!ColorsEqual(color, data2 + i * 4)) {
132 printf("Render to texture failure!\n");
133 abort();
134 }
135 }
136
137 free(data2);
138 }
139
140 glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
141 glDeleteFramebuffersEXT(1, &fb);
142 glDeleteTextures(1, &tex);
143
144 printf("Passed\n");
145 }
146
147
148
149
150 static void
151 Draw(void)
152 {
153 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
154
155 TestGetTexImage();
156
157 if (glutExtensionSupported("GL_EXT_framebuffer_object") ||
158 glutExtensionSupported("GL_ARB_framebuffer_object"))
159 TestGetTexImageRTT();
160
161 glutDestroyWindow(Win);
162 exit(0);
163
164 glutSwapBuffers();
165 }
166
167
168 static void
169 Reshape(int width, int height)
170 {
171 glViewport(0, 0, width, height);
172 glMatrixMode(GL_PROJECTION);
173 glLoadIdentity();
174 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
175 glMatrixMode(GL_MODELVIEW);
176 glLoadIdentity();
177 glTranslatef(0.0, 0.0, -15.0);
178 }
179
180
181 static void
182 Key(unsigned char key, int x, int y)
183 {
184 (void) x;
185 (void) y;
186 switch (key) {
187 case 27:
188 glutDestroyWindow(Win);
189 exit(0);
190 break;
191 }
192 glutPostRedisplay();
193 }
194
195
196 static void
197 Init(void)
198 {
199 }
200
201
202 int
203 main(int argc, char *argv[])
204 {
205 glutInit(&argc, argv);
206 glutInitWindowPosition(0, 0);
207 glutInitWindowSize(400, 400);
208 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
209 Win = glutCreateWindow(argv[0]);
210 glewInit();
211 glutReshapeFunc(Reshape);
212 glutKeyboardFunc(Key);
213 glutDisplayFunc(Draw);
214 Init();
215 glutMainLoop();
216 return 0;
217 }