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.3 2000/03/29 18:02:52 brianp 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. count = 0;
  127. t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
  128. do {
  129. if (SubImage && count > 0) {
  130. glTexSubImage2D(GL_TEXTURE_2D, 0, -TexBorder, -TexBorder, w, h,
  131. FormatTable[Format].Format,
  132. FormatTable[Format].Type, texImage);
  133. }
  134. else {
  135. glTexImage2D(GL_TEXTURE_2D, 0,
  136. FormatTable[Format].IntFormat, w, h, TexBorder,
  137. FormatTable[Format].Format,
  138. FormatTable[Format].Type, texImage);
  139. }
  140. if (count == 0) {
  141. /* draw a tiny polygon to force texture into texram */
  142. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  143. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  144. glEnable(GL_TEXTURE_2D);
  145. glBegin(GL_TRIANGLES);
  146. glTexCoord2f(0, 0); glVertex2f(1, 1);
  147. glTexCoord2f(1, 0); glVertex2f(3, 1);
  148. glTexCoord2f(0.5, 1); glVertex2f(2, 3);
  149. glEnd();
  150. glDisable(GL_TEXTURE_2D);
  151. }
  152. t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
  153. time = t1 - t0;
  154. count++;
  155. } while (time < 3.0);
  156. printf("w*h=%d count=%d time=%f\n", w*h, count, time);
  157. DownloadRate = w * h * count / time;
  158. #if 0
  159. if (!ScaleAndBias) {
  160. /* verify texture readback */
  161. glGetTexImage(GL_TEXTURE_2D, 0,
  162. FormatTable[Format].Format,
  163. FormatTable[Format].Type, getImage);
  164. for (i = 0; i < w * h; i++) {
  165. if (texImage[i] != getImage[i]) {
  166. printf("[%d] %d != %d\n", i, texImage[i], getImage[i]);
  167. }
  168. }
  169. }
  170. #endif
  171. free(texImage);
  172. free(getImage);
  173. {
  174. GLint err = glGetError();
  175. if (err)
  176. printf("GL error %d\n", err);
  177. }
  178. }
  179. static void
  180. PrintString(const char *s)
  181. {
  182. while (*s) {
  183. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  184. s++;
  185. }
  186. }
  187. static void
  188. Display(void)
  189. {
  190. const int w = TexWidth + 2 * TexBorder;
  191. const int h = TexHeight + 2 * TexBorder;
  192. char s[1000];
  193. glClear(GL_COLOR_BUFFER_BIT);
  194. glRasterPos2i(10, 80);
  195. sprintf(s, "Texture size[cursor]: %d x %d Border[b]: %d", w, h, TexBorder);
  196. PrintString(s);
  197. glRasterPos2i(10, 65);
  198. sprintf(s, "Format[f]: %s Type: %s IntFormat: %s",
  199. FormatStr(FormatTable[Format].Format),
  200. TypeStr( FormatTable[Format].Type),
  201. FormatStr(FormatTable[Format].IntFormat));
  202. PrintString(s);
  203. glRasterPos2i(10, 50);
  204. sprintf(s, "Pixel Scale&Bias[p]: %s TexSubImage[s]: %s",
  205. ScaleAndBias ? "Yes" : "No",
  206. SubImage ? "Yes" : "No");
  207. PrintString(s);
  208. if (Mode == 0) {
  209. glRasterPos2i(200, 10);
  210. sprintf(s, "...Measuring...");
  211. PrintString(s);
  212. glutSwapBuffers();
  213. glutPostRedisplay();
  214. Mode++;
  215. }
  216. else if (Mode == 1) {
  217. MeasureDownloadRate();
  218. glutPostRedisplay();
  219. Mode++;
  220. }
  221. else {
  222. /* show results */
  223. glRasterPos2i(10, 10);
  224. sprintf(s, "Download rate: %g Mtexels/second %g MB/second",
  225. DownloadRate / 1000000.0,
  226. DownloadRate * BytesPerTexel(Format) / 1000000.0);
  227. PrintString(s);
  228. {
  229. GLint r, g, b, a, l, i;
  230. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_RED_SIZE, &r);
  231. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_SIZE, &g);
  232. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BLUE_SIZE, &b);
  233. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_SIZE, &a);
  234. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_LUMINANCE_SIZE, &l);
  235. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTENSITY_SIZE, &i);
  236. sprintf(s, "TexelBits: R=%d G=%d B=%d A=%d L=%d I=%d", r, g, b, a, l, i);
  237. glRasterPos2i(10, 25);
  238. PrintString(s);
  239. }
  240. glutSwapBuffers();
  241. }
  242. }
  243. static void
  244. Reshape(int width, int height)
  245. {
  246. glViewport( 0, 0, width, height );
  247. glMatrixMode( GL_PROJECTION );
  248. glLoadIdentity();
  249. glOrtho(0, width, 0, height, -1, 1);
  250. glMatrixMode( GL_MODELVIEW );
  251. glLoadIdentity();
  252. }
  253. static void
  254. Key(unsigned char key, int x, int y)
  255. {
  256. (void) x;
  257. (void) y;
  258. switch (key) {
  259. case ' ':
  260. Mode = 0;
  261. break;
  262. case 'b':
  263. /* toggle border */
  264. TexBorder = 1 - TexBorder;
  265. Mode = 0;
  266. break;
  267. case 'f':
  268. /* change format */
  269. Format = (Format + 1) % NUM_FORMATS;
  270. Mode = 0;
  271. break;
  272. case 'p':
  273. /* toggle border */
  274. ScaleAndBias = !ScaleAndBias;
  275. Mode = 0;
  276. break;
  277. case 's':
  278. SubImage = !SubImage;
  279. Mode = 0;
  280. break;
  281. case 27:
  282. exit(0);
  283. break;
  284. }
  285. glutPostRedisplay();
  286. }
  287. static void
  288. SpecialKey(int key, int x, int y)
  289. {
  290. (void) x;
  291. (void) y;
  292. switch (key) {
  293. case GLUT_KEY_UP:
  294. if (TexHeight < MaxSize)
  295. TexHeight *= 2;
  296. break;
  297. case GLUT_KEY_DOWN:
  298. if (TexHeight > 1)
  299. TexHeight /= 2;
  300. break;
  301. case GLUT_KEY_LEFT:
  302. if (TexWidth > 1)
  303. TexWidth /= 2;
  304. break;
  305. case GLUT_KEY_RIGHT:
  306. if (TexWidth < MaxSize)
  307. TexWidth *= 2;
  308. break;
  309. }
  310. Mode = 0;
  311. glutPostRedisplay();
  312. }
  313. static void
  314. Init(void)
  315. {
  316. printf("GL_VENDOR = %s\n", (const char *) glGetString(GL_VENDOR));
  317. printf("GL_VERSION = %s\n", (const char *) glGetString(GL_VERSION));
  318. printf("GL_RENDERER = %s\n", (const char *) glGetString(GL_RENDERER));
  319. }
  320. int
  321. main(int argc, char *argv[])
  322. {
  323. glutInit( &argc, argv );
  324. glutInitWindowPosition( 0, 0 );
  325. glutInitWindowSize( 600, 100 );
  326. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  327. glutCreateWindow(argv[0]);
  328. glutReshapeFunc( Reshape );
  329. glutKeyboardFunc( Key );
  330. glutSpecialFunc( SpecialKey );
  331. glutDisplayFunc( Display );
  332. Init();
  333. glutMainLoop();
  334. return 0;
  335. }