joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 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 "GLBench.h" |
| 9 | |
| 10 | #if SK_SUPPORT_GPU |
robertphillips | 87f15c8 | 2016-05-20 11:14:33 -0700 | [diff] [blame] | 11 | #include "GrGpu.h" |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 12 | #include "GrTest.h" |
robertphillips | 87f15c8 | 2016-05-20 11:14:33 -0700 | [diff] [blame] | 13 | #include "gl/GrGLContext.h" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 14 | #include "gl/builders/GrGLShaderStringBuilder.h" |
| 15 | #include "SkSLCompiler.h" |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 16 | #include <stdio.h> |
| 17 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 18 | #include "sk_tool_utils.h" |
| 19 | |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 20 | const GrGLContext* GLBench::getGLContext(SkCanvas* canvas) { |
| 21 | // This bench exclusively tests GL calls directly |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 22 | if (nullptr == canvas->getGrContext()) { |
| 23 | return nullptr; |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 24 | } |
| 25 | GrContext* context = canvas->getGrContext(); |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 26 | GrGpu* gpu = context->getGpu(); |
| 27 | if (!gpu) { |
| 28 | SkDebugf("Couldn't get Gr gpu."); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 29 | return nullptr; |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 30 | } |
| 31 | |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 32 | const GrGLContext* ctx = gpu->glContextForTesting(); |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 33 | if (!ctx) { |
| 34 | SkDebugf("Couldn't get an interface\n"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 35 | return nullptr; |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | return this->onGetGLContext(ctx); |
| 39 | } |
| 40 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 41 | void GLBench::onPreDraw(SkCanvas* canvas) { |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 42 | // This bench exclusively tests GL calls directly |
| 43 | const GrGLContext* ctx = this->getGLContext(canvas); |
| 44 | if (!ctx) { |
| 45 | return; |
| 46 | } |
| 47 | this->setup(ctx); |
| 48 | } |
| 49 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 50 | void GLBench::onPostDraw(SkCanvas* canvas) { |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 51 | // This bench exclusively tests GL calls directly |
| 52 | const GrGLContext* ctx = this->getGLContext(canvas); |
| 53 | if (!ctx) { |
| 54 | return; |
| 55 | } |
| 56 | this->teardown(ctx->interface()); |
| 57 | } |
| 58 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 59 | void GLBench::onDraw(int loops, SkCanvas* canvas) { |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 60 | const GrGLContext* ctx = this->getGLContext(canvas); |
| 61 | if (!ctx) { |
| 62 | return; |
| 63 | } |
| 64 | this->glDraw(loops, ctx); |
brianosman | 393c2ff | 2016-05-11 08:49:59 -0700 | [diff] [blame] | 65 | canvas->getGrContext()->resetContext(); |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 66 | } |
| 67 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 68 | GrGLuint GLBench::CompileShader(const GrGLContext* context, const char* sksl, GrGLenum type) { |
| 69 | const GrGLInterface* gl = context->interface(); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 70 | SkSL::String glsl; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 71 | SkSL::Program::Settings settings; |
| 72 | settings.fCaps = context->caps()->shaderCaps(); |
| 73 | std::unique_ptr<SkSL::Program> program = context->compiler()->convertProgram( |
| 74 | type == GR_GL_VERTEX_SHADER ? SkSL::Program::kVertex_Kind |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 75 | : SkSL::Program::kFragment_Kind, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 76 | SkString(sksl), |
| 77 | settings); |
| 78 | if (!program || !context->compiler()->toGLSL(*program, &glsl)) { |
| 79 | SkDebugf("SkSL compilation failed:\n%s\n%s\n", sksl, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 80 | context->compiler()->errorText().c_str()); |
| 81 | } |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 82 | GrGLuint shader; |
| 83 | // Create the shader object |
| 84 | GR_GL_CALL_RET(gl, shader, CreateShader(type)); |
| 85 | |
| 86 | // Load the shader source |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 87 | const char* glslPtr = glsl.c_str(); |
| 88 | GR_GL_CALL(gl, ShaderSource(shader, 1, (const char**) &glslPtr, nullptr)); |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 89 | |
| 90 | // Compile the shader |
| 91 | GR_GL_CALL(gl, CompileShader(shader)); |
| 92 | |
| 93 | // Check for compile time errors |
hendrikw | 894a2e4 | 2015-09-08 15:18:56 -0700 | [diff] [blame] | 94 | GrGLint success = GR_GL_INIT_ZERO; |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 95 | GrGLchar infoLog[512]; |
| 96 | GR_GL_CALL(gl, GetShaderiv(shader, GR_GL_COMPILE_STATUS, &success)); |
| 97 | if (!success) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 98 | GR_GL_CALL(gl, GetShaderInfoLog(shader, 512, nullptr, infoLog)); |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 99 | SkDebugf("ERROR::SHADER::COMPLIATION_FAILED: %s\n", infoLog); |
| 100 | } |
| 101 | |
| 102 | return shader; |
| 103 | } |
| 104 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 105 | GrGLuint GLBench::CreateProgram(const GrGLContext* context, const char* vshader, |
| 106 | const char* fshader) { |
| 107 | const GrGLInterface* gl = context->interface(); |
| 108 | GrGLuint vertexShader = CompileShader(context, vshader, GR_GL_VERTEX_SHADER); |
| 109 | GrGLuint fragmentShader = CompileShader(context, fshader, GR_GL_FRAGMENT_SHADER); |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 110 | |
| 111 | GrGLuint shaderProgram; |
| 112 | GR_GL_CALL_RET(gl, shaderProgram, CreateProgram()); |
| 113 | GR_GL_CALL(gl, AttachShader(shaderProgram, vertexShader)); |
| 114 | GR_GL_CALL(gl, AttachShader(shaderProgram, fragmentShader)); |
| 115 | GR_GL_CALL(gl, LinkProgram(shaderProgram)); |
| 116 | |
| 117 | // Check for linking errors |
hendrikw | 894a2e4 | 2015-09-08 15:18:56 -0700 | [diff] [blame] | 118 | GrGLint success = GR_GL_INIT_ZERO; |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 119 | GrGLchar infoLog[512]; |
| 120 | GR_GL_CALL(gl, GetProgramiv(shaderProgram, GR_GL_LINK_STATUS, &success)); |
| 121 | if (!success) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 122 | GR_GL_CALL(gl, GetProgramInfoLog(shaderProgram, 512, nullptr, infoLog)); |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 123 | SkDebugf("Linker Error: %s\n", infoLog); |
| 124 | } |
| 125 | GR_GL_CALL(gl, DeleteShader(vertexShader)); |
| 126 | GR_GL_CALL(gl, DeleteShader(fragmentShader)); |
| 127 | |
| 128 | return shaderProgram; |
| 129 | } |
| 130 | |
| 131 | GrGLuint GLBench::SetupFramebuffer(const GrGLInterface* gl, int screenWidth, int screenHeight) { |
| 132 | //Setup framebuffer |
| 133 | GrGLuint texture; |
| 134 | GR_GL_CALL(gl, GenTextures(1, &texture)); |
joshualitt | b235168 | 2015-10-02 06:49:23 -0700 | [diff] [blame] | 135 | GR_GL_CALL(gl, ActiveTexture(GR_GL_TEXTURE7)); |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 136 | GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, texture)); |
| 137 | GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MAG_FILTER, GR_GL_NEAREST)); |
| 138 | GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_MIN_FILTER, GR_GL_NEAREST)); |
| 139 | GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_WRAP_S, GR_GL_CLAMP_TO_EDGE)); |
| 140 | GR_GL_CALL(gl, TexParameteri(GR_GL_TEXTURE_2D, GR_GL_TEXTURE_WRAP_T, GR_GL_CLAMP_TO_EDGE)); |
| 141 | GR_GL_CALL(gl, TexImage2D(GR_GL_TEXTURE_2D, |
| 142 | 0, //level |
| 143 | GR_GL_RGBA, //internal format |
| 144 | screenWidth, // width |
| 145 | screenHeight, // height |
| 146 | 0, //border |
| 147 | GR_GL_RGBA, //format |
| 148 | GR_GL_UNSIGNED_BYTE, // type |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 149 | nullptr)); |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 150 | |
| 151 | // bind framebuffer |
| 152 | GrGLuint framebuffer; |
| 153 | GR_GL_CALL(gl, BindTexture(GR_GL_TEXTURE_2D, 0)); |
| 154 | GR_GL_CALL(gl, GenFramebuffers(1, &framebuffer)); |
| 155 | GR_GL_CALL(gl, BindFramebuffer(GR_GL_FRAMEBUFFER, framebuffer)); |
| 156 | GR_GL_CALL(gl, FramebufferTexture2D(GR_GL_FRAMEBUFFER, |
| 157 | GR_GL_COLOR_ATTACHMENT0, |
| 158 | GR_GL_TEXTURE_2D, |
| 159 | texture, 0)); |
| 160 | GR_GL_CALL(gl, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 161 | GR_GL_CALL(gl, Viewport(0, 0, screenWidth, screenHeight)); |
| 162 | return texture; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | void GLBench::DumpImage(const GrGLInterface* gl, uint32_t screenWidth, uint32_t screenHeight, |
| 167 | const char* filename) { |
| 168 | // read back pixels |
| 169 | SkAutoTArray<uint32_t> readback(screenWidth * screenHeight); |
| 170 | GR_GL_CALL(gl, ReadPixels(0, // x |
| 171 | 0, // y |
| 172 | screenWidth, // width |
| 173 | screenHeight, // height |
| 174 | GR_GL_RGBA, //format |
| 175 | GR_GL_UNSIGNED_BYTE, //type |
| 176 | readback.get())); |
| 177 | |
| 178 | // dump png |
| 179 | SkBitmap bm; |
| 180 | if (!bm.tryAllocPixels(SkImageInfo::MakeN32Premul(screenWidth, screenHeight))) { |
| 181 | SkDebugf("couldn't allocate bitmap\n"); |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | bm.setPixels(readback.get()); |
| 186 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 187 | if (!sk_tool_utils::EncodeImageToFile(filename, bm, SkEncodedImageFormat::kPNG, 100)) { |
joshualitt | 74a07db | 2015-07-01 12:39:07 -0700 | [diff] [blame] | 188 | SkDebugf("------ failed to encode %s\n", filename); |
| 189 | remove(filename); // remove any partial file |
| 190 | return; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | #endif |