Clone of mesa.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

texdown.c 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* $Id: texdown.c,v 1.1 2000/01/28 16:25:44 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 GLenum TexFormat = GL_RGBA, TexType = GL_UNSIGNED_BYTE;
  38. static GLenum TexIntFormat = GL_RGBA;
  39. static GLboolean ScaleAndBias = GL_FALSE;
  40. static GLboolean SubImage = GL_FALSE;
  41. static GLdouble DownloadRate = 0.0; /* texels/sec */
  42. static GLuint Mode = 0;
  43. static int
  44. BytesPerTexel(GLenum format, GLenum type)
  45. {
  46. int b, c;
  47. switch (type) {
  48. case GL_UNSIGNED_BYTE:
  49. b = 1;
  50. break;
  51. case GL_UNSIGNED_SHORT:
  52. b = 2;
  53. break;
  54. default:
  55. abort();
  56. }
  57. switch (format) {
  58. case GL_RGB:
  59. c = 3;
  60. break;
  61. case GL_RGBA:
  62. c = 4;
  63. break;
  64. default:
  65. abort();
  66. }
  67. return b * c;
  68. }
  69. static const char *
  70. FormatStr(GLenum format)
  71. {
  72. switch (format) {
  73. case GL_RGB:
  74. return "GL_RGB";
  75. case GL_RGBA:
  76. return "GL_RGBA";
  77. default:
  78. return "";
  79. }
  80. }
  81. static const char *
  82. TypeStr(GLenum type)
  83. {
  84. switch (type) {
  85. case GL_UNSIGNED_BYTE:
  86. return "GLubyte";
  87. case GL_UNSIGNED_SHORT:
  88. return "GLushort";
  89. default:
  90. return "";
  91. }
  92. }
  93. static void
  94. MeasureDownloadRate(void)
  95. {
  96. const int w = TexWidth + 2 * TexBorder;
  97. const int h = TexHeight + 2 * TexBorder;
  98. const int bytes = w * h * BytesPerTexel(TexFormat, TexType);
  99. GLubyte *texImage, *getImage;
  100. GLdouble t0, t1, time;
  101. int count;
  102. int i;
  103. texImage = (GLubyte *) malloc(bytes);
  104. getImage = (GLubyte *) malloc(bytes);
  105. if (!texImage || !getImage) {
  106. DownloadRate = 0.0;
  107. return;
  108. }
  109. for (i = 0; i < bytes; i++) {
  110. texImage[i] = i & 0xff;
  111. }
  112. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  113. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  114. if (ScaleAndBias) {
  115. glPixelTransferf(GL_RED_SCALE, 0.5);
  116. glPixelTransferf(GL_GREEN_SCALE, 0.5);
  117. glPixelTransferf(GL_BLUE_SCALE, 0.5);
  118. glPixelTransferf(GL_RED_BIAS, 0.5);
  119. glPixelTransferf(GL_GREEN_BIAS, 0.5);
  120. glPixelTransferf(GL_BLUE_BIAS, 0.5);
  121. }
  122. else {
  123. glPixelTransferf(GL_RED_SCALE, 1.0);
  124. glPixelTransferf(GL_GREEN_SCALE, 1.0);
  125. glPixelTransferf(GL_BLUE_SCALE, 1.0);
  126. glPixelTransferf(GL_RED_BIAS, 0.0);
  127. glPixelTransferf(GL_GREEN_BIAS, 0.0);
  128. glPixelTransferf(GL_BLUE_BIAS, 0.0);
  129. }
  130. count = 0;
  131. t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
  132. do {
  133. if (SubImage && count > 0) {
  134. glTexSubImage2D(GL_TEXTURE_2D, 0, -TexBorder, -TexBorder, w, h,
  135. TexFormat, TexType, texImage);
  136. }
  137. else {
  138. glTexImage2D(GL_TEXTURE_2D, 0, TexIntFormat, w, h,
  139. TexBorder, TexFormat, TexType, texImage);
  140. }
  141. t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
  142. time = t1 - t0;
  143. count++;
  144. } while (time < 3.0);
  145. printf("w*h=%d count=%d time=%f\n", w*h, count, time);
  146. DownloadRate = w * h * count / time;
  147. #if 0
  148. if (!ScaleAndBias) {
  149. /* verify texture readback */
  150. glGetTexImage(GL_TEXTURE_2D, 0, TexFormat, TexType, getImage);
  151. for (i = 0; i < w * h; i++) {
  152. if (texImage[i] != getImage[i]) {
  153. printf("[%d] %d != %d\n", i, texImage[i], getImage[i]);
  154. }
  155. }
  156. }
  157. #endif
  158. free(texImage);
  159. free(getImage);
  160. {
  161. GLint err = glGetError();
  162. if (err)
  163. printf("GL error %d\n", err);
  164. }
  165. }
  166. static void
  167. PrintString(const char *s)
  168. {
  169. while (*s) {
  170. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  171. s++;
  172. }
  173. }
  174. static void
  175. Display(void)
  176. {
  177. const int w = TexWidth + 2 * TexBorder;
  178. const int h = TexHeight + 2 * TexBorder;
  179. char s[1000];
  180. glClear(GL_COLOR_BUFFER_BIT);
  181. glRasterPos2i(10, 70);
  182. sprintf(s, "Texture size[cursor]: %d x %d Border[b]: %d", w, h, TexBorder);
  183. PrintString(s);
  184. glRasterPos2i(10, 55);
  185. sprintf(s, "Texture format[f]: %s Type[t]: %s IntFormat[i]: %s",
  186. FormatStr(TexFormat), TypeStr(TexType), FormatStr(TexIntFormat));
  187. PrintString(s);
  188. glRasterPos2i(10, 40);
  189. sprintf(s, "Pixel Scale&Bias[p]: %s TexSubImage[s]: %s",
  190. ScaleAndBias ? "Yes" : "No",
  191. SubImage ? "Yes" : "No");
  192. PrintString(s);
  193. if (Mode == 0) {
  194. glRasterPos2i(200, 10);
  195. sprintf(s, "...Measuring...");
  196. PrintString(s);
  197. glutSwapBuffers();
  198. glutPostRedisplay();
  199. Mode++;
  200. }
  201. else if (Mode == 1) {
  202. MeasureDownloadRate();
  203. glutPostRedisplay();
  204. Mode++;
  205. }
  206. else {
  207. /* show results */
  208. glRasterPos2i(10, 10);
  209. sprintf(s, "Download rate: %g Mtexels/second %g MB/second",
  210. DownloadRate / 1000000.0,
  211. DownloadRate * BytesPerTexel(TexFormat, TexType) / 1000000.0);
  212. PrintString(s);
  213. glutSwapBuffers();
  214. }
  215. }
  216. static void
  217. Reshape(int width, int height)
  218. {
  219. glViewport( 0, 0, width, height );
  220. glMatrixMode( GL_PROJECTION );
  221. glLoadIdentity();
  222. glOrtho(0, width, 0, height, -1, 1);
  223. glMatrixMode( GL_MODELVIEW );
  224. glLoadIdentity();
  225. }
  226. static GLenum
  227. NextFormat(GLenum format)
  228. {
  229. if (format == GL_RGB) {
  230. return GL_RGBA;
  231. }
  232. else if (format == GL_RGBA) {
  233. return GL_RGB;
  234. }
  235. else {
  236. return GL_RGBA;
  237. }
  238. }
  239. static GLenum
  240. NextType(GLenum type)
  241. {
  242. if (type == GL_UNSIGNED_BYTE) {
  243. return GL_UNSIGNED_SHORT;
  244. }
  245. else if (type == GL_UNSIGNED_SHORT) {
  246. return GL_UNSIGNED_BYTE;
  247. }
  248. else {
  249. return GL_UNSIGNED_SHORT;
  250. }
  251. }
  252. static void
  253. Key(unsigned char key, int x, int y)
  254. {
  255. (void) x;
  256. (void) y;
  257. switch (key) {
  258. case ' ':
  259. break;
  260. case 'b':
  261. /* toggle border */
  262. TexBorder = 1 - TexBorder;
  263. Mode = 0;
  264. break;
  265. case 'f':
  266. /* change format */
  267. TexFormat = NextFormat(TexFormat);
  268. Mode = 0;
  269. break;
  270. case 'i':
  271. /* change internal format */
  272. TexIntFormat = NextFormat(TexIntFormat);
  273. Mode = 0;
  274. break;
  275. case 'p':
  276. /* toggle border */
  277. ScaleAndBias = !ScaleAndBias;
  278. Mode = 0;
  279. break;
  280. case 's':
  281. SubImage = !SubImage;
  282. Mode = 0;
  283. break;
  284. case 't':
  285. /* change type */
  286. TexType = NextType(TexType);
  287. Mode = 0;
  288. break;
  289. case 27:
  290. exit(0);
  291. break;
  292. }
  293. glutPostRedisplay();
  294. }
  295. static void
  296. SpecialKey(int key, int x, int y)
  297. {
  298. (void) x;
  299. (void) y;
  300. switch (key) {
  301. case GLUT_KEY_UP:
  302. if (TexHeight < MaxSize)
  303. TexHeight *= 2;
  304. break;
  305. case GLUT_KEY_DOWN:
  306. if (TexHeight > 1)
  307. TexHeight /= 2;
  308. break;
  309. case GLUT_KEY_LEFT:
  310. if (TexWidth > 1)
  311. TexWidth /= 2;
  312. break;
  313. case GLUT_KEY_RIGHT:
  314. if (TexWidth < MaxSize)
  315. TexWidth *= 2;
  316. break;
  317. }
  318. Mode = 0;
  319. glutPostRedisplay();
  320. }
  321. static void
  322. Init(void)
  323. {
  324. printf("GL_VENDOR = %s\n", (const char *) glGetString(GL_VENDOR));
  325. printf("GL_VERSION = %s\n", (const char *) glGetString(GL_VERSION));
  326. printf("GL_RENDERER = %s\n", (const char *) glGetString(GL_RENDERER));
  327. }
  328. int
  329. main(int argc, char *argv[])
  330. {
  331. glutInit( &argc, argv );
  332. glutInitWindowPosition( 0, 0 );
  333. glutInitWindowSize( 600, 100 );
  334. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  335. glutCreateWindow(argv[0]);
  336. glutReshapeFunc( Reshape );
  337. glutKeyboardFunc( Key );
  338. glutSpecialFunc( SpecialKey );
  339. glutDisplayFunc( Display );
  340. Init();
  341. glutMainLoop();
  342. return 0;
  343. }