| 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" | |
| 14 #include "GrDrawState.h" | 13 #include "GrDrawState.h" |
| 15 #include "GrGLContext.h" | 14 #include "GrGLContext.h" |
| 16 #include "GrGLProgramDesc.h" | 15 #include "GrGLProgramDesc.h" |
| 17 #include "GrGLSL.h" | 16 #include "GrGLSL.h" |
| 18 #include "GrGLTexture.h" | 17 #include "GrGLTexture.h" |
| 19 #include "GrGLProgramDataManager.h" | 18 #include "GrGLProgramDataManager.h" |
| 20 | 19 |
| 21 #include "SkString.h" | 20 #include "SkString.h" |
| 22 #include "SkXfermode.h" | 21 #include "SkXfermode.h" |
| 23 | 22 |
| 24 class GrGLProcessor; | 23 class GrGLProcessor; |
| 25 class GrGLInstalledProcessors; | 24 class GrGLProgramEffects; |
| 26 class GrGLProgramBuilder; | 25 class GrGLProgramBuilder; |
| 27 | 26 |
| 28 /** | 27 /** |
| 29 * This class manages a GPU program and records per-program information. | 28 * This class manages a GPU program and records per-program information. |
| 30 * We can specify the attribute locations so that they are constant | 29 * We can specify the attribute locations so that they are constant |
| 31 * across our shaders. But the driver determines the uniform locations | 30 * across our shaders. But the driver determines the uniform locations |
| 32 * at link time. We don't need to remember the sampler uniform location | 31 * 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 | 32 * 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 | 33 * Uniforms are program-local so we can't rely on fHWState to hold the |
| 35 * previous uniform state after a program change. | 34 * previous uniform state after a program change. |
| 36 */ | 35 */ |
| 37 class GrGLProgram : public SkRefCnt { | 36 class GrGLProgram : public SkRefCnt { |
| 38 public: | 37 public: |
| 39 SK_DECLARE_INST_COUNT(GrGLProgram) | 38 SK_DECLARE_INST_COUNT(GrGLProgram) |
| 40 | 39 |
| 41 typedef GrGLProgramBuilder::BuiltinUniformHandles BuiltinUniformHandles; | 40 typedef GrGLProgramBuilder::BuiltinUniformHandles BuiltinUniformHandles; |
| 42 | 41 |
| 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 |
| 43 virtual ~GrGLProgram(); | 49 virtual ~GrGLProgram(); |
| 44 | 50 |
| 45 /** | 51 /** |
| 46 * Call to abandon GL objects owned by this program. | 52 * Call to abandon GL objects owned by this program. |
| 47 */ | 53 */ |
| 48 void abandon(); | 54 void abandon(); |
| 49 | 55 |
| 50 const GrGLProgramDesc& getDesc() { return fDesc; } | 56 const GrGLProgramDesc& getDesc() { return fDesc; } |
| 51 | 57 |
| 52 /** | 58 /** |
| 53 * Gets the GL program ID for this program. | 59 * Gets the GL program ID for this program. |
| 54 */ | 60 */ |
| 55 GrGLuint programID() const { return fProgramID; } | 61 GrGLuint programID() const { return fProgramID; } |
| 56 | 62 |
| 57 /* | 63 bool hasVertexShader() const { return fHasVertexShader; } |
| 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; } | |
| 61 | 64 |
| 62 /** | 65 /** |
| 63 * Some GL state that is relevant to programs is not stored per-program. In
particular color | 66 * Some GL state that is relevant to programs is not stored per-program. In
particular color |
| 64 * and coverage attributes can be global state. This struct is read and upda
ted by | 67 * and coverage attributes can be global state. This struct is read and upda
ted by |
| 65 * GrGLProgram::setColor and GrGLProgram::setCoverage to allow us to avoid s
etting this state | 68 * GrGLProgram::setColor and GrGLProgram::setCoverage to allow us to avoid s
etting this state |
| 66 * redundantly. | 69 * redundantly. |
| 67 */ | 70 */ |
| 68 struct SharedGLState { | 71 struct SharedGLState { |
| 69 GrColor fConstAttribColor; | 72 GrColor fConstAttribColor; |
| 70 int fConstAttribColorIndex; | 73 int fConstAttribColorIndex; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 * stages come from GrGLProgramDesc::Build(). | 154 * stages come from GrGLProgramDesc::Build(). |
| 152 */ | 155 */ |
| 153 void setData(const GrOptDrawState&, | 156 void setData(const GrOptDrawState&, |
| 154 GrGpu::DrawType, | 157 GrGpu::DrawType, |
| 155 const GrGeometryStage* geometryProcessor, | 158 const GrGeometryStage* geometryProcessor, |
| 156 const GrFragmentStage* colorStages[], | 159 const GrFragmentStage* colorStages[], |
| 157 const GrFragmentStage* coverageStages[], | 160 const GrFragmentStage* coverageStages[], |
| 158 const GrDeviceCoordTexture* dstCopy, // can be NULL | 161 const GrDeviceCoordTexture* dstCopy, // can be NULL |
| 159 SharedGLState*); | 162 SharedGLState*); |
| 160 | 163 |
| 161 protected: | 164 private: |
| 162 typedef GrGLProgramDataManager::UniformHandle UniformHandle; | 165 typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
| 163 typedef GrGLProgramDataManager::UniformInfoArray UniformInfoArray; | |
| 164 | 166 |
| 165 GrGLProgram(GrGpuGL*, | 167 GrGLProgram(GrGpuGL*, |
| 166 const GrGLProgramDesc&, | 168 const GrGLProgramDesc&, |
| 167 const BuiltinUniformHandles&, | 169 const GrGLProgramBuilder&); |
| 168 GrGLuint programID, | |
| 169 const UniformInfoArray&, | |
| 170 GrGLInstalledProcessors* geometryProcessor, | |
| 171 GrGLInstalledProcessors* colorProcessors, | |
| 172 GrGLInstalledProcessors* coverageProcessors); | |
| 173 | 170 |
| 174 // Sets the texture units for samplers. | 171 // Sets the texture units for samplers. |
| 175 void initSamplerUniforms(); | 172 void initSamplerUniforms(); |
| 176 void initSamplers(GrGLInstalledProcessors* processors, int* texUnitIdx); | |
| 177 | 173 |
| 178 // Helper for setData(). Makes GL calls to specify the initial color when th
ere is not | 174 // Helper for setData(). Makes GL calls to specify the initial color when th
ere is not |
| 179 // per-vertex colors. | 175 // per-vertex colors. |
| 180 void setColor(const GrOptDrawState&, GrColor color, SharedGLState*); | 176 void setColor(const GrOptDrawState&, GrColor color, SharedGLState*); |
| 181 | 177 |
| 182 // Helper for setData(). Makes GL calls to specify the initial coverage when
there is not | 178 // Helper for setData(). Makes GL calls to specify the initial coverage when
there is not |
| 183 // per-vertex coverages. | 179 // per-vertex coverages. |
| 184 void setCoverage(const GrOptDrawState&, GrColor coverage, SharedGLState*); | 180 void setCoverage(const GrOptDrawState&, GrColor coverage, SharedGLState*); |
| 185 | 181 |
| 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 | |
| 211 // Helper for setData() that sets the view matrix and loads the render targe
t height uniform | 182 // Helper for setData() that sets the view matrix and loads the render targe
t height uniform |
| 212 void setMatrixAndRenderTargetHeight(GrGpu::DrawType, const GrOptDrawState&); | 183 void setMatrixAndRenderTargetHeight(GrGpu::DrawType drawType, const GrOptDra
wState&); |
| 213 virtual void onSetMatrixAndRenderTargetHeight(GrGpu::DrawType, const GrOptDr
awState&); | |
| 214 | 184 |
| 215 // these reflect the current values of uniforms (GL uniform values travel wi
th program) | 185 // these reflect the current values of uniforms (GL uniform values travel wi
th program) |
| 216 MatrixState fMatrixState; | 186 MatrixState fMatrixState; |
| 217 GrColor fColor; | 187 GrColor fColor; |
| 218 GrColor fCoverage; | 188 GrColor fCoverage; |
| 219 int fDstCopyTexUnit; | 189 int fDstCopyTexUnit; |
| 220 BuiltinUniformHandles fBuiltinUniformHandles; | |
| 221 GrGLuint fProgramID; | |
| 222 | 190 |
| 223 // the installed effects | 191 BuiltinUniformHandles fBuiltinUniformHandles; |
| 224 SkAutoTUnref<GrGLInstalledProcessors> fGeometryProcessor; | 192 SkAutoTUnref<GrGLProgramEffects> fGeometryProcessor; |
| 225 SkAutoTUnref<GrGLInstalledProcessors> fColorEffects; | 193 SkAutoTUnref<GrGLProgramEffects> fColorEffects; |
| 226 SkAutoTUnref<GrGLInstalledProcessors> fCoverageEffects; | 194 SkAutoTUnref<GrGLProgramEffects> fCoverageEffects; |
| 195 GrGLuint fProgramID; |
| 196 bool fHasVertexShader; |
| 197 int fTexCoordSetCnt; |
| 227 | 198 |
| 228 GrGLProgramDesc fDesc; | 199 GrGLProgramDesc fDesc; |
| 229 GrGpuGL* fGpu; | 200 GrGpuGL* fGpu; |
| 230 GrGLProgramDataManager fProgramDataManager; | |
| 231 | 201 |
| 232 friend class GrGLProgramBuilder; | 202 GrGLProgramDataManager fProgramDataManager; |
| 233 | 203 |
| 234 typedef SkRefCnt INHERITED; | 204 typedef SkRefCnt INHERITED; |
| 235 }; | 205 }; |
| 236 | 206 |
| 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 | |
| 316 #endif | 207 #endif |
| OLD | NEW |