| 1 |
/* |
| 2 |
* Copyright 2023 Google LLC |
| 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 |
|
| 8 |
#include "src/gpu/SkSLToBackend.h" |
| 9 |
|
| 10 |
#include "include/gpu/ShaderErrorHandler.h" |
| 11 |
#include "include/private/base/SkDebug.h" |
| 12 |
#include "src/sksl/SkSLCompiler.h" |
| 13 |
#include "src/sksl/codegen/SkSLNativeShader.h" |
| 14 |
#include "src/sksl/ir/SkSLProgram.h" |
| 15 |
#include "src/utils/SkShaderUtils.h" |
| 16 |
|
| 17 |
#include <memory> |
| 18 |
#include <string> |
| 19 |
|
| 20 |
namespace skgpu { |
| 21 |
|
| 22 |
bool SkSLToBackend(const SkSL::ShaderCaps* caps, |
| 23 |
bool (*toBackend)(SkSL::Program&, const SkSL::ShaderCaps*, SkSL::NativeShader*), |
| 24 |
const char* backendLabel, |
| 25 |
const std::string& sksl, |
| 26 |
SkSL::ProgramKind programKind, |
| 27 |
const SkSL::ProgramSettings& settings, |
| 28 |
SkSL::NativeShader* output, |
| 29 |
SkSL::ProgramInterface* outInterface, |
| 30 |
ShaderErrorHandler* errorHandler) { |
| 31 |
#ifdef SK_DEBUG |
| 32 |
std::string src = SkShaderUtils::PrettyPrint(sksl); |
| 33 |
#else |
| 34 |
const std::string& src = sksl; |
| 35 |
#endif |
| 36 |
SkSL::Compiler compiler; |
| 37 |
std::unique_ptr<SkSL::Program> program = compiler.convertProgram(programKind, src, settings); |
| 38 |
if (!program || !(*toBackend)(*program, caps, output)) { |
| 39 |
errorHandler->compileError(src.c_str(), |
| 40 |
compiler.errorText().c_str(), |
| 41 |
/*shaderWasCached=*/false); |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
#if defined(SK_PRINT_SKSL_SHADERS) |
| 46 |
const bool kPrintSkSL = true; |
| 47 |
#else |
| 48 |
const bool kPrintSkSL = false; |
| 49 |
#endif |
| 50 |
const bool kSkSLPostCompilation = false; |
| 51 |
#if defined(SK_PRINT_NATIVE_SHADERS) |
| 52 |
const bool printBackendSL = (backendLabel != nullptr); |
| 53 |
#else |
| 54 |
const bool printBackendSL = false; |
| 55 |
#endif |
| 56 |
|
| 57 |
if (kPrintSkSL || kSkSLPostCompilation || printBackendSL) { |
| 58 |
SkShaderUtils::PrintShaderBanner(programKind); |
| 59 |
if (kPrintSkSL) { |
| 60 |
SkDebugf("SkSL:\n"); |
| 61 |
SkShaderUtils::PrintLineByLine(SkShaderUtils::PrettyPrint(sksl)); |
| 62 |
} |
| 63 |
if (kSkSLPostCompilation) { |
| 64 |
SkDebugf("SkSL (post-compilation):\n"); |
| 65 |
SkShaderUtils::PrintLineByLine(SkShaderUtils::PrettyPrint(program->description())); |
| 66 |
} |
| 67 |
if (printBackendSL) { |
| 68 |
SkDebugf("%s:\n", backendLabel); |
| 69 |
if (output->isBinary()) { |
| 70 |
const std::string asHex = SkShaderUtils::SpirvAsHexStream(output->fBinary); |
| 71 |
SkShaderUtils::PrintLineByLine(asHex); |
| 72 |
} else { |
| 73 |
SkShaderUtils::PrintLineByLine(output->fText); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
if (outInterface) { |
| 79 |
*outInterface = program->fInterface; |
| 80 |
} |
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
} // namespace skgpu |