• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #ifndef GrGLProgramBuilder_DEFINED
9 #define GrGLProgramBuilder_DEFINED
10 
11 #include "include/gpu/GrContextOptions.h"
12 #include "src/gpu/ganesh/GrPipeline.h"
13 #include "src/gpu/ganesh/gl/GrGLProgram.h"
14 #include "src/gpu/ganesh/gl/GrGLProgramDataManager.h"
15 #include "src/gpu/ganesh/gl/GrGLUniformHandler.h"
16 #include "src/gpu/ganesh/gl/GrGLVaryingHandler.h"
17 #include "src/gpu/ganesh/glsl/GrGLSLProgramBuilder.h"
18 #include "src/gpu/ganesh/glsl/GrGLSLProgramDataManager.h"
19 #include "src/sksl/ir/SkSLProgram.h"
20 
21 class GrFragmentProcessor;
22 class GrGLContextInfo;
23 class GrProgramDesc;
24 class GrGLSLShaderBuilder;
25 struct GrShaderCaps;
26 
27 struct GrGLPrecompiledProgram {
28     GrGLPrecompiledProgram(GrGLuint programID = 0,
29                            SkSL::Program::Inputs inputs = SkSL::Program::Inputs())
fProgramIDGrGLPrecompiledProgram30         : fProgramID(programID)
31         , fInputs(inputs) {}
32 
33     GrGLuint fProgramID;
34     SkSL::Program::Inputs fInputs;
35 };
36 
37 class GrGLProgramBuilder : public GrGLSLProgramBuilder {
38 public:
39     /** Generates a shader program.
40      *
41      * The program implements what is specified in the stages given as input.
42      * After successful generation, the builder result objects are available
43      * to be used.
44      * If a GL program has already been created, the program ID and inputs can
45      * be supplied to skip the shader compilation.
46      * @return the created program if generation was successful.
47      */
48     static sk_sp<GrGLProgram> CreateProgram(GrDirectContext*,
49                                             const GrProgramDesc&,
50                                             const GrProgramInfo&,
51                                             const GrGLPrecompiledProgram* = nullptr);
52 
53     static bool PrecompileProgram(GrDirectContext*, GrGLPrecompiledProgram*, const SkData&);
54 
55     const GrCaps* caps() const override;
56 
gpu()57     GrGLGpu* gpu() const { return fGpu; }
58 
59     SkSL::Compiler* shaderCompiler() const override;
60 
61 private:
62     GrGLProgramBuilder(GrGLGpu*, const GrProgramDesc&, const GrProgramInfo&);
63 
64     void addInputVars(const SkSL::Program::Inputs& inputs);
65     bool compileAndAttachShaders(const std::string& glsl,
66                                  GrGLuint programId,
67                                  GrGLenum type,
68                                  SkTDArray<GrGLuint>* shaderIds,
69                                  GrContextOptions::ShaderErrorHandler* errorHandler);
70 
71     void computeCountsAndStrides(GrGLuint programID,
72                                  const GrGeometryProcessor&,
73                                  bool bindAttribLocations);
74     void storeShaderInCache(const SkSL::Program::Inputs& inputs, GrGLuint programID,
75                             const std::string shaders[], bool isSkSL,
76                             SkSL::ProgramSettings* settings);
77     sk_sp<GrGLProgram> finalize(const GrGLPrecompiledProgram*);
78     void bindProgramResourceLocations(GrGLuint programID);
79     void resolveProgramResourceLocations(GrGLuint programID, bool force);
80 
81     // Subclasses create different programs
82     sk_sp<GrGLProgram> createProgram(GrGLuint programID);
83 
uniformHandler()84     GrGLSLUniformHandler* uniformHandler() override { return &fUniformHandler; }
uniformHandler()85     const GrGLSLUniformHandler* uniformHandler() const override { return &fUniformHandler; }
varyingHandler()86     GrGLSLVaryingHandler* varyingHandler() override { return &fVaryingHandler; }
87 
88     GrGLGpu*              fGpu;
89     GrGLVaryingHandler    fVaryingHandler;
90     GrGLUniformHandler    fUniformHandler;
91 
92     std::unique_ptr<GrGLProgram::Attribute[]> fAttributes;
93     int fVertexAttributeCnt;
94     int fInstanceAttributeCnt;
95     size_t fVertexStride;
96     size_t fInstanceStride;
97 
98     // shader pulled from cache. Data is organized as:
99     // SkSL::Program::Inputs inputs
100     // int binaryFormat
101     // (all remaining bytes) char[] binary
102     sk_sp<SkData> fCached;
103 
104     using INHERITED = GrGLSLProgramBuilder;
105 };
106 #endif
107