OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 "GrConvexPolyEffect.h" | 8 #include "GrConvexPolyEffect.h" |
9 #include "GrInvariantOutput.h" | 9 #include "GrInvariantOutput.h" |
10 #include "SkPathPriv.h" | 10 #include "SkPathPriv.h" |
11 #include "gl/GrGLContext.h" | 11 #include "gl/GrGLContext.h" |
12 #include "gl/GrGLFragmentProcessor.h" | 12 #include "gl/GrGLFragmentProcessor.h" |
13 #include "gl/builders/GrGLProgramBuilder.h" | 13 #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 14 #include "glsl/GrGLSLProgramBuilder.h" |
14 #include "glsl/GrGLSLProgramDataManager.h" | 15 #include "glsl/GrGLSLProgramDataManager.h" |
15 | 16 |
16 ////////////////////////////////////////////////////////////////////////////// | 17 ////////////////////////////////////////////////////////////////////////////// |
17 class AARectEffect : public GrFragmentProcessor { | 18 class AARectEffect : public GrFragmentProcessor { |
18 public: | 19 public: |
19 const SkRect& getRect() const { return fRect; } | 20 const SkRect& getRect() const { return fRect; } |
20 | 21 |
21 static GrFragmentProcessor* Create(GrPrimitiveEdgeType edgeType, const SkRec
t& rect) { | 22 static GrFragmentProcessor* Create(GrPrimitiveEdgeType edgeType, const SkRec
t& rect) { |
22 return new AARectEffect(edgeType, rect); | 23 return new AARectEffect(edgeType, rect); |
23 } | 24 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 | 99 |
99 GLAARectEffect::GLAARectEffect(const GrProcessor& effect) { | 100 GLAARectEffect::GLAARectEffect(const GrProcessor& effect) { |
100 fPrevRect.fLeft = SK_ScalarNaN; | 101 fPrevRect.fLeft = SK_ScalarNaN; |
101 } | 102 } |
102 | 103 |
103 void GLAARectEffect::emitCode(EmitArgs& args) { | 104 void GLAARectEffect::emitCode(EmitArgs& args) { |
104 const AARectEffect& aare = args.fFp.cast<AARectEffect>(); | 105 const AARectEffect& aare = args.fFp.cast<AARectEffect>(); |
105 const char *rectName; | 106 const char *rectName; |
106 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bot
tom - 0.5), | 107 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bot
tom - 0.5), |
107 // respectively. | 108 // respectively. |
108 fRectUniform = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visib
ility, | 109 fRectUniform = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFragment_Vis
ibility, |
109 kVec4f_GrSLType, | 110 kVec4f_GrSLType, |
110 kDefault_GrSLPrecision, | 111 kDefault_GrSLPrecision, |
111 "rect", | 112 "rect", |
112 &rectName); | 113 &rectName); |
113 | 114 |
114 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder(); | 115 GrGLSLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder()
; |
115 const char* fragmentPos = fsBuilder->fragmentPosition(); | 116 const char* fragmentPos = fsBuilder->fragmentPosition(); |
116 if (GrProcessorEdgeTypeIsAA(aare.getEdgeType())) { | 117 if (GrProcessorEdgeTypeIsAA(aare.getEdgeType())) { |
117 // The amount of coverage removed in x and y by the edges is computed as
a pair of negative | 118 // The amount of coverage removed in x and y by the edges is computed as
a pair of negative |
118 // numbers, xSub and ySub. | 119 // numbers, xSub and ySub. |
119 fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n"); | 120 fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n"); |
120 fsBuilder->codeAppendf("\t\txSub = min(%s.x - %s.x, 0.0);\n", fragmentPo
s, rectName); | 121 fsBuilder->codeAppendf("\t\txSub = min(%s.x - %s.x, 0.0);\n", fragmentPo
s, rectName); |
121 fsBuilder->codeAppendf("\t\txSub += min(%s.z - %s.x, 0.0);\n", rectName,
fragmentPos); | 122 fsBuilder->codeAppendf("\t\txSub += min(%s.z - %s.x, 0.0);\n", rectName,
fragmentPos); |
122 fsBuilder->codeAppendf("\t\tySub = min(%s.y - %s.y, 0.0);\n", fragmentPo
s, rectName); | 123 fsBuilder->codeAppendf("\t\tySub = min(%s.y - %s.y, 0.0);\n", fragmentPo
s, rectName); |
123 fsBuilder->codeAppendf("\t\tySub += min(%s.w - %s.y, 0.0);\n", rectName,
fragmentPos); | 124 fsBuilder->codeAppendf("\t\tySub += min(%s.w - %s.y, 0.0);\n", rectName,
fragmentPos); |
124 // Now compute coverage in x and y and multiply them to get the fraction
of the pixel | 125 // Now compute coverage in x and y and multiply them to get the fraction
of the pixel |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 }; | 185 }; |
185 | 186 |
186 GrGLConvexPolyEffect::GrGLConvexPolyEffect(const GrProcessor&) { | 187 GrGLConvexPolyEffect::GrGLConvexPolyEffect(const GrProcessor&) { |
187 fPrevEdges[0] = SK_ScalarNaN; | 188 fPrevEdges[0] = SK_ScalarNaN; |
188 } | 189 } |
189 | 190 |
190 void GrGLConvexPolyEffect::emitCode(EmitArgs& args) { | 191 void GrGLConvexPolyEffect::emitCode(EmitArgs& args) { |
191 const GrConvexPolyEffect& cpe = args.fFp.cast<GrConvexPolyEffect>(); | 192 const GrConvexPolyEffect& cpe = args.fFp.cast<GrConvexPolyEffect>(); |
192 | 193 |
193 const char *edgeArrayName; | 194 const char *edgeArrayName; |
194 fEdgeUniform = args.fBuilder->addUniformArray(GrGLProgramBuilder::kFragment_
Visibility, | 195 fEdgeUniform = args.fBuilder->addUniformArray(GrGLSLProgramBuilder::kFragmen
t_Visibility, |
195 kVec3f_GrSLType, | 196 kVec3f_GrSLType, |
196 kDefault_GrSLPrecision, | 197 kDefault_GrSLPrecision, |
197 "edges", | 198 "edges", |
198 cpe.getEdgeCount(), | 199 cpe.getEdgeCount(), |
199 &edgeArrayName); | 200 &edgeArrayName); |
200 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder(); | 201 GrGLSLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder()
; |
201 fsBuilder->codeAppend("\t\tfloat alpha = 1.0;\n"); | 202 fsBuilder->codeAppend("\t\tfloat alpha = 1.0;\n"); |
202 fsBuilder->codeAppend("\t\tfloat edge;\n"); | 203 fsBuilder->codeAppend("\t\tfloat edge;\n"); |
203 const char* fragmentPos = fsBuilder->fragmentPosition(); | 204 const char* fragmentPos = fsBuilder->fragmentPosition(); |
204 for (int i = 0; i < cpe.getEdgeCount(); ++i) { | 205 for (int i = 0; i < cpe.getEdgeCount(); ++i) { |
205 fsBuilder->codeAppendf("\t\tedge = dot(%s[%d], vec3(%s.x, %s.y, 1));\n", | 206 fsBuilder->codeAppendf("\t\tedge = dot(%s[%d], vec3(%s.x, %s.y, 1));\n", |
206 edgeArrayName, i, fragmentPos, fragmentPos); | 207 edgeArrayName, i, fragmentPos, fragmentPos); |
207 if (GrProcessorEdgeTypeIsAA(cpe.getEdgeType())) { | 208 if (GrProcessorEdgeTypeIsAA(cpe.getEdgeType())) { |
208 fsBuilder->codeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n"); | 209 fsBuilder->codeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n"); |
209 } else { | 210 } else { |
210 fsBuilder->codeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n"); | 211 fsBuilder->codeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n"); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 return AARectEffect::Create(edgeType, rect); | 298 return AARectEffect::Create(edgeType, rect); |
298 } | 299 } |
299 | 300 |
300 GrConvexPolyEffect::~GrConvexPolyEffect() {} | 301 GrConvexPolyEffect::~GrConvexPolyEffect() {} |
301 | 302 |
302 void GrConvexPolyEffect::onComputeInvariantOutput(GrInvariantOutput* inout) cons
t { | 303 void GrConvexPolyEffect::onComputeInvariantOutput(GrInvariantOutput* inout) cons
t { |
303 inout->mulByUnknownSingleComponent(); | 304 inout->mulByUnknownSingleComponent(); |
304 } | 305 } |
305 | 306 |
306 void GrConvexPolyEffect::onGetGLProcessorKey(const GrGLSLCaps& caps, | 307 void GrConvexPolyEffect::onGetGLProcessorKey(const GrGLSLCaps& caps, |
307 GrProcessorKeyBuilder* b) const { | 308 GrProcessorKeyBuilder* b) const { |
308 GrGLConvexPolyEffect::GenKey(*this, caps, b); | 309 GrGLConvexPolyEffect::GenKey(*this, caps, b); |
309 } | 310 } |
310 | 311 |
311 GrGLFragmentProcessor* GrConvexPolyEffect::onCreateGLInstance() const { | 312 GrGLFragmentProcessor* GrConvexPolyEffect::onCreateGLInstance() const { |
312 return new GrGLConvexPolyEffect(*this); | 313 return new GrGLConvexPolyEffect(*this); |
313 } | 314 } |
314 | 315 |
315 GrConvexPolyEffect::GrConvexPolyEffect(GrPrimitiveEdgeType edgeType, int n, cons
t SkScalar edges[]) | 316 GrConvexPolyEffect::GrConvexPolyEffect(GrPrimitiveEdgeType edgeType, int n, cons
t SkScalar edges[]) |
316 : fEdgeType(edgeType) | 317 : fEdgeType(edgeType) |
317 , fEdgeCount(n) { | 318 , fEdgeCount(n) { |
(...skipping 28 matching lines...) Expand all Loading... |
346 } | 347 } |
347 | 348 |
348 GrFragmentProcessor* fp; | 349 GrFragmentProcessor* fp; |
349 do { | 350 do { |
350 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>( | 351 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>( |
351 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt)); | 352 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt)); |
352 fp = GrConvexPolyEffect::Create(edgeType, count, edges); | 353 fp = GrConvexPolyEffect::Create(edgeType, count, edges); |
353 } while (nullptr == fp); | 354 } while (nullptr == fp); |
354 return fp; | 355 return fp; |
355 } | 356 } |
OLD | NEW |