blob: 8a07002dd7d549557a648a8c8f80e1c241a3721f [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
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400114 GrShaderVar aPosition("a_position", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
115 GrShaderVar aColor("a_color", kHalf3_GrSLType, GrShaderVar::kIn_TypeModifier);
116 GrShaderVar oColor("o_color", kHalf3_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 Nicholasbed683a2017-09-26 14:23:59 -0400129 "sk_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
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400134 GrShaderVar oFragColor("o_FragColor", kHalf4_GrSLType, GrShaderVar::kOut_TypeModifier);
joshualitt6cd70ff2015-06-26 14:20:41 -0700135 SkString fshaderTxt(version);
Brian Salomonf31ae492016-11-18 15:35:33 -0500136 oColor.setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -0500137 oColor.appendDecl(shaderCaps, &fshaderTxt);
joshualitt6cd70ff2015-06-26 14:20:41 -0700138 fshaderTxt.append(";\n");
139
140 const char* fsOutName;
Brian Salomon1edc5b92016-11-29 13:43:46 -0500141 if (shaderCaps->mustDeclareFragmentShaderOutput()) {
142 oFragColor.appendDecl(shaderCaps, &fshaderTxt);
joshualitt6cd70ff2015-06-26 14:20:41 -0700143 fshaderTxt.append(";\n");
144 fsOutName = oFragColor.c_str();
145 } else {
146 fsOutName = "gl_FragColor";
147 }
148
149 fshaderTxt.appendf(
150 "void main()\n"
151 "{\n"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400152 "%s = float4(o_color, 1.0);\n"
joshualitt6cd70ff2015-06-26 14:20:41 -0700153 "}\n", fsOutName);
154
ethannicholas5961bc92016-10-12 06:39:56 -0700155 return CreateProgram(ctx, vshaderTxt.c_str(), fshaderTxt.c_str());
joshualitt6cd70ff2015-06-26 14:20:41 -0700156}
157
158template<typename Func>
159static void setup_matrices(int numQuads, Func f) {
joshualitt6cd70ff2015-06-26 14:20:41 -0700160 // We draw a really small triangle so we are not fill rate limited
161 for (int i = 0 ; i < numQuads; i++) {
162 SkMatrix m = SkMatrix::I();
163 m.setScale(0.0001f, 0.0001f);
164 f(m);
165 }
166}
167
joshualitt6cd70ff2015-06-26 14:20:41 -0700168///////////////////////////////////////////////////////////////////////////////////////////////////
169
joshualitt74a07db2015-07-01 12:39:07 -0700170const GrGLContext* GLCpuPosInstancedArraysBench::onGetGLContext(const GrGLContext* ctx) {
171 // We only care about gpus with drawArraysInstanced support
172 if (!ctx->interface()->fFunctions.fDrawArraysInstanced) {
halcanary96fcdcc2015-08-27 07:41:13 -0700173 return nullptr;
joshualitt74a07db2015-07-01 12:39:07 -0700174 }
175 return ctx;
176}
177
joshualitt6cd70ff2015-06-26 14:20:41 -0700178void GLCpuPosInstancedArraysBench::setupInstanceVbo(const GrGLInterface* gl,
179 const SkMatrix* viewMatrices) {
180 // We draw all of the instances at a single place because we aren't allowed to have per vertex
181 // per instance attributes
182 SkPoint positions[kVerticesPerTri];
183 positions[0].set(-1.0f, -1.0f);
184 positions[1].set( 1.0f, -1.0f);
185 positions[2].set( 1.0f, 1.0f);
186 viewMatrices[0].mapPointsWithStride(positions, sizeof(SkPoint), kVerticesPerTri);
187
188 // setup colors so we can detect we are actually drawing instances(the last triangle will be
189 // a different color)
190 GrGLfloat colors[kVerticesPerTri * kNumTri];
191 for (uint32_t i = 0; i < kNumTri; i++) {
192 // set colors
193 uint32_t offset = i * kVerticesPerTri;
194 float color = i == kNumTri - 1 ? 1.0f : 0.0f;
195 colors[offset++] = color; colors[offset++] = 0.0f; colors[offset++] = 0.0f;
196 }
197
198 GrGLuint posVBO;
199 // setup position VBO
200 GR_GL_CALL(gl, GenBuffers(1, &posVBO));
201 GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, posVBO));
202 GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(positions), positions, GR_GL_STATIC_DRAW));
203 GR_GL_CALL(gl, EnableVertexAttribArray(0));
204 GR_GL_CALL(gl, VertexAttribPointer(0, 2, GR_GL_FLOAT, GR_GL_FALSE, 2 * sizeof(GrGLfloat),
205 (GrGLvoid*)0));
206
207 // setup color VBO
208 GrGLuint instanceVBO;
209 GR_GL_CALL(gl, GenBuffers(1, &instanceVBO));
210 GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, instanceVBO));
211 GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(colors), colors, GR_GL_STATIC_DRAW));
212 GR_GL_CALL(gl, EnableVertexAttribArray(1));
213 GR_GL_CALL(gl, VertexAttribPointer(1, 3, GR_GL_FLOAT, GR_GL_FALSE, 3 * sizeof(GrGLfloat),
214 (GrGLvoid*)0));
215 GR_GL_CALL(gl, VertexAttribDivisor(1, 1));
216 fBuffers.push_back(posVBO);
217 fBuffers.push_back(instanceVBO);
218}
219
220void GLCpuPosInstancedArraysBench::setupDoubleVbo(const GrGLInterface* gl,
221 const SkMatrix* viewMatrices) {
222 // Constants for our various shader programs
223 SkPoint positions[kVerticesPerTri * kNumTri];
224 GrGLfloat colors[kVerticesPerTri * kNumTri * 3];
225 for (uint32_t i = 0; i < kNumTri; i++) {
226 SkPoint* position = &positions[i * kVerticesPerTri];
227 position[0].set(-1.0f, -1.0f);
228 position[1].set( 1.0f, -1.0f);
229 position[2].set( 1.0f, 1.0f);
230 viewMatrices[i].mapPointsWithStride(position, sizeof(SkPoint), kVerticesPerTri);
231
232 // set colors
233 float color = i == kNumTri - 1 ? 1.0f : 0.0f;
234 uint32_t offset = i * kVerticesPerTri * 3;
235 for (uint32_t j = 0; j < kVerticesPerTri; j++) {
236 colors[offset++] = color; colors[offset++] = 0.0f; colors[offset++] = 0.0f;
237 }
238 }
239
240 GrGLuint posVBO, colorVBO;
241 // setup position VBO
242 GR_GL_CALL(gl, GenBuffers(1, &posVBO));
243 GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, posVBO));
244 GR_GL_CALL(gl, EnableVertexAttribArray(0));
245 GR_GL_CALL(gl, VertexAttribPointer(0, 2, GR_GL_FLOAT, GR_GL_FALSE, 2 * sizeof(GrGLfloat),
246 (GrGLvoid*)0));
247 GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(positions), positions, GR_GL_STATIC_DRAW));
248
249 // setup color VBO
250 GR_GL_CALL(gl, GenBuffers(1, &colorVBO));
251 GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, colorVBO));
252 GR_GL_CALL(gl, EnableVertexAttribArray(1));
253 GR_GL_CALL(gl, VertexAttribPointer(1, 3, GR_GL_FLOAT, GR_GL_FALSE, 3 * sizeof(GrGLfloat),
254 (GrGLvoid*)0));
255 GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(colors), colors, GR_GL_STATIC_DRAW));
256
257 fBuffers.push_back(posVBO);
258 fBuffers.push_back(colorVBO);
259}
260
261struct Vertex {
262 SkPoint fPositions;
263 GrGLfloat fColors[3];
264};
265
266void GLCpuPosInstancedArraysBench::setupSingleVbo(const GrGLInterface* gl,
267 const SkMatrix* viewMatrices) {
268 // Constants for our various shader programs
269 Vertex vertices[kVerticesPerTri * kNumTri];
270 for (uint32_t i = 0; i < kNumTri; i++) {
271 Vertex* v = &vertices[i * kVerticesPerTri];
272 v[0].fPositions.set(-1.0f, -1.0f);
273 v[1].fPositions.set( 1.0f, -1.0f);
274 v[2].fPositions.set( 1.0f, 1.0f);
275
276 SkPoint* position = reinterpret_cast<SkPoint*>(v);
277 viewMatrices[i].mapPointsWithStride(position, sizeof(Vertex), kVerticesPerTri);
278
279 // set colors
280 float color = i == kNumTri - 1 ? 1.0f : 0.0f;
281 for (uint32_t j = 0; j < kVerticesPerTri; j++) {
282 uint32_t offset = 0;
283 v->fColors[offset++] = color; v->fColors[offset++] = 0.0f; v->fColors[offset++] = 0.0f;
284 v++;
285 }
286 }
287
288 GrGLuint vbo;
289 // setup VBO
290 GR_GL_CALL(gl, GenBuffers(1, &vbo));
291 GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, vbo));
292 GR_GL_CALL(gl, EnableVertexAttribArray(0));
293 GR_GL_CALL(gl, EnableVertexAttribArray(1));
294 GR_GL_CALL(gl, VertexAttribPointer(0, 2, GR_GL_FLOAT, GR_GL_FALSE, sizeof(Vertex),
295 (GrGLvoid*)0));
296 GR_GL_CALL(gl, VertexAttribPointer(1, 3, GR_GL_FLOAT, GR_GL_FALSE, sizeof(Vertex),
297 (GrGLvoid*)(sizeof(SkPoint))));
298 GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(vertices), vertices, GR_GL_STATIC_DRAW));
299 fBuffers.push_back(vbo);
300}
301
302void GLCpuPosInstancedArraysBench::setup(const GrGLContext* ctx) {
303 const GrGLInterface* gl = ctx->interface();
joshualitt74a07db2015-07-01 12:39:07 -0700304 fTexture = SetupFramebuffer(gl, kScreenWidth, kScreenHeight);
joshualitt6cd70ff2015-06-26 14:20:41 -0700305
joshualitt74a07db2015-07-01 12:39:07 -0700306 fProgram = this->setupShader(ctx);
joshualitt6cd70ff2015-06-26 14:20:41 -0700307
308 // setup matrices
309 int index = 0;
310 SkMatrix viewMatrices[kNumTri];
311 setup_matrices(kNumTri, [&index, &viewMatrices](const SkMatrix& m) {
312 viewMatrices[index++] = m;
313 });
314
315 // setup VAO
316 GR_GL_CALL(gl, GenVertexArrays(1, &fVAO));
317 GR_GL_CALL(gl, BindVertexArray(fVAO));
318
319 switch (fVboSetup) {
320 case kUseOne_VboSetup:
321 this->setupSingleVbo(gl, viewMatrices);
322 break;
323 case kUseTwo_VboSetup:
324 this->setupDoubleVbo(gl, viewMatrices);
325 break;
326 case kUseInstance_VboSetup:
327 this->setupInstanceVbo(gl, viewMatrices);
328 break;
329 }
330
331 // clear screen
332 GR_GL_CALL(gl, ClearColor(0.03f, 0.03f, 0.03f, 1.0f));
333 GR_GL_CALL(gl, Clear(GR_GL_COLOR_BUFFER_BIT));
334
335 // set us up to draw
336 GR_GL_CALL(gl, UseProgram(fProgram));
337 GR_GL_CALL(gl, BindVertexArray(fVAO));
338}
339
mtkleina1ebeb22015-10-01 09:43:39 -0700340void GLCpuPosInstancedArraysBench::glDraw(int loops, const GrGLContext* ctx) {
joshualitt6cd70ff2015-06-26 14:20:41 -0700341 const GrGLInterface* gl = ctx->interface();
342
343 uint32_t maxTrianglesPerFlush = fDrawDiv == 0 ? kNumTri :
344 kDrawMultiplier / fDrawDiv;
345 uint32_t trianglesToDraw = loops * kDrawMultiplier;
346
347 if (kUseInstance_VboSetup == fVboSetup) {
348 while (trianglesToDraw > 0) {
349 uint32_t triangles = SkTMin(trianglesToDraw, maxTrianglesPerFlush);
350 GR_GL_CALL(gl, DrawArraysInstanced(GR_GL_TRIANGLES, 0, kVerticesPerTri, triangles));
351 trianglesToDraw -= triangles;
352 }
353 } else {
354 while (trianglesToDraw > 0) {
355 uint32_t triangles = SkTMin(trianglesToDraw, maxTrianglesPerFlush);
356 GR_GL_CALL(gl, DrawArrays(GR_GL_TRIANGLES, 0, kVerticesPerTri * triangles));
357 trianglesToDraw -= triangles;
358 }
359 }
360
joshualitt74a07db2015-07-01 12:39:07 -0700361#if 0
joshualitt6cd70ff2015-06-26 14:20:41 -0700362 //const char* filename = "/data/local/tmp/out.png";
363 SkString filename("out");
364 filename.appendf("_%s.png", this->getName());
joshualitt74a07db2015-07-01 12:39:07 -0700365 DumpImage(gl, kScreenWidth, kScreenHeight, filename.c_str());
joshualitt6cd70ff2015-06-26 14:20:41 -0700366#endif
367}
368
369void GLCpuPosInstancedArraysBench::teardown(const GrGLInterface* gl) {
joshualitt74a07db2015-07-01 12:39:07 -0700370 GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, 0));
371 GR_GL_CALL(gl, BindVertexArray(0));
372 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
373 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
374 GR_GL_CALL(gl, DeleteTextures(1, &fTexture));
joshualitt6cd70ff2015-06-26 14:20:41 -0700375 GR_GL_CALL(gl, DeleteProgram(fProgram));
376 GR_GL_CALL(gl, DeleteBuffers(fBuffers.count(), fBuffers.begin()));
377 GR_GL_CALL(gl, DeleteVertexArrays(1, &fVAO));
joshualitt31b21f62015-07-16 13:40:51 -0700378 fBuffers.reset();
joshualitt6cd70ff2015-06-26 14:20:41 -0700379}
380
381///////////////////////////////////////////////////////////////////////////////
382
383DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench::kUseInstance_VboSetup, 0) )
384DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench::kUseOne_VboSetup, 0) )
385DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench::kUseTwo_VboSetup, 0) )
386DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench::kUseOne_VboSetup, 1) )
387DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench::kUseTwo_VboSetup, 1) )
388DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench::kUseOne_VboSetup, 2) )
389DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench::kUseTwo_VboSetup, 2) )
390DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench::kUseOne_VboSetup, 4) )
391DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench::kUseTwo_VboSetup, 4) )
392DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench::kUseOne_VboSetup, 8) )
393DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench::kUseTwo_VboSetup, 8) )
394
395#endif