blob: b5355525d068e416f3c834806c2e696b0f118716 [file] [log] [blame]
Keith Whitwell2884c312009-09-17 12:09:16 +01001/*
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/**
23 * Measure simple vertex processing rate via:
24 * - immediate mode
25 * - vertex arrays
26 * - VBO vertex arrays
27 * - glDrawElements
28 * - VBO glDrawElements
29 * - glDrawRangeElements
30 * - VBO glDrawRangeElements
31 *
32 * Brian Paul
33 * 16 Sep 2009
34 */
35
36#include <assert.h>
37#include <string.h>
38#include "glmain.h"
39#include "common.h"
40
41
42#define MAX_VERTS (100 * 100)
43
44/** glVertex2/3/4 size */
45#define VERT_SIZE 4
46
47int WinWidth = 500, WinHeight = 500;
48
49static GLuint VertexBO, ElementBO;
50
51static unsigned NumVerts = MAX_VERTS;
52static unsigned VertBytes = VERT_SIZE * sizeof(float);
53static float *VertexData = NULL;
54
55static unsigned NumElements = MAX_VERTS;
56static GLuint *Elements = NULL;
57
58
59/**
60 * Load VertexData buffer with a 2-D grid of points in the range [-1,1]^2.
61 */
62static void
63InitializeVertexData(void)
64{
65 unsigned i;
66 float x = -1.0, y = -1.0;
67 float dx = 2.0 / 100;
68 float dy = 2.0 / 100;
69
70 VertexData = (float *) malloc(NumVerts * VertBytes);
71
72 for (i = 0; i < NumVerts; i++) {
73 VertexData[i * VERT_SIZE + 0] = x;
74 VertexData[i * VERT_SIZE + 1] = y;
75 VertexData[i * VERT_SIZE + 2] = 0.0;
76 VertexData[i * VERT_SIZE + 3] = 1.0;
77 x += dx;
78 if (x > 1.0) {
79 x = -1.0;
80 y += dy;
81 }
82 }
83
84 Elements = (GLuint *) malloc(NumVerts * sizeof(GLuint));
85
86 for (i = 0; i < NumVerts; i++) {
87 Elements[i] = NumVerts - i - 1;
88 }
89}
90
91
92/** Called from test harness/main */
93void
94PerfInit(void)
95{
96 InitializeVertexData();
97
98 /* setup VertexBO */
99 glGenBuffersARB(1, &VertexBO);
100 glBindBufferARB(GL_ARRAY_BUFFER_ARB, VertexBO);
101 glBufferDataARB(GL_ARRAY_BUFFER_ARB,
102 NumVerts * VertBytes, VertexData, GL_STATIC_DRAW_ARB);
103 glEnableClientState(GL_VERTEX_ARRAY);
104
105 /* setup ElementBO */
106 glGenBuffersARB(1, &ElementBO);
107 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, ElementBO);
108 glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
109 NumElements * sizeof(GLuint), Elements, GL_STATIC_DRAW_ARB);
110}
111
112
113static void
114DrawImmediate(unsigned count)
115{
116 unsigned i;
117 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, 0);
118 glBindBufferARB(GL_ARRAY_BUFFER, 0);
119 for (i = 0; i < count; i++) {
120 unsigned j;
121 glBegin(GL_POINTS);
122 for (j = 0; j < NumVerts; j++) {
123#if VERT_SIZE == 4
124 glVertex4fv(VertexData + j * 4);
125#elif VERT_SIZE == 3
126 glVertex3fv(VertexData + j * 3);
127#elif VERT_SIZE == 2
128 glVertex2fv(VertexData + j * 2);
129#else
130 abort();
131#endif
132 }
133 glEnd();
134 }
135 glFinish();
136 PerfSwapBuffers();
137}
138
139
140static void
141DrawArraysMem(unsigned count)
142{
143 unsigned i;
144 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, 0);
145 glBindBufferARB(GL_ARRAY_BUFFER, 0);
146 glVertexPointer(VERT_SIZE, GL_FLOAT, VertBytes, VertexData);
147 for (i = 0; i < count; i++) {
148 glDrawArrays(GL_POINTS, 0, NumVerts);
149 }
150 glFinish();
151 PerfSwapBuffers();
152}
153
154
155static void
156DrawArraysVBO(unsigned count)
157{
158 unsigned i;
159 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, 0);
160 glBindBufferARB(GL_ARRAY_BUFFER, VertexBO);
161 glVertexPointer(VERT_SIZE, GL_FLOAT, VertBytes, (void *) 0);
162 for (i = 0; i < count; i++) {
163 glDrawArrays(GL_POINTS, 0, NumVerts);
164 }
165 glFinish();
166 PerfSwapBuffers();
167}
168
169
170static void
171DrawElementsMem(unsigned count)
172{
173 unsigned i;
174 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, 0);
175 glBindBufferARB(GL_ARRAY_BUFFER, 0);
176 glVertexPointer(VERT_SIZE, GL_FLOAT, VertBytes, VertexData);
177 for (i = 0; i < count; i++) {
178 glDrawElements(GL_POINTS, NumVerts, GL_UNSIGNED_INT, Elements);
179 }
180 glFinish();
181 PerfSwapBuffers();
182}
183
184
185static void
186DrawElementsBO(unsigned count)
187{
188 unsigned i;
189 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, ElementBO);
190 glBindBufferARB(GL_ARRAY_BUFFER, VertexBO);
191 glVertexPointer(VERT_SIZE, GL_FLOAT, VertBytes, (void *) 0);
192 for (i = 0; i < count; i++) {
193 glDrawElements(GL_POINTS, NumVerts, GL_UNSIGNED_INT, (void *) 0);
194 }
195 glFinish();
196 PerfSwapBuffers();
197}
198
199
200static void
201DrawRangeElementsMem(unsigned count)
202{
203 unsigned i;
204 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, 0);
205 glBindBufferARB(GL_ARRAY_BUFFER, 0);
206 glVertexPointer(VERT_SIZE, GL_FLOAT, VertBytes, VertexData);
207 for (i = 0; i < count; i++) {
208 glDrawRangeElements(GL_POINTS, 0, NumVerts - 1,
209 NumVerts, GL_UNSIGNED_INT, Elements);
210 }
211 glFinish();
212 PerfSwapBuffers();
213}
214
215
216static void
217DrawRangeElementsBO(unsigned count)
218{
219 unsigned i;
220 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, ElementBO);
221 glBindBufferARB(GL_ARRAY_BUFFER, VertexBO);
222 glVertexPointer(VERT_SIZE, GL_FLOAT, VertBytes, (void *) 0);
223 for (i = 0; i < count; i++) {
224 glDrawRangeElements(GL_POINTS, 0, NumVerts - 1,
225 NumVerts, GL_UNSIGNED_INT, (void *) 0);
226 }
227 glFinish();
228 PerfSwapBuffers();
229}
230
Keith Whitwella7b26592009-09-21 16:55:12 +0100231void
232PerfNextRound(void)
233{
234}
235
Keith Whitwell2884c312009-09-17 12:09:16 +0100236
237/** Called from test harness/main */
238void
239PerfDraw(void)
240{
241 double rate;
242
243 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
244
245 perf_printf("Vertex rate (%d x Vertex%df)\n", NumVerts, VERT_SIZE);
246
247 rate = PerfMeasureRate(DrawImmediate);
248 rate *= NumVerts;
Keith Whitwell6ab7c122009-09-21 15:52:17 +0100249 perf_printf(" Immediate mode: %s verts/sec\n", PerfHumanFloat(rate));
Keith Whitwell2884c312009-09-17 12:09:16 +0100250
251 rate = PerfMeasureRate(DrawArraysMem);
252 rate *= NumVerts;
Keith Whitwell6ab7c122009-09-21 15:52:17 +0100253 perf_printf(" glDrawArrays: %s verts/sec\n", PerfHumanFloat(rate));
Keith Whitwell2884c312009-09-17 12:09:16 +0100254
255 rate = PerfMeasureRate(DrawArraysVBO);
256 rate *= NumVerts;
Keith Whitwell6ab7c122009-09-21 15:52:17 +0100257 perf_printf(" VBO glDrawArrays: %s verts/sec\n", PerfHumanFloat(rate));
Keith Whitwell2884c312009-09-17 12:09:16 +0100258
259 rate = PerfMeasureRate(DrawElementsMem);
260 rate *= NumVerts;
Keith Whitwell6ab7c122009-09-21 15:52:17 +0100261 perf_printf(" glDrawElements: %s verts/sec\n", PerfHumanFloat(rate));
Keith Whitwell2884c312009-09-17 12:09:16 +0100262
263 rate = PerfMeasureRate(DrawElementsBO);
264 rate *= NumVerts;
Keith Whitwell6ab7c122009-09-21 15:52:17 +0100265 perf_printf(" VBO glDrawElements: %s verts/sec\n", PerfHumanFloat(rate));
Keith Whitwell2884c312009-09-17 12:09:16 +0100266
267 rate = PerfMeasureRate(DrawRangeElementsMem);
268 rate *= NumVerts;
Keith Whitwell6ab7c122009-09-21 15:52:17 +0100269 perf_printf(" glDrawRangeElements: %s verts/sec\n", PerfHumanFloat(rate));
Keith Whitwell2884c312009-09-17 12:09:16 +0100270
271 rate = PerfMeasureRate(DrawRangeElementsBO);
272 rate *= NumVerts;
Keith Whitwell6ab7c122009-09-21 15:52:17 +0100273 perf_printf(" VBO glDrawRangeElements: %s verts/sec\n", PerfHumanFloat(rate));
Keith Whitwell2884c312009-09-17 12:09:16 +0100274
275 exit(0);
276}