GrTextureDomain.cpp revision 2d721d33aad192cc8a7a1321504b39bdca2a57ce
1/* 2 * Copyright 2012 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 "GrTextureDomain.h" 9#include "GrInvariantOutput.h" 10#include "GrSimpleTextureEffect.h" 11#include "SkFloatingPoint.h" 12#include "gl/GrGLContext.h" 13#include "gl/GrGLFragmentProcessor.h" 14#include "glsl/GrGLSLProgramBuilder.h" 15#include "glsl/GrGLSLProgramDataManager.h" 16#include "glsl/GrGLSLTextureSampler.h" 17 18GrTextureDomain::GrTextureDomain(const SkRect& domain, Mode mode, int index) 19 : fIndex(index) { 20 21 static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1}; 22 if (domain.contains(kFullRect) && kClamp_Mode == mode) { 23 fMode = kIgnore_Mode; 24 } else { 25 fMode = mode; 26 } 27 28 if (fMode != kIgnore_Mode) { 29 // We don't currently handle domains that are empty or don't intersect the texture. 30 // It is OK if the domain rect is a line or point, but it should not be inverted. We do not 31 // handle rects that do not intersect the [0..1]x[0..1] rect. 32 SkASSERT(domain.fLeft <= domain.fRight); 33 SkASSERT(domain.fTop <= domain.fBottom); 34 fDomain.fLeft = SkScalarPin(domain.fLeft, kFullRect.fLeft, kFullRect.fRight); 35 fDomain.fRight = SkScalarPin(domain.fRight, kFullRect.fLeft, kFullRect.fRight); 36 fDomain.fTop = SkScalarPin(domain.fTop, kFullRect.fTop, kFullRect.fBottom); 37 fDomain.fBottom = SkScalarPin(domain.fBottom, kFullRect.fTop, kFullRect.fBottom); 38 SkASSERT(fDomain.fLeft <= fDomain.fRight); 39 SkASSERT(fDomain.fTop <= fDomain.fBottom); 40 } 41} 42 43////////////////////////////////////////////////////////////////////////////// 44 45void GrTextureDomain::GLDomain::sampleTexture(GrGLSLShaderBuilder* builder, 46 const GrTextureDomain& textureDomain, 47 const char* outColor, 48 const SkString& inCoords, 49 const GrGLSLTextureSampler& sampler, 50 const char* inModulateColor) { 51 SkASSERT((Mode)-1 == fMode || textureDomain.mode() == fMode); 52 SkDEBUGCODE(fMode = textureDomain.mode();) 53 54 GrGLSLProgramBuilder* program = builder->getProgramBuilder(); 55 56 if (textureDomain.mode() != kIgnore_Mode && !fDomainUni.isValid()) { 57 const char* name; 58 SkString uniName("TexDom"); 59 if (textureDomain.fIndex >= 0) { 60 uniName.appendS32(textureDomain.fIndex); 61 } 62 fDomainUni = program->addUniform(GrGLSLProgramBuilder::kFragment_Visibility, 63 kVec4f_GrSLType, kDefault_GrSLPrecision, 64 uniName.c_str(), &name); 65 fDomainName = name; 66 } 67 68 switch (textureDomain.mode()) { 69 case kIgnore_Mode: { 70 builder->codeAppendf("%s = ", outColor); 71 builder->appendTextureLookupAndModulate(inModulateColor, sampler, 72 inCoords.c_str()); 73 builder->codeAppend(";"); 74 break; 75 } 76 case kClamp_Mode: { 77 SkString clampedCoords; 78 clampedCoords.appendf("clamp(%s, %s.xy, %s.zw)", 79 inCoords.c_str(), fDomainName.c_str(), fDomainName.c_str()); 80 81 builder->codeAppendf("%s = ", outColor); 82 builder->appendTextureLookupAndModulate(inModulateColor, sampler, 83 clampedCoords.c_str()); 84 builder->codeAppend(";"); 85 break; 86 } 87 case kDecal_Mode: { 88 // Add a block since we're going to declare variables. 89 GrGLSLShaderBuilder::ShaderBlock block(builder); 90 91 const char* domain = fDomainName.c_str(); 92 if (!program->glslCaps()->canUseAnyFunctionInShader()) { 93 // On the NexusS and GalaxyNexus, the other path (with the 'any' 94 // call) causes the compilation error "Calls to any function that 95 // may require a gradient calculation inside a conditional block 96 // may return undefined results". This appears to be an issue with 97 // the 'any' call since even the simple "result=black; if (any()) 98 // result=white;" code fails to compile. 99 builder->codeAppend("vec4 outside = vec4(0.0, 0.0, 0.0, 0.0);"); 100 builder->codeAppend("vec4 inside = "); 101 builder->appendTextureLookupAndModulate(inModulateColor, sampler, 102 inCoords.c_str()); 103 builder->codeAppend(";"); 104 105 builder->codeAppend(GrGLSLShaderVar::PrecisionString(program->glslCaps(), 106 kHigh_GrSLPrecision)); 107 builder->codeAppendf("float x = (%s).x;", inCoords.c_str()); 108 builder->codeAppend(GrGLSLShaderVar::PrecisionString(program->glslCaps(), 109 kHigh_GrSLPrecision)); 110 builder->codeAppendf("float y = (%s).y;", inCoords.c_str()); 111 112 builder->codeAppendf("x = abs(2.0*(x - %s.x)/(%s.z - %s.x) - 1.0);", 113 domain, domain, domain); 114 builder->codeAppendf("y = abs(2.0*(y - %s.y)/(%s.w - %s.y) - 1.0);", 115 domain, domain, domain); 116 builder->codeAppend("float blend = step(1.0, max(x, y));"); 117 builder->codeAppendf("%s = mix(inside, outside, blend);", outColor); 118 } else { 119 builder->codeAppend("bvec4 outside;\n"); 120 builder->codeAppendf("outside.xy = lessThan(%s, %s.xy);", inCoords.c_str(), 121 domain); 122 builder->codeAppendf("outside.zw = greaterThan(%s, %s.zw);", inCoords.c_str(), 123 domain); 124 builder->codeAppendf("%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.0) : ", 125 outColor); 126 builder->appendTextureLookupAndModulate(inModulateColor, sampler, 127 inCoords.c_str()); 128 builder->codeAppend(";"); 129 } 130 break; 131 } 132 case kRepeat_Mode: { 133 SkString clampedCoords; 134 clampedCoords.printf("mod(%s - %s.xy, %s.zw - %s.xy) + %s.xy", 135 inCoords.c_str(), fDomainName.c_str(), fDomainName.c_str(), 136 fDomainName.c_str(), fDomainName.c_str()); 137 138 builder->codeAppendf("%s = ", outColor); 139 builder->appendTextureLookupAndModulate(inModulateColor, sampler, 140 clampedCoords.c_str()); 141 builder->codeAppend(";"); 142 break; 143 } 144 } 145} 146 147void GrTextureDomain::GLDomain::setData(const GrGLSLProgramDataManager& pdman, 148 const GrTextureDomain& textureDomain, 149 GrSurfaceOrigin textureOrigin) { 150 SkASSERT(textureDomain.mode() == fMode); 151 if (kIgnore_Mode != textureDomain.mode()) { 152 float values[kPrevDomainCount] = { 153 SkScalarToFloat(textureDomain.domain().left()), 154 SkScalarToFloat(textureDomain.domain().top()), 155 SkScalarToFloat(textureDomain.domain().right()), 156 SkScalarToFloat(textureDomain.domain().bottom()) 157 }; 158 // vertical flip if necessary 159 if (kBottomLeft_GrSurfaceOrigin == textureOrigin) { 160 values[1] = 1.0f - values[1]; 161 values[3] = 1.0f - values[3]; 162 // The top and bottom were just flipped, so correct the ordering 163 // of elements so that values = (l, t, r, b). 164 SkTSwap(values[1], values[3]); 165 } 166 if (0 != memcmp(values, fPrevDomain, kPrevDomainCount * sizeof(float))) { 167 pdman.set4fv(fDomainUni, 1, values); 168 memcpy(fPrevDomain, values, kPrevDomainCount * sizeof(float)); 169 } 170 } 171} 172 173 174////////////////////////////////////////////////////////////////////////////// 175 176class GrGLTextureDomainEffect : public GrGLFragmentProcessor { 177public: 178 GrGLTextureDomainEffect(const GrProcessor&); 179 180 virtual void emitCode(EmitArgs&) override; 181 182 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*); 183 184protected: 185 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override; 186 187private: 188 GrTextureDomain::GLDomain fGLDomain; 189 typedef GrGLFragmentProcessor INHERITED; 190}; 191 192GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrProcessor&) { 193} 194 195void GrGLTextureDomainEffect::emitCode(EmitArgs& args) { 196 const GrTextureDomainEffect& textureDomainEffect = args.fFp.cast<GrTextureDomainEffect>(); 197 const GrTextureDomain& domain = textureDomainEffect.textureDomain(); 198 199 GrGLSLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder(); 200 SkString coords2D = fsBuilder->ensureFSCoords2D(args.fCoords, 0); 201 fGLDomain.sampleTexture(fsBuilder, domain, args.fOutputColor, coords2D, args.fSamplers[0], 202 args.fInputColor); 203} 204 205void GrGLTextureDomainEffect::onSetData(const GrGLSLProgramDataManager& pdman, 206 const GrProcessor& processor) { 207 const GrTextureDomainEffect& textureDomainEffect = processor.cast<GrTextureDomainEffect>(); 208 const GrTextureDomain& domain = textureDomainEffect.textureDomain(); 209 fGLDomain.setData(pdman, domain, processor.texture(0)->origin()); 210} 211 212void GrGLTextureDomainEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&, 213 GrProcessorKeyBuilder* b) { 214 const GrTextureDomain& domain = processor.cast<GrTextureDomainEffect>().textureDomain(); 215 b->add32(GrTextureDomain::GLDomain::DomainKey(domain)); 216} 217 218 219/////////////////////////////////////////////////////////////////////////////// 220 221const GrFragmentProcessor* GrTextureDomainEffect::Create(GrTexture* texture, 222 const SkMatrix& matrix, 223 const SkRect& domain, 224 GrTextureDomain::Mode mode, 225 GrTextureParams::FilterMode filterMode, 226 GrCoordSet coordSet) { 227 static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1}; 228 if (GrTextureDomain::kIgnore_Mode == mode || 229 (GrTextureDomain::kClamp_Mode == mode && domain.contains(kFullRect))) { 230 return GrSimpleTextureEffect::Create(texture, matrix, filterMode); 231 } else { 232 return new GrTextureDomainEffect(texture, matrix, domain, mode, filterMode, coordSet); 233 } 234} 235 236GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, 237 const SkMatrix& matrix, 238 const SkRect& domain, 239 GrTextureDomain::Mode mode, 240 GrTextureParams::FilterMode filterMode, 241 GrCoordSet coordSet) 242 : GrSingleTextureEffect(texture, matrix, filterMode, coordSet) 243 , fTextureDomain(domain, mode) { 244 SkASSERT(mode != GrTextureDomain::kRepeat_Mode || 245 filterMode == GrTextureParams::kNone_FilterMode); 246 this->initClassID<GrTextureDomainEffect>(); 247} 248 249GrTextureDomainEffect::~GrTextureDomainEffect() {} 250 251void GrTextureDomainEffect::onGetGLProcessorKey(const GrGLSLCaps& caps, 252 GrProcessorKeyBuilder* b) const { 253 GrGLTextureDomainEffect::GenKey(*this, caps, b); 254} 255 256GrGLFragmentProcessor* GrTextureDomainEffect::onCreateGLInstance() const { 257 return new GrGLTextureDomainEffect(*this); 258} 259 260bool GrTextureDomainEffect::onIsEqual(const GrFragmentProcessor& sBase) const { 261 const GrTextureDomainEffect& s = sBase.cast<GrTextureDomainEffect>(); 262 return this->fTextureDomain == s.fTextureDomain; 263} 264 265void GrTextureDomainEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { 266 if (GrTextureDomain::kDecal_Mode == fTextureDomain.mode()) { // TODO: helper 267 if (GrPixelConfigIsAlphaOnly(this->texture(0)->config())) { 268 inout->mulByUnknownSingleComponent(); 269 } else { 270 inout->mulByUnknownFourComponents(); 271 } 272 } else { 273 this->updateInvariantOutputForModulation(inout); 274 } 275} 276 277/////////////////////////////////////////////////////////////////////////////// 278 279GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTextureDomainEffect); 280 281const GrFragmentProcessor* GrTextureDomainEffect::TestCreate(GrProcessorTestData* d) { 282 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : 283 GrProcessorUnitTest::kAlphaTextureIdx; 284 SkRect domain; 285 domain.fLeft = d->fRandom->nextUScalar1(); 286 domain.fRight = d->fRandom->nextRangeScalar(domain.fLeft, SK_Scalar1); 287 domain.fTop = d->fRandom->nextUScalar1(); 288 domain.fBottom = d->fRandom->nextRangeScalar(domain.fTop, SK_Scalar1); 289 GrTextureDomain::Mode mode = 290 (GrTextureDomain::Mode) d->fRandom->nextULessThan(GrTextureDomain::kModeCount); 291 const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom); 292 bool bilerp = mode != GrTextureDomain::kRepeat_Mode ? d->fRandom->nextBool() : false; 293 GrCoordSet coords = d->fRandom->nextBool() ? kLocal_GrCoordSet : kDevice_GrCoordSet; 294 return GrTextureDomainEffect::Create( 295 d->fTextures[texIdx], 296 matrix, 297 domain, 298 mode, 299 bilerp ? GrTextureParams::kBilerp_FilterMode : GrTextureParams::kNone_FilterMode, 300 coords); 301} 302