Home | History | Annotate | Download | only in builders
      1 /*
      2  * Copyright 2014 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 "GrGLShaderStringBuilder.h"
      9 #include "gl/GrGLGpu.h"
     10 #include "gl/GrGLSLPrettyPrint.h"
     11 #include "SkRTConf.h"
     12 #include "SkTraceEvent.h"
     13 
     14 #define GL_CALL(X) GR_GL_CALL(gpu->glInterface(), X)
     15 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(gpu->glInterface(), R, X)
     16 
     17 SK_CONF_DECLARE(bool, c_PrintShaders, "gpu.printShaders", false,
     18                 "Print the source code for all shaders generated.");
     19 
     20 GrGLuint GrGLCompileAndAttachShader(const GrGLContext& glCtx,
     21                                     GrGLuint programId,
     22                                     GrGLenum type,
     23                                     const char** strings,
     24                                     int* lengths,
     25                                     int count,
     26                                     GrGpu::Stats* stats) {
     27     const GrGLInterface* gli = glCtx.interface();
     28 
     29     GrGLuint shaderId;
     30     GR_GL_CALL_RET(gli, shaderId, CreateShader(type));
     31     if (0 == shaderId) {
     32         return 0;
     33     }
     34 
     35 #ifdef SK_DEBUG
     36     SkString prettySource = GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths, count, false);
     37     const GrGLchar* sourceStr = prettySource.c_str();
     38     GrGLint sourceLength = static_cast<GrGLint>(prettySource.size());
     39     GR_GL_CALL(gli, ShaderSource(shaderId, 1, &sourceStr, &sourceLength));
     40 #else
     41     GR_GL_CALL(gli, ShaderSource(shaderId, count, strings, lengths));
     42 #endif
     43 
     44     // If tracing is enabled in chrome then we pretty print
     45     bool traceShader;
     46     TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), &traceShader);
     47     if (traceShader) {
     48         SkString shader = GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths, count, false);
     49         TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "skia_gpu::GLShader",
     50                              TRACE_EVENT_SCOPE_THREAD, "shader", TRACE_STR_COPY(shader.c_str()));
     51     }
     52 
     53     stats->incShaderCompilations();
     54     GR_GL_CALL(gli, CompileShader(shaderId));
     55 
     56     // Calling GetShaderiv in Chromium is quite expensive. Assume success in release builds.
     57     bool checkCompiled = !glCtx.isChromium();
     58 #ifdef SK_DEBUG
     59     checkCompiled = true;
     60 #endif
     61     if (checkCompiled) {
     62         GrGLint compiled = GR_GL_INIT_ZERO;
     63         GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_COMPILE_STATUS, &compiled));
     64 
     65         if (!compiled) {
     66             GrGLint infoLen = GR_GL_INIT_ZERO;
     67             GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_INFO_LOG_LENGTH, &infoLen));
     68             SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
     69             if (infoLen > 0) {
     70                 // retrieve length even though we don't need it to workaround bug in Chromium cmd
     71                 // buffer param validation.
     72                 GrGLsizei length = GR_GL_INIT_ZERO;
     73                 GR_GL_CALL(gli, GetShaderInfoLog(shaderId, infoLen+1, &length, (char*)log.get()));
     74                 SkDebugf("%s", GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths, count, true).c_str());
     75                 SkDebugf("\n%s", log.get());
     76             }
     77             SkDEBUGFAIL("Shader compilation failed!");
     78             GR_GL_CALL(gli, DeleteShader(shaderId));
     79             return 0;
     80         }
     81     }
     82 
     83     if (c_PrintShaders) {
     84         SkDebugf("%s", GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths, count, true).c_str());
     85         SkDebugf("\n");
     86     }
     87 
     88     // Attach the shader, but defer deletion until after we have linked the program.
     89     // This works around a bug in the Android emulator's GLES2 wrapper which
     90     // will immediately delete the shader object and free its memory even though it's
     91     // attached to a program, which then causes glLinkProgram to fail.
     92     GR_GL_CALL(gli, AttachShader(programId, shaderId));
     93 
     94     return shaderId;
     95 }
     96