| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "GrGLShaderStringBuilder.h" | 8 #include "GrGLShaderStringBuilder.h" |
| 9 #include "../GrGpuGL.h" | 9 #include "../GrGpuGL.h" |
| 10 #include "gl/GrGLSLPrettyPrint.h" | 10 #include "gl/GrGLSLPrettyPrint.h" |
| 11 #include "SkRTConf.h" | 11 #include "SkRTConf.h" |
| 12 #include "SkTraceEvent.h" | 12 #include "SkTraceEvent.h" |
| 13 | 13 |
| 14 #define GL_CALL(X) GR_GL_CALL(gpu->glInterface(), X) | 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) | 15 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(gpu->glInterface(), R, X) |
| 16 | 16 |
| 17 SK_CONF_DECLARE(bool, c_PrintShaders, "gpu.printShaders", false, | 17 SK_CONF_DECLARE(bool, c_PrintShaders, "gpu.printShaders", false, |
| 18 "Print the source code for all shaders generated."); | 18 "Print the source code for all shaders generated."); |
| 19 | 19 |
| 20 GrGLuint GrGLCompileAndAttachShader(const GrGLContext& glCtx, | 20 GrGLuint GrGLCompileAndAttachShader(const GrGLContext& glCtx, |
| 21 GrGLuint programId, | 21 GrGLuint programId, |
| 22 GrGLenum type, | 22 GrGLenum type, |
| 23 const SkString& shaderSrc) { | 23 const SkString& shaderSrc, |
| 24 GrContext::GPUStats* gpuStats) { |
| 24 const GrGLInterface* gli = glCtx.interface(); | 25 const GrGLInterface* gli = glCtx.interface(); |
| 25 | 26 |
| 26 GrGLuint shaderId; | 27 GrGLuint shaderId; |
| 27 GR_GL_CALL_RET(gli, shaderId, CreateShader(type)); | 28 GR_GL_CALL_RET(gli, shaderId, CreateShader(type)); |
| 28 if (0 == shaderId) { | 29 if (0 == shaderId) { |
| 29 return 0; | 30 return 0; |
| 30 } | 31 } |
| 31 | 32 |
| 32 #ifdef SK_DEBUG | 33 #ifdef SK_DEBUG |
| 33 SkString prettySource = GrGLSLPrettyPrint::PrettyPrintGLSL(shaderSrc, false
); | 34 SkString prettySource = GrGLSLPrettyPrint::PrettyPrintGLSL(shaderSrc, false)
; |
| 34 const GrGLchar* sourceStr = prettySource.c_str(); | 35 const GrGLchar* sourceStr = prettySource.c_str(); |
| 35 GrGLint sourceLength = static_cast<GrGLint>(prettySource.size()); | 36 GrGLint sourceLength = static_cast<GrGLint>(prettySource.size()); |
| 36 #else | 37 #else |
| 37 GrGLint sourceLength = static_cast<GrGLint>(shaderSrc.size()); | 38 GrGLint sourceLength = static_cast<GrGLint>(shaderSrc.size()); |
| 38 const GrGLchar* sourceStr = shaderSrc.c_str(); | 39 const GrGLchar* sourceStr = shaderSrc.c_str(); |
| 39 #endif | 40 #endif |
| 40 GR_GL_CALL(gli, ShaderSource(shaderId, 1, &sourceStr, &sourceLength)); | 41 GR_GL_CALL(gli, ShaderSource(shaderId, 1, &sourceStr, &sourceLength)); |
| 41 GR_GL_CALL(gli, CompileShader(shaderId)); | 42 gpuStats->incShaderCompilations(); |
| 43 GR_GL_CALL(gli, CompileShader(shaderId)); |
| 42 | 44 |
| 43 // Calling GetShaderiv in Chromium is quite expensive. Assume success in re
lease builds. | 45 // Calling GetShaderiv in Chromium is quite expensive. Assume success in rel
ease builds. |
| 44 bool checkCompiled = !glCtx.isChromium(); | 46 bool checkCompiled = !glCtx.isChromium(); |
| 45 #ifdef SK_DEBUG | 47 #ifdef SK_DEBUG |
| 46 checkCompiled = true; | 48 checkCompiled = true; |
| 47 #endif | 49 #endif |
| 48 if (checkCompiled) { | 50 if (checkCompiled) { |
| 49 GrGLint compiled = GR_GL_INIT_ZERO; | 51 GrGLint compiled = GR_GL_INIT_ZERO; |
| 50 GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_COMPILE_STATUS, &compiled))
; | 52 GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_COMPILE_STATUS, &compiled)); |
| 51 | 53 |
| 52 if (!compiled) { | 54 if (!compiled) { |
| 53 GrGLint infoLen = GR_GL_INIT_ZERO; | 55 GrGLint infoLen = GR_GL_INIT_ZERO; |
| 54 GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_INFO_LOG_LENGTH, &infoL
en)); | 56 GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_INFO_LOG_LENGTH, &infoLe
n)); |
| 55 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debug
ger | 57 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugg
er |
| 56 if (infoLen > 0) { | 58 if (infoLen > 0) { |
| 57 // retrieve length even though we don't need it to workaround b
ug in Chromium cmd | 59 // retrieve length even though we don't need it to workaround bu
g in Chromium cmd |
| 58 // buffer param validation. | 60 // buffer param validation. |
| 59 GrGLsizei length = GR_GL_INIT_ZERO; | 61 GrGLsizei length = GR_GL_INIT_ZERO; |
| 60 GR_GL_CALL(gli, GetShaderInfoLog(shaderId, infoLen+1, | 62 GR_GL_CALL(gli, GetShaderInfoLog(shaderId, infoLen+1, |
| 61 &length, (char*)log.get())); | 63 &length, (char*)log.get())); |
| 62 GrPrintf(GrGLSLPrettyPrint::PrettyPrintGLSL(shaderSrc, true).c_
str()); | 64 GrPrintf(GrGLSLPrettyPrint::PrettyPrintGLSL(shaderSrc, true).c_s
tr()); |
| 63 GrPrintf("\n%s", log.get()); | 65 GrPrintf("\n%s", log.get()); |
| 64 } | 66 } |
| 65 SkDEBUGFAIL("Shader compilation failed!"); | 67 SkDEBUGFAIL("Shader compilation failed!"); |
| 66 GR_GL_CALL(gli, DeleteShader(shaderId)); | 68 GR_GL_CALL(gli, DeleteShader(shaderId)); |
| 67 return 0; | 69 return 0; |
| 68 } | 70 } |
| 69 } | 71 } |
| 70 | 72 |
| 71 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "skia_gpu::GLSh
ader", | 73 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "skia_gpu::GLSha
der", |
| 72 TRACE_EVENT_SCOPE_THREAD, "shader", TRACE_STR_COPY(sha
derSrc.c_str())); | 74 TRACE_EVENT_SCOPE_THREAD, "shader", TRACE_STR_COPY(shad
erSrc.c_str())); |
| 73 if (c_PrintShaders) { | 75 if (c_PrintShaders) { |
| 74 GrPrintf(GrGLSLPrettyPrint::PrettyPrintGLSL(shaderSrc, true).c_str()); | 76 GrPrintf(GrGLSLPrettyPrint::PrettyPrintGLSL(shaderSrc, true).c_str()); |
| 75 GrPrintf("\n"); | 77 GrPrintf("\n"); |
| 76 } | 78 } |
| 77 | 79 |
| 78 // Attach the shader, but defer deletion until after we have linked the pro
gram. | 80 // Attach the shader, but defer deletion until after we have linked the prog
ram. |
| 79 // This works around a bug in the Android emulator's GLES2 wrapper which | 81 // This works around a bug in the Android emulator's GLES2 wrapper which |
| 80 // will immediately delete the shader object and free its memory even thoug
h it's | 82 // will immediately delete the shader object and free its memory even though
it's |
| 81 // attached to a program, which then causes glLinkProgram to fail. | 83 // attached to a program, which then causes glLinkProgram to fail. |
| 82 GR_GL_CALL(gli, AttachShader(programId, shaderId)); | 84 GR_GL_CALL(gli, AttachShader(programId, shaderId)); |
| 83 | 85 |
| 84 return shaderId; | 86 return shaderId; |
| 85 } | 87 } |
| OLD | NEW |