OLD | NEW |
1 | |
2 /* | 1 /* |
3 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
4 * | 3 * |
5 * 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 |
6 * found in the LICENSE file. | 5 * found in the LICENSE file. |
7 */ | 6 */ |
8 | 7 |
9 #include "SkBitmapProcState.h" | 8 #include "SkBitmapProcState.h" |
10 #include "SkColor.h" | 9 #include "SkColor.h" |
11 #include "SkEmptyShader.h" | 10 #include "SkEmptyShader.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 @param diffuse the diffuse bitmap | 45 @param diffuse the diffuse bitmap |
47 @param normal the normal map | 46 @param normal the normal map |
48 @param lights the lights applied to the normal map | 47 @param lights the lights applied to the normal map |
49 @param invNormRotation rotation applied to the normal map's normals | 48 @param invNormRotation rotation applied to the normal map's normals |
50 @param diffLocalM the local matrix for the diffuse coordinates | 49 @param diffLocalM the local matrix for the diffuse coordinates |
51 @param normLocalM the local matrix for the normal coordinates | 50 @param normLocalM the local matrix for the normal coordinates |
52 */ | 51 */ |
53 SkLightingShaderImpl(const SkBitmap& diffuse, const SkBitmap& normal, | 52 SkLightingShaderImpl(const SkBitmap& diffuse, const SkBitmap& normal, |
54 const SkLightingShader::Lights* lights, | 53 const SkLightingShader::Lights* lights, |
55 const SkVector& invNormRotation, | 54 const SkVector& invNormRotation, |
56 const SkMatrix* diffLocalM, const SkMatrix* normLocalM)
| 55 const SkMatrix* diffLocalM, const SkMatrix* normLocalM) |
57 : INHERITED(diffLocalM) | 56 : INHERITED(diffLocalM) |
58 , fDiffuseMap(diffuse) | 57 , fDiffuseMap(diffuse) |
59 , fNormalMap(normal) | 58 , fNormalMap(normal) |
60 , fLights(SkRef(lights)) | 59 , fLights(SkRef(lights)) |
61 , fInvNormRotation(invNormRotation) { | 60 , fInvNormRotation(invNormRotation) { |
62 | 61 |
63 if (normLocalM) { | 62 if (normLocalM) { |
64 fNormLocalMatrix = *normLocalM; | 63 fNormLocalMatrix = *normLocalM; |
65 } else { | 64 } else { |
66 fNormLocalMatrix.reset(); | 65 fNormLocalMatrix.reset(); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag
, | 196 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag
, |
198 kVec3f_GrSLType, kDefa
ult_GrSLPrecision, | 197 kVec3f_GrSLType, kDefa
ult_GrSLPrecision, |
199 "AmbientColor", &ambie
ntColorUniName); | 198 "AmbientColor", &ambie
ntColorUniName); |
200 | 199 |
201 const char* xformUniName = nullptr; | 200 const char* xformUniName = nullptr; |
202 fXformUni = uniformHandler->addUniform(kFragment_GrShaderFlag, | 201 fXformUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
203 kVec2f_GrSLType, kDefault_GrS
LPrecision, | 202 kVec2f_GrSLType, kDefault_GrS
LPrecision, |
204 "Xform", &xformUniName); | 203 "Xform", &xformUniName); |
205 | 204 |
206 fragBuilder->codeAppend("vec4 diffuseColor = "); | 205 fragBuilder->codeAppend("vec4 diffuseColor = "); |
207 fragBuilder->appendTextureLookupAndModulate(args.fInputColor, args.f
Samplers[0], | 206 fragBuilder->appendTextureLookupAndModulate(args.fInputColor, args.f
Samplers[0], |
208 args.fCoords[0].c_str(), | 207 args.fCoords[0].c_str(), |
209 args.fCoords[0].getType()); | 208 args.fCoords[0].getType()); |
210 fragBuilder->codeAppend(";"); | 209 fragBuilder->codeAppend(";"); |
211 | 210 |
212 fragBuilder->codeAppend("vec4 normalColor = "); | 211 fragBuilder->codeAppend("vec4 normalColor = "); |
213 fragBuilder->appendTextureLookup(args.fSamplers[1], | 212 fragBuilder->appendTextureLookup(args.fSamplers[1], |
214 args.fCoords[1].c_str(), | 213 args.fCoords[1].c_str(), |
215 args.fCoords[1].getType()); | 214 args.fCoords[1].getType()); |
216 fragBuilder->codeAppend(";"); | 215 fragBuilder->codeAppend(";"); |
217 | 216 |
218 fragBuilder->codeAppend("vec3 normal = normalColor.rgb - vec3(0.5);"
); | 217 fragBuilder->codeAppend("vec3 normal = normalColor.rgb - vec3(0.5);"
); |
219 | 218 |
220 fragBuilder->codeAppendf( | 219 fragBuilder->codeAppendf( |
221 "mat3 m = mat3(%s.x, -%s.y, 0.0, %s.y, %s.x, 0.
0, 0.0, 0.0, 1.0);", | 220 "mat3 m = mat3(%s.x, -%s.y, 0.0, %s.y, %s.x, 0.
0, 0.0, 0.0, 1.0);", |
222 xformUniName, xformUniName, xformUniName, xform
UniName); | 221 xformUniName, xformUniName, xformUniName, xform
UniName); |
223 | 222 |
224 // TODO: inverse map the light direction vectors in the vertex shade
r rather than | 223 // TODO: inverse map the light direction vectors in the vertex shade
r rather than |
225 // transforming all the normals here! | 224 // transforming all the normals here! |
226 fragBuilder->codeAppend("normal = normalize(m*normal);"); | 225 fragBuilder->codeAppend("normal = normalize(m*normal);"); |
227 | 226 |
228 fragBuilder->codeAppendf("float NdotL = clamp(dot(normal, %s), 0.0,
1.0);", | 227 fragBuilder->codeAppendf("float NdotL = clamp(dot(normal, %s), 0.0,
1.0);", |
229 lightDirUniName); | 228 lightDirUniName); |
230 // diffuse light | 229 // diffuse light |
231 fragBuilder->codeAppendf("vec3 result = %s*diffuseColor.rgb*NdotL;",
lightColorUniName); | 230 fragBuilder->codeAppendf("vec3 result = %s*diffuseColor.rgb*NdotL;",
lightColorUniName); |
232 // ambient light | 231 // ambient light |
233 fragBuilder->codeAppendf("result += %s;", ambientColorUniName); | 232 fragBuilder->codeAppendf("result += %s;", ambientColorUniName); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 } | 294 } |
296 | 295 |
297 const SkVector3& lightDir() const { return fLightDir; } | 296 const SkVector3& lightDir() const { return fLightDir; } |
298 const SkColor3f& lightColor() const { return fLightColor; } | 297 const SkColor3f& lightColor() const { return fLightColor; } |
299 const SkColor3f& ambientColor() const { return fAmbientColor; } | 298 const SkColor3f& ambientColor() const { return fAmbientColor; } |
300 const SkVector& invNormRotation() const { return fInvNormRotation; } | 299 const SkVector& invNormRotation() const { return fInvNormRotation; } |
301 | 300 |
302 private: | 301 private: |
303 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new
LightingGLFP; } | 302 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new
LightingGLFP; } |
304 | 303 |
305 bool onIsEqual(const GrFragmentProcessor& proc) const override { | 304 bool onIsEqual(const GrFragmentProcessor& proc) const override { |
306 const LightingFP& lightingFP = proc.cast<LightingFP>(); | 305 const LightingFP& lightingFP = proc.cast<LightingFP>(); |
307 return fDiffDeviceTransform == lightingFP.fDiffDeviceTransform && | 306 return fDiffDeviceTransform == lightingFP.fDiffDeviceTransform && |
308 fNormDeviceTransform == lightingFP.fNormDeviceTransform && | 307 fNormDeviceTransform == lightingFP.fNormDeviceTransform && |
309 fDiffuseTextureAccess == lightingFP.fDiffuseTextureAccess && | 308 fDiffuseTextureAccess == lightingFP.fDiffuseTextureAccess && |
310 fNormalTextureAccess == lightingFP.fNormalTextureAccess && | 309 fNormalTextureAccess == lightingFP.fNormalTextureAccess && |
311 fLightDir == lightingFP.fLightDir && | 310 fLightDir == lightingFP.fLightDir && |
312 fLightColor == lightingFP.fLightColor && | 311 fLightColor == lightingFP.fLightColor && |
313 fAmbientColor == lightingFP.fAmbientColor && | 312 fAmbientColor == lightingFP.fAmbientColor && |
314 fInvNormRotation == lightingFP.fInvNormRotation; | 313 fInvNormRotation == lightingFP.fInvNormRotation; |
315 } | 314 } |
316 | 315 |
317 GrCoordTransform fDiffDeviceTransform; | 316 GrCoordTransform fDiffDeviceTransform; |
318 GrCoordTransform fNormDeviceTransform; | 317 GrCoordTransform fNormDeviceTransform; |
319 GrTextureAccess fDiffuseTextureAccess; | 318 GrTextureAccess fDiffuseTextureAccess; |
320 GrTextureAccess fNormalTextureAccess; | 319 GrTextureAccess fNormalTextureAccess; |
321 SkVector3 fLightDir; | 320 SkVector3 fLightDir; |
322 SkColor3f fLightColor; | 321 SkColor3f fLightColor; |
323 SkColor3f fAmbientColor; | 322 SkColor3f fAmbientColor; |
324 | 323 |
325 SkVector fInvNormRotation; | 324 SkVector fInvNormRotation; |
326 }; | 325 }; |
327 | 326 |
328 //////////////////////////////////////////////////////////////////////////// | 327 //////////////////////////////////////////////////////////////////////////// |
329 | 328 |
330 static bool make_mat(const SkBitmap& bm, | 329 static bool make_mat(const SkBitmap& bm, |
331 const SkMatrix& localMatrix1, | 330 const SkMatrix& localMatrix1, |
332 const SkMatrix* localMatrix2, | 331 const SkMatrix* localMatrix2, |
333 SkMatrix* result) { | 332 SkMatrix* result) { |
334 | 333 |
335 result->setIDiv(bm.width(), bm.height()); | 334 result->setIDiv(bm.width(), bm.height()); |
336 | 335 |
337 SkMatrix lmInverse; | 336 SkMatrix lmInverse; |
338 if (!localMatrix1.invert(&lmInverse)) { | 337 if (!localMatrix1.invert(&lmInverse)) { |
339 return false; | 338 return false; |
340 } | 339 } |
341 if (localMatrix2) { | 340 if (localMatrix2) { |
342 SkMatrix inv; | 341 SkMatrix inv; |
343 if (!localMatrix2->invert(&inv)) { | 342 if (!localMatrix2->invert(&inv)) { |
344 return false; | 343 return false; |
345 } | 344 } |
346 lmInverse.postConcat(inv); | 345 lmInverse.postConcat(inv); |
347 } | 346 } |
348 result->preConcat(lmInverse); | 347 result->preConcat(lmInverse); |
349 | 348 |
350 return true; | 349 return true; |
351 } | 350 } |
352 | 351 |
353 const GrFragmentProcessor* SkLightingShaderImpl::asFragmentProcessor( | 352 const GrFragmentProcessor* SkLightingShaderImpl::asFragmentProcessor( |
354 GrContext* context, | 353 GrContext* context, |
355 const SkMatrix& vie
wM, | 354 const SkMatrix& vie
wM, |
356 const SkMatrix* loc
alMatrix, | 355 const SkMatrix* loc
alMatrix, |
357 SkFilterQuality fil
terQuality) const { | 356 SkFilterQuality fil
terQuality) const { |
358 // we assume diffuse and normal maps have same width and height | 357 // we assume diffuse and normal maps have same width and height |
359 // TODO: support different sizes | 358 // TODO: support different sizes |
360 SkASSERT(fDiffuseMap.width() == fNormalMap.width() && | 359 SkASSERT(fDiffuseMap.width() == fNormalMap.width() && |
361 fDiffuseMap.height() == fNormalMap.height()); | 360 fDiffuseMap.height() == fNormalMap.height()); |
362 SkMatrix diffM, normM; | 361 SkMatrix diffM, normM; |
363 | 362 |
364 if (!make_mat(fDiffuseMap, this->getLocalMatrix(), localMatrix, &diffM)) { | 363 if (!make_mat(fDiffuseMap, this->getLocalMatrix(), localMatrix, &diffM)) { |
365 return nullptr; | 364 return nullptr; |
366 } | 365 } |
367 | 366 |
368 if (!make_mat(fNormalMap, fNormLocalMatrix, localMatrix, &normM)) { | 367 if (!make_mat(fNormalMap, fNormLocalMatrix, localMatrix, &normM)) { |
369 return nullptr; | 368 return nullptr; |
370 } | 369 } |
371 | 370 |
372 bool doBicubic; | 371 bool doBicubic; |
373 GrTextureParams::FilterMode diffFilterMode = GrSkFilterQualityToGrFilterMode
( | 372 GrTextureParams::FilterMode diffFilterMode = GrSkFilterQualityToGrFilterMode
( |
374 SkTMin(filterQuality, kMedium_SkFilterQu
ality), | 373 SkTMin(filterQuality, kMedium_SkFilterQu
ality), |
375 viewM, | 374 viewM, |
376 this->getLocalMatrix(), | 375 this->getLocalMatrix(), |
377 &doBicubic); | 376 &doBicubic); |
378 SkASSERT(!doBicubic); | 377 SkASSERT(!doBicubic); |
379 | 378 |
380 GrTextureParams::FilterMode normFilterMode = GrSkFilterQualityToGrFilterMode
( | 379 GrTextureParams::FilterMode normFilterMode = GrSkFilterQualityToGrFilterMode
( |
381 SkTMin(filterQuality, kMedium_SkFilterQu
ality), | 380 SkTMin(filterQuality, kMedium_SkFilterQu
ality), |
382 viewM, | 381 viewM, |
383 fNormLocalMatrix, | 382 fNormLocalMatrix, |
384 &doBicubic); | 383 &doBicubic); |
385 SkASSERT(!doBicubic); | 384 SkASSERT(!doBicubic); |
386 | 385 |
387 // TODO: support other tile modes | 386 // TODO: support other tile modes |
388 GrTextureParams diffParams(kClamp_TileMode, diffFilterMode); | 387 GrTextureParams diffParams(kClamp_TileMode, diffFilterMode); |
389 SkAutoTUnref<GrTexture> diffuseTexture(GrRefCachedBitmapTexture(context, | 388 SkAutoTUnref<GrTexture> diffuseTexture(GrRefCachedBitmapTexture(context, |
390 fDiffuseMap,
diffParams)); | 389 fDiffuseMap,
diffParams)); |
391 if (!diffuseTexture) { | 390 if (!diffuseTexture) { |
392 SkErrorInternals::SetError(kInternalError_SkError, "Couldn't convert bit
map to texture."); | 391 SkErrorInternals::SetError(kInternalError_SkError, "Couldn't convert bit
map to texture."); |
393 return nullptr; | 392 return nullptr; |
394 } | 393 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 // elsewhere. Call the destructors but leave the freeing of the memory to th
e caller. | 439 // elsewhere. Call the destructors but leave the freeing of the memory to th
e caller. |
441 fDiffuseState->~SkBitmapProcState(); | 440 fDiffuseState->~SkBitmapProcState(); |
442 fNormalState->~SkBitmapProcState(); | 441 fNormalState->~SkBitmapProcState(); |
443 } | 442 } |
444 | 443 |
445 static inline SkPMColor convert(SkColor3f color, U8CPU a) { | 444 static inline SkPMColor convert(SkColor3f color, U8CPU a) { |
446 if (color.fX <= 0.0f) { | 445 if (color.fX <= 0.0f) { |
447 color.fX = 0.0f; | 446 color.fX = 0.0f; |
448 } else if (color.fX >= 255.0f) { | 447 } else if (color.fX >= 255.0f) { |
449 color.fX = 255.0f; | 448 color.fX = 255.0f; |
450 } | 449 } |
451 | 450 |
452 if (color.fY <= 0.0f) { | 451 if (color.fY <= 0.0f) { |
453 color.fY = 0.0f; | 452 color.fY = 0.0f; |
454 } else if (color.fY >= 255.0f) { | 453 } else if (color.fY >= 255.0f) { |
455 color.fY = 255.0f; | 454 color.fY = 255.0f; |
456 } | 455 } |
457 | 456 |
458 if (color.fZ <= 0.0f) { | 457 if (color.fZ <= 0.0f) { |
459 color.fZ = 0.0f; | 458 color.fZ = 0.0f; |
460 } else if (color.fZ >= 255.0f) { | 459 } else if (color.fZ >= 255.0f) { |
461 color.fZ = 255.0f; | 460 color.fZ = 255.0f; |
462 } | 461 } |
463 | 462 |
464 return SkPreMultiplyARGB(a, (int) color.fX, (int) color.fY, (int) color.fZ)
; | 463 return SkPreMultiplyARGB(a, (int) color.fX, (int) color.fY, (int) color.fZ)
; |
465 } | 464 } |
466 | 465 |
467 // larger is better (fewer times we have to loop), but we shouldn't | 466 // larger is better (fewer times we have to loop), but we shouldn't |
468 // take up too much stack-space (each one here costs 16 bytes) | 467 // take up too much stack-space (each one here costs 16 bytes) |
469 #define TMP_COUNT 16 | 468 #define TMP_COUNT 16 |
470 | 469 |
471 void SkLightingShaderImpl::LightingShaderContext::shadeSpan(int x, int y, | 470 void SkLightingShaderImpl::LightingShaderContext::shadeSpan(int x, int y, |
472 SkPMColor result[],
int count) { | 471 SkPMColor result[],
int count) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 | 503 |
505 for (int i = 0; i < n; ++i) { | 504 for (int i = 0; i < n; ++i) { |
506 SkASSERT(0xFF == SkColorGetA(tmpNormal2[i])); // opaque -> unpremul | 505 SkASSERT(0xFF == SkColorGetA(tmpNormal2[i])); // opaque -> unpremul |
507 norm.set(SkIntToScalar(SkGetPackedR32(tmpNormal2[i]))-127.0f, | 506 norm.set(SkIntToScalar(SkGetPackedR32(tmpNormal2[i]))-127.0f, |
508 SkIntToScalar(SkGetPackedG32(tmpNormal2[i]))-127.0f, | 507 SkIntToScalar(SkGetPackedG32(tmpNormal2[i]))-127.0f, |
509 SkIntToScalar(SkGetPackedB32(tmpNormal2[i]))-127.0f); | 508 SkIntToScalar(SkGetPackedB32(tmpNormal2[i]))-127.0f); |
510 norm.normalize(); | 509 norm.normalize(); |
511 | 510 |
512 xformedNorm.fX = lightShader.fInvNormRotation.fX * norm.fX + | 511 xformedNorm.fX = lightShader.fInvNormRotation.fX * norm.fX + |
513 lightShader.fInvNormRotation.fY * norm.fY; | 512 lightShader.fInvNormRotation.fY * norm.fY; |
514 xformedNorm.fY = lightShader.fInvNormRotation.fX * norm.fX - | 513 xformedNorm.fY = lightShader.fInvNormRotation.fX * norm.fX - |
515 lightShader.fInvNormRotation.fY * norm.fY; | 514 lightShader.fInvNormRotation.fY * norm.fY; |
516 xformedNorm.fZ = norm.fZ; | 515 xformedNorm.fZ = norm.fZ; |
517 | 516 |
518 SkColor diffColor = SkUnPreMultiply::PMColorToColor(tmpColor2[i]); | 517 SkColor diffColor = SkUnPreMultiply::PMColorToColor(tmpColor2[i]); |
519 | 518 |
520 SkColor3f accum = SkColor3f::Make(0.0f, 0.0f, 0.0f); | 519 SkColor3f accum = SkColor3f::Make(0.0f, 0.0f, 0.0f); |
521 // This is all done in linear unpremul color space (each component 0
..255.0f though) | 520 // This is all done in linear unpremul color space (each component 0
..255.0f though) |
522 for (int l = 0; l < lightShader.fLights->numLights(); ++l) { | 521 for (int l = 0; l < lightShader.fLights->numLights(); ++l) { |
523 const SkLight& light = lightShader.fLights->light(l); | 522 const SkLight& light = lightShader.fLights->light(l); |
524 | 523 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
594 return nullptr; | 593 return nullptr; |
595 } | 594 } |
596 | 595 |
597 if (isAmbient) { | 596 if (isAmbient) { |
598 builder.add(SkLight(color)); | 597 builder.add(SkLight(color)); |
599 } else { | 598 } else { |
600 SkVector3 dir; | 599 SkVector3 dir; |
601 if (!buf.readScalarArray(&dir.fX, 3)) { | 600 if (!buf.readScalarArray(&dir.fX, 3)) { |
602 return nullptr; | 601 return nullptr; |
603 } | 602 } |
604 builder.add(SkLight(color, dir)); | 603 builder.add(SkLight(color, dir)); |
605 } | 604 } |
606 } | 605 } |
607 | 606 |
608 SkAutoTUnref<const SkLightingShader::Lights> lights(builder.finish()); | 607 SkAutoTUnref<const SkLightingShader::Lights> lights(builder.finish()); |
609 | 608 |
610 return new SkLightingShaderImpl(diffuse, normal, lights, SkVector::Make(1.0f
, 0.0f), | 609 return new SkLightingShaderImpl(diffuse, normal, lights, SkVector::Make(1.0f
, 0.0f), |
611 &diffLocalM, &normLocalM); | 610 &diffLocalM, &normLocalM); |
612 } | 611 } |
613 | 612 |
614 void SkLightingShaderImpl::flatten(SkWriteBuffer& buf) const { | 613 void SkLightingShaderImpl::flatten(SkWriteBuffer& buf) const { |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
717 normLocalM); | 716 normLocalM); |
718 } | 717 } |
719 | 718 |
720 /////////////////////////////////////////////////////////////////////////////// | 719 /////////////////////////////////////////////////////////////////////////////// |
721 | 720 |
722 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader) | 721 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader) |
723 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl) | 722 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl) |
724 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 723 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
725 | 724 |
726 /////////////////////////////////////////////////////////////////////////////// | 725 /////////////////////////////////////////////////////////////////////////////// |
OLD | NEW |