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

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

Issue 2114993002: GrFP can express distance vector field req., program builder declares variable for it (Closed) Base URL: https://skia.googlesource.com/skia@dvonbeck-bevel-api-change
Patch Set: Addressed patch 9 comments, guarded onSetData from invalid calls 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
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 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 "SkError.h" 8 #include "SkError.h"
9 #include "SkErrorInternals.h" 9 #include "SkErrorInternals.h"
10 #include "SkLightingShader.h" 10 #include "SkLightingShader.h"
11 #include "SkMatrix.h" 11 #include "SkMatrix.h"
12 #include "SkNormalSource.h" 12 #include "SkNormalSource.h"
13 #include "SkPM4f.h" 13 #include "SkPM4f.h"
14 #include "SkReadBuffer.h" 14 #include "SkReadBuffer.h"
15 #include "SkWriteBuffer.h" 15 #include "SkWriteBuffer.h"
16 16
17 // Genretating vtable 17 #if SK_SUPPORT_GPU
18 #include "GrCoordTransform.h"
19 #include "GrInvariantOutput.h"
20 #include "GrTextureParams.h"
21 #include "glsl/GrGLSLFragmentProcessor.h"
22 #include "glsl/GrGLSLFragmentShaderBuilder.h"
23 #include "SkGr.h"
24 #endif
25
26 // Generating vtable
18 SkNormalSource::~SkNormalSource() {} 27 SkNormalSource::~SkNormalSource() {}
19 28
29 #if SK_SUPPORT_GPU
30
31 // GLSLFragmentProcessors for NormalSourceImpls must sub-class this class and ov erride onEmitCode.
32 // This class exists to intercept emitCode calls and emit <0, 0, 1> if the FP re quires a distance
33 // vector but the GP doesn't provide it. onSetData calls need to be intercepted too because
34 // uniform handlers will be invalid in subclasses where onEmitCode isn't called.
35 // We don't need to adjust the key here since the use of a given GP (through its class ID already in
36 // the key), will determine what code gets emitted here.
37 class GLSLNormalFP : public GrGLSLFragmentProcessor {
38 public:
39 GLSLNormalFP()
40 : fDidIntercept(false) {}
41
42 void emitCode(EmitArgs& args) override {
egdaniel 2016/07/14 02:47:46 lets mark emitCode and onSetData here as final so
dvonbeck 2016/07/14 13:40:34 Done.
43 if (args.fFp.usesDistanceVectorField() && !args.fGpImplementsDistanceVec tor) {
44 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
45 fragBuilder->codeAppendf("// GLSLNormalFP intercepted emitCode call, GP does not "
46 "implement required distance vector feature \n");
47 fragBuilder->codeAppendf("%s = vec4(0, 0, 1, 0);", args.fOutputColor );
48
49 fDidIntercept = true;
50 } else {
51 this->onEmitCode(args);
52 }
53 }
54
55 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& pro c) {
56 if (!fDidIntercept) {
57 this->onOnSetData(pdman, proc);
58 }
59 }
60
61 protected:
62 virtual void onEmitCode(EmitArgs& args) = 0;
63 virtual void onOnSetData(const GrGLSLProgramDataManager& pdman, const GrProc essor& proc) = 0;
egdaniel 2016/07/14 02:47:46 hmm this looks ugly. Maybe make it setNormalData?
dvonbeck 2016/07/14 13:40:34 Done.
64
65 private:
66 bool fDidIntercept;
67 };
68
69 #endif
70
20 /////////////////////////////////////////////////////////////////////////////// 71 ///////////////////////////////////////////////////////////////////////////////
21 72
22 class NormalMapSourceImpl : public SkNormalSource { 73 class NormalMapSourceImpl : public SkNormalSource {
23 public: 74 public:
24 NormalMapSourceImpl(sk_sp<SkShader> mapShader, const SkMatrix& invCTM) 75 NormalMapSourceImpl(sk_sp<SkShader> mapShader, const SkMatrix& invCTM)
25 : fMapShader(std::move(mapShader)) 76 : fMapShader(std::move(mapShader))
26 , fInvCTM(invCTM) {} 77 , fInvCTM(invCTM) {}
27 78
28 #if SK_SUPPORT_GPU 79 #if SK_SUPPORT_GPU
29 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, 80 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*,
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 119
69 friend class SkNormalSource; 120 friend class SkNormalSource;
70 121
71 typedef SkNormalSource INHERITED; 122 typedef SkNormalSource INHERITED;
72 }; 123 };
73 124
74 //////////////////////////////////////////////////////////////////////////// 125 ////////////////////////////////////////////////////////////////////////////
75 126
76 #if SK_SUPPORT_GPU 127 #if SK_SUPPORT_GPU
77 128
78 #include "GrCoordTransform.h"
79 #include "GrInvariantOutput.h"
80 #include "GrTextureParams.h"
81 #include "glsl/GrGLSLFragmentProcessor.h"
82 #include "glsl/GrGLSLFragmentShaderBuilder.h"
83 #include "SkGr.h"
84
85 class NormalMapFP : public GrFragmentProcessor { 129 class NormalMapFP : public GrFragmentProcessor {
86 public: 130 public:
87 NormalMapFP(sk_sp<GrFragmentProcessor> mapFP, const SkMatrix& invCTM) 131 NormalMapFP(sk_sp<GrFragmentProcessor> mapFP, const SkMatrix& invCTM)
88 : fInvCTM(invCTM) { 132 : fInvCTM(invCTM) {
89 this->registerChildProcessor(mapFP); 133 this->registerChildProcessor(mapFP);
90 134
91 this->initClassID<NormalMapFP>(); 135 this->initClassID<NormalMapFP>();
92 } 136 }
93 137
94 class GLSLNormalMapFP : public GrGLSLFragmentProcessor { 138 class GLSLNormalMapFP : public GLSLNormalFP {
95 public: 139 public:
96 GLSLNormalMapFP() 140 GLSLNormalMapFP()
97 : fColumnMajorInvCTM22{0.0f} {} 141 : fColumnMajorInvCTM22{0.0f} {}
98 142
99 void emitCode(EmitArgs& args) override { 143 void onEmitCode(EmitArgs& args) override {
100 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; 144 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
101 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; 145 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
102 146
103 // add uniform 147 // add uniform
104 const char* xformUniName = nullptr; 148 const char* xformUniName = nullptr;
105 fXformUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kMat2 2f_GrSLType, 149 fXformUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kMat2 2f_GrSLType,
106 kDefault_GrSLPrecision, "Xfor m", &xformUniName); 150 kDefault_GrSLPrecision, "Xfor m", &xformUniName);
107 151
108 SkString dstNormalColorName("dstNormalColor"); 152 SkString dstNormalColorName("dstNormalColor");
109 this->emitChild(0, nullptr, &dstNormalColorName, args); 153 this->emitChild(0, nullptr, &dstNormalColorName, args);
(...skipping 21 matching lines...) Expand all
131 args.fOutputColor); 175 args.fOutputColor);
132 fragBuilder->codeAppend( "}"); 176 fragBuilder->codeAppend( "}");
133 } 177 }
134 178
135 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, 179 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&,
136 GrProcessorKeyBuilder* b) { 180 GrProcessorKeyBuilder* b) {
137 b->add32(0x0); 181 b->add32(0x0);
138 } 182 }
139 183
140 protected: 184 protected:
141 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override { 185 void onOnSetData(const GrGLSLProgramDataManager& pdman, const GrProcesso r& proc) override {
142 const NormalMapFP& normalMapFP = proc.cast<NormalMapFP>(); 186 const NormalMapFP& normalMapFP = proc.cast<NormalMapFP>();
143 187
144 const SkMatrix& invCTM = normalMapFP.invCTM(); 188 const SkMatrix& invCTM = normalMapFP.invCTM();
145 fColumnMajorInvCTM22[0] = invCTM.get(SkMatrix::kMScaleX); 189 fColumnMajorInvCTM22[0] = invCTM.get(SkMatrix::kMScaleX);
146 fColumnMajorInvCTM22[1] = invCTM.get(SkMatrix::kMSkewY); 190 fColumnMajorInvCTM22[1] = invCTM.get(SkMatrix::kMSkewY);
147 fColumnMajorInvCTM22[2] = invCTM.get(SkMatrix::kMSkewX); 191 fColumnMajorInvCTM22[2] = invCTM.get(SkMatrix::kMSkewX);
148 fColumnMajorInvCTM22[3] = invCTM.get(SkMatrix::kMScaleY); 192 fColumnMajorInvCTM22[3] = invCTM.get(SkMatrix::kMScaleY);
149 pdman.setMatrix2f(fXformUni, fColumnMajorInvCTM22); 193 pdman.setMatrix2f(fXformUni, fColumnMajorInvCTM22);
150 } 194 }
151 195
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 //////////////////////////////////////////////////////////////////////////// 419 ////////////////////////////////////////////////////////////////////////////
376 420
377 #if SK_SUPPORT_GPU 421 #if SK_SUPPORT_GPU
378 422
379 class NormalFlatFP : public GrFragmentProcessor { 423 class NormalFlatFP : public GrFragmentProcessor {
380 public: 424 public:
381 NormalFlatFP() { 425 NormalFlatFP() {
382 this->initClassID<NormalFlatFP>(); 426 this->initClassID<NormalFlatFP>();
383 } 427 }
384 428
385 class GLSLNormalFlatFP : public GrGLSLFragmentProcessor { 429 class GLSLNormalFlatFP : public GLSLNormalFP {
386 public: 430 public:
387 GLSLNormalFlatFP() {} 431 GLSLNormalFlatFP() {}
388 432
389 void emitCode(EmitArgs& args) override { 433 void onEmitCode(EmitArgs& args) override {
390 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; 434 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
391
392 fragBuilder->codeAppendf("%s = vec4(0, 0, 1, 0);", args.fOutputColor ); 435 fragBuilder->codeAppendf("%s = vec4(0, 0, 1, 0);", args.fOutputColor );
393 } 436 }
394 437
395 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, 438 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&,
396 GrProcessorKeyBuilder* b) { 439 GrProcessorKeyBuilder* b) {
397 b->add32(0x0); 440 b->add32(0x0);
398 } 441 }
399 442
400 protected: 443 protected:
401 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override {} 444 void onOnSetData(const GrGLSLProgramDataManager& pdman, const GrProcesso r& proc) override {}
402 }; 445 };
403 446
404 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { 447 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {
405 GLSLNormalFlatFP::GenKey(*this, caps, b); 448 GLSLNormalFlatFP::GenKey(*this, caps, b);
406 } 449 }
407 450
408 const char* name() const override { return "NormalFlatFP"; } 451 const char* name() const override { return "NormalFlatFP"; }
409 452
410 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { 453 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
411 inout->setToUnknown(GrInvariantOutput::ReadInput::kWillNot_ReadInput); 454 inout->setToUnknown(GrInvariantOutput::ReadInput::kWillNot_ReadInput);
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
523 //////////////////////////////////////////////////////////////////////////// 566 ////////////////////////////////////////////////////////////////////////////
524 567
525 #if SK_SUPPORT_GPU 568 #if SK_SUPPORT_GPU
526 569
527 class NormalBevelFP : public GrFragmentProcessor { 570 class NormalBevelFP : public GrFragmentProcessor {
528 public: 571 public:
529 NormalBevelFP(SkNormalSource::BevelType type, SkScalar width, SkScalar heigh t) 572 NormalBevelFP(SkNormalSource::BevelType type, SkScalar width, SkScalar heigh t)
530 : fType(type) 573 : fType(type)
531 , fWidth(width) 574 , fWidth(width)
532 , fHeight(height) { 575 , fHeight(height) {
576 fUsesDistanceVectorField = true;
577
533 this->initClassID<NormalBevelFP>(); 578 this->initClassID<NormalBevelFP>();
534 } 579 }
535 580
536 class GLSLNormalBevelFP : public GrGLSLFragmentProcessor { 581 class GLSLNormalBevelFP : public GLSLNormalFP {
537 public: 582 public:
538 GLSLNormalBevelFP() { 583 GLSLNormalBevelFP() {
539 fPrevType = SkNormalSource::BevelType::kLinear; 584 fPrevType = SkNormalSource::BevelType::kLinear;
540 fPrevWidth = SkFloatToScalar(0.0f); 585 fPrevWidth = SkFloatToScalar(0.0f);
541 fPrevHeight = SkFloatToScalar(0.0f); 586 fPrevHeight = SkFloatToScalar(0.0f);
542 } 587 }
543 588
544 void emitCode(EmitArgs& args) override { 589 void onEmitCode(EmitArgs& args) override {
545 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; 590 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
546 591 fragBuilder->codeAppendf("%s = normalize(vec4(0.0, 0.0, 1.0, 0.0));" ,
547 fragBuilder->codeAppendf("%s = vec4(0, 0, 1, 0);", args.fOutputColor ); 592 args.fOutputColor);
548 } 593 }
549 594
550 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, 595 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&,
551 GrProcessorKeyBuilder* b) { 596 GrProcessorKeyBuilder* b) {
552 b->add32(0x0); 597 b->add32(0x0);
553 } 598 }
554 599
555 protected: 600 protected:
556 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override { 601 void onOnSetData(const GrGLSLProgramDataManager& pdman, const GrProcesso r& proc) override {
557 const NormalBevelFP& normalBevelFP = proc.cast<NormalBevelFP>(); 602 const NormalBevelFP& normalBevelFP = proc.cast<NormalBevelFP>();
558 603
559 fPrevType = normalBevelFP.fType; 604 fPrevType = normalBevelFP.fType;
560 fPrevWidth = normalBevelFP.fWidth; 605 fPrevWidth = normalBevelFP.fWidth;
561 fPrevHeight = normalBevelFP.fHeight; 606 fPrevHeight = normalBevelFP.fHeight;
562 } 607 }
563 608
564 private: 609 private:
565 SkNormalSource::BevelType fPrevType; 610 SkNormalSource::BevelType fPrevType;
566 SkScalar fPrevWidth; 611 SkScalar fPrevWidth;
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
656 701
657 //////////////////////////////////////////////////////////////////////////// 702 ////////////////////////////////////////////////////////////////////////////
658 703
659 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkNormalSource) 704 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkNormalSource)
660 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(NormalMapSourceImpl) 705 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(NormalMapSourceImpl)
661 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(NormalFlatSourceImpl) 706 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(NormalFlatSourceImpl)
662 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(NormalBevelSourceImpl) 707 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(NormalBevelSourceImpl)
663 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END 708 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
664 709
665 //////////////////////////////////////////////////////////////////////////// 710 ////////////////////////////////////////////////////////////////////////////
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698