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 "GrTextureDomainEffect.h" | 8 #include "GrTextureDomainEffect.h" |
9 #include "GrSimpleTextureEffect.h" | 9 #include "GrSimpleTextureEffect.h" |
10 #include "GrTBackendEffectFactory.h" | 10 #include "GrTBackendEffectFactory.h" |
11 #include "gl/GrGLEffect.h" | 11 #include "gl/GrGLEffect.h" |
12 #include "gl/GrGLEffectMatrix.h" | 12 #include "gl/GrGLEffectMatrix.h" |
13 #include "SkFloatingPoint.h" | 13 #include "SkFloatingPoint.h" |
14 | 14 |
15 class GrGLTextureDomainEffect : public GrGLEffect { | 15 class GrGLTextureDomainEffect : public GrGLEffect { |
16 public: | 16 public: |
17 GrGLTextureDomainEffect(const GrBackendEffectFactory&, const GrDrawEffect&); | 17 GrGLTextureDomainEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
18 | 18 |
19 virtual void emitCode(GrGLShaderBuilder*, | 19 virtual void emitCode(GrGLShaderBuilder*, |
20 const GrDrawEffect&, | 20 const GrDrawEffect&, |
21 EffectKey, | 21 EffectKey, |
22 const char* outputColor, | 22 const char* outputColor, |
23 const char* inputColor, | 23 const char* inputColor, |
24 const TextureSamplerArray&) SK_OVERRIDE; | 24 const TextureSamplerArray&) SK_OVERRIDE; |
25 | 25 |
26 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; | 26 virtual void setData(const GrGLContext&, const GrDrawEffect&) SK_OVERRIDE; |
27 | 27 |
28 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); | 28 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
29 | 29 |
30 private: | 30 private: |
31 GrGLUniformManager::UniformHandle fNameUni; | 31 GrGLUniform* fNameUni; |
32 GrGLEffectMatrix fEffectMatrix; | 32 GrGLEffectMatrix fEffectMatrix; |
33 GrGLfloat fPrevDomain[4]; | 33 GrGLfloat fPrevDomain[4]; |
34 | 34 |
35 typedef GrGLEffect INHERITED; | 35 typedef GrGLEffect INHERITED; |
36 }; | 36 }; |
37 | 37 |
38 GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrBackendEffectFactory& f
actory, | 38 GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrBackendEffectFactory& f
actory, |
39 const GrDrawEffect& drawEffect) | 39 const GrDrawEffect& drawEffect) |
40 : INHERITED(factory) | 40 : INHERITED(factory) |
41 , fNameUni(GrGLUniformManager::kInvalidUniformHandle) | 41 , fNameUni(NULL) |
42 , fEffectMatrix(drawEffect.castEffect<GrTextureDomainEffect>().coordsType())
{ | 42 , fEffectMatrix(drawEffect.castEffect<GrTextureDomainEffect>().coordsType())
{ |
43 fPrevDomain[0] = SK_FloatNaN; | 43 fPrevDomain[0] = SK_FloatNaN; |
44 } | 44 } |
45 | 45 |
46 void GrGLTextureDomainEffect::emitCode(GrGLShaderBuilder* builder, | 46 void GrGLTextureDomainEffect::emitCode(GrGLShaderBuilder* builder, |
47 const GrDrawEffect& drawEffect, | 47 const GrDrawEffect& drawEffect, |
48 EffectKey key, | 48 EffectKey key, |
49 const char* outputColor, | 49 const char* outputColor, |
50 const char* inputColor, | 50 const char* inputColor, |
51 const TextureSamplerArray& samplers) { | 51 const TextureSamplerArray& samplers) { |
52 const GrTextureDomainEffect& texDom = drawEffect.castEffect<GrTextureDomainE
ffect>(); | 52 const GrTextureDomainEffect& texDom = drawEffect.castEffect<GrTextureDomainE
ffect>(); |
53 | 53 |
54 const char* coords; | 54 const char* coords; |
55 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &coords); | 55 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &coords); |
56 const char* domain; | 56 const char* domain; |
57 fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, | 57 fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
58 kVec4f_GrSLType, "TexDom", &domain); | 58 kVec4f_GrSLType, "TexDom", &domain)->glUnifor
m(); |
59 if (GrTextureDomainEffect::kClamp_WrapMode == texDom.wrapMode()) { | 59 if (GrTextureDomainEffect::kClamp_WrapMode == texDom.wrapMode()) { |
60 | 60 |
61 builder->fsCodeAppendf("\tvec2 clampCoord = clamp(%s, %s.xy, %s.zw);\n", | 61 builder->fsCodeAppendf("\tvec2 clampCoord = clamp(%s, %s.xy, %s.zw);\n", |
62 coords, domain, domain); | 62 coords, domain, domain); |
63 | 63 |
64 builder->fsCodeAppendf("\t%s = ", outputColor); | 64 builder->fsCodeAppendf("\t%s = ", outputColor); |
65 builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment_Sha
derType, | 65 builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment_Sha
derType, |
66 inputColor, | 66 inputColor, |
67 samplers[0], | 67 samplers[0], |
68 "clampCoord"); | 68 "clampCoord"); |
(...skipping 29 matching lines...) Expand all Loading... |
98 builder->fsCodeAppendf("\t%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.
0) : ", outputColor); | 98 builder->fsCodeAppendf("\t%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.
0) : ", outputColor); |
99 builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment
_ShaderType, | 99 builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment
_ShaderType, |
100 inputColor, | 100 inputColor, |
101 samplers[0], | 101 samplers[0], |
102 coords); | 102 coords); |
103 builder->fsCodeAppend(";\n"); | 103 builder->fsCodeAppend(";\n"); |
104 } | 104 } |
105 } | 105 } |
106 } | 106 } |
107 | 107 |
108 void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman, | 108 void GrGLTextureDomainEffect::setData(const GrGLContext& context, |
109 const GrDrawEffect& drawEffect) { | 109 const GrDrawEffect& drawEffect) { |
110 const GrTextureDomainEffect& texDom = drawEffect.castEffect<GrTextureDomainE
ffect>(); | 110 const GrTextureDomainEffect& texDom = drawEffect.castEffect<GrTextureDomainE
ffect>(); |
111 const SkRect& domain = texDom.domain(); | 111 const SkRect& domain = texDom.domain(); |
112 | 112 |
113 float values[4] = { | 113 float values[4] = { |
114 SkScalarToFloat(domain.left()), | 114 SkScalarToFloat(domain.left()), |
115 SkScalarToFloat(domain.top()), | 115 SkScalarToFloat(domain.top()), |
116 SkScalarToFloat(domain.right()), | 116 SkScalarToFloat(domain.right()), |
117 SkScalarToFloat(domain.bottom()) | 117 SkScalarToFloat(domain.bottom()) |
118 }; | 118 }; |
119 // vertical flip if necessary | 119 // vertical flip if necessary |
120 if (kBottomLeft_GrSurfaceOrigin == texDom.texture(0)->origin()) { | 120 if (kBottomLeft_GrSurfaceOrigin == texDom.texture(0)->origin()) { |
121 values[1] = 1.0f - values[1]; | 121 values[1] = 1.0f - values[1]; |
122 values[3] = 1.0f - values[3]; | 122 values[3] = 1.0f - values[3]; |
123 // The top and bottom were just flipped, so correct the ordering | 123 // The top and bottom were just flipped, so correct the ordering |
124 // of elements so that values = (l, t, r, b). | 124 // of elements so that values = (l, t, r, b). |
125 SkTSwap(values[1], values[3]); | 125 SkTSwap(values[1], values[3]); |
126 } | 126 } |
127 if (0 != memcmp(values, fPrevDomain, 4 * sizeof(GrGLfloat))) { | 127 if (0 != memcmp(values, fPrevDomain, 4 * sizeof(GrGLfloat))) { |
128 uman.set4fv(fNameUni, 0, 1, values); | 128 fNameUni->set4fv(context, 0, 1, values); |
129 memcpy(fPrevDomain, values, 4 * sizeof(GrGLfloat)); | 129 memcpy(fPrevDomain, values, 4 * sizeof(GrGLfloat)); |
130 } | 130 } |
131 fEffectMatrix.setData(uman, | 131 fEffectMatrix.setData(context, |
132 texDom.getMatrix(), | 132 texDom.getMatrix(), |
133 drawEffect, | 133 drawEffect, |
134 texDom.texture(0)); | 134 texDom.texture(0)); |
135 } | 135 } |
136 | 136 |
137 GrGLEffect::EffectKey GrGLTextureDomainEffect::GenKey(const GrDrawEffect& drawEf
fect, | 137 GrGLEffect::EffectKey GrGLTextureDomainEffect::GenKey(const GrDrawEffect& drawEf
fect, |
138 const GrGLCaps&) { | 138 const GrGLCaps&) { |
139 const GrTextureDomainEffect& texDom = drawEffect.castEffect<GrTextureDomainE
ffect>(); | 139 const GrTextureDomainEffect& texDom = drawEffect.castEffect<GrTextureDomainE
ffect>(); |
140 EffectKey key = texDom.wrapMode(); | 140 EffectKey key = texDom.wrapMode(); |
141 key <<= GrGLEffectMatrix::kKeyBits; | 141 key <<= GrGLEffectMatrix::kKeyBits; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random); | 235 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random); |
236 bool bilerp = random->nextBool(); | 236 bool bilerp = random->nextBool(); |
237 CoordsType coords = random->nextBool() ? kLocal_CoordsType : kPosition_Coord
sType; | 237 CoordsType coords = random->nextBool() ? kLocal_CoordsType : kPosition_Coord
sType; |
238 return GrTextureDomainEffect::Create(textures[texIdx], | 238 return GrTextureDomainEffect::Create(textures[texIdx], |
239 matrix, | 239 matrix, |
240 domain, | 240 domain, |
241 wrapMode, | 241 wrapMode, |
242 bilerp ? GrTextureParams::kBilerp_Filte
rMode : GrTextureParams::kNone_FilterMode, | 242 bilerp ? GrTextureParams::kBilerp_Filte
rMode : GrTextureParams::kNone_FilterMode, |
243 coords); | 243 coords); |
244 } | 244 } |
OLD | NEW |