aboutsummaryrefslogtreecommitdiff
path: root/src/gpu/PipelineUtils.cpp
blob: f2634bdab249c43ac2c0d3ced9ee6018ad430a14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * Copyright 2023 Google LLC
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "src/gpu/PipelineUtils.h"

namespace skgpu {

static bool sksl_to_backend(SkSL::Compiler* compiler,
                            bool (SkSL::Compiler::*toBackend)(SkSL::Program&, std::string*),
                            const char* backendLabel,
                            const std::string& sksl,
                            SkSL::ProgramKind programKind,
                            const SkSL::ProgramSettings& settings,
                            std::string* output,
                            SkSL::Program::Interface* outInterface,
                            ShaderErrorHandler* errorHandler) {
#ifdef SK_DEBUG
    std::string src = SkShaderUtils::PrettyPrint(sksl);
#else
    const std::string& src = sksl;
#endif
    std::unique_ptr<SkSL::Program> program = compiler->convertProgram(programKind,
                                                                      src,
                                                                      settings);
    if (!program || !(compiler->*toBackend)(*program, output)) {
        errorHandler->compileError(src.c_str(), compiler->errorText().c_str());
        return false;
    }

#if defined(SK_PRINT_SKSL_SHADERS)
    const bool kPrintSkSL = true;
#else
    const bool kPrintSkSL = false;
#endif
#if defined(SK_PRINT_NATIVE_SHADERS)
    const bool printBackendSL = (backendLabel != nullptr);
#else
    const bool printBackendSL = false;
#endif

    if (kPrintSkSL || printBackendSL) {
        SkShaderUtils::PrintShaderBanner(programKind);
        if (kPrintSkSL) {
            SkDebugf("SkSL:\n");
            SkShaderUtils::PrintLineByLine(SkShaderUtils::PrettyPrint(sksl));
        }
        if (printBackendSL) {
            SkDebugf("%s:\n", backendLabel);
            SkShaderUtils::PrintLineByLine(*output);
        }
    }

    if (outInterface) {
        *outInterface = program->fInterface;
    }
    return true;
}

bool SkSLToGLSL(SkSL::Compiler* compiler,
                const std::string& sksl,
                SkSL::ProgramKind programKind,
                const SkSL::ProgramSettings& settings,
                std::string* glsl,
                SkSL::Program::Interface* outInterface,
                ShaderErrorHandler* errorHandler) {
    return sksl_to_backend(compiler, &SkSL::Compiler::toGLSL, "GLSL",
                           sksl, programKind, settings, glsl, outInterface, errorHandler);
}

bool SkSLToSPIRV(SkSL::Compiler* compiler,
                 const std::string& sksl,
                 SkSL::ProgramKind programKind,
                 const SkSL::ProgramSettings& settings,
                 std::string* spirv,
                 SkSL::Program::Interface* outInterface,
                 ShaderErrorHandler* errorHandler) {
    return sksl_to_backend(compiler, &SkSL::Compiler::toSPIRV, /*backendLabel=*/nullptr,
                           sksl, programKind, settings, spirv, outInterface, errorHandler);
}

bool SkSLToWGSL(SkSL::Compiler* compiler,
                const std::string& sksl,
                SkSL::ProgramKind programKind,
                const SkSL::ProgramSettings& settings,
                std::string* wgsl,
                SkSL::Program::Interface* outInterface,
                ShaderErrorHandler* errorHandler) {
    return sksl_to_backend(compiler, &SkSL::Compiler::toWGSL, "WGSL",
                           sksl, programKind, settings, wgsl, outInterface, errorHandler);
}

bool SkSLToMSL(SkSL::Compiler* compiler,
               const std::string& sksl,
               SkSL::ProgramKind programKind,
               const SkSL::ProgramSettings& settings,
               std::string* msl,
               SkSL::Program::Interface* outInterface,
               ShaderErrorHandler* errorHandler) {
    return sksl_to_backend(compiler, &SkSL::Compiler::toMetal, "MSL",
                           sksl, programKind, settings, msl, outInterface, errorHandler);
}

bool SkSLToHLSL(SkSL::Compiler* compiler,
                const std::string& sksl,
                SkSL::ProgramKind programKind,
                const SkSL::ProgramSettings& settings,
                std::string* hlsl,
                SkSL::Program::Interface* outInterface,
                ShaderErrorHandler* errorHandler) {
    return sksl_to_backend(compiler, &SkSL::Compiler::toHLSL, "HLSL",
                           sksl, programKind, settings, hlsl, outInterface, errorHandler);
}

} // namespace skgpu