egl: match geometry position of glxgears
[mesa-demos.git] / src / perf / vbo.c
bloba68625627c091ea33f205f255a157d32b2f749e0
1 /*
2 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
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:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
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.
22 /**
23 * Measure VBO upload speed.
24 * That is, measure glBufferDataARB() and glBufferSubDataARB().
26 * Brian Paul
27 * 16 Sep 2009
30 #include <string.h>
31 #include "glmain.h"
32 #include "common.h"
34 /* Copy data out of a large array to avoid caching effects:
36 #define DATA_SIZE (16*1024*1024)
38 int WinWidth = 100, WinHeight = 100;
40 static GLuint VBO;
42 static GLsizei VBOSize = 0;
43 static GLsizei SubSize = 0;
44 static GLubyte *VBOData = NULL; /* array[DATA_SIZE] */
46 static const GLboolean DrawPoint = GL_TRUE;
48 static const GLfloat Vertex0[2] = { 0.0, 0.0 };
51 /** Called from test harness/main */
52 void
53 PerfInit(void)
55 /* setup VBO */
56 glGenBuffersARB(1, &VBO);
57 glBindBufferARB(GL_ARRAY_BUFFER_ARB, VBO);
58 glVertexPointer(2, GL_FLOAT, sizeof(Vertex0), (void *) 0);
59 glEnableClientState(GL_VERTEX_ARRAY);
63 static void
64 UploadVBO(unsigned count)
66 unsigned i;
67 unsigned total = 0;
68 unsigned src = 0;
70 for (i = 0; i < count; i++) {
71 glBufferDataARB(GL_ARRAY_BUFFER, VBOSize, VBOData + src, GL_STREAM_DRAW_ARB);
72 glDrawArrays(GL_POINTS, 0, 1);
74 /* Throw in an occasional flush to work around a driver crash:
76 total += VBOSize;
77 if (total >= 16*1024*1024) {
78 glFlush();
79 total = 0;
82 src += VBOSize;
83 src %= DATA_SIZE;
85 glFinish();
89 static void
90 UploadSubVBO(unsigned count)
92 unsigned i;
93 unsigned src = 0;
95 for (i = 0; i < count; i++) {
96 unsigned offset = (i * SubSize) % VBOSize;
97 glBufferSubDataARB(GL_ARRAY_BUFFER, offset, SubSize, VBOData + src);
99 if (DrawPoint) {
100 glDrawArrays(GL_POINTS, offset / sizeof(Vertex0), 1);
103 src += SubSize;
104 src %= DATA_SIZE;
106 glFinish();
110 /* Do multiple small SubData uploads, then call DrawArrays. This may be a
111 * fairer comparison to back-to-back BufferData calls:
113 static void
114 BatchUploadSubVBO(unsigned count)
116 unsigned i = 0, j;
117 unsigned period = VBOSize / SubSize;
118 unsigned src = 0;
120 while (i < count) {
121 for (j = 0; j < period && i < count; j++, i++) {
122 unsigned offset = j * SubSize;
123 glBufferSubDataARB(GL_ARRAY_BUFFER, offset, SubSize, VBOData + src);
126 glDrawArrays(GL_POINTS, 0, 1);
128 src += SubSize;
129 src %= DATA_SIZE;
131 glFinish();
136 * Test the sequence:
137 * create/load VBO
138 * draw
139 * destroy VBO
141 static void
142 CreateDrawDestroyVBO(unsigned count)
144 unsigned i;
145 for (i = 0; i < count; i++) {
146 GLuint vbo;
147 /* create/load */
148 glGenBuffersARB(1, &vbo);
149 glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo);
150 glBufferDataARB(GL_ARRAY_BUFFER, VBOSize, VBOData, GL_STREAM_DRAW_ARB);
151 /* draw */
152 glVertexPointer(2, GL_FLOAT, sizeof(Vertex0), (void *) 0);
153 glDrawArrays(GL_POINTS, 0, 1);
154 /* destroy */
155 glDeleteBuffersARB(1, &vbo);
157 glFinish();
161 static const GLsizei Sizes[] = {
163 1024,
164 16*1024,
165 256*1024,
166 1024*1024,
167 16*1024*1024,
168 0 /* end of list */
171 void
172 PerfNextRound(void)
176 /** Called from test harness/main */
177 void
178 PerfDraw(void)
180 double rate, mbPerSec;
181 int i, sz;
183 /* Load VBOData buffer with duplicated Vertex0.
185 VBOData = calloc(DATA_SIZE, 1);
187 for (i = 0; i < DATA_SIZE / sizeof(Vertex0); i++) {
188 memcpy(VBOData + i * sizeof(Vertex0),
189 Vertex0,
190 sizeof(Vertex0));
193 /* glBufferDataARB()
195 for (sz = 0; Sizes[sz]; sz++) {
196 SubSize = VBOSize = Sizes[sz];
197 rate = PerfMeasureRate(UploadVBO);
198 mbPerSec = rate * VBOSize / (1024.0 * 1024.0);
199 perf_printf(" glBufferDataARB(size = %d): %.1f MB/sec\n",
200 VBOSize, mbPerSec);
203 /* glBufferSubDataARB()
205 for (sz = 0; Sizes[sz]; sz++) {
206 SubSize = VBOSize = Sizes[sz];
207 rate = PerfMeasureRate(UploadSubVBO);
208 mbPerSec = rate * VBOSize / (1024.0 * 1024.0);
209 perf_printf(" glBufferSubDataARB(size = %d): %.1f MB/sec\n",
210 VBOSize, mbPerSec);
213 /* Batch upload
215 VBOSize = 1024 * 1024;
216 glBufferDataARB(GL_ARRAY_BUFFER, VBOSize, VBOData, GL_STREAM_DRAW_ARB);
218 for (sz = 0; Sizes[sz] < VBOSize; sz++) {
219 SubSize = Sizes[sz];
220 rate = PerfMeasureRate(UploadSubVBO);
221 mbPerSec = rate * SubSize / (1024.0 * 1024.0);
222 perf_printf(" glBufferSubDataARB(size = %d, VBOSize = %d): %.1f MB/sec\n",
223 SubSize, VBOSize, mbPerSec);
226 for (sz = 0; Sizes[sz] < VBOSize; sz++) {
227 SubSize = Sizes[sz];
228 rate = PerfMeasureRate(BatchUploadSubVBO);
229 mbPerSec = rate * SubSize / (1024.0 * 1024.0);
230 perf_printf(" glBufferSubDataARB(size = %d, VBOSize = %d), batched: %.1f MB/sec\n",
231 SubSize, VBOSize, mbPerSec);
234 /* Create/Draw/Destroy
236 for (sz = 0; Sizes[sz]; sz++) {
237 SubSize = VBOSize = Sizes[sz];
238 rate = PerfMeasureRate(CreateDrawDestroyVBO);
239 mbPerSec = rate * VBOSize / (1024.0 * 1024.0);
240 perf_printf(" VBO Create/Draw/Destroy(size = %d): %.1f MB/sec, %.1f draws/sec\n",
241 VBOSize, mbPerSec, rate);
244 exit(0);