2 * Test packed pixel formats for textures.
12 #include "glut_wrap.h"
20 GLuint redTexel
, greenTexel
; /* with approx 51% alpha, when applicable */
23 static const struct pixel_format Formats
[] = {
25 { "GL_RGBA/GL_UNSIGNED_INT_8_8_8_8",
26 GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8
, 4, 0xff000080, 0x00ff0080 },
27 { "GL_RGBA/GL_UNSIGNED_INT_8_8_8_8_REV",
28 GL_RGBA
, GL_UNSIGNED_INT_8_8_8_8_REV
, 4, 0x800000ff, 0x8000ff00 },
29 { "GL_RGBA/GL_UNSIGNED_INT_10_10_10_2",
30 GL_RGBA
, GL_UNSIGNED_INT_10_10_10_2
, 4, 0xffc00002, 0x3ff002 },
31 { "GL_RGBA/GL_UNSIGNED_INT_2_10_10_10_REV",
32 GL_RGBA
, GL_UNSIGNED_INT_2_10_10_10_REV
, 4, 0xc00003ff, 0xc00ffc00 },
33 { "GL_RGBA/GL_UNSIGNED_SHORT_4_4_4_4",
34 GL_RGBA
, GL_UNSIGNED_SHORT_4_4_4_4
, 2, 0xf008, 0x0f08 },
35 { "GL_RGBA/GL_UNSIGNED_SHORT_4_4_4_4_REV",
36 GL_RGBA
, GL_UNSIGNED_SHORT_4_4_4_4_REV
, 2, 0x800f, 0x80f0 },
37 { "GL_RGBA/GL_UNSIGNED_SHORT_5_5_5_1",
38 GL_RGBA
, GL_UNSIGNED_SHORT_5_5_5_1
, 2, 0xf801, 0x7c1 },
39 { "GL_RGBA/GL_UNSIGNED_SHORT_1_5_5_5_REV",
40 GL_RGBA
, GL_UNSIGNED_SHORT_1_5_5_5_REV
, 2, 0x801f, 0x83e0 },
42 { "GL_BGRA/GL_UNSIGNED_INT_8_8_8_8",
43 GL_BGRA
, GL_UNSIGNED_INT_8_8_8_8
, 4, 0x0000ff80, 0x00ff0080 },
44 { "GL_BGRA/GL_UNSIGNED_INT_8_8_8_8_REV",
45 GL_BGRA
, GL_UNSIGNED_INT_8_8_8_8_REV
, 4, 0x80ff0000, 0x8000ff00 },
46 { "GL_BGRA/GL_UNSIGNED_SHORT_4_4_4_4",
47 GL_BGRA
, GL_UNSIGNED_SHORT_4_4_4_4
, 2, 0x00f8, 0x0f08 },
48 { "GL_BGRA/GL_UNSIGNED_SHORT_4_4_4_4_REV",
49 GL_BGRA
, GL_UNSIGNED_SHORT_4_4_4_4_REV
, 2, 0x8f00, 0x80f0 },
50 { "GL_BGRA/GL_UNSIGNED_SHORT_5_5_5_1",
51 GL_BGRA
, GL_UNSIGNED_SHORT_5_5_5_1
, 2, 0x3f, 0x7c1 },
52 { "GL_BGRA/GL_UNSIGNED_SHORT_1_5_5_5_REV",
53 GL_BGRA
, GL_UNSIGNED_SHORT_1_5_5_5_REV
, 2, 0xfc00, 0x83e0 },
55 { "GL_ABGR_EXT/GL_UNSIGNED_INT_8_8_8_8",
56 GL_ABGR_EXT
, GL_UNSIGNED_INT_8_8_8_8
, 4, 0x800000ff, 0x8000ff00 },
57 { "GL_ABGR_EXT/GL_UNSIGNED_INT_8_8_8_8_REV",
58 GL_ABGR_EXT
, GL_UNSIGNED_INT_8_8_8_8_REV
, 4, 0xff000080, 0x00ff0080 },
59 { "GL_ABGR_EXT/GL_UNSIGNED_SHORT_4_4_4_4",
60 GL_ABGR_EXT
, GL_UNSIGNED_SHORT_4_4_4_4
, 2, 0x800f, 0x80f0 },
61 { "GL_ABGR_EXT/GL_UNSIGNED_SHORT_4_4_4_4_REV",
62 GL_ABGR_EXT
, GL_UNSIGNED_SHORT_4_4_4_4_REV
, 2, 0xf008, 0x0f08 },
63 { "GL_ABGR_EXT/GL_UNSIGNED_SHORT_5_5_5_1",
64 GL_ABGR_EXT
, GL_UNSIGNED_SHORT_5_5_5_1
, 2, 0xf801, 0xf83e },
65 { "GL_ABGR_EXT/GL_UNSIGNED_SHORT_1_5_5_5_REV",
66 GL_ABGR_EXT
, GL_UNSIGNED_SHORT_1_5_5_5_REV
, 2, 0x800f, 0x7c0f },
68 { "GL_RGB/GL_UNSIGNED_SHORT_5_6_5",
69 GL_RGB
, GL_UNSIGNED_SHORT_5_6_5
, 2, 0xf800, 0x7e0 },
70 { "GL_RGB/GL_UNSIGNED_SHORT_5_6_5_REV",
71 GL_RGB
, GL_UNSIGNED_SHORT_5_6_5_REV
, 2, 0x1f, 0x7e0 },
72 { "GL_RGB/GL_UNSIGNED_BYTE_3_3_2",
73 GL_RGB
, GL_UNSIGNED_BYTE_3_3_2
, 1, 0xe0, 0x1c },
74 { "GL_RGB/GL_UNSIGNED_BYTE_2_3_3_REV",
75 GL_RGB
, GL_UNSIGNED_BYTE_2_3_3_REV
, 1, 0x7, 0x38 },
77 { NULL
, 0, 0, 0, 0, 0 }
86 static const struct name_format IntFormats
[] = {
87 { "GL_RGBA", GL_RGBA
},
88 { "GL_RGBA2", GL_RGBA2
},
89 { "GL_RGBA4", GL_RGBA4
},
90 { "GL_RGB5_A1", GL_RGB5_A1
},
91 { "GL_RGBA8", GL_RGBA8
},
92 { "GL_RGBA12", GL_RGBA12
},
93 { "GL_RGBA16", GL_RGBA16
},
94 { "GL_RGB10_A2", GL_RGB10_A2
},
97 { "GL_R3_G3_B2", GL_R3_G3_B2
},
98 { "GL_RGB4", GL_RGB4
},
99 { "GL_RGB5", GL_RGB5
},
100 { "GL_RGB8", GL_RGB8
},
101 { "GL_RGB10", GL_RGB10
},
102 { "GL_RGB12", GL_RGB12
},
103 { "GL_RGB16", GL_RGB16
},
107 #define NUM_INT_FORMATS (sizeof(IntFormats) / sizeof(IntFormats[0]))
108 static GLuint CurFormat
= 0;
110 static GLboolean Test3D
= GL_FALSE
;
111 static GLboolean Blend
= GL_FALSE
;
115 PrintString(const char *s
)
118 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
125 MakeTexture(const struct pixel_format
*format
, GLenum intFormat
, GLboolean swap
)
127 GLubyte texBuffer
[1000];
130 glPixelStorei(GL_UNPACK_SWAP_BYTES
, swap
);
132 if (format
->bytes
== 1) {
133 for (i
= 0; i
< 8; i
++) {
134 texBuffer
[i
] = format
->redTexel
;
136 for (i
= 8; i
< 16; i
++) {
137 texBuffer
[i
] = format
->greenTexel
;
140 else if (format
->bytes
== 2) {
141 GLushort
*us
= (GLushort
*) texBuffer
;
142 for (i
= 0; i
< 8; i
++) {
143 us
[i
] = format
->redTexel
;
145 for (i
= 8; i
< 16; i
++) {
146 us
[i
] = format
->greenTexel
;
149 for (i
= 0; i
< 16; i
++)
150 us
[i
] = (us
[i
] << 8) | (us
[i
] >> 8);
153 else if (format
->bytes
== 4) {
154 GLuint
*ui
= (GLuint
*) texBuffer
;
155 for (i
= 0; i
< 8; i
++) {
156 ui
[i
] = format
->redTexel
;
158 for (i
= 8; i
< 16; i
++) {
159 ui
[i
] = format
->greenTexel
;
162 for (i
= 0; i
< 16; i
++) {
165 | ((b
>> 8) & 0xff00)
166 | ((b
<< 8) & 0xff0000)
167 | ((b
<< 24) & 0xff000000);
176 /* 4 x 4 x 4 texture, undefined data */
177 glTexImage3D(GL_TEXTURE_3D
, 0, intFormat
, 4, 4, 4, 0,
178 format
->format
, format
->type
, NULL
);
179 /* fill in Z=1 and Z=2 slices with the real texture data */
180 glTexSubImage3D(GL_TEXTURE_3D
, 0,
181 0, 0, 1, /* offset */
183 format
->format
, format
->type
, texBuffer
);
184 glTexSubImage3D(GL_TEXTURE_3D
, 0,
185 0, 0, 2, /* offset */
187 format
->format
, format
->type
, texBuffer
);
190 glTexImage2D(GL_TEXTURE_2D
, 0, intFormat
, 4, 4, 0,
191 format
->format
, format
->type
, texBuffer
);
195 GLint r
, g
, b
, a
, l
, i
;
196 glGetTexLevelParameteriv(GL_TEXTURE_2D
, 0, GL_TEXTURE_RED_SIZE
, &r
);
197 glGetTexLevelParameteriv(GL_TEXTURE_2D
, 0, GL_TEXTURE_GREEN_SIZE
, &g
);
198 glGetTexLevelParameteriv(GL_TEXTURE_2D
, 0, GL_TEXTURE_BLUE_SIZE
, &b
);
199 glGetTexLevelParameteriv(GL_TEXTURE_2D
, 0, GL_TEXTURE_ALPHA_SIZE
, &a
);
200 glGetTexLevelParameteriv(GL_TEXTURE_2D
, 0, GL_TEXTURE_LUMINANCE_SIZE
, &l
);
201 glGetTexLevelParameteriv(GL_TEXTURE_2D
, 0, GL_TEXTURE_INTENSITY_SIZE
, &i
);
202 printf("IntFormat: 0x%x R %d G %d B %d A %d L %d I %d\n",
203 intFormat
, r
, g
, b
, a
, l
, i
);
208 printf("GL Error for %s\n", format
->name
);
209 memset(texBuffer
, 255, 1000);
210 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, 4, 4, 0,
211 GL_RGB
, GL_UNSIGNED_BYTE
, texBuffer
);
224 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
226 for (swap
= 0; swap
< 2; swap
++) {
227 for (i
= 0; Formats
[i
].name
; i
++) {
229 glTranslatef(swap
* (w
+ 2), i
* (h
+ 2), 0);
231 MakeTexture(Formats
+ i
, IntFormats
[CurFormat
].format
, swap
);
234 glEnable(GL_TEXTURE_3D
);
236 glEnable(GL_TEXTURE_2D
);
242 glTexCoord3f(0, 0, 0.5); glVertex2f(0, 0);
243 glTexCoord3f(1, 0, 0.5); glVertex2f(w
, 0);
244 glTexCoord3f(1, 1, 0.5); glVertex2f(w
, h
);
245 glTexCoord3f(0, 1, 0.5); glVertex2f(0, h
);
249 glDisable(GL_TEXTURE_3D
);
251 glDisable(GL_TEXTURE_2D
);
257 PrintString(Formats
[i
].name
);
264 glTranslatef(2, i
* (h
+ 2), 0);
267 PrintString("Normal");
268 glRasterPos2i(w
+ 2, 6);
269 PrintString("Byte Swapped");
273 glTranslatef(2, (i
+ 1) * (h
+ 2), 0);
275 sprintf(s
, "Internal Texture Format [f/F]: %s (%d of %lu)",
276 IntFormats
[CurFormat
].name
, CurFormat
+ 1, (unsigned long) NUM_INT_FORMATS
);
281 glTranslatef(2, (i
+ 2) * (h
+ 2), 0);
284 PrintString("Target [2/3]: GL_TEXTURE_3D");
286 PrintString("Target [2/3]: GL_TEXTURE_2D");
290 glTranslatef(2, (i
+ 3) * (h
+ 2), 0);
293 PrintString("Blend: Yes");
295 PrintString("Blend: No");
303 Reshape(int width
, int height
)
305 glViewport(0, 0, width
, height
);
306 glMatrixMode(GL_PROJECTION
);
308 glOrtho(0, width
, 0, height
, -1, 1);
309 glMatrixMode(GL_MODELVIEW
);
315 Key(unsigned char key
, int x
, int y
)
325 CurFormat
= NUM_INT_FORMATS
- 1;
331 if (CurFormat
== NUM_INT_FORMATS
)
351 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
352 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
353 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
354 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
355 glTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
356 glTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
357 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
358 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
363 main(int argc
, char *argv
[])
365 glutInit(&argc
, argv
);
366 glutInitWindowPosition(0, 0);
367 glutInitWindowSize(700, 800);
368 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
369 glutCreateWindow(argv
[0]);
371 glutReshapeFunc(Reshape
);
372 glutKeyboardFunc(Key
);
373 glutDisplayFunc(Draw
);