• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 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 
9 #ifndef GrGLProgram_DEFINED
10 #define GrGLProgram_DEFINED
11 
12 #include "src/gpu/ganesh/GrFragmentProcessor.h"
13 #include "src/gpu/ganesh/GrGeometryProcessor.h"
14 #include "src/gpu/ganesh/GrXferProcessor.h"
15 #include "src/gpu/ganesh/gl/GrGLProgramDataManager.h"
16 #include "src/gpu/ganesh/glsl/GrGLSLProgramDataManager.h"
17 #include "src/gpu/ganesh/glsl/GrGLSLUniformHandler.h"
18 
19 #include <vector>
20 
21 class GrPipeline;
22 class GrGeometryProcessor;
23 class GrProgramInfo;
24 class GrRenderTarget;
25 class GrTextureProxy;
26 
27 /**
28  * This class manages a GPU program and records per-program information. It also records the vertex
29  * and instance attribute layouts that are to be used with the program.
30  */
31 class GrGLProgram : public SkRefCnt {
32 public:
33     /**
34      * This class has its own Attribute representation as it does not need the name and we don't
35      * want to worry about copying the name string to memory with life time of GrGLProgram.
36      * Additionally, these store the attribute location.
37      */
38     struct Attribute {
39         GrVertexAttribType fCPUType;
40         SkSLType fGPUType;
41         size_t fOffset;
42         GrGLint fLocation;
43     };
44 
45     using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
46     using UniformInfoArray = GrGLProgramDataManager::UniformInfoArray;
47     using VaryingInfoArray = GrGLProgramDataManager::VaryingInfoArray;
48 
49     /**
50      * The attribute array consists of vertexAttributeCnt + instanceAttributeCnt elements with
51      * the vertex attributes preceding the instance attributes.
52      */
53     static sk_sp<GrGLProgram> Make(
54             GrGLGpu*,
55             const GrGLSLBuiltinUniformHandles&,
56             GrGLuint programID,
57             const UniformInfoArray& uniforms,
58             const UniformInfoArray& textureSamplers,
59             std::unique_ptr<GrGeometryProcessor::ProgramImpl>,
60             std::unique_ptr<GrXferProcessor::ProgramImpl>,
61             std::vector<std::unique_ptr<GrFragmentProcessor::ProgramImpl>> fps,
62             std::unique_ptr<Attribute[]>,
63             int vertexAttributeCnt,
64             int instanceAttributeCnt,
65             int vertexStride,
66             int instanceStride);
67 
68     ~GrGLProgram() override;
69 
70     /**
71      * Call to abandon GL objects owned by this program.
72      */
73     void abandon();
74 
75     /**
76      * Gets the GL program ID for this program.
77      */
programID()78     GrGLuint programID() const { return fProgramID; }
79 
80     /**
81      * We use the RT's size and origin to adjust from Skia device space to OpenGL normalized device
82      * space and to make device space positions have the correct origin for processors that require
83      * them.
84      */
85     struct RenderTargetState {
86         SkISize         fRenderTargetSize;
87         GrSurfaceOrigin fRenderTargetOrigin;
88 
RenderTargetStateRenderTargetState89         RenderTargetState() { this->invalidate(); }
invalidateRenderTargetState90         void invalidate() {
91             fRenderTargetSize.fWidth = -1;
92             fRenderTargetSize.fHeight = -1;
93             fRenderTargetOrigin = (GrSurfaceOrigin) -1;
94         }
95     };
96 
97     /**
98      * This function uploads uniforms and calls each GrGLSL*Processor's setData.
99      *
100      * It is the caller's responsibility to ensure the program is bound before calling.
101      */
102     void updateUniforms(const GrRenderTarget*, const GrProgramInfo&);
103 
104     /**
105      * Binds all geometry processor and fragment processor textures.
106      */
107     void bindTextures(const GrGeometryProcessor&,
108                       const GrSurfaceProxy* const geomProcTextures[],
109                       const GrPipeline&);
110 
vertexStride()111     int vertexStride() const { return fVertexStride; }
instanceStride()112     int instanceStride() const { return fInstanceStride; }
113 
numVertexAttributes()114     int numVertexAttributes() const { return fVertexAttributeCnt; }
vertexAttribute(int i)115     const Attribute& vertexAttribute(int i) const {
116         SkASSERT(i >= 0 && i < fVertexAttributeCnt);
117         return fAttributes[i];
118     }
119 
numInstanceAttributes()120     int numInstanceAttributes() const { return fInstanceAttributeCnt; }
instanceAttribute(int i)121     const Attribute& instanceAttribute(int i) const {
122         SkASSERT(i >= 0 && i < fInstanceAttributeCnt);
123         return fAttributes[i + fVertexAttributeCnt];
124     }
125 
126 private:
127     GrGLProgram(GrGLGpu*,
128                 const GrGLSLBuiltinUniformHandles&,
129                 GrGLuint programID,
130                 const UniformInfoArray& uniforms,
131                 const UniformInfoArray& textureSamplers,
132                 std::unique_ptr<GrGeometryProcessor::ProgramImpl>,
133                 std::unique_ptr<GrXferProcessor::ProgramImpl>,
134                 std::vector<std::unique_ptr<GrFragmentProcessor::ProgramImpl>> fpImpls,
135                 std::unique_ptr<Attribute[]>,
136                 int vertexAttributeCnt,
137                 int instanceAttributeCnt,
138                 int vertexStride,
139                 int instanceStride);
140 
141     // Helper for setData() that sets the view matrix and loads the render target height uniform
142     void setRenderTargetState(const GrRenderTarget*, GrSurfaceOrigin, const GrGeometryProcessor&);
143 
144     // these reflect the current values of uniforms (GL uniform values travel with program)
145     RenderTargetState fRenderTargetState;
146     GrGLSLBuiltinUniformHandles fBuiltinUniformHandles;
147     GrGLuint fProgramID;
148 
149     // the installed effects
150     std::unique_ptr<GrGeometryProcessor::ProgramImpl>              fGPImpl;
151     std::unique_ptr<GrXferProcessor::ProgramImpl>                  fXPImpl;
152     std::vector<std::unique_ptr<GrFragmentProcessor::ProgramImpl>> fFPImpls;
153 
154     std::unique_ptr<Attribute[]> fAttributes;
155     int fVertexAttributeCnt;
156     int fInstanceAttributeCnt;
157     int fVertexStride;
158     int fInstanceStride;
159 
160     GrGLGpu* fGpu;
161     GrGLProgramDataManager fProgramDataManager;
162 
163     int fNumTextureSamplers;
164 
165     using INHERITED = SkRefCnt;
166 };
167 
168 #endif
169