| 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 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 | 194 |
| 195 return true; | 195 return true; |
| 196 } | 196 } |
| 197 | 197 |
| 198 /////////////////////////////////////////////////////////////////////////////// | 198 /////////////////////////////////////////////////////////////////////////////// |
| 199 | 199 |
| 200 #if SK_SUPPORT_GPU | 200 #if SK_SUPPORT_GPU |
| 201 class GrGLDisplacementMapEffect : public GrGLEffect { | 201 class GrGLDisplacementMapEffect : public GrGLEffect { |
| 202 public: | 202 public: |
| 203 GrGLDisplacementMapEffect(const GrBackendEffectFactory& factory, | 203 GrGLDisplacementMapEffect(const GrBackendEffectFactory& factory, |
| 204 const GrEffectRef& effect); | 204 const GrDrawEffect& drawEffect); |
| 205 virtual ~GrGLDisplacementMapEffect(); | 205 virtual ~GrGLDisplacementMapEffect(); |
| 206 | 206 |
| 207 virtual void emitCode(GrGLShaderBuilder*, | 207 virtual void emitCode(GrGLShaderBuilder*, |
| 208 const GrEffectStage&, | 208 const GrDrawEffect&, |
| 209 EffectKey, | 209 EffectKey, |
| 210 const char* vertexCoords, | |
| 211 const char* outputColor, | 210 const char* outputColor, |
| 212 const char* inputColor, | 211 const char* inputColor, |
| 213 const TextureSamplerArray&) SK_OVERRIDE; | 212 const TextureSamplerArray&) SK_OVERRIDE; |
| 214 | 213 |
| 215 static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&); | 214 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
| 216 | 215 |
| 217 virtual void setData(const GrGLUniformManager&, const GrEffectStage&); | 216 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; |
| 218 | 217 |
| 219 private: | 218 private: |
| 219 static const GrEffect::CoordsType kCoordsType = GrEffect::kLocal_CoordsType; |
| 220 |
| 220 SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector; | 221 SkDisplacementMapEffect::ChannelSelectorType fXChannelSelector; |
| 221 SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector; | 222 SkDisplacementMapEffect::ChannelSelectorType fYChannelSelector; |
| 222 GrGLEffectMatrix fDisplacementEffectMatrix; | 223 GrGLEffectMatrix fDisplacementEffectMatrix; |
| 223 GrGLEffectMatrix fColorEffectMatrix; | 224 GrGLEffectMatrix fColorEffectMatrix; |
| 224 GrGLUniformManager::UniformHandle fScaleUni; | 225 GrGLUniformManager::UniformHandle fScaleUni; |
| 225 GrGLUniformManager::UniformHandle fYSignColor; | 226 GrGLUniformManager::UniformHandle fYSignColor; |
| 226 GrGLUniformManager::UniformHandle fYSignDispl; | 227 GrGLUniformManager::UniformHandle fYSignDispl; |
| 227 | 228 |
| 228 typedef GrGLEffect INHERITED; | 229 typedef GrGLEffect INHERITED; |
| 229 }; | 230 }; |
| 230 | 231 |
| 231 /////////////////////////////////////////////////////////////////////////////// | 232 /////////////////////////////////////////////////////////////////////////////// |
| 232 | 233 |
| 233 class GrDisplacementMapEffect : public GrEffect { | 234 class GrDisplacementMapEffect : public GrEffect { |
| 234 public: | 235 public: |
| 235 static GrEffectRef* Create(SkDisplacementMapEffect::ChannelSelectorType xCha
nnelSelector, | 236 static GrEffectRef* Create(SkDisplacementMapEffect::ChannelSelectorType xCha
nnelSelector, |
| 236 SkDisplacementMapEffect::ChannelSelectorType yCha
nnelSelector, | 237 SkDisplacementMapEffect::ChannelSelectorType yCha
nnelSelector, |
| 237 SkScalar scale, GrTexture* displacement, GrTextur
e* color) { | 238 SkScalar scale, GrTexture* displacement, GrTextur
e* color) { |
| 238 AutoEffectUnref effect(SkNEW_ARGS(GrDisplacementMapEffect, (xChannelSele
ctor, | 239 AutoEffectUnref effect(SkNEW_ARGS(GrDisplacementMapEffect, (xChannelSele
ctor, |
| 239 yChannelSele
ctor, | 240 yChannelSele
ctor, |
| 240 scale, | 241 scale, |
| 241 displacement
, | 242 displacement
, |
| 242 color))); | 243 color))); |
| 243 return CreateEffectRef(effect); | 244 return CreateEffectRef(effect); |
| 244 } | 245 } |
| 245 | 246 |
| 246 virtual ~GrDisplacementMapEffect(); | 247 virtual ~GrDisplacementMapEffect(); |
| 247 | 248 |
| 248 const GrBackendEffectFactory& getFactory() const; | 249 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
| 249 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector() const | 250 SkDisplacementMapEffect::ChannelSelectorType xChannelSelector() const |
| 250 { return fXChannelSelector; } | 251 { return fXChannelSelector; } |
| 251 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector() const | 252 SkDisplacementMapEffect::ChannelSelectorType yChannelSelector() const |
| 252 { return fYChannelSelector; } | 253 { return fYChannelSelector; } |
| 253 SkScalar scale() const { return fScale; } | 254 SkScalar scale() const { return fScale; } |
| 254 | 255 |
| 255 typedef GrGLDisplacementMapEffect GLEffect; | 256 typedef GrGLDisplacementMapEffect GLEffect; |
| 256 static const char* Name() { return "DisplacementMap"; } | 257 static const char* Name() { return "DisplacementMap"; } |
| 257 | 258 |
| 258 void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const
SK_OVERRIDE; | 259 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags
) const SK_OVERRIDE; |
| 259 | 260 |
| 260 private: | 261 private: |
| 262 |
| 261 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; | 263 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
| 262 | 264 |
| 263 GrDisplacementMapEffect(SkDisplacementMapEffect::ChannelSelectorType xChanne
lSelector, | 265 GrDisplacementMapEffect(SkDisplacementMapEffect::ChannelSelectorType xChanne
lSelector, |
| 264 SkDisplacementMapEffect::ChannelSelectorType yChanne
lSelector, | 266 SkDisplacementMapEffect::ChannelSelectorType yChanne
lSelector, |
| 265 SkScalar scale, GrTexture* displacement, GrTexture*
color); | 267 SkScalar scale, GrTexture* displacement, GrTexture*
color); |
| 266 | 268 |
| 267 GR_DECLARE_EFFECT_TEST; | 269 GR_DECLARE_EFFECT_TEST; |
| 268 | 270 |
| 269 GrTextureAccess fDisplacementAccess; | 271 GrTextureAccess fDisplacementAccess; |
| 270 GrTextureAccess fColorAccess; | 272 GrTextureAccess fColorAccess; |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 random->nextRangeU(1, kMaxComponent)); | 377 random->nextRangeU(1, kMaxComponent)); |
| 376 SkScalar scale = random->nextRangeScalar(0, SkFloatToScalar(100.0f)); | 378 SkScalar scale = random->nextRangeScalar(0, SkFloatToScalar(100.0f)); |
| 377 | 379 |
| 378 return GrDisplacementMapEffect::Create(xChannelSelector, yChannelSelector, s
cale, | 380 return GrDisplacementMapEffect::Create(xChannelSelector, yChannelSelector, s
cale, |
| 379 textures[texIdxDispl], textures[texId
xColor]); | 381 textures[texIdxDispl], textures[texId
xColor]); |
| 380 } | 382 } |
| 381 | 383 |
| 382 /////////////////////////////////////////////////////////////////////////////// | 384 /////////////////////////////////////////////////////////////////////////////// |
| 383 | 385 |
| 384 GrGLDisplacementMapEffect::GrGLDisplacementMapEffect(const GrBackendEffectFactor
y& factory, | 386 GrGLDisplacementMapEffect::GrGLDisplacementMapEffect(const GrBackendEffectFactor
y& factory, |
| 385 const GrEffectRef& effect) | 387 const GrDrawEffect& drawEff
ect) |
| 386 : INHERITED(factory) | 388 : INHERITED(factory) |
| 387 , fXChannelSelector(CastEffect<GrDisplacementMapEffect>(effect).xChannelSele
ctor()) | 389 , fDisplacementEffectMatrix(kCoordsType) |
| 388 , fYChannelSelector(CastEffect<GrDisplacementMapEffect>(effect).yChannelSele
ctor()) { | 390 , fColorEffectMatrix(kCoordsType) |
| 391 , fXChannelSelector(drawEffect.castEffect<GrDisplacementMapEffect>().xChanne
lSelector()) |
| 392 , fYChannelSelector(drawEffect.castEffect<GrDisplacementMapEffect>().yChanne
lSelector()) { |
| 389 } | 393 } |
| 390 | 394 |
| 391 GrGLDisplacementMapEffect::~GrGLDisplacementMapEffect() { | 395 GrGLDisplacementMapEffect::~GrGLDisplacementMapEffect() { |
| 392 } | 396 } |
| 393 | 397 |
| 394 void GrGLDisplacementMapEffect::emitCode(GrGLShaderBuilder* builder, | 398 void GrGLDisplacementMapEffect::emitCode(GrGLShaderBuilder* builder, |
| 395 const GrEffectStage&, | 399 const GrDrawEffect&, |
| 396 EffectKey key, | 400 EffectKey key, |
| 397 const char* vertexCoords, | 401 const char* outputColor, |
| 398 const char* outputColor, | 402 const char* inputColor, |
| 399 const char* inputColor, | 403 const TextureSamplerArray& samplers) { |
| 400 const TextureSamplerArray& samplers) { | |
| 401 fScaleUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, | 404 fScaleUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 402 kVec2f_GrSLType, "Scale"); | 405 kVec2f_GrSLType, "Scale"); |
| 403 const char* scaleUni = builder->getUniformCStr(fScaleUni); | 406 const char* scaleUni = builder->getUniformCStr(fScaleUni); |
| 404 | 407 |
| 405 const char* dCoordsIn; | 408 const char* dCoordsIn; |
| 406 GrSLType dCoordsType = fDisplacementEffectMatrix.emitCode( | 409 GrSLType dCoordsType = fDisplacementEffectMatrix.emitCode( |
| 407 builder, key, vertexCoords, &dCoordsIn, NULL, "D
ISPL"); | 410 builder, key, &dCoordsIn, NULL, "DISPL"); |
| 408 const char* cCoordsIn; | 411 const char* cCoordsIn; |
| 409 GrSLType cCoordsType = fColorEffectMatrix.emitCode( | 412 GrSLType cCoordsType = fColorEffectMatrix.emitCode( |
| 410 builder, key, vertexCoords, &cCoordsIn, NULL, "C
OLOR"); | 413 builder, key, &cCoordsIn, NULL, "COLOR"); |
| 411 | 414 |
| 412 const char* dColor = "dColor"; | 415 const char* dColor = "dColor"; |
| 413 const char* cCoords = "cCoords"; | 416 const char* cCoords = "cCoords"; |
| 414 const char* nearZero = "1e-6"; // Since 6.10352e−5 is the smallest half floa
t, use | 417 const char* nearZero = "1e-6"; // Since 6.10352e−5 is the smallest half floa
t, use |
| 415 // a number smaller than that to approximate
0, but | 418 // a number smaller than that to approximate
0, but |
| 416 // leave room for 32-bit float GPU rounding e
rrors. | 419 // leave room for 32-bit float GPU rounding e
rrors. |
| 417 | 420 |
| 418 builder->fsCodeAppendf("\t\tvec4 %s = ", dColor); | 421 builder->fsCodeAppendf("\t\tvec4 %s = ", dColor); |
| 419 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, | 422 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, |
| 420 samplers[0], | 423 samplers[0], |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 builder->fsCodeAppendf( | 474 builder->fsCodeAppendf( |
| 472 "%s = any(greaterThan(vec4(vec2(0.0), %s), vec4(%s, vec2(1.0)))) ? vec4(
0.0) : ", | 475 "%s = any(greaterThan(vec4(vec2(0.0), %s), vec4(%s, vec2(1.0)))) ? vec4(
0.0) : ", |
| 473 outputColor, cCoords, cCoords); | 476 outputColor, cCoords, cCoords); |
| 474 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, | 477 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, |
| 475 samplers[1], | 478 samplers[1], |
| 476 cCoords, | 479 cCoords, |
| 477 cCoordsType); | 480 cCoordsType); |
| 478 builder->fsCodeAppend(";\n"); | 481 builder->fsCodeAppend(";\n"); |
| 479 } | 482 } |
| 480 | 483 |
| 481 void GrGLDisplacementMapEffect::setData(const GrGLUniformManager& uman, const Gr
EffectStage& stage) { | 484 void GrGLDisplacementMapEffect::setData(const GrGLUniformManager& uman, |
| 482 const GrDisplacementMapEffect& displacementMap = GetEffectFromStage<GrDispla
cementMapEffect>(stage); | 485 const GrDrawEffect& drawEffect) { |
| 486 const GrDisplacementMapEffect& displacementMap = |
| 487 drawEffect.castEffect<GrDisplacementMapEffect>(); |
| 483 GrTexture* displTex = displacementMap.texture(0); | 488 GrTexture* displTex = displacementMap.texture(0); |
| 484 GrTexture* colorTex = displacementMap.texture(1); | 489 GrTexture* colorTex = displacementMap.texture(1); |
| 485 fDisplacementEffectMatrix.setData(uman, | 490 fDisplacementEffectMatrix.setData(uman, |
| 486 GrEffect::MakeDivByTextureWHMatrix(displTex
), | 491 GrEffect::MakeDivByTextureWHMatrix(displTex
), |
| 487 stage.getCoordChangeMatrix(), | 492 drawEffect, |
| 488 displTex); | 493 displTex); |
| 489 fColorEffectMatrix.setData(uman, | 494 fColorEffectMatrix.setData(uman, |
| 490 GrEffect::MakeDivByTextureWHMatrix(colorTex), | 495 GrEffect::MakeDivByTextureWHMatrix(colorTex), |
| 491 stage.getCoordChangeMatrix(), | 496 drawEffect, |
| 492 colorTex); | 497 colorTex); |
| 493 | 498 |
| 494 SkScalar scaleX = SkScalarDiv(displacementMap.scale(), SkIntToScalar(colorTe
x->width())); | 499 SkScalar scaleX = SkScalarDiv(displacementMap.scale(), SkIntToScalar(colorTe
x->width())); |
| 495 SkScalar scaleY = SkScalarDiv(displacementMap.scale(), SkIntToScalar(colorTe
x->height())); | 500 SkScalar scaleY = SkScalarDiv(displacementMap.scale(), SkIntToScalar(colorTe
x->height())); |
| 496 uman.set2f(fScaleUni, SkScalarToFloat(scaleX), | 501 uman.set2f(fScaleUni, SkScalarToFloat(scaleX), |
| 497 colorTex->origin() == kTopLeft_GrSurfaceOrigin ? | 502 colorTex->origin() == kTopLeft_GrSurfaceOrigin ? |
| 498 SkScalarToFloat(scaleY) : SkScalarToFloat(-scaleY)); | 503 SkScalarToFloat(scaleY) : SkScalarToFloat(-scaleY)); |
| 499 } | 504 } |
| 500 | 505 |
| 501 GrGLEffect::EffectKey GrGLDisplacementMapEffect::GenKey(const GrEffectStage& sta
ge, | 506 GrGLEffect::EffectKey GrGLDisplacementMapEffect::GenKey(const GrDrawEffect& draw
Effect, |
| 502 const GrGLCaps&) { | 507 const GrGLCaps&) { |
| 503 const GrDisplacementMapEffect& displacementMap = | 508 const GrDisplacementMapEffect& displacementMap = |
| 504 GetEffectFromStage<GrDisplacementMapEffect>(stage); | 509 drawEffect.castEffect<GrDisplacementMapEffect>(); |
| 505 | 510 |
| 506 GrTexture* displTex = displacementMap.texture(0); | 511 GrTexture* displTex = displacementMap.texture(0); |
| 507 GrTexture* colorTex = displacementMap.texture(1); | 512 GrTexture* colorTex = displacementMap.texture(1); |
| 508 | 513 |
| 509 EffectKey displKey = GrGLEffectMatrix::GenKey(GrEffect::MakeDivByTextureWHMa
trix(displTex), | 514 EffectKey displKey = GrGLEffectMatrix::GenKey(GrEffect::MakeDivByTextureWHMa
trix(displTex), |
| 510 stage.getCoordChangeMatrix(), | 515 drawEffect, |
| 516 kCoordsType, |
| 511 displTex); | 517 displTex); |
| 512 | 518 |
| 513 EffectKey colorKey = GrGLEffectMatrix::GenKey(GrEffect::MakeDivByTextureWHMa
trix(colorTex), | 519 EffectKey colorKey = GrGLEffectMatrix::GenKey(GrEffect::MakeDivByTextureWHMa
trix(colorTex), |
| 514 stage.getCoordChangeMatrix(), | 520 drawEffect, |
| 521 kCoordsType, |
| 515 colorTex); | 522 colorTex); |
| 516 | 523 |
| 517 colorKey <<= GrGLEffectMatrix::kKeyBits; | 524 colorKey <<= GrGLEffectMatrix::kKeyBits; |
| 518 EffectKey xKey = displacementMap.xChannelSelector() << (2 * GrGLEffectMatrix
::kKeyBits); | 525 EffectKey xKey = displacementMap.xChannelSelector() << (2 * GrGLEffectMatrix
::kKeyBits); |
| 519 EffectKey yKey = displacementMap.yChannelSelector() << (2 * GrGLEffectMatrix
::kKeyBits + | 526 EffectKey yKey = displacementMap.yChannelSelector() << (2 * GrGLEffectMatrix
::kKeyBits + |
| 520 SkDisplacementMapEff
ect::kKeyBits); | 527 SkDisplacementMapEff
ect::kKeyBits); |
| 521 | 528 |
| 522 return xKey | yKey | displKey | colorKey; | 529 return xKey | yKey | displKey | colorKey; |
| 523 } | 530 } |
| 524 #endif | 531 #endif |
| OLD | NEW |