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