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