blob: b007a7a8ba3c219b1d5ea3a48d3f5ddbcc500ee8 [file] [log] [blame]
wangyix891f0f32015-07-15 12:26:07 -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 "SkMatrix.h"
9#include "SkPoint.h"
10#include "SkString.h"
11
12#if SK_SUPPORT_GPU
13#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"
wangyix891f0f32015-07-15 12:26:07 -070017#include "gl/GrGLInterface.h"
wangyix891f0f32015-07-15 12:26:07 -070018#include "gl/GrGLUtil.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050019#include "../private/GrGLSL.h"
wangyix891f0f32015-07-15 12:26:07 -070020
21#include <stdio.h>
22
23/**
24 * This is a GL benchmark for comparing the performance of using vec4 or float for coverage in GLSL.
25 * The generated shader code from this bench will draw several overlapping circles, one in each
26 * stage, to simulate coverage calculations. The number of circles (i.e. the number of stages) can
27 * be set as a parameter.
28 */
29
30class GLVec4ScalarBench : public GLBench {
31public:
32 /*
33 * Use float or vec4 as GLSL data type for the output coverage
34 */
35 enum CoverageSetup {
36 kUseScalar_CoverageSetup,
37 kUseVec4_CoverageSetup,
38 };
39
40 /*
41 * numStages determines the number of shader stages before the XP,
42 * which consequently determines how many circles are drawn
43 */
44 GLVec4ScalarBench(CoverageSetup coverageSetup, uint32_t numStages)
45 : fCoverageSetup(coverageSetup)
46 , fNumStages(numStages)
47 , fVboId(0)
48 , fProgram(0) {
49 fName = NumStagesSetupToStr(coverageSetup, numStages);
50 }
51
52protected:
53 const char* onGetName() override {
54 return fName.c_str();
55 }
56
57 void setup(const GrGLContext*) override;
mtkleina1ebeb22015-10-01 09:43:39 -070058 void glDraw(int loops, const GrGLContext*) override;
wangyix891f0f32015-07-15 12:26:07 -070059 void teardown(const GrGLInterface*) override;
60
61private:
62 void setupSingleVbo(const GrGLInterface*, const SkMatrix*);
63 GrGLuint setupShader(const GrGLContext*);
64
65
66 static SkString NumStagesSetupToStr(CoverageSetup coverageSetup, uint32_t numStages) {
67 SkString name("GLVec4ScalarBench");
68 switch (coverageSetup) {
69 default:
70 case kUseScalar_CoverageSetup:
71 name.appendf("_scalar_%u_stage", numStages);
72 break;
73 case kUseVec4_CoverageSetup:
74 name.appendf("_vec4_%u_stage", numStages);
75 break;
76 }
77 return name;
78 }
79
80 static const GrGLuint kScreenWidth = 800;
81 static const GrGLuint kScreenHeight = 600;
82 static const uint32_t kNumTriPerDraw = 512;
83 static const uint32_t kVerticesPerTri = 3;
84
85 SkString fName;
86 CoverageSetup fCoverageSetup;
87 uint32_t fNumStages;
88 GrGLuint fVboId;
89 GrGLuint fProgram;
90 GrGLuint fFboTextureId;
91};
92
93///////////////////////////////////////////////////////////////////////////////////////////////////
94
95GrGLuint GLVec4ScalarBench::setupShader(const GrGLContext* ctx) {
Brian Salomon1edc5b92016-11-29 13:43:46 -050096 const GrShaderCaps* shaderCaps = ctx->caps()->shaderCaps();
97 const char* version = shaderCaps->versionDeclString();
wangyix891f0f32015-07-15 12:26:07 -070098
99 // this shader draws fNumStages overlapping circles of increasing opacity (coverage) and
100 // decreasing size, with the center of each subsequent circle closer to the bottom-right
101 // corner of the screen than the previous circle.
102
103 // set up vertex shader; this is a trivial vertex shader that passes through position and color
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400104 GrShaderVar aPosition("a_position", kHalf2_GrSLType, GrShaderVar::kIn_TypeModifier);
105 GrShaderVar oPosition("o_position", kHalf2_GrSLType, GrShaderVar::kOut_TypeModifier);
106 GrShaderVar aColor("a_color", kHalf3_GrSLType, GrShaderVar::kIn_TypeModifier);
107 GrShaderVar oColor("o_color", kHalf3_GrSLType, GrShaderVar::kOut_TypeModifier);
wangyix891f0f32015-07-15 12:26:07 -0700108
109 SkString vshaderTxt(version);
Brian Salomon1edc5b92016-11-29 13:43:46 -0500110 aPosition.appendDecl(shaderCaps, &vshaderTxt);
wangyix891f0f32015-07-15 12:26:07 -0700111 vshaderTxt.append(";\n");
Brian Salomon1edc5b92016-11-29 13:43:46 -0500112 aColor.appendDecl(shaderCaps, &vshaderTxt);
wangyix891f0f32015-07-15 12:26:07 -0700113 vshaderTxt.append(";\n");
Brian Salomon1edc5b92016-11-29 13:43:46 -0500114 oPosition.appendDecl(shaderCaps, &vshaderTxt);
wangyix891f0f32015-07-15 12:26:07 -0700115 vshaderTxt.append(";\n");
Brian Salomon1edc5b92016-11-29 13:43:46 -0500116 oColor.appendDecl(shaderCaps, &vshaderTxt);
wangyix891f0f32015-07-15 12:26:07 -0700117 vshaderTxt.append(";\n");
118
119 vshaderTxt.append(
120 "void main()\n"
121 "{\n"
Ethan Nicholasbed683a2017-09-26 14:23:59 -0400122 " sk_Position = float4(a_position, 0.0, 1.0);\n"
wangyix891f0f32015-07-15 12:26:07 -0700123 " o_position = a_position;\n"
124 " o_color = a_color;\n"
125 "}\n");
126
wangyix891f0f32015-07-15 12:26:07 -0700127 // set up fragment shader; this fragment shader will have fNumStages coverage stages plus an
128 // XP stage at the end. Each coverage stage computes the pixel's distance from some hard-
129 // coded center and compare that to some hard-coded circle radius to compute a coverage.
130 // Then, this coverage is mixed with the coverage from the previous stage and passed to the
131 // next stage.
wangyix891f0f32015-07-15 12:26:07 -0700132 SkString fshaderTxt(version);
Brian Salomonf31ae492016-11-18 15:35:33 -0500133 oPosition.setTypeModifier(GrShaderVar::kIn_TypeModifier);
Brian Salomon1edc5b92016-11-29 13:43:46 -0500134 oPosition.appendDecl(shaderCaps, &fshaderTxt);
wangyix891f0f32015-07-15 12:26:07 -0700135 fshaderTxt.append(";\n");
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);
wangyix891f0f32015-07-15 12:26:07 -0700138 fshaderTxt.append(";\n");
139
wangyix891f0f32015-07-15 12:26:07 -0700140 fshaderTxt.appendf(
141 "void main()\n"
142 "{\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400143 " half4 outputColor;\n"
wangyix891f0f32015-07-15 12:26:07 -0700144 " %s outputCoverage;\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400145 " outputColor = half4(%s, 1.0);\n"
wangyix891f0f32015-07-15 12:26:07 -0700146 " outputCoverage = %s;\n",
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400147 fCoverageSetup == kUseVec4_CoverageSetup ? "half4" : "half",
wangyix891f0f32015-07-15 12:26:07 -0700148 oColor.getName().c_str(),
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400149 fCoverageSetup == kUseVec4_CoverageSetup ? "half4(1.0)" : "1.0"
wangyix891f0f32015-07-15 12:26:07 -0700150 );
151
152 float radius = 1.0f;
153 for (uint32_t i = 0; i < fNumStages; i++) {
154 float centerX = 1.0f - radius;
155 float centerY = 1.0f - radius;
156 fshaderTxt.appendf(
157 " {\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400158 " half d = length(%s - half2(%f, %f));\n"
159 " half edgeAlpha = clamp(100.0 * (%f - d), 0.0, 1.0);\n"
wangyix891f0f32015-07-15 12:26:07 -0700160 " outputCoverage = 0.5 * outputCoverage + 0.5 * %s;\n"
161 " }\n",
162 oPosition.getName().c_str(), centerX, centerY,
163 radius,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400164 fCoverageSetup == kUseVec4_CoverageSetup ? "half4(edgeAlpha)" : "edgeAlpha"
wangyix891f0f32015-07-15 12:26:07 -0700165 );
166 radius *= 0.8f;
167 }
Brian Osmanf5beefd2017-11-17 15:29:47 -0500168 fshaderTxt.append(
wangyix891f0f32015-07-15 12:26:07 -0700169 " {\n"
Brian Osmanf5beefd2017-11-17 15:29:47 -0500170 " sk_FragColor = outputColor * outputCoverage;\n"
wangyix891f0f32015-07-15 12:26:07 -0700171 " }\n"
Brian Osmanf5beefd2017-11-17 15:29:47 -0500172 "}\n");
wangyix891f0f32015-07-15 12:26:07 -0700173
ethannicholas5961bc92016-10-12 06:39:56 -0700174 return CreateProgram(ctx, vshaderTxt.c_str(), fshaderTxt.c_str());
wangyix891f0f32015-07-15 12:26:07 -0700175}
176
177template<typename Func>
178static void setup_matrices(int numQuads, Func f) {
179 // We draw a really small triangle so we are not fill rate limited
180 for (int i = 0 ; i < numQuads; i++) {
181 SkMatrix m = SkMatrix::I();
182 m.setScale(0.01f, 0.01f);
183 f(m);
184 }
185}
186
187///////////////////////////////////////////////////////////////////////////////////////////////////
188
189struct Vertex {
190 SkPoint fPositions;
191 GrGLfloat fColors[3];
192};
193
194void GLVec4ScalarBench::setupSingleVbo(const GrGLInterface* gl, const SkMatrix* viewMatrices) {
195 // triangles drawn will alternate between the top-right half of the screen and the bottom-left
196 // half of the screen
197 Vertex vertices[kVerticesPerTri * kNumTriPerDraw];
198 for (uint32_t i = 0; i < kNumTriPerDraw; i++) {
199 Vertex* v = &vertices[i * kVerticesPerTri];
200 if (i % 2 == 0) {
201 v[0].fPositions.set(-1.0f, -1.0f);
202 v[1].fPositions.set( 1.0f, -1.0f);
203 v[2].fPositions.set( 1.0f, 1.0f);
204 } else {
205 v[0].fPositions.set(-1.0f, -1.0f);
206 v[1].fPositions.set( 1.0f, 1.0f);
207 v[2].fPositions.set( -1.0f, 1.0f);
208 }
209 SkPoint* position = reinterpret_cast<SkPoint*>(v);
210 viewMatrices[i].mapPointsWithStride(position, sizeof(Vertex), kVerticesPerTri);
211
212 GrGLfloat color[3] = {1.0f, 0.0f, 1.0f};
213 for (uint32_t j = 0; j < kVerticesPerTri; j++) {
214 v->fColors[0] = color[0];
215 v->fColors[1] = color[1];
216 v->fColors[2] = color[2];
217 v++;
218 }
219 }
220
221 GR_GL_CALL(gl, GenBuffers(1, &fVboId));
222 GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, fVboId));
223 GR_GL_CALL(gl, EnableVertexAttribArray(0));
224 GR_GL_CALL(gl, EnableVertexAttribArray(1));
225 GR_GL_CALL(gl, VertexAttribPointer(0, 2, GR_GL_FLOAT, GR_GL_FALSE, sizeof(Vertex),
226 (GrGLvoid*)0));
227 GR_GL_CALL(gl, VertexAttribPointer(1, 3, GR_GL_FLOAT, GR_GL_FALSE, sizeof(Vertex),
228 (GrGLvoid*)(sizeof(SkPoint))));
229 GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(vertices), vertices, GR_GL_STATIC_DRAW));
230}
231
232void GLVec4ScalarBench::setup(const GrGLContext* ctx) {
233 const GrGLInterface* gl = ctx->interface();
234 if (!gl) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400235 SK_ABORT("GL interface is nullptr in setup()!\n");
wangyix891f0f32015-07-15 12:26:07 -0700236 }
237 fFboTextureId = SetupFramebuffer(gl, kScreenWidth, kScreenHeight);
238
239 fProgram = this->setupShader(ctx);
240
241 int index = 0;
242 SkMatrix viewMatrices[kNumTriPerDraw];
243 setup_matrices(kNumTriPerDraw, [&index, &viewMatrices](const SkMatrix& m) {
244 viewMatrices[index++] = m;
245 });
246 this->setupSingleVbo(gl, viewMatrices);
247
248 GR_GL_CALL(gl, UseProgram(fProgram));
249}
250
mtkleina1ebeb22015-10-01 09:43:39 -0700251void GLVec4ScalarBench::glDraw(int loops, const GrGLContext* ctx) {
wangyix891f0f32015-07-15 12:26:07 -0700252 const GrGLInterface* gl = ctx->interface();
253
254 for (int i = 0; i < loops; i++) {
255 GR_GL_CALL(gl, DrawArrays(GR_GL_TRIANGLES, 0, kVerticesPerTri * kNumTriPerDraw));
256 }
257
258// using -w when running nanobench will not produce correct images;
259// changing this to #if 1 will write the correct images to the Skia folder.
260#if 0
261 SkString filename("out");
262 filename.appendf("_%s.png", this->getName());
263 DumpImage(gl, kScreenWidth, kScreenHeight, filename.c_str());
264#endif
265}
266
267void GLVec4ScalarBench::teardown(const GrGLInterface* gl) {
268 GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, 0));
269 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0));
270 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, 0));
271 GR_GL_CALL(gl, DeleteTextures(1, &fFboTextureId));
272 GR_GL_CALL(gl, DeleteProgram(fProgram));
273 GR_GL_CALL(gl, DeleteBuffers(1, &fVboId));
274}
275
276///////////////////////////////////////////////////////////////////////////////
277
278DEF_BENCH( return new GLVec4ScalarBench(GLVec4ScalarBench::kUseScalar_CoverageSetup, 1) )
279DEF_BENCH( return new GLVec4ScalarBench(GLVec4ScalarBench::kUseVec4_CoverageSetup, 1) )
280DEF_BENCH( return new GLVec4ScalarBench(GLVec4ScalarBench::kUseScalar_CoverageSetup, 2) )
281DEF_BENCH( return new GLVec4ScalarBench(GLVec4ScalarBench::kUseVec4_CoverageSetup, 2) )
282DEF_BENCH( return new GLVec4ScalarBench(GLVec4ScalarBench::kUseScalar_CoverageSetup, 4) )
283DEF_BENCH( return new GLVec4ScalarBench(GLVec4ScalarBench::kUseVec4_CoverageSetup, 4) )
284DEF_BENCH( return new GLVec4ScalarBench(GLVec4ScalarBench::kUseScalar_CoverageSetup, 6) )
285DEF_BENCH( return new GLVec4ScalarBench(GLVec4ScalarBench::kUseVec4_CoverageSetup, 6) )
286DEF_BENCH( return new GLVec4ScalarBench(GLVec4ScalarBench::kUseScalar_CoverageSetup, 8) )
287DEF_BENCH( return new GLVec4ScalarBench(GLVec4ScalarBench::kUseVec4_CoverageSetup, 8) )
288
289#endif