Make git ignore files only generated at build time.
[mesa.git] / progs / tests / packedpixels.c
1 /*
2 * Test packed pixel formats for textures.
3 * Brian Paul
4 * 12 May 2004
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <math.h>
10 #include <string.h>
11 #include <GL/glut.h>
12
13
14 struct pixel_format {
15 const char *name;
16 GLenum format;
17 GLenum type;
18 GLint bytes;
19 GLuint redTexel, greenTexel;
20 };
21
22 static const struct pixel_format Formats[] = {
23
24 { "GL_RGBA/GL_UNSIGNED_INT_8_8_8_8",
25 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, 4, 0xff000000, 0x00ff0000 },
26 { "GL_RGBA/GL_UNSIGNED_INT_8_8_8_8_REV",
27 GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, 4, 0x000000ff, 0x0000ff00 },
28 { "GL_RGBA/GL_UNSIGNED_INT_10_10_10_2",
29 GL_RGBA, GL_UNSIGNED_INT_10_10_10_2, 4, 0xffc00000, 0x3ff000 },
30 { "GL_RGBA/GL_UNSIGNED_INT_2_10_10_10_REV",
31 GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, 4, 0x3ff, 0xffc00 },
32 { "GL_RGBA/GL_UNSIGNED_SHORT_4_4_4_4",
33 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 2, 0xf000, 0x0f00 },
34 { "GL_RGBA/GL_UNSIGNED_SHORT_4_4_4_4_REV",
35 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4_REV, 2, 0x000f, 0x00f0 },
36 { "GL_RGBA/GL_UNSIGNED_SHORT_5_5_5_1",
37 GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, 2, 0xf800, 0x7c0 },
38 { "GL_RGBA/GL_UNSIGNED_SHORT_1_5_5_5_REV",
39 GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 2, 0x1f, 0x3e0 },
40
41 { "GL_BGRA/GL_UNSIGNED_INT_8_8_8_8",
42 GL_BGRA, GL_UNSIGNED_INT_8_8_8_8, 4, 0x0000ff00, 0x00ff0000 },
43 { "GL_BGRA/GL_UNSIGNED_INT_8_8_8_8_REV",
44 GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 4, 0x00ff0000, 0x0000ff00 },
45 { "GL_BGRA/GL_UNSIGNED_SHORT_4_4_4_4",
46 GL_BGRA, GL_UNSIGNED_SHORT_4_4_4_4, 2, 0x00f0, 0x0f00 },
47 { "GL_BGRA/GL_UNSIGNED_SHORT_4_4_4_4_REV",
48 GL_BGRA, GL_UNSIGNED_SHORT_4_4_4_4_REV, 2, 0x0f00, 0x00f0 },
49 { "GL_BGRA/GL_UNSIGNED_SHORT_5_5_5_1",
50 GL_BGRA, GL_UNSIGNED_SHORT_5_5_5_1, 2, 0x3e, 0x7c0 },
51 { "GL_BGRA/GL_UNSIGNED_SHORT_1_5_5_5_REV",
52 GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 2, 0x7c00, 0x3e0 },
53
54 { "GL_ABGR_EXT/GL_UNSIGNED_INT_8_8_8_8",
55 GL_ABGR_EXT, GL_UNSIGNED_INT_8_8_8_8, 4, 0x000000ff, 0x0000ff00 },
56 { "GL_ABGR_EXT/GL_UNSIGNED_INT_8_8_8_8_REV",
57 GL_ABGR_EXT, GL_UNSIGNED_INT_8_8_8_8_REV, 4, 0xff000000, 0x00ff0000 },
58 { "GL_ABGR_EXT/GL_UNSIGNED_SHORT_4_4_4_4",
59 GL_ABGR_EXT, GL_UNSIGNED_SHORT_4_4_4_4, 2, 0x000f, 0x00f0 },
60 { "GL_ABGR_EXT/GL_UNSIGNED_SHORT_4_4_4_4_REV",
61 GL_ABGR_EXT, GL_UNSIGNED_SHORT_4_4_4_4_REV, 2, 0xf000, 0x0f00 },
62 { "GL_ABGR_EXT/GL_UNSIGNED_SHORT_5_5_5_1",
63 GL_ABGR_EXT, GL_UNSIGNED_SHORT_5_5_5_1, 2, 0x1, 0x3e },
64 { "GL_ABGR_EXT/GL_UNSIGNED_SHORT_1_5_5_5_REV",
65 GL_ABGR_EXT, GL_UNSIGNED_SHORT_1_5_5_5_REV, 2, 0x8000, 0x7c00 },
66
67 { "GL_RGB/GL_UNSIGNED_SHORT_5_6_5",
68 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 2, 0xf800, 0x7e0 },
69 { "GL_RGB/GL_UNSIGNED_SHORT_5_6_5_REV",
70 GL_RGB, GL_UNSIGNED_SHORT_5_6_5_REV, 2, 0x1f, 0x7e0 },
71 { "GL_RGB/GL_UNSIGNED_BYTE_3_3_2",
72 GL_RGB, GL_UNSIGNED_BYTE_3_3_2, 1, 0xe0, 0x1c },
73 { "GL_RGB/GL_UNSIGNED_BYTE_2_3_3_REV",
74 GL_RGB, GL_UNSIGNED_BYTE_2_3_3_REV, 1, 0x7, 0x38 },
75
76 { NULL, 0, 0, 0, 0, 0 }
77 };
78
79
80 struct name_format {
81 const char *name;
82 GLenum format;
83 };
84
85 static const struct name_format IntFormats[] = {
86 { "GL_RGBA", GL_RGBA },
87 { "GL_RGBA2", GL_RGBA2 },
88 { "GL_RGBA4", GL_RGBA4 },
89 { "GL_RGB5_A1", GL_RGB5_A1 },
90 { "GL_RGBA8", GL_RGBA8 },
91 { "GL_RGBA12", GL_RGBA12 },
92 { "GL_RGBA16", GL_RGBA16 },
93 { "GL_RGB10_A2", GL_RGB10_A2 },
94
95 { "GL_RGB", GL_RGB },
96 { "GL_R3_G3_B2", GL_R3_G3_B2 },
97 { "GL_RGB4", GL_RGB4 },
98 { "GL_RGB5", GL_RGB5 },
99 { "GL_RGB8", GL_RGB8 },
100 { "GL_RGB10", GL_RGB10 },
101 { "GL_RGB12", GL_RGB12 },
102 { "GL_RGB16", GL_RGB16 },
103
104 };
105
106 #define NUM_INT_FORMATS (sizeof(IntFormats) / sizeof(IntFormats[0]))
107 static GLuint CurFormat = 0;
108
109 static GLboolean Test3D = GL_FALSE;
110
111
112
113 static void
114 PrintString(const char *s)
115 {
116 while (*s) {
117 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
118 s++;
119 }
120 }
121
122
123 static void
124 MakeTexture(const struct pixel_format *format, GLenum intFormat, GLboolean swap)
125 {
126 GLubyte texBuffer[1000];
127 int i;
128
129 glPixelStorei(GL_UNPACK_SWAP_BYTES, swap);
130
131 if (format->bytes == 1) {
132 for (i = 0; i < 8; i++) {
133 texBuffer[i] = format->redTexel;
134 }
135 for (i = 8; i < 16; i++) {
136 texBuffer[i] = format->greenTexel;
137 }
138 }
139 else if (format->bytes == 2) {
140 GLushort *us = (GLushort *) texBuffer;
141 for (i = 0; i < 8; i++) {
142 us[i] = format->redTexel;
143 }
144 for (i = 8; i < 16; i++) {
145 us[i] = format->greenTexel;
146 }
147 if (swap) {
148 for (i = 0; i < 16; i++)
149 us[i] = (us[i] << 8) | (us[i] >> 8);
150 }
151 }
152 else if (format->bytes == 4) {
153 GLuint *ui = (GLuint *) texBuffer;
154 for (i = 0; i < 8; i++) {
155 ui[i] = format->redTexel;
156 }
157 for (i = 8; i < 16; i++) {
158 ui[i] = format->greenTexel;
159 }
160 if (swap) {
161 for (i = 0; i < 16; i++) {
162 GLuint b = ui[i];
163 ui[i] = (b >> 24)
164 | ((b >> 8) & 0xff00)
165 | ((b << 8) & 0xff0000)
166 | ((b << 24) & 0xff000000);
167 }
168 }
169 }
170 else {
171 abort();
172 }
173
174 if (Test3D) {
175 /* 4 x 4 x 4 texture, undefined data */
176 glTexImage3D(GL_TEXTURE_3D, 0, intFormat, 4, 4, 4, 0,
177 format->format, format->type, NULL);
178 /* fill in Z=1 and Z=2 slices with the real texture data */
179 glTexSubImage3D(GL_TEXTURE_3D, 0,
180 0, 0, 1, /* offset */
181 4, 4, 1, /* size */
182 format->format, format->type, texBuffer);
183 glTexSubImage3D(GL_TEXTURE_3D, 0,
184 0, 0, 2, /* offset */
185 4, 4, 1, /* size */
186 format->format, format->type, texBuffer);
187 }
188 else {
189 glTexImage2D(GL_TEXTURE_2D, 0, intFormat, 4, 4, 0,
190 format->format, format->type, texBuffer);
191 }
192
193 if (glGetError()) {
194 printf("GL Error for %s\n", format->name);
195 memset(texBuffer, 255, 1000);
196 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0,
197 GL_RGB, GL_UNSIGNED_BYTE, texBuffer);
198 }
199 }
200
201
202
203 static void
204 Draw(void)
205 {
206 char s[1000];
207 int w = 350, h = 20;
208 int i, swap;
209
210 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
211
212 for (swap = 0; swap < 2; swap++) {
213 for (i = 0; Formats[i].name; i++) {
214 glPushMatrix();
215 glTranslatef(swap * (w + 2), i * (h + 2), 0);
216
217 MakeTexture(Formats + i, IntFormats[CurFormat].format, swap);
218
219 if (Test3D)
220 glEnable(GL_TEXTURE_3D);
221 else
222 glEnable(GL_TEXTURE_2D);
223 glBegin(GL_POLYGON);
224 glTexCoord3f(0, 0, 0.5); glVertex2f(0, 0);
225 glTexCoord3f(1, 0, 0.5); glVertex2f(w, 0);
226 glTexCoord3f(1, 1, 0.5); glVertex2f(w, h);
227 glTexCoord3f(0, 1, 0.5); glVertex2f(0, h);
228 glEnd();
229
230 if (Test3D)
231 glDisable(GL_TEXTURE_3D);
232 else
233 glDisable(GL_TEXTURE_2D);
234 glColor3f(0, 0, 0);
235 glRasterPos2i(8, 6);
236 PrintString(Formats[i].name);
237
238 glPopMatrix();
239 }
240 }
241
242 glPushMatrix();
243 glTranslatef(2, i * (h + 2), 0);
244 glColor3f(1, 1, 1);
245 glRasterPos2i(8, 6);
246 PrintString("Normal");
247 glRasterPos2i(w + 2, 6);
248 PrintString("Byte Swapped");
249 glPopMatrix();
250
251 glPushMatrix();
252 glTranslatef(2, (i + 1) * (h + 2), 0);
253 glRasterPos2i(8, 6);
254 sprintf(s, "Internal Texture Format [f/F]: %s (%d of %d)",
255 IntFormats[CurFormat].name, CurFormat + 1, NUM_INT_FORMATS);
256 PrintString(s);
257 glPopMatrix();
258
259 glPushMatrix();
260 glTranslatef(2, (i + 2) * (h + 2), 0);
261 glRasterPos2i(8, 6);
262 if (Test3D)
263 PrintString("Target [2/3]: GL_TEXTURE_3D");
264 else
265 PrintString("Target [2/3]: GL_TEXTURE_2D");
266 glPopMatrix();
267
268 glutSwapBuffers();
269 }
270
271
272 static void
273 Reshape(int width, int height)
274 {
275 glViewport(0, 0, width, height);
276 glMatrixMode(GL_PROJECTION);
277 glLoadIdentity();
278 glOrtho(0, width, 0, height, -1, 1);
279 glMatrixMode(GL_MODELVIEW);
280 glLoadIdentity();
281 }
282
283
284 static void
285 Key(unsigned char key, int x, int y)
286 {
287 (void) x;
288 (void) y;
289 switch (key) {
290 case 'F':
291 if (CurFormat == 0)
292 CurFormat = NUM_INT_FORMATS - 1;
293 else
294 CurFormat--;
295 break;
296 case 'f':
297 CurFormat++;
298 if (CurFormat == NUM_INT_FORMATS)
299 CurFormat = 0;
300 break;
301 case '2':
302 Test3D = GL_FALSE;
303 break;
304 case '3':
305 Test3D = GL_TRUE;
306 break;
307 case 27:
308 exit(0);
309 break;
310 }
311 glutPostRedisplay();
312 }
313
314
315 static void
316 Init(void)
317 {
318 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
319 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
320 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
321 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
322 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
323 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
324 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
325 }
326
327
328 int
329 main(int argc, char *argv[])
330 {
331 glutInit(&argc, argv);
332 glutInitWindowPosition(0, 0);
333 glutInitWindowSize(700, 800);
334 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
335 glutCreateWindow(argv[0]);
336 glutReshapeFunc(Reshape);
337 glutKeyboardFunc(Key);
338 glutDisplayFunc(Draw);
339 Init();
340 glutMainLoop();
341 return 0;
342 }