joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/core/SkAutoMalloc.h" |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame] | 9 | #include "src/gpu/GrShaderUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/gpu/gl/GrGLGpu.h" |
| 11 | #include "src/gpu/gl/builders/GrGLShaderStringBuilder.h" |
| 12 | #include "src/sksl/SkSLCompiler.h" |
| 13 | #include "src/sksl/SkSLGLSLCodeGenerator.h" |
| 14 | #include "src/sksl/ir/SkSLProgram.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 15 | |
halcanary | 4e44efe | 2016-08-04 10:47:16 -0700 | [diff] [blame] | 16 | // Print the source code for all shaders generated. |
Brian Salomon | 06ab383 | 2017-12-04 16:45:30 -0500 | [diff] [blame] | 17 | static const bool gPrintSKSL = false; |
| 18 | static const bool gPrintGLSL = false; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 19 | |
Brian Osman | 7971926 | 2020-11-23 14:54:33 -0500 | [diff] [blame] | 20 | std::unique_ptr<SkSL::Program> GrSkSLtoGLSL(const GrGLGpu* gpu, |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 21 | SkSL::ProgramKind programKind, |
Brian Osman | 6c431d5 | 2019-04-15 16:31:54 -0400 | [diff] [blame] | 22 | const SkSL::String& sksl, |
Ethan Nicholas | d1b2eec | 2017-11-01 15:45:43 -0400 | [diff] [blame] | 23 | const SkSL::Program::Settings& settings, |
Brian Osman | 5e7fbfd | 2019-05-03 13:13:35 -0400 | [diff] [blame] | 24 | SkSL::String* glsl, |
| 25 | GrContextOptions::ShaderErrorHandler* errorHandler) { |
Brian Osman | 7971926 | 2020-11-23 14:54:33 -0500 | [diff] [blame] | 26 | SkSL::Compiler* compiler = gpu->shaderCompiler(); |
Ethan Nicholas | 068acd5 | 2017-05-26 14:53:23 -0400 | [diff] [blame] | 27 | std::unique_ptr<SkSL::Program> program; |
Brian Osman | 1e2d4a1 | 2019-06-06 15:25:44 -0400 | [diff] [blame] | 28 | #ifdef SK_DEBUG |
| 29 | SkSL::String src = GrShaderUtils::PrettyPrint(sksl); |
| 30 | #else |
| 31 | const SkSL::String& src = sksl; |
| 32 | #endif |
| 33 | program = compiler->convertProgram(programKind, src, settings); |
Ethan Nicholas | 068acd5 | 2017-05-26 14:53:23 -0400 | [diff] [blame] | 34 | if (!program || !compiler->toGLSL(*program, glsl)) { |
Brian Osman | 1e2d4a1 | 2019-06-06 15:25:44 -0400 | [diff] [blame] | 35 | errorHandler->compileError(src.c_str(), compiler->errorText().c_str()); |
Brian Salomon | e334c59 | 2017-05-15 11:00:58 -0400 | [diff] [blame] | 36 | return nullptr; |
| 37 | } |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame] | 38 | |
| 39 | if (gPrintSKSL || gPrintGLSL) { |
Robert Phillips | 797831c | 2020-11-20 22:35:55 +0000 | [diff] [blame] | 40 | GrShaderUtils::PrintShaderBanner(programKind); |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame] | 41 | if (gPrintSKSL) { |
Chris Dalton | 7791298 | 2019-12-16 11:18:13 -0700 | [diff] [blame] | 42 | SkDebugf("SKSL:\n"); |
| 43 | GrShaderUtils::PrintLineByLine(GrShaderUtils::PrettyPrint(sksl)); |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame] | 44 | } |
| 45 | if (gPrintGLSL) { |
Chris Dalton | 7791298 | 2019-12-16 11:18:13 -0700 | [diff] [blame] | 46 | SkDebugf("GLSL:\n"); |
| 47 | GrShaderUtils::PrintLineByLine(GrShaderUtils::PrettyPrint(*glsl)); |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame] | 48 | } |
Brian Salomon | 06ab383 | 2017-12-04 16:45:30 -0500 | [diff] [blame] | 49 | } |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame] | 50 | |
Ethan Nicholas | 068acd5 | 2017-05-26 14:53:23 -0400 | [diff] [blame] | 51 | return program; |
Brian Salomon | e334c59 | 2017-05-15 11:00:58 -0400 | [diff] [blame] | 52 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 53 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 54 | GrGLuint GrGLCompileAndAttachShader(const GrGLContext& glCtx, |
| 55 | GrGLuint programId, |
| 56 | GrGLenum type, |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame] | 57 | const SkSL::String& glsl, |
Robert Phillips | ae67c52 | 2021-03-03 11:03:38 -0500 | [diff] [blame^] | 58 | GrThreadSafePipelineBuilder::Stats* stats, |
Brian Osman | 5e7fbfd | 2019-05-03 13:13:35 -0400 | [diff] [blame] | 59 | GrContextOptions::ShaderErrorHandler* errorHandler) { |
Brian Osman | d010f65 | 2021-02-24 13:59:39 -0500 | [diff] [blame] | 60 | TRACE_EVENT0_ALWAYS("skia.gpu", "driver_compile_shader"); |
Jim Van Verth | 03b8ab2 | 2020-02-24 11:36:15 -0500 | [diff] [blame] | 61 | const GrGLInterface* gli = glCtx.glInterface(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 62 | |
Brian Salomon | e334c59 | 2017-05-15 11:00:58 -0400 | [diff] [blame] | 63 | // Specify GLSL source to the driver. |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 64 | GrGLuint shaderId; |
| 65 | GR_GL_CALL_RET(gli, shaderId, CreateShader(type)); |
| 66 | if (0 == shaderId) { |
| 67 | return 0; |
| 68 | } |
Brian Osman | ac9be9d | 2019-05-01 10:29:34 -0400 | [diff] [blame] | 69 | const GrGLchar* source = glsl.c_str(); |
| 70 | GrGLint sourceLength = glsl.size(); |
| 71 | GR_GL_CALL(gli, ShaderSource(shaderId, 1, &source, &sourceLength)); |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 72 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 73 | stats->incShaderCompilations(); |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 74 | GR_GL_CALL(gli, CompileShader(shaderId)); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 75 | |
Brian Salomon | d857545 | 2020-02-25 12:13:29 -0500 | [diff] [blame] | 76 | bool checkCompiled = !glCtx.caps()->skipErrorChecks(); |
| 77 | |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 78 | if (checkCompiled) { |
Leon Scroggins | b66214e | 2021-02-11 17:14:18 -0500 | [diff] [blame] | 79 | ATRACE_ANDROID_FRAMEWORK("checkCompiled"); |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 80 | GrGLint compiled = GR_GL_INIT_ZERO; |
| 81 | GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_COMPILE_STATUS, &compiled)); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 82 | |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 83 | if (!compiled) { |
| 84 | GrGLint infoLen = GR_GL_INIT_ZERO; |
| 85 | GR_GL_CALL(gli, GetShaderiv(shaderId, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
| 86 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
| 87 | if (infoLen > 0) { |
| 88 | // retrieve length even though we don't need it to workaround bug in Chromium cmd |
| 89 | // buffer param validation. |
| 90 | GrGLsizei length = GR_GL_INIT_ZERO; |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 91 | GR_GL_CALL(gli, GetShaderInfoLog(shaderId, infoLen+1, &length, (char*)log.get())); |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 92 | } |
Brian Osman | 5e7fbfd | 2019-05-03 13:13:35 -0400 | [diff] [blame] | 93 | errorHandler->compileError(glsl.c_str(), infoLen > 0 ? (const char*)log.get() : ""); |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 94 | GR_GL_CALL(gli, DeleteShader(shaderId)); |
| 95 | return 0; |
| 96 | } |
| 97 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 98 | |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 99 | // Attach the shader, but defer deletion until after we have linked the program. |
| 100 | // This works around a bug in the Android emulator's GLES2 wrapper which |
| 101 | // will immediately delete the shader object and free its memory even though it's |
| 102 | // attached to a program, which then causes glLinkProgram to fail. |
| 103 | GR_GL_CALL(gli, AttachShader(programId, shaderId)); |
robertphillips | 754f4e9 | 2014-09-18 13:52:08 -0700 | [diff] [blame] | 104 | return shaderId; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 105 | } |