Chromium Code Reviews| 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 | |
| 21 GrGLuint GrGLCompileAndAttachShader(const GrGLContext& glCtx, | 91 GrGLuint GrGLCompileAndAttachShader(const GrGLContext& glCtx, |
| 22 GrGLuint programId, | 92 GrGLuint programId, |
| 23 GrGLenum type, | 93 GrGLenum type, |
| 24 const char** strings, | 94 const char** strings, |
| 25 int* lengths, | 95 int* lengths, |
| 26 int count, | 96 int count, |
| 27 GrGpu::Stats* stats) { | 97 GrGpu::Stats* stats) { |
| 28 const GrGLInterface* gli = glCtx.interface(); | 98 const GrGLInterface* gli = glCtx.interface(); |
| 29 | 99 |
| 30 GrGLuint shaderId; | 100 GrGLuint shaderId; |
| 31 GR_GL_CALL_RET(gli, shaderId, CreateShader(type)); | 101 GR_GL_CALL_RET(gli, shaderId, CreateShader(type)); |
| 32 if (0 == shaderId) { | 102 if (0 == shaderId) { |
| 33 return 0; | 103 return 0; |
| 34 } | 104 } |
| 35 | 105 |
| 106 std::string sksl; | |
| 36 #ifdef SK_DEBUG | 107 #ifdef SK_DEBUG |
| 37 SkString prettySource = GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths, count, false); | 108 SkString prettySource = GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths, count, false); |
| 38 const GrGLchar* sourceStr = prettySource.c_str(); | 109 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 | 110 #else |
| 42 GR_GL_CALL(gli, ShaderSource(shaderId, count, strings, lengths)); | 111 for (int i = 0; i < count; i++) { |
| 112 sksl.append(strings[i], lengths[i]); | |
| 113 } | |
| 43 #endif | 114 #endif |
| 44 | 115 |
| 116 std::string glsl; | |
| 117 // creating Compiler is expensive, and we are single-threaded anyway, so jus t reuse a static one | |
| 118 static SkSL::Compiler compiler; | |
|
bsalomon
2016/09/08 19:51:37
We only allow a single GrContext to be used on one
ethannicholas
2016/09/09 16:34:54
I moved the compiler instance into GrGLContext to
bsalomon
2016/09/12 13:48:57
I don't have a problem with including SkSLCompiler
ethannicholas
2016/09/12 14:53:26
error: invalid application of ‘sizeof’ to incomple
| |
| 119 SkSL::GLCaps caps = skslcaps_for_context(glCtx); | |
| 120 SkASSERT(type == GR_GL_VERTEX_SHADER || type == GR_GL_FRAGMENT_SHADER); | |
| 121 bool result = compiler.toGLSL(type == GR_GL_VERTEX_SHADER ? SkSL::Program::k Vertex_Kind | |
| 122 : SkSL::Program::k Fragment_Kind, | |
| 123 std::string(sksl.c_str()), | |
| 124 caps, | |
| 125 &glsl); | |
| 126 SkASSERTF(result, "SkSL errors:\n%s", compiler.errorText().c_str()); | |
| 127 const char* glslChars = glsl.c_str(); | |
| 128 GrGLint glslLength = (GrGLint) glsl.length(); | |
| 129 GR_GL_CALL(gli, ShaderSource(shaderId, 1, &glslChars, &glslLength)); | |
| 130 | |
| 45 // If tracing is enabled in chrome then we pretty print | 131 // If tracing is enabled in chrome then we pretty print |
| 46 bool traceShader; | 132 bool traceShader; |
| 47 TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), &t raceShader); | 133 TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), &t raceShader); |
| 48 if (traceShader) { | 134 if (traceShader) { |
| 49 SkString shader = GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths, c ount, false); | 135 SkString shader = GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths, c ount, false); |
| 50 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "skia_gpu::G LShader", | 136 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())); | 137 TRACE_EVENT_SCOPE_THREAD, "shader", TRACE_STR_COPY( shader.c_str())); |
| 52 } | 138 } |
| 53 | 139 |
| 54 stats->incShaderCompilations(); | 140 stats->incShaderCompilations(); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 | 182 |
| 97 static void print_shader_source(const char** strings, int* lengths, int count) { | 183 static void print_shader_source(const char** strings, int* lengths, int count) { |
| 98 const SkString& pretty = GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths , count, true); | 184 const SkString& pretty = GrGLSLPrettyPrint::PrettyPrintGLSL(strings, lengths , count, true); |
| 99 SkTArray<SkString> lines; | 185 SkTArray<SkString> lines; |
| 100 SkStrSplit(pretty.c_str(), "\n", &lines); | 186 SkStrSplit(pretty.c_str(), "\n", &lines); |
| 101 for (const SkString& line : lines) { | 187 for (const SkString& line : lines) { |
| 102 // Print the shader one line at the time so it doesn't get truncated by the adb log. | 188 // 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()); | 189 SkDebugf("%s\n", line.c_str()); |
| 104 } | 190 } |
| 105 } | 191 } |
| OLD | NEW |