OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 "GrAAHairLinePathRenderer.h" | 8 #include "GrAAHairLinePathRenderer.h" |
9 | 9 |
10 #include "GrContext.h" | 10 #include "GrContext.h" |
(...skipping 720 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
731 (*vert)[i].fPos.set(SK_ScalarMax, SK_ScalarMax); | 731 (*vert)[i].fPos.set(SK_ScalarMax, SK_ScalarMax); |
732 } | 732 } |
733 } | 733 } |
734 | 734 |
735 *vert += kVertsPerLineSeg; | 735 *vert += kVertsPerLineSeg; |
736 } | 736 } |
737 | 737 |
738 } | 738 } |
739 | 739 |
740 /** | 740 /** |
| 741 * Shader is based off of "Resolution Independent Curve Rendering using |
| 742 * Programmable Graphics Hardware" by Loop and Blinn. |
| 743 * The output of this effect is a hairline edge for non rational cubics. |
| 744 * Cubics are specified by implicit equation K^3 - LM. |
| 745 * K, L, and M, are the first three values of the vertex attribute, |
| 746 * the fourth value is not used. Distance is calculated using a |
| 747 * first order approximation from the taylor series. |
| 748 * Coverage is max(0, 1-distance). |
| 749 */ |
| 750 class HairCubicEdgeEffect : public GrEffect { |
| 751 public: |
| 752 static GrEffectRef* Create() { |
| 753 GR_CREATE_STATIC_EFFECT(gHairCubicEdgeEffect, HairCubicEdgeEffect, ()); |
| 754 gHairCubicEdgeEffect->ref(); |
| 755 return gHairCubicEdgeEffect; |
| 756 } |
| 757 |
| 758 virtual ~HairCubicEdgeEffect() {} |
| 759 |
| 760 static const char* Name() { return "HairCubicEdge"; } |
| 761 |
| 762 virtual void getConstantColorComponents(GrColor* color, |
| 763 uint32_t* validFlags) const SK_OVERR
IDE { |
| 764 *validFlags = 0; |
| 765 } |
| 766 |
| 767 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 768 return GrTBackendEffectFactory<HairCubicEdgeEffect>::getInstance(); |
| 769 } |
| 770 |
| 771 class GLEffect : public GrGLEffect { |
| 772 public: |
| 773 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
| 774 : INHERITED (factory) {} |
| 775 |
| 776 virtual void emitCode(GrGLShaderBuilder* builder, |
| 777 const GrDrawEffect& drawEffect, |
| 778 EffectKey key, |
| 779 const char* outputColor, |
| 780 const char* inputColor, |
| 781 const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 782 const char *vsName, *fsName; |
| 783 |
| 784 SkAssertResult(builder->enableFeature( |
| 785 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); |
| 786 builder->addVarying(kVec4f_GrSLType, "CubicCoeffs", |
| 787 &vsName, &fsName); |
| 788 const SkString* attr0Name = |
| 789 builder->getEffectAttributeName(drawEffect.getVertexAttribIndice
s()[0]); |
| 790 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attr0Name->c_str()); |
| 791 |
| 792 builder->fsCodeAppend("\t\tfloat edgeAlpha;\n"); |
| 793 |
| 794 builder->fsCodeAppendf("\t\tvec3 dklmdx = dFdx(%s.xyz);\n", fsName); |
| 795 builder->fsCodeAppendf("\t\tvec3 dklmdy = dFdy(%s.xyz);\n", fsName); |
| 796 builder->fsCodeAppendf("\t\tfloat dfdx =\n" |
| 797 "\t\t3.0*%s.x*%s.x*dklmdx.x - %s.y*dklmdx.z -
%s.z*dklmdx.y;\n", |
| 798 fsName, fsName, fsName, fsName); |
| 799 builder->fsCodeAppendf("\t\tfloat dfdy =\n" |
| 800 "\t\t3.0*%s.x*%s.x*dklmdy.x - %s.y*dklmdy.z -
%s.z*dklmdy.y;\n", |
| 801 fsName, fsName, fsName, fsName); |
| 802 builder->fsCodeAppend("\t\tvec2 gF = vec2(dfdx, dfdy);\n"); |
| 803 builder->fsCodeAppend("\t\tfloat gFM = sqrt(dot(gF, gF));\n"); |
| 804 builder->fsCodeAppendf("\t\tfloat func = abs(%s.x*%s.x*%s.x - %s.y*%
s.z);\n", |
| 805 fsName, fsName, fsName, fsName, fsName); |
| 806 builder->fsCodeAppend("\t\tedgeAlpha = func / gFM;\n"); |
| 807 builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"
); |
| 808 // Add line below for smooth cubic ramp |
| 809 // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2
.0*edgeAlpha);\n"); |
| 810 |
| 811 SkString modulate; |
| 812 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha"); |
| 813 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()
); |
| 814 } |
| 815 |
| 816 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrG
LCaps&) { |
| 817 return 0x0; |
| 818 } |
| 819 |
| 820 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_
OVERRIDE {} |
| 821 |
| 822 private: |
| 823 typedef GrGLEffect INHERITED; |
| 824 }; |
| 825 private: |
| 826 HairCubicEdgeEffect() { |
| 827 this->addVertexAttrib(kVec4f_GrSLType); |
| 828 } |
| 829 |
| 830 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
| 831 return true; |
| 832 } |
| 833 |
| 834 GR_DECLARE_EFFECT_TEST; |
| 835 |
| 836 typedef GrEffect INHERITED; |
| 837 }; |
| 838 |
| 839 /** |
741 * Shader is based off of Loop-Blinn Quadratic GPU Rendering | 840 * Shader is based off of Loop-Blinn Quadratic GPU Rendering |
742 * The output of this effect is a hairline edge for conics. | 841 * The output of this effect is a hairline edge for conics. |
743 * Conics specified by implicit equation K^2 - LM. | 842 * Conics specified by implicit equation K^2 - LM. |
744 * K, L, and M, are the first three values of the vertex attribute, | 843 * K, L, and M, are the first three values of the vertex attribute, |
745 * the fourth value is not used. Distance is calculated using a | 844 * the fourth value is not used. Distance is calculated using a |
746 * first order approximation from the taylor series. | 845 * first order approximation from the taylor series. |
747 * Coverage is max(0, 1-distance). | 846 * Coverage is max(0, 1-distance). |
748 */ | 847 */ |
749 | 848 |
750 /** | 849 /** |
(...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1279 &devBounds); | 1378 &devBounds); |
1280 conics += n; | 1379 conics += n; |
1281 } | 1380 } |
1282 } | 1381 } |
1283 } | 1382 } |
1284 | 1383 |
1285 target->resetIndexSource(); | 1384 target->resetIndexSource(); |
1286 | 1385 |
1287 return true; | 1386 return true; |
1288 } | 1387 } |
OLD | NEW |