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 "GrPipeline.h"
12 #include "gl/GrGLProgram.h"
13 #include "gl/GrGLProgramDataManager.h"
14 #include "gl/GrGLUniformHandler.h"
15 #include "gl/GrGLVaryingHandler.h"
16 #include "glsl/GrGLSLProgramBuilder.h"
17 #include "glsl/GrGLSLProgramDataManager.h"
18 #include "ir/SkSLProgram.h"
19 
20 class GrFragmentProcessor;
21 class GrGLContextInfo;
22 class GrProgramDesc;
23 struct GrGLSLSet;
24 class GrGLSLShaderBuilder;
25 class GrShaderCaps;
26 
27 class GrGLProgramBuilder : public GrGLSLProgramBuilder {
28 public:
29     /** Generates a shader program.
30      *
31      * The program implements what is specified in the stages given as input.
32      * After successful generation, the builder result objects are available
33      * to be used.
34      * This function may modify the GrProgramDesc by setting the surface origin
35      * key to 0 (unspecified) if it turns out the program does not care about
36      * the surface origin.
37      * @return true if generation was successful.
38      */
39     static GrGLProgram* CreateProgram(GrRenderTarget*, GrSurfaceOrigin,
40                                       const GrPrimitiveProcessor&,
41                                       const GrTextureProxy* const primProcProxies[],
42                                       const GrPipeline&,
43                                       GrProgramDesc*,
44                                       GrGLGpu*);
45 
46     const GrCaps* caps() const override;
47 
gpu()48     GrGLGpu* gpu() const { return fGpu; }
49 
50 private:
51     GrGLProgramBuilder(GrGLGpu*, GrRenderTarget*, GrSurfaceOrigin,
52                        const GrPipeline&, const GrPrimitiveProcessor&,
53                        const GrTextureProxy* const primProcProxies[], GrProgramDesc*);
54 
55     void addInputVars(const SkSL::Program::Inputs& inputs);
56     bool compileAndAttachShaders(const char* glsl,
57                                  int length,
58                                  GrGLuint programId,
59                                  GrGLenum type,
60                                  SkTDArray<GrGLuint>* shaderIds,
61                                  const SkSL::Program::Settings& settings,
62                                  const SkSL::Program::Inputs& inputs);
63 
64     bool compileAndAttachShaders(GrGLSLShaderBuilder& shader,
65                                  GrGLuint programId,
66                                  GrGLenum type,
67                                  SkTDArray<GrGLuint>* shaderIds,
68                                  const SkSL::Program::Settings& settings,
69                                  SkSL::Program::Inputs* outInputs);
70     void computeCountsAndStrides(GrGLuint programID, const GrPrimitiveProcessor& primProc,
71                                  bool bindAttribLocations);
72     void storeShaderInCache(const SkSL::Program::Inputs& inputs, GrGLuint programID,
73                             const GrGLSLSet& glsl);
74     GrGLProgram* finalize();
75     void bindProgramResourceLocations(GrGLuint programID);
76     bool checkLinkStatus(GrGLuint programID);
77     void resolveProgramResourceLocations(GrGLuint programID);
78     void cleanupProgram(GrGLuint programID, const SkTDArray<GrGLuint>& shaderIDs);
79     void cleanupShaders(const SkTDArray<GrGLuint>& shaderIDs);
80 
81     // Subclasses create different programs
82     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     typedef GrGLSLProgramBuilder INHERITED;
105 };
106 #endif
107