| 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 |
#include "src/gpu/ganesh/gl/builders/GrGLShaderStringBuilder.h" |
| 8 |
|
| 9 |
#include "include/gpu/ganesh/gl/GrGLFunctions.h" |
| 10 |
#include "include/gpu/ganesh/gl/GrGLInterface.h" |
| 11 |
#include "include/private/base/SkTo.h" |
| 12 |
#include "src/base/SkAutoMalloc.h" |
| 13 |
#include "src/core/SkTraceEvent.h" |
| 14 |
#include "src/gpu/ganesh/gl/GrGLDefines.h" |
| 15 |
#include "src/gpu/ganesh/gl/GrGLGpu.h" |
| 16 |
#include "src/gpu/ganesh/gl/GrGLUtil.h" |
| 17 |
#include "src/sksl/SkSLString.h" |
| 18 |
#include "src/sksl/codegen/SkSLNativeShader.h" |
| 19 |
|
| 20 |
#include <cstdint> |
| 21 |
|
| 22 |
GrGLuint GrGLCompileAndAttachShader(const GrGLContext& glCtx, |
| 23 |
GrGLuint programId, |
| 24 |
GrGLenum type, |
| 25 |
const SkSL::NativeShader& glsl, |
| 26 |
bool shaderWasCached, |
| 27 |
GrThreadSafePipelineBuilder::Stats* stats, |
| 28 |
GrContextOptions::ShaderErrorHandler* errorHandler) { |
| 29 |
TRACE_EVENT0_ALWAYS("skia.shaders", "driver_compile_shader"); |
| 30 |
const GrGLInterface* gli = glCtx.glInterface(); |
| 31 |
|
| 32 |
// Specify GLSL source to the driver. |
| 33 |
GrGLuint shaderId; |
| 34 |
GR_GL_CALL_RET(gli, shaderId, CreateShader(type)); |
| 35 |
if (0 == shaderId) { |
| 36 |
return 0; |
| 37 |
} |
| 38 |
const GrGLchar* source = glsl.fText.c_str(); |
| 39 |
GrGLint sourceLength = SkToInt(glsl.fText.size()); |
| 40 |
GR_GL_CALL(gli, ShaderSource(shaderId, 1, &source, &sourceLength)); |
| 41 |
|
| 42 |
stats->incShaderCompilations(); |
| 43 |
GR_GL_CALL(gli, CompileShader(shaderId)); |
| 44 |
|
| 45 |
{ |
| 46 |
ATRACE_ANDROID_FRAMEWORK("checkCompiled"); |
| 47 |
GrGLint compiled = GR_GL_INIT_ZERO; |
| 48 |
GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_COMPILE_STATUS, &compiled)); |
| 49 |
|
| 50 |
if (!compiled) { |
| 51 |
GrGLint infoLen = GR_GL_INIT_ZERO; |
| 52 |
GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
| 53 |
SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
| 54 |
if (infoLen > 0) { |
| 55 |
// retrieve length even though we don't need it to workaround bug in Chromium cmd |
| 56 |
// buffer param validation. |
| 57 |
GrGLsizei length = GR_GL_INIT_ZERO; |
| 58 |
GR_GL_CALL(gli, GetShaderInfoLog(shaderId, infoLen+1, &length, (char*)log.get())); |
| 59 |
} |
| 60 |
errorHandler->compileError( |
| 61 |
glsl.fText.c_str(), infoLen > 0 ? (const char*)log.get() : "", shaderWasCached); |
| 62 |
GR_GL_CALL(gli, DeleteShader(shaderId)); |
| 63 |
return 0; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
// Attach the shader, but defer deletion until after we have linked the program. |
| 68 |
// This works around a bug in the Android emulator's GLES2 wrapper which |
| 69 |
// will immediately delete the shader object and free its memory even though it's |
| 70 |
// attached to a program, which then causes glLinkProgram to fail. |
| 71 |
GR_GL_CALL(gli, AttachShader(programId, shaderId)); |
| 72 |
return shaderId; |
| 73 |
} |
| 74 |
|
| 75 |
bool GrGLCheckLinkStatus(const GrGLGpu* gpu, |
| 76 |
GrGLuint programID, |
| 77 |
bool shaderWasCached, |
| 78 |
GrContextOptions::ShaderErrorHandler* errorHandler, |
| 79 |
const std::string* sksl[kGrShaderTypeCount], |
| 80 |
const SkSL::NativeShader glsl[kGrShaderTypeCount]) { |
| 81 |
const GrGLInterface* gli = gpu->glInterface(); |
| 82 |
|
| 83 |
GrGLint linked = GR_GL_INIT_ZERO; |
| 84 |
GR_GL_CALL(gli, GetProgramiv(programID, GR_GL_LINK_STATUS, &linked)); |
| 85 |
if (!linked && errorHandler) { |
| 86 |
std::string allShaders; |
| 87 |
#if defined(SK_DEBUG) |
| 88 |
#define SKSL_FORMAT "// Vertex SKSL\n%s\n// Fragment SKSL\n%s\n" |
| 89 |
#else |
| 90 |
#define SKSL_FORMAT "%s\n%s\n" |
| 91 |
#endif |
| 92 |
if (sksl) { |
| 93 |
SkSL::String::appendf(&allShaders, SKSL_FORMAT, |
| 94 |
sksl[kVertex_GrShaderType]->c_str(), |
| 95 |
sksl[kFragment_GrShaderType]->c_str()); |
| 96 |
} |
| 97 |
if (glsl) { |
| 98 |
SkSL::String::appendf(&allShaders, |
| 99 |
SKSL_FORMAT, |
| 100 |
glsl[kVertex_GrShaderType].fText.c_str(), |
| 101 |
glsl[kFragment_GrShaderType].fText.c_str()); |
| 102 |
} |
| 103 |
#undef SKSL_FORMAT |
| 104 |
GrGLint infoLen = GR_GL_INIT_ZERO; |
| 105 |
GR_GL_CALL(gli, GetProgramiv(programID, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
| 106 |
SkAutoMalloc log(infoLen+1); |
| 107 |
if (infoLen > 0) { |
| 108 |
// retrieve length even though we don't need it to workaround |
| 109 |
// bug in chrome cmd buffer param validation. |
| 110 |
GrGLsizei length = GR_GL_INIT_ZERO; |
| 111 |
GR_GL_CALL(gli, GetProgramInfoLog(programID, infoLen+1, &length, (char*)log.get())); |
| 112 |
} |
| 113 |
const char* errorMsg = (infoLen > 0) ? (const char*)log.get() |
| 114 |
: "link failed but did not provide an info log"; |
| 115 |
errorHandler->compileError(allShaders.c_str(), errorMsg, shaderWasCached); |
| 116 |
} |
| 117 |
return SkToBool(linked); |
| 118 |
} |