OLD | NEW |
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 "SkArithmeticMode_gpu.h" | 8 #include "SkArithmeticMode_gpu.h" |
9 | 9 |
10 #if SK_SUPPORT_GPU | 10 #if SK_SUPPORT_GPU |
11 #include "GrContext.h" | 11 #include "GrContext.h" |
12 #include "GrFragmentProcessor.h" | 12 #include "GrFragmentProcessor.h" |
13 #include "GrInvariantOutput.h" | 13 #include "GrInvariantOutput.h" |
14 #include "GrProcessor.h" | 14 #include "GrProcessor.h" |
15 #include "GrTexture.h" | 15 #include "GrTexture.h" |
16 #include "gl/GrGLCaps.h" | 16 #include "gl/GrGLCaps.h" |
17 #include "gl/GrGLFragmentProcessor.h" | 17 #include "gl/GrGLFragmentProcessor.h" |
18 #include "gl/GrGLProgramDataManager.h" | 18 #include "gl/GrGLProgramDataManager.h" |
19 #include "gl/builders/GrGLProgramBuilder.h" | 19 #include "gl/builders/GrGLProgramBuilder.h" |
20 | 20 |
21 static const bool gUseUnpremul = false; | 21 static const bool gUseUnpremul = false; |
22 | 22 |
23 static void add_arithmetic_code(GrGLFragmentBuilder* fsBuilder, | 23 static void add_arithmetic_code(GrGLFragmentBuilder* fsBuilder, |
24 const char* inputColor, | 24 const char* srcColor, |
25 const char* dstColor, | 25 const char* dstColor, |
26 const char* outputColor, | 26 const char* outputColor, |
27 const char* kUni, | 27 const char* kUni, |
28 bool enforcePMColor) { | 28 bool enforcePMColor) { |
29 // We don't try to optimize for this case at all | 29 // We don't try to optimize for this case at all |
30 if (nullptr == inputColor) { | 30 if (nullptr == srcColor) { |
31 fsBuilder->codeAppend("const vec4 src = vec4(1);"); | 31 fsBuilder->codeAppend("const vec4 src = vec4(1);"); |
32 } else { | 32 } else { |
33 fsBuilder->codeAppendf("vec4 src = %s;", inputColor); | 33 fsBuilder->codeAppendf("vec4 src = %s;", srcColor); |
34 if (gUseUnpremul) { | 34 if (gUseUnpremul) { |
35 fsBuilder->codeAppend("src.rgb = clamp(src.rgb / src.a, 0.0, 1.0);")
; | 35 fsBuilder->codeAppend("src.rgb = clamp(src.rgb / src.a, 0.0, 1.0);")
; |
36 } | 36 } |
37 } | 37 } |
38 | 38 |
39 fsBuilder->codeAppendf("vec4 dst = %s;", dstColor); | 39 fsBuilder->codeAppendf("vec4 dst = %s;", dstColor); |
40 if (gUseUnpremul) { | 40 if (gUseUnpremul) { |
41 fsBuilder->codeAppend("dst.rgb = clamp(dst.rgb / dst.a, 0.0, 1.0);"); | 41 fsBuilder->codeAppend("dst.rgb = clamp(dst.rgb / dst.a, 0.0, 1.0);"); |
42 } | 42 } |
43 | 43 |
44 fsBuilder->codeAppendf("%s = %s.x * src * dst + %s.y * src + %s.z * dst + %s
.w;", | 44 fsBuilder->codeAppendf("%s = %s.x * src * dst + %s.y * src + %s.z * dst + %s
.w;", |
45 outputColor, kUni, kUni, kUni, kUni); | 45 outputColor, kUni, kUni, kUni, kUni); |
46 fsBuilder->codeAppendf("%s = clamp(%s, 0.0, 1.0);\n", outputColor, outputCol
or); | 46 fsBuilder->codeAppendf("%s = clamp(%s, 0.0, 1.0);\n", outputColor, outputCol
or); |
47 if (gUseUnpremul) { | 47 if (gUseUnpremul) { |
48 fsBuilder->codeAppendf("%s.rgb *= %s.a;", outputColor, outputColor); | 48 fsBuilder->codeAppendf("%s.rgb *= %s.a;", outputColor, outputColor); |
49 } else if (enforcePMColor) { | 49 } else if (enforcePMColor) { |
50 fsBuilder->codeAppendf("%s.rgb = min(%s.rgb, %s.a);", | 50 fsBuilder->codeAppendf("%s.rgb = min(%s.rgb, %s.a);", |
51 outputColor, outputColor, outputColor); | 51 outputColor, outputColor, outputColor); |
52 } | 52 } |
53 } | 53 } |
54 | 54 |
55 class GLArithmeticFP : public GrGLFragmentProcessor { | 55 class GLArithmeticFP : public GrGLFragmentProcessor { |
56 public: | 56 public: |
57 GLArithmeticFP(const GrProcessor&) | 57 GLArithmeticFP(const GrProcessor&) : fEnforcePMColor(true) {} |
58 : fEnforcePMColor(true) { | |
59 } | |
60 | 58 |
61 ~GLArithmeticFP() override {} | 59 ~GLArithmeticFP() override {} |
62 | 60 |
63 void emitCode(EmitArgs& args) override { | 61 void emitCode(EmitArgs& args) override { |
64 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder
(); | 62 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder
(); |
65 fsBuilder->codeAppend("vec4 bgColor = "); | 63 fsBuilder->codeAppend("vec4 _dstColor;"); |
66 fsBuilder->appendTextureLookup(args.fSamplers[0], args.fCoords[0].c_str(
), | 64 this->emitChild(0, nullptr, "_dstColor", args); |
67 args.fCoords[0].getType()); | |
68 fsBuilder->codeAppendf(";"); | |
69 const char* dstColor = "bgColor"; | |
70 | 65 |
71 fKUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibili
ty, | 66 fKUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibili
ty, |
72 kVec4f_GrSLType, kDefault_GrSLPrecision, | 67 kVec4f_GrSLType, kDefault_GrSLPrecisio
n, |
73 "k"); | 68 "k"); |
74 const char* kUni = args.fBuilder->getUniformCStr(fKUni); | 69 const char* kUni = args.fBuilder->getUniformCStr(fKUni); |
75 | 70 |
76 add_arithmetic_code(fsBuilder, args.fInputColor, dstColor, args.fOutputC
olor, kUni, | 71 add_arithmetic_code(fsBuilder, args.fInputColor, "_dstColor", args.fOutp
utColor, kUni, |
77 fEnforcePMColor); | 72 fEnforcePMColor); |
78 } | 73 } |
79 | 74 |
80 static void GenKey(const GrProcessor& proc, const GrGLSLCaps& caps, GrProces
sorKeyBuilder* b) { | 75 static void GenKey(const GrProcessor& proc, const GrGLSLCaps& caps, GrProces
sorKeyBuilder* b) { |
81 const GrArithmeticFP& arith = proc.cast<GrArithmeticFP>(); | 76 const GrArithmeticFP& arith = proc.cast<GrArithmeticFP>(); |
82 uint32_t key = arith.enforcePMColor() ? 1 : 0; | 77 uint32_t key = arith.enforcePMColor() ? 1 : 0; |
83 b->add32(key); | 78 b->add32(key); |
84 } | 79 } |
85 | 80 |
86 protected: | 81 protected: |
87 void onSetData(const GrGLProgramDataManager& pdman, const GrProcessor& proc)
override { | 82 void onSetData(const GrGLProgramDataManager& pdman, const GrProcessor& proc)
override { |
88 const GrArithmeticFP& arith = proc.cast<GrArithmeticFP>(); | 83 const GrArithmeticFP& arith = proc.cast<GrArithmeticFP>(); |
89 pdman.set4f(fKUni, arith.k1(), arith.k2(), arith.k3(), arith.k4()); | 84 pdman.set4f(fKUni, arith.k1(), arith.k2(), arith.k3(), arith.k4()); |
90 fEnforcePMColor = arith.enforcePMColor(); | 85 fEnforcePMColor = arith.enforcePMColor(); |
91 } | 86 } |
92 | 87 |
93 private: | 88 private: |
94 GrGLProgramDataManager::UniformHandle fKUni; | 89 GrGLProgramDataManager::UniformHandle fKUni; |
95 bool fEnforcePMColor; | 90 bool fEnforcePMColor; |
96 | 91 |
97 typedef GrGLFragmentProcessor INHERITED; | 92 typedef GrGLFragmentProcessor INHERITED; |
98 }; | 93 }; |
99 | 94 |
100 /////////////////////////////////////////////////////////////////////////////// | 95 /////////////////////////////////////////////////////////////////////////////// |
101 | 96 |
102 GrArithmeticFP::GrArithmeticFP(GrProcessorDataManager*, float k1, float k2, floa
t k3, float k4, | 97 GrArithmeticFP::GrArithmeticFP(GrProcessorDataManager*, float k1, float k2, floa
t k3, float k4, |
103 bool enforcePMColor, GrTexture* background) | 98 bool enforcePMColor, const GrFragmentProcessor* d
st) |
104 : fK1(k1), fK2(k2), fK3(k3), fK4(k4), fEnforcePMColor(enforcePMColor) { | 99 : fK1(k1), fK2(k2), fK3(k3), fK4(k4), fEnforcePMColor(enforcePMColor) { |
105 this->initClassID<GrArithmeticFP>(); | 100 this->initClassID<GrArithmeticFP>(); |
106 | 101 |
107 SkASSERT(background); | 102 SkASSERT(dst); |
108 | 103 SkDEBUGCODE(int dstIndex = )this->registerChildProcessor(dst); |
109 fBackgroundTransform.reset(kLocal_GrCoordSet, background, | 104 SkASSERT(0 == dstIndex); |
110 GrTextureParams::kNone_FilterMode); | |
111 this->addCoordTransform(&fBackgroundTransform); | |
112 fBackgroundAccess.reset(background); | |
113 this->addTextureAccess(&fBackgroundAccess); | |
114 } | 105 } |
115 | 106 |
116 void GrArithmeticFP::onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyB
uilder* b) const { | 107 void GrArithmeticFP::onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyB
uilder* b) const { |
117 GLArithmeticFP::GenKey(*this, caps, b); | 108 GLArithmeticFP::GenKey(*this, caps, b); |
118 } | 109 } |
119 | 110 |
120 GrGLFragmentProcessor* GrArithmeticFP::onCreateGLInstance() const { | 111 GrGLFragmentProcessor* GrArithmeticFP::onCreateGLInstance() const { |
121 return new GLArithmeticFP(*this); | 112 return new GLArithmeticFP(*this); |
122 } | 113 } |
123 | 114 |
(...skipping 13 matching lines...) Expand all Loading... |
137 | 128 |
138 /////////////////////////////////////////////////////////////////////////////// | 129 /////////////////////////////////////////////////////////////////////////////// |
139 | 130 |
140 const GrFragmentProcessor* GrArithmeticFP::TestCreate(GrProcessorTestData* d) { | 131 const GrFragmentProcessor* GrArithmeticFP::TestCreate(GrProcessorTestData* d) { |
141 float k1 = d->fRandom->nextF(); | 132 float k1 = d->fRandom->nextF(); |
142 float k2 = d->fRandom->nextF(); | 133 float k2 = d->fRandom->nextF(); |
143 float k3 = d->fRandom->nextF(); | 134 float k3 = d->fRandom->nextF(); |
144 float k4 = d->fRandom->nextF(); | 135 float k4 = d->fRandom->nextF(); |
145 bool enforcePMColor = d->fRandom->nextBool(); | 136 bool enforcePMColor = d->fRandom->nextBool(); |
146 | 137 |
147 return new GrArithmeticFP(d->fProcDataManager, k1, k2, k3, k4, enforcePMColo
r, d->fTextures[0]); | 138 SkAutoTUnref<const GrFragmentProcessor> dst(GrProcessorUnitTest::CreateChild
FP(d)); |
| 139 return new GrArithmeticFP(d->fProcDataManager, k1, k2, k3, k4, enforcePMColo
r, dst); |
148 } | 140 } |
149 | 141 |
150 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrArithmeticFP); | 142 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrArithmeticFP); |
151 | 143 |
152 /////////////////////////////////////////////////////////////////////////////// | 144 /////////////////////////////////////////////////////////////////////////////// |
153 // Xfer Processor | 145 // Xfer Processor |
154 /////////////////////////////////////////////////////////////////////////////// | 146 /////////////////////////////////////////////////////////////////////////////// |
155 | 147 |
156 class ArithmeticXP : public GrXferProcessor { | 148 class ArithmeticXP : public GrXferProcessor { |
157 public: | 149 public: |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 float k1 = d->fRandom->nextF(); | 291 float k1 = d->fRandom->nextF(); |
300 float k2 = d->fRandom->nextF(); | 292 float k2 = d->fRandom->nextF(); |
301 float k3 = d->fRandom->nextF(); | 293 float k3 = d->fRandom->nextF(); |
302 float k4 = d->fRandom->nextF(); | 294 float k4 = d->fRandom->nextF(); |
303 bool enforcePMColor = d->fRandom->nextBool(); | 295 bool enforcePMColor = d->fRandom->nextBool(); |
304 | 296 |
305 return GrArithmeticXPFactory::Create(k1, k2, k3, k4, enforcePMColor); | 297 return GrArithmeticXPFactory::Create(k1, k2, k3, k4, enforcePMColor); |
306 } | 298 } |
307 | 299 |
308 #endif | 300 #endif |
OLD | NEW |