Brian Paul | 9abbeda | 2009-09-16 19:33:01 -0600 | [diff] [blame] | 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 | /**
|
| 23 | * Measure glTexSubImage2D rate
|
| 24 | *
|
| 25 | * Brian Paul
|
| 26 | * 16 Sep 2009
|
| 27 | */
|
| 28 |
|
| 29 | #include "glmain.h"
|
| 30 | #include "common.h"
|
| 31 |
|
| 32 |
|
| 33 | int WinWidth = 100, WinHeight = 100;
|
| 34 |
|
| 35 | static GLuint VBO;
|
| 36 | static GLuint TexObj = 0;
|
| 37 | static GLubyte *TexImage = NULL;
|
| 38 | static GLsizei TexSize;
|
| 39 | static GLenum TexSrcFormat, TexSrcType;
|
| 40 |
|
| 41 | static const GLboolean DrawPoint = GL_TRUE;
|
| 42 | static const GLboolean TexSubImage4 = GL_TRUE;
|
| 43 |
|
| 44 | struct vertex
|
| 45 | {
|
| 46 | GLfloat x, y, s, t;
|
| 47 | };
|
| 48 |
|
| 49 | static const struct vertex vertices[1] = {
|
| 50 | { 0.0, 0.0, 0.5, 0.5 },
|
| 51 | };
|
| 52 |
|
| 53 |
|
| 54 | #define VOFFSET(F) ((void *) offsetof(struct vertex, F))
|
| 55 |
|
| 56 | /** Called from test harness/main */
|
| 57 | void
|
| 58 | PerfInit(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 |
|
| 79 | static void
|
| 80 | UploadTexImage2D(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 |
|
| 99 | static void
|
| 100 | UploadTexSubImage2D(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? */
|
| 149 | static 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 |
|
| 158 |
|
| 159 |
|
| 160 | /** Called from test harness/main */
|
| 161 | void
|
| 162 | PerfDraw(void)
|
| 163 | {
|
| 164 | GLint maxSize;
|
| 165 | double rate;
|
| 166 | GLint fmt, subImage;
|
| 167 |
|
| 168 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxSize);
|
| 169 |
|
| 170 | /* loop over source data formats */
|
| 171 | for (fmt = 0; SrcFormats[fmt].format; fmt++) {
|
| 172 | TexSrcFormat = SrcFormats[fmt].format;
|
| 173 | TexSrcType = SrcFormats[fmt].type;
|
| 174 |
|
| 175 | /* loop over glTexImage, glTexSubImage */
|
| 176 | for (subImage = 0; subImage < 2; subImage++) {
|
| 177 |
|
| 178 | /* loop over texture sizes */
|
| 179 | for (TexSize = 16; TexSize <= maxSize; TexSize *= 2) {
|
| 180 | GLint bytesPerImage;
|
| 181 | double mbPerSec;
|
| 182 |
|
| 183 | bytesPerImage = TexSize * TexSize * 4;
|
| 184 | TexImage = malloc(bytesPerImage);
|
| 185 |
|
| 186 | if (subImage) {
|
| 187 | /* create initial, empty texture */
|
| 188 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
|
| 189 | TexSize, TexSize, 0,
|
| 190 | TexSrcFormat, TexSrcType, NULL);
|
| 191 | rate = PerfMeasureRate(UploadTexSubImage2D);
|
| 192 | }
|
| 193 | else {
|
| 194 | rate = PerfMeasureRate(UploadTexImage2D);
|
| 195 | }
|
| 196 |
|
| 197 | mbPerSec = rate * bytesPerImage / (1024.0 * 1024.0);
|
| 198 |
|
| 199 | printf(" glTex%sImage2D(%s %d x %d): "
|
| 200 | "%.1f images/sec, %.1f MB/sec\n",
|
| 201 | (subImage ? "Sub" : ""),
|
| 202 | SrcFormats[fmt].name, TexSize, TexSize, rate, mbPerSec);
|
| 203 |
|
| 204 | free(TexImage);
|
| 205 | }
|
| 206 | }
|
| 207 | }
|
| 208 |
|
| 209 | exit(0);
|
| 210 | }
|