Chromium Code Reviews| 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 #include "GrGLProgram.h" | 8 #include "GrGLProgram.h" |
| 9 | 9 |
| 10 #include "GrAllocator.h" | 10 #include "GrAllocator.h" |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 41 : fGpu(gpu) | 41 : fGpu(gpu) |
| 42 , fUniformManager(gpu) { | 42 , fUniformManager(gpu) { |
| 43 fDesc = desc; | 43 fDesc = desc; |
| 44 fProgramID = 0; | 44 fProgramID = 0; |
| 45 | 45 |
| 46 fDstCopyTexUnit = -1; | 46 fDstCopyTexUnit = -1; |
| 47 | 47 |
| 48 fColor = GrColor_ILLEGAL; | 48 fColor = GrColor_ILLEGAL; |
| 49 fColorFilterColor = GrColor_ILLEGAL; | 49 fColorFilterColor = GrColor_ILLEGAL; |
| 50 | 50 |
| 51 fColorEffects.reset(desc.numColorEffects()); | |
| 52 fCoverageEffects.reset(desc.numCoverageEffects()); | |
| 53 | |
| 54 this->genProgram(colorStages, coverageStages); | 51 this->genProgram(colorStages, coverageStages); |
| 55 } | 52 } |
| 56 | 53 |
| 57 GrGLProgram::~GrGLProgram() { | 54 GrGLProgram::~GrGLProgram() { |
| 58 if (fProgramID) { | 55 if (fProgramID) { |
| 59 GL_CALL(DeleteProgram(fProgramID)); | 56 GL_CALL(DeleteProgram(fProgramID)); |
| 60 } | 57 } |
| 61 } | 58 } |
| 62 | 59 |
| 63 void GrGLProgram::abandon() { | 60 void GrGLProgram::abandon() { |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 228 // Get the coeffs for the Mode-based color filter, determine if color is nee ded. | 225 // Get the coeffs for the Mode-based color filter, determine if color is nee ded. |
| 229 SkXfermode::Coeff colorCoeff; | 226 SkXfermode::Coeff colorCoeff; |
| 230 SkXfermode::Coeff filterColorCoeff; | 227 SkXfermode::Coeff filterColorCoeff; |
| 231 SkAssertResult( | 228 SkAssertResult( |
| 232 SkXfermode::ModeAsCoeff(header.fColorFilterXfermode, | 229 SkXfermode::ModeAsCoeff(header.fColorFilterXfermode, |
| 233 &filterColorCoeff, | 230 &filterColorCoeff, |
| 234 &colorCoeff)); | 231 &colorCoeff)); |
| 235 bool needColor, needFilterColor; | 232 bool needColor, needFilterColor; |
| 236 need_blend_inputs(filterColorCoeff, colorCoeff, &needFilterColor, &needColor ); | 233 need_blend_inputs(filterColorCoeff, colorCoeff, &needFilterColor, &needColor ); |
| 237 | 234 |
| 238 // used in order for builder to return the per-stage uniform handles. | 235 fColorEffects.reset(builder.emitEffects(colorStages, |
|
bsalomon
2013/10/02 20:17:06
this is nice
| |
| 239 typedef SkTArray<GrGLCoordTransform, false>* CoordTransformArrayPtr; | 236 fDesc.effectKeys(), |
| 240 typedef SkTArray<GrGLUniformManager::UniformHandle, true>* UniHandleArrayPtr ; | 237 needColor ? fDesc.numColorEffects() : 0, |
| 241 int maxColorOrCovEffectCnt = GrMax(fDesc.numColorEffects(), fDesc.numCoverag eEffects()); | 238 &inColor, |
| 242 SkAutoTArray<CoordTransformArrayPtr> effectCoordTransformArrays(maxColorOrCo vEffectCnt); | 239 &knownColorValue)); |
| 243 SkAutoTArray<UniHandleArrayPtr> effectUniformArrays(maxColorOrCovEffectCnt); | |
| 244 SkAutoTArray<GrGLEffect*> glEffects(maxColorOrCovEffectCnt); | |
| 245 | |
| 246 if (needColor) { | |
| 247 for (int e = 0; e < fDesc.numColorEffects(); ++e) { | |
| 248 effectCoordTransformArrays[e] = &fColorEffects[e].fCoordTransforms; | |
| 249 effectUniformArrays[e] = &fColorEffects[e].fSamplerUnis; | |
| 250 } | |
| 251 | |
| 252 builder.emitEffects(colorStages, | |
| 253 fDesc.effectKeys(), | |
| 254 fDesc.numColorEffects(), | |
| 255 &inColor, | |
| 256 &knownColorValue, | |
| 257 effectCoordTransformArrays.get(), | |
| 258 effectUniformArrays.get(), | |
| 259 glEffects.get()); | |
| 260 | |
| 261 for (int e = 0; e < fDesc.numColorEffects(); ++e) { | |
| 262 fColorEffects[e].fGLEffect = glEffects[e]; | |
| 263 } | |
| 264 } | |
| 265 | 240 |
| 266 // Insert the color filter. This will soon be replaced by a color effect. | 241 // Insert the color filter. This will soon be replaced by a color effect. |
| 267 if (SkXfermode::kDst_Mode != header.fColorFilterXfermode) { | 242 if (SkXfermode::kDst_Mode != header.fColorFilterXfermode) { |
| 268 const char* colorFilterColorUniName = NULL; | 243 const char* colorFilterColorUniName = NULL; |
| 269 fUniformHandles.fColorFilterUni = builder.addUniform(GrGLShaderBuilder:: kFragment_Visibility, | 244 fUniformHandles.fColorFilterUni = builder.addUniform(GrGLShaderBuilder:: kFragment_Visibility, |
| 270 kVec4f_GrSLType, "F ilterColor", | 245 kVec4f_GrSLType, "F ilterColor", |
| 271 &colorFilterColorUn iName); | 246 &colorFilterColorUn iName); |
| 272 | 247 |
| 273 builder.fsCodeAppend("\tvec4 filteredColor;\n"); | 248 builder.fsCodeAppend("\tvec4 filteredColor;\n"); |
| 274 const char* color; | 249 const char* color; |
| 275 // add_color_filter requires a real input string. | 250 // add_color_filter requires a real input string. |
| 276 if (knownColorValue == kOnes_GrSLConstantVec) { | 251 if (knownColorValue == kOnes_GrSLConstantVec) { |
| 277 color = GrGLSLOnesVecf(4); | 252 color = GrGLSLOnesVecf(4); |
| 278 } else if (knownColorValue == kZeros_GrSLConstantVec) { | 253 } else if (knownColorValue == kZeros_GrSLConstantVec) { |
| 279 color = GrGLSLZerosVecf(4); | 254 color = GrGLSLZerosVecf(4); |
| 280 } else { | 255 } else { |
| 281 color = inColor.c_str(); | 256 color = inColor.c_str(); |
| 282 } | 257 } |
| 283 add_color_filter(&builder, "filteredColor", filterColorCoeff, | 258 add_color_filter(&builder, "filteredColor", filterColorCoeff, |
| 284 colorCoeff, colorFilterColorUniName, color); | 259 colorCoeff, colorFilterColorUniName, color); |
| 285 inColor = "filteredColor"; | 260 inColor = "filteredColor"; |
| 286 } | 261 } |
| 287 | 262 |
| 288 /////////////////////////////////////////////////////////////////////////// | 263 /////////////////////////////////////////////////////////////////////////// |
| 289 // compute the partial coverage | 264 // compute the partial coverage |
| 290 SkString inCoverage = builder.getInputCoverage(); | 265 SkString inCoverage = builder.getInputCoverage(); |
| 291 GrSLConstantVec knownCoverageValue = builder.getKnownCoverageValue(); | 266 GrSLConstantVec knownCoverageValue = builder.getKnownCoverageValue(); |
| 292 | 267 |
| 293 for (int e = 0; e < fDesc.numCoverageEffects(); ++e) { | 268 fCoverageEffects.reset(builder.emitEffects(coverageStages, |
| 294 effectCoordTransformArrays[e] = &fCoverageEffects[e].fCoordTransforms; | 269 fDesc.getEffectKeys() + fDesc.num ColorEffects(), |
| 295 effectUniformArrays[e] = &fCoverageEffects[e].fSamplerUnis; | 270 fDesc.numCoverageEffects(), |
| 296 } | 271 &inCoverage, |
| 297 | 272 &knownCoverageValue)); |
| 298 builder.emitEffects(coverageStages, | |
| 299 fDesc.getEffectKeys() + fDesc.numColorEffects(), | |
| 300 fDesc.numCoverageEffects(), | |
| 301 &inCoverage, | |
| 302 &knownCoverageValue, | |
| 303 effectCoordTransformArrays.get(), | |
| 304 effectUniformArrays.get(), | |
| 305 glEffects.get()); | |
| 306 for (int e = 0; e < fDesc.numCoverageEffects(); ++e) { | |
| 307 fCoverageEffects[e].fGLEffect = glEffects[e]; | |
| 308 } | |
| 309 | 273 |
| 310 // discard if coverage is zero | 274 // discard if coverage is zero |
| 311 if (header.fDiscardIfZeroCoverage && kOnes_GrSLConstantVec != knownCoverageV alue) { | 275 if (header.fDiscardIfZeroCoverage && kOnes_GrSLConstantVec != knownCoverageV alue) { |
| 312 if (kZeros_GrSLConstantVec == knownCoverageValue) { | 276 if (kZeros_GrSLConstantVec == knownCoverageValue) { |
| 313 // This is unfortunate. | 277 // This is unfortunate. |
| 314 builder.fsCodeAppend("\tdiscard;\n"); | 278 builder.fsCodeAppend("\tdiscard;\n"); |
| 315 } else { | 279 } else { |
| 316 builder.fsCodeAppendf("\tif (all(lessThanEqual(%s, vec4(0.0)))) {\n\ t\tdiscard;\n\t}\n", | 280 builder.fsCodeAppendf("\tif (all(lessThanEqual(%s, vec4(0.0)))) {\n\ t\tdiscard;\n\t}\n", |
| 317 inCoverage.c_str()); | 281 inCoverage.c_str()); |
| 318 } | 282 } |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 414 return true; | 378 return true; |
| 415 } | 379 } |
| 416 | 380 |
| 417 void GrGLProgram::initSamplerUniforms() { | 381 void GrGLProgram::initSamplerUniforms() { |
| 418 GL_CALL(UseProgram(fProgramID)); | 382 GL_CALL(UseProgram(fProgramID)); |
| 419 GrGLint texUnitIdx = 0; | 383 GrGLint texUnitIdx = 0; |
| 420 if (fUniformHandles.fDstCopySamplerUni.isValid()) { | 384 if (fUniformHandles.fDstCopySamplerUni.isValid()) { |
| 421 fUniformManager.setSampler(fUniformHandles.fDstCopySamplerUni, texUnitId x); | 385 fUniformManager.setSampler(fUniformHandles.fDstCopySamplerUni, texUnitId x); |
| 422 fDstCopyTexUnit = texUnitIdx++; | 386 fDstCopyTexUnit = texUnitIdx++; |
| 423 } | 387 } |
| 424 | 388 fColorEffects->initSamplers(fUniformManager, &texUnitIdx); |
| 425 for (int e = 0; e < fColorEffects.count(); ++e) { | 389 fCoverageEffects->initSamplers(fUniformManager, &texUnitIdx); |
|
Chris Dalton
2013/10/02 19:57:32
I considered making these null when empty, instead
| |
| 426 this->initEffectSamplerUniforms(&fColorEffects[e], &texUnitIdx); | |
| 427 } | |
| 428 | |
| 429 for (int e = 0; e < fCoverageEffects.count(); ++e) { | |
| 430 this->initEffectSamplerUniforms(&fCoverageEffects[e], &texUnitIdx); | |
| 431 } | |
| 432 } | |
| 433 | |
| 434 void GrGLProgram::initEffectSamplerUniforms(EffectAndSamplers* effect, int* texU nitIdx) { | |
| 435 int numSamplers = effect->fSamplerUnis.count(); | |
| 436 effect->fTextureUnits.reset(numSamplers); | |
| 437 for (int s = 0; s < numSamplers; ++s) { | |
| 438 UniformHandle handle = effect->fSamplerUnis[s]; | |
| 439 if (handle.isValid()) { | |
| 440 fUniformManager.setSampler(handle, *texUnitIdx); | |
| 441 effect->fTextureUnits[s] = (*texUnitIdx)++; | |
| 442 } | |
| 443 } | |
| 444 } | 390 } |
| 445 | 391 |
| 446 /////////////////////////////////////////////////////////////////////////////// | 392 /////////////////////////////////////////////////////////////////////////////// |
| 447 | 393 |
| 448 void GrGLProgram::setEffectData(const GrEffectStage& stage, | |
| 449 EffectAndSamplers& effect) { | |
| 450 | |
| 451 // Let the GrGLEffect set its data. | |
| 452 bool explicitLocalCoords = -1 != fDesc.getHeader().fLocalCoordAttributeIndex ; | |
| 453 GrDrawEffect drawEffect(stage, explicitLocalCoords); | |
| 454 effect.fGLEffect->setData(fUniformManager, drawEffect); | |
| 455 | |
| 456 // Set the effect's coord transform matrices. | |
| 457 int numTransforms = effect.fCoordTransforms.count(); | |
| 458 SkASSERT((*stage.getEffect())->numTransforms() == numTransforms); | |
| 459 for (int c = 0; c < numTransforms; ++c) { | |
| 460 effect.fCoordTransforms[c].setData(fUniformManager, drawEffect, c); | |
| 461 } | |
| 462 | |
| 463 // Bind the texures for the effect. | |
| 464 int numSamplers = effect.fSamplerUnis.count(); | |
| 465 SkASSERT((*stage.getEffect())->numTextures() == numSamplers); | |
| 466 for (int s = 0; s < numSamplers; ++s) { | |
| 467 UniformHandle handle = effect.fSamplerUnis[s]; | |
| 468 if (handle.isValid()) { | |
| 469 const GrTextureAccess& access = (*stage.getEffect())->textureAccess( s); | |
| 470 GrGLTexture* texture = static_cast<GrGLTexture*>(access.getTexture() ); | |
| 471 int unit = effect.fTextureUnits[s]; | |
| 472 fGpu->bindTexture(unit, access.getParams(), texture); | |
| 473 } | |
| 474 } | |
| 475 } | |
| 476 | |
| 477 void GrGLProgram::setData(GrDrawState::BlendOptFlags blendOpts, | 394 void GrGLProgram::setData(GrDrawState::BlendOptFlags blendOpts, |
| 478 const GrEffectStage* colorStages[], | 395 const GrEffectStage* colorStages[], |
| 479 const GrEffectStage* coverageStages[], | 396 const GrEffectStage* coverageStages[], |
| 480 const GrDeviceCoordTexture* dstCopy, | 397 const GrDeviceCoordTexture* dstCopy, |
| 481 SharedGLState* sharedState) { | 398 SharedGLState* sharedState) { |
| 482 const GrDrawState& drawState = fGpu->getDrawState(); | 399 const GrDrawState& drawState = fGpu->getDrawState(); |
| 483 | 400 |
| 484 GrColor color; | 401 GrColor color; |
| 485 GrColor coverage; | 402 GrColor coverage; |
| 486 if (blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag) { | 403 if (blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 521 } else { | 438 } else { |
| 522 SkASSERT(!fUniformHandles.fDstCopyScaleUni.isValid()); | 439 SkASSERT(!fUniformHandles.fDstCopyScaleUni.isValid()); |
| 523 SkASSERT(!fUniformHandles.fDstCopySamplerUni.isValid()); | 440 SkASSERT(!fUniformHandles.fDstCopySamplerUni.isValid()); |
| 524 } | 441 } |
| 525 } else { | 442 } else { |
| 526 SkASSERT(!fUniformHandles.fDstCopyTopLeftUni.isValid()); | 443 SkASSERT(!fUniformHandles.fDstCopyTopLeftUni.isValid()); |
| 527 SkASSERT(!fUniformHandles.fDstCopyScaleUni.isValid()); | 444 SkASSERT(!fUniformHandles.fDstCopyScaleUni.isValid()); |
| 528 SkASSERT(!fUniformHandles.fDstCopySamplerUni.isValid()); | 445 SkASSERT(!fUniformHandles.fDstCopySamplerUni.isValid()); |
| 529 } | 446 } |
| 530 | 447 |
| 531 for (int e = 0; e < fColorEffects.count(); ++e) { | 448 fColorEffects->setData(fGpu, fUniformManager, colorStages); |
| 532 // We may have omitted the GrGLEffect because of the color filter logic in genProgram. | 449 fCoverageEffects->setData(fGpu, fUniformManager, coverageStages); |
| 533 // This can be removed when the color filter is an effect. | |
| 534 if (NULL != fColorEffects[e].fGLEffect) { | |
| 535 this->setEffectData(*colorStages[e], fColorEffects[e]); | |
| 536 } | |
| 537 } | |
| 538 | |
| 539 for (int e = 0; e < fCoverageEffects.count(); ++e) { | |
| 540 if (NULL != fCoverageEffects[e].fGLEffect) { | |
| 541 this->setEffectData(*coverageStages[e], fCoverageEffects[e]); | |
| 542 } | |
| 543 } | |
| 544 } | 450 } |
| 545 | 451 |
| 546 void GrGLProgram::setColor(const GrDrawState& drawState, | 452 void GrGLProgram::setColor(const GrDrawState& drawState, |
| 547 GrColor color, | 453 GrColor color, |
| 548 SharedGLState* sharedState) { | 454 SharedGLState* sharedState) { |
| 549 const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); | 455 const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader(); |
| 550 if (!drawState.hasColorVertexAttribute()) { | 456 if (!drawState.hasColorVertexAttribute()) { |
| 551 switch (header.fColorInput) { | 457 switch (header.fColorInput) { |
| 552 case GrGLProgramDesc::kAttribute_ColorInput: | 458 case GrGLProgramDesc::kAttribute_ColorInput: |
| 553 SkASSERT(-1 != header.fColorAttributeIndex); | 459 SkASSERT(-1 != header.fColorAttributeIndex); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 639 | 545 |
| 640 fMatrixState.fViewMatrix = drawState.getViewMatrix(); | 546 fMatrixState.fViewMatrix = drawState.getViewMatrix(); |
| 641 fMatrixState.fRenderTargetSize = size; | 547 fMatrixState.fRenderTargetSize = size; |
| 642 fMatrixState.fRenderTargetOrigin = rt->origin(); | 548 fMatrixState.fRenderTargetOrigin = rt->origin(); |
| 643 | 549 |
| 644 GrGLfloat viewMatrix[3 * 3]; | 550 GrGLfloat viewMatrix[3 * 3]; |
| 645 fMatrixState.getGLMatrix<3>(viewMatrix); | 551 fMatrixState.getGLMatrix<3>(viewMatrix); |
| 646 fUniformManager.setMatrix3f(fUniformHandles.fViewMatrixUni, viewMatrix); | 552 fUniformManager.setMatrix3f(fUniformHandles.fViewMatrixUni, viewMatrix); |
| 647 } | 553 } |
| 648 } | 554 } |
| OLD | NEW |