Convert libGL and DRI drivers to require libdrm.
[mesa-demos.git] / src / tests / packedpixels.c
blob842cf7f34b93822dfb5fa19c8eb197ac21dc08d6
1 /*
2 * Test packed pixel formats for textures.
3 * Brian Paul
4 * 12 May 2004
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <math.h>
10 #include <string.h>
11 #include <GL/glut.h>
14 struct pixel_format {
15 const char *name;
16 GLenum format;
17 GLenum type;
18 GLint bytes;
19 GLuint redTexel, greenTexel;
22 static const struct pixel_format Formats[] = {
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 },
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 },
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 },
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 },
76 { NULL, 0, 0, 0, 0, 0 }
80 struct name_format {
81 const char *name;
82 GLenum format;
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 },
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 },
106 #define NUM_INT_FORMATS (sizeof(IntFormats) / sizeof(IntFormats[0]))
107 static GLuint CurFormat = 0;
110 static void
111 PrintString(const char *s)
113 while (*s) {
114 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
115 s++;
120 static void
121 MakeTexture(const struct pixel_format *format, GLenum intFormat, GLboolean swap)
123 GLubyte texBuffer[1000];
124 int i;
126 glPixelStorei(GL_UNPACK_SWAP_BYTES, swap);
128 if (format->bytes == 1) {
129 for (i = 0; i < 8; i++) {
130 texBuffer[i] = format->redTexel;
132 for (i = 8; i < 16; i++) {
133 texBuffer[i] = format->greenTexel;
136 else if (format->bytes == 2) {
137 GLushort *us = (GLushort *) texBuffer;
138 for (i = 0; i < 8; i++) {
139 us[i] = format->redTexel;
141 for (i = 8; i < 16; i++) {
142 us[i] = format->greenTexel;
144 if (swap) {
145 for (i = 0; i < 16; i++)
146 us[i] = (us[i] << 8) | (us[i] >> 8);
149 else if (format->bytes == 4) {
150 GLuint *ui = (GLuint *) texBuffer;
151 for (i = 0; i < 8; i++) {
152 ui[i] = format->redTexel;
154 for (i = 8; i < 16; i++) {
155 ui[i] = format->greenTexel;
157 if (swap) {
158 for (i = 0; i < 16; i++) {
159 GLuint b = ui[i];
160 ui[i] = (b >> 24)
161 | ((b >> 8) & 0xff00)
162 | ((b << 8) & 0xff0000)
163 | ((b << 24) & 0xff000000);
167 else {
168 abort();
170 glTexImage2D(GL_TEXTURE_2D, 0, intFormat, 4, 4, 0,
171 format->format, format->type, texBuffer);
173 if (glGetError()) {
174 printf("GL Error for %s\n", format->name);
175 memset(texBuffer, 255, 1000);
176 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0,
177 GL_RGB, GL_UNSIGNED_BYTE, texBuffer);
183 static void
184 Draw(void)
186 char s[1000];
187 int w = 350, h = 20;
188 int i, swap;
190 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
192 for (swap = 0; swap < 2; swap++) {
193 for (i = 0; Formats[i].name; i++) {
194 glPushMatrix();
195 glTranslatef(swap * (w + 2), i * (h + 2), 0);
197 MakeTexture(Formats + i, IntFormats[CurFormat].format, swap);
199 glEnable(GL_TEXTURE_2D);
200 glBegin(GL_POLYGON);
201 glTexCoord2f(0, 0); glVertex2f(0, 0);
202 glTexCoord2f(1, 0); glVertex2f(w, 0);
203 glTexCoord2f(1, 1); glVertex2f(w, h);
204 glTexCoord2f(0, 1); glVertex2f(0, h);
205 glEnd();
207 glDisable(GL_TEXTURE_2D);
208 glColor3f(0, 0, 0);
209 glRasterPos2i(8, 6);
210 PrintString(Formats[i].name);
212 glPopMatrix();
216 glPushMatrix();
217 glTranslatef(2, i * (h + 2), 0);
218 glColor3f(1, 1, 1);
219 glRasterPos2i(8, 6);
220 PrintString("Normal");
221 glRasterPos2i(w + 2, 6);
222 PrintString("Byte Swapped");
223 glPopMatrix();
225 glPushMatrix();
226 glTranslatef(2, (i + 1) * (h + 2), 0);
227 glRasterPos2i(8, 6);
228 sprintf(s, "Internal Texture Format [f/F]: %s (%d of %d)",
229 IntFormats[CurFormat].name, CurFormat + 1, NUM_INT_FORMATS);
230 PrintString(s);
231 glPopMatrix();
233 glutSwapBuffers();
237 static void
238 Reshape(int width, int height)
240 glViewport(0, 0, width, height);
241 glMatrixMode(GL_PROJECTION);
242 glLoadIdentity();
243 glOrtho(0, width, 0, height, -1, 1);
244 glMatrixMode(GL_MODELVIEW);
245 glLoadIdentity();
249 static void
250 Key(unsigned char key, int x, int y)
252 (void) x;
253 (void) y;
254 switch (key) {
255 case 'F':
256 if (CurFormat == 0)
257 CurFormat = NUM_INT_FORMATS - 1;
258 else
259 CurFormat--;
260 break;
261 case 'f':
262 CurFormat++;
263 if (CurFormat == NUM_INT_FORMATS)
264 CurFormat = 0;
265 break;
266 case 27:
267 exit(0);
268 break;
270 glutPostRedisplay();
274 static void
275 Init(void)
277 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
278 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
279 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
280 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
281 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
286 main(int argc, char *argv[])
288 glutInit(&argc, argv);
289 glutInitWindowPosition(0, 0);
290 glutInitWindowSize(700, 800);
291 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
292 glutCreateWindow(argv[0]);
293 glutReshapeFunc(Reshape);
294 glutKeyboardFunc(Key);
295 glutDisplayFunc(Draw);
296 Init();
297 glutMainLoop();
298 return 0;