blob: 86f0ef873665beafcd35db9ba24d0bf5d81157c8 [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 glTexSubImage2D rate
24 *
25 * Brian Paul
26 * 16 Sep 2009
27 */
28
29#include "glmain.h"
30#include "common.h"
31
32
33int WinWidth = 100, WinHeight = 100;
34
35static GLuint VBO;
36static GLuint TexObj = 0;
37static GLubyte *TexImage = NULL;
38static GLsizei TexSize;
39static GLenum TexSrcFormat, TexSrcType;
40
41static const GLboolean DrawPoint = GL_TRUE;
42static const GLboolean TexSubImage4 = GL_TRUE;
43
44struct vertex
45{
46 GLfloat x, y, s, t;
47};
48
49static const struct vertex vertices[1] = {
50 { 0.0, 0.0, 0.5, 0.5 },
51};
52
Keith Whitwell2884c312009-09-17 12:09:16 +010053#define VOFFSET(F) ((void *) offsetof(struct vertex, F))
Brian Paul83fbee62009-09-21 11:09:00 -060054
Keith Whitwell2884c312009-09-17 12:09:16 +010055
56/** Called from test harness/main */
57void
58PerfInit(void)
59{
60 /* setup VBO w/ vertex data */
61 glGenBuffersARB(1, &VBO);
62 glBindBufferARB(GL_ARRAY_BUFFER_ARB, VBO);
63 glBufferDataARB(GL_ARRAY_BUFFER_ARB,
64 sizeof(vertices), vertices, GL_STATIC_DRAW_ARB);
65 glVertexPointer(2, GL_FLOAT, sizeof(struct vertex), VOFFSET(x));
66 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), VOFFSET(s));
67 glEnableClientState(GL_VERTEX_ARRAY);
68 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
69
70 /* texture */
71 glGenTextures(1, &TexObj);
72 glBindTexture(GL_TEXTURE_2D, TexObj);
73 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
74 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
75 glEnable(GL_TEXTURE_2D);
76}
77
78
79static void
80UploadTexImage2D(unsigned count)
81{
82 unsigned i;
83 for (i = 0; i < count; i++) {
84 /* XXX is this equivalent to a glTexSubImage call since we're
85 * always specifying the same image size? That case isn't optimized
86 * in Mesa but may be optimized in other drivers. Note sure how
87 * much difference that might make.
88 */
89 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
90 TexSize, TexSize, 0,
91 TexSrcFormat, TexSrcType, TexImage);
92 if (DrawPoint)
93 glDrawArrays(GL_POINTS, 0, 1);
94 }
95 glFinish();
96}
97
98
99static void
100UploadTexSubImage2D(unsigned count)
101{
102 unsigned i;
103 for (i = 0; i < count; i++) {
104 if (TexSubImage4) {
105 GLsizei halfSize = (TexSize == 1) ? 1 : TexSize / 2;
106 GLsizei halfPos = TexSize - halfSize;
107 /* do glTexSubImage2D in four pieces */
108 /* lower-left */
109 glPixelStorei(GL_UNPACK_ROW_LENGTH, TexSize);
110 glTexSubImage2D(GL_TEXTURE_2D, 0,
111 0, 0, halfSize, halfSize,
112 TexSrcFormat, TexSrcType, TexImage);
113 /* lower-right */
114 glPixelStorei(GL_UNPACK_SKIP_PIXELS, halfPos);
115 glTexSubImage2D(GL_TEXTURE_2D, 0,
116 halfPos, 0, halfSize, halfSize,
117 TexSrcFormat, TexSrcType, TexImage);
118 /* upper-left */
119 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
120 glPixelStorei(GL_UNPACK_SKIP_ROWS, halfPos);
121 glTexSubImage2D(GL_TEXTURE_2D, 0,
122 0, halfPos, halfSize, halfSize,
123 TexSrcFormat, TexSrcType, TexImage);
124 /* upper-right */
125 glPixelStorei(GL_UNPACK_SKIP_PIXELS, halfPos);
126 glPixelStorei(GL_UNPACK_SKIP_ROWS, halfPos);
127 glTexSubImage2D(GL_TEXTURE_2D, 0,
128 halfPos, halfPos, halfSize, halfSize,
129 TexSrcFormat, TexSrcType, TexImage);
130 /* reset the unpacking state */
131 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
132 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
133 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
134 }
135 else {
136 /* replace whole texture image at once */
137 glTexSubImage2D(GL_TEXTURE_2D, 0,
138 0, 0, TexSize, TexSize,
139 TexSrcFormat, TexSrcType, TexImage);
140 }
141 if (DrawPoint)
142 glDrawArrays(GL_POINTS, 0, 1);
143 }
144 glFinish();
145}
146
147
148/* XXX any other formats to measure? */
149static const struct {
150 GLenum format, type;
151 const char *name;
152} SrcFormats[] = {
153 { GL_RGBA, GL_UNSIGNED_BYTE, "GL_RGBA/GLubyte" },
154 { GL_BGRA, GL_UNSIGNED_BYTE, "GL_BGRA/GLubyte" },
155 { 0, 0, NULL }
156};
157
Keith Whitwella7b26592009-09-21 16:55:12 +0100158void
159PerfNextRound(void)
160{
161}
Keith Whitwell2884c312009-09-17 12:09:16 +0100162
163
164/** Called from test harness/main */
165void
166PerfDraw(void)
167{
168 GLint maxSize;
169 double rate;
170 GLint fmt, subImage;
171
172 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxSize);
173
174 /* loop over source data formats */
175 for (fmt = 0; SrcFormats[fmt].format; fmt++) {
176 TexSrcFormat = SrcFormats[fmt].format;
177 TexSrcType = SrcFormats[fmt].type;
178
179 /* loop over glTexImage, glTexSubImage */
180 for (subImage = 0; subImage < 2; subImage++) {
181
182 /* loop over texture sizes */
183 for (TexSize = 16; TexSize <= maxSize; TexSize *= 2) {
184 GLint bytesPerImage;
185 double mbPerSec;
186
187 bytesPerImage = TexSize * TexSize * 4;
188 TexImage = malloc(bytesPerImage);
189
190 if (subImage) {
191 /* create initial, empty texture */
192 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
193 TexSize, TexSize, 0,
194 TexSrcFormat, TexSrcType, NULL);
195 rate = PerfMeasureRate(UploadTexSubImage2D);
196 }
197 else {
198 rate = PerfMeasureRate(UploadTexImage2D);
199 }
200
201 mbPerSec = rate * bytesPerImage / (1024.0 * 1024.0);
202
203 perf_printf(" glTex%sImage2D(%s %d x %d): "
204 "%.1f images/sec, %.1f MB/sec\n",
205 (subImage ? "Sub" : ""),
206 SrcFormats[fmt].name, TexSize, TexSize, rate, mbPerSec);
207
208 free(TexImage);
209 }
210 }
211 }
212
213 exit(0);
214}