OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 "GrSimpleTextureEffect.h" | 8 #include "GrHQScaleTextureEffect.h" |
9 #include "gl/GrGLEffect.h" | 9 #include "gl/GrGLEffect.h" |
10 #include "gl/GrGLEffectMatrix.h" | 10 #include "gl/GrGLEffectMatrix.h" |
11 #include "gl/GrGLSL.h" | 11 #include "gl/GrGLSL.h" |
12 #include "gl/GrGLTexture.h" | 12 #include "gl/GrGLTexture.h" |
13 #include "GrTBackendEffectFactory.h" | 13 #include "GrTBackendEffectFactory.h" |
14 #include "GrTexture.h" | 14 #include "GrTexture.h" |
15 | 15 |
16 class GrGLSimpleTextureEffect : public GrGLEffect { | 16 class GrGLHQScaleTextureEffect : public GrGLEffect { |
17 public: | 17 public: |
18 GrGLSimpleTextureEffect(const GrBackendEffectFactory& factory, const GrDrawE
ffect& drawEffect) | 18 GrGLHQScaleTextureEffect(const GrBackendEffectFactory& factory, const GrDraw
Effect& drawEffect) |
19 : INHERITED (factory) { | 19 : INHERITED (factory) { |
20 GrEffect::CoordsType coordsType = | 20 GrEffect::CoordsType coordsType = |
21 drawEffect.castEffect<GrSimpleTextureEffect>().coordsType(); | 21 drawEffect.castEffect<GrHQScaleTextureEffect>().coordsType(); |
22 if (GrEffect::kCustom_CoordsType != coordsType) { | 22 if (GrEffect::kCustom_CoordsType != coordsType) { |
23 SkNEW_IN_TLAZY(&fEffectMatrix, GrGLEffectMatrix, (coordsType)); | 23 SkNEW_IN_TLAZY(&fEffectMatrix, GrGLEffectMatrix, (coordsType)); |
24 } | 24 } |
25 } | 25 } |
26 | 26 |
27 virtual void emitCode(GrGLShaderBuilder* builder, | 27 virtual void emitCode(GrGLShaderBuilder* builder, |
28 const GrDrawEffect& drawEffect, | 28 const GrDrawEffect& drawEffect, |
29 EffectKey key, | 29 EffectKey key, |
30 const char* outputColor, | 30 const char* outputColor, |
31 const char* inputColor, | 31 const char* inputColor, |
32 const TextureSamplerArray& samplers) SK_OVERRIDE { | 32 const TextureSamplerArray& samplers) SK_OVERRIDE { |
33 const GrSimpleTextureEffect& ste = drawEffect.castEffect<GrSimpleTexture
Effect>(); | 33 const GrHQScaleTextureEffect& ste = drawEffect.castEffect<GrHQScaleTextu
reEffect>(); |
34 SkString fsCoordName; | 34 const char* fsCoordName; |
35 GrSLType fsCoordSLType; | 35 GrSLType fsCoordSLType; |
36 if (GrEffect::kCustom_CoordsType == ste.coordsType()) { | 36 if (GrEffect::kCustom_CoordsType == ste.coordsType()) { |
37 SkASSERT(ste.getMatrix().isIdentity()); | 37 SkASSERT(ste.getMatrix().isIdentity()); |
38 SkASSERT(1 == ste.numVertexAttribs()); | 38 SkASSERT(1 == ste.numVertexAttribs()); |
39 fsCoordSLType = kVec2f_GrSLType; | 39 fsCoordSLType = kVec2f_GrSLType; |
40 const char* vsVaryingName; | 40 const char* vsVaryingName; |
41 const char* fsVaryingNamePtr; | 41 builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsVaryingName
, &fsCoordName); |
42 GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertex
Builder(); | |
43 SkASSERT(NULL != vertexBuilder); | |
44 vertexBuilder->addVarying(kVec2f_GrSLType, "textureCoords", &vsVaryi
ngName, &fsVaryingNamePtr); | |
45 fsCoordName = fsVaryingNamePtr; | |
46 const char* attrName = | 42 const char* attrName = |
47 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttrib
Indices()[0])->c_str(); | 43 builder->getEffectAttributeName(drawEffect.getVertexAttribIndice
s()[0])->c_str(); |
48 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, attrName
); | 44 builder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, attrName); |
49 } else { | 45 } else { |
50 fsCoordSLType = fEffectMatrix.get()->emitCode(builder, key, &fsCoord
Name); | 46 fsCoordSLType = fEffectMatrix.get()->emitCode(builder, key, &fsCoord
Name); |
51 } | 47 } |
52 builder->fsCodeAppendf("\t%s = ", outputColor); | 48 builder->fsCodeAppendf("\t%s = ", outputColor); |
53 builder->fsAppendTextureLookupAndModulate(inputColor, | 49 builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment_Sha
derType, |
54 samplers[0], | 50 inputColor, |
55 fsCoordName.c_str(), | 51 samplers[0], |
56 fsCoordSLType); | 52 fsCoordName, |
| 53 fsCoordSLType); |
57 builder->fsCodeAppend(";\n"); | 54 builder->fsCodeAppend(";\n"); |
58 } | 55 } |
59 | 56 |
60 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCap
s&) { | 57 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCap
s&) { |
61 const GrSimpleTextureEffect& ste = drawEffect.castEffect<GrSimpleTexture
Effect>(); | 58 const GrHQScaleTextureEffect& ste = drawEffect.castEffect<GrHQScaleTextu
reEffect>(); |
62 if (GrEffect::kCustom_CoordsType == ste.coordsType()) { | 59 if (GrEffect::kCustom_CoordsType == ste.coordsType()) { |
63 return 1 << GrGLEffectMatrix::kKeyBits; | 60 return 1 << GrGLEffectMatrix::kKeyBits; |
64 } else { | 61 } else { |
65 return GrGLEffectMatrix::GenKey(ste.getMatrix(), | 62 return GrGLEffectMatrix::GenKey(ste.getMatrix(), |
66 drawEffect, | 63 drawEffect, |
67 ste.coordsType(), | 64 ste.coordsType(), |
68 ste.texture(0)); | 65 ste.texture(0)); |
69 } | 66 } |
70 } | 67 } |
71 | 68 |
72 virtual void setData(const GrGLUniformManager& uman, | 69 virtual void setData(const GrGLUniformManager& uman, |
73 const GrDrawEffect& drawEffect) SK_OVERRIDE { | 70 const GrDrawEffect& drawEffect) SK_OVERRIDE { |
74 const GrSimpleTextureEffect& ste = drawEffect.castEffect<GrSimpleTexture
Effect>(); | 71 const GrHQScaleTextureEffect& ste = drawEffect.castEffect<GrHQScaleTextu
reEffect>(); |
75 if (GrEffect::kCustom_CoordsType == ste.coordsType()) { | 72 if (GrEffect::kCustom_CoordsType == ste.coordsType()) { |
76 SkASSERT(ste.getMatrix().isIdentity()); | 73 SkASSERT(ste.getMatrix().isIdentity()); |
77 } else { | 74 } else { |
78 fEffectMatrix.get()->setData(uman, ste.getMatrix(), drawEffect, ste.
texture(0)); | 75 fEffectMatrix.get()->setData(uman, ste.getMatrix(), drawEffect, ste.
texture(0)); |
79 } | 76 } |
80 } | 77 } |
81 | 78 |
82 private: | 79 private: |
83 SkTLazy<GrGLEffectMatrix> fEffectMatrix; | 80 SkTLazy<GrGLEffectMatrix> fEffectMatrix; |
84 typedef GrGLEffect INHERITED; | 81 typedef GrGLEffect INHERITED; |
85 }; | 82 }; |
86 | 83 |
87 /////////////////////////////////////////////////////////////////////////////// | 84 /////////////////////////////////////////////////////////////////////////////// |
88 | 85 |
89 void GrSimpleTextureEffect::getConstantColorComponents(GrColor* color, uint32_t*
validFlags) const { | 86 void GrHQScaleTextureEffect::getConstantColorComponents(GrColor* color, uint32_t
* validFlags) const { |
90 this->updateConstantColorComponentsForModulation(color, validFlags); | 87 this->updateConstantColorComponentsForModulation(color, validFlags); |
91 } | 88 } |
92 | 89 |
93 const GrBackendEffectFactory& GrSimpleTextureEffect::getFactory() const { | 90 const GrBackendEffectFactory& GrHQScaleTextureEffect::getFactory() const { |
94 return GrTBackendEffectFactory<GrSimpleTextureEffect>::getInstance(); | 91 return GrTBackendEffectFactory<GrHQScaleTextureEffect>::getInstance(); |
95 } | 92 } |
96 | 93 |
97 /////////////////////////////////////////////////////////////////////////////// | 94 /////////////////////////////////////////////////////////////////////////////// |
98 | 95 |
99 GR_DEFINE_EFFECT_TEST(GrSimpleTextureEffect); | 96 GR_DEFINE_EFFECT_TEST(GrHQScaleTextureEffect); |
100 | 97 |
101 GrEffectRef* GrSimpleTextureEffect::TestCreate(SkMWCRandom* random, | 98 GrEffectRef* GrHQScaleTextureEffect::TestCreate(SkMWCRandom* random, |
102 GrContext*, | 99 GrContext*, |
103 const GrDrawTargetCaps&, | 100 const GrDrawTargetCaps&, |
104 GrTexture* textures[]) { | 101 GrTexture* textures[]) { |
105 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : | 102 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
106 GrEffectUnitTest::kAlphaTextureIdx; | 103 GrEffectUnitTest::kAlphaTextureIdx; |
107 static const SkShader::TileMode kTileModes[] = { | 104 static const SkShader::TileMode kTileModes[] = { |
108 SkShader::kClamp_TileMode, | 105 SkShader::kClamp_TileMode, |
109 SkShader::kRepeat_TileMode, | 106 SkShader::kRepeat_TileMode, |
110 SkShader::kMirror_TileMode, | 107 SkShader::kMirror_TileMode, |
111 }; | 108 }; |
112 SkShader::TileMode tileModes[] = { | 109 SkShader::TileMode tileModes[] = { |
113 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], | 110 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
114 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], | 111 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
115 }; | 112 }; |
116 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBil
erp_FilterMode : | 113 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBil
erp_FilterMode : |
117 GrTextureParams::kNon
e_FilterMode); | 114 GrTextureParams::kNon
e_FilterMode); |
118 | 115 |
119 static const CoordsType kCoordsTypes[] = { | 116 static const CoordsType kCoordsTypes[] = { |
120 kLocal_CoordsType, | 117 kLocal_CoordsType, |
121 kPosition_CoordsType, | 118 kPosition_CoordsType, |
122 kCustom_CoordsType | 119 kCustom_CoordsType |
123 }; | 120 }; |
124 CoordsType coordsType = kCoordsTypes[random->nextULessThan(GR_ARRAY_COUNT(kC
oordsTypes))]; | 121 CoordsType coordsType = kCoordsTypes[random->nextULessThan(GR_ARRAY_COUNT(kC
oordsTypes))]; |
125 | 122 |
126 if (kCustom_CoordsType == coordsType) { | 123 if (kCustom_CoordsType == coordsType) { |
127 return GrSimpleTextureEffect::CreateWithCustomCoords(textures[texIdx], p
arams); | 124 return GrHQScaleTextureEffect::CreateWithCustomCoords(textures[texIdx],
params); |
128 } else { | 125 } else { |
129 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random); | 126 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random); |
130 return GrSimpleTextureEffect::Create(textures[texIdx], matrix); | 127 return GrHQScaleTextureEffect::Create(textures[texIdx], matrix); |
131 } | 128 } |
132 } | 129 } |
OLD | NEW |