OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 "GrEdgeEffect.h" | 8 #include "GrEdgeEffect.h" |
9 #include "gl/GrGLEffect.h" | 9 #include "gl/GrGLEffect.h" |
10 #include "gl/GrGLSL.h" | 10 #include "gl/GrGLSL.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 | 31 |
32 switch (type) { | 32 switch (type) { |
33 case GrEdgeEffect::kHairLine_EdgeType: | 33 case GrEdgeEffect::kHairLine_EdgeType: |
34 builder->addVarying(kVec4f_GrSLType, "HairEdge", &vsName, &fsName); | 34 builder->addVarying(kVec4f_GrSLType, "HairEdge", &vsName, &fsName); |
35 | 35 |
36 builder->fsCodeAppendf("\t\tedgeAlpha = abs(dot(vec3(%s.xy,1), %s.xy
z));\n", | 36 builder->fsCodeAppendf("\t\tedgeAlpha = abs(dot(vec3(%s.xy,1), %s.xy
z));\n", |
37 builder->fragmentPosition(), fsName); | 37 builder->fragmentPosition(), fsName); |
38 builder->fsCodeAppendf("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n
"); | 38 builder->fsCodeAppendf("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n
"); |
39 break; | 39 break; |
40 case GrEdgeEffect::kQuad_EdgeType: | 40 case GrEdgeEffect::kQuad_EdgeType: |
41 GrAssert(builder->ctxInfo().caps()->shaderDerivativeSupport()); | 41 SkAssertResult(builder->enableFeature(GrGLShaderBuilder::kStandardDe
rivatives_GLSLFeature)); |
42 builder->addVarying(kVec4f_GrSLType, "QuadEdge", &vsName, &fsName); | 42 builder->addVarying(kVec4f_GrSLType, "QuadEdge", &vsName, &fsName); |
43 | 43 |
44 // keep the derivative instructions outside the conditional | 44 // keep the derivative instructions outside the conditional |
45 builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName); | 45 builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName); |
46 builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName); | 46 builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName); |
47 builder->fsCodeAppendf("\t\tif (%s.z > 0.0 && %s.w > 0.0) {\n", fsNa
me, fsName); | 47 builder->fsCodeAppendf("\t\tif (%s.z > 0.0 && %s.w > 0.0) {\n", fsNa
me, fsName); |
48 // today we know z and w are in device space. We could use derivativ
es | 48 // today we know z and w are in device space. We could use derivativ
es |
49 builder->fsCodeAppendf("\t\t\tedgeAlpha = min(min(%s.z, %s.w) + 0.5,
1.0);\n", fsName, | 49 builder->fsCodeAppendf("\t\t\tedgeAlpha = min(min(%s.z, %s.w) + 0.5,
1.0);\n", fsName, |
50 fsName); | 50 fsName); |
51 builder->fsCodeAppendf ("\t\t} else {\n"); | 51 builder->fsCodeAppendf ("\t\t} else {\n"); |
52 builder->fsCodeAppendf("\t\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvd
x.y,\n" | 52 builder->fsCodeAppendf("\t\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvd
x.y,\n" |
53 "\t\t\t 2.0*%s.x*duvdy.x - duvd
y.y);\n", | 53 "\t\t\t 2.0*%s.x*duvdy.x - duvd
y.y);\n", |
54 fsName, fsName); | 54 fsName, fsName); |
55 builder->fsCodeAppendf("\t\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fs
Name, fsName, | 55 builder->fsCodeAppendf("\t\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fs
Name, fsName, |
56 fsName); | 56 fsName); |
57 builder->fsCodeAppendf("\t\t\tedgeAlpha = " | 57 builder->fsCodeAppendf("\t\t\tedgeAlpha = " |
58 "clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0
);\n\t\t}\n"); | 58 "clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0
);\n\t\t}\n"); |
59 if (kES2_GrGLBinding == builder->ctxInfo().binding()) { | |
60 builder->fHeader.append("#extension GL_OES_standard_derivatives:
enable\n"); | |
61 } | |
62 break; | 59 break; |
63 case GrEdgeEffect::kHairQuad_EdgeType: | 60 case GrEdgeEffect::kHairQuad_EdgeType: |
64 GrAssert(builder->ctxInfo().caps()->shaderDerivativeSupport()); | 61 SkAssertResult(builder->enableFeature(GrGLShaderBuilder::kStandardDe
rivatives_GLSLFeature)); |
65 builder->addVarying(kVec4f_GrSLType, "HairQuadEdge", &vsName, &fsNam
e); | 62 builder->addVarying(kVec4f_GrSLType, "HairQuadEdge", &vsName, &fsNam
e); |
66 | 63 |
67 builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName); | 64 builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName); |
68 builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName); | 65 builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName); |
69 builder->fsCodeAppendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.
y,\n" | 66 builder->fsCodeAppendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.
y,\n" |
70 "\t\t 2.0*%s.x*duvdy.x - duvdy.
y);\n", | 67 "\t\t 2.0*%s.x*duvdy.x - duvdy.
y);\n", |
71 fsName, fsName); | 68 fsName, fsName); |
72 builder->fsCodeAppendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsNa
me, fsName, | 69 builder->fsCodeAppendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsNa
me, fsName, |
73 fsName); | 70 fsName); |
74 builder->fsCodeAppend("\t\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / do
t(gF, gF));\n"); | 71 builder->fsCodeAppend("\t\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / do
t(gF, gF));\n"); |
75 builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"
); | 72 builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"
); |
76 if (kES2_GrGLBinding == builder->ctxInfo().binding()) { | |
77 builder->fHeader.append("#extension GL_OES_standard_derivatives:
enable\n"); | |
78 } | |
79 break; | 73 break; |
80 }; | 74 }; |
81 | 75 |
82 SkString modulate; | 76 SkString modulate; |
83 GrGLSLModulate4f(&modulate, inputColor, "edgeAlpha"); | 77 GrGLSLModulate4f(&modulate, inputColor, "edgeAlpha"); |
84 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); | 78 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); |
85 | 79 |
86 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str()); | 80 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str()); |
87 } | 81 } |
88 | 82 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 GrTexture*[]) { | 123 GrTexture*[]) { |
130 // Only kHairLine works without derivative instructions. | 124 // Only kHairLine works without derivative instructions. |
131 EdgeType edgeType; | 125 EdgeType edgeType; |
132 if (caps.shaderDerivativeSupport()) { | 126 if (caps.shaderDerivativeSupport()) { |
133 edgeType = static_cast<EdgeType>(random->nextULessThan(kEdgeTypeCount)); | 127 edgeType = static_cast<EdgeType>(random->nextULessThan(kEdgeTypeCount)); |
134 } else { | 128 } else { |
135 edgeType = kHairLine_EdgeType; | 129 edgeType = kHairLine_EdgeType; |
136 } | 130 } |
137 return GrEdgeEffect::Create(edgeType); | 131 return GrEdgeEffect::Create(edgeType); |
138 } | 132 } |
OLD | NEW |