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