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.

vbo.c 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. * Measure VBO upload speed.
  23. * That is, measure glBufferDataARB() and glBufferSubDataARB().
  24. *
  25. * Brian Paul
  26. * 16 Sep 2009
  27. */
  28. #include <string.h>
  29. #include "glmain.h"
  30. #include "common.h"
  31. /* Copy data out of a large array to avoid caching effects:
  32. */
  33. #define DATA_SIZE (16*1024*1024)
  34. int WinWidth = 100, WinHeight = 100;
  35. static GLuint VBO;
  36. static GLsizei VBOSize = 0;
  37. static GLsizei SubSize = 0;
  38. static GLubyte *VBOData = NULL; /* array[DATA_SIZE] */
  39. static const GLboolean DrawPoint = GL_TRUE;
  40. static const GLboolean BufferSubDataInHalves = GL_TRUE;
  41. static const GLfloat Vertex0[2] = { 0.0, 0.0 };
  42. /** Called from test harness/main */
  43. void
  44. PerfInit(void)
  45. {
  46. /* setup VBO */
  47. glGenBuffersARB(1, &VBO);
  48. glBindBufferARB(GL_ARRAY_BUFFER_ARB, VBO);
  49. glVertexPointer(2, GL_FLOAT, sizeof(Vertex0), (void *) 0);
  50. glEnableClientState(GL_VERTEX_ARRAY);
  51. }
  52. static void
  53. UploadVBO(unsigned count)
  54. {
  55. unsigned i;
  56. unsigned total = 0;
  57. unsigned src = 0;
  58. for (i = 0; i < count; i++) {
  59. glBufferDataARB(GL_ARRAY_BUFFER, VBOSize, VBOData + src, GL_STREAM_DRAW_ARB);
  60. glDrawArrays(GL_POINTS, 0, 1);
  61. /* Throw in an occasional flush to work around a driver crash:
  62. */
  63. total += VBOSize;
  64. if (total >= 16*1024*1024) {
  65. glFlush();
  66. total = 0;
  67. }
  68. src += VBOSize;
  69. src %= DATA_SIZE;
  70. }
  71. glFinish();
  72. }
  73. static void
  74. UploadSubVBO(unsigned count)
  75. {
  76. unsigned i;
  77. unsigned src = 0;
  78. for (i = 0; i < count; i++) {
  79. unsigned offset = (i * SubSize) % VBOSize;
  80. glBufferSubDataARB(GL_ARRAY_BUFFER, offset, SubSize, VBOData + src);
  81. if (DrawPoint) {
  82. glDrawArrays(GL_POINTS, offset / sizeof(Vertex0), 1);
  83. }
  84. src += SubSize;
  85. src %= DATA_SIZE;
  86. }
  87. glFinish();
  88. }
  89. /* Do multiple small SubData uploads, then call DrawArrays. This may be a
  90. * fairer comparison to back-to-back BufferData calls:
  91. */
  92. static void
  93. BatchUploadSubVBO(unsigned count)
  94. {
  95. unsigned i = 0, j;
  96. unsigned period = VBOSize / SubSize;
  97. unsigned src = 0;
  98. while (i < count) {
  99. for (j = 0; j < period && i < count; j++, i++) {
  100. unsigned offset = j * SubSize;
  101. glBufferSubDataARB(GL_ARRAY_BUFFER, offset, SubSize, VBOData + src);
  102. }
  103. glDrawArrays(GL_POINTS, 0, 1);
  104. src += SubSize;
  105. src %= DATA_SIZE;
  106. }
  107. glFinish();
  108. }
  109. /**
  110. * Test the sequence:
  111. * create/load VBO
  112. * draw
  113. * destroy VBO
  114. */
  115. static void
  116. CreateDrawDestroyVBO(unsigned count)
  117. {
  118. unsigned i;
  119. for (i = 0; i < count; i++) {
  120. GLuint vbo;
  121. /* create/load */
  122. glGenBuffersARB(1, &vbo);
  123. glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo);
  124. glBufferDataARB(GL_ARRAY_BUFFER, VBOSize, VBOData, GL_STREAM_DRAW_ARB);
  125. /* draw */
  126. glVertexPointer(2, GL_FLOAT, sizeof(Vertex0), (void *) 0);
  127. glDrawArrays(GL_POINTS, 0, 1);
  128. /* destroy */
  129. glDeleteBuffersARB(1, &vbo);
  130. }
  131. glFinish();
  132. }
  133. static const GLsizei Sizes[] = {
  134. 64,
  135. 1024,
  136. 16*1024,
  137. 256*1024,
  138. 1024*1024,
  139. 16*1024*1024,
  140. 0 /* end of list */
  141. };
  142. void
  143. PerfNextRound(void)
  144. {
  145. }
  146. /** Called from test harness/main */
  147. void
  148. PerfDraw(void)
  149. {
  150. double rate, mbPerSec;
  151. int i, sz;
  152. /* Load VBOData buffer with duplicated Vertex0.
  153. */
  154. VBOData = calloc(DATA_SIZE, 1);
  155. for (i = 0; i < DATA_SIZE / sizeof(Vertex0); i++) {
  156. memcpy(VBOData + i * sizeof(Vertex0),
  157. Vertex0,
  158. sizeof(Vertex0));
  159. }
  160. /* glBufferDataARB()
  161. */
  162. for (sz = 0; Sizes[sz]; sz++) {
  163. SubSize = VBOSize = Sizes[sz];
  164. rate = PerfMeasureRate(UploadVBO);
  165. mbPerSec = rate * VBOSize / (1024.0 * 1024.0);
  166. perf_printf(" glBufferDataARB(size = %d): %.1f MB/sec\n",
  167. VBOSize, mbPerSec);
  168. }
  169. /* glBufferSubDataARB()
  170. */
  171. for (sz = 0; Sizes[sz]; sz++) {
  172. SubSize = VBOSize = Sizes[sz];
  173. rate = PerfMeasureRate(UploadSubVBO);
  174. mbPerSec = rate * VBOSize / (1024.0 * 1024.0);
  175. perf_printf(" glBufferSubDataARB(size = %d): %.1f MB/sec\n",
  176. VBOSize, mbPerSec);
  177. }
  178. /* Batch upload
  179. */
  180. VBOSize = 1024 * 1024;
  181. glBufferDataARB(GL_ARRAY_BUFFER, VBOSize, VBOData, GL_STREAM_DRAW_ARB);
  182. for (sz = 0; Sizes[sz] < VBOSize; sz++) {
  183. SubSize = Sizes[sz];
  184. rate = PerfMeasureRate(UploadSubVBO);
  185. mbPerSec = rate * SubSize / (1024.0 * 1024.0);
  186. perf_printf(" glBufferSubDataARB(size = %d, VBOSize = %d): %.1f MB/sec\n",
  187. SubSize, VBOSize, mbPerSec);
  188. }
  189. for (sz = 0; Sizes[sz] < VBOSize; sz++) {
  190. SubSize = Sizes[sz];
  191. rate = PerfMeasureRate(BatchUploadSubVBO);
  192. mbPerSec = rate * SubSize / (1024.0 * 1024.0);
  193. perf_printf(" glBufferSubDataARB(size = %d, VBOSize = %d), batched: %.1f MB/sec\n",
  194. SubSize, VBOSize, mbPerSec);
  195. }
  196. /* Create/Draw/Destroy
  197. */
  198. for (sz = 0; Sizes[sz]; sz++) {
  199. SubSize = VBOSize = Sizes[sz];
  200. rate = PerfMeasureRate(CreateDrawDestroyVBO);
  201. mbPerSec = rate * VBOSize / (1024.0 * 1024.0);
  202. perf_printf(" VBO Create/Draw/Destroy(size = %d): %.1f MB/sec, %.1f draws/sec\n",
  203. VBOSize, mbPerSec, rate);
  204. }
  205. exit(0);
  206. }