Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2013 Google Inc. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license that can be | |
| 5 * found in the LICENSE file. | |
| 6 */ | |
| 7 | |
| 8 #include "GrRectEffect.h" | |
| 9 #include "gl/GrGLEffect.h" | |
| 10 #include "gl/GrGLEffectMatrix.h" | |
| 11 #include "gl/GrGLSL.h" | |
| 12 #include "gl/GrGLTexture.h" | |
| 13 #include "GrTBackendEffectFactory.h" | |
| 14 #include "GrTexture.h" | |
| 15 | |
| 16 class GrGLRectEffect : public GrGLEffect { | |
| 17 public: | |
| 18 GrGLRectEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) | |
| 19 : INHERITED (factory) {} | |
| 20 | |
| 21 virtual void emitCode(GrGLShaderBuilder* builder, | |
| 22 const GrDrawEffect& drawEffect, | |
| 23 EffectKey key, | |
| 24 const char* outputColor, | |
| 25 const char* inputColor, | |
| 26 const TextureSamplerArray& samplers) SK_OVERRIDE { | |
| 27 // setup the varying for the center point and the unit vector | |
| 28 // that points down the height of the rect | |
| 29 const char *vsRectEdgeName, *fsRectEdgeName; | |
| 30 builder->addVarying(kVec4f_GrSLType, "RectEdge", | |
| 31 &vsRectEdgeName, &fsRectEdgeName); | |
| 32 const SkString* attr0Name = | |
| 33 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[ 0]); | |
| 34 builder->vsCodeAppendf("\t%s = %s;\n", vsRectEdgeName, attr0Name->c_str( )); | |
| 35 | |
| 36 // setup the varying for width/2+.5 and height/2+.5 | |
| 37 const char *vsWidthHeightName, *fsWidthHeightName; | |
| 38 builder->addVarying(kVec2f_GrSLType, "WidthHeight", | |
| 39 &vsWidthHeightName, &fsWidthHeightName); | |
| 40 const SkString* attr1Name = | |
| 41 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[ 1]); | |
| 42 builder->vsCodeAppendf("\t%s = %s;\n", vsWidthHeightName, attr1Name->c_s tr()); | |
| 43 | |
| 44 // These scale factors adjust the coverage for < 1 pixel wide/high rects | |
| 45 builder->fsCodeAppendf("\tfloat wScale = max(1.0, 2.0/(0.5+%s.x));\n", f sWidthHeightName); | |
|
bsalomon
2013/04/08 15:48:39
Do you think it makes sense to do this in the VS?
robertphillips
2013/04/08 19:33:20
Yes but I would like to delay to the next revision
| |
| 46 builder->fsCodeAppendf("\tfloat hScale = max(1.0, 2.0/(0.5+%s.y));\n", f sWidthHeightName); | |
| 47 | |
| 48 // Compute the coverage for the rect's width | |
| 49 builder->fsCodeAppendf("\tvec2 offset = %s.xy - %s.xy;\n", | |
| 50 builder->fragmentPosition(), fsRectEdgeName); | |
| 51 builder->fsCodeAppendf("\tfloat perpDot = abs(offset.x * %s.w - offset.y * %s.z);\n", | |
| 52 fsRectEdgeName, fsRectEdgeName); | |
| 53 builder->fsCodeAppendf("\tfloat coverage = clamp(wScale*(%s.x-perpDot), 0.0, 1.0);\n", | |
| 54 fsWidthHeightName); | |
| 55 | |
| 56 // Compute the coverage for the rect's height and merge with the width | |
| 57 builder->fsCodeAppendf("\tperpDot = abs(dot(offset, %s.zw));\n", | |
| 58 fsRectEdgeName); | |
| 59 builder->fsCodeAppendf("\tcoverage = min(coverage, clamp(hScale*(%s.y-pe rpDot), 0.0, 1.0));\n", | |
| 60 fsWidthHeightName); | |
| 61 | |
| 62 SkString modulate; | |
| 63 GrGLSLModulate4f(&modulate, inputColor, "coverage"); | |
| 64 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); | |
| 65 } | |
| 66 | |
| 67 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCap s&) { | |
| 68 return 0; | |
| 69 } | |
| 70 | |
| 71 virtual void setData(const GrGLUniformManager& uman, const GrDrawEffect&) SK _OVERRIDE { | |
| 72 } | |
| 73 | |
| 74 private: | |
| 75 typedef GrGLEffect INHERITED; | |
| 76 }; | |
| 77 | |
| 78 /////////////////////////////////////////////////////////////////////////////// | |
| 79 | |
| 80 GrRectEffect::GrRectEffect() : GrEffect() { | |
| 81 this->addVertexAttrib(kVec4f_GrSLType); | |
| 82 this->addVertexAttrib(kVec2f_GrSLType); | |
| 83 } | |
| 84 | |
| 85 void GrRectEffect::getConstantColorComponents(GrColor* color, uint32_t* validFla gs) const { | |
| 86 *validFlags = 0; | |
| 87 } | |
| 88 | |
| 89 const GrBackendEffectFactory& GrRectEffect::getFactory() const { | |
| 90 return GrTBackendEffectFactory<GrRectEffect>::getInstance(); | |
| 91 } | |
| 92 | |
| 93 /////////////////////////////////////////////////////////////////////////////// | |
| 94 | |
| 95 GR_DEFINE_EFFECT_TEST(GrRectEffect); | |
| 96 | |
| 97 GrEffectRef* GrRectEffect::TestCreate(SkMWCRandom* random, | |
| 98 GrContext* context, | |
| 99 const GrDrawTargetCaps&, | |
| 100 GrTexture* textures[]) { | |
| 101 return GrRectEffect::Create(); | |
| 102 } | |
| OLD | NEW |