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

Side by Side Diff: src/gpu/effects/GrConvexPolyEffect.cpp

Issue 1251173002: Added GrGLFragmentProcessor::EmitArgs struct for use with emitCode() (Closed) Base URL: https://skia.googlesource.com/skia@composeshader_gpu
Patch Set: Created 5 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/gpu/effects/GrConstColorProcessor.cpp ('k') | src/gpu/effects/GrConvolutionEffect.cpp » ('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 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"
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 } while (NULL == fp); 74 } while (NULL == fp);
75 return fp; 75 return fp;
76 } 76 }
77 77
78 ////////////////////////////////////////////////////////////////////////////// 78 //////////////////////////////////////////////////////////////////////////////
79 79
80 class GLAARectEffect : public GrGLFragmentProcessor { 80 class GLAARectEffect : public GrGLFragmentProcessor {
81 public: 81 public:
82 GLAARectEffect(const GrProcessor&); 82 GLAARectEffect(const GrProcessor&);
83 83
84 virtual void emitCode(GrGLFPBuilder* builder, 84 virtual void emitCode(EmitArgs&) override;
85 const GrFragmentProcessor& fp,
86 const char* outputColor,
87 const char* inputColor,
88 const TransformedCoordsArray&,
89 const TextureSamplerArray&) override;
90 85
91 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessor KeyBuilder*); 86 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessor KeyBuilder*);
92 87
93 void setData(const GrGLProgramDataManager&, const GrProcessor&) override; 88 void setData(const GrGLProgramDataManager&, const GrProcessor&) override;
94 89
95 private: 90 private:
96 GrGLProgramDataManager::UniformHandle fRectUniform; 91 GrGLProgramDataManager::UniformHandle fRectUniform;
97 SkRect fPrevRect; 92 SkRect fPrevRect;
98 typedef GrGLFragmentProcessor INHERITED; 93 typedef GrGLFragmentProcessor INHERITED;
99 }; 94 };
100 95
101 GLAARectEffect::GLAARectEffect(const GrProcessor& effect) { 96 GLAARectEffect::GLAARectEffect(const GrProcessor& effect) {
102 fPrevRect.fLeft = SK_ScalarNaN; 97 fPrevRect.fLeft = SK_ScalarNaN;
103 } 98 }
104 99
105 void GLAARectEffect::emitCode(GrGLFPBuilder* builder, 100 void GLAARectEffect::emitCode(EmitArgs& args) {
106 const GrFragmentProcessor& fp, 101 const AARectEffect& aare = args.fFp.cast<AARectEffect>();
107 const char* outputColor,
108 const char* inputColor,
109 const TransformedCoordsArray&,
110 const TextureSamplerArray& samplers) {
111 const AARectEffect& aare = fp.cast<AARectEffect>();
112 const char *rectName; 102 const char *rectName;
113 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bot tom - 0.5), 103 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bot tom - 0.5),
114 // respectively. 104 // respectively.
115 fRectUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, 105 fRectUniform = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visib ility,
116 kVec4f_GrSLType, 106 kVec4f_GrSLType,
117 kDefault_GrSLPrecision, 107 kDefault_GrSLPrecision,
118 "rect", 108 "rect",
119 &rectName); 109 &rectName);
120 110
121 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); 111 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder();
122 const char* fragmentPos = fsBuilder->fragmentPosition(); 112 const char* fragmentPos = fsBuilder->fragmentPosition();
123 if (GrProcessorEdgeTypeIsAA(aare.getEdgeType())) { 113 if (GrProcessorEdgeTypeIsAA(aare.getEdgeType())) {
124 // The amount of coverage removed in x and y by the edges is computed as a pair of negative 114 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
125 // numbers, xSub and ySub. 115 // numbers, xSub and ySub.
126 fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n"); 116 fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
127 fsBuilder->codeAppendf("\t\txSub = min(%s.x - %s.x, 0.0);\n", fragmentPo s, rectName); 117 fsBuilder->codeAppendf("\t\txSub = min(%s.x - %s.x, 0.0);\n", fragmentPo s, rectName);
128 fsBuilder->codeAppendf("\t\txSub += min(%s.z - %s.x, 0.0);\n", rectName, fragmentPos); 118 fsBuilder->codeAppendf("\t\txSub += min(%s.z - %s.x, 0.0);\n", rectName, fragmentPos);
129 fsBuilder->codeAppendf("\t\tySub = min(%s.y - %s.y, 0.0);\n", fragmentPo s, rectName); 119 fsBuilder->codeAppendf("\t\tySub = min(%s.y - %s.y, 0.0);\n", fragmentPo s, rectName);
130 fsBuilder->codeAppendf("\t\tySub += min(%s.w - %s.y, 0.0);\n", rectName, fragmentPos); 120 fsBuilder->codeAppendf("\t\tySub += min(%s.w - %s.y, 0.0);\n", rectName, fragmentPos);
131 // Now compute coverage in x and y and multiply them to get the fraction of the pixel 121 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
132 // covered. 122 // covered.
133 fsBuilder->codeAppendf("\t\tfloat alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));\n"); 123 fsBuilder->codeAppendf("\t\tfloat alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));\n");
134 } else { 124 } else {
135 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n"); 125 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
136 fsBuilder->codeAppendf("\t\talpha *= (%s.x - %s.x) > -0.5 ? 1.0 : 0.0;\n ", fragmentPos, rectName); 126 fsBuilder->codeAppendf("\t\talpha *= (%s.x - %s.x) > -0.5 ? 1.0 : 0.0;\n ", fragmentPos, rectName);
137 fsBuilder->codeAppendf("\t\talpha *= (%s.z - %s.x) > -0.5 ? 1.0 : 0.0;\n ", rectName, fragmentPos); 127 fsBuilder->codeAppendf("\t\talpha *= (%s.z - %s.x) > -0.5 ? 1.0 : 0.0;\n ", rectName, fragmentPos);
138 fsBuilder->codeAppendf("\t\talpha *= (%s.y - %s.y) > -0.5 ? 1.0 : 0.0;\n ", fragmentPos, rectName); 128 fsBuilder->codeAppendf("\t\talpha *= (%s.y - %s.y) > -0.5 ? 1.0 : 0.0;\n ", fragmentPos, rectName);
139 fsBuilder->codeAppendf("\t\talpha *= (%s.w - %s.y) > -0.5 ? 1.0 : 0.0;\n ", rectName, fragmentPos); 129 fsBuilder->codeAppendf("\t\talpha *= (%s.w - %s.y) > -0.5 ? 1.0 : 0.0;\n ", rectName, fragmentPos);
140 } 130 }
141 131
142 if (GrProcessorEdgeTypeIsInverseFill(aare.getEdgeType())) { 132 if (GrProcessorEdgeTypeIsInverseFill(aare.getEdgeType())) {
143 fsBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n"); 133 fsBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n");
144 } 134 }
145 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor, 135 fsBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutputColor,
146 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_st r()); 136 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha") ).c_str());
147 } 137 }
148 138
149 void GLAARectEffect::setData(const GrGLProgramDataManager& pdman, const GrProces sor& processor) { 139 void GLAARectEffect::setData(const GrGLProgramDataManager& pdman, const GrProces sor& processor) {
150 const AARectEffect& aare = processor.cast<AARectEffect>(); 140 const AARectEffect& aare = processor.cast<AARectEffect>();
151 const SkRect& rect = aare.getRect(); 141 const SkRect& rect = aare.getRect();
152 if (rect != fPrevRect) { 142 if (rect != fPrevRect) {
153 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f, 143 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
154 rect.fRight - 0.5f, rect.fBottom - 0.5f); 144 rect.fRight - 0.5f, rect.fBottom - 0.5f);
155 fPrevRect = rect; 145 fPrevRect = rect;
156 } 146 }
(...skipping 12 matching lines...) Expand all
169 GrGLFragmentProcessor* AARectEffect::createGLInstance() const { 159 GrGLFragmentProcessor* AARectEffect::createGLInstance() const {
170 return SkNEW_ARGS(GLAARectEffect, (*this)); 160 return SkNEW_ARGS(GLAARectEffect, (*this));
171 } 161 }
172 162
173 ////////////////////////////////////////////////////////////////////////////// 163 //////////////////////////////////////////////////////////////////////////////
174 164
175 class GrGLConvexPolyEffect : public GrGLFragmentProcessor { 165 class GrGLConvexPolyEffect : public GrGLFragmentProcessor {
176 public: 166 public:
177 GrGLConvexPolyEffect(const GrProcessor&); 167 GrGLConvexPolyEffect(const GrProcessor&);
178 168
179 virtual void emitCode(GrGLFPBuilder* builder, 169 virtual void emitCode(EmitArgs&) override;
180 const GrFragmentProcessor& fp,
181 const char* outputColor,
182 const char* inputColor,
183 const TransformedCoordsArray&,
184 const TextureSamplerArray&) override;
185 170
186 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessor KeyBuilder*); 171 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessor KeyBuilder*);
187 172
188 void setData(const GrGLProgramDataManager&, const GrProcessor&) override; 173 void setData(const GrGLProgramDataManager&, const GrProcessor&) override;
189 174
190 private: 175 private:
191 GrGLProgramDataManager::UniformHandle fEdgeUniform; 176 GrGLProgramDataManager::UniformHandle fEdgeUniform;
192 SkScalar fPrevEdges[3 * GrConvexPolyEffect::kMa xEdges]; 177 SkScalar fPrevEdges[3 * GrConvexPolyEffect::kMa xEdges];
193 typedef GrGLFragmentProcessor INHERITED; 178 typedef GrGLFragmentProcessor INHERITED;
194 }; 179 };
195 180
196 GrGLConvexPolyEffect::GrGLConvexPolyEffect(const GrProcessor&) { 181 GrGLConvexPolyEffect::GrGLConvexPolyEffect(const GrProcessor&) {
197 fPrevEdges[0] = SK_ScalarNaN; 182 fPrevEdges[0] = SK_ScalarNaN;
198 } 183 }
199 184
200 void GrGLConvexPolyEffect::emitCode(GrGLFPBuilder* builder, 185 void GrGLConvexPolyEffect::emitCode(EmitArgs& args) {
201 const GrFragmentProcessor& fp, 186 const GrConvexPolyEffect& cpe = args.fFp.cast<GrConvexPolyEffect>();
202 const char* outputColor,
203 const char* inputColor,
204 const TransformedCoordsArray&,
205 const TextureSamplerArray& samplers) {
206 const GrConvexPolyEffect& cpe = fp.cast<GrConvexPolyEffect>();
207 187
208 const char *edgeArrayName; 188 const char *edgeArrayName;
209 fEdgeUniform = builder->addUniformArray(GrGLProgramBuilder::kFragment_Visibi lity, 189 fEdgeUniform = args.fBuilder->addUniformArray(GrGLProgramBuilder::kFragment_ Visibility,
210 kVec3f_GrSLType, 190 kVec3f_GrSLType,
211 kDefault_GrSLPrecision, 191 kDefault_GrSLPrecision,
212 "edges", 192 "edges",
213 cpe.getEdgeCount(), 193 cpe.getEdgeCount(),
214 &edgeArrayName); 194 &edgeArrayName);
215 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); 195 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder();
216 fsBuilder->codeAppend("\t\tfloat alpha = 1.0;\n"); 196 fsBuilder->codeAppend("\t\tfloat alpha = 1.0;\n");
217 fsBuilder->codeAppend("\t\tfloat edge;\n"); 197 fsBuilder->codeAppend("\t\tfloat edge;\n");
218 const char* fragmentPos = fsBuilder->fragmentPosition(); 198 const char* fragmentPos = fsBuilder->fragmentPosition();
219 for (int i = 0; i < cpe.getEdgeCount(); ++i) { 199 for (int i = 0; i < cpe.getEdgeCount(); ++i) {
220 fsBuilder->codeAppendf("\t\tedge = dot(%s[%d], vec3(%s.x, %s.y, 1));\n", 200 fsBuilder->codeAppendf("\t\tedge = dot(%s[%d], vec3(%s.x, %s.y, 1));\n",
221 edgeArrayName, i, fragmentPos, fragmentPos); 201 edgeArrayName, i, fragmentPos, fragmentPos);
222 if (GrProcessorEdgeTypeIsAA(cpe.getEdgeType())) { 202 if (GrProcessorEdgeTypeIsAA(cpe.getEdgeType())) {
223 fsBuilder->codeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n"); 203 fsBuilder->codeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n");
224 } else { 204 } else {
225 fsBuilder->codeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n"); 205 fsBuilder->codeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n");
226 } 206 }
227 fsBuilder->codeAppend("\t\talpha *= edge;\n"); 207 fsBuilder->codeAppend("\t\talpha *= edge;\n");
228 } 208 }
229 209
230 // Woe is me. See skbug.com/2149. 210 // Woe is me. See skbug.com/2149.
231 if (kTegra2_GrGLRenderer == builder->ctxInfo().renderer()) { 211 if (kTegra2_GrGLRenderer == args.fBuilder->ctxInfo().renderer()) {
232 fsBuilder->codeAppend("\t\tif (-1.0 == alpha) {\n\t\t\tdiscard;\n\t\t}\n "); 212 fsBuilder->codeAppend("\t\tif (-1.0 == alpha) {\n\t\t\tdiscard;\n\t\t}\n ");
233 } 213 }
234 214
235 if (GrProcessorEdgeTypeIsInverseFill(cpe.getEdgeType())) { 215 if (GrProcessorEdgeTypeIsInverseFill(cpe.getEdgeType())) {
236 fsBuilder->codeAppend("\talpha = 1.0 - alpha;\n"); 216 fsBuilder->codeAppend("\talpha = 1.0 - alpha;\n");
237 } 217 }
238 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor, 218 fsBuilder->codeAppendf("\t%s = %s;\n", args.fOutputColor,
239 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_st r()); 219 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha") ).c_str());
240 } 220 }
241 221
242 void GrGLConvexPolyEffect::setData(const GrGLProgramDataManager& pdman, const Gr Processor& effect) { 222 void GrGLConvexPolyEffect::setData(const GrGLProgramDataManager& pdman, const Gr Processor& effect) {
243 const GrConvexPolyEffect& cpe = effect.cast<GrConvexPolyEffect>(); 223 const GrConvexPolyEffect& cpe = effect.cast<GrConvexPolyEffect>();
244 size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar); 224 size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar);
245 if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) { 225 if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) {
246 pdman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges()); 226 pdman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges());
247 memcpy(fPrevEdges, cpe.getEdges(), byteSize); 227 memcpy(fPrevEdges, cpe.getEdges(), byteSize);
248 } 228 }
249 } 229 }
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 } 345 }
366 346
367 GrFragmentProcessor* fp; 347 GrFragmentProcessor* fp;
368 do { 348 do {
369 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>( 349 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(
370 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt)); 350 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt));
371 fp = GrConvexPolyEffect::Create(edgeType, count, edges); 351 fp = GrConvexPolyEffect::Create(edgeType, count, edges);
372 } while (NULL == fp); 352 } while (NULL == fp);
373 return fp; 353 return fp;
374 } 354 }
OLDNEW
« no previous file with comments | « src/gpu/effects/GrConstColorProcessor.cpp ('k') | src/gpu/effects/GrConvolutionEffect.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698