| 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" | |
| 14 | 12 |
| 15 #define GL_CALL(X) GR_GL_CALL(gpu->glInterface(), X) | 13 #define GL_CALL(X) GR_GL_CALL(gpu->glInterface(), X) |
| 16 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(gpu->glInterface(), R, X) | 14 #define GL_CALL_RET(R, X) GR_GL_CALL_RET(gpu->glInterface(), R, X) |
| 17 | 15 |
| 18 // Print the source code for all shaders generated. | 16 // Print the source code for all shaders generated. |
| 19 static const bool c_PrintShaders{false}; | 17 static const bool c_PrintShaders{false}; |
| 20 | 18 |
| 21 static void print_shader_source(const char** strings, int* lengths, int count); | 19 static void print_shader_source(const char** strings, int* lengths, int count); |
| 22 | 20 |
| 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 | |
| 107 GrGLuint GrGLCompileAndAttachShader(const GrGLContext& glCtx, | 21 GrGLuint GrGLCompileAndAttachShader(const GrGLContext& glCtx, |
| 108 GrGLuint programId, | 22 GrGLuint programId, |
| 109 GrGLenum type, | 23 GrGLenum type, |
| 110 const char** strings, | 24 const char** strings, |
| 111 int* lengths, | 25 int* lengths, |
| 112 int count, | 26 int count, |
| 113 GrGpu::Stats* stats) { | 27 GrGpu::Stats* stats) { |
| 114 const GrGLInterface* gli = glCtx.interface(); | 28 const GrGLInterface* gli = glCtx.interface(); |
| 115 | 29 |
| 116 GrGLuint shaderId; | 30 GrGLuint shaderId; |
| 117 GR_GL_CALL_RET(gli, shaderId, CreateShader(type)); | 31 GR_GL_CALL_RET(gli, shaderId, CreateShader(type)); |
| 118 if (0 == shaderId) { | 32 if (0 == shaderId) { |
| 119 return 0; | 33 return 0; |
| 120 } | 34 } |
| 121 | 35 |
| 122 std::string sksl; | |
| 123 #ifdef SK_DEBUG | 36 #ifdef SK_DEBUG |
| 124 SkString prettySource = GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths,
count, false); | 37 SkString prettySource = GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths,
count, false); |
| 125 sksl = std::string(prettySource.c_str()); | 38 const GrGLchar* sourceStr = prettySource.c_str(); |
| 39 GrGLint sourceLength = static_cast<GrGLint>(prettySource.size()); |
| 40 GR_GL_CALL(gli, ShaderSource(shaderId, 1, &sourceStr, &sourceLength)); |
| 126 #else | 41 #else |
| 127 for (int i = 0; i < count; i++) { | 42 GR_GL_CALL(gli, ShaderSource(shaderId, count, strings, lengths)); |
| 128 sksl.append(strings[i], lengths[i]); | |
| 129 } | |
| 130 #endif | 43 #endif |
| 131 | 44 |
| 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 | |
| 155 // If tracing is enabled in chrome then we pretty print | 45 // If tracing is enabled in chrome then we pretty print |
| 156 bool traceShader; | 46 bool traceShader; |
| 157 TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), &t
raceShader); | 47 TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), &t
raceShader); |
| 158 if (traceShader) { | 48 if (traceShader) { |
| 159 SkString shader = GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths, c
ount, false); | 49 SkString shader = GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths, c
ount, false); |
| 160 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "skia_gpu::G
LShader", | 50 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "skia_gpu::G
LShader", |
| 161 TRACE_EVENT_SCOPE_THREAD, "shader", TRACE_STR_COPY(
shader.c_str())); | 51 TRACE_EVENT_SCOPE_THREAD, "shader", TRACE_STR_COPY(
shader.c_str())); |
| 162 } | 52 } |
| 163 | 53 |
| 164 stats->incShaderCompilations(); | 54 stats->incShaderCompilations(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 175 | 65 |
| 176 if (!compiled) { | 66 if (!compiled) { |
| 177 GrGLint infoLen = GR_GL_INIT_ZERO; | 67 GrGLint infoLen = GR_GL_INIT_ZERO; |
| 178 GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_INFO_LOG_LENGTH, &infoLe
n)); | 68 GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_INFO_LOG_LENGTH, &infoLe
n)); |
| 179 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugg
er | 69 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugg
er |
| 180 if (infoLen > 0) { | 70 if (infoLen > 0) { |
| 181 // retrieve length even though we don't need it to workaround bu
g in Chromium cmd | 71 // retrieve length even though we don't need it to workaround bu
g in Chromium cmd |
| 182 // buffer param validation. | 72 // buffer param validation. |
| 183 GrGLsizei length = GR_GL_INIT_ZERO; | 73 GrGLsizei length = GR_GL_INIT_ZERO; |
| 184 GR_GL_CALL(gli, GetShaderInfoLog(shaderId, infoLen+1, &length, (
char*)log.get())); | 74 GR_GL_CALL(gli, GetShaderInfoLog(shaderId, infoLen+1, &length, (
char*)log.get())); |
| 185 SkDebugf("GLSL compilation error\n----------------------\n"); | 75 print_shader_source(strings, lengths, count); |
| 186 SkDebugf("SKSL:\n"); | 76 SkDebugf("\n%s", (const char*)log.get()); |
| 187 dump_string(sksl); | |
| 188 SkDebugf("GLSL:\n"); | |
| 189 dump_string(glsl); | |
| 190 SkDebugf("Errors:\n%s\n", (const char*) log.get()); | |
| 191 } | 77 } |
| 192 SkDEBUGFAIL("GLSL compilation failed!"); | 78 SkDEBUGFAIL("Shader compilation failed!"); |
| 193 GR_GL_CALL(gli, DeleteShader(shaderId)); | 79 GR_GL_CALL(gli, DeleteShader(shaderId)); |
| 194 return 0; | 80 return 0; |
| 195 } | 81 } |
| 196 } | 82 } |
| 197 | 83 |
| 198 if (c_PrintShaders) { | 84 if (c_PrintShaders) { |
| 199 print_shader_source(strings, lengths, count); | 85 print_shader_source(strings, lengths, count); |
| 200 } | 86 } |
| 201 | 87 |
| 202 // 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. |
| 203 // 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 |
| 204 // 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 |
| 205 // attached to a program, which then causes glLinkProgram to fail. | 91 // attached to a program, which then causes glLinkProgram to fail. |
| 206 GR_GL_CALL(gli, AttachShader(programId, shaderId)); | 92 GR_GL_CALL(gli, AttachShader(programId, shaderId)); |
| 207 | 93 |
| 208 return shaderId; | 94 return shaderId; |
| 209 } | 95 } |
| 210 | 96 |
| 211 static void print_shader_source(const char** strings, int* lengths, int count) { | 97 static void print_shader_source(const char** strings, int* lengths, int count) { |
| 212 const SkString& pretty = GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths
, count, true); | 98 const SkString& pretty = GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths
, count, true); |
| 213 SkTArray<SkString> lines; | 99 SkTArray<SkString> lines; |
| 214 SkStrSplit(pretty.c_str(), "\n", &lines); | 100 SkStrSplit(pretty.c_str(), "\n", &lines); |
| 215 for (const SkString& line : lines) { | 101 for (const SkString& line : lines) { |
| 216 // Print the shader one line at the time so it doesn't get truncated by
the adb log. | 102 // Print the shader one line at the time so it doesn't get truncated by
the adb log. |
| 217 SkDebugf("%s\n", line.c_str()); | 103 SkDebugf("%s\n", line.c_str()); |
| 218 } | 104 } |
| 219 } | 105 } |
| OLD | NEW |