| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 "SkDisplacementMapEffect.h" | 8 #include "SkDisplacementMapEffect.h" |
| 9 #include "SkFlattenableBuffers.h" | 9 #include "SkFlattenableBuffers.h" |
| 10 #include "SkUnPreMultiply.h" | 10 #include "SkUnPreMultiply.h" |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 | 206 |
| 207 virtual void emitCode(GrGLShaderBuilder*, | 207 virtual void emitCode(GrGLShaderBuilder*, |
| 208 const GrDrawEffect&, | 208 const GrDrawEffect&, |
| 209 EffectKey, | 209 EffectKey, |
| 210 const char* outputColor, | 210 const char* outputColor, |
| 211 const char* inputColor, | 211 const char* inputColor, |
| 212 const TextureSamplerArray&) SK_OVERRIDE; | 212 const TextureSamplerArray&) SK_OVERRIDE; |
| 213 | 213 |
| 214 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); | 214 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
| 215 | 215 |
| 216 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; | 216 virtual void setData(const GrGLContext&, const GrDrawEffect&) SK_OVERRIDE; |
| 217 | 217 |
| 218 private: | 218 private: |
| 219 static const GrEffect::CoordsType kCoordsType = GrEffect::kLocal_CoordsType; | 219 static const GrEffect::CoordsType kCoordsType = GrEffect::kLocal_CoordsType; |
| 220 | 220 |
| 221 SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector; | 221 SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector; |
| 222 SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector; | 222 SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector; |
| 223 GrGLEffectMatrix fDisplacementEffectMatrix; | 223 GrGLEffectMatrix fDisplacementEffectMatrix; |
| 224 GrGLEffectMatrix fColorEffectMatrix; | 224 GrGLEffectMatrix fColorEffectMatrix; |
| 225 GrGLUniformManager::UniformHandle fScaleUni; | 225 GrGLUniform* fScaleUni; |
| 226 | 226 |
| 227 typedef GrGLEffect INHERITED; | 227 typedef GrGLEffect INHERITED; |
| 228 }; | 228 }; |
| 229 | 229 |
| 230 /////////////////////////////////////////////////////////////////////////////// | 230 /////////////////////////////////////////////////////////////////////////////// |
| 231 | 231 |
| 232 class GrDisplacementMapEffect : public GrEffect { | 232 class GrDisplacementMapEffect : public GrEffect { |
| 233 public: | 233 public: |
| 234 static GrEffectRef* Create(SkDisplacementMapEffect::ChannelSelectorType xCha
nnelSelector, | 234 static GrEffectRef* Create(SkDisplacementMapEffect::ChannelSelectorType xCha
nnelSelector, |
| 235 SkDisplacementMapEffect::ChannelSelectorType yCha
nnelSelector, | 235 SkDisplacementMapEffect::ChannelSelectorType yCha
nnelSelector, |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 } | 402 } |
| 403 | 403 |
| 404 void GrGLDisplacementMapEffect::emitCode(GrGLShaderBuilder* builder, | 404 void GrGLDisplacementMapEffect::emitCode(GrGLShaderBuilder* builder, |
| 405 const GrDrawEffect&, | 405 const GrDrawEffect&, |
| 406 EffectKey key, | 406 EffectKey key, |
| 407 const char* outputColor, | 407 const char* outputColor, |
| 408 const char* inputColor, | 408 const char* inputColor, |
| 409 const TextureSamplerArray& samplers) { | 409 const TextureSamplerArray& samplers) { |
| 410 sk_ignore_unused_variable(inputColor); | 410 sk_ignore_unused_variable(inputColor); |
| 411 | 411 |
| 412 fScaleUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, | 412 GrGLShaderBuilder::Uniform* scaleUni = |
| 413 kVec2f_GrSLType, "Scale"); | 413 builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, kVec2f_GrSL
Type, "Scale"); |
| 414 const char* scaleUni = builder->getUniformCStr(fScaleUni); | 414 fScaleUni = scaleUni->glUniform(); |
| 415 | 415 |
| 416 const char* dCoordsIn; | 416 const char* dCoordsIn; |
| 417 GrSLType dCoordsType = fDisplacementEffectMatrix.emitCode( | 417 GrSLType dCoordsType = fDisplacementEffectMatrix.emitCode( |
| 418 builder, key, &dCoordsIn, NULL, "DISPL"); | 418 builder, key, &dCoordsIn, NULL, "DISPL"); |
| 419 const char* cCoordsIn; | 419 const char* cCoordsIn; |
| 420 GrSLType cCoordsType = fColorEffectMatrix.emitCode( | 420 GrSLType cCoordsType = fColorEffectMatrix.emitCode( |
| 421 builder, key, &cCoordsIn, NULL, "COLOR"); | 421 builder, key, &cCoordsIn, NULL, "COLOR"); |
| 422 | 422 |
| 423 const char* dColor = "dColor"; | 423 const char* dColor = "dColor"; |
| 424 const char* cCoords = "cCoords"; | 424 const char* cCoords = "cCoords"; |
| 425 const char* outOfBounds = "outOfBounds"; | 425 const char* outOfBounds = "outOfBounds"; |
| 426 const char* nearZero = "1e-6"; // Since 6.10352e−5 is the smallest half floa
t, use | 426 const char* nearZero = "1e-6"; // Since 6.10352e−5 is the smallest half floa
t, use |
| 427 // a number smaller than that to approximate
0, but | 427 // a number smaller than that to approximate
0, but |
| 428 // leave room for 32-bit float GPU rounding e
rrors. | 428 // leave room for 32-bit float GPU rounding e
rrors. |
| 429 | 429 |
| 430 builder->fsCodeAppendf("\t\tvec4 %s = ", dColor); | 430 builder->fsCodeAppendf("\t\tvec4 %s = ", dColor); |
| 431 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, | 431 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, |
| 432 samplers[0], | 432 samplers[0], |
| 433 dCoordsIn, | 433 dCoordsIn, |
| 434 dCoordsType); | 434 dCoordsType); |
| 435 builder->fsCodeAppend(";\n"); | 435 builder->fsCodeAppend(";\n"); |
| 436 | 436 |
| 437 // Unpremultiply the displacement | 437 // Unpremultiply the displacement |
| 438 builder->fsCodeAppendf("\t\t%s.rgb = (%s.a < %s) ? vec3(0.0) : clamp(%s.rgb
/ %s.a, 0.0, 1.0);", | 438 builder->fsCodeAppendf("\t\t%s.rgb = (%s.a < %s) ? vec3(0.0) : clamp(%s.rgb
/ %s.a, 0.0, 1.0);", |
| 439 dColor, dColor, nearZero, dColor, dColor); | 439 dColor, dColor, nearZero, dColor, dColor); |
| 440 | 440 |
| 441 builder->fsCodeAppendf("\t\tvec2 %s = %s + %s*(%s.", | 441 builder->fsCodeAppendf("\t\tvec2 %s = %s + %s*(%s.", |
| 442 cCoords, cCoordsIn, scaleUni, dColor); | 442 cCoords, cCoordsIn, scaleUni->c_str(), dColor); |
| 443 | 443 |
| 444 switch (fXChannelSelector) { | 444 switch (fXChannelSelector) { |
| 445 case SkDisplacementMapEffect::kR_ChannelSelectorType: | 445 case SkDisplacementMapEffect::kR_ChannelSelectorType: |
| 446 builder->fsCodeAppend("r"); | 446 builder->fsCodeAppend("r"); |
| 447 break; | 447 break; |
| 448 case SkDisplacementMapEffect::kG_ChannelSelectorType: | 448 case SkDisplacementMapEffect::kG_ChannelSelectorType: |
| 449 builder->fsCodeAppend("g"); | 449 builder->fsCodeAppend("g"); |
| 450 break; | 450 break; |
| 451 case SkDisplacementMapEffect::kB_ChannelSelectorType: | 451 case SkDisplacementMapEffect::kB_ChannelSelectorType: |
| 452 builder->fsCodeAppend("b"); | 452 builder->fsCodeAppend("b"); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 "bool %s = (%s.x < 0.0) || (%s.y < 0.0) || (%s.x > 1.0) || (%s.y > 1.0);
\t\t", | 484 "bool %s = (%s.x < 0.0) || (%s.y < 0.0) || (%s.x > 1.0) || (%s.y > 1.0);
\t\t", |
| 485 outOfBounds, cCoords, cCoords, cCoords, cCoords); | 485 outOfBounds, cCoords, cCoords, cCoords, cCoords); |
| 486 builder->fsCodeAppendf("%s = %s ? vec4(0.0) : ", outputColor, outOfBounds); | 486 builder->fsCodeAppendf("%s = %s ? vec4(0.0) : ", outputColor, outOfBounds); |
| 487 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, | 487 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, |
| 488 samplers[1], | 488 samplers[1], |
| 489 cCoords, | 489 cCoords, |
| 490 cCoordsType); | 490 cCoordsType); |
| 491 builder->fsCodeAppend(";\n"); | 491 builder->fsCodeAppend(";\n"); |
| 492 } | 492 } |
| 493 | 493 |
| 494 void GrGLDisplacementMapEffect::setData(const GrGLUniformManager& uman, | 494 void GrGLDisplacementMapEffect::setData(const GrGLContext& context, |
| 495 const GrDrawEffect& drawEffect) { | 495 const GrDrawEffect& drawEffect) { |
| 496 const GrDisplacementMapEffect& displacementMap = | 496 const GrDisplacementMapEffect& displacementMap = |
| 497 drawEffect.castEffect<GrDisplacementMapEffect>(); | 497 drawEffect.castEffect<GrDisplacementMapEffect>(); |
| 498 GrTexture* displTex = displacementMap.texture(0); | 498 GrTexture* displTex = displacementMap.texture(0); |
| 499 GrTexture* colorTex = displacementMap.texture(1); | 499 GrTexture* colorTex = displacementMap.texture(1); |
| 500 fDisplacementEffectMatrix.setData(uman, | 500 fDisplacementEffectMatrix.setData(context, |
| 501 GrEffect::MakeDivByTextureWHMatrix(displTex
), | 501 GrEffect::MakeDivByTextureWHMatrix(displTex
), |
| 502 drawEffect, | 502 drawEffect, |
| 503 displTex); | 503 displTex); |
| 504 fColorEffectMatrix.setData(uman, | 504 fColorEffectMatrix.setData(context, |
| 505 GrEffect::MakeDivByTextureWHMatrix(colorTex), | 505 GrEffect::MakeDivByTextureWHMatrix(colorTex), |
| 506 drawEffect, | 506 drawEffect, |
| 507 colorTex); | 507 colorTex); |
| 508 | 508 |
| 509 SkScalar scaleX = SkScalarDiv(displacementMap.scale(), SkIntToScalar(colorTe
x->width())); | 509 SkScalar scaleX = SkScalarDiv(displacementMap.scale(), SkIntToScalar(colorTe
x->width())); |
| 510 SkScalar scaleY = SkScalarDiv(displacementMap.scale(), SkIntToScalar(colorTe
x->height())); | 510 SkScalar scaleY = SkScalarDiv(displacementMap.scale(), SkIntToScalar(colorTe
x->height())); |
| 511 uman.set2f(fScaleUni, SkScalarToFloat(scaleX), | 511 fScaleUni->set2f(context, SkScalarToFloat(scaleX), |
| 512 colorTex->origin() == kTopLeft_GrSurfaceOrigin ? | 512 colorTex->origin() == kTopLeft_GrSurfaceOrigin ? |
| 513 SkScalarToFloat(scaleY) : SkScalarToFloat(-scaleY)); | 513 SkScalarToFloat(scaleY) : SkScalarToFloat(-scaleY)); |
| 514 } | 514 } |
| 515 | 515 |
| 516 GrGLEffect::EffectKey GrGLDisplacementMapEffect::GenKey(const GrDrawEffect& draw
Effect, | 516 GrGLEffect::EffectKey GrGLDisplacementMapEffect::GenKey(const GrDrawEffect& draw
Effect, |
| 517 const GrGLCaps&) { | 517 const GrGLCaps&) { |
| 518 const GrDisplacementMapEffect& displacementMap = | 518 const GrDisplacementMapEffect& displacementMap = |
| 519 drawEffect.castEffect<GrDisplacementMapEffect>(); | 519 drawEffect.castEffect<GrDisplacementMapEffect>(); |
| 520 | 520 |
| 521 GrTexture* displTex = displacementMap.texture(0); | 521 GrTexture* displTex = displacementMap.texture(0); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 532 colorTex); | 532 colorTex); |
| 533 | 533 |
| 534 colorKey <<= GrGLEffectMatrix::kKeyBits; | 534 colorKey <<= GrGLEffectMatrix::kKeyBits; |
| 535 EffectKey xKey = displacementMap.xChannelSelector() << (2 * GrGLEffectMatrix
::kKeyBits); | 535 EffectKey xKey = displacementMap.xChannelSelector() << (2 * GrGLEffectMatrix
::kKeyBits); |
| 536 EffectKey yKey = displacementMap.yChannelSelector() << (2 * GrGLEffectMatrix
::kKeyBits + | 536 EffectKey yKey = displacementMap.yChannelSelector() << (2 * GrGLEffectMatrix
::kKeyBits + |
| 537 SkDisplacementMapEff
ect::kKeyBits); | 537 SkDisplacementMapEff
ect::kKeyBits); |
| 538 | 538 |
| 539 return xKey | yKey | displKey | colorKey; | 539 return xKey | yKey | displKey | colorKey; |
| 540 } | 540 } |
| 541 #endif | 541 #endif |
| OLD | NEW |