| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 #ifndef GrGLProgramBuilder_DEFINED | 8 #ifndef GrGLProgramBuilder_DEFINED |
| 9 #define GrGLProgramBuilder_DEFINED | 9 #define GrGLProgramBuilder_DEFINED |
| 10 | 10 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 class GrGLVarying { | 82 class GrGLVarying { |
| 83 public: | 83 public: |
| 84 bool vsVarying() const { return kVertToFrag_Varying == fVarying || | 84 bool vsVarying() const { return kVertToFrag_Varying == fVarying || |
| 85 kVertToGeo_Varying == fVarying; } | 85 kVertToGeo_Varying == fVarying; } |
| 86 bool fsVarying() const { return kVertToFrag_Varying == fVarying || | 86 bool fsVarying() const { return kVertToFrag_Varying == fVarying || |
| 87 kGeoToFrag_Varying == fVarying; } | 87 kGeoToFrag_Varying == fVarying; } |
| 88 const char* vsOut() const { return fVsOut; } | 88 const char* vsOut() const { return fVsOut; } |
| 89 const char* gsIn() const { return fGsIn; } | 89 const char* gsIn() const { return fGsIn; } |
| 90 const char* gsOut() const { return fGsOut; } | 90 const char* gsOut() const { return fGsOut; } |
| 91 const char* fsIn() const { return fFsIn; } | 91 const char* fsIn() const { return fFsIn; } |
| 92 GrSLType type() const { return fType; } |
| 92 | 93 |
| 93 protected: | 94 protected: |
| 94 enum Varying { | 95 enum Varying { |
| 95 kVertToFrag_Varying, | 96 kVertToFrag_Varying, |
| 96 kVertToGeo_Varying, | 97 kVertToGeo_Varying, |
| 97 kGeoToFrag_Varying, | 98 kGeoToFrag_Varying, |
| 98 }; | 99 }; |
| 99 | 100 |
| 100 GrGLVarying(GrSLType type, Varying varying) | 101 GrGLVarying(GrSLType type, Varying varying) |
| 101 : fVarying(varying), fType(type), fVsOut(NULL), fGsIn(NULL), fGsOut(NULL
), | 102 : fVarying(varying), fType(type), fVsOut(NULL), fGsIn(NULL), fGsOut(NULL
), |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 | 175 |
| 175 /* a specializations for XPs. Lets the user add uniforms and FS code */ | 176 /* a specializations for XPs. Lets the user add uniforms and FS code */ |
| 176 class GrGLXPBuilder : public virtual GrGLUniformBuilder { | 177 class GrGLXPBuilder : public virtual GrGLUniformBuilder { |
| 177 public: | 178 public: |
| 178 virtual GrGLFPFragmentBuilder* getFragmentShaderBuilder() = 0; | 179 virtual GrGLFPFragmentBuilder* getFragmentShaderBuilder() = 0; |
| 179 | 180 |
| 180 /* | 181 /* |
| 181 * *NOTE* NO MEMBERS ALLOWED, MULTIPLE INHERITANCE | 182 * *NOTE* NO MEMBERS ALLOWED, MULTIPLE INHERITANCE |
| 182 */ | 183 */ |
| 183 }; | 184 }; |
| 184 struct GrGLInstalledProc; | 185 |
| 185 struct GrGLInstalledGeoProc; | 186 /** |
| 186 struct GrGLInstalledXferProc; | 187 * The below struct represent processors installed in programs. |
| 187 struct GrGLInstalledFragProc; | 188 */ |
| 188 struct GrGLInstalledFragProcs; | 189 template <class Proc> |
| 190 struct GrGLInstalledProc { |
| 191 typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
| 192 |
| 193 struct Sampler { |
| 194 SkDEBUGCODE(Sampler() : fTextureUnit(-1) {}) |
| 195 UniformHandle fUniform; |
| 196 int fTextureUnit; |
| 197 }; |
| 198 SkSTArray<4, Sampler, true> fSamplers; |
| 199 SkAutoTDelete<Proc> fGLProc; |
| 200 }; |
| 201 |
| 202 typedef GrGLInstalledProc<GrGLPrimitiveProcessor> GrGLInstalledGeoProc; |
| 203 typedef GrGLInstalledProc<GrGLXferProcessor> GrGLInstalledXferProc; |
| 204 typedef GrGLInstalledProc<GrGLFragmentProcessor> GrGLInstalledFragProc; |
| 205 |
| 206 struct GrGLInstalledFragProcs : public SkRefCnt { |
| 207 virtual ~GrGLInstalledFragProcs(); |
| 208 SkSTArray<8, GrGLInstalledFragProc*, true> fProcs; |
| 209 }; |
| 189 | 210 |
| 190 /* | 211 /* |
| 191 * Please note - no diamond problems because of virtual inheritance. Also, both
base classes | 212 * Please note - no diamond problems because of virtual inheritance. Also, both
base classes |
| 192 * are pure virtual with no data members. This is the base class for program bu
ilding. | 213 * are pure virtual with no data members. This is the base class for program bu
ilding. |
| 193 * Subclasses are nearly identical but each has their own way of emitting transf
orms. State for | 214 * Subclasses are nearly identical but each has their own way of emitting transf
orms. State for |
| 194 * each of the elements of the shader pipeline, ie vertex, fragment, geometry, e
tc, lives in those | 215 * each of the elements of the shader pipeline, ie vertex, fragment, geometry, e
tc, lives in those |
| 195 * respective builders | 216 * respective builders |
| 196 */ | 217 */ |
| 197 class GrGLProgramBuilder : public GrGLGPBuilder, | 218 class GrGLProgramBuilder : public GrGLGPBuilder, |
| 198 public GrGLFPBuilder, | 219 public GrGLFPBuilder, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 // Uniforms for computing texture coords to do the dst-copy lookup | 270 // Uniforms for computing texture coords to do the dst-copy lookup |
| 250 UniformHandle fDstCopyTopLeftUni; | 271 UniformHandle fDstCopyTopLeftUni; |
| 251 UniformHandle fDstCopyScaleUni; | 272 UniformHandle fDstCopyScaleUni; |
| 252 UniformHandle fDstCopySamplerUni; | 273 UniformHandle fDstCopySamplerUni; |
| 253 }; | 274 }; |
| 254 | 275 |
| 255 protected: | 276 protected: |
| 256 typedef GrGLProgramDataManager::UniformInfo UniformInfo; | 277 typedef GrGLProgramDataManager::UniformInfo UniformInfo; |
| 257 typedef GrGLProgramDataManager::UniformInfoArray UniformInfoArray; | 278 typedef GrGLProgramDataManager::UniformInfoArray UniformInfoArray; |
| 258 | 279 |
| 259 static GrGLProgramBuilder* CreateProgramBuilder(const GrOptDrawState&, | 280 static GrGLProgramBuilder* CreateProgramBuilder(const GrOptDrawState&, GrGLG
pu*); |
| 260 bool hasGeometryProcessor, | |
| 261 GrGLGpu*); | |
| 262 | 281 |
| 263 GrGLProgramBuilder(GrGLGpu*, const GrOptDrawState&); | 282 GrGLProgramBuilder(GrGLGpu*, const GrOptDrawState&); |
| 264 | 283 |
| 265 const GrOptDrawState& optState() const { return fOptState; } | 284 const GrOptDrawState& optState() const { return fOptState; } |
| 266 const GrProgramDesc& desc() const { return fDesc; } | 285 const GrProgramDesc& desc() const { return fDesc; } |
| 267 const GrProgramDesc::KeyHeader& header() const { return fDesc.header(); } | 286 const GrProgramDesc::KeyHeader& header() const { return fDesc.header(); } |
| 268 | 287 |
| 269 // Generates a name for a variable. The generated string will be name prefix
ed by the prefix | 288 // Generates a name for a variable. The generated string will be name prefix
ed by the prefix |
| 270 // char (unless the prefix is '\0'). It also mangles the name to be stage-sp
ecific if we're | 289 // char (unless the prefix is '\0'). It also mangles the name to be stage-sp
ecific if we're |
| 271 // generating stage code. | 290 // generating stage code. |
| 272 void nameVariable(SkString* out, char prefix, const char* name); | 291 void nameVariable(SkString* out, char prefix, const char* name); |
| 273 // Generates a possibly mangled name for a stage variable and writes it to t
he fragment shader. | 292 // Generates a possibly mangled name for a stage variable and writes it to t
he fragment shader. |
| 274 // If GrGLSLExpr4 has a valid name then it will use that instead | 293 // If GrGLSLExpr4 has a valid name then it will use that instead |
| 275 void nameExpression(GrGLSLExpr4*, const char* baseName); | 294 void nameExpression(GrGLSLExpr4*, const char* baseName); |
| 276 void emitAndInstallProcs(GrGLSLExpr4* inputColor, | 295 void emitAndInstallProcs(GrGLSLExpr4* inputColor, |
| 277 GrGLSLExpr4* inputCoverage); | 296 GrGLSLExpr4* inputCoverage); |
| 278 void emitAndInstallFragProcs(int procOffset, int numProcs, GrGLSLExpr4* inOu
t); | 297 void emitAndInstallFragProcs(int procOffset, int numProcs, GrGLSLExpr4* inOu
t); |
| 279 void emitAndInstallProc(const GrPendingFragmentStage&, | 298 void emitAndInstallProc(const GrPendingFragmentStage&, |
| 280 int index, | 299 int index, |
| 281 const GrGLSLExpr4& input, | 300 const GrGLSLExpr4& input, |
| 282 GrGLSLExpr4* output); | 301 GrGLSLExpr4* output); |
| 283 | 302 |
| 284 void emitAndInstallProc(const GrPrimitiveProcessor&, | 303 void emitAndInstallProc(const GrPrimitiveProcessor&, |
| 285 GrGLSLExpr4* outputColor, | 304 GrGLSLExpr4* outputColor, |
| 286 GrGLSLExpr4* outputCoverage); | 305 GrGLSLExpr4* outputCoverage); |
| 287 | 306 |
| 288 // these emit functions help to keep the createAndEmitProcessors template ge
neral | 307 // these emit functions help to keep the createAndEmitProcessors template ge
neral |
| 289 void emitAndInstallProc(const GrPendingFragmentStage&, | 308 void emitAndInstallProc(const GrPendingFragmentStage&, |
| 309 int index, |
| 290 const char* outColor, | 310 const char* outColor, |
| 291 const char* inColor); | 311 const char* inColor); |
| 292 void emitAndInstallProc(const GrPrimitiveProcessor&, | 312 void emitAndInstallProc(const GrPrimitiveProcessor&, |
| 293 const char* outColor, | 313 const char* outColor, |
| 294 const char* outCoverage); | 314 const char* outCoverage); |
| 295 void emitAndInstallXferProc(const GrXferProcessor&, | 315 void emitAndInstallXferProc(const GrXferProcessor&, |
| 296 const GrGLSLExpr4& colorIn, | 316 const GrGLSLExpr4& colorIn, |
| 297 const GrGLSLExpr4& coverageIn); | 317 const GrGLSLExpr4& coverageIn); |
| 298 | 318 |
| 299 void verify(const GrPrimitiveProcessor&); | 319 void verify(const GrPrimitiveProcessor&); |
| 300 void verify(const GrXferProcessor&); | 320 void verify(const GrXferProcessor&); |
| 301 void verify(const GrFragmentProcessor&); | 321 void verify(const GrFragmentProcessor&); |
| 322 template <class Proc> |
| 302 void emitSamplers(const GrProcessor&, | 323 void emitSamplers(const GrProcessor&, |
| 303 GrGLProcessor::TextureSamplerArray* outSamplers, | 324 GrGLProcessor::TextureSamplerArray* outSamplers, |
| 304 GrGLInstalledProc*); | 325 GrGLInstalledProc<Proc>*); |
| 305 | 326 |
| 306 // each specific program builder has a distinct transform and must override
this function | |
| 307 virtual void emitTransforms(const GrPendingFragmentStage&, | |
| 308 GrGLProcessor::TransformedCoordsArray* outCoords
, | |
| 309 GrGLInstalledFragProc*); | |
| 310 GrGLProgram* finalize(); | 327 GrGLProgram* finalize(); |
| 311 void bindUniformLocations(GrGLuint programID); | 328 void bindUniformLocations(GrGLuint programID); |
| 312 bool checkLinkStatus(GrGLuint programID); | 329 bool checkLinkStatus(GrGLuint programID); |
| 313 void resolveUniformLocations(GrGLuint programID); | 330 void resolveUniformLocations(GrGLuint programID); |
| 314 void cleanupProgram(GrGLuint programID, const SkTDArray<GrGLuint>& shaderIDs
); | 331 void cleanupProgram(GrGLuint programID, const SkTDArray<GrGLuint>& shaderIDs
); |
| 315 void cleanupShaders(const SkTDArray<GrGLuint>& shaderIDs); | 332 void cleanupShaders(const SkTDArray<GrGLuint>& shaderIDs); |
| 316 | 333 |
| 317 // Subclasses create different programs | 334 // Subclasses create different programs |
| 318 virtual GrGLProgram* createProgram(GrGLuint programID); | 335 virtual GrGLProgram* createProgram(GrGLuint programID); |
| 319 | 336 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 343 public: | 360 public: |
| 344 AutoStageAdvance(GrGLProgramBuilder* pb) : fPB(pb) { fPB->reset(); } | 361 AutoStageAdvance(GrGLProgramBuilder* pb) : fPB(pb) { fPB->reset(); } |
| 345 ~AutoStageAdvance() { fPB->exitStage(); } | 362 ~AutoStageAdvance() { fPB->exitStage(); } |
| 346 private: | 363 private: |
| 347 GrGLProgramBuilder* fPB; | 364 GrGLProgramBuilder* fPB; |
| 348 }; | 365 }; |
| 349 void exitStage() { fOutOfStage = true; } | 366 void exitStage() { fOutOfStage = true; } |
| 350 void enterStage() { fOutOfStage = false; } | 367 void enterStage() { fOutOfStage = false; } |
| 351 int stageIndex() const { return fStageIndex; } | 368 int stageIndex() const { return fStageIndex; } |
| 352 | 369 |
| 353 struct TransformVarying { | |
| 354 TransformVarying(const GrGLVarying& v, const char* uniName, GrCoordSet c
oordSet) | |
| 355 : fV(v), fUniName(uniName), fCoordSet(coordSet) {} | |
| 356 GrGLVarying fV; | |
| 357 SkString fUniName; | |
| 358 GrCoordSet fCoordSet; | |
| 359 }; | |
| 360 | |
| 361 const char* rtAdjustment() const { return "rtAdjustment"; } | 370 const char* rtAdjustment() const { return "rtAdjustment"; } |
| 362 | 371 |
| 363 // number of each input/output type in a single allocation block, used by ma
ny builders | 372 // number of each input/output type in a single allocation block, used by ma
ny builders |
| 364 static const int kVarsPerBlock; | 373 static const int kVarsPerBlock; |
| 365 | 374 |
| 366 BuiltinUniformHandles fUniformHandles; | 375 BuiltinUniformHandles fUniformHandles; |
| 367 GrGLVertexBuilder fVS; | 376 GrGLVertexBuilder fVS; |
| 368 GrGLGeometryBuilder fGS; | 377 GrGLGeometryBuilder fGS; |
| 369 GrGLFragmentShaderBuilder fFS; | 378 GrGLFragmentShaderBuilder fFS; |
| 370 bool fOutOfStage; | 379 bool fOutOfStage; |
| 371 int fStageIndex; | 380 int fStageIndex; |
| 372 | 381 |
| 373 GrGLInstalledGeoProc* fGeometryProcessor; | 382 GrGLInstalledGeoProc* fGeometryProcessor; |
| 374 GrGLInstalledXferProc* fXferProcessor; | 383 GrGLInstalledXferProc* fXferProcessor; |
| 375 SkAutoTUnref<GrGLInstalledFragProcs> fFragmentProcessors; | 384 SkAutoTUnref<GrGLInstalledFragProcs> fFragmentProcessors; |
| 376 | 385 |
| 377 const GrOptDrawState& fOptState; | 386 const GrOptDrawState& fOptState; |
| 378 const GrProgramDesc& fDesc; | 387 const GrProgramDesc& fDesc; |
| 379 GrGLGpu* fGpu; | 388 GrGLGpu* fGpu; |
| 380 UniformInfoArray fUniforms; | 389 UniformInfoArray fUniforms; |
| 381 SkSTArray<16, TransformVarying, true> fCoordVaryings; | 390 GrGLPrimitiveProcessor::TransformsIn fCoordTransforms; |
| 391 GrGLPrimitiveProcessor::TransformsOut fOutCoords; |
| 382 | 392 |
| 383 friend class GrGLShaderBuilder; | 393 friend class GrGLShaderBuilder; |
| 384 friend class GrGLVertexBuilder; | 394 friend class GrGLVertexBuilder; |
| 385 friend class GrGLFragmentShaderBuilder; | 395 friend class GrGLFragmentShaderBuilder; |
| 386 friend class GrGLGeometryBuilder; | 396 friend class GrGLGeometryBuilder; |
| 387 }; | 397 }; |
| 388 | |
| 389 /** | |
| 390 * The below structs represent processors installed in programs. All processors
can have texture | |
| 391 * samplers, but only frag processors have coord transforms, hence the need for
different structs | |
| 392 */ | |
| 393 struct GrGLInstalledProc { | |
| 394 typedef GrGLProgramDataManager::UniformHandle UniformHandle; | |
| 395 | |
| 396 struct Sampler { | |
| 397 SkDEBUGCODE(Sampler() : fTextureUnit(-1) {}) | |
| 398 UniformHandle fUniform; | |
| 399 int fTextureUnit; | |
| 400 }; | |
| 401 SkSTArray<4, Sampler, true> fSamplers; | |
| 402 }; | |
| 403 | |
| 404 struct GrGLInstalledGeoProc : public GrGLInstalledProc { | |
| 405 SkAutoTDelete<GrGLGeometryProcessor> fGLProc; | |
| 406 }; | |
| 407 | |
| 408 struct GrGLInstalledXferProc : public GrGLInstalledProc { | |
| 409 SkAutoTDelete<GrGLXferProcessor> fGLProc; | |
| 410 }; | |
| 411 | |
| 412 struct GrGLInstalledFragProc : public GrGLInstalledProc { | |
| 413 GrGLInstalledFragProc() : fGLProc(NULL) {} | |
| 414 class ShaderVarHandle { | |
| 415 public: | |
| 416 bool isValid() const { return fHandle > -1; } | |
| 417 ShaderVarHandle() : fHandle(-1) {} | |
| 418 ShaderVarHandle(int value) : fHandle(value) { SkASSERT(this->isValid());
} | |
| 419 int handle() const { SkASSERT(this->isValid()); return fHandle; } | |
| 420 UniformHandle convertToUniformHandle() { | |
| 421 SkASSERT(this->isValid()); | |
| 422 return GrGLProgramDataManager::UniformHandle::CreateFromUniformIndex
(fHandle); | |
| 423 } | |
| 424 | |
| 425 private: | |
| 426 int fHandle; | |
| 427 }; | |
| 428 | |
| 429 struct Transform { | |
| 430 Transform() : fType(kVoid_GrSLType) { fCurrentValue = SkMatrix::InvalidM
atrix(); } | |
| 431 ShaderVarHandle fHandle; | |
| 432 SkMatrix fCurrentValue; | |
| 433 GrSLType fType; | |
| 434 }; | |
| 435 | |
| 436 SkAutoTDelete<GrGLFragmentProcessor> fGLProc; | |
| 437 SkSTArray<2, Transform, true> fTransforms; | |
| 438 }; | |
| 439 | |
| 440 struct GrGLInstalledFragProcs : public SkRefCnt { | |
| 441 virtual ~GrGLInstalledFragProcs(); | |
| 442 SkSTArray<8, GrGLInstalledFragProc*, true> fProcs; | |
| 443 }; | |
| 444 | |
| 445 #endif | 398 #endif |
| OLD | NEW |