Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included
  12. * in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * VMWARE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  18. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. /**
  22. * Common perf code. This should be re-usable with other APIs.
  23. */
  24. #include "common.h"
  25. #include "glmain.h"
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <stdarg.h>
  29. #if defined(_MSC_VER)
  30. #define snprintf _snprintf
  31. #endif
  32. /* Need to add a fflush windows console with mingw, otherwise nothing
  33. * shows up until program exit. May want to add logging here.
  34. */
  35. void
  36. perf_printf(const char *format, ...)
  37. {
  38. va_list ap;
  39. va_start(ap, format);
  40. fflush(stdout);
  41. vfprintf(stdout, format, ap);
  42. fflush(stdout);
  43. va_end(ap);
  44. }
  45. /**
  46. * Run function 'f' for enough iterations to reach a steady state.
  47. * Return the rate (iterations/second).
  48. */
  49. double
  50. PerfMeasureRate(PerfRateFunc f)
  51. {
  52. const double minDuration = 1.0;
  53. double rate = 0.0, prevRate = 0.0;
  54. unsigned subiters;
  55. /* Compute initial number of iterations to try.
  56. * If the test function is pretty slow this helps to avoid
  57. * extraordarily long run times.
  58. */
  59. subiters = 2;
  60. {
  61. const double t0 = PerfGetTime();
  62. double t1;
  63. do {
  64. f(subiters); /* call the rendering function */
  65. t1 = PerfGetTime();
  66. subiters *= 2;
  67. } while (t1 - t0 < 0.1 * minDuration);
  68. }
  69. /*perf_printf("initial subIters = %u\n", subiters);*/
  70. while (1) {
  71. const double t0 = PerfGetTime();
  72. unsigned iters = 0;
  73. double t1;
  74. do {
  75. f(subiters); /* call the rendering function */
  76. t1 = PerfGetTime();
  77. iters += subiters;
  78. } while (t1 - t0 < minDuration);
  79. rate = iters / (t1 - t0);
  80. if (0)
  81. perf_printf("prevRate %f rate %f ratio %f iters %u\n",
  82. prevRate, rate, rate/prevRate, iters);
  83. /* Try and speed the search up by skipping a few steps:
  84. */
  85. if (rate > prevRate * 1.6)
  86. subiters *= 8;
  87. else if (rate > prevRate * 1.2)
  88. subiters *= 4;
  89. else if (rate > prevRate * 1.05)
  90. subiters *= 2;
  91. else
  92. break;
  93. prevRate = rate;
  94. }
  95. if (0)
  96. perf_printf("%s returning iters %u rate %f\n", __FUNCTION__, subiters, rate);
  97. return rate;
  98. }
  99. /* Note static buffer, can only use once per printf.
  100. */
  101. const char *
  102. PerfHumanFloat( double d )
  103. {
  104. static char buf[80];
  105. if (d > 1000000000.0)
  106. snprintf(buf, sizeof(buf), "%.1f billion", d / 1000000000.0);
  107. else if (d > 1000000.0)
  108. snprintf(buf, sizeof(buf), "%.1f million", d / 1000000.0);
  109. else if (d > 1000.0)
  110. snprintf(buf, sizeof(buf), "%.1f thousand", d / 1000.0);
  111. else
  112. snprintf(buf, sizeof(buf), "%.1f", d);
  113. return buf;
  114. }