Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

packedpixels.c 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Test packed pixel formats for textures.
  3. * Brian Paul
  4. * 12 May 2004
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <math.h>
  9. #include <string.h>
  10. #include <GL/glut.h>
  11. struct pixel_format {
  12. const char *name;
  13. GLenum format;
  14. GLenum type;
  15. GLint bytes;
  16. GLuint redTexel, greenTexel;
  17. };
  18. static const struct pixel_format Formats[] = {
  19. { "GL_RGBA/GL_UNSIGNED_INT_8_8_8_8",
  20. GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, 4, 0xff000000, 0x00ff0000 },
  21. { "GL_RGBA/GL_UNSIGNED_INT_8_8_8_8_REV",
  22. GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, 4, 0x000000ff, 0x0000ff00 },
  23. { "GL_RGBA/GL_UNSIGNED_INT_10_10_10_2",
  24. GL_RGBA, GL_UNSIGNED_INT_10_10_10_2, 4, 0xffc00000, 0x3ff000 },
  25. { "GL_RGBA/GL_UNSIGNED_INT_2_10_10_10_REV",
  26. GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, 4, 0x3ff, 0xffc00 },
  27. { "GL_RGBA/GL_UNSIGNED_SHORT_4_4_4_4",
  28. GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 2, 0xf000, 0x0f00 },
  29. { "GL_RGBA/GL_UNSIGNED_SHORT_4_4_4_4_REV",
  30. GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4_REV, 2, 0x000f, 0x00f0 },
  31. { "GL_RGBA/GL_UNSIGNED_SHORT_5_5_5_1",
  32. GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, 2, 0xf800, 0x7c0 },
  33. { "GL_RGBA/GL_UNSIGNED_SHORT_1_5_5_5_REV",
  34. GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 2, 0x1f, 0x3e0 },
  35. { "GL_BGRA/GL_UNSIGNED_INT_8_8_8_8",
  36. GL_BGRA, GL_UNSIGNED_INT_8_8_8_8, 4, 0x0000ff00, 0x00ff0000 },
  37. { "GL_BGRA/GL_UNSIGNED_INT_8_8_8_8_REV",
  38. GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 4, 0x00ff0000, 0x0000ff00 },
  39. { "GL_BGRA/GL_UNSIGNED_SHORT_4_4_4_4",
  40. GL_BGRA, GL_UNSIGNED_SHORT_4_4_4_4, 2, 0x00f0, 0x0f00 },
  41. { "GL_BGRA/GL_UNSIGNED_SHORT_4_4_4_4_REV",
  42. GL_BGRA, GL_UNSIGNED_SHORT_4_4_4_4_REV, 2, 0x0f00, 0x00f0 },
  43. { "GL_BGRA/GL_UNSIGNED_SHORT_5_5_5_1",
  44. GL_BGRA, GL_UNSIGNED_SHORT_5_5_5_1, 2, 0x3e, 0x7c0 },
  45. { "GL_BGRA/GL_UNSIGNED_SHORT_1_5_5_5_REV",
  46. GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 2, 0x7c00, 0x3e0 },
  47. { "GL_ABGR_EXT/GL_UNSIGNED_INT_8_8_8_8",
  48. GL_ABGR_EXT, GL_UNSIGNED_INT_8_8_8_8, 4, 0x000000ff, 0x0000ff00 },
  49. { "GL_ABGR_EXT/GL_UNSIGNED_INT_8_8_8_8_REV",
  50. GL_ABGR_EXT, GL_UNSIGNED_INT_8_8_8_8_REV, 4, 0xff000000, 0x00ff0000 },
  51. { "GL_ABGR_EXT/GL_UNSIGNED_SHORT_4_4_4_4",
  52. GL_ABGR_EXT, GL_UNSIGNED_SHORT_4_4_4_4, 2, 0x000f, 0x00f0 },
  53. { "GL_ABGR_EXT/GL_UNSIGNED_SHORT_4_4_4_4_REV",
  54. GL_ABGR_EXT, GL_UNSIGNED_SHORT_4_4_4_4_REV, 2, 0xf000, 0x0f00 },
  55. { "GL_ABGR_EXT/GL_UNSIGNED_SHORT_5_5_5_1",
  56. GL_ABGR_EXT, GL_UNSIGNED_SHORT_5_5_5_1, 2, 0x1, 0x3e },
  57. { "GL_ABGR_EXT/GL_UNSIGNED_SHORT_1_5_5_5_REV",
  58. GL_ABGR_EXT, GL_UNSIGNED_SHORT_1_5_5_5_REV, 2, 0x8000, 0x7c00 },
  59. { "GL_RGB/GL_UNSIGNED_SHORT_5_6_5",
  60. GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 2, 0xf800, 0x7e0 },
  61. { "GL_RGB/GL_UNSIGNED_SHORT_5_6_5_REV",
  62. GL_RGB, GL_UNSIGNED_SHORT_5_6_5_REV, 2, 0x1f, 0x7e0 },
  63. { "GL_RGB/GL_UNSIGNED_BYTE_3_3_2",
  64. GL_RGB, GL_UNSIGNED_BYTE_3_3_2, 1, 0xe0, 0x1c },
  65. { "GL_RGB/GL_UNSIGNED_BYTE_2_3_3_REV",
  66. GL_RGB, GL_UNSIGNED_BYTE_2_3_3_REV, 1, 0x7, 0x38 },
  67. { NULL, 0, 0, 0, 0, 0 }
  68. };
  69. struct name_format {
  70. const char *name;
  71. GLenum format;
  72. };
  73. static const struct name_format IntFormats[] = {
  74. { "GL_RGBA", GL_RGBA },
  75. { "GL_RGBA2", GL_RGBA2 },
  76. { "GL_RGBA4", GL_RGBA4 },
  77. { "GL_RGB5_A1", GL_RGB5_A1 },
  78. { "GL_RGBA8", GL_RGBA8 },
  79. { "GL_RGBA12", GL_RGBA12 },
  80. { "GL_RGBA16", GL_RGBA16 },
  81. { "GL_RGB10_A2", GL_RGB10_A2 },
  82. { "GL_RGB", GL_RGB },
  83. { "GL_R3_G3_B2", GL_R3_G3_B2 },
  84. { "GL_RGB4", GL_RGB4 },
  85. { "GL_RGB5", GL_RGB5 },
  86. { "GL_RGB8", GL_RGB8 },
  87. { "GL_RGB10", GL_RGB10 },
  88. { "GL_RGB12", GL_RGB12 },
  89. { "GL_RGB16", GL_RGB16 },
  90. };
  91. #define NUM_INT_FORMATS (sizeof(IntFormats) / sizeof(IntFormats[0]))
  92. static GLuint CurFormat = 0;
  93. static void
  94. PrintString(const char *s)
  95. {
  96. while (*s) {
  97. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  98. s++;
  99. }
  100. }
  101. static void
  102. MakeTexture(const struct pixel_format *format, GLenum intFormat, GLboolean swap)
  103. {
  104. GLubyte texBuffer[1000];
  105. int i;
  106. glPixelStorei(GL_UNPACK_SWAP_BYTES, swap);
  107. if (format->bytes == 1) {
  108. for (i = 0; i < 8; i++) {
  109. texBuffer[i] = format->redTexel;
  110. }
  111. for (i = 8; i < 16; i++) {
  112. texBuffer[i] = format->greenTexel;
  113. }
  114. }
  115. else if (format->bytes == 2) {
  116. GLushort *us = (GLushort *) texBuffer;
  117. for (i = 0; i < 8; i++) {
  118. us[i] = format->redTexel;
  119. }
  120. for (i = 8; i < 16; i++) {
  121. us[i] = format->greenTexel;
  122. }
  123. if (swap) {
  124. for (i = 0; i < 16; i++)
  125. us[i] = (us[i] << 8) | (us[i] >> 8);
  126. }
  127. }
  128. else if (format->bytes == 4) {
  129. GLuint *ui = (GLuint *) texBuffer;
  130. for (i = 0; i < 8; i++) {
  131. ui[i] = format->redTexel;
  132. }
  133. for (i = 8; i < 16; i++) {
  134. ui[i] = format->greenTexel;
  135. }
  136. if (swap) {
  137. for (i = 0; i < 16; i++) {
  138. GLuint b = ui[i];
  139. ui[i] = (b >> 24)
  140. | ((b >> 8) & 0xff00)
  141. | ((b << 8) & 0xff0000)
  142. | ((b << 24) & 0xff000000);
  143. }
  144. }
  145. }
  146. else {
  147. abort();
  148. }
  149. glTexImage2D(GL_TEXTURE_2D, 0, intFormat, 4, 4, 0,
  150. format->format, format->type, texBuffer);
  151. if (glGetError()) {
  152. printf("GL Error for %s\n", format->name);
  153. memset(texBuffer, 255, 1000);
  154. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0,
  155. GL_RGB, GL_UNSIGNED_BYTE, texBuffer);
  156. }
  157. }
  158. static void
  159. Draw(void)
  160. {
  161. char s[1000];
  162. int w = 350, h = 20;
  163. int i, swap;
  164. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  165. for (swap = 0; swap < 2; swap++) {
  166. for (i = 0; Formats[i].name; i++) {
  167. glPushMatrix();
  168. glTranslatef(swap * (w + 2), i * (h + 2), 0);
  169. MakeTexture(Formats + i, IntFormats[CurFormat].format, swap);
  170. glEnable(GL_TEXTURE_2D);
  171. glBegin(GL_POLYGON);
  172. glTexCoord2f(0, 0); glVertex2f(0, 0);
  173. glTexCoord2f(1, 0); glVertex2f(w, 0);
  174. glTexCoord2f(1, 1); glVertex2f(w, h);
  175. glTexCoord2f(0, 1); glVertex2f(0, h);
  176. glEnd();
  177. glDisable(GL_TEXTURE_2D);
  178. glColor3f(0, 0, 0);
  179. glRasterPos2i(8, 6);
  180. PrintString(Formats[i].name);
  181. glPopMatrix();
  182. }
  183. }
  184. glPushMatrix();
  185. glTranslatef(2, i * (h + 2), 0);
  186. glColor3f(1, 1, 1);
  187. glRasterPos2i(8, 6);
  188. PrintString("Normal");
  189. glRasterPos2i(w + 2, 6);
  190. PrintString("Byte Swapped");
  191. glPopMatrix();
  192. glPushMatrix();
  193. glTranslatef(2, (i + 1) * (h + 2), 0);
  194. glRasterPos2i(8, 6);
  195. sprintf(s, "Internal Texture Format [f/F]: %s (%d of %d)",
  196. IntFormats[CurFormat].name, CurFormat + 1, NUM_INT_FORMATS);
  197. PrintString(s);
  198. glPopMatrix();
  199. glutSwapBuffers();
  200. }
  201. static void
  202. Reshape(int width, int height)
  203. {
  204. glViewport(0, 0, width, height);
  205. glMatrixMode(GL_PROJECTION);
  206. glLoadIdentity();
  207. glOrtho(0, width, 0, height, -1, 1);
  208. glMatrixMode(GL_MODELVIEW);
  209. glLoadIdentity();
  210. }
  211. static void
  212. Key(unsigned char key, int x, int y)
  213. {
  214. (void) x;
  215. (void) y;
  216. switch (key) {
  217. case 'F':
  218. if (CurFormat == 0)
  219. CurFormat = NUM_INT_FORMATS - 1;
  220. else
  221. CurFormat--;
  222. break;
  223. case 'f':
  224. CurFormat++;
  225. if (CurFormat == NUM_INT_FORMATS)
  226. CurFormat = 0;
  227. break;
  228. case 27:
  229. exit(0);
  230. break;
  231. }
  232. glutPostRedisplay();
  233. }
  234. static void
  235. Init(void)
  236. {
  237. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  238. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  239. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  240. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  241. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  242. }
  243. int
  244. main(int argc, char *argv[])
  245. {
  246. glutInit(&argc, argv);
  247. glutInitWindowPosition(0, 0);
  248. glutInitWindowSize(700, 800);
  249. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  250. glutCreateWindow(argv[0]);
  251. glutReshapeFunc(Reshape);
  252. glutKeyboardFunc(Key);
  253. glutDisplayFunc(Draw);
  254. Init();
  255. glutMainLoop();
  256. return 0;
  257. }