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