blob: fb9fa2453f62e4e00baa9ab1977ca069eab6c7b2 [file] [log] [blame]
Brian Paul9297e692009-09-21 12:17:49 -06001/*
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 fill rates.
24 *
25 * Brian Paul
26 * 21 Sep 2009
27 */
28
29#include "glmain.h"
30#include "common.h"
31
32
33int WinWidth = 1000, WinHeight = 1000;
34
35static GLuint VBO, TexObj;
36
37
38struct vertex
39{
40 GLfloat x, y, s, t, r, g, b, a;
41};
42
43#define VOFFSET(F) ((void *) offsetof(struct vertex, F))
44
45static const struct vertex vertices[4] = {
46 /* x y s t r g b a */
47 { -1.0, -1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.5 },
48 { 1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.5 },
49 { 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.5 },
50 { -1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.5 }
51};
52
53
54static const char *VertexShader =
55 "void main() \n"
56 "{ \n"
57 " gl_Position = ftransform(); \n"
58 " gl_TexCoord[0] = gl_MultiTexCoord0; \n"
59 " gl_FrontColor = gl_Color; \n"
60 "} \n";
61
62/* simple fragment shader */
63static const char *FragmentShader1 =
64 "uniform sampler2D Tex; \n"
65 "void main() \n"
66 "{ \n"
67 " vec4 t = texture2D(Tex, gl_TexCoord[0].xy); \n"
68 " gl_FragColor = vec4(1.0) - t * gl_Color; \n"
69 "} \n";
70
71/**
72 * A more complex fragment shader (but equivalent to first shader).
73 * A good optimizer should catch some of these no-op operations, but
74 * probably not all of them.
75 */
76static const char *FragmentShader2 =
77 "uniform sampler2D Tex; \n"
78 "void main() \n"
79 "{ \n"
80 " // as above \n"
81 " vec4 t = texture2D(Tex, gl_TexCoord[0].xy); \n"
82 " t = vec4(1.0) - t * gl_Color; \n"
83
84 " vec4 u; \n"
85
86 " // no-op negate/swizzle \n"
87 " u = -t.wzyx; \n"
88 " t = -u.wzyx; \n"
89
90 " // no-op inverts \n"
91 " t = vec4(1.0) - t; \n"
92 " t = vec4(1.0) - t; \n"
93
94 " // no-op min/max \n"
95 " t = min(t, t); \n"
96 " t = max(t, t); \n"
97
98 " // no-op moves \n"
99 " u = t; \n"
100 " t = u; \n"
101 " u = t; \n"
102 " t = u; \n"
103
104 " // no-op add/mul \n"
105 " t = (t + t + t + t) * 0.25; \n"
106
107 " // no-op mul/sub \n"
108 " t = 3.0 * t - 2.0 * t; \n"
109
110 " // no-op negate/min/max \n"
111 " t = -min(-t, -t); \n"
112 " t = -max(-t, -t); \n"
113
114 " gl_FragColor = t; \n"
115 "} \n";
116
117static GLuint ShaderProg1, ShaderProg2;
118
119
120
121/** Called from test harness/main */
122void
123PerfInit(void)
124{
125 GLint u;
126
127 /* setup VBO w/ vertex data */
128 glGenBuffersARB(1, &VBO);
129 glBindBufferARB(GL_ARRAY_BUFFER_ARB, VBO);
130 glBufferDataARB(GL_ARRAY_BUFFER_ARB,
131 sizeof(vertices), vertices, GL_STATIC_DRAW_ARB);
132 glVertexPointer(2, GL_FLOAT, sizeof(struct vertex), VOFFSET(x));
133 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), VOFFSET(s));
134 glColorPointer(4, GL_FLOAT, sizeof(struct vertex), VOFFSET(r));
135 glEnableClientState(GL_VERTEX_ARRAY);
136 glEnableClientState(GL_COLOR_ARRAY);
137
138 /* setup texture */
139 TexObj = PerfCheckerTexture(128, 128);
140
141 /* setup shaders */
142 ShaderProg1 = PerfShaderProgram(VertexShader, FragmentShader1);
143 glUseProgram(ShaderProg1);
144 u = glGetUniformLocation(ShaderProg1, "Tex");
145 glUniform1i(u, 0); /* texture unit 0 */
146
147 ShaderProg2 = PerfShaderProgram(VertexShader, FragmentShader2);
148 glUseProgram(ShaderProg2);
149 u = glGetUniformLocation(ShaderProg2, "Tex");
150 glUniform1i(u, 0); /* texture unit 0 */
151
152 glUseProgram(0);
153}
154
155
156static void
157Ortho(void)
158{
159 glMatrixMode(GL_PROJECTION);
160 glLoadIdentity();
161 glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
162 glMatrixMode(GL_MODELVIEW);
163 glLoadIdentity();
164}
165
166
167
168static void
169DrawQuad(unsigned count)
170{
171 unsigned i;
172 for (i = 0; i < count; i++) {
173 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
174 }
175 glFinish();
176 if (0)
177 PerfSwapBuffers();
178}
179
180
181/** Called from test harness/main */
182void
183PerfDraw(void)
184{
185 double rate;
186 double pixelsPerDraw = WinWidth * WinHeight;
187
188 Ortho();
189
190 /* simple fill */
191 rate = PerfMeasureRate(DrawQuad) * pixelsPerDraw;
192 perf_printf(" Simple fill: %s pixels/second\n",
193 PerfHumanFloat(rate));
194
195 /* blended fill */
196 glEnable(GL_BLEND);
197 rate = PerfMeasureRate(DrawQuad) * pixelsPerDraw;
198 glDisable(GL_BLEND);
199 perf_printf(" Blended fill: %s pixels/second\n",
200 PerfHumanFloat(rate));
201
202 /* textured fill */
203 glEnable(GL_TEXTURE_2D);
204 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
205 rate = PerfMeasureRate(DrawQuad) * pixelsPerDraw;
206 glDisable(GL_TEXTURE_2D);
207 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
208 perf_printf(" Textured fill: %s pixels/second\n",
209 PerfHumanFloat(rate));
210
211 /* shader1 fill */
212 glUseProgram(ShaderProg1);
213 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
214 rate = PerfMeasureRate(DrawQuad) * pixelsPerDraw;
215 glUseProgram(0);
216 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
217 perf_printf(" Shader1 fill: %s pixels/second\n",
218 PerfHumanFloat(rate));
219
220 /* shader2 fill */
221 glUseProgram(ShaderProg2);
222 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
223 rate = PerfMeasureRate(DrawQuad) * pixelsPerDraw;
224 glUseProgram(0);
225 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
226 perf_printf(" Shader2 fill: %s pixels/second\n",
227 PerfHumanFloat(rate));
228
229 exit(0);
230}
231