forked from native-toolkit/libangle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DrawCallPerf.cpp
389 lines (337 loc) · 13.5 KB
/
DrawCallPerf.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
//
// Copyright 2014 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// DrawCallPerf:
// Performance tests for ANGLE draw call overhead.
//
#include "ANGLEPerfTest.h"
#include "DrawCallPerfParams.h"
#include "test_utils/draw_call_perf_utils.h"
#include "util/shader_utils.h"
namespace
{
enum class StateChange
{
NoChange,
VertexAttrib,
VertexBuffer,
ManyVertexBuffers,
Texture,
};
struct DrawArraysPerfParams : public DrawCallPerfParams
{
DrawArraysPerfParams(const DrawCallPerfParams &base) : DrawCallPerfParams(base) {}
std::string story() const override;
StateChange stateChange = StateChange::NoChange;
};
std::string DrawArraysPerfParams::story() const
{
std::stringstream strstr;
strstr << DrawCallPerfParams::story();
switch (stateChange)
{
case StateChange::VertexAttrib:
strstr << "_attrib_change";
break;
case StateChange::VertexBuffer:
strstr << "_vbo_change";
break;
case StateChange::ManyVertexBuffers:
strstr << "_manyvbos_change";
break;
case StateChange::Texture:
strstr << "_tex_change";
break;
default:
break;
}
return strstr.str();
}
std::ostream &operator<<(std::ostream &os, const DrawArraysPerfParams ¶ms)
{
os << params.backendAndStory().substr(1);
return os;
}
GLuint CreateSimpleTexture2D()
{
// Use tightly packed data
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Generate a texture object
GLuint texture;
glGenTextures(1, &texture);
// Bind the texture object
glBindTexture(GL_TEXTURE_2D, texture);
// Load the texture: 2x2 Image, 3 bytes per pixel (R, G, B)
constexpr size_t width = 2;
constexpr size_t height = 2;
GLubyte pixels[width * height * 3] = {
255, 0, 0, // Red
0, 255, 0, // Green
0, 0, 255, // Blue
255, 255, 0, // Yellow
};
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
// Set the filtering mode
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
return texture;
}
class DrawCallPerfBenchmark : public ANGLERenderTest,
public ::testing::WithParamInterface<DrawArraysPerfParams>
{
public:
DrawCallPerfBenchmark();
void initializeBenchmark() override;
void destroyBenchmark() override;
void drawBenchmark() override;
private:
GLuint mProgram = 0;
GLuint mBuffer1 = 0;
GLuint mBuffer2 = 0;
GLuint mFBO = 0;
GLuint mFBOTexture = 0;
GLuint mTexture1 = 0;
GLuint mTexture2 = 0;
int mNumTris = GetParam().numTris;
};
DrawCallPerfBenchmark::DrawCallPerfBenchmark() : ANGLERenderTest("DrawCallPerf", GetParam()) {}
void DrawCallPerfBenchmark::initializeBenchmark()
{
const auto ¶ms = GetParam();
if (params.stateChange == StateChange::Texture)
{
mProgram = SetupSimpleTextureProgram();
}
else if (params.stateChange == StateChange::ManyVertexBuffers)
{
constexpr char kVS[] = R"(attribute vec2 vPosition;
attribute vec2 v0;
attribute vec2 v1;
attribute vec2 v2;
attribute vec2 v3;
const float scale = 0.5;
const float offset = -0.5;
varying vec2 v;
void main()
{
gl_Position = vec4(vPosition * vec2(scale) + vec2(offset), 0, 1);
v = (v0 + v1 + v2 + v3) * 0.25;
})";
constexpr char kFS[] = R"(precision mediump float;
varying vec2 v;
void main()
{
gl_FragColor = vec4(v, 0, 1);
})";
mProgram = CompileProgram(kVS, kFS);
glBindAttribLocation(mProgram, 1, "v0");
glBindAttribLocation(mProgram, 2, "v1");
glBindAttribLocation(mProgram, 3, "v2");
glBindAttribLocation(mProgram, 4, "v3");
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3);
glEnableVertexAttribArray(4);
}
else
{
mProgram = SetupSimpleDrawProgram();
}
ASSERT_NE(0u, mProgram);
// Re-link program to ensure the attrib bindings are used.
glBindAttribLocation(mProgram, 0, "vPosition");
glLinkProgram(mProgram);
glUseProgram(mProgram);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
mBuffer1 = Create2DTriangleBuffer(mNumTris, GL_STATIC_DRAW);
mBuffer2 = Create2DTriangleBuffer(mNumTris, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
// Set the viewport
glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
if (params.offscreen)
{
CreateColorFBO(getWindow()->getWidth(), getWindow()->getHeight(), &mFBOTexture, &mFBO);
}
mTexture1 = CreateSimpleTexture2D();
mTexture2 = CreateSimpleTexture2D();
ASSERT_GL_NO_ERROR();
}
void DrawCallPerfBenchmark::destroyBenchmark()
{
glDeleteProgram(mProgram);
glDeleteBuffers(1, &mBuffer1);
glDeleteBuffers(1, &mBuffer2);
glDeleteTextures(1, &mFBOTexture);
glDeleteTextures(1, &mTexture1);
glDeleteTextures(1, &mTexture2);
glDeleteFramebuffers(1, &mFBO);
}
void ClearThenDraw(unsigned int iterations, GLsizei numElements)
{
glClear(GL_COLOR_BUFFER_BIT);
for (unsigned int it = 0; it < iterations; it++)
{
glDrawArrays(GL_TRIANGLES, 0, numElements);
}
}
void JustDraw(unsigned int iterations, GLsizei numElements)
{
for (unsigned int it = 0; it < iterations; it++)
{
glDrawArrays(GL_TRIANGLES, 0, numElements);
}
}
template <int kArrayBufferCount>
void ChangeVertexAttribThenDraw(unsigned int iterations, GLsizei numElements, GLuint buffer)
{
glBindBuffer(GL_ARRAY_BUFFER, buffer);
for (unsigned int it = 0; it < iterations; it++)
{
for (int arrayIndex = 0; arrayIndex < kArrayBufferCount; ++arrayIndex)
{
glVertexAttribPointer(arrayIndex, 2, GL_FLOAT, GL_FALSE, 0, 0);
}
glDrawArrays(GL_TRIANGLES, 0, numElements);
for (int arrayIndex = 0; arrayIndex < kArrayBufferCount; ++arrayIndex)
{
glVertexAttribPointer(arrayIndex, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
}
glDrawArrays(GL_TRIANGLES, 0, numElements);
}
}
template <int kArrayBufferCount>
void ChangeArrayBuffersThenDraw(unsigned int iterations,
GLsizei numElements,
GLuint buffer1,
GLuint buffer2)
{
for (unsigned int it = 0; it < iterations; it++)
{
glBindBuffer(GL_ARRAY_BUFFER, buffer1);
for (int arrayIndex = 0; arrayIndex < kArrayBufferCount; ++arrayIndex)
{
glVertexAttribPointer(arrayIndex, 2, GL_FLOAT, GL_FALSE, 0, 0);
}
glDrawArrays(GL_TRIANGLES, 0, numElements);
glBindBuffer(GL_ARRAY_BUFFER, buffer2);
for (int arrayIndex = 0; arrayIndex < kArrayBufferCount; ++arrayIndex)
{
glVertexAttribPointer(arrayIndex, 2, GL_FLOAT, GL_FALSE, 0, 0);
}
glDrawArrays(GL_TRIANGLES, 0, numElements);
}
}
void ChangeTextureThenDraw(unsigned int iterations,
GLsizei numElements,
GLuint texture1,
GLuint texture2)
{
for (unsigned int it = 0; it < iterations; it++)
{
glBindTexture(GL_TEXTURE_2D, texture1);
glDrawArrays(GL_TRIANGLES, 0, numElements);
glBindTexture(GL_TEXTURE_2D, texture2);
glDrawArrays(GL_TRIANGLES, 0, numElements);
}
}
void DrawCallPerfBenchmark::drawBenchmark()
{
// This workaround fixes a huge queue of graphics commands accumulating on the GL
// back-end. The GL back-end doesn't have a proper NULL device at the moment.
// TODO(jmadill): Remove this when/if we ever get a proper OpenGL NULL device.
const auto &eglParams = GetParam().eglParameters;
const auto ¶ms = GetParam();
GLsizei numElements = static_cast<GLsizei>(3 * mNumTris);
switch (params.stateChange)
{
case StateChange::VertexAttrib:
ChangeVertexAttribThenDraw<1>(params.iterationsPerStep, numElements, mBuffer1);
break;
case StateChange::VertexBuffer:
ChangeArrayBuffersThenDraw<1>(params.iterationsPerStep, numElements, mBuffer1,
mBuffer2);
break;
case StateChange::ManyVertexBuffers:
ChangeArrayBuffersThenDraw<5>(params.iterationsPerStep, numElements, mBuffer1,
mBuffer2);
break;
case StateChange::Texture:
ChangeTextureThenDraw(params.iterationsPerStep, numElements, mTexture1, mTexture2);
break;
case StateChange::NoChange:
if (eglParams.deviceType != EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE ||
(eglParams.renderer != EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE &&
eglParams.renderer != EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE))
{
ClearThenDraw(params.iterationsPerStep, numElements);
}
else
{
JustDraw(params.iterationsPerStep, numElements);
}
break;
}
ASSERT_GL_NO_ERROR();
}
TEST_P(DrawCallPerfBenchmark, Run)
{
run();
}
DrawArraysPerfParams DrawArrays(const DrawCallPerfParams &base, StateChange stateChange)
{
DrawArraysPerfParams params(base);
params.stateChange = stateChange;
return params;
}
using namespace params;
ANGLE_INSTANTIATE_TEST(DrawCallPerfBenchmark,
DrawArrays(DrawCallD3D9(), StateChange::NoChange),
DrawArrays(NullDevice(DrawCallD3D9()), StateChange::NoChange),
DrawArrays(DrawCallD3D11(), StateChange::NoChange),
DrawArrays(NullDevice(DrawCallD3D11()), StateChange::NoChange),
DrawArrays(NullDevice(Offscreen(DrawCallD3D11())), StateChange::NoChange),
DrawArrays(DrawCallD3D11(), StateChange::VertexAttrib),
DrawArrays(NullDevice(DrawCallD3D11()), StateChange::VertexAttrib),
DrawArrays(DrawCallD3D11(), StateChange::VertexBuffer),
DrawArrays(NullDevice(DrawCallD3D11()), StateChange::VertexBuffer),
DrawArrays(DrawCallD3D11(), StateChange::Texture),
DrawArrays(NullDevice(DrawCallD3D11()), StateChange::Texture),
DrawArrays(DrawCallOpenGL(), StateChange::NoChange),
DrawArrays(NullDevice(DrawCallOpenGL()), StateChange::NoChange),
DrawArrays(NullDevice(Offscreen(DrawCallOpenGL())), StateChange::NoChange),
DrawArrays(DrawCallOpenGL(), StateChange::VertexAttrib),
DrawArrays(NullDevice(DrawCallOpenGL()), StateChange::VertexAttrib),
DrawArrays(DrawCallOpenGL(), StateChange::VertexBuffer),
DrawArrays(NullDevice(DrawCallOpenGL()), StateChange::VertexBuffer),
DrawArrays(DrawCallOpenGL(), StateChange::ManyVertexBuffers),
DrawArrays(NullDevice(DrawCallOpenGL()), StateChange::ManyVertexBuffers),
DrawArrays(DrawCallOpenGL(), StateChange::Texture),
DrawArrays(NullDevice(DrawCallOpenGL()), StateChange::Texture),
DrawArrays(DrawCallValidation(), StateChange::NoChange),
DrawArrays(DrawCallVulkan(), StateChange::NoChange),
DrawArrays(Offscreen(DrawCallVulkan()), StateChange::NoChange),
DrawArrays(NullDevice(DrawCallVulkan()), StateChange::NoChange),
DrawArrays(DrawCallVulkan(), StateChange::VertexAttrib),
DrawArrays(Offscreen(DrawCallVulkan()), StateChange::VertexAttrib),
DrawArrays(NullDevice(DrawCallVulkan()), StateChange::VertexAttrib),
DrawArrays(DrawCallVulkan(), StateChange::VertexBuffer),
DrawArrays(Offscreen(DrawCallVulkan()), StateChange::VertexBuffer),
DrawArrays(NullDevice(DrawCallVulkan()), StateChange::VertexBuffer),
DrawArrays(DrawCallVulkan(), StateChange::ManyVertexBuffers),
DrawArrays(Offscreen(DrawCallVulkan()), StateChange::ManyVertexBuffers),
DrawArrays(NullDevice(DrawCallVulkan()), StateChange::ManyVertexBuffers),
DrawArrays(DrawCallVulkan(), StateChange::Texture),
DrawArrays(Offscreen(DrawCallVulkan()), StateChange::Texture),
DrawArrays(NullDevice(DrawCallVulkan()), StateChange::Texture),
DrawArrays(DrawCallWGL(), StateChange::NoChange),
DrawArrays(Offscreen(DrawCallWGL()), StateChange::NoChange),
DrawArrays(DrawCallWGL(), StateChange::VertexAttrib),
DrawArrays(Offscreen(DrawCallWGL()), StateChange::VertexAttrib),
DrawArrays(DrawCallWGL(), StateChange::VertexBuffer),
DrawArrays(Offscreen(DrawCallWGL()), StateChange::VertexBuffer),
DrawArrays(DrawCallWGL(), StateChange::Texture),
DrawArrays(Offscreen(DrawCallWGL()), StateChange::Texture));
} // anonymous namespace