OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 | 8 |
9 #ifndef GrGLProgram_DEFINED | 9 #ifndef GrGLProgram_DEFINED |
10 #define GrGLProgram_DEFINED | 10 #define GrGLProgram_DEFINED |
11 | 11 |
12 #include "builders/GrGLProgramBuilder.h" | 12 #include "builders/GrGLProgramBuilder.h" |
| 13 #include "builders/GrGLNvprProgramBuilder.h" |
13 #include "GrDrawState.h" | 14 #include "GrDrawState.h" |
14 #include "GrGLContext.h" | 15 #include "GrGLContext.h" |
15 #include "GrGLProgramDesc.h" | 16 #include "GrGLProgramDesc.h" |
16 #include "GrGLSL.h" | 17 #include "GrGLSL.h" |
17 #include "GrGLTexture.h" | 18 #include "GrGLTexture.h" |
18 #include "GrGLProgramDataManager.h" | 19 #include "GrGLProgramDataManager.h" |
19 | 20 |
20 #include "SkString.h" | 21 #include "SkString.h" |
21 #include "SkXfermode.h" | 22 #include "SkXfermode.h" |
22 | 23 |
23 class GrGLProcessor; | 24 class GrGLProcessor; |
24 class GrGLProgramEffects; | 25 class GrGLInstalledProcessors; |
25 class GrGLProgramBuilder; | 26 class GrGLProgramBuilder; |
26 | 27 |
27 /** | 28 /** |
28 * This class manages a GPU program and records per-program information. | 29 * This class manages a GPU program and records per-program information. |
29 * We can specify the attribute locations so that they are constant | 30 * We can specify the attribute locations so that they are constant |
30 * across our shaders. But the driver determines the uniform locations | 31 * across our shaders. But the driver determines the uniform locations |
31 * at link time. We don't need to remember the sampler uniform location | 32 * at link time. We don't need to remember the sampler uniform location |
32 * because we will bind a texture slot to it and never change it | 33 * because we will bind a texture slot to it and never change it |
33 * Uniforms are program-local so we can't rely on fHWState to hold the | 34 * Uniforms are program-local so we can't rely on fHWState to hold the |
34 * previous uniform state after a program change. | 35 * previous uniform state after a program change. |
35 */ | 36 */ |
36 class GrGLProgram : public SkRefCnt { | 37 class GrGLProgram : public SkRefCnt { |
37 public: | 38 public: |
38 SK_DECLARE_INST_COUNT(GrGLProgram) | 39 SK_DECLARE_INST_COUNT(GrGLProgram) |
39 | 40 |
40 typedef GrGLProgramBuilder::BuiltinUniformHandles BuiltinUniformHandles; | 41 typedef GrGLProgramBuilder::BuiltinUniformHandles BuiltinUniformHandles; |
41 | 42 |
42 static GrGLProgram* Create(GrGpuGL* gpu, | |
43 const GrOptDrawState& optState, | |
44 const GrGLProgramDesc& desc, | |
45 const GrGeometryStage* geometryProcessor, | |
46 const GrFragmentStage* colorStages[], | |
47 const GrFragmentStage* coverageStages[]); | |
48 | |
49 virtual ~GrGLProgram(); | 43 virtual ~GrGLProgram(); |
50 | 44 |
51 /** | 45 /** |
52 * Call to abandon GL objects owned by this program. | 46 * Call to abandon GL objects owned by this program. |
53 */ | 47 */ |
54 void abandon(); | 48 void abandon(); |
55 | 49 |
56 const GrGLProgramDesc& getDesc() { return fDesc; } | 50 const GrGLProgramDesc& getDesc() { return fDesc; } |
57 | 51 |
58 /** | 52 /** |
59 * Gets the GL program ID for this program. | 53 * Gets the GL program ID for this program. |
60 */ | 54 */ |
61 GrGLuint programID() const { return fProgramID; } | 55 GrGLuint programID() const { return fProgramID; } |
62 | 56 |
63 bool hasVertexShader() const { return fHasVertexShader; } | 57 /* |
| 58 * The base class always has a vertex shader, only the NVPR variants may omi
t a vertex shader |
| 59 */ |
| 60 virtual bool hasVertexShader() const { return true; } |
64 | 61 |
65 /** | 62 /** |
66 * Some GL state that is relevant to programs is not stored per-program. In
particular color | 63 * Some GL state that is relevant to programs is not stored per-program. In
particular color |
67 * and coverage attributes can be global state. This struct is read and upda
ted by | 64 * and coverage attributes can be global state. This struct is read and upda
ted by |
68 * GrGLProgram::setColor and GrGLProgram::setCoverage to allow us to avoid s
etting this state | 65 * GrGLProgram::setColor and GrGLProgram::setCoverage to allow us to avoid s
etting this state |
69 * redundantly. | 66 * redundantly. |
70 */ | 67 */ |
71 struct SharedGLState { | 68 struct SharedGLState { |
72 GrColor fConstAttribColor; | 69 GrColor fConstAttribColor; |
73 int fConstAttribColorIndex; | 70 int fConstAttribColorIndex; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 * stages come from GrGLProgramDesc::Build(). | 151 * stages come from GrGLProgramDesc::Build(). |
155 */ | 152 */ |
156 void setData(const GrOptDrawState&, | 153 void setData(const GrOptDrawState&, |
157 GrGpu::DrawType, | 154 GrGpu::DrawType, |
158 const GrGeometryStage* geometryProcessor, | 155 const GrGeometryStage* geometryProcessor, |
159 const GrFragmentStage* colorStages[], | 156 const GrFragmentStage* colorStages[], |
160 const GrFragmentStage* coverageStages[], | 157 const GrFragmentStage* coverageStages[], |
161 const GrDeviceCoordTexture* dstCopy, // can be NULL | 158 const GrDeviceCoordTexture* dstCopy, // can be NULL |
162 SharedGLState*); | 159 SharedGLState*); |
163 | 160 |
164 private: | 161 protected: |
165 typedef GrGLProgramDataManager::UniformHandle UniformHandle; | 162 typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
| 163 typedef GrGLProgramDataManager::UniformInfoArray UniformInfoArray; |
166 | 164 |
167 GrGLProgram(GrGpuGL*, | 165 GrGLProgram(GrGpuGL*, |
168 const GrGLProgramDesc&, | 166 const GrGLProgramDesc&, |
169 const GrGLProgramBuilder&); | 167 const BuiltinUniformHandles&, |
| 168 GrGLuint programID, |
| 169 const UniformInfoArray&, |
| 170 GrGLInstalledProcessors* geometryProcessor, |
| 171 GrGLInstalledProcessors* colorProcessors, |
| 172 GrGLInstalledProcessors* coverageProcessors); |
170 | 173 |
171 // Sets the texture units for samplers. | 174 // Sets the texture units for samplers. |
172 void initSamplerUniforms(); | 175 void initSamplerUniforms(); |
| 176 void initSamplers(GrGLInstalledProcessors* processors, int* texUnitIdx); |
173 | 177 |
174 // Helper for setData(). Makes GL calls to specify the initial color when th
ere is not | 178 // Helper for setData(). Makes GL calls to specify the initial color when th
ere is not |
175 // per-vertex colors. | 179 // per-vertex colors. |
176 void setColor(const GrOptDrawState&, GrColor color, SharedGLState*); | 180 void setColor(const GrOptDrawState&, GrColor color, SharedGLState*); |
177 | 181 |
178 // Helper for setData(). Makes GL calls to specify the initial coverage when
there is not | 182 // Helper for setData(). Makes GL calls to specify the initial coverage when
there is not |
179 // per-vertex coverages. | 183 // per-vertex coverages. |
180 void setCoverage(const GrOptDrawState&, GrColor coverage, SharedGLState*); | 184 void setCoverage(const GrOptDrawState&, GrColor coverage, SharedGLState*); |
181 | 185 |
| 186 // A templated helper to loop over effects, set the transforms(via subclass)
and bind textures |
| 187 template <class ProcessorStage> |
| 188 void setData(const ProcessorStage* effectStages[], |
| 189 GrGLInstalledProcessors* installedProcessors) { |
| 190 int numEffects = installedProcessors->fGLProcessors.count(); |
| 191 SkASSERT(numEffects == installedProcessors->fTransforms.count()); |
| 192 SkASSERT(numEffects == installedProcessors->fSamplers.count()); |
| 193 for (int e = 0; e < numEffects; ++e) { |
| 194 const GrProcessor& effect = *effectStages[e]->getProcessor(); |
| 195 installedProcessors->fGLProcessors[e]->setData(fProgramDataManager,
effect); |
| 196 this->setTransformData(*effectStages[e], e, installedProcessors); |
| 197 this->bindTextures(installedProcessors, effect, e); |
| 198 } |
| 199 } |
| 200 virtual void setTransformData(const GrProcessorStage& effectStage, |
| 201 int effectIdx, |
| 202 GrGLInstalledProcessors* pe); |
| 203 void bindTextures(const GrGLInstalledProcessors*, const GrProcessor&, int ef
fectIdx); |
| 204 |
| 205 /* |
| 206 * Legacy NVPR needs a hook here to flush path tex gen settings. |
| 207 * TODO when legacy nvpr is removed, remove this call. |
| 208 */ |
| 209 virtual void didSetData(GrGpu::DrawType); |
| 210 |
182 // Helper for setData() that sets the view matrix and loads the render targe
t height uniform | 211 // Helper for setData() that sets the view matrix and loads the render targe
t height uniform |
183 void setMatrixAndRenderTargetHeight(GrGpu::DrawType drawType, const GrOptDra
wState&); | 212 void setMatrixAndRenderTargetHeight(GrGpu::DrawType, const GrOptDrawState&); |
| 213 virtual void onSetMatrixAndRenderTargetHeight(GrGpu::DrawType, const GrOptDr
awState&); |
184 | 214 |
185 // these reflect the current values of uniforms (GL uniform values travel wi
th program) | 215 // these reflect the current values of uniforms (GL uniform values travel wi
th program) |
186 MatrixState fMatrixState; | 216 MatrixState fMatrixState; |
187 GrColor fColor; | 217 GrColor fColor; |
188 GrColor fCoverage; | 218 GrColor fCoverage; |
189 int fDstCopyTexUnit; | 219 int fDstCopyTexUnit; |
| 220 BuiltinUniformHandles fBuiltinUniformHandles; |
| 221 GrGLuint fProgramID; |
190 | 222 |
191 BuiltinUniformHandles fBuiltinUniformHandles; | 223 // the installed effects |
192 SkAutoTUnref<GrGLProgramEffects> fGeometryProcessor; | 224 SkAutoTUnref<GrGLInstalledProcessors> fGeometryProcessor; |
193 SkAutoTUnref<GrGLProgramEffects> fColorEffects; | 225 SkAutoTUnref<GrGLInstalledProcessors> fColorEffects; |
194 SkAutoTUnref<GrGLProgramEffects> fCoverageEffects; | 226 SkAutoTUnref<GrGLInstalledProcessors> fCoverageEffects; |
195 GrGLuint fProgramID; | |
196 bool fHasVertexShader; | |
197 int fTexCoordSetCnt; | |
198 | 227 |
199 GrGLProgramDesc fDesc; | 228 GrGLProgramDesc fDesc; |
200 GrGpuGL* fGpu; | 229 GrGpuGL* fGpu; |
| 230 GrGLProgramDataManager fProgramDataManager; |
201 | 231 |
202 GrGLProgramDataManager fProgramDataManager; | 232 friend class GrGLProgramBuilder; |
203 | 233 |
204 typedef SkRefCnt INHERITED; | 234 typedef SkRefCnt INHERITED; |
205 }; | 235 }; |
206 | 236 |
| 237 /* |
| 238 * Below are slight specializations of the program object for the different type
s of programs |
| 239 * The default GrGL programs consist of at the very least a vertex and fragment
shader. |
| 240 * Legacy Nvpr only has a fragment shader, 1.3+ Nvpr ignores the vertex shader,
but both require |
| 241 * specialized methods for setting transform data. Both types of NVPR also requi
re setting the |
| 242 * projection matrix through a special function call |
| 243 */ |
| 244 class GrGLNvprProgramBase : public GrGLProgram { |
| 245 protected: |
| 246 GrGLNvprProgramBase(GrGpuGL*, |
| 247 const GrGLProgramDesc&, |
| 248 const BuiltinUniformHandles&, |
| 249 GrGLuint programID, |
| 250 const UniformInfoArray&, |
| 251 GrGLInstalledProcessors* colorProcessors, |
| 252 GrGLInstalledProcessors* coverageProcessors); |
| 253 virtual void onSetMatrixAndRenderTargetHeight(GrGpu::DrawType, const GrOptDr
awState&); |
| 254 |
| 255 typedef GrGLProgram INHERITED; |
| 256 }; |
| 257 |
| 258 class GrGLNvprProgram : public GrGLNvprProgramBase { |
| 259 public: |
| 260 virtual bool hasVertexShader() const SK_OVERRIDE { return true; } |
| 261 |
| 262 private: |
| 263 typedef GrGLNvprProgramBuilder::SeparableVaryingInfo SeparableVaryingInfo; |
| 264 typedef GrGLNvprProgramBuilder::SeparableVaryingInfoArray SeparableVaryingIn
foArray; |
| 265 GrGLNvprProgram(GrGpuGL*, |
| 266 const GrGLProgramDesc&, |
| 267 const BuiltinUniformHandles&, |
| 268 GrGLuint programID, |
| 269 const UniformInfoArray&, |
| 270 GrGLInstalledProcessors* colorProcessors, |
| 271 GrGLInstalledProcessors* coverageProcessors, |
| 272 const SeparableVaryingInfoArray& separableVaryings); |
| 273 virtual void didSetData(GrGpu::DrawType) SK_OVERRIDE; |
| 274 virtual void setTransformData(const GrProcessorStage&, |
| 275 int effectIdx, |
| 276 GrGLInstalledProcessors*) SK_OVERRIDE; |
| 277 |
| 278 struct Varying { |
| 279 GrGLint fLocation; |
| 280 SkDEBUGCODE( |
| 281 GrSLType fType; |
| 282 ); |
| 283 }; |
| 284 SkTArray<Varying, true> fVaryings; |
| 285 |
| 286 friend class GrGLNvprProgramBuilder; |
| 287 |
| 288 typedef GrGLNvprProgramBase INHERITED; |
| 289 }; |
| 290 |
| 291 class GrGLLegacyNvprProgram : public GrGLNvprProgramBase { |
| 292 public: |
| 293 virtual bool hasVertexShader() const SK_OVERRIDE { return false; } |
| 294 |
| 295 private: |
| 296 GrGLLegacyNvprProgram(GrGpuGL* gpu, |
| 297 const GrGLProgramDesc& desc, |
| 298 const BuiltinUniformHandles&, |
| 299 GrGLuint programID, |
| 300 const UniformInfoArray&, |
| 301 GrGLInstalledProcessors* colorProcessors, |
| 302 GrGLInstalledProcessors* coverageProcessors, |
| 303 int texCoordSetCnt); |
| 304 virtual void didSetData(GrGpu::DrawType) SK_OVERRIDE; |
| 305 virtual void setTransformData(const GrProcessorStage&, |
| 306 int effectIdx, |
| 307 GrGLInstalledProcessors*) SK_OVERRIDE; |
| 308 |
| 309 int fTexCoordSetCnt; |
| 310 |
| 311 friend class GrGLLegacyNvprProgramBuilder; |
| 312 |
| 313 typedef GrGLNvprProgramBase INHERITED; |
| 314 }; |
| 315 |
207 #endif | 316 #endif |
OLD | NEW |