OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 "gl/builders/GrGLProgramBuilder.h" |
8 #include "GrBicubicEffect.h" | 9 #include "GrBicubicEffect.h" |
9 | 10 |
10 #include "gl/GrGLShaderBuilder.h" | |
11 | 11 |
12 #define DS(x) SkDoubleToScalar(x) | 12 #define DS(x) SkDoubleToScalar(x) |
13 | 13 |
14 const SkScalar GrBicubicEffect::gMitchellCoefficients[16] = { | 14 const SkScalar GrBicubicEffect::gMitchellCoefficients[16] = { |
15 DS( 1.0 / 18.0), DS(-9.0 / 18.0), DS( 15.0 / 18.0), DS( -7.0 / 18.0), | 15 DS( 1.0 / 18.0), DS(-9.0 / 18.0), DS( 15.0 / 18.0), DS( -7.0 / 18.0), |
16 DS(16.0 / 18.0), DS( 0.0 / 18.0), DS(-36.0 / 18.0), DS( 21.0 / 18.0), | 16 DS(16.0 / 18.0), DS( 0.0 / 18.0), DS(-36.0 / 18.0), DS( 21.0 / 18.0), |
17 DS( 1.0 / 18.0), DS( 9.0 / 18.0), DS( 27.0 / 18.0), DS(-21.0 / 18.0), | 17 DS( 1.0 / 18.0), DS( 9.0 / 18.0), DS( 27.0 / 18.0), DS(-21.0 / 18.0), |
18 DS( 0.0 / 18.0), DS( 0.0 / 18.0), DS( -6.0 / 18.0), DS( 7.0 / 18.0), | 18 DS( 0.0 / 18.0), DS( 0.0 / 18.0), DS( -6.0 / 18.0), DS( 7.0 / 18.0), |
19 }; | 19 }; |
20 | 20 |
21 | 21 |
22 class GrGLBicubicEffect : public GrGLEffect { | 22 class GrGLBicubicEffect : public GrGLEffect { |
23 public: | 23 public: |
24 GrGLBicubicEffect(const GrBackendEffectFactory& factory, | 24 GrGLBicubicEffect(const GrBackendEffectFactory& factory, |
25 const GrDrawEffect&); | 25 const GrDrawEffect&); |
26 | 26 |
27 virtual void emitCode(GrGLShaderBuilder*, | 27 virtual void emitCode(GrGLProgramBuilder*, |
28 const GrDrawEffect&, | 28 const GrDrawEffect&, |
29 const GrEffectKey&, | 29 const GrEffectKey&, |
30 const char* outputColor, | 30 const char* outputColor, |
31 const char* inputColor, | 31 const char* inputColor, |
32 const TransformedCoordsArray&, | 32 const TransformedCoordsArray&, |
33 const TextureSamplerArray&) SK_OVERRIDE; | 33 const TextureSamplerArray&) SK_OVERRIDE; |
34 | 34 |
35 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_
OVERRIDE; | 35 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_
OVERRIDE; |
36 | 36 |
37 static inline void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&, | 37 static inline void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&, |
38 GrEffectKeyBuilder* b) { | 38 GrEffectKeyBuilder* b) { |
39 const GrTextureDomain& domain = drawEffect.castEffect<GrBicubicEffect>()
.domain(); | 39 const GrTextureDomain& domain = drawEffect.castEffect<GrBicubicEffect>()
.domain(); |
40 b->add32(GrTextureDomain::GLDomain::DomainKey(domain)); | 40 b->add32(GrTextureDomain::GLDomain::DomainKey(domain)); |
41 } | 41 } |
42 | 42 |
43 private: | 43 private: |
44 typedef GrGLProgramDataManager::UniformHandle UniformHandle; | 44 typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
45 | 45 |
46 UniformHandle fCoefficientsUni; | 46 UniformHandle fCoefficientsUni; |
47 UniformHandle fImageIncrementUni; | 47 UniformHandle fImageIncrementUni; |
48 GrTextureDomain::GLDomain fDomain; | 48 GrTextureDomain::GLDomain fDomain; |
49 | 49 |
50 typedef GrGLEffect INHERITED; | 50 typedef GrGLEffect INHERITED; |
51 }; | 51 }; |
52 | 52 |
53 GrGLBicubicEffect::GrGLBicubicEffect(const GrBackendEffectFactory& factory, cons
t GrDrawEffect&) | 53 GrGLBicubicEffect::GrGLBicubicEffect(const GrBackendEffectFactory& factory, cons
t GrDrawEffect&) |
54 : INHERITED(factory) { | 54 : INHERITED(factory) { |
55 } | 55 } |
56 | 56 |
57 void GrGLBicubicEffect::emitCode(GrGLShaderBuilder* builder, | 57 void GrGLBicubicEffect::emitCode(GrGLProgramBuilder* builder, |
58 const GrDrawEffect& drawEffect, | 58 const GrDrawEffect& drawEffect, |
59 const GrEffectKey& key, | 59 const GrEffectKey& key, |
60 const char* outputColor, | 60 const char* outputColor, |
61 const char* inputColor, | 61 const char* inputColor, |
62 const TransformedCoordsArray& coords, | 62 const TransformedCoordsArray& coords, |
63 const TextureSamplerArray& samplers) { | 63 const TextureSamplerArray& samplers) { |
64 const GrTextureDomain& domain = drawEffect.castEffect<GrBicubicEffect>().dom
ain(); | 64 const GrTextureDomain& domain = drawEffect.castEffect<GrBicubicEffect>().dom
ain(); |
65 | 65 |
66 SkString coords2D = builder->ensureFSCoords2D(coords, 0); | 66 fCoefficientsUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibil
ity, |
67 fCoefficientsUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibili
ty, | |
68 kMat44f_GrSLType, "Coefficients"); | 67 kMat44f_GrSLType, "Coefficients"); |
69 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibi
lity, | 68 fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visib
ility, |
70 kVec2f_GrSLType, "ImageIncrement"); | 69 kVec2f_GrSLType, "ImageIncrement"); |
71 | 70 |
72 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); | 71 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
73 const char* coeff = builder->getUniformCStr(fCoefficientsUni); | 72 const char* coeff = builder->getUniformCStr(fCoefficientsUni); |
74 | 73 |
75 SkString cubicBlendName; | 74 SkString cubicBlendName; |
76 | 75 |
77 static const GrGLShaderVar gCubicBlendArgs[] = { | 76 static const GrGLShaderVar gCubicBlendArgs[] = { |
78 GrGLShaderVar("coefficients", kMat44f_GrSLType), | 77 GrGLShaderVar("coefficients", kMat44f_GrSLType), |
79 GrGLShaderVar("t", kFloat_GrSLType), | 78 GrGLShaderVar("t", kFloat_GrSLType), |
80 GrGLShaderVar("c0", kVec4f_GrSLType), | 79 GrGLShaderVar("c0", kVec4f_GrSLType), |
81 GrGLShaderVar("c1", kVec4f_GrSLType), | 80 GrGLShaderVar("c1", kVec4f_GrSLType), |
82 GrGLShaderVar("c2", kVec4f_GrSLType), | 81 GrGLShaderVar("c2", kVec4f_GrSLType), |
83 GrGLShaderVar("c3", kVec4f_GrSLType), | 82 GrGLShaderVar("c3", kVec4f_GrSLType), |
84 }; | 83 }; |
85 builder->fsEmitFunction(kVec4f_GrSLType, | 84 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 85 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0); |
| 86 fsBuilder->emitFunction(kVec4f_GrSLType, |
86 "cubicBlend", | 87 "cubicBlend", |
87 SK_ARRAY_COUNT(gCubicBlendArgs), | 88 SK_ARRAY_COUNT(gCubicBlendArgs), |
88 gCubicBlendArgs, | 89 gCubicBlendArgs, |
89 "\tvec4 ts = vec4(1.0, t, t * t, t * t * t);\n" | 90 "\tvec4 ts = vec4(1.0, t, t * t, t * t * t);\n" |
90 "\tvec4 c = coefficients * ts;\n" | 91 "\tvec4 c = coefficients * ts;\n" |
91 "\treturn c.x * c0 + c.y * c1 + c.z * c2 + c.w * c3;
\n", | 92 "\treturn c.x * c0 + c.y * c1 + c.z * c2 + c.w * c3;
\n", |
92 &cubicBlendName); | 93 &cubicBlendName); |
93 builder->fsCodeAppendf("\tvec2 coord = %s - %s * vec2(0.5);\n", coords2D.c_s
tr(), imgInc); | 94 fsBuilder->codeAppendf("\tvec2 coord = %s - %s * vec2(0.5);\n", coords2D.c_s
tr(), imgInc); |
94 // We unnormalize the coord in order to determine our fractional offset (f)
within the texel | 95 // We unnormalize the coord in order to determine our fractional offset (f)
within the texel |
95 // We then snap coord to a texel center and renormalize. The snap prevents c
ases where the | 96 // We then snap coord to a texel center and renormalize. The snap prevents c
ases where the |
96 // starting coords are near a texel boundary and accumulations of imgInc wou
ld cause us to skip/ | 97 // starting coords are near a texel boundary and accumulations of imgInc wou
ld cause us to skip/ |
97 // double hit a texel. | 98 // double hit a texel. |
98 builder->fsCodeAppendf("\tcoord /= %s;\n", imgInc); | 99 fsBuilder->codeAppendf("\tcoord /= %s;\n", imgInc); |
99 builder->fsCodeAppend("\tvec2 f = fract(coord);\n"); | 100 fsBuilder->codeAppend("\tvec2 f = fract(coord);\n"); |
100 builder->fsCodeAppendf("\tcoord = (coord - f + vec2(0.5)) * %s;\n", imgInc); | 101 fsBuilder->codeAppendf("\tcoord = (coord - f + vec2(0.5)) * %s;\n", imgInc); |
101 builder->fsCodeAppend("\tvec4 rowColors[4];\n"); | 102 fsBuilder->codeAppend("\tvec4 rowColors[4];\n"); |
102 for (int y = 0; y < 4; ++y) { | 103 for (int y = 0; y < 4; ++y) { |
103 for (int x = 0; x < 4; ++x) { | 104 for (int x = 0; x < 4; ++x) { |
104 SkString coord; | 105 SkString coord; |
105 coord.printf("coord + %s * vec2(%d, %d)", imgInc, x - 1, y - 1); | 106 coord.printf("coord + %s * vec2(%d, %d)", imgInc, x - 1, y - 1); |
106 SkString sampleVar; | 107 SkString sampleVar; |
107 sampleVar.printf("rowColors[%d]", x); | 108 sampleVar.printf("rowColors[%d]", x); |
108 fDomain.sampleTexture(builder, domain, sampleVar.c_str(), coord, sam
plers[0]); | 109 fDomain.sampleTexture(fsBuilder, domain, sampleVar.c_str(), coord, s
amplers[0]); |
109 } | 110 } |
110 builder->fsCodeAppendf("\tvec4 s%d = %s(%s, f.x, rowColors[0], rowColors
[1], rowColors[2], rowColors[3]);\n", y, cubicBlendName.c_str(), coeff); | 111 fsBuilder->codeAppendf("\tvec4 s%d = %s(%s, f.x, rowColors[0], rowColors
[1], rowColors[2], rowColors[3]);\n", y, cubicBlendName.c_str(), coeff); |
111 } | 112 } |
112 SkString bicubicColor; | 113 SkString bicubicColor; |
113 bicubicColor.printf("%s(%s, f.y, s0, s1, s2, s3)", cubicBlendName.c_str(), c
oeff); | 114 bicubicColor.printf("%s(%s, f.y, s0, s1, s2, s3)", cubicBlendName.c_str(), c
oeff); |
114 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, (GrGLSLExpr4(bicubicColo
r.c_str()) * GrGLSLExpr4(inputColor)).c_str()); | 115 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor, (GrGLSLExpr4(bicubicColo
r.c_str()) * GrGLSLExpr4(inputColor)).c_str()); |
115 } | 116 } |
116 | 117 |
117 void GrGLBicubicEffect::setData(const GrGLProgramDataManager& pdman, | 118 void GrGLBicubicEffect::setData(const GrGLProgramDataManager& pdman, |
118 const GrDrawEffect& drawEffect) { | 119 const GrDrawEffect& drawEffect) { |
119 const GrBicubicEffect& effect = drawEffect.castEffect<GrBicubicEffect>(); | 120 const GrBicubicEffect& effect = drawEffect.castEffect<GrBicubicEffect>(); |
120 const GrTexture& texture = *effect.texture(0); | 121 const GrTexture& texture = *effect.texture(0); |
121 float imageIncrement[2]; | 122 float imageIncrement[2]; |
122 imageIncrement[0] = 1.0f / texture.width(); | 123 imageIncrement[0] = 1.0f / texture.width(); |
123 imageIncrement[1] = 1.0f / texture.height(); | 124 imageIncrement[1] = 1.0f / texture.height(); |
124 pdman.set2fv(fImageIncrementUni, 1, imageIncrement); | 125 pdman.set2fv(fImageIncrementUni, 1, imageIncrement); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 // Use bilerp to handle rotation or fractional translation. | 215 // Use bilerp to handle rotation or fractional translation. |
215 *filterMode = GrTextureParams::kBilerp_FilterMode; | 216 *filterMode = GrTextureParams::kBilerp_FilterMode; |
216 } | 217 } |
217 return false; | 218 return false; |
218 } | 219 } |
219 // When we use the bicubic filtering effect each sample is read from the tex
ture using | 220 // When we use the bicubic filtering effect each sample is read from the tex
ture using |
220 // nearest neighbor sampling. | 221 // nearest neighbor sampling. |
221 *filterMode = GrTextureParams::kNone_FilterMode; | 222 *filterMode = GrTextureParams::kNone_FilterMode; |
222 return true; | 223 return true; |
223 } | 224 } |
OLD | NEW |