OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Benchmark.h" | 8 #include "Benchmark.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkImageEncoder.h" | 10 #include "SkImageEncoder.h" |
| 11 |
11 #if SK_SUPPORT_GPU | 12 #if SK_SUPPORT_GPU |
12 #include "GrTest.h" | 13 #include "GLBench.h" |
13 #include "gl/GrGLGLSL.h" | 14 #include "gl/GrGLGLSL.h" |
14 #include "gl/GrGLInterface.h" | 15 #include "gl/GrGLInterface.h" |
15 #include "gl/GrGLShaderVar.h" | 16 #include "gl/GrGLShaderVar.h" |
16 #include "gl/GrGLUtil.h" | 17 #include "gl/GrGLUtil.h" |
17 #include "glsl/GrGLSLCaps.h" | 18 #include "glsl/GrGLSLCaps.h" |
18 #include <stdio.h> | |
19 | 19 |
20 /* | 20 /* |
21 * This is a native GL benchmark for instanced arrays vs vertex buffer objects.
To benchmark this | 21 * This is a native GL benchmark for instanced arrays vs vertex buffer objects.
To benchmark this |
22 * functionality, we draw n * kDrawMultipier triangles per run. If this number
is less than | 22 * functionality, we draw n * kDrawMultipier triangles per run. If this number
is less than |
23 * kNumTri then we do a single draw, either with instances, or drawArrays. Othe
rwise we do | 23 * kNumTri then we do a single draw, either with instances, or drawArrays. Othe
rwise we do |
24 * multiple draws. | 24 * multiple draws. |
25 * | 25 * |
26 * Additionally, there is a divisor, which if > 0 will act as a multiplier for t
he number of draws | 26 * Additionally, there is a divisor, which if > 0 will act as a multiplier for t
he number of draws |
27 * issued. | 27 * issued. |
28 */ | 28 */ |
29 class GLInstancedArraysBench : public Benchmark { | |
30 public: | |
31 GLInstancedArraysBench() : fTexture(0) {} | |
32 | 29 |
33 protected: | 30 class GLCpuPosInstancedArraysBench : public GLBench { |
34 void onPerCanvasPreDraw(SkCanvas* canvas) override; | |
35 virtual void setup(const GrGLContext*)=0; | |
36 void onPerCanvasPostDraw(SkCanvas* canvas) override; | |
37 virtual void teardown(const GrGLInterface*)=0; | |
38 | |
39 static const GrGLuint kScreenWidth = 800; | |
40 static const GrGLuint kScreenHeight = 600; | |
41 static const uint32_t kNumTri = 10000; | |
42 static const uint32_t kVerticesPerTri = 3; | |
43 static const uint32_t kDrawMultiplier = 512; | |
44 | |
45 private: | |
46 GrGLuint fTexture; | |
47 typedef Benchmark INHERITED; | |
48 }; | |
49 | |
50 #if 0 | |
51 class GLGpuPosInstancedArraysBench : public GLInstancedArraysBench { | |
52 protected: | |
53 const char* onGetName() override { | |
54 return "GLInstancedArraysBench_gpupos"; | |
55 } | |
56 | |
57 void setup(const GrGLContext*) override; | |
58 void onDraw(const int loops, SkCanvas* canvas) override; | |
59 }; | |
60 #endif | |
61 | |
62 class GLCpuPosInstancedArraysBench : public GLInstancedArraysBench { | |
63 public: | 31 public: |
64 /* | 32 /* |
65 * Clients can decide to use either: | 33 * Clients can decide to use either: |
66 * kUseOne_VboSetup - one vertex buffer with colors and positions inter
leaved | 34 * kUseOne_VboSetup - one vertex buffer with colors and positions inter
leaved |
67 * kUseTwo_VboSetup - two vertex buffers, one for colors, one for posit
ions | 35 * kUseTwo_VboSetup - two vertex buffers, one for colors, one for posit
ions |
68 * kUseInstance_VboSetup - two vertex buffers, one with per vertex indices,
one with per | 36 * kUseInstance_VboSetup - two vertex buffers, one with per vertex indices,
one with per |
69 * instance colors | 37 * instance colors |
70 */ | 38 */ |
71 enum VboSetup { | 39 enum VboSetup { |
72 kUseOne_VboSetup, | 40 kUseOne_VboSetup, |
(...skipping 12 matching lines...) Expand all Loading... |
85 , fProgram(0) | 53 , fProgram(0) |
86 , fVAO(0) { | 54 , fVAO(0) { |
87 fName = VboSetupToStr(vboSetup, fDrawDiv); | 55 fName = VboSetupToStr(vboSetup, fDrawDiv); |
88 } | 56 } |
89 | 57 |
90 protected: | 58 protected: |
91 const char* onGetName() override { | 59 const char* onGetName() override { |
92 return fName.c_str(); | 60 return fName.c_str(); |
93 } | 61 } |
94 | 62 |
| 63 const GrGLContext* onGetGLContext(const GrGLContext*) override; |
95 void setup(const GrGLContext*) override; | 64 void setup(const GrGLContext*) override; |
96 void onDraw(const int loops, SkCanvas* canvas) override; | 65 void glDraw(const int loops, const GrGLContext*) override; |
97 void teardown(const GrGLInterface*) override; | 66 void teardown(const GrGLInterface*) override; |
98 | 67 |
99 private: | 68 private: |
100 void setupInstanceVbo(const GrGLInterface*, const SkMatrix*); | 69 void setupInstanceVbo(const GrGLInterface*, const SkMatrix*); |
101 void setupDoubleVbo(const GrGLInterface*, const SkMatrix*); | 70 void setupDoubleVbo(const GrGLInterface*, const SkMatrix*); |
102 void setupSingleVbo(const GrGLInterface*, const SkMatrix*); | 71 void setupSingleVbo(const GrGLInterface*, const SkMatrix*); |
| 72 GrGLuint setupShader(const GrGLContext*); |
103 | 73 |
104 static SkString VboSetupToStr(VboSetup vboSetup, uint32_t drawDiv) { | 74 static SkString VboSetupToStr(VboSetup vboSetup, uint32_t drawDiv) { |
105 SkString name("GLInstancedArraysBench"); | 75 SkString name("GLInstancedArraysBench"); |
106 switch (vboSetup) { | 76 switch (vboSetup) { |
107 default: | 77 default: |
108 case kUseOne_VboSetup: | 78 case kUseOne_VboSetup: |
109 name.appendf("_one_%u", drawDiv); | 79 name.appendf("_one_%u", drawDiv); |
110 break; | 80 break; |
111 case kUseTwo_VboSetup: | 81 case kUseTwo_VboSetup: |
112 name.appendf("_two_%u", drawDiv); | 82 name.appendf("_two_%u", drawDiv); |
113 break; | 83 break; |
114 case kUseInstance_VboSetup: | 84 case kUseInstance_VboSetup: |
115 name.append("_instance"); | 85 name.append("_instance"); |
116 break; | 86 break; |
117 } | 87 } |
118 return name; | 88 return name; |
119 } | 89 } |
120 | 90 |
| 91 static const GrGLuint kScreenWidth = 800; |
| 92 static const GrGLuint kScreenHeight = 600; |
| 93 static const uint32_t kNumTri = 10000; |
| 94 static const uint32_t kVerticesPerTri = 3; |
| 95 static const uint32_t kDrawMultiplier = 512; |
| 96 |
121 SkString fName; | 97 SkString fName; |
122 VboSetup fVboSetup; | 98 VboSetup fVboSetup; |
123 uint32_t fDrawDiv; | 99 uint32_t fDrawDiv; |
124 SkTArray<GrGLuint> fBuffers; | 100 SkTArray<GrGLuint> fBuffers; |
125 GrGLuint fProgram; | 101 GrGLuint fProgram; |
126 GrGLuint fVAO; | 102 GrGLuint fVAO; |
| 103 GrGLuint fTexture; |
127 }; | 104 }; |
128 | 105 |
129 static const GrGLContext* get_gl_context(SkCanvas* canvas) { | |
130 // This bench exclusively tests GL calls directly | |
131 if (NULL == canvas->getGrContext()) { | |
132 return NULL; | |
133 } | |
134 GrContext* context = canvas->getGrContext(); | |
135 | |
136 GrTestTarget tt; | |
137 context->getTestTarget(&tt); | |
138 if (!tt.target()) { | |
139 SkDebugf("Couldn't get Gr test target."); | |
140 return NULL; | |
141 } | |
142 | |
143 const GrGLContext* ctx = tt.glContext(); | |
144 if (!ctx) { | |
145 SkDebugf("Couldn't get an interface\n"); | |
146 return NULL; | |
147 } | |
148 | |
149 // We only care about gpus with drawArraysInstanced support | |
150 if (!ctx->interface()->fFunctions.fDrawArraysInstanced) { | |
151 return NULL; | |
152 } | |
153 return ctx; | |
154 } | |
155 | |
156 void GLInstancedArraysBench::onPerCanvasPreDraw(SkCanvas* canvas) { | |
157 // This bench exclusively tests GL calls directly | |
158 const GrGLContext* ctx = get_gl_context(canvas); | |
159 if (!ctx) { | |
160 return; | |
161 } | |
162 this->setup(ctx); | |
163 } | |
164 | |
165 void GLInstancedArraysBench::onPerCanvasPostDraw(SkCanvas* canvas) { | |
166 // This bench exclusively tests GL calls directly | |
167 const GrGLContext* ctx = get_gl_context(canvas); | |
168 if (!ctx) { | |
169 return; | |
170 } | |
171 | |
172 const GrGLInterface* gl = ctx->interface(); | |
173 | |
174 // teardown | |
175 GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, 0)); | |
176 GR_GL_CALL(gl, BindVertexArray(0)); | |
177 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0)); | |
178 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, 0)); | |
179 GR_GL_CALL(gl, DeleteTextures(1, &fTexture)); | |
180 | |
181 this->teardown(gl); | |
182 } | |
183 | |
184 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 106 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
185 | 107 |
186 // TODO move all of the gpu positioning stuff to a new file | 108 GrGLuint GLCpuPosInstancedArraysBench::setupShader(const GrGLContext* ctx) { |
187 #ifdef GPU_POS | |
188 static const char* gpu_vertex_shader = | |
189 "layout (location = 0) in vec2 position;\n" | |
190 "layout (location = 1) in vec3 color;\n" | |
191 "layout (location = 2) in mat3 offset;\n" | |
192 | |
193 "out vec3 fColor;\n" | |
194 | |
195 "void main()\n" | |
196 "{\n" | |
197 "gl_Position = vec4(offset * vec3(position, 1.0f), 1.f);\n" | |
198 "fColor = color;\n" | |
199 "}\n"; | |
200 #endif | |
201 | |
202 static GrGLuint load_shader(const GrGLInterface* gl, const char* shaderSrc, GrGL
enum type) { | |
203 GrGLuint shader; | |
204 // Create the shader object | |
205 GR_GL_CALL_RET(gl, shader, CreateShader(type)); | |
206 | |
207 // Load the shader source | |
208 GR_GL_CALL(gl, ShaderSource(shader, 1, &shaderSrc, NULL)); | |
209 | |
210 // Compile the shader | |
211 GR_GL_CALL(gl, CompileShader(shader)); | |
212 | |
213 // Check for compile time errors | |
214 GrGLint success; | |
215 GrGLchar infoLog[512]; | |
216 GR_GL_CALL(gl, GetShaderiv(shader, GR_GL_COMPILE_STATUS, &success)); | |
217 if (!success) | |
218 { | |
219 GR_GL_CALL(gl, GetShaderInfoLog(shader, 512, NULL, infoLog)); | |
220 SkDebugf("ERROR::SHADER::COMPLIATION_FAILED: %s\n", infoLog); | |
221 } | |
222 | |
223 return shader; | |
224 } | |
225 | |
226 static GrGLuint compile_shader(const GrGLContext* ctx) { | |
227 const char* version = GrGLGetGLSLVersionDecl(*ctx); | 109 const char* version = GrGLGetGLSLVersionDecl(*ctx); |
228 | 110 |
229 // setup vertex shader | 111 // setup vertex shader |
230 GrGLShaderVar aPosition("a_position", kVec2f_GrSLType, GrShaderVar::kAttribu
te_TypeModifier); | 112 GrGLShaderVar aPosition("a_position", kVec2f_GrSLType, GrShaderVar::kAttribu
te_TypeModifier); |
231 GrGLShaderVar aColor("a_color", kVec3f_GrSLType, GrShaderVar::kAttribute_Typ
eModifier); | 113 GrGLShaderVar aColor("a_color", kVec3f_GrSLType, GrShaderVar::kAttribute_Typ
eModifier); |
232 GrGLShaderVar oColor("o_color", kVec3f_GrSLType, GrShaderVar::kVaryingOut_Ty
peModifier); | 114 GrGLShaderVar oColor("o_color", kVec3f_GrSLType, GrShaderVar::kVaryingOut_Ty
peModifier); |
233 | 115 |
234 SkString vshaderTxt(version); | 116 SkString vshaderTxt(version); |
235 aPosition.appendDecl(*ctx, &vshaderTxt); | 117 aPosition.appendDecl(*ctx, &vshaderTxt); |
236 vshaderTxt.append(";\n"); | 118 vshaderTxt.append(";\n"); |
237 aColor.appendDecl(*ctx, &vshaderTxt); | 119 aColor.appendDecl(*ctx, &vshaderTxt); |
238 vshaderTxt.append(";\n"); | 120 vshaderTxt.append(";\n"); |
239 oColor.appendDecl(*ctx, &vshaderTxt); | 121 oColor.appendDecl(*ctx, &vshaderTxt); |
240 vshaderTxt.append(";\n"); | 122 vshaderTxt.append(";\n"); |
241 | 123 |
242 vshaderTxt.append( | 124 vshaderTxt.append( |
243 "void main()\n" | 125 "void main()\n" |
244 "{\n" | 126 "{\n" |
245 "gl_Position = vec4(a_position, 0.f, 1.f);\n" | 127 "gl_Position = vec4(a_position, 0.f, 1.f);\n" |
246 "o_color = a_color;\n" | 128 "o_color = a_color;\n" |
247 "}\n"); | 129 "}\n"); |
248 | 130 |
249 const GrGLInterface* gl = ctx->interface(); | 131 const GrGLInterface* gl = ctx->interface(); |
250 GrGLuint vertexShader = load_shader(gl, vshaderTxt.c_str(), GR_GL_VERTEX_SHA
DER); | |
251 | 132 |
252 // setup fragment shader | 133 // setup fragment shader |
253 GrGLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, GrShaderVar::kOut_T
ypeModifier); | 134 GrGLShaderVar oFragColor("o_FragColor", kVec4f_GrSLType, GrShaderVar::kOut_T
ypeModifier); |
254 SkString fshaderTxt(version); | 135 SkString fshaderTxt(version); |
255 GrGLAppendGLSLDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, gl->f
Standard, | 136 GrGLAppendGLSLDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, gl->f
Standard, |
256 &fshaderTxt); | 137 &fshaderTxt); |
257 oColor.setTypeModifier(GrShaderVar::kVaryingIn_TypeModifier); | 138 oColor.setTypeModifier(GrShaderVar::kVaryingIn_TypeModifier); |
258 oColor.appendDecl(*ctx, &fshaderTxt); | 139 oColor.appendDecl(*ctx, &fshaderTxt); |
259 fshaderTxt.append(";\n"); | 140 fshaderTxt.append(";\n"); |
260 | 141 |
261 const char* fsOutName; | 142 const char* fsOutName; |
262 if (ctx->caps()->glslCaps()->mustDeclareFragmentShaderOutput()) { | 143 if (ctx->caps()->glslCaps()->mustDeclareFragmentShaderOutput()) { |
263 oFragColor.appendDecl(*ctx, &fshaderTxt); | 144 oFragColor.appendDecl(*ctx, &fshaderTxt); |
264 fshaderTxt.append(";\n"); | 145 fshaderTxt.append(";\n"); |
265 fsOutName = oFragColor.c_str(); | 146 fsOutName = oFragColor.c_str(); |
266 } else { | 147 } else { |
267 fsOutName = "gl_FragColor"; | 148 fsOutName = "gl_FragColor"; |
268 } | 149 } |
269 | 150 |
270 fshaderTxt.appendf( | 151 fshaderTxt.appendf( |
271 "void main()\n" | 152 "void main()\n" |
272 "{\n" | 153 "{\n" |
273 "%s = vec4(o_color, 1.0f);\n" | 154 "%s = vec4(o_color, 1.0f);\n" |
274 "}\n", fsOutName); | 155 "}\n", fsOutName); |
275 | 156 |
276 GrGLuint fragmentShader = load_shader(gl, fshaderTxt.c_str(), GR_GL_FRAGMENT
_SHADER); | 157 return CreateProgram(gl, vshaderTxt.c_str(), fshaderTxt.c_str()); |
277 | |
278 GrGLint shaderProgram; | |
279 GR_GL_CALL_RET(gl, shaderProgram, CreateProgram()); | |
280 GR_GL_CALL(gl, AttachShader(shaderProgram, vertexShader)); | |
281 GR_GL_CALL(gl, AttachShader(shaderProgram, fragmentShader)); | |
282 GR_GL_CALL(gl, LinkProgram(shaderProgram)); | |
283 | |
284 // Check for linking errors | |
285 GrGLint success; | |
286 GrGLchar infoLog[512]; | |
287 GR_GL_CALL(gl, GetProgramiv(shaderProgram, GR_GL_LINK_STATUS, &success)); | |
288 if (!success) { | |
289 GR_GL_CALL(gl, GetProgramInfoLog(shaderProgram, 512, NULL, infoLog)); | |
290 SkDebugf("Linker Error: %s\n", infoLog); | |
291 } | |
292 GR_GL_CALL(gl, DeleteShader(vertexShader)); | |
293 GR_GL_CALL(gl, DeleteShader(fragmentShader)); | |
294 | |
295 return shaderProgram; | |
296 } | |
297 | |
298 //#define DUMP_IMAGES | |
299 #ifdef DUMP_IMAGES | |
300 static void dump_image(const GrGLInterface* gl, uint32_t screenWidth, uint32_t s
creenHeight, | |
301 const char* filename) { | |
302 // read back pixels | |
303 uint32_t readback[screenWidth * screenHeight]; | |
304 GR_GL_CALL(gl, ReadPixels(0, // x | |
305 0, // y | |
306 screenWidth, // width | |
307 screenHeight, // height | |
308 GR_GL_RGBA, //format | |
309 GR_GL_UNSIGNED_BYTE, //type | |
310 readback)); | |
311 | |
312 // dump png | |
313 SkBitmap bm; | |
314 if (!bm.tryAllocPixels(SkImageInfo::MakeN32Premul(screenWidth, screenHeight)
)) { | |
315 SkDebugf("couldn't allocate bitmap\n"); | |
316 return; | |
317 } | |
318 | |
319 bm.setPixels(readback); | |
320 | |
321 if (!SkImageEncoder::EncodeFile(filename, bm, SkImageEncoder::kPNG_Type, 100
)) { | |
322 SkDebugf("------ failed to encode %s\n", filename); | |
323 remove(filename); // remove any partial file | |
324 return; | |
325 } | |
326 } | |
327 #endif | |
328 | |
329 static void setup_framebuffer(const GrGLInterface* gl, int screenWidth, int scre
enHeight) { | |
330 //Setup framebuffer | |
331 GrGLuint texture; | |
332 GR_GL_CALL(gl, PixelStorei(GR_GL_UNPACK_ROW_LENGTH, 0)); | |
333 GR_GL_CALL(gl, PixelStorei(GR_GL_PACK_ROW_LENGTH, 0)); | |
334 GR_GL_CALL(gl, GenTextures(1, &texture)); | |
335 GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE15)); | |
336 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, texture)); | |
337 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MAG_FILTER, GR_
GL_NEAREST)); | |
338 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MIN_FILTER, GR_
GL_NEAREST)); | |
339 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_WRAP_S, GR_GL_C
LAMP_TO_EDGE)); | |
340 GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_WRAP_T, GR_GL_C
LAMP_TO_EDGE)); | |
341 GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, | |
342 0, //level | |
343 GR_GL_RGBA8, //internal format | |
344 screenWidth, // width | |
345 screenHeight, // height | |
346 0, //border | |
347 GR_GL_RGBA, //format | |
348 GR_GL_UNSIGNED_BYTE, // type | |
349 NULL)); | |
350 | |
351 // bind framebuffer | |
352 GrGLuint framebuffer; | |
353 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0)); | |
354 GR_GL_CALL(gl, GenFramebuffers(1, &framebuffer)); | |
355 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, framebuffer)); | |
356 GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER, | |
357 GR_GL_COLOR_ATTACHMENT0, | |
358 GR_GL_TEXTURE_2D, | |
359 texture, 0)); | |
360 GR_GL_CALL(gl, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); | |
361 GR_GL_CALL(gl, Viewport(0, 0, screenWidth, screenHeight)); | |
362 } | 158 } |
363 | 159 |
364 template<typename Func> | 160 template<typename Func> |
365 static void setup_matrices(int numQuads, Func f) { | 161 static void setup_matrices(int numQuads, Func f) { |
366 #if 0 | |
367 float max = sqrt(numQuads); | |
368 float pos = 1.f / (2 * max); | |
369 GrGLfloat offset = pos * 2; | |
370 for(GrGLint row = 0; row < max; row++) { | |
371 for(GrGLint col = 0; col < max; col++) { | |
372 SkScalar xOffset = col / max * 2.f - 1.f + offset; | |
373 SkScalar yOffset = row / max * 2.f - 1.f + offset; | |
374 SkMatrix translation; | |
375 SkRandom random; | |
376 translation.setScale(pos, pos); | |
377 translation.postTranslate(xOffset, yOffset); | |
378 f(translation); | |
379 } | |
380 } | |
381 #endif | |
382 // We draw a really small triangle so we are not fill rate limited | 162 // We draw a really small triangle so we are not fill rate limited |
383 for (int i = 0 ; i < numQuads; i++) { | 163 for (int i = 0 ; i < numQuads; i++) { |
384 SkMatrix m = SkMatrix::I(); | 164 SkMatrix m = SkMatrix::I(); |
385 m.setScale(0.0001f, 0.0001f); | 165 m.setScale(0.0001f, 0.0001f); |
386 f(m); | 166 f(m); |
387 } | 167 } |
388 } | 168 } |
389 | 169 |
390 #ifdef GPU_POS | 170 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
391 void GLGpuPosInstancedArraysBench::setup(const GrGLInterface* gl) { | |
392 setup_framebuffer(gl, kScreenWidth, kScreenHeight); | |
393 | 171 |
394 // compile and use shaders | 172 const GrGLContext* GLCpuPosInstancedArraysBench::onGetGLContext(const GrGLContex
t* ctx) { |
395 GrGLint shaderProgram = compile_shader(gl, gpu_vertex_shader, fragment_shade
r); | 173 // We only care about gpus with drawArraysInstanced support |
396 | 174 if (!ctx->interface()->fFunctions.fDrawArraysInstanced) { |
397 // translations | 175 return NULL; |
398 int index = 0; | 176 } |
399 GrGLfloat viewMatrices[fNumQuads * fSkMatrixNumCells]; | 177 return ctx; |
400 setup_matrices(fNumQuads, [&index, &viewMatrices](const SkMatrix& m) { | |
401 GrGLGetMatrix<3>(&viewMatrices[index], m); | |
402 index += fSkMatrixNumCells; | |
403 }); | |
404 | |
405 // Constants for our various shader programs | |
406 GrGLfloat quad_vertices[] = { | |
407 // Positions // Colors | |
408 -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, | |
409 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, | |
410 -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, | |
411 | |
412 -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, | |
413 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, | |
414 1.0f, 1.0f, 0.0f, 1.0f, 1.0f | |
415 }; | |
416 | |
417 // update vertex data | |
418 GrGLuint quadVAO, quadVBO; | |
419 GR_GL_CALL(gl, GenVertexArrays(1, &quadVAO)); | |
420 GR_GL_CALL(gl, GenBuffers(1, &quadVBO)); | |
421 GR_GL_CALL(gl, BindVertexArray(quadVAO)); | |
422 GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, quadVBO)); | |
423 GR_GL_CALL(gl, EnableVertexAttribArray(0)); | |
424 GR_GL_CALL(gl, VertexAttribPointer(0, 2, GR_GL_FLOAT, GR_GL_FALSE, 5 * sizeo
f(GrGLfloat), (GrGLvoid*)0)); | |
425 GR_GL_CALL(gl, EnableVertexAttribArray(1)); | |
426 GR_GL_CALL(gl, VertexAttribPointer(1, 3, GR_GL_FLOAT, GR_GL_FALSE, 5 * sizeo
f(GrGLfloat), (GrGLvoid*)(2 * sizeof(GrGLfloat)))); | |
427 GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(quad_vertices), quad_ve
rtices, GR_GL_STATIC_DRAW)); | |
428 | |
429 // Also set instance data | |
430 GrGLuint instanceVBO; | |
431 GR_GL_CALL(gl, GenBuffers(1, &instanceVBO)); | |
432 GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, instanceVBO)); | |
433 GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(GrGLfloat) * fSkMatrixN
umCells * fNumQuads, | |
434 &viewMatrices[0], GR_GL_STATIC_DRAW)); | |
435 GR_GL_CALL(gl, EnableVertexAttribArray(2)); | |
436 GR_GL_CALL(gl, EnableVertexAttribArray(3)); | |
437 GR_GL_CALL(gl, EnableVertexAttribArray(4)); | |
438 GR_GL_CALL(gl, VertexAttribPointer(2, 3, GR_GL_FLOAT, GR_GL_FALSE, 9 * sizeo
f(GrGLfloat), (GrGLvoid*)0)); | |
439 GR_GL_CALL(gl, VertexAttribPointer(3, 3, GR_GL_FLOAT, GR_GL_FALSE, 9 * sizeo
f(GrGLfloat), (GrGLvoid*)(3 * sizeof(GrGLfloat)))); | |
440 GR_GL_CALL(gl, VertexAttribPointer(4, 3, GR_GL_FLOAT, GR_GL_FALSE, 9 * sizeo
f(GrGLfloat), (GrGLvoid*)(6 * sizeof(GrGLfloat)))); | |
441 GR_GL_CALL(gl, VertexAttribDivisor(2, 1)); | |
442 GR_GL_CALL(gl, VertexAttribDivisor(3, 1)); | |
443 GR_GL_CALL(gl, VertexAttribDivisor(4, 1)); | |
444 | |
445 // draw | |
446 GR_GL_CALL(gl, ClearColor(0.03f, 0.03f, 0.03f, 1.0f)); | |
447 GR_GL_CALL(gl, Clear(GR_GL_COLOR_BUFFER_BIT)); | |
448 | |
449 // set us up to draw | |
450 GR_GL_CALL(gl, UseProgram(shaderProgram)); | |
451 GR_GL_CALL(gl, BindVertexArray(quadVAO)); | |
452 } | 178 } |
453 | 179 |
454 void GLGpuPosInstancedArraysBench::onDraw(const int loops, SkCanvas* canvas) { | |
455 const GrGLInterface* gl = get_interface(canvas); | |
456 if (!gl) { | |
457 return; | |
458 } | |
459 | |
460 GR_GL_CALL(gl, DrawArraysInstanced(GR_GL_TRIANGLES, 0, 6, fNumQuads)); | |
461 | |
462 #ifdef DUMP_IMAGES | |
463 const char* filename = "out.png"; | |
464 dump_image(gl, kScreenWidth, kScreenHeight, filename); | |
465 #endif | |
466 SkFAIL("done\n"); | |
467 } | |
468 | |
469 static uint32_t setup_quad_index_buffer(const GrGLInterface* gl) { | |
470 static const int kMaxQuads = 1;//1 << 12; // max possible: (1 << 14) - 1; | |
471 GR_STATIC_ASSERT(4 * kMaxQuads <= 65535); | |
472 static const uint16_t kPattern[] = { 0, 1, 2, 0, 2, 3 }; | |
473 static const int kPatternSize = 6; | |
474 static const int kVertCount = 4; | |
475 static const int kIndicesCount = kPatternSize * kMaxQuads; | |
476 int size = kPatternSize * kMaxQuads * sizeof(uint16_t); | |
477 | |
478 uint16_t* data = SkNEW_ARRAY(uint16_t, kMaxQuads * kPatternSize); | |
479 | |
480 for (int i = 0; i < kMaxQuads; ++i) { | |
481 int baseIdx = i * kPatternSize; | |
482 uint16_t baseVert = (uint16_t)(i * kVertCount); | |
483 for (int j = 0; j < kPatternSize; ++j) { | |
484 data[baseIdx+j] = baseVert + kPattern[j]; | |
485 } | |
486 } | |
487 | |
488 GrGLuint quadIBO; | |
489 GR_GL_CALL(gl, GenBuffers(1, &quadIBO)); | |
490 GR_GL_CALL(gl, BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, quadIBO)); | |
491 GR_GL_CALL(gl, BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, size, data, GR_GL_STAT
IC_DRAW)); | |
492 | |
493 SkDELETE_ARRAY(data); | |
494 return kIndicesCount; | |
495 } | |
496 #endif | |
497 | |
498 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | |
499 | |
500 void GLCpuPosInstancedArraysBench::setupInstanceVbo(const GrGLInterface* gl, | 180 void GLCpuPosInstancedArraysBench::setupInstanceVbo(const GrGLInterface* gl, |
501 const SkMatrix* viewMatrices
) { | 181 const SkMatrix* viewMatrices
) { |
502 // We draw all of the instances at a single place because we aren't allowed
to have per vertex | 182 // We draw all of the instances at a single place because we aren't allowed
to have per vertex |
503 // per instance attributes | 183 // per instance attributes |
504 SkPoint positions[kVerticesPerTri]; | 184 SkPoint positions[kVerticesPerTri]; |
505 positions[0].set(-1.0f, -1.0f); | 185 positions[0].set(-1.0f, -1.0f); |
506 positions[1].set( 1.0f, -1.0f); | 186 positions[1].set( 1.0f, -1.0f); |
507 positions[2].set( 1.0f, 1.0f); | 187 positions[2].set( 1.0f, 1.0f); |
508 viewMatrices[0].mapPointsWithStride(positions, sizeof(SkPoint), kVerticesPer
Tri); | 188 viewMatrices[0].mapPointsWithStride(positions, sizeof(SkPoint), kVerticesPer
Tri); |
509 | 189 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
616 GR_GL_CALL(gl, VertexAttribPointer(0, 2, GR_GL_FLOAT, GR_GL_FALSE, sizeof(Ve
rtex), | 296 GR_GL_CALL(gl, VertexAttribPointer(0, 2, GR_GL_FLOAT, GR_GL_FALSE, sizeof(Ve
rtex), |
617 (GrGLvoid*)0)); | 297 (GrGLvoid*)0)); |
618 GR_GL_CALL(gl, VertexAttribPointer(1, 3, GR_GL_FLOAT, GR_GL_FALSE, sizeof(Ve
rtex), | 298 GR_GL_CALL(gl, VertexAttribPointer(1, 3, GR_GL_FLOAT, GR_GL_FALSE, sizeof(Ve
rtex), |
619 (GrGLvoid*)(sizeof(SkPoint)))); | 299 (GrGLvoid*)(sizeof(SkPoint)))); |
620 GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(vertices), vertices, GR
_GL_STATIC_DRAW)); | 300 GR_GL_CALL(gl, BufferData(GR_GL_ARRAY_BUFFER, sizeof(vertices), vertices, GR
_GL_STATIC_DRAW)); |
621 fBuffers.push_back(vbo); | 301 fBuffers.push_back(vbo); |
622 } | 302 } |
623 | 303 |
624 void GLCpuPosInstancedArraysBench::setup(const GrGLContext* ctx) { | 304 void GLCpuPosInstancedArraysBench::setup(const GrGLContext* ctx) { |
625 const GrGLInterface* gl = ctx->interface(); | 305 const GrGLInterface* gl = ctx->interface(); |
626 setup_framebuffer(gl, kScreenWidth, kScreenHeight); | 306 fTexture = SetupFramebuffer(gl, kScreenWidth, kScreenHeight); |
627 | 307 |
628 fProgram = compile_shader(ctx); | 308 fProgram = this->setupShader(ctx); |
629 | 309 |
630 // setup matrices | 310 // setup matrices |
631 int index = 0; | 311 int index = 0; |
632 SkMatrix viewMatrices[kNumTri]; | 312 SkMatrix viewMatrices[kNumTri]; |
633 setup_matrices(kNumTri, [&index, &viewMatrices](const SkMatrix& m) { | 313 setup_matrices(kNumTri, [&index, &viewMatrices](const SkMatrix& m) { |
634 viewMatrices[index++] = m; | 314 viewMatrices[index++] = m; |
635 }); | 315 }); |
636 | 316 |
637 // setup VAO | 317 // setup VAO |
638 GR_GL_CALL(gl, GenVertexArrays(1, &fVAO)); | 318 GR_GL_CALL(gl, GenVertexArrays(1, &fVAO)); |
(...skipping 13 matching lines...) Expand all Loading... |
652 | 332 |
653 // clear screen | 333 // clear screen |
654 GR_GL_CALL(gl, ClearColor(0.03f, 0.03f, 0.03f, 1.0f)); | 334 GR_GL_CALL(gl, ClearColor(0.03f, 0.03f, 0.03f, 1.0f)); |
655 GR_GL_CALL(gl, Clear(GR_GL_COLOR_BUFFER_BIT)); | 335 GR_GL_CALL(gl, Clear(GR_GL_COLOR_BUFFER_BIT)); |
656 | 336 |
657 // set us up to draw | 337 // set us up to draw |
658 GR_GL_CALL(gl, UseProgram(fProgram)); | 338 GR_GL_CALL(gl, UseProgram(fProgram)); |
659 GR_GL_CALL(gl, BindVertexArray(fVAO)); | 339 GR_GL_CALL(gl, BindVertexArray(fVAO)); |
660 } | 340 } |
661 | 341 |
662 void GLCpuPosInstancedArraysBench::onDraw(const int loops, SkCanvas* canvas) { | 342 void GLCpuPosInstancedArraysBench::glDraw(const int loops, const GrGLContext* ct
x) { |
663 const GrGLContext* ctx = get_gl_context(canvas); | |
664 if (!ctx) { | |
665 return; | |
666 } | |
667 | |
668 const GrGLInterface* gl = ctx->interface(); | 343 const GrGLInterface* gl = ctx->interface(); |
669 | 344 |
670 uint32_t maxTrianglesPerFlush = fDrawDiv == 0 ? kNumTri : | 345 uint32_t maxTrianglesPerFlush = fDrawDiv == 0 ? kNumTri : |
671 kDrawMultiplier / fDrawDiv; | 346 kDrawMultiplier / fDrawDiv; |
672 uint32_t trianglesToDraw = loops * kDrawMultiplier; | 347 uint32_t trianglesToDraw = loops * kDrawMultiplier; |
673 | 348 |
674 if (kUseInstance_VboSetup == fVboSetup) { | 349 if (kUseInstance_VboSetup == fVboSetup) { |
675 while (trianglesToDraw > 0) { | 350 while (trianglesToDraw > 0) { |
676 uint32_t triangles = SkTMin(trianglesToDraw, maxTrianglesPerFlush); | 351 uint32_t triangles = SkTMin(trianglesToDraw, maxTrianglesPerFlush); |
677 GR_GL_CALL(gl, DrawArraysInstanced(GR_GL_TRIANGLES, 0, kVerticesPerT
ri, triangles)); | 352 GR_GL_CALL(gl, DrawArraysInstanced(GR_GL_TRIANGLES, 0, kVerticesPerT
ri, triangles)); |
678 trianglesToDraw -= triangles; | 353 trianglesToDraw -= triangles; |
679 } | 354 } |
680 } else { | 355 } else { |
681 while (trianglesToDraw > 0) { | 356 while (trianglesToDraw > 0) { |
682 uint32_t triangles = SkTMin(trianglesToDraw, maxTrianglesPerFlush); | 357 uint32_t triangles = SkTMin(trianglesToDraw, maxTrianglesPerFlush); |
683 GR_GL_CALL(gl, DrawArrays(GR_GL_TRIANGLES, 0, kVerticesPerTri * tria
ngles)); | 358 GR_GL_CALL(gl, DrawArrays(GR_GL_TRIANGLES, 0, kVerticesPerTri * tria
ngles)); |
684 trianglesToDraw -= triangles; | 359 trianglesToDraw -= triangles; |
685 } | 360 } |
686 } | 361 } |
687 | 362 |
688 #ifdef DUMP_IMAGES | 363 #if 0 |
689 //const char* filename = "/data/local/tmp/out.png"; | 364 //const char* filename = "/data/local/tmp/out.png"; |
690 SkString filename("out"); | 365 SkString filename("out"); |
691 filename.appendf("_%s.png", this->getName()); | 366 filename.appendf("_%s.png", this->getName()); |
692 dump_image(gl, kScreenWidth, kScreenHeight, filename.c_str()); | 367 DumpImage(gl, kScreenWidth, kScreenHeight, filename.c_str()); |
693 #endif | 368 #endif |
694 } | 369 } |
695 | 370 |
696 void GLCpuPosInstancedArraysBench::teardown(const GrGLInterface* gl) { | 371 void GLCpuPosInstancedArraysBench::teardown(const GrGLInterface* gl) { |
| 372 GR_GL_CALL(gl, BindBuffer(GR_GL_ARRAY_BUFFER, 0)); |
| 373 GR_GL_CALL(gl, BindVertexArray(0)); |
| 374 GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0)); |
| 375 GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, 0)); |
| 376 GR_GL_CALL(gl, DeleteTextures(1, &fTexture)); |
697 GR_GL_CALL(gl, DeleteProgram(fProgram)); | 377 GR_GL_CALL(gl, DeleteProgram(fProgram)); |
698 GR_GL_CALL(gl, DeleteBuffers(fBuffers.count(), fBuffers.begin())); | 378 GR_GL_CALL(gl, DeleteBuffers(fBuffers.count(), fBuffers.begin())); |
699 GR_GL_CALL(gl, DeleteVertexArrays(1, &fVAO)); | 379 GR_GL_CALL(gl, DeleteVertexArrays(1, &fVAO)); |
700 } | 380 } |
701 | 381 |
702 /////////////////////////////////////////////////////////////////////////////// | 382 /////////////////////////////////////////////////////////////////////////////// |
703 | 383 |
704 DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench:
:kUseInstance_VboSetup, 0) ) | 384 DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench:
:kUseInstance_VboSetup, 0) ) |
705 DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench:
:kUseOne_VboSetup, 0) ) | 385 DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench:
:kUseOne_VboSetup, 0) ) |
706 DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench:
:kUseTwo_VboSetup, 0) ) | 386 DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench:
:kUseTwo_VboSetup, 0) ) |
707 DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench:
:kUseOne_VboSetup, 1) ) | 387 DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench:
:kUseOne_VboSetup, 1) ) |
708 DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench:
:kUseTwo_VboSetup, 1) ) | 388 DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench:
:kUseTwo_VboSetup, 1) ) |
709 DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench:
:kUseOne_VboSetup, 2) ) | 389 DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench:
:kUseOne_VboSetup, 2) ) |
710 DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench:
:kUseTwo_VboSetup, 2) ) | 390 DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench:
:kUseTwo_VboSetup, 2) ) |
711 DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench:
:kUseOne_VboSetup, 4) ) | 391 DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench:
:kUseOne_VboSetup, 4) ) |
712 DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench:
:kUseTwo_VboSetup, 4) ) | 392 DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench:
:kUseTwo_VboSetup, 4) ) |
713 DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench:
:kUseOne_VboSetup, 8) ) | 393 DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench:
:kUseOne_VboSetup, 8) ) |
714 DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench:
:kUseTwo_VboSetup, 8) ) | 394 DEF_BENCH( return new GLCpuPosInstancedArraysBench(GLCpuPosInstancedArraysBench:
:kUseTwo_VboSetup, 8) ) |
715 | 395 |
716 #endif | 396 #endif |
OLD | NEW |