| 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 "GrRRectEffect.h" | 8 #include "GrRRectEffect.h" |
| 9 | 9 |
| 10 #include "GrConvexPolyEffect.h" | 10 #include "GrConvexPolyEffect.h" |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 // half a pixel. | 158 // half a pixel. |
| 159 fInnerRectUniform = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFragmen
t_Visibility, | 159 fInnerRectUniform = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFragmen
t_Visibility, |
| 160 kVec4f_GrSLType, kDefault_GrSL
Precision, | 160 kVec4f_GrSLType, kDefault_GrSL
Precision, |
| 161 "innerRect", | 161 "innerRect", |
| 162 &rectName); | 162 &rectName); |
| 163 fRadiusPlusHalfUniform = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFr
agment_Visibility, | 163 fRadiusPlusHalfUniform = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFr
agment_Visibility, |
| 164 kFloat_GrSLType, kDefault
_GrSLPrecision, | 164 kFloat_GrSLType, kDefault
_GrSLPrecision, |
| 165 "radiusPlusHalf", | 165 "radiusPlusHalf", |
| 166 &radiusPlusHalfName); | 166 &radiusPlusHalfName); |
| 167 | 167 |
| 168 GrGLSLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder()
; | 168 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 169 const char* fragmentPos = fsBuilder->fragmentPosition(); | 169 const char* fragmentPos = fragBuilder->fragmentPosition(); |
| 170 // At each quarter-circle corner we compute a vector that is the offset of t
he fragment position | 170 // At each quarter-circle corner we compute a vector that is the offset of t
he fragment position |
| 171 // from the circle center. The vector is pinned in x and y to be in the quar
ter-plane relevant | 171 // from the circle center. The vector is pinned in x and y to be in the quar
ter-plane relevant |
| 172 // to that corner. This means that points near the interior near the rrect t
op edge will have | 172 // to that corner. This means that points near the interior near the rrect t
op edge will have |
| 173 // a vector that points straight up for both the TL left and TR corners. Com
puting an | 173 // a vector that points straight up for both the TL left and TR corners. Com
puting an |
| 174 // alpha from this vector at either the TR or TL corner will give the correc
t result. Similarly, | 174 // alpha from this vector at either the TR or TL corner will give the correc
t result. Similarly, |
| 175 // fragments near the other three edges will get the correct AA. Fragments i
n the interior of | 175 // fragments near the other three edges will get the correct AA. Fragments i
n the interior of |
| 176 // the rrect will have a (0,0) vector at all four corners. So long as the ra
dius > 0.5 they will | 176 // the rrect will have a (0,0) vector at all four corners. So long as the ra
dius > 0.5 they will |
| 177 // correctly produce an alpha value of 1 at all four corners. We take the mi
n of all the alphas. | 177 // correctly produce an alpha value of 1 at all four corners. We take the mi
n of all the alphas. |
| 178 // The code below is a simplified version of the above that performs maxs on
the vector | 178 // The code below is a simplified version of the above that performs maxs on
the vector |
| 179 // components before computing distances and alpha values so that only one d
istance computation | 179 // components before computing distances and alpha values so that only one d
istance computation |
| 180 // need be computed to determine the min alpha. | 180 // need be computed to determine the min alpha. |
| 181 // | 181 // |
| 182 // For the cases where one half of the rrect is rectangular we drop one of t
he x or y | 182 // For the cases where one half of the rrect is rectangular we drop one of t
he x or y |
| 183 // computations, compute a separate rect edge alpha for the rect side, and m
ul the two computed | 183 // computations, compute a separate rect edge alpha for the rect side, and m
ul the two computed |
| 184 // alphas together. | 184 // alphas together. |
| 185 switch (crre.getCircularCornerFlags()) { | 185 switch (crre.getCircularCornerFlags()) { |
| 186 case CircularRRectEffect::kAll_CornerFlags: | 186 case CircularRRectEffect::kAll_CornerFlags: |
| 187 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName,
fragmentPos); | 187 fragBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectNam
e, fragmentPos); |
| 188 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentP
os, rectName); | 188 fragBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmen
tPos, rectName); |
| 189 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n")
; | 189 fragBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n
"); |
| 190 fsBuilder->codeAppendf("\t\tfloat alpha = clamp(%s - length(dxy), 0.
0, 1.0);\n", | 190 fragBuilder->codeAppendf("\t\tfloat alpha = clamp(%s - length(dxy),
0.0, 1.0);\n", |
| 191 radiusPlusHalfName); | 191 radiusPlusHalfName); |
| 192 break; | 192 break; |
| 193 case CircularRRectEffect::kTopLeft_CornerFlag: | 193 case CircularRRectEffect::kTopLeft_CornerFlag: |
| 194 fsBuilder->codeAppendf("\t\tvec2 dxy = max(%s.xy - %s.xy, 0.0);\n", | 194 fragBuilder->codeAppendf("\t\tvec2 dxy = max(%s.xy - %s.xy, 0.0);\n"
, |
| 195 rectName, fragmentPos); | 195 rectName, fragmentPos); |
| 196 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.
0, 1.0);\n", | 196 fragBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x,
0.0, 1.0);\n", |
| 197 rectName, fragmentPos); | 197 rectName, fragmentPos); |
| 198 fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0
.0, 1.0);\n", | 198 fragBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y,
0.0, 1.0);\n", |
| 199 rectName, fragmentPos); | 199 rectName, fragmentPos); |
| 200 fsBuilder->codeAppendf("\t\tfloat alpha = bottomAlpha * rightAlpha *
clamp(%s - length(dxy), 0.0, 1.0);\n", | 200 fragBuilder->codeAppendf( |
| 201 radiusPlusHalfName); | 201 "\t\tfloat alpha = bottomAlpha * rightAlpha * clamp(%s - length(
dxy), 0.0, 1.0);\n", |
| 202 radiusPlusHalfName); |
| 202 break; | 203 break; |
| 203 case CircularRRectEffect::kTopRight_CornerFlag: | 204 case CircularRRectEffect::kTopRight_CornerFlag: |
| 204 fsBuilder->codeAppendf("\t\tvec2 dxy = max(vec2(%s.x - %s.z, %s.y -
%s.y), 0.0);\n", | 205 fragBuilder->codeAppendf("\t\tvec2 dxy = max(vec2(%s.x - %s.z, %s.y
- %s.y), 0.0);\n", |
| 205 fragmentPos, rectName, rectName, fragmentPos)
; | 206 fragmentPos, rectName, rectName, fragmentPo
s); |
| 206 fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0
, 1.0);\n", | 207 fragBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0
.0, 1.0);\n", |
| 207 fragmentPos, rectName); | 208 fragmentPos, rectName); |
| 208 fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0
.0, 1.0);\n", | 209 fragBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y,
0.0, 1.0);\n", |
| 209 rectName, fragmentPos); | 210 rectName, fragmentPos); |
| 210 fsBuilder->codeAppendf("\t\tfloat alpha = bottomAlpha * leftAlpha *
clamp(%s - length(dxy), 0.0, 1.0);\n", | 211 fragBuilder->codeAppendf( |
| 211 radiusPlusHalfName); | 212 "\t\tfloat alpha = bottomAlpha * leftAlpha * clamp(%s - length(d
xy), 0.0, 1.0);\n", |
| 213 radiusPlusHalfName); |
| 212 break; | 214 break; |
| 213 case CircularRRectEffect::kBottomRight_CornerFlag: | 215 case CircularRRectEffect::kBottomRight_CornerFlag: |
| 214 fsBuilder->codeAppendf("\t\tvec2 dxy = max(%s.xy - %s.zw, 0.0);\n", | 216 fragBuilder->codeAppendf("\t\tvec2 dxy = max(%s.xy - %s.zw, 0.0);\n"
, |
| 215 fragmentPos, rectName); | 217 fragmentPos, rectName); |
| 216 fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0
, 1.0);\n", | 218 fragBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0
.0, 1.0);\n", |
| 217 fragmentPos, rectName); | 219 fragmentPos, rectName); |
| 218 fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0,
1.0);\n", | 220 fragBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.
0, 1.0);\n", |
| 219 fragmentPos, rectName); | 221 fragmentPos, rectName); |
| 220 fsBuilder->codeAppendf("\t\tfloat alpha = topAlpha * leftAlpha * cla
mp(%s - length(dxy), 0.0, 1.0);\n", | 222 fragBuilder->codeAppendf( |
| 221 radiusPlusHalfName); | 223 "\t\tfloat alpha = topAlpha * leftAlpha * clamp(%s - length(dxy)
, 0.0, 1.0);\n", |
| 224 radiusPlusHalfName); |
| 222 break; | 225 break; |
| 223 case CircularRRectEffect::kBottomLeft_CornerFlag: | 226 case CircularRRectEffect::kBottomLeft_CornerFlag: |
| 224 fsBuilder->codeAppendf("\t\tvec2 dxy = max(vec2(%s.x - %s.x, %s.y -
%s.w), 0.0);\n", | 227 fragBuilder->codeAppendf("\t\tvec2 dxy = max(vec2(%s.x - %s.x, %s.y
- %s.w), 0.0);\n", |
| 225 rectName, fragmentPos, fragmentPos, rectName)
; | 228 rectName, fragmentPos, fragmentPos, rectNam
e); |
| 226 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.
0, 1.0);\n", | 229 fragBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x,
0.0, 1.0);\n", |
| 227 rectName, fragmentPos); | 230 rectName, fragmentPos); |
| 228 fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0,
1.0);\n", | 231 fragBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.
0, 1.0);\n", |
| 229 fragmentPos, rectName); | 232 fragmentPos, rectName); |
| 230 fsBuilder->codeAppendf("\t\tfloat alpha = topAlpha * rightAlpha * cl
amp(%s - length(dxy), 0.0, 1.0);\n", | 233 fragBuilder->codeAppendf( |
| 231 radiusPlusHalfName); | 234 "\t\tfloat alpha = topAlpha * rightAlpha * clamp(%s - length(dxy
), 0.0, 1.0);\n", |
| 235 radiusPlusHalfName); |
| 232 break; | 236 break; |
| 233 case CircularRRectEffect::kLeft_CornerFlags: | 237 case CircularRRectEffect::kLeft_CornerFlags: |
| 234 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName,
fragmentPos); | 238 fragBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectNam
e, fragmentPos); |
| 235 fsBuilder->codeAppendf("\t\tfloat dy1 = %s.y - %s.w;\n", fragmentPos
, rectName); | 239 fragBuilder->codeAppendf("\t\tfloat dy1 = %s.y - %s.w;\n", fragmentP
os, rectName); |
| 236 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(dxy0.x, max(dxy0.y, d
y1)), 0.0);\n"); | 240 fragBuilder->codeAppend("\t\tvec2 dxy = max(vec2(dxy0.x, max(dxy0.y,
dy1)), 0.0);\n"); |
| 237 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.
0, 1.0);\n", | 241 fragBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x,
0.0, 1.0);\n", |
| 238 rectName, fragmentPos); | 242 rectName, fragmentPos); |
| 239 fsBuilder->codeAppendf("\t\tfloat alpha = rightAlpha * clamp(%s - le
ngth(dxy), 0.0, 1.0);\n", | 243 fragBuilder->codeAppendf( |
| 240 radiusPlusHalfName); | 244 "\t\tfloat alpha = rightAlpha * clamp(%s - length(dxy), 0.0, 1.0
);\n", |
| 245 radiusPlusHalfName); |
| 241 break; | 246 break; |
| 242 case CircularRRectEffect::kTop_CornerFlags: | 247 case CircularRRectEffect::kTop_CornerFlags: |
| 243 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName,
fragmentPos); | 248 fragBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectNam
e, fragmentPos); |
| 244 fsBuilder->codeAppendf("\t\tfloat dx1 = %s.x - %s.z;\n", fragmentPos
, rectName); | 249 fragBuilder->codeAppendf("\t\tfloat dx1 = %s.x - %s.z;\n", fragmentP
os, rectName); |
| 245 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(max(dxy0.x, dx1), dxy
0.y), 0.0);\n"); | 250 fragBuilder->codeAppend("\t\tvec2 dxy = max(vec2(max(dxy0.x, dx1), d
xy0.y), 0.0);\n"); |
| 246 fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0
.0, 1.0);\n", | 251 fragBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y,
0.0, 1.0);\n", |
| 247 rectName, fragmentPos); | 252 rectName, fragmentPos); |
| 248 fsBuilder->codeAppendf("\t\tfloat alpha = bottomAlpha * clamp(%s - l
ength(dxy), 0.0, 1.0);\n", | 253 fragBuilder->codeAppendf( |
| 249 radiusPlusHalfName); | 254 "\t\tfloat alpha = bottomAlpha * clamp(%s - length(dxy), 0.0, 1.
0);\n", |
| 255 radiusPlusHalfName); |
| 250 break; | 256 break; |
| 251 case CircularRRectEffect::kRight_CornerFlags: | 257 case CircularRRectEffect::kRight_CornerFlags: |
| 252 fsBuilder->codeAppendf("\t\tfloat dy0 = %s.y - %s.y;\n", rectName, f
ragmentPos); | 258 fragBuilder->codeAppendf("\t\tfloat dy0 = %s.y - %s.y;\n", rectName,
fragmentPos); |
| 253 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentP
os, rectName); | 259 fragBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmen
tPos, rectName); |
| 254 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(dxy1.x, max(dy0, dxy1
.y)), 0.0);\n"); | 260 fragBuilder->codeAppend("\t\tvec2 dxy = max(vec2(dxy1.x, max(dy0, dx
y1.y)), 0.0);\n"); |
| 255 fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0
, 1.0);\n", | 261 fragBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0
.0, 1.0);\n", |
| 256 fragmentPos, rectName); | 262 fragmentPos, rectName); |
| 257 fsBuilder->codeAppendf("\t\tfloat alpha = leftAlpha * clamp(%s - len
gth(dxy), 0.0, 1.0);\n", | 263 fragBuilder->codeAppendf( |
| 258 radiusPlusHalfName); | 264 "\t\tfloat alpha = leftAlpha * clamp(%s - length(dxy), 0.0, 1.0)
;\n", |
| 265 radiusPlusHalfName); |
| 259 break; | 266 break; |
| 260 case CircularRRectEffect::kBottom_CornerFlags: | 267 case CircularRRectEffect::kBottom_CornerFlags: |
| 261 fsBuilder->codeAppendf("\t\tfloat dx0 = %s.x - %s.x;\n", rectName, f
ragmentPos); | 268 fragBuilder->codeAppendf("\t\tfloat dx0 = %s.x - %s.x;\n", rectName,
fragmentPos); |
| 262 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentP
os, rectName); | 269 fragBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmen
tPos, rectName); |
| 263 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(max(dx0, dxy1.x), dxy
1.y), 0.0);\n"); | 270 fragBuilder->codeAppend("\t\tvec2 dxy = max(vec2(max(dx0, dxy1.x), d
xy1.y), 0.0);\n"); |
| 264 fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0,
1.0);\n", | 271 fragBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.
0, 1.0);\n", |
| 265 fragmentPos, rectName); | 272 fragmentPos, rectName); |
| 266 fsBuilder->codeAppendf("\t\tfloat alpha = topAlpha * clamp(%s - leng
th(dxy), 0.0, 1.0);\n", | 273 fragBuilder->codeAppendf( |
| 267 radiusPlusHalfName); | 274 "\t\tfloat alpha = topAlpha * clamp(%s - length(dxy), 0.0, 1.0);
\n", |
| 275 radiusPlusHalfName); |
| 268 break; | 276 break; |
| 269 } | 277 } |
| 270 | 278 |
| 271 if (kInverseFillAA_GrProcessorEdgeType == crre.getEdgeType()) { | 279 if (kInverseFillAA_GrProcessorEdgeType == crre.getEdgeType()) { |
| 272 fsBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n"); | 280 fragBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n"); |
| 273 } | 281 } |
| 274 | 282 |
| 275 fsBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutputColor, | 283 fragBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutputColor, |
| 276 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")
).c_str()); | 284 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha
")).c_str()); |
| 277 } | 285 } |
| 278 | 286 |
| 279 void GLCircularRRectEffect::GenKey(const GrProcessor& processor, const GrGLSLCap
s&, | 287 void GLCircularRRectEffect::GenKey(const GrProcessor& processor, const GrGLSLCap
s&, |
| 280 GrProcessorKeyBuilder* b) { | 288 GrProcessorKeyBuilder* b) { |
| 281 const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>(); | 289 const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>(); |
| 282 GR_STATIC_ASSERT(kGrProcessorEdgeTypeCnt <= 8); | 290 GR_STATIC_ASSERT(kGrProcessorEdgeTypeCnt <= 8); |
| 283 b->add32((crre.getCircularCornerFlags() << 3) | crre.getEdgeType()); | 291 b->add32((crre.getCircularCornerFlags() << 3) | crre.getEdgeType()); |
| 284 } | 292 } |
| 285 | 293 |
| 286 void GLCircularRRectEffect::onSetData(const GrGLSLProgramDataManager& pdman, | 294 void GLCircularRRectEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 | 504 |
| 497 void GLEllipticalRRectEffect::emitCode(EmitArgs& args) { | 505 void GLEllipticalRRectEffect::emitCode(EmitArgs& args) { |
| 498 const EllipticalRRectEffect& erre = args.fFp.cast<EllipticalRRectEffect>(); | 506 const EllipticalRRectEffect& erre = args.fFp.cast<EllipticalRRectEffect>(); |
| 499 const char *rectName; | 507 const char *rectName; |
| 500 // The inner rect is the rrect bounds inset by the x/y radii | 508 // The inner rect is the rrect bounds inset by the x/y radii |
| 501 fInnerRectUniform = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFragmen
t_Visibility, | 509 fInnerRectUniform = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFragmen
t_Visibility, |
| 502 kVec4f_GrSLType, kDefault_GrSLPrecis
ion, | 510 kVec4f_GrSLType, kDefault_GrSLPrecis
ion, |
| 503 "innerRect", | 511 "innerRect", |
| 504 &rectName); | 512 &rectName); |
| 505 | 513 |
| 506 GrGLSLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder()
; | 514 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 507 const char* fragmentPos = fsBuilder->fragmentPosition(); | 515 const char* fragmentPos = fragBuilder->fragmentPosition(); |
| 508 // At each quarter-ellipse corner we compute a vector that is the offset of
the fragment pos | 516 // At each quarter-ellipse corner we compute a vector that is the offset of
the fragment pos |
| 509 // to the ellipse center. The vector is pinned in x and y to be in the quart
er-plane relevant | 517 // to the ellipse center. The vector is pinned in x and y to be in the quart
er-plane relevant |
| 510 // to that corner. This means that points near the interior near the rrect t
op edge will have | 518 // to that corner. This means that points near the interior near the rrect t
op edge will have |
| 511 // a vector that points straight up for both the TL left and TR corners. Com
puting an | 519 // a vector that points straight up for both the TL left and TR corners. Com
puting an |
| 512 // alpha from this vector at either the TR or TL corner will give the correc
t result. Similarly, | 520 // alpha from this vector at either the TR or TL corner will give the correc
t result. Similarly, |
| 513 // fragments near the other three edges will get the correct AA. Fragments i
n the interior of | 521 // fragments near the other three edges will get the correct AA. Fragments i
n the interior of |
| 514 // the rrect will have a (0,0) vector at all four corners. So long as the ra
dii > 0.5 they will | 522 // the rrect will have a (0,0) vector at all four corners. So long as the ra
dii > 0.5 they will |
| 515 // correctly produce an alpha value of 1 at all four corners. We take the mi
n of all the alphas. | 523 // correctly produce an alpha value of 1 at all four corners. We take the mi
n of all the alphas. |
| 516 // | 524 // |
| 517 // The code below is a simplified version of the above that performs maxs on
the vector | 525 // The code below is a simplified version of the above that performs maxs on
the vector |
| 518 // components before computing distances and alpha values so that only one d
istance computation | 526 // components before computing distances and alpha values so that only one d
istance computation |
| 519 // need be computed to determine the min alpha. | 527 // need be computed to determine the min alpha. |
| 520 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmen
tPos); | 528 fragBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragm
entPos); |
| 521 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rect
Name); | 529 fragBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, re
ctName); |
| 522 // The uniforms with the inv squared radii are highp to prevent underflow. | 530 // The uniforms with the inv squared radii are highp to prevent underflow. |
| 523 switch (erre.getRRect().getType()) { | 531 switch (erre.getRRect().getType()) { |
| 524 case SkRRect::kSimple_Type: { | 532 case SkRRect::kSimple_Type: { |
| 525 const char *invRadiiXYSqdName; | 533 const char *invRadiiXYSqdName; |
| 526 fInvRadiiSqdUniform = args.fBuilder->addUniform( | 534 fInvRadiiSqdUniform = args.fBuilder->addUniform( |
| 527 GrGLSLProgramBuilder::k
Fragment_Visibility, | 535 GrGLSLProgramBuilder::k
Fragment_Visibility, |
| 528 kVec2f_GrSLType, kHigh_
GrSLPrecision, | 536 kVec2f_GrSLType, kHigh_
GrSLPrecision, |
| 529 "invRadiiXY", | 537 "invRadiiXY", |
| 530 &invRadiiXYSqdName); | 538 &invRadiiXYSqdName); |
| 531 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n")
; | 539 fragBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n
"); |
| 532 // Z is the x/y offsets divided by squared radii. | 540 // Z is the x/y offsets divided by squared radii. |
| 533 fsBuilder->codeAppendf("\t\tvec2 Z = dxy * %s;\n", invRadiiXYSqdName
); | 541 fragBuilder->codeAppendf("\t\tvec2 Z = dxy * %s;\n", invRadiiXYSqdNa
me); |
| 534 break; | 542 break; |
| 535 } | 543 } |
| 536 case SkRRect::kNinePatch_Type: { | 544 case SkRRect::kNinePatch_Type: { |
| 537 const char *invRadiiLTRBSqdName; | 545 const char *invRadiiLTRBSqdName; |
| 538 fInvRadiiSqdUniform = args.fBuilder->addUniform( | 546 fInvRadiiSqdUniform = args.fBuilder->addUniform( |
| 539 GrGLSLProgramBuilder::k
Fragment_Visibility, | 547 GrGLSLProgramBuilder::k
Fragment_Visibility, |
| 540 kVec4f_GrSLType, kHigh_
GrSLPrecision, | 548 kVec4f_GrSLType, kHigh_
GrSLPrecision, |
| 541 "invRadiiLTRB", | 549 "invRadiiLTRB", |
| 542 &invRadiiLTRBSqdName); | 550 &invRadiiLTRBSqdName); |
| 543 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n")
; | 551 fragBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n
"); |
| 544 // Z is the x/y offsets divided by squared radii. We only care about
the (at most) one | 552 // Z is the x/y offsets divided by squared radii. We only care about
the (at most) one |
| 545 // corner where both the x and y offsets are positive, hence the max
es. (The inverse | 553 // corner where both the x and y offsets are positive, hence the max
es. (The inverse |
| 546 // squared radii will always be positive.) | 554 // squared radii will always be positive.) |
| 547 fsBuilder->codeAppendf("\t\tvec2 Z = max(max(dxy0 * %s.xy, dxy1 * %s
.zw), 0.0);\n", | 555 fragBuilder->codeAppendf("\t\tvec2 Z = max(max(dxy0 * %s.xy, dxy1 *
%s.zw), 0.0);\n", |
| 548 invRadiiLTRBSqdName, invRadiiLTRBSqdName); | 556 invRadiiLTRBSqdName, invRadiiLTRBSqdName); |
| 549 break; | 557 break; |
| 550 } | 558 } |
| 551 default: | 559 default: |
| 552 SkFAIL("RRect should always be simple or nine-patch."); | 560 SkFAIL("RRect should always be simple or nine-patch."); |
| 553 } | 561 } |
| 554 // implicit is the evaluation of (x/a)^2 + (y/b)^2 - 1. | 562 // implicit is the evaluation of (x/a)^2 + (y/b)^2 - 1. |
| 555 fsBuilder->codeAppend("\t\tfloat implicit = dot(Z, dxy) - 1.0;\n"); | 563 fragBuilder->codeAppend("\t\tfloat implicit = dot(Z, dxy) - 1.0;\n"); |
| 556 // grad_dot is the squared length of the gradient of the implicit. | 564 // grad_dot is the squared length of the gradient of the implicit. |
| 557 fsBuilder->codeAppendf("\t\tfloat grad_dot = 4.0 * dot(Z, Z);\n"); | 565 fragBuilder->codeAppendf("\t\tfloat grad_dot = 4.0 * dot(Z, Z);\n"); |
| 558 // avoid calling inversesqrt on zero. | 566 // avoid calling inversesqrt on zero. |
| 559 fsBuilder->codeAppend("\t\tgrad_dot = max(grad_dot, 1.0e-4);\n"); | 567 fragBuilder->codeAppend("\t\tgrad_dot = max(grad_dot, 1.0e-4);\n"); |
| 560 fsBuilder->codeAppendf("\t\tfloat approx_dist = implicit * inversesqrt(grad_
dot);\n"); | 568 fragBuilder->codeAppendf("\t\tfloat approx_dist = implicit * inversesqrt(gra
d_dot);\n"); |
| 561 | 569 |
| 562 if (kFillAA_GrProcessorEdgeType == erre.getEdgeType()) { | 570 if (kFillAA_GrProcessorEdgeType == erre.getEdgeType()) { |
| 563 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 - approx_dist, 0.0, 1
.0);\n"); | 571 fragBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 - approx_dist, 0.0,
1.0);\n"); |
| 564 } else { | 572 } else { |
| 565 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 + approx_dist, 0.0, 1
.0);\n"); | 573 fragBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 + approx_dist, 0.0,
1.0);\n"); |
| 566 } | 574 } |
| 567 | 575 |
| 568 fsBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutputColor, | 576 fragBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutputColor, |
| 569 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")
).c_str()); | 577 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha
")).c_str()); |
| 570 } | 578 } |
| 571 | 579 |
| 572 void GLEllipticalRRectEffect::GenKey(const GrProcessor& effect, const GrGLSLCaps
&, | 580 void GLEllipticalRRectEffect::GenKey(const GrProcessor& effect, const GrGLSLCaps
&, |
| 573 GrProcessorKeyBuilder* b) { | 581 GrProcessorKeyBuilder* b) { |
| 574 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>(); | 582 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>(); |
| 575 GR_STATIC_ASSERT(kLast_GrProcessorEdgeType < (1 << 3)); | 583 GR_STATIC_ASSERT(kLast_GrProcessorEdgeType < (1 << 3)); |
| 576 b->add32(erre.getRRect().getType() | erre.getEdgeType() << 3); | 584 b->add32(erre.getRRect().getType() | erre.getEdgeType() << 3); |
| 577 } | 585 } |
| 578 | 586 |
| 579 void GLEllipticalRRectEffect::onSetData(const GrGLSLProgramDataManager& pdman, | 587 void GLEllipticalRRectEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 715 if (rrect.isNinePatch()) { | 723 if (rrect.isNinePatch()) { |
| 716 return EllipticalRRectEffect::Create(edgeType, rrect); | 724 return EllipticalRRectEffect::Create(edgeType, rrect); |
| 717 } | 725 } |
| 718 return nullptr; | 726 return nullptr; |
| 719 } | 727 } |
| 720 } | 728 } |
| 721 } | 729 } |
| 722 | 730 |
| 723 return nullptr; | 731 return nullptr; |
| 724 } | 732 } |
| OLD | NEW |