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

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

Issue 491673002: Initial refactor of shaderbuilder (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 4 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/GrConfigConversionEffect.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 "gl/builders/GrGLProgramBuilder.h"
8 #include "GrConvexPolyEffect.h" 9 #include "GrConvexPolyEffect.h"
9 10
10 #include "gl/GrGLEffect.h" 11 #include "gl/GrGLEffect.h"
11 #include "gl/GrGLShaderBuilder.h"
12 #include "gl/GrGLSL.h" 12 #include "gl/GrGLSL.h"
13 #include "GrTBackendEffectFactory.h" 13 #include "GrTBackendEffectFactory.h"
14 14
15 #include "SkPath.h" 15 #include "SkPath.h"
16 16
17 ////////////////////////////////////////////////////////////////////////////// 17 //////////////////////////////////////////////////////////////////////////////
18 class GLAARectEffect; 18 class GLAARectEffect;
19 19
20 class AARectEffect : public GrEffect { 20 class AARectEffect : public GrEffect {
21 public: 21 public:
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 } while (NULL == effect); 82 } while (NULL == effect);
83 return effect; 83 return effect;
84 } 84 }
85 85
86 ////////////////////////////////////////////////////////////////////////////// 86 //////////////////////////////////////////////////////////////////////////////
87 87
88 class GLAARectEffect : public GrGLEffect { 88 class GLAARectEffect : public GrGLEffect {
89 public: 89 public:
90 GLAARectEffect(const GrBackendEffectFactory&, const GrDrawEffect&); 90 GLAARectEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
91 91
92 virtual void emitCode(GrGLShaderBuilder* builder, 92 virtual void emitCode(GrGLProgramBuilder* builder,
93 const GrDrawEffect& drawEffect, 93 const GrDrawEffect& drawEffect,
94 const GrEffectKey& key, 94 const GrEffectKey& key,
95 const char* outputColor, 95 const char* outputColor,
96 const char* inputColor, 96 const char* inputColor,
97 const TransformedCoordsArray&, 97 const TransformedCoordsArray&,
98 const TextureSamplerArray&) SK_OVERRIDE; 98 const TextureSamplerArray&) SK_OVERRIDE;
99 99
100 static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyB uilder*); 100 static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyB uilder*);
101 101
102 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_ OVERRIDE; 102 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_ OVERRIDE;
103 103
104 private: 104 private:
105 GrGLProgramDataManager::UniformHandle fRectUniform; 105 GrGLProgramDataManager::UniformHandle fRectUniform;
106 SkRect fPrevRect; 106 SkRect fPrevRect;
107 typedef GrGLEffect INHERITED; 107 typedef GrGLEffect INHERITED;
108 }; 108 };
109 109
110 GLAARectEffect::GLAARectEffect(const GrBackendEffectFactory& factory, 110 GLAARectEffect::GLAARectEffect(const GrBackendEffectFactory& factory,
111 const GrDrawEffect& drawEffect) 111 const GrDrawEffect& drawEffect)
112 : INHERITED (factory) { 112 : INHERITED (factory) {
113 fPrevRect.fLeft = SK_ScalarNaN; 113 fPrevRect.fLeft = SK_ScalarNaN;
114 } 114 }
115 115
116 void GLAARectEffect::emitCode(GrGLShaderBuilder* builder, 116 void GLAARectEffect::emitCode(GrGLProgramBuilder* builder,
117 const GrDrawEffect& drawEffect, 117 const GrDrawEffect& drawEffect,
118 const GrEffectKey& key, 118 const GrEffectKey& key,
119 const char* outputColor, 119 const char* outputColor,
120 const char* inputColor, 120 const char* inputColor,
121 const TransformedCoordsArray&, 121 const TransformedCoordsArray&,
122 const TextureSamplerArray& samplers) { 122 const TextureSamplerArray& samplers) {
123 const AARectEffect& aare = drawEffect.castEffect<AARectEffect>(); 123 const AARectEffect& aare = drawEffect.castEffect<AARectEffect>();
124 const char *rectName; 124 const char *rectName;
125 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bot tom - 0.5), 125 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bot tom - 0.5),
126 // respectively. 126 // respectively.
127 fRectUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, 127 fRectUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
128 kVec4f_GrSLType, 128 kVec4f_GrSLType,
129 "rect", 129 "rect",
130 &rectName); 130 &rectName);
131 const char* fragmentPos = builder->fragmentPosition(); 131
132 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
133 const char* fragmentPos = fsBuilder->fragmentPosition();
132 if (GrEffectEdgeTypeIsAA(aare.getEdgeType())) { 134 if (GrEffectEdgeTypeIsAA(aare.getEdgeType())) {
133 // The amount of coverage removed in x and y by the edges is computed as a pair of negative 135 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
134 // numbers, xSub and ySub. 136 // numbers, xSub and ySub.
135 builder->fsCodeAppend("\t\tfloat xSub, ySub;\n"); 137 fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
136 builder->fsCodeAppendf("\t\txSub = min(%s.x - %s.x, 0.0);\n", fragmentPo s, rectName); 138 fsBuilder->codeAppendf("\t\txSub = min(%s.x - %s.x, 0.0);\n", fragmentPo s, rectName);
137 builder->fsCodeAppendf("\t\txSub += min(%s.z - %s.x, 0.0);\n", rectName, fragmentPos); 139 fsBuilder->codeAppendf("\t\txSub += min(%s.z - %s.x, 0.0);\n", rectName, fragmentPos);
138 builder->fsCodeAppendf("\t\tySub = min(%s.y - %s.y, 0.0);\n", fragmentPo s, rectName); 140 fsBuilder->codeAppendf("\t\tySub = min(%s.y - %s.y, 0.0);\n", fragmentPo s, rectName);
139 builder->fsCodeAppendf("\t\tySub += min(%s.w - %s.y, 0.0);\n", rectName, fragmentPos); 141 fsBuilder->codeAppendf("\t\tySub += min(%s.w - %s.y, 0.0);\n", rectName, fragmentPos);
140 // Now compute coverage in x and y and multiply them to get the fraction of the pixel 142 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
141 // covered. 143 // covered.
142 builder->fsCodeAppendf("\t\tfloat alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));\n"); 144 fsBuilder->codeAppendf("\t\tfloat alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));\n");
143 } else { 145 } else {
144 builder->fsCodeAppendf("\t\tfloat alpha = 1.0;\n"); 146 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
145 builder->fsCodeAppendf("\t\talpha *= (%s.x - %s.x) > -0.5 ? 1.0 : 0.0;\n ", fragmentPos, rectName); 147 fsBuilder->codeAppendf("\t\talpha *= (%s.x - %s.x) > -0.5 ? 1.0 : 0.0;\n ", fragmentPos, rectName);
146 builder->fsCodeAppendf("\t\talpha *= (%s.z - %s.x) > -0.5 ? 1.0 : 0.0;\n ", rectName, fragmentPos); 148 fsBuilder->codeAppendf("\t\talpha *= (%s.z - %s.x) > -0.5 ? 1.0 : 0.0;\n ", rectName, fragmentPos);
147 builder->fsCodeAppendf("\t\talpha *= (%s.y - %s.y) > -0.5 ? 1.0 : 0.0;\n ", fragmentPos, rectName); 149 fsBuilder->codeAppendf("\t\talpha *= (%s.y - %s.y) > -0.5 ? 1.0 : 0.0;\n ", fragmentPos, rectName);
148 builder->fsCodeAppendf("\t\talpha *= (%s.w - %s.y) > -0.5 ? 1.0 : 0.0;\n ", rectName, fragmentPos); 150 fsBuilder->codeAppendf("\t\talpha *= (%s.w - %s.y) > -0.5 ? 1.0 : 0.0;\n ", rectName, fragmentPos);
149 } 151 }
150 152
151 if (GrEffectEdgeTypeIsInverseFill(aare.getEdgeType())) { 153 if (GrEffectEdgeTypeIsInverseFill(aare.getEdgeType())) {
152 builder->fsCodeAppend("\t\talpha = 1.0 - alpha;\n"); 154 fsBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n");
153 } 155 }
154 builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor, 156 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor,
155 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_st r()); 157 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_st r());
156 } 158 }
157 159
158 void GLAARectEffect::setData(const GrGLProgramDataManager& pdman, const GrDrawEf fect& drawEffect) { 160 void GLAARectEffect::setData(const GrGLProgramDataManager& pdman, const GrDrawEf fect& drawEffect) {
159 const AARectEffect& aare = drawEffect.castEffect<AARectEffect>(); 161 const AARectEffect& aare = drawEffect.castEffect<AARectEffect>();
160 const SkRect& rect = aare.getRect(); 162 const SkRect& rect = aare.getRect();
161 if (rect != fPrevRect) { 163 if (rect != fPrevRect) {
162 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f, 164 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
163 rect.fRight - 0.5f, rect.fBottom - 0.5f); 165 rect.fRight - 0.5f, rect.fBottom - 0.5f);
164 fPrevRect = rect; 166 fPrevRect = rect;
165 } 167 }
166 } 168 }
167 169
168 void GLAARectEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&, 170 void GLAARectEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&,
169 GrEffectKeyBuilder* b) { 171 GrEffectKeyBuilder* b) {
170 const AARectEffect& aare = drawEffect.castEffect<AARectEffect>(); 172 const AARectEffect& aare = drawEffect.castEffect<AARectEffect>();
171 b->add32(aare.getEdgeType()); 173 b->add32(aare.getEdgeType());
172 } 174 }
173 175
174 const GrBackendEffectFactory& AARectEffect::getFactory() const { 176 const GrBackendEffectFactory& AARectEffect::getFactory() const {
175 return GrTBackendEffectFactory<AARectEffect>::getInstance(); 177 return GrTBackendEffectFactory<AARectEffect>::getInstance();
176 } 178 }
177 179
178 ////////////////////////////////////////////////////////////////////////////// 180 //////////////////////////////////////////////////////////////////////////////
179 181
180 class GrGLConvexPolyEffect : public GrGLEffect { 182 class GrGLConvexPolyEffect : public GrGLEffect {
181 public: 183 public:
182 GrGLConvexPolyEffect(const GrBackendEffectFactory&, const GrDrawEffect&); 184 GrGLConvexPolyEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
183 185
184 virtual void emitCode(GrGLShaderBuilder* builder, 186 virtual void emitCode(GrGLProgramBuilder* builder,
185 const GrDrawEffect& drawEffect, 187 const GrDrawEffect& drawEffect,
186 const GrEffectKey& key, 188 const GrEffectKey& key,
187 const char* outputColor, 189 const char* outputColor,
188 const char* inputColor, 190 const char* inputColor,
189 const TransformedCoordsArray&, 191 const TransformedCoordsArray&,
190 const TextureSamplerArray&) SK_OVERRIDE; 192 const TextureSamplerArray&) SK_OVERRIDE;
191 193
192 static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyB uilder*); 194 static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyB uilder*);
193 195
194 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_ OVERRIDE; 196 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_ OVERRIDE;
195 197
196 private: 198 private:
197 GrGLProgramDataManager::UniformHandle fEdgeUniform; 199 GrGLProgramDataManager::UniformHandle fEdgeUniform;
198 SkScalar fPrevEdges[3 * GrConvexPolyEffect::kMa xEdges]; 200 SkScalar fPrevEdges[3 * GrConvexPolyEffect::kMa xEdges];
199 typedef GrGLEffect INHERITED; 201 typedef GrGLEffect INHERITED;
200 }; 202 };
201 203
202 GrGLConvexPolyEffect::GrGLConvexPolyEffect(const GrBackendEffectFactory& factory , 204 GrGLConvexPolyEffect::GrGLConvexPolyEffect(const GrBackendEffectFactory& factory ,
203 const GrDrawEffect& drawEffect) 205 const GrDrawEffect& drawEffect)
204 : INHERITED (factory) { 206 : INHERITED (factory) {
205 fPrevEdges[0] = SK_ScalarNaN; 207 fPrevEdges[0] = SK_ScalarNaN;
206 } 208 }
207 209
208 void GrGLConvexPolyEffect::emitCode(GrGLShaderBuilder* builder, 210 void GrGLConvexPolyEffect::emitCode(GrGLProgramBuilder* builder,
209 const GrDrawEffect& drawEffect, 211 const GrDrawEffect& drawEffect,
210 const GrEffectKey& key, 212 const GrEffectKey& key,
211 const char* outputColor, 213 const char* outputColor,
212 const char* inputColor, 214 const char* inputColor,
213 const TransformedCoordsArray&, 215 const TransformedCoordsArray&,
214 const TextureSamplerArray& samplers) { 216 const TextureSamplerArray& samplers) {
215 const GrConvexPolyEffect& cpe = drawEffect.castEffect<GrConvexPolyEffect>(); 217 const GrConvexPolyEffect& cpe = drawEffect.castEffect<GrConvexPolyEffect>();
216 218
217 const char *edgeArrayName; 219 const char *edgeArrayName;
218 fEdgeUniform = builder->addUniformArray(GrGLShaderBuilder::kFragment_Visibil ity, 220 fEdgeUniform = builder->addUniformArray(GrGLProgramBuilder::kFragment_Visibi lity,
219 kVec3f_GrSLType, 221 kVec3f_GrSLType,
220 "edges", 222 "edges",
221 cpe.getEdgeCount(), 223 cpe.getEdgeCount(),
222 &edgeArrayName); 224 &edgeArrayName);
223 builder->fsCodeAppend("\t\tfloat alpha = 1.0;\n"); 225 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
224 builder->fsCodeAppend("\t\tfloat edge;\n"); 226 fsBuilder->codeAppend("\t\tfloat alpha = 1.0;\n");
225 const char* fragmentPos = builder->fragmentPosition(); 227 fsBuilder->codeAppend("\t\tfloat edge;\n");
228 const char* fragmentPos = fsBuilder->fragmentPosition();
226 for (int i = 0; i < cpe.getEdgeCount(); ++i) { 229 for (int i = 0; i < cpe.getEdgeCount(); ++i) {
227 builder->fsCodeAppendf("\t\tedge = dot(%s[%d], vec3(%s.x, %s.y, 1));\n", 230 fsBuilder->codeAppendf("\t\tedge = dot(%s[%d], vec3(%s.x, %s.y, 1));\n",
228 edgeArrayName, i, fragmentPos, fragmentPos); 231 edgeArrayName, i, fragmentPos, fragmentPos);
229 if (GrEffectEdgeTypeIsAA(cpe.getEdgeType())) { 232 if (GrEffectEdgeTypeIsAA(cpe.getEdgeType())) {
230 builder->fsCodeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n"); 233 fsBuilder->codeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n");
231 } else { 234 } else {
232 builder->fsCodeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n"); 235 fsBuilder->codeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n");
233 } 236 }
234 builder->fsCodeAppend("\t\talpha *= edge;\n"); 237 fsBuilder->codeAppend("\t\talpha *= edge;\n");
235 } 238 }
236 239
237 // Woe is me. See skbug.com/2149. 240 // Woe is me. See skbug.com/2149.
238 if (kTegra2_GrGLRenderer == builder->ctxInfo().renderer()) { 241 if (kTegra2_GrGLRenderer == builder->ctxInfo().renderer()) {
239 builder->fsCodeAppend("\t\tif (-1.0 == alpha) {\n\t\t\tdiscard;\n\t\t}\n "); 242 fsBuilder->codeAppend("\t\tif (-1.0 == alpha) {\n\t\t\tdiscard;\n\t\t}\n ");
240 } 243 }
241 244
242 if (GrEffectEdgeTypeIsInverseFill(cpe.getEdgeType())) { 245 if (GrEffectEdgeTypeIsInverseFill(cpe.getEdgeType())) {
243 builder->fsCodeAppend("\talpha = 1.0 - alpha;\n"); 246 fsBuilder->codeAppend("\talpha = 1.0 - alpha;\n");
244 } 247 }
245 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, 248 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor,
246 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_st r()); 249 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_st r());
247 } 250 }
248 251
249 void GrGLConvexPolyEffect::setData(const GrGLProgramDataManager& pdman, const Gr DrawEffect& drawEffect) { 252 void GrGLConvexPolyEffect::setData(const GrGLProgramDataManager& pdman, const Gr DrawEffect& drawEffect) {
250 const GrConvexPolyEffect& cpe = drawEffect.castEffect<GrConvexPolyEffect>(); 253 const GrConvexPolyEffect& cpe = drawEffect.castEffect<GrConvexPolyEffect>();
251 size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar); 254 size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar);
252 if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) { 255 if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) {
253 pdman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges()); 256 pdman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges());
254 memcpy(fPrevEdges, cpe.getEdges(), byteSize); 257 memcpy(fPrevEdges, cpe.getEdges(), byteSize);
255 } 258 }
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 } 372 }
370 373
371 GrEffect* effect; 374 GrEffect* effect;
372 do { 375 do {
373 GrEffectEdgeType edgeType = static_cast<GrEffectEdgeType>( 376 GrEffectEdgeType edgeType = static_cast<GrEffectEdgeType>(
374 random->nextULessThan(kGrEffectEdgeTypeC nt)); 377 random->nextULessThan(kGrEffectEdgeTypeC nt));
375 effect = GrConvexPolyEffect::Create(edgeType, count, edges); 378 effect = GrConvexPolyEffect::Create(edgeType, count, edges);
376 } while (NULL == effect); 379 } while (NULL == effect);
377 return effect; 380 return effect;
378 } 381 }
OLDNEW
« no previous file with comments | « src/gpu/effects/GrConfigConversionEffect.cpp ('k') | src/gpu/effects/GrConvolutionEffect.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698