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

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: Addressed remaining patch 8 comments Created 4 years, 5 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
« no previous file with comments | « src/core/SkLightingShader.h ('k') | src/core/SkNormalSource.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "SkBitmapProcShader.h" 8 #include "SkBitmapProcShader.h"
9 #include "SkBitmapProcState.h" 9 #include "SkBitmapProcState.h"
10 #include "SkColor.h" 10 #include "SkColor.h"
(...skipping 27 matching lines...) Expand all
38 38
39 /** \class SkLightingShaderImpl 39 /** \class SkLightingShaderImpl
40 This subclass of shader applies lighting. 40 This subclass of shader applies lighting.
41 */ 41 */
42 class SkLightingShaderImpl : public SkShader { 42 class SkLightingShaderImpl : public SkShader {
43 public: 43 public:
44 44
45 /** Create a new lighting shader that uses the provided normal map and 45 /** Create a new lighting shader that uses the provided normal map and
46 lights to light the diffuse bitmap. 46 lights to light the diffuse bitmap.
47 @param diffuse the diffuse bitmap 47 @param diffuse the diffuse bitmap
48 @param normal the normal map
49 @param lights the lights applied to the normal map 48 @param lights the lights applied to the normal map
50 @param invNormRotation rotation applied to the normal map's normals
51 @param diffLocalM the local matrix for the diffuse coordinates 49 @param diffLocalM the local matrix for the diffuse coordinates
52 @param normLocalM the local matrix for the normal coordinates 50 @param normalSource the source of normals for lighting computation
53 @param normalSource the normal source for GPU computations
54 */ 51 */
55 SkLightingShaderImpl(const SkBitmap& diffuse, const SkBitmap& normal, 52 SkLightingShaderImpl(const SkBitmap& diffuse,
56 const sk_sp<SkLights> lights, 53 const sk_sp<SkLights> lights,
57 const SkVector& invNormRotation, 54 const SkMatrix* diffLocalM,
58 const SkMatrix* diffLocalM, const SkMatrix* normLocalM,
59 sk_sp<SkNormalSource> normalSource) 55 sk_sp<SkNormalSource> normalSource)
60 : INHERITED(diffLocalM) 56 : INHERITED(diffLocalM)
61 , fDiffuseMap(diffuse) 57 , fDiffuseMap(diffuse)
62 , fNormalMap(normal)
63 , fLights(std::move(lights)) 58 , fLights(std::move(lights))
64 , fInvNormRotation(invNormRotation) { 59 , fNormalSource(std::move(normalSource)) {}
65
66 if (normLocalM) {
67 fNormLocalMatrix = *normLocalM;
68 } else {
69 fNormLocalMatrix.reset();
70 }
71 // Pre-cache so future calls to fNormLocalMatrix.getType() are threadsaf e.
72 (void)fNormLocalMatrix.getType();
73
74 fNormalSource = std::move(normalSource);
75 }
76 60
77 bool isOpaque() const override; 61 bool isOpaque() const override;
78 62
79 #if SK_SUPPORT_GPU 63 #if SK_SUPPORT_GPU
80 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, 64 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*,
81 const SkMatrix& viewM, 65 const SkMatrix& viewM,
82 const SkMatrix* localMatrix, 66 const SkMatrix* localMatrix,
83 SkFilterQuality, 67 SkFilterQuality,
84 SkSourceGammaTreatment) const override; 68 SkSourceGammaTreatment) const override;
85 #endif 69 #endif
(...skipping 24 matching lines...) Expand all
110 SK_TO_STRING_OVERRIDE() 94 SK_TO_STRING_OVERRIDE()
111 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLightingShaderImpl) 95 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLightingShaderImpl)
112 96
113 protected: 97 protected:
114 void flatten(SkWriteBuffer&) const override; 98 void flatten(SkWriteBuffer&) const override;
115 size_t onContextSize(const ContextRec&) const override; 99 size_t onContextSize(const ContextRec&) const override;
116 Context* onCreateContext(const ContextRec&, void*) const override; 100 Context* onCreateContext(const ContextRec&, void*) const override;
117 101
118 private: 102 private:
119 SkBitmap fDiffuseMap; 103 SkBitmap fDiffuseMap;
120 SkBitmap fNormalMap;
121
122 sk_sp<SkLights> fLights; 104 sk_sp<SkLights> fLights;
123 105
124 SkMatrix fNormLocalMatrix;
125 SkVector fInvNormRotation;
126
127 sk_sp<SkNormalSource> fNormalSource; 106 sk_sp<SkNormalSource> fNormalSource;
128 107
129 friend class SkLightingShader; 108 friend class SkLightingShader;
130 109
131 typedef SkShader INHERITED; 110 typedef SkShader INHERITED;
132 }; 111 };
133 112
134 //////////////////////////////////////////////////////////////////////////// 113 ////////////////////////////////////////////////////////////////////////////
135 114
136 #if SK_SUPPORT_GPU 115 #if SK_SUPPORT_GPU
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 299
321 sk_sp<GrFragmentProcessor> SkLightingShaderImpl::asFragmentProcessor( 300 sk_sp<GrFragmentProcessor> SkLightingShaderImpl::asFragmentProcessor(
322 GrContext* context, 301 GrContext* context,
323 const SkMatrix& viewM, 302 const SkMatrix& viewM,
324 const SkMatrix* localMatrix , 303 const SkMatrix* localMatrix ,
325 SkFilterQuality filterQuali ty, 304 SkFilterQuality filterQuali ty,
326 SkSourceGammaTreatment gamm aTreatment) const { 305 SkSourceGammaTreatment gamm aTreatment) const {
327 // we assume diffuse and normal maps have same width and height 306 // we assume diffuse and normal maps have same width and height
328 // TODO: support different sizes, will be addressed when diffuse maps are fa ctored out of 307 // TODO: support different sizes, will be addressed when diffuse maps are fa ctored out of
329 // SkLightingShader in a future CL 308 // SkLightingShader in a future CL
330 SkASSERT(fDiffuseMap.width() == fNormalMap.width() &&
331 fDiffuseMap.height() == fNormalMap.height());
332 SkMatrix diffM; 309 SkMatrix diffM;
333 310
334 if (!make_mat(fDiffuseMap, this->getLocalMatrix(), localMatrix, &diffM)) { 311 if (!make_mat(fDiffuseMap, this->getLocalMatrix(), localMatrix, &diffM)) {
335 return nullptr; 312 return nullptr;
336 } 313 }
337 314
338 bool doBicubic; 315 bool doBicubic;
339 GrTextureParams::FilterMode diffFilterMode = GrSkFilterQualityToGrFilterMode ( 316 GrTextureParams::FilterMode diffFilterMode = GrSkFilterQualityToGrFilterMode (
340 SkTMin(filterQuality, kMediu m_SkFilterQuality), 317 SkTMin(filterQuality, kMediu m_SkFilterQuality),
341 viewM, 318 viewM,
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 470
494 sk_sp<SkFlattenable> SkLightingShaderImpl::CreateProc(SkReadBuffer& buf) { 471 sk_sp<SkFlattenable> SkLightingShaderImpl::CreateProc(SkReadBuffer& buf) {
495 SkMatrix diffLocalM; 472 SkMatrix diffLocalM;
496 bool hasDiffLocalM = buf.readBool(); 473 bool hasDiffLocalM = buf.readBool();
497 if (hasDiffLocalM) { 474 if (hasDiffLocalM) {
498 buf.readMatrix(&diffLocalM); 475 buf.readMatrix(&diffLocalM);
499 } else { 476 } else {
500 diffLocalM.reset(); 477 diffLocalM.reset();
501 } 478 }
502 479
503 SkMatrix normLocalM;
504 bool hasNormLocalM = buf.readBool();
505 if (hasNormLocalM) {
506 buf.readMatrix(&normLocalM);
507 } else {
508 normLocalM.reset();
509 }
510
511 SkBitmap diffuse; 480 SkBitmap diffuse;
512 if (!buf.readBitmap(&diffuse)) { 481 if (!buf.readBitmap(&diffuse)) {
513 return nullptr; 482 return nullptr;
514 } 483 }
515 diffuse.setImmutable(); 484 diffuse.setImmutable();
516 485
517 SkBitmap normal;
518 if (!buf.readBitmap(&normal)) {
519 return nullptr;
520 }
521 normal.setImmutable();
522
523 int numLights = buf.readInt(); 486 int numLights = buf.readInt();
524 487
525 SkLights::Builder builder; 488 SkLights::Builder builder;
526 489
527 for (int l = 0; l < numLights; ++l) { 490 for (int l = 0; l < numLights; ++l) {
528 bool isAmbient = buf.readBool(); 491 bool isAmbient = buf.readBool();
529 492
530 SkColor3f color; 493 SkColor3f color;
531 if (!buf.readScalarArray(&color.fX, 3)) { 494 if (!buf.readScalarArray(&color.fX, 3)) {
532 return nullptr; 495 return nullptr;
533 } 496 }
534 497
535 if (isAmbient) { 498 if (isAmbient) {
536 builder.add(SkLights::Light(color)); 499 builder.add(SkLights::Light(color));
537 } else { 500 } else {
538 SkVector3 dir; 501 SkVector3 dir;
539 if (!buf.readScalarArray(&dir.fX, 3)) { 502 if (!buf.readScalarArray(&dir.fX, 3)) {
540 return nullptr; 503 return nullptr;
541 } 504 }
542 builder.add(SkLights::Light(color, dir)); 505 builder.add(SkLights::Light(color, dir));
543 } 506 }
544 } 507 }
545 508
546 sk_sp<SkLights> lights(builder.finish()); 509 sk_sp<SkLights> lights(builder.finish());
547 510
548 SkVector invNormRotation = {1,0};
549 if (!buf.isVersionLT(SkReadBuffer::kLightingShaderWritesInvNormRotation)) {
550 invNormRotation = buf.readPoint();
551 }
552
553 sk_sp<SkNormalSource> normalSource(buf.readFlattenable<SkNormalSource>()); 511 sk_sp<SkNormalSource> normalSource(buf.readFlattenable<SkNormalSource>());
554 512
555 return sk_make_sp<SkLightingShaderImpl>(diffuse, normal, std::move(lights), invNormRotation, 513 return sk_make_sp<SkLightingShaderImpl>(diffuse, std::move(lights), &diffLoc alM,
556 &diffLocalM, &normLocalM, std::move( normalSource)); 514 std::move(normalSource));
557 } 515 }
558 516
559 void SkLightingShaderImpl::flatten(SkWriteBuffer& buf) const { 517 void SkLightingShaderImpl::flatten(SkWriteBuffer& buf) const {
560 this->INHERITED::flatten(buf); 518 this->INHERITED::flatten(buf);
561 519
562 bool hasNormLocalM = !fNormLocalMatrix.isIdentity();
563 buf.writeBool(hasNormLocalM);
564 if (hasNormLocalM) {
565 buf.writeMatrix(fNormLocalMatrix);
566 }
567
568 buf.writeBitmap(fDiffuseMap); 520 buf.writeBitmap(fDiffuseMap);
569 buf.writeBitmap(fNormalMap);
570 521
571 buf.writeInt(fLights->numLights()); 522 buf.writeInt(fLights->numLights());
572 for (int l = 0; l < fLights->numLights(); ++l) { 523 for (int l = 0; l < fLights->numLights(); ++l) {
573 const SkLights::Light& light = fLights->light(l); 524 const SkLights::Light& light = fLights->light(l);
574 525
575 bool isAmbient = SkLights::Light::kAmbient_LightType == light.type(); 526 bool isAmbient = SkLights::Light::kAmbient_LightType == light.type();
576 527
577 buf.writeBool(isAmbient); 528 buf.writeBool(isAmbient);
578 buf.writeScalarArray(&light.color().fX, 3); 529 buf.writeScalarArray(&light.color().fX, 3);
579 if (!isAmbient) { 530 if (!isAmbient) {
580 buf.writeScalarArray(&light.dir().fX, 3); 531 buf.writeScalarArray(&light.dir().fX, 3);
581 } 532 }
582 } 533 }
583 buf.writePoint(fInvNormRotation);
584 534
585 buf.writeFlattenable(fNormalSource.get()); 535 buf.writeFlattenable(fNormalSource.get());
586 } 536 }
587 537
588 size_t SkLightingShaderImpl::onContextSize(const ContextRec& rec) const { 538 size_t SkLightingShaderImpl::onContextSize(const ContextRec& rec) const {
589 return sizeof(LightingShaderContext); 539 return sizeof(LightingShaderContext);
590 } 540 }
591 541
592 SkShader::Context* SkLightingShaderImpl::onCreateContext(const ContextRec& rec, 542 SkShader::Context* SkLightingShaderImpl::onCreateContext(const ContextRec& rec,
593 void* storage) const { 543 void* storage) const {
(...skipping 24 matching lines...) Expand all
618 sk_free(heapAllocated); 568 sk_free(heapAllocated);
619 return nullptr; 569 return nullptr;
620 } 570 }
621 571
622 return new (storage) LightingShaderContext(*this, rec, diffuseState, normalP rovider, 572 return new (storage) LightingShaderContext(*this, rec, diffuseState, normalP rovider,
623 heapAllocated); 573 heapAllocated);
624 } 574 }
625 575
626 /////////////////////////////////////////////////////////////////////////////// 576 ///////////////////////////////////////////////////////////////////////////////
627 577
628 sk_sp<SkShader> SkLightingShader::Make(const SkBitmap& diffuse, const SkBitmap& normal, 578 sk_sp<SkShader> SkLightingShader::Make(const SkBitmap& diffuse,
629 sk_sp<SkLights> lights, 579 sk_sp<SkLights> lights,
630 const SkVector& invNormRotation, 580 const SkMatrix* diffLocalM,
631 const SkMatrix* diffLocalM, const SkMatri x* normLocalM) { 581 sk_sp<SkNormalSource> normalSource) {
632 if (diffuse.isNull() || SkBitmapProcShader::BitmapIsTooBig(diffuse) || 582 if (diffuse.isNull() || SkBitmapProcShader::BitmapIsTooBig(diffuse)) {
633 normal.isNull() || SkBitmapProcShader::BitmapIsTooBig(normal) ||
634 diffuse.width() != normal.width() ||
635 diffuse.height() != normal.height()) {
636 return nullptr; 583 return nullptr;
637 } 584 }
638 SkASSERT(SkScalarNearlyEqual(invNormRotation.lengthSqd(), SK_Scalar1));
639 585
640 // TODO: support other tile modes 586 if (!normalSource) {
641 sk_sp<SkShader> mapShader = SkMakeBitmapShader(normal, SkShader::kClamp_Tile Mode, 587 // TODO: Use a default implementation of normalSource instead
642 SkShader::kClamp_TileMode, no rmLocalM, nullptr); 588 return nullptr;
589 }
643 590
644 sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeFromNormalMap(mapSh ader, 591 return sk_make_sp<SkLightingShaderImpl>(diffuse, std::move(lights), diffLoca lM,
645 invNo rmRotation); 592 std::move(normalSource));
646
647 return sk_make_sp<SkLightingShaderImpl>(diffuse, normal, std::move(lights),
648 invNormRotation, diffLocalM, normLocalM, std::move(normalSource));
649 } 593 }
650 594
651 /////////////////////////////////////////////////////////////////////////////// 595 ///////////////////////////////////////////////////////////////////////////////
652 596
653 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader) 597 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader)
654 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl) 598 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl)
655 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END 599 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
656 600
657 /////////////////////////////////////////////////////////////////////////////// 601 ///////////////////////////////////////////////////////////////////////////////
OLDNEW
« no previous file with comments | « src/core/SkLightingShader.h ('k') | src/core/SkNormalSource.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698