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 "gl/GrGLGpu.h" | 9 #include "gl/GrGLGpu.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 char** strings, |
| 24 int* lengths, |
| 25 int count, |
24 GrGpu::Stats* stats) { | 26 GrGpu::Stats* stats) { |
25 const GrGLInterface* gli = glCtx.interface(); | 27 const GrGLInterface* gli = glCtx.interface(); |
26 | 28 |
27 GrGLuint shaderId; | 29 GrGLuint shaderId; |
28 GR_GL_CALL_RET(gli, shaderId, CreateShader(type)); | 30 GR_GL_CALL_RET(gli, shaderId, CreateShader(type)); |
29 if (0 == shaderId) { | 31 if (0 == shaderId) { |
30 return 0; | 32 return 0; |
31 } | 33 } |
32 | 34 |
33 #ifdef SK_DEBUG | 35 #ifdef SK_DEBUG |
34 SkString prettySource = GrGLSLPrettyPrint::PrettyPrintGLSL(shaderSrc, false)
; | 36 SkString prettySource = GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths,
count, false); |
35 const GrGLchar* sourceStr = prettySource.c_str(); | 37 const GrGLchar* sourceStr = prettySource.c_str(); |
36 GrGLint sourceLength = static_cast<GrGLint>(prettySource.size()); | 38 GrGLint sourceLength = static_cast<GrGLint>(prettySource.size()); |
| 39 GR_GL_CALL(gli, ShaderSource(shaderId, 1, &sourceStr, &sourceLength)); |
37 #else | 40 #else |
38 GrGLint sourceLength = static_cast<GrGLint>(shaderSrc.size()); | 41 GR_GL_CALL(gli, ShaderSource(shaderId, count, strings, lengths)); |
39 const GrGLchar* sourceStr = shaderSrc.c_str(); | |
40 #endif | 42 #endif |
41 GR_GL_CALL(gli, ShaderSource(shaderId, 1, &sourceStr, &sourceLength)); | 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"), &t
raceShader); |
| 47 if (traceShader) { |
| 48 SkString shader = GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths, c
ount, false); |
| 49 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "skia_gpu::G
LShader", |
| 50 TRACE_EVENT_SCOPE_THREAD, "shader", TRACE_STR_COPY(
shader.c_str())); |
| 51 } |
| 52 |
42 stats->incShaderCompilations(); | 53 stats->incShaderCompilations(); |
43 GR_GL_CALL(gli, CompileShader(shaderId)); | 54 GR_GL_CALL(gli, CompileShader(shaderId)); |
44 | 55 |
45 // Calling GetShaderiv in Chromium is quite expensive. Assume success in rel
ease builds. | 56 // Calling GetShaderiv in Chromium is quite expensive. Assume success in rel
ease builds. |
46 bool checkCompiled = !glCtx.isChromium(); | 57 bool checkCompiled = !glCtx.isChromium(); |
47 #ifdef SK_DEBUG | 58 #ifdef SK_DEBUG |
48 checkCompiled = true; | 59 checkCompiled = true; |
49 #endif | 60 #endif |
50 if (checkCompiled) { | 61 if (checkCompiled) { |
51 GrGLint compiled = GR_GL_INIT_ZERO; | 62 GrGLint compiled = GR_GL_INIT_ZERO; |
52 GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_COMPILE_STATUS, &compiled)); | 63 GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_COMPILE_STATUS, &compiled)); |
53 | 64 |
54 if (!compiled) { | 65 if (!compiled) { |
55 GrGLint infoLen = GR_GL_INIT_ZERO; | 66 GrGLint infoLen = GR_GL_INIT_ZERO; |
56 GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_INFO_LOG_LENGTH, &infoLe
n)); | 67 GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_INFO_LOG_LENGTH, &infoLe
n)); |
57 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugg
er | 68 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugg
er |
58 if (infoLen > 0) { | 69 if (infoLen > 0) { |
59 // retrieve length even though we don't need it to workaround bu
g in Chromium cmd | 70 // retrieve length even though we don't need it to workaround bu
g in Chromium cmd |
60 // buffer param validation. | 71 // buffer param validation. |
61 GrGLsizei length = GR_GL_INIT_ZERO; | 72 GrGLsizei length = GR_GL_INIT_ZERO; |
62 GR_GL_CALL(gli, GetShaderInfoLog(shaderId, infoLen+1, | 73 GR_GL_CALL(gli, GetShaderInfoLog(shaderId, infoLen+1, &length, (
char*)log.get())); |
63 &length, (char*)log.get())); | 74 SkDebugf(GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths, co
unt, true).c_str()); |
64 SkDebugf(GrGLSLPrettyPrint::PrettyPrintGLSL(shaderSrc, true).c_s
tr()); | |
65 SkDebugf("\n%s", log.get()); | 75 SkDebugf("\n%s", log.get()); |
66 } | 76 } |
67 SkDEBUGFAIL("Shader compilation failed!"); | 77 SkDEBUGFAIL("Shader compilation failed!"); |
68 GR_GL_CALL(gli, DeleteShader(shaderId)); | 78 GR_GL_CALL(gli, DeleteShader(shaderId)); |
69 return 0; | 79 return 0; |
70 } | 80 } |
71 } | 81 } |
72 | 82 |
73 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "skia_gpu::GLSha
der", | |
74 TRACE_EVENT_SCOPE_THREAD, "shader", TRACE_STR_COPY(shad
erSrc.c_str())); | |
75 if (c_PrintShaders) { | 83 if (c_PrintShaders) { |
76 SkDebugf(GrGLSLPrettyPrint::PrettyPrintGLSL(shaderSrc, true).c_str()); | 84 SkDebugf(GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths, count, tru
e).c_str()); |
77 SkDebugf("\n"); | 85 SkDebugf("\n"); |
78 } | 86 } |
79 | 87 |
80 // Attach the shader, but defer deletion until after we have linked the prog
ram. | 88 // Attach the shader, but defer deletion until after we have linked the prog
ram. |
81 // This works around a bug in the Android emulator's GLES2 wrapper which | 89 // This works around a bug in the Android emulator's GLES2 wrapper which |
82 // will immediately delete the shader object and free its memory even though
it's | 90 // will immediately delete the shader object and free its memory even though
it's |
83 // attached to a program, which then causes glLinkProgram to fail. | 91 // attached to a program, which then causes glLinkProgram to fail. |
84 GR_GL_CALL(gli, AttachShader(programId, shaderId)); | 92 GR_GL_CALL(gli, AttachShader(programId, shaderId)); |
85 | 93 |
86 return shaderId; | 94 return shaderId; |
87 } | 95 } |
OLD | NEW |