blob: 1d3b37dddd98d84b23745178381cf302b0fd59cd [file] [log] [blame]
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +00001/*
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
joshualitt30ba4362014-08-21 20:18:45 -07008#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +00009#include "GrTextureDomain.h"
10#include "GrSimpleTextureEffect.h"
joshualittb0a8a372014-09-23 09:50:21 -070011#include "GrTBackendProcessorFactory.h"
12#include "gl/GrGLProcessor.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000013#include "SkFloatingPoint.h"
14
15
16GrTextureDomain::GrTextureDomain(const SkRect& domain, Mode mode, int index)
17 : fIndex(index) {
18
19 static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1};
commit-bot@chromium.org26632632014-03-25 15:13:18 +000020 if (domain.contains(kFullRect) && kClamp_Mode == mode) {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000021 fMode = kIgnore_Mode;
22 } else {
23 fMode = mode;
24 }
25
26 if (fMode != kIgnore_Mode) {
27 // We don't currently handle domains that are empty or don't intersect the texture.
28 // It is OK if the domain rect is a line or point, but it should not be inverted. We do not
29 // handle rects that do not intersect the [0..1]x[0..1] rect.
30 SkASSERT(domain.fLeft <= domain.fRight);
31 SkASSERT(domain.fTop <= domain.fBottom);
32 fDomain.fLeft = SkMaxScalar(domain.fLeft, kFullRect.fLeft);
33 fDomain.fRight = SkMinScalar(domain.fRight, kFullRect.fRight);
34 fDomain.fTop = SkMaxScalar(domain.fTop, kFullRect.fTop);
35 fDomain.fBottom = SkMinScalar(domain.fBottom, kFullRect.fBottom);
36 SkASSERT(fDomain.fLeft <= fDomain.fRight);
37 SkASSERT(fDomain.fTop <= fDomain.fBottom);
38 }
39}
40
41//////////////////////////////////////////////////////////////////////////////
42
43void GrTextureDomain::GLDomain::sampleTexture(GrGLShaderBuilder* builder,
44 const GrTextureDomain& textureDomain,
45 const char* outColor,
46 const SkString& inCoords,
joshualittb0a8a372014-09-23 09:50:21 -070047 const GrGLProcessor::TextureSampler sampler,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000048 const char* inModulateColor) {
reed@google.comd7b1af62013-12-09 20:31:50 +000049 SkASSERT((Mode)-1 == fMode || textureDomain.mode() == fMode);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000050 SkDEBUGCODE(fMode = textureDomain.mode();)
51
joshualitt30ba4362014-08-21 20:18:45 -070052 GrGLProgramBuilder* program = builder->getProgramBuilder();
53
joshualitt5ae5fc52014-07-29 12:59:27 -070054 if (textureDomain.mode() != kIgnore_Mode && !fDomainUni.isValid()) {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000055 const char* name;
56 SkString uniName("TexDom");
57 if (textureDomain.fIndex >= 0) {
58 uniName.appendS32(textureDomain.fIndex);
59 }
joshualitt30ba4362014-08-21 20:18:45 -070060 fDomainUni = program->addUniform(GrGLProgramBuilder::kFragment_Visibility, kVec4f_GrSLType,
61 uniName.c_str(), &name);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000062 fDomainName = name;
63 }
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000064
joshualitt5ae5fc52014-07-29 12:59:27 -070065 switch (textureDomain.mode()) {
66 case kIgnore_Mode: {
joshualitt30ba4362014-08-21 20:18:45 -070067 builder->codeAppendf("\t%s = ", outColor);
68 builder->appendTextureLookupAndModulate(inModulateColor, sampler,
joshualitt5ae5fc52014-07-29 12:59:27 -070069 inCoords.c_str());
joshualitt30ba4362014-08-21 20:18:45 -070070 builder->codeAppend(";\n");
joshualitt5ae5fc52014-07-29 12:59:27 -070071 break;
72 }
73 case kClamp_Mode: {
74 SkString clampedCoords;
75 clampedCoords.appendf("\tclamp(%s, %s.xy, %s.zw)",
76 inCoords.c_str(), fDomainName.c_str(), fDomainName.c_str());
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000077
joshualitt30ba4362014-08-21 20:18:45 -070078 builder->codeAppendf("\t%s = ", outColor);
79 builder->appendTextureLookupAndModulate(inModulateColor, sampler,
joshualitt5ae5fc52014-07-29 12:59:27 -070080 clampedCoords.c_str());
joshualitt30ba4362014-08-21 20:18:45 -070081 builder->codeAppend(";\n");
joshualitt5ae5fc52014-07-29 12:59:27 -070082 break;
83 }
84 case kDecal_Mode: {
85 // Add a block since we're going to declare variables.
joshualitt30ba4362014-08-21 20:18:45 -070086 GrGLShaderBuilder::ShaderBlock block(builder);
joshualitt5ae5fc52014-07-29 12:59:27 -070087
88 const char* domain = fDomainName.c_str();
joshualitt30ba4362014-08-21 20:18:45 -070089 if (kImagination_GrGLVendor == program->ctxInfo().vendor()) {
joshualitt5ae5fc52014-07-29 12:59:27 -070090 // On the NexusS and GalaxyNexus, the other path (with the 'any'
91 // call) causes the compilation error "Calls to any function that
92 // may require a gradient calculation inside a conditional block
93 // may return undefined results". This appears to be an issue with
94 // the 'any' call since even the simple "result=black; if (any())
95 // result=white;" code fails to compile.
joshualitt30ba4362014-08-21 20:18:45 -070096 builder->codeAppend("\tvec4 outside = vec4(0.0, 0.0, 0.0, 0.0);\n");
97 builder->codeAppend("\tvec4 inside = ");
98 builder->appendTextureLookupAndModulate(inModulateColor, sampler,
joshualitt5ae5fc52014-07-29 12:59:27 -070099 inCoords.c_str());
joshualitt30ba4362014-08-21 20:18:45 -0700100 builder->codeAppend(";\n");
101 builder->codeAppendf("\tfloat x = (%s).x;\n", inCoords.c_str());
102 builder->codeAppendf("\tfloat y = (%s).y;\n", inCoords.c_str());
joshualitt5ae5fc52014-07-29 12:59:27 -0700103
joshualitt30ba4362014-08-21 20:18:45 -0700104 builder->codeAppendf("\tx = abs(2.0*(x - %s.x)/(%s.z - %s.x) - 1.0);\n",
joshualittd9e183f2014-07-31 07:26:36 -0700105 domain, domain, domain);
joshualitt30ba4362014-08-21 20:18:45 -0700106 builder->codeAppendf("\ty = abs(2.0*(y - %s.y)/(%s.w - %s.y) - 1.0);\n",
joshualittd9e183f2014-07-31 07:26:36 -0700107 domain, domain, domain);
joshualitt30ba4362014-08-21 20:18:45 -0700108 builder->codeAppend("\tfloat blend = step(1.0, max(x, y));\n");
109 builder->codeAppendf("\t%s = mix(inside, outside, blend);\n", outColor);
joshualitt5ae5fc52014-07-29 12:59:27 -0700110 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700111 builder->codeAppend("\tbvec4 outside;\n");
112 builder->codeAppendf("\toutside.xy = lessThan(%s, %s.xy);\n", inCoords.c_str(),
joshualitt5ae5fc52014-07-29 12:59:27 -0700113 domain);
joshualitt30ba4362014-08-21 20:18:45 -0700114 builder->codeAppendf("\toutside.zw = greaterThan(%s, %s.zw);\n", inCoords.c_str(),
joshualitt5ae5fc52014-07-29 12:59:27 -0700115 domain);
joshualitt30ba4362014-08-21 20:18:45 -0700116 builder->codeAppendf("\t%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.0) : ",
joshualitt5ae5fc52014-07-29 12:59:27 -0700117 outColor);
joshualitt30ba4362014-08-21 20:18:45 -0700118 builder->appendTextureLookupAndModulate(inModulateColor, sampler,
joshualitt5ae5fc52014-07-29 12:59:27 -0700119 inCoords.c_str());
joshualitt30ba4362014-08-21 20:18:45 -0700120 builder->codeAppend(";\n");
joshualitt5ae5fc52014-07-29 12:59:27 -0700121 }
122 break;
123 }
124 case kRepeat_Mode: {
125 SkString clampedCoords;
126 clampedCoords.printf("\tmod(%s - %s.xy, %s.zw - %s.xy) + %s.xy",
127 inCoords.c_str(), fDomainName.c_str(), fDomainName.c_str(),
128 fDomainName.c_str(), fDomainName.c_str());
129
joshualitt30ba4362014-08-21 20:18:45 -0700130 builder->codeAppendf("\t%s = ", outColor);
131 builder->appendTextureLookupAndModulate(inModulateColor, sampler,
joshualitt5ae5fc52014-07-29 12:59:27 -0700132 clampedCoords.c_str());
joshualitt30ba4362014-08-21 20:18:45 -0700133 builder->codeAppend(";\n");
joshualitt5ae5fc52014-07-29 12:59:27 -0700134 break;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000135 }
136 }
137}
138
kkinnunen7510b222014-07-30 00:04:16 -0700139void GrTextureDomain::GLDomain::setData(const GrGLProgramDataManager& pdman,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000140 const GrTextureDomain& textureDomain,
141 GrSurfaceOrigin textureOrigin) {
142 SkASSERT(textureDomain.mode() == fMode);
143 if (kIgnore_Mode != textureDomain.mode()) {
144 GrGLfloat values[4] = {
145 SkScalarToFloat(textureDomain.domain().left()),
146 SkScalarToFloat(textureDomain.domain().top()),
147 SkScalarToFloat(textureDomain.domain().right()),
148 SkScalarToFloat(textureDomain.domain().bottom())
149 };
150 // vertical flip if necessary
151 if (kBottomLeft_GrSurfaceOrigin == textureOrigin) {
152 values[1] = 1.0f - values[1];
153 values[3] = 1.0f - values[3];
154 // The top and bottom were just flipped, so correct the ordering
155 // of elements so that values = (l, t, r, b).
156 SkTSwap(values[1], values[3]);
157 }
158 if (0 != memcmp(values, fPrevDomain, 4 * sizeof(GrGLfloat))) {
kkinnunen7510b222014-07-30 00:04:16 -0700159 pdman.set4fv(fDomainUni, 1, values);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000160 memcpy(fPrevDomain, values, 4 * sizeof(GrGLfloat));
161 }
162 }
163}
164
165
166//////////////////////////////////////////////////////////////////////////////
167
joshualittb0a8a372014-09-23 09:50:21 -0700168class GrGLTextureDomainEffect : public GrGLFragmentProcessor {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000169public:
joshualittb0a8a372014-09-23 09:50:21 -0700170 GrGLTextureDomainEffect(const GrBackendProcessorFactory&, const GrProcessor&);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000171
joshualitt30ba4362014-08-21 20:18:45 -0700172 virtual void emitCode(GrGLProgramBuilder*,
joshualittb0a8a372014-09-23 09:50:21 -0700173 const GrFragmentProcessor&,
174 const GrProcessorKey&,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000175 const char* outputColor,
176 const char* inputColor,
177 const TransformedCoordsArray&,
178 const TextureSamplerArray&) SK_OVERRIDE;
179
joshualittb0a8a372014-09-23 09:50:21 -0700180 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000181
joshualittb0a8a372014-09-23 09:50:21 -0700182 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000183
184private:
185 GrTextureDomain::GLDomain fGLDomain;
joshualittb0a8a372014-09-23 09:50:21 -0700186 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000187};
188
joshualittb0a8a372014-09-23 09:50:21 -0700189GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrBackendProcessorFactory& factory,
190 const GrProcessor&)
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000191 : INHERITED(factory) {
192}
193
joshualitt30ba4362014-08-21 20:18:45 -0700194void GrGLTextureDomainEffect::emitCode(GrGLProgramBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700195 const GrFragmentProcessor& fp,
196 const GrProcessorKey& key,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000197 const char* outputColor,
198 const char* inputColor,
199 const TransformedCoordsArray& coords,
200 const TextureSamplerArray& samplers) {
joshualittb0a8a372014-09-23 09:50:21 -0700201 const GrTextureDomainEffect& textureDomainEffect = fp.cast<GrTextureDomainEffect>();
joshualitt49586be2014-09-16 08:21:41 -0700202 const GrTextureDomain& domain = textureDomainEffect.textureDomain();
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000203
joshualitt30ba4362014-08-21 20:18:45 -0700204 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
205 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0);
206 fGLDomain.sampleTexture(fsBuilder, domain, outputColor, coords2D, samplers[0], inputColor);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000207}
208
kkinnunen7510b222014-07-30 00:04:16 -0700209void GrGLTextureDomainEffect::setData(const GrGLProgramDataManager& pdman,
joshualittb0a8a372014-09-23 09:50:21 -0700210 const GrProcessor& processor) {
211 const GrTextureDomainEffect& textureDomainEffect = processor.cast<GrTextureDomainEffect>();
joshualitt49586be2014-09-16 08:21:41 -0700212 const GrTextureDomain& domain = textureDomainEffect.textureDomain();
joshualittb0a8a372014-09-23 09:50:21 -0700213 fGLDomain.setData(pdman, domain, processor.texture(0)->origin());
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000214}
215
joshualittb0a8a372014-09-23 09:50:21 -0700216void GrGLTextureDomainEffect::GenKey(const GrProcessor& processor, const GrGLCaps&,
217 GrProcessorKeyBuilder* b) {
218 const GrTextureDomain& domain = processor.cast<GrTextureDomainEffect>().textureDomain();
bsalomon63e99f72014-07-21 08:03:14 -0700219 b->add32(GrTextureDomain::GLDomain::DomainKey(domain));
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000220}
221
222
223///////////////////////////////////////////////////////////////////////////////
224
joshualittb0a8a372014-09-23 09:50:21 -0700225GrFragmentProcessor* GrTextureDomainEffect::Create(GrTexture* texture,
226 const SkMatrix& matrix,
227 const SkRect& domain,
228 GrTextureDomain::Mode mode,
229 GrTextureParams::FilterMode filterMode,
230 GrCoordSet coordSet) {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000231 static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1};
232 if (GrTextureDomain::kIgnore_Mode == mode ||
233 (GrTextureDomain::kClamp_Mode == mode && domain.contains(kFullRect))) {
234 return GrSimpleTextureEffect::Create(texture, matrix, filterMode);
235 } else {
236
bsalomon55fad7a2014-07-08 07:34:20 -0700237 return SkNEW_ARGS(GrTextureDomainEffect, (texture,
238 matrix,
239 domain,
240 mode,
241 filterMode,
242 coordSet));
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000243 }
244}
245
246GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture,
247 const SkMatrix& matrix,
248 const SkRect& domain,
249 GrTextureDomain::Mode mode,
250 GrTextureParams::FilterMode filterMode,
251 GrCoordSet coordSet)
252 : GrSingleTextureEffect(texture, matrix, filterMode, coordSet)
253 , fTextureDomain(domain, mode) {
joshualitt5ae5fc52014-07-29 12:59:27 -0700254 SkASSERT(mode != GrTextureDomain::kRepeat_Mode ||
255 filterMode == GrTextureParams::kNone_FilterMode);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000256}
257
258GrTextureDomainEffect::~GrTextureDomainEffect() {
259
260}
261
joshualittb0a8a372014-09-23 09:50:21 -0700262const GrBackendFragmentProcessorFactory& GrTextureDomainEffect::getFactory() const {
263 return GrTBackendFragmentProcessorFactory<GrTextureDomainEffect>::getInstance();
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000264}
265
joshualittb0a8a372014-09-23 09:50:21 -0700266bool GrTextureDomainEffect::onIsEqual(const GrProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700267 const GrTextureDomainEffect& s = sBase.cast<GrTextureDomainEffect>();
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000268 return this->hasSameTextureParamsMatrixAndSourceCoords(s) &&
269 this->fTextureDomain == s.fTextureDomain;
270}
271
272void GrTextureDomainEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
273 if (GrTextureDomain::kDecal_Mode == fTextureDomain.mode()) { // TODO: helper
274 *validFlags = 0;
275 } else {
276 this->updateConstantColorComponentsForModulation(color, validFlags);
277 }
278}
279
280///////////////////////////////////////////////////////////////////////////////
281
joshualittb0a8a372014-09-23 09:50:21 -0700282GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTextureDomainEffect);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000283
joshualittb0a8a372014-09-23 09:50:21 -0700284GrFragmentProcessor* GrTextureDomainEffect::TestCreate(SkRandom* random,
285 GrContext*,
286 const GrDrawTargetCaps&,
287 GrTexture* textures[]) {
288 int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
289 GrProcessorUnitTest::kAlphaTextureIdx;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000290 SkRect domain;
291 domain.fLeft = random->nextUScalar1();
292 domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1);
293 domain.fTop = random->nextUScalar1();
294 domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1);
295 GrTextureDomain::Mode mode =
296 (GrTextureDomain::Mode) random->nextULessThan(GrTextureDomain::kModeCount);
joshualittb0a8a372014-09-23 09:50:21 -0700297 const SkMatrix& matrix = GrProcessorUnitTest::TestMatrix(random);
joshualitt5ae5fc52014-07-29 12:59:27 -0700298 bool bilerp = mode != GrTextureDomain::kRepeat_Mode ? random->nextBool() : false;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000299 GrCoordSet coords = random->nextBool() ? kLocal_GrCoordSet : kPosition_GrCoordSet;
300 return GrTextureDomainEffect::Create(textures[texIdx],
301 matrix,
302 domain,
303 mode,
304 bilerp ? GrTextureParams::kBilerp_FilterMode : GrTextureParams::kNone_FilterMode,
305 coords);
306}