blob: 37d1f2c4306d45287d2be89895a1392cea10a043 [file] [log] [blame]
joshualitt6cd70ff2015-06-26 14:20:41 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Benchmark.h"
9#include "SkCanvas.h"
10#include "SkImageEncoder.h"
joshualitt74a07db2015-07-01 12:39:07 -070011
joshualitt6cd70ff2015-06-26 14:20:41 -070012#if SK_SUPPORT_GPU
joshualitt74a07db2015-07-01 12:39:07 -070013#include "GLBench.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050014#include "GrShaderCaps.h"
Brian Salomon99938a82016-11-21 13:41:08 -050015#include "GrShaderVar.h"
egdanielf5294392015-10-21 07:14:17 -070016#include "gl/GrGLContext.h"
joshualitt6cd70ff2015-06-26 14:20:41 -070017#include "gl/GrGLInterface.h"
joshualitt6cd70ff2015-06-26 14:20:41 -070018#include "gl/GrGLUtil.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050019#include "../private/GrGLSL.h"
joshualitt6cd70ff2015-06-26 14:20:41 -070020
21/*
22 * This is a native GL benchmark for instanced arrays vs vertex buffer objects. To benchmark this
23 * functionality, we draw n * kDrawMultipier triangles per run. If this number is less than
24 * kNumTri then we do a single draw, either with instances, or drawArrays. Otherwise we do
25 * multiple draws.
26 *
27 * Additionally, there is a divisor, which if > 0 will act as a multiplier for the number of draws
28 * issued.
29 */
joshualitte784db42015-06-29 06:58:06 -070030
joshualitt74a07db2015-07-01 12:39:07 -070031class GLCpuPosInstancedArraysBench : public GLBench {
joshualitt6cd70ff2015-06-26 14:20:41 -070032public:
33 /*
34 * Clients can decide to use either:
35 * kUseOne_VboSetup - one vertex buffer with colors and positions interleaved
36 * kUseTwo_VboSetup - two vertex buffers, one for colors, one for positions
37 * kUseInstance_VboSetup - two vertex buffers, one with per vertex indices, one with per
38 * instance colors
39 */
40 enum VboSetup {
41 kUseOne_VboSetup,
42 kUseTwo_VboSetup,
43 kUseInstance_VboSetup,
44 };
45
46 /*
47 * drawDiv will act as a multiplier for the number of draws we issue if > 0. ie, 2 will issue
48 * 2x as many draws, 4 will issue 4x as many draws etc. There is a limit however, which is
49 * kDrawMultipier.
50 */
51 GLCpuPosInstancedArraysBench(VboSetup vboSetup, int32_t drawDiv)
52 : fVboSetup(vboSetup)
joshualitte784db42015-06-29 06:58:06 -070053 , fDrawDiv(drawDiv)
54 , fProgram(0)
55 , fVAO(0) {
joshualitt6cd70ff2015-06-26 14:20:41 -070056 fName = VboSetupToStr(vboSetup, fDrawDiv);
57 }
58
59protected:
60 const char* onGetName() override {
61 return fName.c_str();
62 }
63
joshualitt74a07db2015-07-01 12:39:07 -070064 const GrGLContext* onGetGLContext(const GrGLContext*) override;
joshualitt6cd70ff2015-06-26 14:20:41 -070065 void setup(const GrGLContext*) override;
mtkleina1ebeb22015-10-01 09:43:39 -070066 void glDraw(int loops, const GrGLContext*) override;
joshualitt6cd70ff2015-06-26 14:20:41 -070067 void teardown(const GrGLInterface*) override;
68
69private:
70 void setupInstanceVbo(const GrGLInterface*, const SkMatrix*);
71 void setupDoubleVbo(const GrGLInterface*, const SkMatrix*);
72 void setupSingleVbo(const GrGLInterface*, const SkMatrix*);
joshualitt74a07db2015-07-01 12:39:07 -070073 GrGLuint setupShader(const GrGLContext*);
joshualitt6cd70ff2015-06-26 14:20:41 -070074
75 static SkString VboSetupToStr(VboSetup vboSetup, uint32_t drawDiv) {
76 SkString name("GLInstancedArraysBench");
77 switch (vboSetup) {
78 default:
79 case kUseOne_VboSetup:
80 name.appendf("_one_%u", drawDiv);
81 break;
82 case kUseTwo_VboSetup:
83 name.appendf("_two_%u", drawDiv);
84 break;
85 case kUseInstance_VboSetup:
86 name.append("_instance");
87 break;
88 }
89 return name;
90 }
91
joshualitt74a07db2015-07-01 12:39:07 -070092 static const GrGLuint kScreenWidth = 800;
93 static const GrGLuint kScreenHeight = 600;
94 static const uint32_t kNumTri = 10000;
95 static const uint32_t kVerticesPerTri = 3;
96 static const uint32_t kDrawMultiplier = 512;
97
joshualitt6cd70ff2015-06-26 14:20:41 -070098 SkString fName;
99 VboSetup fVboSetup;
100 uint32_t fDrawDiv;
101 SkTArray<GrGLuint> fBuffers;
102 GrGLuint fProgram;
103 GrGLuint fVAO;
joshualitt74a07db2015-07-01 12:39:07 -0700104 GrGLuint fTexture;
joshualitt6cd70ff2015-06-26 14:20:41 -0700105};
106
joshualitt6cd70ff2015-06-26 14:20:41 -0700107///////////////////////////////////////////////////////////////////////////////////////////////////
108
joshualitt74a07db2015-07-01 12:39:07 -0700109GrGLuint GLCpuPosInstancedArraysBench::setupShader(const GrGLContext* ctx) {
Brian Salomon1edc5b92016-11-29 13:43:46 -0500110 const GrShaderCaps* shaderCaps = ctx->caps()->shaderCaps();
111 const char* version = shaderCaps->versionDeclString();
joshualitt6cd70ff2015-06-26 14:20:41 -0700112
113 // setup vertex shader
Brian Salomon99938a82016-11-21 13:41:08 -0500114 GrShaderVar aPosition("a_position", kVec2f_GrSLType, GrShaderVar::kIn_TypeModifier);
115 GrShaderVar aColor("a_color", kVec3f_GrSLType, GrShaderVar::kIn_TypeModifier);
116 GrShaderVar oColor("o_color", kVec3f_GrSLType, GrShaderVar::kOut_TypeModifier);
joshualitt6cd70ff2015-06-26 14:20:41 -0700117
118 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -0500119 aPosition.appendDecl(shaderCaps, &vshaderTxt);
joshualitt6cd70ff2015-06-26 14:20:41 -0700120 vshaderTxt.append(";\n");
Brian Salomon1edc5b92016-11-29 13:43:46 -0500121 aColor.appendDecl(shaderCaps, &vshaderTxt);
joshualitt6cd70ff2015-06-26 14:20:41 -0700122 vshaderTxt.append(";\n");
Brian Salomon1edc5b92016-11-29 13:43:46 -0500123 oColor.appendDecl(shaderCaps, &vshaderTxt);
joshualitt6cd70ff2015-06-26 14:20:41 -0700124 vshaderTxt.append(";\n");
125
126 vshaderTxt.append(
127 "void main()\n"
128 "{\n"
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400129 "gl_Position = float4(a_position, 0., 1.);\n"
joshualitt6cd70ff2015-06-26 14:20:41 -0700130 "o_color = a_color;\n"
131 "}\n");
132
joshualitt6cd70ff2015-06-26 14:20:41 -0700133 // setup fragment shader
Brian Salomon99938a82016-11-21 13:41:08 -0500134 GrShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, GrShaderVar::kOut_TypeModifier);
joshualitt6cd70ff2015-06-26 14:20:41 -0700135 SkString fshaderTxt(version);
Brian Osman33aa2c72017-04-05 09:26:15 -0400136 GrGLSLAppendDefaultFloatPrecisionDeclaration(kMedium_GrSLPrecision, *shaderCaps, &fshaderTxt);
Brian Salomonf31ae492016-11-18 15:35:33 -0500137 oColor.setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -0500138 oColor.appendDecl(shaderCaps, &fshaderTxt);
joshualitt6cd70ff2015-06-26 14:20:41 -0700139 fshaderTxt.append(";\n");
140
141 const char* fsOutName;
Brian Salomon1edc5b92016-11-29 13:43:46 -0500142 if (shaderCaps->mustDeclareFragmentShaderOutput()) {
143 oFragColor.appendDecl(shaderCaps, &fshaderTxt);
joshualitt6cd70ff2015-06-26 14:20:41 -0700144 fshaderTxt.append(";\n");
145 fsOutName = oFragColor.c_str();
146 } else {
147 fsOutName = "gl_FragColor";
148 }
149
150 fshaderTxt.appendf(
151 "void main()\n"
152 "{\n"
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400153 "%s = float4(o_color, 1.0);\n"
joshualitt6cd70ff2015-06-26 14:20:41 -0700154 "}\n", fsOutName);
155
ethannicholas5961bc92016-10-12 06:39:56 -0700156 return CreateProgram(ctx, vshaderTxt.c_str(), fshaderTxt.c_str());
joshualitt6cd70ff2015-06-26 14:20:41 -0700157}
158
159template<typename Func>
160static void setup_matrices(int numQuads, Func f) {
joshualitt6cd70ff2015-06-26 14:20:41 -0700161 // We draw a really small triangle so we are not fill rate limited
162 for (int i = 0 ; i < numQuads; i++) {
163 SkMatrix m = SkMatrix::I();
164 m.setScale(0.0001f, 0.0001f);
165 f(m);
166 }
167}
168
joshualitt6cd70ff2015-06-26 14:20:41 -0700169///////////////////////////////////////////////////////////////////////////////////////////////////
170
joshualitt74a07db2015-07-01 12:39:07 -0700171const GrGLContext* GLCpuPosInstancedArraysBench::onGetGLContext(const GrGLContext* ctx) {
172 // We only care about gpus with drawArraysInstanced support
173 if (!ctx->interface()->fFunctions.fDrawArraysInstanced) {
halcanary96fcdcc2015-08-27 07:41:13 -0700174 return nullptr;
joshualitt74a07db2015-07-01 12:39:07 -0700175 }
176 return ctx;
177}
178
joshualitt6cd70ff2015-06-26 14:20:41 -0700179void GLCpuPosInstancedArraysBench::setupInstanceVbo(const GrGLInterface* gl,
180 const SkMatrix* viewMatrices) {
181 // We draw all of the instances at a single place because we aren't allowed to have per vertex
182 // per instance attributes
183 SkPoint positions[kVerticesPerTri];
184 positions[0].set(-1.0f, -1.0f);
185 positions[1].set( 1.0f, -1.0f);
186 positions[2].set( 1.0f, 1.0f);
187 viewMatrices[0].mapPointsWithStride(positions, sizeof(SkPoint), kVerticesPerTri);
188
189 // setup colors so we can detect we are actually drawing instances(the last triangle will be
190 // a different color)
191 GrGLfloat colors[kVerticesPerTri * kNumTri];
192 for (uint32_t i = 0; i < kNumTri; i++) {
193 // set colors
194 uint32_t offset = i * kVerticesPerTri;
195 float color = i == kNumTri - 1 ? 1.0f : 0.0f;
196 colors[offset++] = color; colors[offset++] = 0.0f; colors[offset++] = 0.0f;
197 }
198
199 GrGLuint posVBO;
200 // setup position VBO
201 GR_GL_CALL(gl, GenBuffers(1, &posVBO));
202 GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, posVBO));
203 GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(positions), positions, GR_GL_STATIC_DRAW));
204 GR_GL_CALL(gl, EnableVertexAttribArray(0));
205 GR_GL_CALL(gl, VertexAttribPointer(0, 2, GR_GL_FLOAT, GR_GL_FALSE, 2 * sizeof(GrGLfloat),
206 (GrGLvoid*)0));
207
208 // setup color VBO
209 GrGLuint instanceVBO;
210 GR_GL_CALL(gl, GenBuffers(1, &instanceVBO));
211 GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, instanceVBO));
212 GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(colors), colors, GR_GL_STATIC_DRAW));
213 GR_GL_CALL(gl, EnableVertexAttribArray(1));
214 GR_GL_CALL(gl, VertexAttribPointer(1, 3, GR_GL_FLOAT, GR_GL_FALSE, 3 * sizeof(GrGLfloat),
215 (GrGLvoid*)0));
216 GR_GL_CALL(gl, VertexAttribDivisor(1, 1));
217 fBuffers.push_back(posVBO);
218 fBuffers.push_back(instanceVBO);
219}
220
221void GLCpuPosInstancedArraysBench::setupDoubleVbo(const GrGLInterface* gl,
222 const SkMatrix* viewMatrices) {
223 // Constants for our various shader programs
224 SkPoint positions[kVerticesPerTri * kNumTri];
225 GrGLfloat colors[kVerticesPerTri * kNumTri * 3];
226 for (uint32_t i = 0; i < kNumTri; i++) {
227 SkPoint* position = &positions[i * kVerticesPerTri];
228 position[0].set(-1.0f, -1.0f);
229 position[1].set( 1.0f, -1.0f);
230 position[2].set( 1.0f, 1.0f);
231 viewMatrices[i].mapPointsWithStride(position, sizeof(SkPoint), kVerticesPerTri);
232
233 // set colors
234 float color = i == kNumTri - 1 ? 1.0f : 0.0f;
235 uint32_t offset = i * kVerticesPerTri * 3;
236 for (uint32_t j = 0; j < kVerticesPerTri; j++) {
237 colors[offset++] = color; colors[offset++] = 0.0f; colors[offset++] = 0.0f;
238 }
239 }
240
241 GrGLuint posVBO, colorVBO;
242 // setup position VBO
243 GR_GL_CALL(gl, GenBuffers(1, &posVBO));
244 GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, posVBO));
245 GR_GL_CALL(gl, EnableVertexAttribArray(0));
246 GR_GL_CALL(gl, VertexAttribPointer(0, 2, GR_GL_FLOAT, GR_GL_FALSE, 2 * sizeof(GrGLfloat),
247 (GrGLvoid*)0));
248 GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(positions), positions, GR_GL_STATIC_DRAW));
249
250 // setup color VBO
251 GR_GL_CALL(gl, GenBuffers(1, &colorVBO));
252 GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, colorVBO));
253 GR_GL_CALL(gl, EnableVertexAttribArray(1));
254 GR_GL_CALL(gl, VertexAttribPointer(1, 3, GR_GL_FLOAT, GR_GL_FALSE, 3 * sizeof(GrGLfloat),
255 (GrGLvoid*)0));
256 GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(colors), colors, GR_GL_STATIC_DRAW));
257
258 fBuffers.push_back(posVBO);
259 fBuffers.push_back(colorVBO);
260}
261
262struct Vertex {
263 SkPoint fPositions;
264 GrGLfloat fColors[3];
265};
266
267void GLCpuPosInstancedArraysBench::setupSingleVbo(const GrGLInterface* gl,
268 const SkMatrix* viewMatrices) {
269 // Constants for our various shader programs
270 Vertex vertices[kVerticesPerTri * kNumTri];
271 for (uint32_t i = 0; i < kNumTri; i++) {
272 Vertex* v = &vertices[i * kVerticesPerTri];
273 v[0].fPositions.set(-1.0f, -1.0f);
274 v[1].fPositions.set( 1.0f, -1.0f);
275 v[2].fPositions.set( 1.0f, 1.0f);
276
277 SkPoint* position = reinterpret_cast<SkPoint*>(v);
278 viewMatrices[i].mapPointsWithStride(position, sizeof(Vertex), kVerticesPerTri);
279
280 // set colors
281 float color = i == kNumTri - 1 ? 1.0f : 0.0f;
282 for (uint32_t j = 0; j < kVerticesPerTri; j++) {
283 uint32_t offset = 0;
284 v->fColors[offset++] = color; v->fColors[offset++] = 0.0f; v->fColors[offset++] = 0.0f;
285 v++;
286 }
287 }
288
289 GrGLuint vbo;
290 // setup VBO
291 GR_GL_CALL(gl, GenBuffers(1, &vbo));
292 GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, vbo));
293 GR_GL_CALL(gl, EnableVertexAttribArray(0));
294 GR_GL_CALL(gl, EnableVertexAttribArray(1));
295 GR_GL_CALL(gl, VertexAttribPointer(0, 2, GR_GL_FLOAT, GR_GL_FALSE, sizeof(Vertex),
296 (GrGLvoid*)0));
297 GR_GL_CALL(gl, VertexAttribPointer(1, 3, GR_GL_FLOAT, GR_GL_FALSE, sizeof(Vertex),
298 (GrGLvoid*)(sizeof(SkPoint))));
299 GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(vertices), vertices, GR_GL_STATIC_DRAW));
300 fBuffers.push_back(vbo);
301}
302
303void GLCpuPosInstancedArraysBench::setup(const GrGLContext* ctx) {
304 const GrGLInterface* gl = ctx->interface();
joshualitt74a07db2015-07-01 12:39:07 -0700305 fTexture = SetupFramebuffer(gl, kScreenWidth, kScreenHeight);
joshualitt6cd70ff2015-06-26 14:20:41 -0700306
joshualitt74a07db2015-07-01 12:39:07 -0700307 fProgram = this->setupShader(ctx);
joshualitt6cd70ff2015-06-26 14:20:41 -0700308
309 // setup matrices
310 int index = 0;
311 SkMatrix viewMatrices[kNumTri];
312 setup_matrices(kNumTri, [&index, &viewMatrices](const SkMatrix& m) {
313 viewMatrices[index++] = m;
314 });
315
316 // setup VAO
317 GR_GL_CALL(gl, GenVertexArrays(1, &fVAO));
318 GR_GL_CALL(gl, BindVertexArray(fVAO));
319
320 switch (fVboSetup) {
321 case kUseOne_VboSetup:
322 this->setupSingleVbo(gl, viewMatrices);
323 break;
324 case kUseTwo_VboSetup:
325 this->setupDoubleVbo(gl, viewMatrices);
326 break;
327 case kUseInstance_VboSetup:
328 this->setupInstanceVbo(gl, viewMatrices);
329 break;
330 }
331
332 // clear screen
333 GR_GL_CALL(gl, ClearColor(0.03f, 0.03f, 0.03f, 1.0f));
334 GR_GL_CALL(gl, Clear(GR_GL_COLOR_BUFFER_BIT));
335
336 // set us up to draw
337 GR_GL_CALL(gl, UseProgram(fProgram));
338 GR_GL_CALL(gl, BindVertexArray(fVAO));
339}
340
mtkleina1ebeb22015-10-01 09:43:39 -0700341void GLCpuPosInstancedArraysBench::glDraw(int loops, const GrGLContext* ctx) {
joshualitt6cd70ff2015-06-26 14:20:41 -0700342 const GrGLInterface* gl = ctx->interface();
343
344 uint32_t maxTrianglesPerFlush = fDrawDiv == 0 ? kNumTri :
345 kDrawMultiplier / fDrawDiv;
346 uint32_t trianglesToDraw = loops * kDrawMultiplier;
347
348 if (kUseInstance_VboSetup == fVboSetup) {
349 while (trianglesToDraw > 0) {
350 uint32_t triangles = SkTMin(trianglesToDraw, maxTrianglesPerFlush);
351 GR_GL_CALL(gl, DrawArraysInstanced(GR_GL_TRIANGLES, 0, kVerticesPerTri, triangles));
352 trianglesToDraw -= triangles;
353 }
354 } else {
355 while (trianglesToDraw > 0) {
356 uint32_t triangles = SkTMin(trianglesToDraw, maxTrianglesPerFlush);
357 GR_GL_CALL(gl, DrawArrays(GR_GL_TRIANGLES, 0, kVerticesPerTri * triangles));
358 trianglesToDraw -= triangles;
359 }
360 }
361
joshualitt74a07db2015-07-01 12:39:07 -0700362#if 0
joshualitt6cd70ff2015-06-26 14:20:41 -0700363 //const char* filename = "/data/local/tmp/out.png";
364 SkString filename("out");
365 filename.appendf("_%s.png", this->getName());
joshualitt74a07db2015-07-01 12:39:07 -0700366 DumpImage(gl, kScreenWidth, kScreenHeight, filename.c_str());
joshualitt6cd70ff2015-06-26 14:20:41 -0700367#endif
368}
369
370void GLCpuPosInstancedArraysBench::teardown(const GrGLInterface* gl) {
joshualitt74a07db2015-07-01 12:39:07 -0700371 GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, 0));
372 GR_GL_CALL(gl, BindVertexArray(0));
373 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
374 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
375 GR_GL_CALL(gl, DeleteTextures(1, &fTexture));
joshualitt6cd70ff2015-06-26 14:20:41 -0700376 GR_GL_CALL(gl, DeleteProgram(fProgram));
377 GR_GL_CALL(gl, DeleteBuffers(fBuffers.count(), fBuffers.begin()));
378 GR_GL_CALL(gl, DeleteVertexArrays(1, &fVAO));
joshualitt31b21f62015-07-16 13:40:51 -0700379 fBuffers.reset();
joshualitt6cd70ff2015-06-26 14:20:41 -0700380}
381
382///////////////////////////////////////////////////////////////////////////////
383
384DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench::kUseInstance_VboSetup, 0) )
385DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench::kUseOne_VboSetup, 0) )
386DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench::kUseTwo_VboSetup, 0) )
387DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench::kUseOne_VboSetup, 1) )
388DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench::kUseTwo_VboSetup, 1) )
389DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench::kUseOne_VboSetup, 2) )
390DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench::kUseTwo_VboSetup, 2) )
391DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench::kUseOne_VboSetup, 4) )
392DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench::kUseTwo_VboSetup, 4) )
393DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench::kUseOne_VboSetup, 8) )
394DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench::kUseTwo_VboSetup, 8) )
395
396#endif