|
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 "GrDrawState.h" |
|
13 #include "GrGLContext.h" |
|
14 #include "GrGLProgramDesc.h" |
|
15 #include "GrGLShaderBuilder.h" |
|
16 #include "GrGLSL.h" |
|
17 #include "GrGLTexture.h" |
|
18 #include "GrGLUniformManager.h" |
|
19 |
|
20 #include "SkString.h" |
|
21 #include "SkXfermode.h" |
|
22 |
|
23 class GrBinHashKeyBuilder; |
|
24 class GrGLEffect; |
|
25 class GrGLProgramEffects; |
|
26 class GrGLShaderBuilder; |
|
27 |
|
28 /** |
|
29 * This class manages a GPU program and records per-program information. |
|
30 * We can specify the attribute locations so that they are constant |
|
31 * across our shaders. But the driver determines the uniform locations |
|
32 * at link time. We don't need to remember the sampler uniform location |
|
33 * because we will bind a texture slot to it and never change it |
|
34 * Uniforms are program-local so we can't rely on fHWState to hold the |
|
35 * previous uniform state after a program change. |
|
36 */ |
|
37 class GrGLProgram : public SkRefCnt { |
|
38 public: |
|
39 SK_DECLARE_INST_COUNT(GrGLProgram) |
|
40 |
|
41 static GrGLProgram* Create(GrGpuGL* gpu, |
|
42 const GrGLProgramDesc& desc, |
|
43 const GrEffectStage* colorStages[], |
|
44 const GrEffectStage* coverageStages[]); |
|
45 |
|
46 virtual ~GrGLProgram(); |
|
47 |
|
48 /** |
|
49 * Call to abandon GL objects owned by this program. |
|
50 */ |
|
51 void abandon(); |
|
52 |
|
53 /** |
|
54 * The shader may modify the blend coefficients. Params are in/out. |
|
55 */ |
|
56 void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const; |
|
57 |
|
58 const GrGLProgramDesc& getDesc() { return fDesc; } |
|
59 |
|
60 /** |
|
61 * Gets the GL program ID for this program. |
|
62 */ |
|
63 GrGLuint programID() const { return fProgramID; } |
|
64 |
|
65 bool hasVertexShader() const { return fHasVertexShader; } |
|
66 |
|
67 /** |
|
68 * Some GL state that is relevant to programs is not stored per-program. In particular color |
|
69 * and coverage attributes can be global state. This struct is read and updated by |
|
70 * GrGLProgram::setColor and GrGLProgram::setCoverage to allow us to avoid setting this state |
|
71 * redundantly. |
|
72 */ |
|
73 struct SharedGLState { |
|
74 GrColor fConstAttribColor; |
|
75 int fConstAttribColorIndex; |
|
76 GrColor fConstAttribCoverage; |
|
77 int fConstAttribCoverageIndex; |
|
78 |
|
79 SharedGLState() { this->invalidate(); } |
|
80 void invalidate() { |
|
81 fConstAttribColor = GrColor_ILLEGAL; |
|
82 fConstAttribColorIndex = -1; |
|
83 fConstAttribCoverage = GrColor_ILLEGAL; |
|
84 fConstAttribCoverageIndex = -1; |
|
85 } |
|
86 }; |
|
87 |
|
88 /** |
|
89 * The GrDrawState's view matrix along with the aspects of the render target determine the |
|
90 * matrix sent to GL. The size of the render target affects the GL matrix because we must |
|
91 * convert from Skia device coords to GL's normalized coords. Also the origin of the render |
|
92 * target may require us to perform a mirror-flip. |
|
93 */ |
|
94 struct MatrixState { |
|
95 SkMatrix fViewMatrix; |
|
96 SkISize fRenderTargetSize; |
|
97 GrSurfaceOrigin fRenderTargetOrigin; |
|
98 |
|
99 MatrixState() { this->invalidate(); } |
|
100 void invalidate() { |
|
101 fViewMatrix = SkMatrix::InvalidMatrix(); |
|
102 fRenderTargetSize.fWidth = -1; |
|
103 fRenderTargetSize.fHeight = -1; |
|
104 fRenderTargetOrigin = (GrSurfaceOrigin) -1; |
|
105 } |
|
106 template<int Size> void getGLMatrix(GrGLfloat* destMatrix) { |
|
107 SkMatrix combined; |
|
108 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) { |
|
109 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1, |
|
110 0, -SkIntToScalar(2) / fRenderTargetSize.fHeight, SK_Scalar1, |
|
111 0, 0, SkMatrix::I()[8]); |
|
112 } else { |
|
113 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1, |
|
114 0, SkIntToScalar(2) / fRenderTargetSize.fHeight, -SK_Scalar1, |
|
115 0, 0, SkMatrix::I()[8]); |
|
116 } |
|
117 combined.setConcat(combined, fViewMatrix); |
|
118 GrGLGetMatrix<Size>(destMatrix, combined); |
|
119 } |
|
120 }; |
|
121 |
|
122 /** |
|
123 * This function uploads uniforms and calls each GrGLEffect's setData. It is called before a |
|
124 * draw occurs using the program after the program has already been bound. It also uses the |
|
125 * GrGpuGL object to bind the textures required by the GrGLEffects. The color and coverage |
|
126 * stages come from GrGLProgramDesc::Build(). |
|
127 */ |
|
128 void setData(GrDrawState::BlendOptFlags, |
|
129 const GrEffectStage* colorStages[], |
|
130 const GrEffectStage* coverageStages[], |
|
131 const GrDeviceCoordTexture* dstCopy, // can be NULL |
|
132 SharedGLState*); |
|
133 |
|
134 private: |
|
135 typedef GrGLUniformManager::UniformHandle UniformHandle; |
|
136 |
|
137 // handles for uniforms (aside from per-effect samplers) |
|
138 struct UniformHandles { |
|
139 UniformHandle fViewMatrixUni; |
|
140 UniformHandle fColorUni; |
|
141 UniformHandle fCoverageUni; |
|
142 |
|
143 // We use the render target height to provide a y-down frag coord when specifying |
|
144 // origin_upper_left is not supported. |
|
145 UniformHandle fRTHeightUni; |
|
146 |
|
147 // Uniforms for computing texture coords to do the dst-copy lookup |
|
148 UniformHandle fDstCopyTopLeftUni; |
|
149 UniformHandle fDstCopyScaleUni; |
|
150 UniformHandle fDstCopySamplerUni; |
|
151 }; |
|
152 |
|
153 GrGLProgram(GrGpuGL* gpu, |
|
154 const GrGLProgramDesc& desc, |
|
155 const GrEffectStage* colorStages[], |
|
156 const GrEffectStage* coverageStages[]); |
|
157 |
|
158 bool succeeded() const { return 0 != fProgramID; } |
|
159 |
|
160 /** |
|
161 * This is the heavy initialization routine for building a GLProgram. colorStages and |
|
162 * coverageStages correspond to the output of GrGLProgramDesc::Build(). |
|
163 */ |
|
164 bool genProgram(GrGLShaderBuilder* builder, |
|
165 const GrEffectStage* colorStages[], |
|
166 const GrEffectStage* coverageStages[]); |
|
167 |
|
168 // Sets the texture units for samplers |
|
169 void initSamplerUniforms(); |
|
170 |
|
171 // Helper for setData(). Makes GL calls to specify the initial color when there is not |
|
172 // per-vertex colors. |
|
173 void setColor(const GrDrawState&, GrColor color, SharedGLState*); |
|
174 |
|
175 // Helper for setData(). Makes GL calls to specify the initial coverage when there is not |
|
176 // per-vertex coverages. |
|
177 void setCoverage(const GrDrawState&, GrColor coverage, SharedGLState*); |
|
178 |
|
179 // Helper for setData() that sets the view matrix and loads the render target height uniform |
|
180 void setMatrixAndRenderTargetHeight(const GrDrawState&); |
|
181 |
|
182 // GL program ID |
|
183 GrGLuint fProgramID; |
|
184 |
|
185 // these reflect the current values of uniforms (GL uniform values travel with program) |
|
186 MatrixState fMatrixState; |
|
187 GrColor fColor; |
|
188 GrColor fCoverage; |
|
189 int fDstCopyTexUnit; |
|
190 |
|
191 SkAutoTDelete<GrGLProgramEffects> fColorEffects; |
|
192 SkAutoTDelete<GrGLProgramEffects> fCoverageEffects; |
|
193 |
|
194 GrGLProgramDesc fDesc; |
|
195 GrGpuGL* fGpu; |
|
196 |
|
197 GrGLUniformManager fUniformManager; |
|
198 UniformHandles fUniformHandles; |
|
199 |
|
200 bool fHasVertexShader; |
|
201 int fNumTexCoordSets; |
|
202 |
|
203 typedef SkRefCnt INHERITED; |
|
204 }; |
|
205 |
|
206 #endif |