gallium: change remaining util functions to use cso sampler views
[mesa/mesa-lb.git] / progs / tests / getteximage.c
blobe4053b8de1a5e43ba47330df58d813d0a55d1992
1 /**
2 * Test glGetTexImage()
3 * Brian Paul
4 * 9 June 2009
5 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <math.h>
11 #include <GL/glew.h>
12 #include <GL/glut.h>
14 static int Win;
17 static void
18 TestGetTexImage(GLboolean npot)
20 GLuint iter;
21 GLubyte *data = (GLubyte *) malloc(1024 * 1024 * 4);
22 GLubyte *data2 = (GLubyte *) malloc(1024 * 1024 * 4);
24 glEnable(GL_TEXTURE_2D);
26 printf("glTexImage2D + glGetTexImage:\n");
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;
35 printf(" Testing %d x %d tex image\n", w, h);
37 /* fill data */
38 for (i = 0; i < w * h * 4; i++) {
39 data[i] = i & 0xff;
40 data2[i] = 0;
43 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w, h, 0,
44 GL_RGBA, GL_UNSIGNED_BYTE, data);
46 glBegin(GL_POINTS);
47 glVertex2f(0, 0);
48 glEnd();
50 /* get */
51 glGetTexImage(GL_TEXTURE_2D, level, GL_RGBA, GL_UNSIGNED_BYTE, data2);
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();
62 /* get as BGRA */
63 glGetTexImage(GL_TEXTURE_2D, level, GL_BGRA, GL_UNSIGNED_BYTE, data2);
65 /* compare */
67 const GLubyte *rgba = (GLubyte *) data;
68 const GLubyte *bgra = (GLubyte *) data2;
69 for (i = 0; i < w * h; i += 4) {
70 if (rgba[i+0] != bgra[i+2] ||
71 rgba[i+1] != bgra[i+1] ||
72 rgba[i+2] != bgra[i+0] ||
73 rgba[i+3] != bgra[i+3]) {
74 printf("glTexImage + glGetTexImage(GL_BGRA) failure!\n");
75 printf("Expected value %d, found %d\n", data[i], data2[i]);
76 abort();
83 printf("Passed\n");
84 glDisable(GL_TEXTURE_2D);
85 free(data);
86 free(data2);
90 static GLboolean
91 ColorsEqual(const GLubyte ref[4], const GLubyte act[4])
93 if (abs((int) ref[0] - (int) act[0]) > 1 ||
94 abs((int) ref[1] - (int) act[1]) > 1 ||
95 abs((int) ref[2] - (int) act[2]) > 1 ||
96 abs((int) ref[3] - (int) act[3]) > 1) {
97 printf("expected %d %d %d %d\n", ref[0], ref[1], ref[2], ref[3]);
98 printf("found %d %d %d %d\n", act[0], act[1], act[2], act[3]);
99 return GL_FALSE;
101 return GL_TRUE;
105 static void
106 TestGetTexImageRTT(GLboolean npot)
108 GLuint iter;
110 printf("Render to texture + glGetTexImage:\n");
112 for (iter = 0; iter < 8; iter++) {
114 GLuint fb, tex;
115 GLint w, h;
116 GLint level = 0;
118 if (npot) {
119 w = 200 + iter * 40;
120 h = 200 + iter * 12;
122 else {
123 w = 4 << iter;
124 h = 4 << iter;
127 glGenTextures(1, &tex);
128 glGenFramebuffersEXT(1, &fb);
130 glBindTexture(GL_TEXTURE_2D, tex);
131 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
132 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
133 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0,
134 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
136 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
137 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
138 GL_TEXTURE_2D, tex, level);
140 glViewport(0, 0, w, h);
142 printf(" Testing %d x %d tex image\n", w, h);
144 static const GLubyte blue[4] = {0, 0, 255, 255};
145 GLubyte color[4];
146 GLubyte *data2 = (GLubyte *) malloc(w * h * 4);
147 GLuint i;
149 /* random clear color */
150 for (i = 0; i < 4; i++) {
151 color[i] = rand() % 256;
154 glClearColor(color[0] / 255.0,
155 color[1] / 255.0,
156 color[2] / 255.0,
157 color[3] / 255.0);
159 glClear(GL_COLOR_BUFFER_BIT);
161 /* draw polygon over top half, in blue */
162 glColor4ubv(blue);
163 glRectf(0, 0.5, 1.0, 1.0);
165 /* get */
166 glGetTexImage(GL_TEXTURE_2D, level, GL_RGBA, GL_UNSIGNED_BYTE, data2);
168 /* compare */
169 for (i = 0; i < w * h; i += 4) {
170 if (i < w * h / 2) {
171 /* lower half */
172 if (!ColorsEqual(color, data2 + i * 4)) {
173 printf("Render to texture failure (expected clear color)!\n");
174 abort();
177 else {
178 /* upper half */
179 if (!ColorsEqual(blue, data2 + i * 4)) {
180 printf("Render to texture failure (expected blue)!\n");
181 abort();
186 free(data2);
189 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
190 glDeleteFramebuffersEXT(1, &fb);
191 glDeleteTextures(1, &tex);
195 printf("Passed\n");
201 static void
202 Draw(void)
204 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
206 TestGetTexImage(GL_FALSE);
207 if (glutExtensionSupported("GL_ARB_texture_non_power_of_two"))
208 TestGetTexImage(GL_TRUE);
210 if (glutExtensionSupported("GL_EXT_framebuffer_object") ||
211 glutExtensionSupported("GL_ARB_framebuffer_object")) {
212 TestGetTexImageRTT(GL_FALSE);
213 if (glutExtensionSupported("GL_ARB_texture_non_power_of_two"))
214 TestGetTexImageRTT(GL_TRUE);
217 glutDestroyWindow(Win);
218 exit(0);
220 glutSwapBuffers();
224 static void
225 Reshape(int width, int height)
227 glViewport(0, 0, width, height);
228 glMatrixMode(GL_PROJECTION);
229 glLoadIdentity();
230 glOrtho(0, 1, 0, 1, -1, 1);
231 glMatrixMode(GL_MODELVIEW);
232 glLoadIdentity();
233 glTranslatef(0.0, 0.0, 0.0);
237 static void
238 Key(unsigned char key, int x, int y)
240 (void) x;
241 (void) y;
242 switch (key) {
243 case 27:
244 glutDestroyWindow(Win);
245 exit(0);
246 break;
248 glutPostRedisplay();
252 static void
253 Init(void)
259 main(int argc, char *argv[])
261 glutInit(&argc, argv);
262 glutInitWindowPosition(0, 0);
263 glutInitWindowSize(400, 400);
264 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
265 Win = glutCreateWindow(argv[0]);
266 glewInit();
267 glutReshapeFunc(Reshape);
268 glutKeyboardFunc(Key);
269 glutDisplayFunc(Draw);
270 Init();
271 glutMainLoop();
272 return 0;