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 "GrDistanceFieldTextureEffect.h" | 8 #include "GrDistanceFieldTextureEffect.h" |
9 #include "GrFontAtlasSizes.h" | 9 #include "GrFontAtlasSizes.h" |
10 #include "GrInvariantOutput.h" | 10 #include "GrInvariantOutput.h" |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 // by using the length of the gradient of the texture coordinates. W
e use st coordinates | 93 // by using the length of the gradient of the texture coordinates. W
e use st coordinates |
94 // to ensure we're mapping 1:1 from texel space to pixel space. | 94 // to ensure we're mapping 1:1 from texel space to pixel space. |
95 | 95 |
96 // this gives us a smooth step across approximately one fragment | 96 // this gives us a smooth step across approximately one fragment |
97 fsBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*dF
dx(st.x));"); | 97 fsBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*dF
dx(st.x));"); |
98 } else { | 98 } else { |
99 // For general transforms, to determine the amount of correction we
multiply a unit | 99 // For general transforms, to determine the amount of correction we
multiply a unit |
100 // vector pointing along the SDF gradient direction by the Jacobian
of the st coords | 100 // vector pointing along the SDF gradient direction by the Jacobian
of the st coords |
101 // (which is the inverse transform for this fragment) and take the l
ength of the result. | 101 // (which is the inverse transform for this fragment) and take the l
ength of the result. |
102 fsBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance), dFdy(di
stance));"); | 102 fsBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance), dFdy(di
stance));"); |
103 if (args.fPB->ctxInfo().caps()->dropsTileOnZeroDivide()) { | 103 // the length of the gradient may be 0, so we need to check for this |
104 // this is to compensate for the Adreno, which likes to drop til
es on division by 0 | 104 // this also compensates for the Adreno, which likes to drop tiles o
n division by 0 |
105 fsBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad)
;"); | 105 fsBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad);"); |
106 fsBuilder->codeAppend("if (dg_len2 < 0.0001) {"); | 106 fsBuilder->codeAppend("if (dg_len2 < 0.0001) {"); |
107 fsBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);"); | 107 fsBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);"); |
108 fsBuilder->codeAppend("} else {"); | 108 fsBuilder->codeAppend("} else {"); |
109 fsBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2
);"); | 109 fsBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);")
; |
110 fsBuilder->codeAppend("}"); | 110 fsBuilder->codeAppend("}"); |
111 } else { | 111 |
112 fsBuilder->codeAppend("dist_grad = normalize(dist_grad);\n"); | |
113 } | |
114 fsBuilder->codeAppend("vec2 Jdx = dFdx(st);"); | 112 fsBuilder->codeAppend("vec2 Jdx = dFdx(st);"); |
115 fsBuilder->codeAppend("vec2 Jdy = dFdy(st);"); | 113 fsBuilder->codeAppend("vec2 Jdy = dFdy(st);"); |
116 fsBuilder->codeAppend("vec2 grad = vec2(dist_grad.x*Jdx.x + dist_gra
d.y*Jdy.x,"); | 114 fsBuilder->codeAppend("vec2 grad = vec2(dist_grad.x*Jdx.x + dist_gra
d.y*Jdy.x,"); |
117 fsBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_gra
d.y*Jdy.y);"); | 115 fsBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_gra
d.y*Jdy.y);"); |
118 | 116 |
119 // this gives us a smooth step across approximately one fragment | 117 // this gives us a smooth step across approximately one fragment |
120 fsBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length
(grad);"); | 118 fsBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length
(grad);"); |
121 } | 119 } |
122 fsBuilder->codeAppend("float val = smoothstep(-afwidth, afwidth, distanc
e);"); | 120 fsBuilder->codeAppend("float val = smoothstep(-afwidth, afwidth, distanc
e);"); |
123 | 121 |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 // by using the length of the gradient of the texture coordinates. W
e use st coordinates | 378 // by using the length of the gradient of the texture coordinates. W
e use st coordinates |
381 // to ensure we're mapping 1:1 from texel space to pixel space. | 379 // to ensure we're mapping 1:1 from texel space to pixel space. |
382 | 380 |
383 // this gives us a smooth step across approximately one fragment | 381 // this gives us a smooth step across approximately one fragment |
384 fsBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*dF
dx(st.x));"); | 382 fsBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*dF
dx(st.x));"); |
385 } else { | 383 } else { |
386 // For general transforms, to determine the amount of correction we
multiply a unit | 384 // For general transforms, to determine the amount of correction we
multiply a unit |
387 // vector pointing along the SDF gradient direction by the Jacobian
of the st coords | 385 // vector pointing along the SDF gradient direction by the Jacobian
of the st coords |
388 // (which is the inverse transform for this fragment) and take the l
ength of the result. | 386 // (which is the inverse transform for this fragment) and take the l
ength of the result. |
389 fsBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance), dFdy(di
stance));"); | 387 fsBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance), dFdy(di
stance));"); |
390 if (args.fPB->ctxInfo().caps()->dropsTileOnZeroDivide()) { | 388 // the length of the gradient may be 0, so we need to check for this |
391 // this is to compensate for the Adreno, which likes to drop til
es on division by 0 | 389 // this also compensates for the Adreno, which likes to drop tiles o
n division by 0 |
392 fsBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad)
;"); | 390 fsBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad);"); |
393 fsBuilder->codeAppend("if (dg_len2 < 0.0001) {"); | 391 fsBuilder->codeAppend("if (dg_len2 < 0.0001) {"); |
394 fsBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);"); | 392 fsBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);"); |
395 fsBuilder->codeAppend("} else {"); | 393 fsBuilder->codeAppend("} else {"); |
396 fsBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2
);"); | 394 fsBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);")
; |
397 fsBuilder->codeAppend("}"); | 395 fsBuilder->codeAppend("}"); |
398 } else { | 396 |
399 fsBuilder->codeAppend("dist_grad = normalize(dist_grad);"); | |
400 } | |
401 fsBuilder->codeAppend("vec2 Jdx = dFdx(st);"); | 397 fsBuilder->codeAppend("vec2 Jdx = dFdx(st);"); |
402 fsBuilder->codeAppend("vec2 Jdy = dFdy(st);"); | 398 fsBuilder->codeAppend("vec2 Jdy = dFdy(st);"); |
403 fsBuilder->codeAppend("vec2 grad = vec2(dist_grad.x*Jdx.x + dist_gra
d.y*Jdy.x,"); | 399 fsBuilder->codeAppend("vec2 grad = vec2(dist_grad.x*Jdx.x + dist_gra
d.y*Jdy.x,"); |
404 fsBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_gra
d.y*Jdy.y);"); | 400 fsBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_gra
d.y*Jdy.y);"); |
405 | 401 |
406 // this gives us a smooth step across approximately one fragment | 402 // this gives us a smooth step across approximately one fragment |
407 fsBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length
(grad);"); | 403 fsBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length
(grad);"); |
408 } | 404 } |
409 fsBuilder->codeAppend("float val = smoothstep(-afwidth, afwidth, distanc
e);"); | 405 fsBuilder->codeAppend("float val = smoothstep(-afwidth, afwidth, distanc
e);"); |
410 | 406 |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
664 // by using the length of the gradient of the texture coordinates. W
e use st coordinates | 660 // by using the length of the gradient of the texture coordinates. W
e use st coordinates |
665 // to ensure we're mapping 1:1 from texel space to pixel space. | 661 // to ensure we're mapping 1:1 from texel space to pixel space. |
666 | 662 |
667 // this gives us a smooth step across approximately one fragment | 663 // this gives us a smooth step across approximately one fragment |
668 fsBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*dx
);"); | 664 fsBuilder->codeAppend("afwidth = abs(" SK_DistanceFieldAAFactor "*dx
);"); |
669 } else { | 665 } else { |
670 // For general transforms, to determine the amount of correction we
multiply a unit | 666 // For general transforms, to determine the amount of correction we
multiply a unit |
671 // vector pointing along the SDF gradient direction by the Jacobian
of the st coords | 667 // vector pointing along the SDF gradient direction by the Jacobian
of the st coords |
672 // (which is the inverse transform for this fragment) and take the l
ength of the result. | 668 // (which is the inverse transform for this fragment) and take the l
ength of the result. |
673 fsBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance.r), dFdy(
distance.r));"); | 669 fsBuilder->codeAppend("vec2 dist_grad = vec2(dFdx(distance.r), dFdy(
distance.r));"); |
674 if (args.fPB->ctxInfo().caps()->dropsTileOnZeroDivide()) { | 670 // the length of the gradient may be 0, so we need to check for this |
675 // this is to compensate for the Adreno, which likes to drop til
es on division by 0 | 671 // this also compensates for the Adreno, which likes to drop tiles o
n division by 0 |
676 fsBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad)
;"); | 672 fsBuilder->codeAppend("float dg_len2 = dot(dist_grad, dist_grad);"); |
677 fsBuilder->codeAppend("if (dg_len2 < 0.0001) {"); | 673 fsBuilder->codeAppend("if (dg_len2 < 0.0001) {"); |
678 fsBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);"); | 674 fsBuilder->codeAppend("dist_grad = vec2(0.7071, 0.7071);"); |
679 fsBuilder->codeAppend("} else {"); | 675 fsBuilder->codeAppend("} else {"); |
680 fsBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2
);"); | 676 fsBuilder->codeAppend("dist_grad = dist_grad*inversesqrt(dg_len2);")
; |
681 fsBuilder->codeAppend("}"); | 677 fsBuilder->codeAppend("}"); |
682 } else { | |
683 fsBuilder->codeAppend("dist_grad = normalize(dist_grad);\n"); | |
684 } | |
685 fsBuilder->codeAppend("vec2 grad = vec2(dist_grad.x*Jdx.x + dist_gra
d.y*Jdy.x,"); | 678 fsBuilder->codeAppend("vec2 grad = vec2(dist_grad.x*Jdx.x + dist_gra
d.y*Jdy.x,"); |
686 fsBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_gra
d.y*Jdy.y);"); | 679 fsBuilder->codeAppend(" dist_grad.x*Jdx.y + dist_gra
d.y*Jdy.y);"); |
687 | 680 |
688 // this gives us a smooth step across approximately one fragment | 681 // this gives us a smooth step across approximately one fragment |
689 fsBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length
(grad);"); | 682 fsBuilder->codeAppend("afwidth = " SK_DistanceFieldAAFactor "*length
(grad);"); |
690 } | 683 } |
691 | 684 |
692 fsBuilder->codeAppend("vec4 val = vec4(smoothstep(vec3(-afwidth), vec3(a
fwidth), distance), 1.0);"); | 685 fsBuilder->codeAppend("vec4 val = vec4(smoothstep(vec3(-afwidth), vec3(a
fwidth), distance), 1.0);"); |
693 | 686 |
694 // adjust based on gamma | 687 // adjust based on gamma |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
865 uint32_t flags = kUseLCD_DistanceFieldEffectFlag; | 858 uint32_t flags = kUseLCD_DistanceFieldEffectFlag; |
866 flags |= random->nextBool() ? kUniformScale_DistanceFieldEffectMask : 0; | 859 flags |= random->nextBool() ? kUniformScale_DistanceFieldEffectMask : 0; |
867 flags |= random->nextBool() ? kBGR_DistanceFieldEffectFlag : 0; | 860 flags |= random->nextBool() ? kBGR_DistanceFieldEffectFlag : 0; |
868 return GrDistanceFieldLCDTextureEffect::Create(GrRandomColor(random), | 861 return GrDistanceFieldLCDTextureEffect::Create(GrRandomColor(random), |
869 GrProcessorUnitTest::TestMatr
ix(random), | 862 GrProcessorUnitTest::TestMatr
ix(random), |
870 textures[texIdx], params, | 863 textures[texIdx], params, |
871 textures[texIdx2], params2, | 864 textures[texIdx2], params2, |
872 textColor, | 865 textColor, |
873 flags); | 866 flags); |
874 } | 867 } |
OLD | NEW |