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.

texdown.c 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* $Id: texdown.c,v 1.4 2001/02/07 03:04:58 gareth Exp $ */
  2. /*
  3. * Copyright (C) 1999 Brian Paul All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  19. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. /*
  23. * texdown
  24. *
  25. * Measure texture download speed.
  26. * Use keyboard to change texture size, format, datatype, scale/bias,
  27. * subimageload, etc.
  28. *
  29. * Brian Paul 28 January 2000
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <math.h>
  34. #include <GL/glut.h>
  35. static GLsizei MaxSize = 1024;
  36. static GLsizei TexWidth = 256, TexHeight = 256, TexBorder = 0;
  37. static GLboolean ScaleAndBias = GL_FALSE;
  38. static GLboolean SubImage = GL_FALSE;
  39. static GLdouble DownloadRate = 0.0; /* texels/sec */
  40. static GLuint Mode = 0;
  41. #define NUM_FORMATS 4
  42. struct FormatRec {
  43. GLenum Format;
  44. GLenum Type;
  45. GLenum IntFormat;
  46. GLint TexelSize;
  47. };
  48. static const struct FormatRec FormatTable[NUM_FORMATS] = {
  49. /* Format Type IntFormat TexelSize */
  50. { GL_RGB, GL_UNSIGNED_BYTE, GL_RGB, 3 },
  51. { GL_RGBA, GL_UNSIGNED_BYTE, GL_RGBA, 4 },
  52. { GL_RGBA, GL_UNSIGNED_BYTE, GL_RGB, 4 },
  53. { GL_RGB, GL_UNSIGNED_SHORT_5_6_5, GL_RGB, 2 },
  54. };
  55. static GLint Format;
  56. static int
  57. BytesPerTexel(GLint format)
  58. {
  59. return FormatTable[format].TexelSize;
  60. }
  61. static const char *
  62. FormatStr(GLenum format)
  63. {
  64. switch (format) {
  65. case GL_RGB:
  66. return "GL_RGB";
  67. case GL_RGBA:
  68. return "GL_RGBA";
  69. default:
  70. return "";
  71. }
  72. }
  73. static const char *
  74. TypeStr(GLenum type)
  75. {
  76. switch (type) {
  77. case GL_UNSIGNED_BYTE:
  78. return "GL_UNSIGNED_BYTE";
  79. case GL_UNSIGNED_SHORT:
  80. return "GL_UNSIGNED_SHORT";
  81. case GL_UNSIGNED_SHORT_5_6_5:
  82. return "GL_UNSIGNED_SHORT_5_6_5";
  83. case GL_UNSIGNED_SHORT_5_6_5_REV:
  84. return "GL_UNSIGNED_SHORT_5_6_5_REV";
  85. default:
  86. return "";
  87. }
  88. }
  89. static void
  90. MeasureDownloadRate(void)
  91. {
  92. const int w = TexWidth + 2 * TexBorder;
  93. const int h = TexHeight + 2 * TexBorder;
  94. const int bytes = w * h * BytesPerTexel(Format);
  95. GLubyte *texImage, *getImage;
  96. GLdouble t0, t1, time;
  97. int count;
  98. int i;
  99. texImage = (GLubyte *) malloc(bytes);
  100. getImage = (GLubyte *) malloc(bytes);
  101. if (!texImage || !getImage) {
  102. DownloadRate = 0.0;
  103. return;
  104. }
  105. for (i = 0; i < bytes; i++) {
  106. texImage[i] = i & 0xff;
  107. }
  108. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  109. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  110. if (ScaleAndBias) {
  111. glPixelTransferf(GL_RED_SCALE, 0.5);
  112. glPixelTransferf(GL_GREEN_SCALE, 0.5);
  113. glPixelTransferf(GL_BLUE_SCALE, 0.5);
  114. glPixelTransferf(GL_RED_BIAS, 0.5);
  115. glPixelTransferf(GL_GREEN_BIAS, 0.5);
  116. glPixelTransferf(GL_BLUE_BIAS, 0.5);
  117. }
  118. else {
  119. glPixelTransferf(GL_RED_SCALE, 1.0);
  120. glPixelTransferf(GL_GREEN_SCALE, 1.0);
  121. glPixelTransferf(GL_BLUE_SCALE, 1.0);
  122. glPixelTransferf(GL_RED_BIAS, 0.0);
  123. glPixelTransferf(GL_GREEN_BIAS, 0.0);
  124. glPixelTransferf(GL_BLUE_BIAS, 0.0);
  125. }
  126. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  127. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  128. glEnable(GL_TEXTURE_2D);
  129. count = 0;
  130. t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
  131. do {
  132. if (SubImage && count > 0) {
  133. glTexSubImage2D(GL_TEXTURE_2D, 0, -TexBorder, -TexBorder, w, h,
  134. FormatTable[Format].Format,
  135. FormatTable[Format].Type, texImage);
  136. }
  137. else {
  138. glTexImage2D(GL_TEXTURE_2D, 0,
  139. FormatTable[Format].IntFormat, w, h, TexBorder,
  140. FormatTable[Format].Format,
  141. FormatTable[Format].Type, texImage);
  142. }
  143. /* draw a tiny polygon to force texture into texram */
  144. glBegin(GL_TRIANGLES);
  145. glTexCoord2f(0, 0); glVertex2f(1, 1);
  146. glTexCoord2f(1, 0); glVertex2f(3, 1);
  147. glTexCoord2f(0.5, 1); glVertex2f(2, 3);
  148. glEnd();
  149. t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
  150. time = t1 - t0;
  151. count++;
  152. } while (time < 3.0);
  153. glDisable(GL_TEXTURE_2D);
  154. printf("w*h=%d count=%d time=%f\n", w*h, count, time);
  155. DownloadRate = w * h * count / time;
  156. #if 0
  157. if (!ScaleAndBias) {
  158. /* verify texture readback */
  159. glGetTexImage(GL_TEXTURE_2D, 0,
  160. FormatTable[Format].Format,
  161. FormatTable[Format].Type, getImage);
  162. for (i = 0; i < w * h; i++) {
  163. if (texImage[i] != getImage[i]) {
  164. printf("[%d] %d != %d\n", i, texImage[i], getImage[i]);
  165. }
  166. }
  167. }
  168. #endif
  169. free(texImage);
  170. free(getImage);
  171. {
  172. GLint err = glGetError();
  173. if (err)
  174. printf("GL error %d\n", err);
  175. }
  176. }
  177. static void
  178. PrintString(const char *s)
  179. {
  180. while (*s) {
  181. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  182. s++;
  183. }
  184. }
  185. static void
  186. Display(void)
  187. {
  188. const int w = TexWidth + 2 * TexBorder;
  189. const int h = TexHeight + 2 * TexBorder;
  190. char s[1000];
  191. glClear(GL_COLOR_BUFFER_BIT);
  192. glRasterPos2i(10, 80);
  193. sprintf(s, "Texture size[cursor]: %d x %d Border[b]: %d", w, h, TexBorder);
  194. PrintString(s);
  195. glRasterPos2i(10, 65);
  196. sprintf(s, "Format[f]: %s Type: %s IntFormat: %s",
  197. FormatStr(FormatTable[Format].Format),
  198. TypeStr( FormatTable[Format].Type),
  199. FormatStr(FormatTable[Format].IntFormat));
  200. PrintString(s);
  201. glRasterPos2i(10, 50);
  202. sprintf(s, "Pixel Scale&Bias[p]: %s TexSubImage[s]: %s",
  203. ScaleAndBias ? "Yes" : "No",
  204. SubImage ? "Yes" : "No");
  205. PrintString(s);
  206. if (Mode == 0) {
  207. glRasterPos2i(200, 10);
  208. sprintf(s, "...Measuring...");
  209. PrintString(s);
  210. glutSwapBuffers();
  211. glutPostRedisplay();
  212. Mode++;
  213. }
  214. else if (Mode == 1) {
  215. MeasureDownloadRate();
  216. glutPostRedisplay();
  217. Mode++;
  218. }
  219. else {
  220. /* show results */
  221. glRasterPos2i(10, 10);
  222. sprintf(s, "Download rate: %g Mtexels/second %g MB/second",
  223. DownloadRate / 1000000.0,
  224. DownloadRate * BytesPerTexel(Format) / 1000000.0);
  225. PrintString(s);
  226. {
  227. GLint r, g, b, a, l, i;
  228. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_RED_SIZE, &r);
  229. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_SIZE, &g);
  230. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BLUE_SIZE, &b);
  231. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_SIZE, &a);
  232. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_LUMINANCE_SIZE, &l);
  233. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTENSITY_SIZE, &i);
  234. sprintf(s, "TexelBits: R=%d G=%d B=%d A=%d L=%d I=%d", r, g, b, a, l, i);
  235. glRasterPos2i(10, 25);
  236. PrintString(s);
  237. }
  238. glutSwapBuffers();
  239. }
  240. }
  241. static void
  242. Reshape(int width, int height)
  243. {
  244. glViewport( 0, 0, width, height );
  245. glMatrixMode( GL_PROJECTION );
  246. glLoadIdentity();
  247. glOrtho(0, width, 0, height, -1, 1);
  248. glMatrixMode( GL_MODELVIEW );
  249. glLoadIdentity();
  250. }
  251. static void
  252. Key(unsigned char key, int x, int y)
  253. {
  254. (void) x;
  255. (void) y;
  256. switch (key) {
  257. case ' ':
  258. Mode = 0;
  259. break;
  260. case 'b':
  261. /* toggle border */
  262. TexBorder = 1 - TexBorder;
  263. Mode = 0;
  264. break;
  265. case 'f':
  266. /* change format */
  267. Format = (Format + 1) % NUM_FORMATS;
  268. Mode = 0;
  269. break;
  270. case 'p':
  271. /* toggle border */
  272. ScaleAndBias = !ScaleAndBias;
  273. Mode = 0;
  274. break;
  275. case 's':
  276. SubImage = !SubImage;
  277. Mode = 0;
  278. break;
  279. case 27:
  280. exit(0);
  281. break;
  282. }
  283. glutPostRedisplay();
  284. }
  285. static void
  286. SpecialKey(int key, int x, int y)
  287. {
  288. (void) x;
  289. (void) y;
  290. switch (key) {
  291. case GLUT_KEY_UP:
  292. if (TexHeight < MaxSize)
  293. TexHeight *= 2;
  294. break;
  295. case GLUT_KEY_DOWN:
  296. if (TexHeight > 1)
  297. TexHeight /= 2;
  298. break;
  299. case GLUT_KEY_LEFT:
  300. if (TexWidth > 1)
  301. TexWidth /= 2;
  302. break;
  303. case GLUT_KEY_RIGHT:
  304. if (TexWidth < MaxSize)
  305. TexWidth *= 2;
  306. break;
  307. }
  308. Mode = 0;
  309. glutPostRedisplay();
  310. }
  311. static void
  312. Init(void)
  313. {
  314. printf("GL_VENDOR = %s\n", (const char *) glGetString(GL_VENDOR));
  315. printf("GL_VERSION = %s\n", (const char *) glGetString(GL_VERSION));
  316. printf("GL_RENDERER = %s\n", (const char *) glGetString(GL_RENDERER));
  317. }
  318. int
  319. main(int argc, char *argv[])
  320. {
  321. glutInit( &argc, argv );
  322. glutInitWindowPosition( 0, 0 );
  323. glutInitWindowSize( 600, 100 );
  324. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  325. glutCreateWindow(argv[0]);
  326. glutReshapeFunc( Reshape );
  327. glutKeyboardFunc( Key );
  328. glutSpecialFunc( SpecialKey );
  329. glutDisplayFunc( Display );
  330. Init();
  331. glutMainLoop();
  332. return 0;
  333. }