xref: /aosp_15_r20/external/skia/src/gpu/ganesh/mtl/GrMtlPipelineStateBuilder.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2018 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 
8 #ifndef GrMtlPipelineStateBuilder_DEFINED
9 #define GrMtlPipelineStateBuilder_DEFINED
10 
11 #include "include/gpu/ganesh/GrContextOptions.h"
12 #include "src/gpu/ganesh/GrPipeline.h"
13 #include "src/gpu/ganesh/glsl/GrGLSLProgramBuilder.h"
14 #include "src/gpu/ganesh/mtl/GrMtlUniformHandler.h"
15 #include "src/gpu/ganesh/mtl/GrMtlVaryingHandler.h"
16 #include "src/sksl/ir/SkSLProgram.h"
17 
18 #import <Metal/Metal.h>
19 
20 class GrProgramDesc;
21 class GrProgramInfo;
22 class GrMtlCaps;
23 class GrMtlGpu;
24 class GrMtlPipelineState;
25 class SkReadBuffer;
26 
27 namespace SkSL { class Compiler; }
28 
29 struct GrMtlPrecompiledLibraries {
30     // TODO: wrap these in sk_cfp<> or unique_ptr<> when we remove ARC
31     id<MTLLibrary> fVertexLibrary;
32     id<MTLLibrary> fFragmentLibrary;
33     bool fRTFlip = false;
34 };
35 
36 class GrMtlPipelineStateBuilder : public GrGLSLProgramBuilder {
37 public:
38     /** Generates a pipeline state.
39      *
40      * The returned GrMtlPipelineState implements the supplied GrProgramInfo.
41      *
42      * @return the created pipeline if generation was successful; nullptr otherwise
43      */
44     static GrMtlPipelineState* CreatePipelineState(
45                                        GrMtlGpu*,
46                                        const GrProgramDesc&,
47                                        const GrProgramInfo&,
48                                        const GrMtlPrecompiledLibraries* precompiledLibs = nullptr);
49 
50     static bool PrecompileShaders(GrMtlGpu*, const SkData&,
51                                   GrMtlPrecompiledLibraries* precompiledLibs);
52 
53 private:
54     GrMtlPipelineStateBuilder(GrMtlGpu*, const GrProgramDesc&, const GrProgramInfo&);
55 
56     GrMtlPipelineState* finalize(const GrProgramDesc&, const GrProgramInfo&,
57                                  const GrMtlPrecompiledLibraries* precompiledLibraries);
58 
59     const GrCaps* caps() const override;
60 
61     void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) override;
62 
63     id<MTLLibrary> compileMtlShaderLibrary(const std::string& shader,
64                                            SkSL::Program::Interface,
65                                            GrContextOptions::ShaderErrorHandler* errorHandler);
66     void storeShadersInCache(const std::string shaders[],
67                              const SkSL::Program::Interface[],
68                              SkSL::ProgramSettings*,
69                              sk_sp<SkData>,
70                              bool isSkSL);
71 
uniformHandler()72     GrGLSLUniformHandler* uniformHandler() override { return &fUniformHandler; }
uniformHandler()73     const GrGLSLUniformHandler* uniformHandler() const override { return &fUniformHandler; }
varyingHandler()74     GrGLSLVaryingHandler* varyingHandler() override { return &fVaryingHandler; }
75 
76     GrMtlGpu* fGpu;
77     GrMtlUniformHandler fUniformHandler;
78     GrMtlVaryingHandler fVaryingHandler;
79 
80     using INHERITED = GrGLSLProgramBuilder;
81 };
82 #endif
83