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 "SkTraceEvent.h" | 11 #include "SkTraceEvent.h" |
| 12 #include "SkSLCompiler.h" |
| 13 #include "ir/SkSLProgram.h" |
12 | 14 |
13 #define GL_CALL(X) GR_GL_CALL(gpu->glInterface(), X) | 15 #define GL_CALL(X) GR_GL_CALL(gpu->glInterface(), X) |
14 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(gpu->glInterface(), R, X) | 16 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(gpu->glInterface(), R, X) |
15 | 17 |
16 // Print the source code for all shaders generated. | 18 // Print the source code for all shaders generated. |
17 static const bool c_PrintShaders{false}; | 19 static const bool c_PrintShaders{false}; |
18 | 20 |
19 static void print_shader_source(const char** strings, int* lengths, int count); | 21 static void print_shader_source(const char** strings, int* lengths, int count); |
20 | 22 |
| 23 static SkSL::GLCaps skslcaps_for_context(const GrGLContext& context) { |
| 24 GrGLStandard standard = context.standard(); |
| 25 const GrGLCaps* caps = context.caps(); |
| 26 const GrGLSLCaps* glslCaps = caps->glslCaps(); |
| 27 SkSL::GLCaps result; |
| 28 switch (standard) { |
| 29 case kGL_GrGLStandard: |
| 30 result.fStandard = SkSL::GLCaps::kGL_Standard; |
| 31 break; |
| 32 case kGLES_GrGLStandard: |
| 33 result.fStandard = SkSL::GLCaps::kGLES_Standard; |
| 34 break; |
| 35 default: |
| 36 SkASSERT(false); |
| 37 result.fStandard = SkSL::GLCaps::kGL_Standard; |
| 38 } |
| 39 |
| 40 switch (glslCaps->generation()) { |
| 41 case k110_GrGLSLGeneration: |
| 42 if (kGLES_GrGLStandard == standard) { |
| 43 // ES2's shader language is based on GLSL 1.20 but is version 1.
00 of the ES |
| 44 // language |
| 45 result.fVersion = 100; |
| 46 } else { |
| 47 SkASSERT(kGL_GrGLStandard == standard); |
| 48 result.fVersion = 110; |
| 49 } |
| 50 break; |
| 51 case k130_GrGLSLGeneration: |
| 52 SkASSERT(kGL_GrGLStandard == standard); |
| 53 result.fVersion = 130; |
| 54 break; |
| 55 case k140_GrGLSLGeneration: |
| 56 SkASSERT(kGL_GrGLStandard == standard); |
| 57 result.fVersion = 140; |
| 58 break; |
| 59 case k150_GrGLSLGeneration: |
| 60 SkASSERT(kGL_GrGLStandard == standard); |
| 61 result.fVersion = 150; |
| 62 break; |
| 63 case k330_GrGLSLGeneration: |
| 64 if (kGLES_GrGLStandard == standard) { |
| 65 result.fVersion = 300; |
| 66 } else { |
| 67 SkASSERT(kGL_GrGLStandard == standard); |
| 68 result.fVersion = 330; |
| 69 } |
| 70 break; |
| 71 case k400_GrGLSLGeneration: |
| 72 SkASSERT(kGL_GrGLStandard == standard); |
| 73 result.fVersion = 400; |
| 74 break; |
| 75 case k310es_GrGLSLGeneration: |
| 76 SkASSERT(kGLES_GrGLStandard == standard); |
| 77 result.fVersion = 310; |
| 78 break; |
| 79 case k320es_GrGLSLGeneration: |
| 80 SkASSERT(kGLES_GrGLStandard == standard); |
| 81 result.fVersion = 320; |
| 82 break; |
| 83 } |
| 84 result.fIsCoreProfile = caps->isCoreProfile(); |
| 85 result.fUsesPrecisionModifiers = glslCaps->usesPrecisionModifiers(); |
| 86 result.fMustDeclareFragmentShaderOutput = glslCaps->mustDeclareFragmentShade
rOutput(); |
| 87 result.fCanUseMinAndAbsTogether = glslCaps->canUseMinAndAbsTogether(); |
| 88 return result; |
| 89 } |
| 90 |
| 91 static void dump_string(std::string s) { |
| 92 // on Android, SkDebugf only displays the first 1K characters of output, whi
ch results in |
| 93 // incomplete shader source code. Print each line individually to avoid this
problem. |
| 94 size_t index = 0; |
| 95 for (;;) { |
| 96 size_t next = s.find("\n", index); |
| 97 if (next == std::string::npos) { |
| 98 SkDebugf("%s", s.substr(index).c_str()); |
| 99 break; |
| 100 } else { |
| 101 SkDebugf("%s", s.substr(index, next - index + 1).c_str()); |
| 102 index = next + 1; |
| 103 } |
| 104 } |
| 105 } |
| 106 |
21 GrGLuint GrGLCompileAndAttachShader(const GrGLContext& glCtx, | 107 GrGLuint GrGLCompileAndAttachShader(const GrGLContext& glCtx, |
22 GrGLuint programId, | 108 GrGLuint programId, |
23 GrGLenum type, | 109 GrGLenum type, |
24 const char** strings, | 110 const char** strings, |
25 int* lengths, | 111 int* lengths, |
26 int count, | 112 int count, |
27 GrGpu::Stats* stats) { | 113 GrGpu::Stats* stats) { |
28 const GrGLInterface* gli = glCtx.interface(); | 114 const GrGLInterface* gli = glCtx.interface(); |
29 | 115 |
30 GrGLuint shaderId; | 116 GrGLuint shaderId; |
31 GR_GL_CALL_RET(gli, shaderId, CreateShader(type)); | 117 GR_GL_CALL_RET(gli, shaderId, CreateShader(type)); |
32 if (0 == shaderId) { | 118 if (0 == shaderId) { |
33 return 0; | 119 return 0; |
34 } | 120 } |
35 | 121 |
| 122 std::string sksl; |
36 #ifdef SK_DEBUG | 123 #ifdef SK_DEBUG |
37 SkString prettySource = GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths,
count, false); | 124 SkString prettySource = GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths,
count, false); |
38 const GrGLchar* sourceStr = prettySource.c_str(); | 125 sksl = std::string(prettySource.c_str()); |
39 GrGLint sourceLength = static_cast<GrGLint>(prettySource.size()); | |
40 GR_GL_CALL(gli, ShaderSource(shaderId, 1, &sourceStr, &sourceLength)); | |
41 #else | 126 #else |
42 GR_GL_CALL(gli, ShaderSource(shaderId, count, strings, lengths)); | 127 for (int i = 0; i < count; i++) { |
| 128 sksl.append(strings[i], lengths[i]); |
| 129 } |
43 #endif | 130 #endif |
44 | 131 |
| 132 std::string glsl; |
| 133 SkSL::Compiler& compiler = *glCtx.compiler(); |
| 134 SkSL::GLCaps caps = skslcaps_for_context(glCtx); |
| 135 SkASSERT(type == GR_GL_VERTEX_SHADER || type == GR_GL_FRAGMENT_SHADER); |
| 136 SkDEBUGCODE(bool result = )compiler.toGLSL(type == GR_GL_VERTEX_SHADER |
| 137 ? SkSL::Prog
ram::kVertex_Kind |
| 138 : SkSL::Prog
ram::kFragment_Kind, |
| 139 std::string(sksl.c_str()), |
| 140 caps, |
| 141 &glsl); |
| 142 #ifdef SK_DEBUG |
| 143 if (!result) { |
| 144 SkDebugf("SKSL compilation error\n----------------------\n"); |
| 145 SkDebugf("SKSL:\n"); |
| 146 dump_string(sksl); |
| 147 SkDebugf("\nErrors:\n%s\n", compiler.errorText().c_str()); |
| 148 SkDEBUGFAIL("SKSL compilation failed!\n"); |
| 149 } |
| 150 #endif |
| 151 const char* glslChars = glsl.c_str(); |
| 152 GrGLint glslLength = (GrGLint) glsl.length(); |
| 153 GR_GL_CALL(gli, ShaderSource(shaderId, 1, &glslChars, &glslLength)); |
| 154 |
45 // If tracing is enabled in chrome then we pretty print | 155 // If tracing is enabled in chrome then we pretty print |
46 bool traceShader; | 156 bool traceShader; |
47 TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), &t
raceShader); | 157 TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), &t
raceShader); |
48 if (traceShader) { | 158 if (traceShader) { |
49 SkString shader = GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths, c
ount, false); | 159 SkString shader = GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths, c
ount, false); |
50 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "skia_gpu::G
LShader", | 160 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "skia_gpu::G
LShader", |
51 TRACE_EVENT_SCOPE_THREAD, "shader", TRACE_STR_COPY(
shader.c_str())); | 161 TRACE_EVENT_SCOPE_THREAD, "shader", TRACE_STR_COPY(
shader.c_str())); |
52 } | 162 } |
53 | 163 |
54 stats->incShaderCompilations(); | 164 stats->incShaderCompilations(); |
(...skipping 10 matching lines...) Expand all Loading... |
65 | 175 |
66 if (!compiled) { | 176 if (!compiled) { |
67 GrGLint infoLen = GR_GL_INIT_ZERO; | 177 GrGLint infoLen = GR_GL_INIT_ZERO; |
68 GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_INFO_LOG_LENGTH, &infoLe
n)); | 178 GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_INFO_LOG_LENGTH, &infoLe
n)); |
69 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugg
er | 179 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugg
er |
70 if (infoLen > 0) { | 180 if (infoLen > 0) { |
71 // retrieve length even though we don't need it to workaround bu
g in Chromium cmd | 181 // retrieve length even though we don't need it to workaround bu
g in Chromium cmd |
72 // buffer param validation. | 182 // buffer param validation. |
73 GrGLsizei length = GR_GL_INIT_ZERO; | 183 GrGLsizei length = GR_GL_INIT_ZERO; |
74 GR_GL_CALL(gli, GetShaderInfoLog(shaderId, infoLen+1, &length, (
char*)log.get())); | 184 GR_GL_CALL(gli, GetShaderInfoLog(shaderId, infoLen+1, &length, (
char*)log.get())); |
75 print_shader_source(strings, lengths, count); | 185 SkDebugf("GLSL compilation error\n----------------------\n"); |
76 SkDebugf("\n%s", (const char*)log.get()); | 186 SkDebugf("SKSL:\n"); |
| 187 dump_string(sksl); |
| 188 SkDebugf("GLSL:\n"); |
| 189 dump_string(glsl); |
| 190 SkDebugf("Errors:\n%s\n", (const char*) log.get()); |
77 } | 191 } |
78 SkDEBUGFAIL("Shader compilation failed!"); | 192 SkDEBUGFAIL("GLSL compilation failed!"); |
79 GR_GL_CALL(gli, DeleteShader(shaderId)); | 193 GR_GL_CALL(gli, DeleteShader(shaderId)); |
80 return 0; | 194 return 0; |
81 } | 195 } |
82 } | 196 } |
83 | 197 |
84 if (c_PrintShaders) { | 198 if (c_PrintShaders) { |
85 print_shader_source(strings, lengths, count); | 199 print_shader_source(strings, lengths, count); |
86 } | 200 } |
87 | 201 |
88 // Attach the shader, but defer deletion until after we have linked the prog
ram. | 202 // Attach the shader, but defer deletion until after we have linked the prog
ram. |
89 // This works around a bug in the Android emulator's GLES2 wrapper which | 203 // 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 | 204 // 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. | 205 // attached to a program, which then causes glLinkProgram to fail. |
92 GR_GL_CALL(gli, AttachShader(programId, shaderId)); | 206 GR_GL_CALL(gli, AttachShader(programId, shaderId)); |
93 | 207 |
94 return shaderId; | 208 return shaderId; |
95 } | 209 } |
96 | 210 |
97 static void print_shader_source(const char** strings, int* lengths, int count) { | 211 static void print_shader_source(const char** strings, int* lengths, int count) { |
98 const SkString& pretty = GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths
, count, true); | 212 const SkString& pretty = GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths
, count, true); |
99 SkTArray<SkString> lines; | 213 SkTArray<SkString> lines; |
100 SkStrSplit(pretty.c_str(), "\n", &lines); | 214 SkStrSplit(pretty.c_str(), "\n", &lines); |
101 for (const SkString& line : lines) { | 215 for (const SkString& line : lines) { |
102 // Print the shader one line at the time so it doesn't get truncated by
the adb log. | 216 // Print the shader one line at the time so it doesn't get truncated by
the adb log. |
103 SkDebugf("%s\n", line.c_str()); | 217 SkDebugf("%s\n", line.c_str()); |
104 } | 218 } |
105 } | 219 } |
OLD | NEW |