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 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 GLboolean Test3D = GL_FALSE;
  94. static void
  95. PrintString(const char *s)
  96. {
  97. while (*s) {
  98. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  99. s++;
  100. }
  101. }
  102. static void
  103. MakeTexture(const struct pixel_format *format, GLenum intFormat, GLboolean swap)
  104. {
  105. GLubyte texBuffer[1000];
  106. int i;
  107. glPixelStorei(GL_UNPACK_SWAP_BYTES, swap);
  108. if (format->bytes == 1) {
  109. for (i = 0; i < 8; i++) {
  110. texBuffer[i] = format->redTexel;
  111. }
  112. for (i = 8; i < 16; i++) {
  113. texBuffer[i] = format->greenTexel;
  114. }
  115. }
  116. else if (format->bytes == 2) {
  117. GLushort *us = (GLushort *) texBuffer;
  118. for (i = 0; i < 8; i++) {
  119. us[i] = format->redTexel;
  120. }
  121. for (i = 8; i < 16; i++) {
  122. us[i] = format->greenTexel;
  123. }
  124. if (swap) {
  125. for (i = 0; i < 16; i++)
  126. us[i] = (us[i] << 8) | (us[i] >> 8);
  127. }
  128. }
  129. else if (format->bytes == 4) {
  130. GLuint *ui = (GLuint *) texBuffer;
  131. for (i = 0; i < 8; i++) {
  132. ui[i] = format->redTexel;
  133. }
  134. for (i = 8; i < 16; i++) {
  135. ui[i] = format->greenTexel;
  136. }
  137. if (swap) {
  138. for (i = 0; i < 16; i++) {
  139. GLuint b = ui[i];
  140. ui[i] = (b >> 24)
  141. | ((b >> 8) & 0xff00)
  142. | ((b << 8) & 0xff0000)
  143. | ((b << 24) & 0xff000000);
  144. }
  145. }
  146. }
  147. else {
  148. abort();
  149. }
  150. if (Test3D) {
  151. /* 4 x 4 x 4 texture, undefined data */
  152. glTexImage3D(GL_TEXTURE_3D, 0, intFormat, 4, 4, 4, 0,
  153. format->format, format->type, NULL);
  154. /* fill in Z=1 and Z=2 slices with the real texture data */
  155. glTexSubImage3D(GL_TEXTURE_3D, 0,
  156. 0, 0, 1, /* offset */
  157. 4, 4, 1, /* size */
  158. format->format, format->type, texBuffer);
  159. glTexSubImage3D(GL_TEXTURE_3D, 0,
  160. 0, 0, 2, /* offset */
  161. 4, 4, 1, /* size */
  162. format->format, format->type, texBuffer);
  163. }
  164. else {
  165. glTexImage2D(GL_TEXTURE_2D, 0, intFormat, 4, 4, 0,
  166. format->format, format->type, texBuffer);
  167. }
  168. if (glGetError()) {
  169. printf("GL Error for %s\n", format->name);
  170. memset(texBuffer, 255, 1000);
  171. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0,
  172. GL_RGB, GL_UNSIGNED_BYTE, texBuffer);
  173. }
  174. }
  175. static void
  176. Draw(void)
  177. {
  178. char s[1000];
  179. int w = 350, h = 20;
  180. int i, swap;
  181. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  182. for (swap = 0; swap < 2; swap++) {
  183. for (i = 0; Formats[i].name; i++) {
  184. glPushMatrix();
  185. glTranslatef(swap * (w + 2), i * (h + 2), 0);
  186. MakeTexture(Formats + i, IntFormats[CurFormat].format, swap);
  187. if (Test3D)
  188. glEnable(GL_TEXTURE_3D);
  189. else
  190. glEnable(GL_TEXTURE_2D);
  191. glBegin(GL_POLYGON);
  192. glTexCoord3f(0, 0, 0.5); glVertex2f(0, 0);
  193. glTexCoord3f(1, 0, 0.5); glVertex2f(w, 0);
  194. glTexCoord3f(1, 1, 0.5); glVertex2f(w, h);
  195. glTexCoord3f(0, 1, 0.5); glVertex2f(0, h);
  196. glEnd();
  197. if (Test3D)
  198. glDisable(GL_TEXTURE_3D);
  199. else
  200. glDisable(GL_TEXTURE_2D);
  201. glColor3f(0, 0, 0);
  202. glRasterPos2i(8, 6);
  203. PrintString(Formats[i].name);
  204. glPopMatrix();
  205. }
  206. }
  207. glPushMatrix();
  208. glTranslatef(2, i * (h + 2), 0);
  209. glColor3f(1, 1, 1);
  210. glRasterPos2i(8, 6);
  211. PrintString("Normal");
  212. glRasterPos2i(w + 2, 6);
  213. PrintString("Byte Swapped");
  214. glPopMatrix();
  215. glPushMatrix();
  216. glTranslatef(2, (i + 1) * (h + 2), 0);
  217. glRasterPos2i(8, 6);
  218. sprintf(s, "Internal Texture Format [f/F]: %s (%d of %d)",
  219. IntFormats[CurFormat].name, CurFormat + 1, NUM_INT_FORMATS);
  220. PrintString(s);
  221. glPopMatrix();
  222. glPushMatrix();
  223. glTranslatef(2, (i + 2) * (h + 2), 0);
  224. glRasterPos2i(8, 6);
  225. if (Test3D)
  226. PrintString("Target [2/3]: GL_TEXTURE_3D");
  227. else
  228. PrintString("Target [2/3]: GL_TEXTURE_2D");
  229. glPopMatrix();
  230. glutSwapBuffers();
  231. }
  232. static void
  233. Reshape(int width, int height)
  234. {
  235. glViewport(0, 0, width, height);
  236. glMatrixMode(GL_PROJECTION);
  237. glLoadIdentity();
  238. glOrtho(0, width, 0, height, -1, 1);
  239. glMatrixMode(GL_MODELVIEW);
  240. glLoadIdentity();
  241. }
  242. static void
  243. Key(unsigned char key, int x, int y)
  244. {
  245. (void) x;
  246. (void) y;
  247. switch (key) {
  248. case 'F':
  249. if (CurFormat == 0)
  250. CurFormat = NUM_INT_FORMATS - 1;
  251. else
  252. CurFormat--;
  253. break;
  254. case 'f':
  255. CurFormat++;
  256. if (CurFormat == NUM_INT_FORMATS)
  257. CurFormat = 0;
  258. break;
  259. case '2':
  260. Test3D = GL_FALSE;
  261. break;
  262. case '3':
  263. Test3D = GL_TRUE;
  264. break;
  265. case 27:
  266. exit(0);
  267. break;
  268. }
  269. glutPostRedisplay();
  270. }
  271. static void
  272. Init(void)
  273. {
  274. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  275. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  276. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  277. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  278. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  279. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  280. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  281. }
  282. int
  283. main(int argc, char *argv[])
  284. {
  285. glutInit(&argc, argv);
  286. glutInitWindowPosition(0, 0);
  287. glutInitWindowSize(700, 800);
  288. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  289. glutCreateWindow(argv[0]);
  290. glutReshapeFunc(Reshape);
  291. glutKeyboardFunc(Key);
  292. glutDisplayFunc(Draw);
  293. Init();
  294. glutMainLoop();
  295. return 0;
  296. }