• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/core/SkAutoMalloc.h"
9 #include "src/core/SkTraceEvent.h"
10 #include "src/gpu/GrShaderUtils.h"
11 #include "src/gpu/gl/GrGLGpu.h"
12 #include "src/gpu/gl/builders/GrGLShaderStringBuilder.h"
13 #include "src/sksl/SkSLCompiler.h"
14 #include "src/sksl/codegen/SkSLGLSLCodeGenerator.h"
15 #include "src/sksl/ir/SkSLProgram.h"
16 
17 // Print the source code for all shaders generated.
18 static const bool gPrintSKSL = false;
19 static const bool gPrintGLSL = false;
20 
GrSkSLtoGLSL(const GrGLGpu * gpu,SkSL::ProgramKind programKind,const SkSL::String & sksl,const SkSL::Program::Settings & settings,SkSL::String * glsl,GrContextOptions::ShaderErrorHandler * errorHandler)21 std::unique_ptr<SkSL::Program> GrSkSLtoGLSL(const GrGLGpu* gpu,
22                                             SkSL::ProgramKind programKind,
23                                             const SkSL::String& sksl,
24                                             const SkSL::Program::Settings& settings,
25                                             SkSL::String* glsl,
26                                             GrContextOptions::ShaderErrorHandler* errorHandler) {
27     SkSL::Compiler* compiler = gpu->shaderCompiler();
28     std::unique_ptr<SkSL::Program> program;
29 #ifdef SK_DEBUG
30     SkSL::String src = GrShaderUtils::PrettyPrint(sksl);
31 #else
32     const SkSL::String& src = sksl;
33 #endif
34     program = compiler->convertProgram(programKind, src, settings);
35     if (!program || !compiler->toGLSL(*program, glsl)) {
36         errorHandler->compileError(src.c_str(), compiler->errorText().c_str());
37         return nullptr;
38     }
39 
40     if (gPrintSKSL || gPrintGLSL) {
41         GrShaderUtils::PrintShaderBanner(programKind);
42         if (gPrintSKSL) {
43             SkDebugf("SKSL:\n");
44             GrShaderUtils::PrintLineByLine(GrShaderUtils::PrettyPrint(sksl));
45         }
46         if (gPrintGLSL) {
47             SkDebugf("GLSL:\n");
48             GrShaderUtils::PrintLineByLine(GrShaderUtils::PrettyPrint(*glsl));
49         }
50     }
51 
52     return program;
53 }
54 
GrGLCompileAndAttachShader(const GrGLContext & glCtx,GrGLuint programId,GrGLenum type,const SkSL::String & glsl,GrThreadSafePipelineBuilder::Stats * stats,GrContextOptions::ShaderErrorHandler * errorHandler)55 GrGLuint GrGLCompileAndAttachShader(const GrGLContext& glCtx,
56                                     GrGLuint programId,
57                                     GrGLenum type,
58                                     const SkSL::String& glsl,
59                                     GrThreadSafePipelineBuilder::Stats* stats,
60                                     GrContextOptions::ShaderErrorHandler* errorHandler) {
61     TRACE_EVENT0_ALWAYS("skia.shaders", "driver_compile_shader");
62     const GrGLInterface* gli = glCtx.glInterface();
63 
64     // Specify GLSL source to the driver.
65     GrGLuint shaderId;
66     GR_GL_CALL_RET(gli, shaderId, CreateShader(type));
67     if (0 == shaderId) {
68         return 0;
69     }
70     const GrGLchar* source = glsl.c_str();
71     GrGLint sourceLength = glsl.size();
72     GR_GL_CALL(gli, ShaderSource(shaderId, 1, &source, &sourceLength));
73 
74     stats->incShaderCompilations();
75     GR_GL_CALL(gli, CompileShader(shaderId));
76 
77     bool checkCompiled = !glCtx.caps()->skipErrorChecks();
78 
79     if (checkCompiled) {
80         ATRACE_ANDROID_FRAMEWORK("checkCompiled");
81         GrGLint compiled = GR_GL_INIT_ZERO;
82         GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_COMPILE_STATUS, &compiled));
83 
84         if (!compiled) {
85             GrGLint infoLen = GR_GL_INIT_ZERO;
86             GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_INFO_LOG_LENGTH, &infoLen));
87             SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
88             if (infoLen > 0) {
89                 // retrieve length even though we don't need it to workaround bug in Chromium cmd
90                 // buffer param validation.
91                 GrGLsizei length = GR_GL_INIT_ZERO;
92                 GR_GL_CALL(gli, GetShaderInfoLog(shaderId, infoLen+1, &length, (char*)log.get()));
93             }
94             errorHandler->compileError(glsl.c_str(), infoLen > 0 ? (const char*)log.get() : "");
95             GR_GL_CALL(gli, DeleteShader(shaderId));
96             return 0;
97         }
98     }
99 
100     // Attach the shader, but defer deletion until after we have linked the program.
101     // This works around a bug in the Android emulator's GLES2 wrapper which
102     // will immediately delete the shader object and free its memory even though it's
103     // attached to a program, which then causes glLinkProgram to fail.
104     GR_GL_CALL(gli, AttachShader(programId, shaderId));
105     return shaderId;
106 }
107