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 GrGLProgramDesc& desc, | |
44 const GrGeometryStage* geometryProcessor, | |
45 const GrFragmentStage* colorStages[], | |
46 const GrFragmentStage* coverageStages[]); | |
47 | |
48 virtual ~GrGLProgram(); | 43 virtual ~GrGLProgram(); |
49 | 44 |
50 /** | 45 /** |
51 * Call to abandon GL objects owned by this program. | 46 * Call to abandon GL objects owned by this program. |
52 */ | 47 */ |
53 void abandon(); | 48 void abandon(); |
54 | 49 |
55 const GrGLProgramDesc& getDesc() { return fDesc; } | 50 const GrGLProgramDesc& getDesc() { return fDesc; } |
56 | 51 |
57 /** | 52 /** |
58 * Gets the GL program ID for this program. | 53 * Gets the GL program ID for this program. |
59 */ | 54 */ |
60 GrGLuint programID() const { return fProgramID; } | 55 GrGLuint programID() const { return fProgramID; } |
61 | 56 |
62 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; } |
63 | 61 |
64 /** | 62 /** |
65 * 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 |
66 * 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 |
67 * 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 |
68 * redundantly. | 66 * redundantly. |
69 */ | 67 */ |
70 struct SharedGLState { | 68 struct SharedGLState { |
71 GrColor fConstAttribColor; | 69 GrColor fConstAttribColor; |
72 int fConstAttribColorIndex; | 70 int fConstAttribColorIndex; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 * stages come from GrGLProgramDesc::Build(). | 151 * stages come from GrGLProgramDesc::Build(). |
154 */ | 152 */ |
155 void setData(const GrOptDrawState&, | 153 void setData(const GrOptDrawState&, |
156 GrGpu::DrawType, | 154 GrGpu::DrawType, |
157 const GrGeometryStage* geometryProcessor, | 155 const GrGeometryStage* geometryProcessor, |
158 const GrFragmentStage* colorStages[], | 156 const GrFragmentStage* colorStages[], |
159 const GrFragmentStage* coverageStages[], | 157 const GrFragmentStage* coverageStages[], |
160 const GrDeviceCoordTexture* dstCopy, // can be NULL | 158 const GrDeviceCoordTexture* dstCopy, // can be NULL |
161 SharedGLState*); | 159 SharedGLState*); |
162 | 160 |
163 private: | 161 protected: |
164 typedef GrGLProgramDataManager::UniformHandle UniformHandle; | 162 typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
| 163 typedef GrGLProgramDataManager::UniformInfoArray UniformInfoArray; |
165 | 164 |
166 GrGLProgram(GrGpuGL*, | 165 GrGLProgram(GrGpuGL*, |
167 const GrGLProgramDesc&, | 166 const GrGLProgramDesc&, |
168 const GrGLProgramBuilder&); | 167 const BuiltinUniformHandles&, |
| 168 GrGLuint programID, |
| 169 const UniformInfoArray&, |
| 170 GrGLInstalledProcessors* geometryProcessor, |
| 171 GrGLInstalledProcessors* colorProcessors, |
| 172 GrGLInstalledProcessors* coverageProcessors); |
169 | 173 |
170 // Sets the texture units for samplers. | 174 // Sets the texture units for samplers. |
171 void initSamplerUniforms(); | 175 void initSamplerUniforms(); |
| 176 void initSamplers(GrGLInstalledProcessors* processors, int* texUnitIdx); |
172 | 177 |
173 // 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 |
174 // per-vertex colors. | 179 // per-vertex colors. |
175 void setColor(const GrOptDrawState&, GrColor color, SharedGLState*); | 180 void setColor(const GrOptDrawState&, GrColor color, SharedGLState*); |
176 | 181 |
177 // 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 |
178 // per-vertex coverages. | 183 // per-vertex coverages. |
179 void setCoverage(const GrOptDrawState&, GrColor coverage, SharedGLState*); | 184 void setCoverage(const GrOptDrawState&, GrColor coverage, SharedGLState*); |
180 | 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 virtual void didSetData(GrGpu::DrawType); |
| 205 |
181 // Helper for setData() that sets the view matrix and loads the render targe
t height uniform | 206 // Helper for setData() that sets the view matrix and loads the render targe
t height uniform |
182 void setMatrixAndRenderTargetHeight(GrGpu::DrawType drawType, const GrOptDra
wState&); | 207 void setMatrixAndRenderTargetHeight(GrGpu::DrawType, const GrOptDrawState&); |
| 208 virtual void onSetMatrixAndRenderTargetHeight(GrGpu::DrawType, const GrOptDr
awState&); |
183 | 209 |
184 // these reflect the current values of uniforms (GL uniform values travel wi
th program) | 210 // these reflect the current values of uniforms (GL uniform values travel wi
th program) |
185 MatrixState fMatrixState; | 211 MatrixState fMatrixState; |
186 GrColor fColor; | 212 GrColor fColor; |
187 GrColor fCoverage; | 213 GrColor fCoverage; |
188 int fDstCopyTexUnit; | 214 int fDstCopyTexUnit; |
| 215 BuiltinUniformHandles fBuiltinUniformHandles; |
| 216 GrGLuint fProgramID; |
189 | 217 |
190 BuiltinUniformHandles fBuiltinUniformHandles; | 218 // the installed effects |
191 SkAutoTUnref<GrGLProgramEffects> fGeometryProcessor; | 219 SkAutoTUnref<GrGLInstalledProcessors> fGeometryProcessor; |
192 SkAutoTUnref<GrGLProgramEffects> fColorEffects; | 220 SkAutoTUnref<GrGLInstalledProcessors> fColorEffects; |
193 SkAutoTUnref<GrGLProgramEffects> fCoverageEffects; | 221 SkAutoTUnref<GrGLInstalledProcessors> fCoverageEffects; |
194 GrGLuint fProgramID; | |
195 bool fHasVertexShader; | |
196 int fTexCoordSetCnt; | |
197 | 222 |
198 GrGLProgramDesc fDesc; | 223 GrGLProgramDesc fDesc; |
199 GrGpuGL* fGpu; | 224 GrGpuGL* fGpu; |
| 225 GrGLProgramDataManager fProgramDataManager; |
200 | 226 |
201 GrGLProgramDataManager fProgramDataManager; | 227 friend class GrGLProgramBuilder; |
202 | 228 |
203 typedef SkRefCnt INHERITED; | 229 typedef SkRefCnt INHERITED; |
204 }; | 230 }; |
205 | 231 |
| 232 /* |
| 233 * Below are slight specializations of the program object for the different type
s of programs |
| 234 * The default GrGL programs consist of at the very least a vertex and fragment
shader. |
| 235 * Legacy Nvpr only has a fragment shader, 1.3+ Nvpr ignores the vertex shader,
but both require |
| 236 * specialized methods for setting transform data. Both types of NVPR also requi
re setting the |
| 237 * projection matrix through a special function call |
| 238 */ |
| 239 class GrGLNvprProgramBase : public GrGLProgram { |
| 240 protected: |
| 241 GrGLNvprProgramBase(GrGpuGL*, |
| 242 const GrGLProgramDesc&, |
| 243 const BuiltinUniformHandles&, |
| 244 GrGLuint programID, |
| 245 const UniformInfoArray&, |
| 246 GrGLInstalledProcessors* colorProcessors, |
| 247 GrGLInstalledProcessors* coverageProcessors); |
| 248 virtual void onSetMatrixAndRenderTargetHeight(GrGpu::DrawType, const GrOptDr
awState&); |
| 249 |
| 250 typedef GrGLProgram INHERITED; |
| 251 }; |
| 252 |
| 253 class GrGLNvprProgram : public GrGLNvprProgramBase { |
| 254 public: |
| 255 virtual bool hasVertexShader() const SK_OVERRIDE { return true; } |
| 256 |
| 257 private: |
| 258 typedef GrGLNvprProgramBuilder::SeparableVaryingInfo SeparableVaryingInfo; |
| 259 typedef GrGLNvprProgramBuilder::SeparableVaryingInfoArray SeparableVaryingIn
foArray; |
| 260 GrGLNvprProgram(GrGpuGL*, |
| 261 const GrGLProgramDesc&, |
| 262 const BuiltinUniformHandles&, |
| 263 GrGLuint programID, |
| 264 const UniformInfoArray&, |
| 265 GrGLInstalledProcessors* colorProcessors, |
| 266 GrGLInstalledProcessors* coverageProcessors, |
| 267 const SeparableVaryingInfoArray& separableVaryings); |
| 268 virtual void didSetData(GrGpu::DrawType) SK_OVERRIDE; |
| 269 virtual void setTransformData(const GrProcessorStage&, |
| 270 int effectIdx, |
| 271 GrGLInstalledProcessors*) SK_OVERRIDE; |
| 272 |
| 273 struct Varying { |
| 274 GrGLint fLocation; |
| 275 SkDEBUGCODE( |
| 276 GrSLType fType; |
| 277 ); |
| 278 }; |
| 279 SkTArray<Varying, true> fVaryings; |
| 280 |
| 281 friend class GrGLNvprProgramBuilder; |
| 282 |
| 283 typedef GrGLNvprProgramBase INHERITED; |
| 284 }; |
| 285 |
| 286 class GrGLLegacyNvprProgram : public GrGLNvprProgramBase { |
| 287 public: |
| 288 virtual bool hasVertexShader() const SK_OVERRIDE { return false; } |
| 289 |
| 290 private: |
| 291 GrGLLegacyNvprProgram(GrGpuGL* gpu, |
| 292 const GrGLProgramDesc& desc, |
| 293 const BuiltinUniformHandles&, |
| 294 GrGLuint programID, |
| 295 const UniformInfoArray&, |
| 296 GrGLInstalledProcessors* colorProcessors, |
| 297 GrGLInstalledProcessors* coverageProcessors, |
| 298 int texCoordSetCnt); |
| 299 virtual void didSetData(GrGpu::DrawType) SK_OVERRIDE; |
| 300 virtual void setTransformData(const GrProcessorStage&, |
| 301 int effectIdx, |
| 302 GrGLInstalledProcessors*) SK_OVERRIDE; |
| 303 |
| 304 int fTexCoordSetCnt; |
| 305 |
| 306 friend class GrGLLegacyNvprProgramBuilder; |
| 307 |
| 308 typedef GrGLNvprProgramBase INHERITED; |
| 309 }; |
| 310 |
206 #endif | 311 #endif |
OLD | NEW |