Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(464)

Side by Side Diff: src/core/SkLightingShader.cpp

Issue 2063793002: API change to allow for NormalSource selection at the user level. (Closed) Base URL: https://skia.googlesource.com/skia@dvonbeck-normal-factor-out
Patch Set: Small rebase and comment fix Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2015 Google Inc. 2 * Copyright 2015 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 "SkBitmapProcState.h" 8 #include "SkBitmapProcState.h"
9 #include "SkColor.h" 9 #include "SkColor.h"
10 #include "SkEmptyShader.h" 10 #include "SkEmptyShader.h"
(...skipping 25 matching lines...) Expand all
36 36
37 /** \class SkLightingShaderImpl 37 /** \class SkLightingShaderImpl
38 This subclass of shader applies lighting. 38 This subclass of shader applies lighting.
39 */ 39 */
40 class SK_API SkLightingShaderImpl : public SkShader { 40 class SK_API SkLightingShaderImpl : public SkShader {
41 public: 41 public:
42 42
43 /** Create a new lighting shader that uses the provided normal map and 43 /** Create a new lighting shader that uses the provided normal map and
44 lights to light the diffuse bitmap. 44 lights to light the diffuse bitmap.
45 @param diffuse the diffuse bitmap 45 @param diffuse the diffuse bitmap
46 @param normal the normal map
47 @param lights the lights applied to the normal map 46 @param lights the lights applied to the normal map
48 @param invNormRotation rotation applied to the normal map's normals
49 @param diffLocalM the local matrix for the diffuse coordinates 47 @param diffLocalM the local matrix for the diffuse coordinates
50 @param normLocalM the local matrix for the normal coordinates 48 @param normalSource the source of normals for lighting computation
51 @param normalSource the normal source for GPU computations
52 */ 49 */
53 SkLightingShaderImpl(const SkBitmap& diffuse, const SkBitmap& normal, 50 SkLightingShaderImpl(const SkBitmap& diffuse,
54 const sk_sp<SkLights> lights, 51 const sk_sp<SkLights> lights,
55 const SkVector& invNormRotation, 52 const SkMatrix* diffLocalM,
56 const SkMatrix* diffLocalM, const SkMatrix* normLocalM,
57 sk_sp<SkLightingShader::NormalSource> normalSource) 53 sk_sp<SkLightingShader::NormalSource> normalSource)
58 : INHERITED(diffLocalM) 54 : INHERITED(diffLocalM)
59 , fDiffuseMap(diffuse) 55 , fDiffuseMap(diffuse)
60 , fNormalMap(normal) 56 , fLights(std::move(lights)) {
61 , fLights(std::move(lights))
62 , fInvNormRotation(invNormRotation) {
63
64 if (normLocalM) {
65 fNormLocalMatrix = *normLocalM;
66 } else {
67 fNormLocalMatrix.reset();
68 }
69 // Pre-cache so future calls to fNormLocalMatrix.getType() are threadsaf e.
70 (void)fNormLocalMatrix.getType();
71 57
72 fNormalSource = std::move(normalSource); 58 fNormalSource = std::move(normalSource);
73 } 59 }
74 60
75 bool isOpaque() const override; 61 bool isOpaque() const override;
76 62
77 #if SK_SUPPORT_GPU 63 #if SK_SUPPORT_GPU
78 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, 64 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*,
79 const SkMatrix& viewM, 65 const SkMatrix& viewM,
80 const SkMatrix* localMatrix, 66 const SkMatrix* localMatrix,
(...skipping 25 matching lines...) Expand all
106 SK_TO_STRING_OVERRIDE() 92 SK_TO_STRING_OVERRIDE()
107 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLightingShaderImpl) 93 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLightingShaderImpl)
108 94
109 protected: 95 protected:
110 void flatten(SkWriteBuffer&) const override; 96 void flatten(SkWriteBuffer&) const override;
111 size_t onContextSize(const ContextRec&) const override; 97 size_t onContextSize(const ContextRec&) const override;
112 Context* onCreateContext(const ContextRec&, void*) const override; 98 Context* onCreateContext(const ContextRec&, void*) const override;
113 99
114 private: 100 private:
115 SkBitmap fDiffuseMap; 101 SkBitmap fDiffuseMap;
116 SkBitmap fNormalMap;
117
118 sk_sp<SkLights> fLights; 102 sk_sp<SkLights> fLights;
119 103
120 SkMatrix fNormLocalMatrix;
121 SkVector fInvNormRotation;
122
123 sk_sp<SkLightingShader::NormalSource> fNormalSource; 104 sk_sp<SkLightingShader::NormalSource> fNormalSource;
124 105
125 friend class SkLightingShader; 106 friend class SkLightingShader;
126 107
127 typedef SkShader INHERITED; 108 typedef SkShader INHERITED;
128 }; 109 };
129 110
130 //////////////////////////////////////////////////////////////////////////// 111 ////////////////////////////////////////////////////////////////////////////
131 112
132 #if SK_SUPPORT_GPU 113 #if SK_SUPPORT_GPU
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 298
318 sk_sp<GrFragmentProcessor> SkLightingShaderImpl::asFragmentProcessor( 299 sk_sp<GrFragmentProcessor> SkLightingShaderImpl::asFragmentProcessor(
319 GrContext* context, 300 GrContext* context,
320 const SkMatrix& viewM, 301 const SkMatrix& viewM,
321 const SkMatrix* localMatrix , 302 const SkMatrix* localMatrix ,
322 SkFilterQuality filterQuali ty, 303 SkFilterQuality filterQuali ty,
323 SkSourceGammaTreatment gamm aTreatment) const { 304 SkSourceGammaTreatment gamm aTreatment) const {
324 // we assume diffuse and normal maps have same width and height 305 // we assume diffuse and normal maps have same width and height
325 // TODO: support different sizes, will be addressed when diffuse maps are fa ctored out of 306 // TODO: support different sizes, will be addressed when diffuse maps are fa ctored out of
326 // SkLightingShader in a future CL 307 // SkLightingShader in a future CL
327 SkASSERT(fDiffuseMap.width() == fNormalMap.width() &&
328 fDiffuseMap.height() == fNormalMap.height());
329 SkMatrix diffM; 308 SkMatrix diffM;
330 309
331 if (!make_mat(fDiffuseMap, this->getLocalMatrix(), localMatrix, &diffM)) { 310 if (!make_mat(fDiffuseMap, this->getLocalMatrix(), localMatrix, &diffM)) {
332 return nullptr; 311 return nullptr;
333 } 312 }
334 313
335 bool doBicubic; 314 bool doBicubic;
336 GrTextureParams::FilterMode diffFilterMode = GrSkFilterQualityToGrFilterMode ( 315 GrTextureParams::FilterMode diffFilterMode = GrSkFilterQualityToGrFilterMode (
337 SkTMin(filterQuality, kMediu m_SkFilterQuality), 316 SkTMin(filterQuality, kMediu m_SkFilterQuality),
338 viewM, 317 viewM,
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 467
489 sk_sp<SkFlattenable> SkLightingShaderImpl::CreateProc(SkReadBuffer& buf) { 468 sk_sp<SkFlattenable> SkLightingShaderImpl::CreateProc(SkReadBuffer& buf) {
490 SkMatrix diffLocalM; 469 SkMatrix diffLocalM;
491 bool hasDiffLocalM = buf.readBool(); 470 bool hasDiffLocalM = buf.readBool();
492 if (hasDiffLocalM) { 471 if (hasDiffLocalM) {
493 buf.readMatrix(&diffLocalM); 472 buf.readMatrix(&diffLocalM);
494 } else { 473 } else {
495 diffLocalM.reset(); 474 diffLocalM.reset();
496 } 475 }
497 476
498 SkMatrix normLocalM;
499 bool hasNormLocalM = buf.readBool();
500 if (hasNormLocalM) {
501 buf.readMatrix(&normLocalM);
502 } else {
503 normLocalM.reset();
504 }
505
506 SkBitmap diffuse; 477 SkBitmap diffuse;
507 if (!buf.readBitmap(&diffuse)) { 478 if (!buf.readBitmap(&diffuse)) {
508 return nullptr; 479 return nullptr;
509 } 480 }
510 diffuse.setImmutable(); 481 diffuse.setImmutable();
511 482
512 SkBitmap normal;
513 if (!buf.readBitmap(&normal)) {
514 return nullptr;
515 }
516 normal.setImmutable();
517
518 int numLights = buf.readInt(); 483 int numLights = buf.readInt();
519 484
520 SkLights::Builder builder; 485 SkLights::Builder builder;
521 486
522 for (int l = 0; l < numLights; ++l) { 487 for (int l = 0; l < numLights; ++l) {
523 bool isAmbient = buf.readBool(); 488 bool isAmbient = buf.readBool();
524 489
525 SkColor3f color; 490 SkColor3f color;
526 if (!buf.readScalarArray(&color.fX, 3)) { 491 if (!buf.readScalarArray(&color.fX, 3)) {
527 return nullptr; 492 return nullptr;
528 } 493 }
529 494
530 if (isAmbient) { 495 if (isAmbient) {
531 builder.add(SkLights::Light(color)); 496 builder.add(SkLights::Light(color));
532 } else { 497 } else {
533 SkVector3 dir; 498 SkVector3 dir;
534 if (!buf.readScalarArray(&dir.fX, 3)) { 499 if (!buf.readScalarArray(&dir.fX, 3)) {
535 return nullptr; 500 return nullptr;
536 } 501 }
537 builder.add(SkLights::Light(color, dir)); 502 builder.add(SkLights::Light(color, dir));
538 } 503 }
539 } 504 }
540 505
541 sk_sp<SkLights> lights(builder.finish()); 506 sk_sp<SkLights> lights(builder.finish());
542 507
543 SkVector invNormRotation = {1,0};
544 if (!buf.isVersionLT(SkReadBuffer::kLightingShaderWritesInvNormRotation)) {
545 invNormRotation = buf.readPoint();
546 }
547
548 sk_sp<SkLightingShader::NormalSource> normalSource( 508 sk_sp<SkLightingShader::NormalSource> normalSource(
549 buf.readFlattenable<SkLightingShader::NormalSource>()); 509 buf.readFlattenable<SkLightingShader::NormalSource>());
550 510
551 return sk_make_sp<SkLightingShaderImpl>(diffuse, normal, std::move(lights), invNormRotation, 511 return sk_make_sp<SkLightingShaderImpl>(diffuse, std::move(lights), &diffLoc alM,
552 &diffLocalM, &normLocalM, std::move( normalSource)); 512 std::move(normalSource));
553 } 513 }
554 514
555 void SkLightingShaderImpl::flatten(SkWriteBuffer& buf) const { 515 void SkLightingShaderImpl::flatten(SkWriteBuffer& buf) const {
556 this->INHERITED::flatten(buf); 516 this->INHERITED::flatten(buf);
557 517
558 bool hasNormLocalM = !fNormLocalMatrix.isIdentity();
559 buf.writeBool(hasNormLocalM);
560 if (hasNormLocalM) {
561 buf.writeMatrix(fNormLocalMatrix);
562 }
563
564 buf.writeBitmap(fDiffuseMap); 518 buf.writeBitmap(fDiffuseMap);
565 buf.writeBitmap(fNormalMap);
566 519
567 buf.writeInt(fLights->numLights()); 520 buf.writeInt(fLights->numLights());
568 for (int l = 0; l < fLights->numLights(); ++l) { 521 for (int l = 0; l < fLights->numLights(); ++l) {
569 const SkLights::Light& light = fLights->light(l); 522 const SkLights::Light& light = fLights->light(l);
570 523
571 bool isAmbient = SkLights::Light::kAmbient_LightType == light.type(); 524 bool isAmbient = SkLights::Light::kAmbient_LightType == light.type();
572 525
573 buf.writeBool(isAmbient); 526 buf.writeBool(isAmbient);
574 buf.writeScalarArray(&light.color().fX, 3); 527 buf.writeScalarArray(&light.color().fX, 3);
575 if (!isAmbient) { 528 if (!isAmbient) {
576 buf.writeScalarArray(&light.dir().fX, 3); 529 buf.writeScalarArray(&light.dir().fX, 3);
577 } 530 }
578 } 531 }
579 buf.writePoint(fInvNormRotation);
580 532
581 buf.writeFlattenable(fNormalSource.get()); 533 buf.writeFlattenable(fNormalSource.get());
582 } 534 }
583 535
584 size_t SkLightingShaderImpl::onContextSize(const ContextRec&) const { 536 size_t SkLightingShaderImpl::onContextSize(const ContextRec&) const {
585 return sizeof(LightingShaderContext) + 537 return sizeof(LightingShaderContext) +
586 sizeof(SkBitmapProcState) + 538 sizeof(SkBitmapProcState) +
587 fNormalSource->providerSize(); 539 fNormalSource->providerSize();
588 } 540 }
589 541
(...skipping 22 matching lines...) Expand all
612 if (!normalProvider) { 564 if (!normalProvider) {
613 diffuseState->~SkBitmapProcState(); 565 diffuseState->~SkBitmapProcState();
614 return nullptr; 566 return nullptr;
615 } 567 }
616 568
617 return new (storage) LightingShaderContext(*this, rec, diffuseState, normalP rovider); 569 return new (storage) LightingShaderContext(*this, rec, diffuseState, normalP rovider);
618 } 570 }
619 571
620 /////////////////////////////////////////////////////////////////////////////// 572 ///////////////////////////////////////////////////////////////////////////////
621 573
622 sk_sp<SkShader> SkLightingShader::Make(const SkBitmap& diffuse, const SkBitmap& normal, 574 sk_sp<SkShader> SkLightingShader::Make(const SkBitmap& diffuse, sk_sp<SkLights> lights,
623 sk_sp<SkLights> lights, 575 const SkMatrix* diffLocalM, sk_sp<NormalSource> normalSource) {
egdaniel 2016/06/16 13:15:08 We should either check or at minimum assert that n
dvonbeck 2016/06/16 23:21:56 Done. Will return a default implementation of Norm
624 const SkVector& invNormRotation, 576 if (diffuse.isNull() || SkBitmapProcShader::BitmapIsTooBig(diffuse)) {
625 const SkMatrix* diffLocalM, const SkMatri x* normLocalM) {
626 if (diffuse.isNull() || SkBitmapProcShader::BitmapIsTooBig(diffuse) ||
627 normal.isNull() || SkBitmapProcShader::BitmapIsTooBig(normal) ||
628 diffuse.width() != normal.width() ||
629 diffuse.height() != normal.height()) {
630 return nullptr; 577 return nullptr;
631 } 578 }
632 SkASSERT(SkScalarNearlyEqual(invNormRotation.lengthSqd(), SK_Scalar1));
633 579
634 sk_sp<SkLightingShader::NormalSource> normalSource = 580 return sk_make_sp<SkLightingShaderImpl>(diffuse, std::move(lights), diffLoca lM,
635 SkLightingShader::NormalMapSource::Make(normal, invNormRotation, nor mLocalM); 581 std::move(normalSource));
636
637 return sk_make_sp<SkLightingShaderImpl>(diffuse, normal, std::move(lights),
638 invNormRotation, diffLocalM, normLocalM, std::move(normalSource));
639 } 582 }
640 583
641 /////////////////////////////////////////////////////////////////////////////// 584 ///////////////////////////////////////////////////////////////////////////////
642 585
643 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader) 586 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader)
644 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl) 587 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl)
645 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END 588 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
646 589
647 /////////////////////////////////////////////////////////////////////////////// 590 ///////////////////////////////////////////////////////////////////////////////
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698