blob: 040ca183004a1fce15fe094d5ebf6f793777a12b [file] [log] [blame]
joshualittf06c3892015-07-01 06:20:13 -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
joshualittf06c3892015-07-01 06:20:13 -070012#if SK_SUPPORT_GPU
joshualitt74a07db2015-07-01 12:39:07 -070013#include "GLBench.h"
egdanielf5294392015-10-21 07:14:17 -070014#include "gl/GrGLContext.h"
joshualittf06c3892015-07-01 06:20:13 -070015#include "gl/GrGLInterface.h"
joshualittf06c3892015-07-01 06:20:13 -070016#include "gl/GrGLUtil.h"
egdanielcb7ba1e2015-10-26 08:38:25 -070017#include "glsl/GrGLSL.h"
joshualittf06c3892015-07-01 06:20:13 -070018#include "glsl/GrGLSLCaps.h"
egdaniel0d3f0612015-10-21 10:45:48 -070019#include "glsl/GrGLSLShaderVar.h"
joshualittf06c3892015-07-01 06:20:13 -070020#include <stdio.h>
21
22/*
23 * This is a native GL benchmark for determining the cost of uploading vertex attributes
24 */
joshualitt74a07db2015-07-01 12:39:07 -070025class GLVertexAttributesBench : public GLBench {
joshualittf06c3892015-07-01 06:20:13 -070026public:
27 GLVertexAttributesBench(uint32_t attribs)
28 : fTexture(0)
29 , fBuffers(0)
30 , fProgram(0)
joshualittf06c3892015-07-01 06:20:13 -070031 , fVBO(0)
32 , fAttribs(attribs)
33 , fStride(2 * sizeof(SkPoint) + fAttribs * sizeof(GrGLfloat) * 4) {
34 fName.appendf("GLVertexAttributesBench_%d", fAttribs);
35 }
36
37protected:
38 const char* onGetName() override { return fName.c_str(); }
joshualitt74a07db2015-07-01 12:39:07 -070039 void setup(const GrGLContext*) override;
mtkleina1ebeb22015-10-01 09:43:39 -070040 void glDraw(int loops, const GrGLContext*) override;
joshualitt74a07db2015-07-01 12:39:07 -070041 void teardown(const GrGLInterface*) override;
joshualittf06c3892015-07-01 06:20:13 -070042
43 static const GrGLuint kScreenWidth = 800;
44 static const GrGLuint kScreenHeight = 600;
45 static const uint32_t kNumTri = 10000;
46 static const uint32_t kVerticesPerTri = 3;
47 static const uint32_t kDrawMultiplier = 512;
48 static const uint32_t kMaxAttribs = 7;
49
50private:
joshualitt74a07db2015-07-01 12:39:07 -070051 GrGLuint setupShader(const GrGLContext*, uint32_t attribs, uint32_t maxAttribs);
52
joshualittf06c3892015-07-01 06:20:13 -070053 GrGLuint fTexture;
54 SkTArray<GrGLuint> fBuffers;
55 GrGLuint fProgram;
joshualittf06c3892015-07-01 06:20:13 -070056 GrGLuint fVBO;
57 SkTArray<unsigned char> fVertices;
58 uint32_t fAttribs;
59 size_t fStride;
60 SkString fName;
61 typedef Benchmark INHERITED;
62};
63
joshualittf06c3892015-07-01 06:20:13 -070064///////////////////////////////////////////////////////////////////////////////////////////////////
65
joshualitt74a07db2015-07-01 12:39:07 -070066GrGLuint GLVertexAttributesBench::setupShader(const GrGLContext* ctx, uint32_t attribs,
67 uint32_t maxAttribs) {
egdanielcb7ba1e2015-10-26 08:38:25 -070068 const GrGLSLCaps* glslCaps = ctx->caps()->glslCaps();
69 const char* version = glslCaps->versionDeclString();
joshualittf06c3892015-07-01 06:20:13 -070070
71 // setup vertex shader
egdaniel0d3f0612015-10-21 10:45:48 -070072 GrGLSLShaderVar aPosition("a_position", kVec4f_GrSLType, GrShaderVar::kAttribute_TypeModifier);
73 SkTArray<GrGLSLShaderVar> aVars;
74 SkTArray<GrGLSLShaderVar> oVars;
joshualittf06c3892015-07-01 06:20:13 -070075
76 SkString vshaderTxt(version);
egdanielcb7ba1e2015-10-26 08:38:25 -070077 aPosition.appendDecl(glslCaps, &vshaderTxt);
joshualittf06c3892015-07-01 06:20:13 -070078 vshaderTxt.append(";\n");
79
80 for (uint32_t i = 0; i < attribs; i++) {
81 SkString aname;
82 aname.appendf("a_color_%d", i);
egdaniel0d3f0612015-10-21 10:45:48 -070083 aVars.push_back(GrGLSLShaderVar(aname.c_str(),
egdanielf5294392015-10-21 07:14:17 -070084 kVec4f_GrSLType,
85 GrShaderVar::kAttribute_TypeModifier));
egdanielcb7ba1e2015-10-26 08:38:25 -070086 aVars.back().appendDecl(glslCaps, &vshaderTxt);
joshualittf06c3892015-07-01 06:20:13 -070087 vshaderTxt.append(";\n");
88
89 }
90
91 for (uint32_t i = 0; i < maxAttribs; i++) {
92 SkString oname;
93 oname.appendf("o_color_%d", i);
egdaniel0d3f0612015-10-21 10:45:48 -070094 oVars.push_back(GrGLSLShaderVar(oname.c_str(),
egdanielf5294392015-10-21 07:14:17 -070095 kVec4f_GrSLType,
96 GrShaderVar::kVaryingOut_TypeModifier));
egdanielcb7ba1e2015-10-26 08:38:25 -070097 oVars.back().appendDecl(glslCaps, &vshaderTxt);
joshualittf06c3892015-07-01 06:20:13 -070098 vshaderTxt.append(";\n");
99 }
100
101 vshaderTxt.append(
102 "void main()\n"
103 "{\n"
104 "gl_Position = a_position;\n");
105
106 for (uint32_t i = 0; i < attribs; i++) {
107 vshaderTxt.appendf("%s = %s;\n", oVars[i].c_str(), aVars[i].c_str());
108 }
109
110 // Passthrough position as a dummy
111 for (uint32_t i = attribs; i < maxAttribs; i++) {
joshualittbd929d92015-07-01 08:34:50 -0700112 vshaderTxt.appendf("%s = vec4(0, 0, 0, 1);\n", oVars[i].c_str());
joshualittf06c3892015-07-01 06:20:13 -0700113 }
114
115 vshaderTxt.append("}\n");
116
117 const GrGLInterface* gl = ctx->interface();
joshualittf06c3892015-07-01 06:20:13 -0700118
119 // setup fragment shader
egdaniel0d3f0612015-10-21 10:45:48 -0700120 GrGLSLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, GrShaderVar::kOut_TypeModifier);
joshualittf06c3892015-07-01 06:20:13 -0700121 SkString fshaderTxt(version);
egdanielcb7ba1e2015-10-26 08:38:25 -0700122 GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, *glslCaps, &fshaderTxt);
joshualittf06c3892015-07-01 06:20:13 -0700123
124 const char* fsOutName;
egdanielcb7ba1e2015-10-26 08:38:25 -0700125 if (glslCaps->mustDeclareFragmentShaderOutput()) {
126 oFragColor.appendDecl(glslCaps, &fshaderTxt);
joshualittf06c3892015-07-01 06:20:13 -0700127 fshaderTxt.append(";\n");
128 fsOutName = oFragColor.c_str();
129 } else {
130 fsOutName = "gl_FragColor";
131 }
132
133 for (uint32_t i = 0; i < maxAttribs; i++) {
134 oVars[i].setTypeModifier(GrShaderVar::kVaryingIn_TypeModifier);
egdanielcb7ba1e2015-10-26 08:38:25 -0700135 oVars[i].appendDecl(glslCaps, &fshaderTxt);
joshualittf06c3892015-07-01 06:20:13 -0700136 fshaderTxt.append(";\n");
137 }
138
139 fshaderTxt.appendf(
140 "void main()\n"
141 "{\n"
142 "%s = ", fsOutName);
143
144 fshaderTxt.appendf("%s", oVars[0].c_str());
145 for (uint32_t i = 1; i < maxAttribs; i++) {
146 fshaderTxt.appendf(" + %s", oVars[i].c_str());
147 }
148
149 fshaderTxt.append(";\n"
150 "}\n");
151
joshualitt74a07db2015-07-01 12:39:07 -0700152 return CreateProgram(gl, vshaderTxt.c_str(), fshaderTxt.c_str());
joshualittf06c3892015-07-01 06:20:13 -0700153}
154
155///////////////////////////////////////////////////////////////////////////////////////////////////
156
157void GLVertexAttributesBench::setup(const GrGLContext* ctx) {
158 const GrGLInterface* gl = ctx->interface();
joshualitt74a07db2015-07-01 12:39:07 -0700159 fTexture = SetupFramebuffer(gl, kScreenWidth, kScreenHeight);
joshualittf06c3892015-07-01 06:20:13 -0700160
joshualitt74a07db2015-07-01 12:39:07 -0700161 fProgram = setupShader(ctx, fAttribs, kMaxAttribs);
joshualittf06c3892015-07-01 06:20:13 -0700162
163 // setup matrices
164 SkMatrix viewMatrices[kNumTri];
165 for (uint32_t i = 0 ; i < kNumTri; i++) {
166 SkMatrix m = SkMatrix::I();
167 m.setScale(0.0001f, 0.0001f);
168 viewMatrices[i] = m;
169 }
170
joshualittf06c3892015-07-01 06:20:13 -0700171 // presetup vertex attributes, color is set to be a light gray no matter how many vertex
172 // attributes are used
173 float targetColor = 0.9f;
174 float colorContribution = targetColor / fAttribs;
175 fVertices.reset(static_cast<int>(kVerticesPerTri * kNumTri * fStride));
176 for (uint32_t i = 0; i < kNumTri; i++) {
177 unsigned char* ptr = &fVertices[static_cast<int>(i * kVerticesPerTri * fStride)];
178 SkPoint* p = reinterpret_cast<SkPoint*>(ptr);
179 p->set(-1.0f, -1.0f); p++; p->set( 0.0f, 1.0f);
180 p = reinterpret_cast<SkPoint*>(ptr + fStride);
181 p->set( 1.0f, -1.0f); p++; p->set( 0.0f, 1.0f);
182 p = reinterpret_cast<SkPoint*>(ptr + fStride * 2);
183 p->set( 1.0f, 1.0f); p++; p->set( 0.0f, 1.0f);
184
185 SkPoint* position = reinterpret_cast<SkPoint*>(ptr);
186 viewMatrices[i].mapPointsWithStride(position, fStride, kVerticesPerTri);
187
188 // set colors
189 for (uint32_t j = 0; j < kVerticesPerTri; j++) {
190 GrGLfloat* f = reinterpret_cast<GrGLfloat*>(ptr + 2 * sizeof(SkPoint) + fStride * j);
191 for (uint32_t k = 0; k < fAttribs * 4; k += 4) {
192 f[k] = colorContribution;
193 f[k + 1] = colorContribution;
194 f[k + 2] = colorContribution;
195 f[k + 3] = 1.0f;
196 }
197 }
198 }
199
200 GR_GL_CALL(gl, GenBuffers(1, &fVBO));
201 fBuffers.push_back(fVBO);
202
203 // clear screen
204 GR_GL_CALL(gl, ClearColor(0.03f, 0.03f, 0.03f, 1.0f));
205 GR_GL_CALL(gl, Clear(GR_GL_COLOR_BUFFER_BIT));
206
207 // set us up to draw
208 GR_GL_CALL(gl, UseProgram(fProgram));
joshualittf06c3892015-07-01 06:20:13 -0700209}
210
mtkleina1ebeb22015-10-01 09:43:39 -0700211void GLVertexAttributesBench::glDraw(int loops, const GrGLContext* ctx) {
joshualittf06c3892015-07-01 06:20:13 -0700212 const GrGLInterface* gl = ctx->interface();
213
214 // upload vertex attributes
215 GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, fVBO));
216 GR_GL_CALL(gl, EnableVertexAttribArray(0));
217 GR_GL_CALL(gl, VertexAttribPointer(0, 4, GR_GL_FLOAT, GR_GL_FALSE, (GrGLsizei)fStride,
218 (GrGLvoid*)0));
219
220 size_t runningStride = 2 * sizeof(SkPoint);
221 for (uint32_t i = 0; i < fAttribs; i++) {
222 int attribId = i + 1;
223 GR_GL_CALL(gl, EnableVertexAttribArray(attribId));
224 GR_GL_CALL(gl, VertexAttribPointer(attribId, 4, GR_GL_FLOAT, GR_GL_FALSE,
225 (GrGLsizei)fStride, (GrGLvoid*)(runningStride)));
226 runningStride += sizeof(GrGLfloat) * 4;
227 }
228
229 GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, fVertices.count(), fVertices.begin(),
230 GR_GL_STREAM_DRAW));
231
232 uint32_t maxTrianglesPerFlush = kNumTri;
233 uint32_t trianglesToDraw = loops * kDrawMultiplier;
234
235 while (trianglesToDraw > 0) {
236 uint32_t triangles = SkTMin(trianglesToDraw, maxTrianglesPerFlush);
237 GR_GL_CALL(gl, DrawArrays(GR_GL_TRIANGLES, 0, kVerticesPerTri * triangles));
238 trianglesToDraw -= triangles;
239 }
240
joshualitt74a07db2015-07-01 12:39:07 -0700241#if 0
joshualittf06c3892015-07-01 06:20:13 -0700242 //const char* filename = "/data/local/tmp/out.png";
243 SkString filename("out");
244 filename.appendf("_%s.png", this->getName());
joshualitt74a07db2015-07-01 12:39:07 -0700245 DumpImage(gl, kScreenWidth, kScreenHeight, filename.c_str());
joshualittf06c3892015-07-01 06:20:13 -0700246#endif
247}
248
joshualitt74a07db2015-07-01 12:39:07 -0700249void GLVertexAttributesBench::teardown(const GrGLInterface* gl) {
250 // teardown
251 GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, 0));
252 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
253 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
254 GR_GL_CALL(gl, DeleteTextures(1, &fTexture));
255 GR_GL_CALL(gl, DeleteProgram(fProgram));
256 GR_GL_CALL(gl, DeleteBuffers(fBuffers.count(), fBuffers.begin()));
joshualitt31b21f62015-07-16 13:40:51 -0700257 fBuffers.reset();
joshualitt74a07db2015-07-01 12:39:07 -0700258}
joshualittf06c3892015-07-01 06:20:13 -0700259
260///////////////////////////////////////////////////////////////////////////////
261
262DEF_BENCH( return new GLVertexAttributesBench(0) )
263DEF_BENCH( return new GLVertexAttributesBench(1) )
264DEF_BENCH( return new GLVertexAttributesBench(2) )
265DEF_BENCH( return new GLVertexAttributesBench(3) )
266DEF_BENCH( return new GLVertexAttributesBench(4) )
267DEF_BENCH( return new GLVertexAttributesBench(5) )
268DEF_BENCH( return new GLVertexAttributesBench(6) )
269DEF_BENCH( return new GLVertexAttributesBench(7) )
270#endif