blob: 31f51aad2224e83aa25728ebbd5d86d64b170f07 [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
8#include "GrTextureDomain.h"
egdaniel605dd0f2014-11-12 08:35:25 -08009#include "GrInvariantOutput.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000010#include "GrSimpleTextureEffect.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000011#include "SkFloatingPoint.h"
joshualitteb2a6762014-12-04 11:35:33 -080012#include "gl/GrGLProcessor.h"
13#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000014
15GrTextureDomain::GrTextureDomain(const SkRect& domain, Mode mode, int index)
16 : fIndex(index) {
17
18 static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1};
commit-bot@chromium.org26632632014-03-25 15:13:18 +000019 if (domain.contains(kFullRect) && kClamp_Mode == mode) {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000020 fMode = kIgnore_Mode;
21 } else {
22 fMode = mode;
23 }
24
25 if (fMode != kIgnore_Mode) {
26 // We don't currently handle domains that are empty or don't intersect the texture.
27 // It is OK if the domain rect is a line or point, but it should not be inverted. We do not
28 // handle rects that do not intersect the [0..1]x[0..1] rect.
29 SkASSERT(domain.fLeft <= domain.fRight);
30 SkASSERT(domain.fTop <= domain.fBottom);
senorblancod0d37ca2015-04-02 04:54:56 -070031 fDomain.fLeft = SkScalarPin(domain.fLeft, kFullRect.fLeft, kFullRect.fRight);
32 fDomain.fRight = SkScalarPin(domain.fRight, kFullRect.fLeft, kFullRect.fRight);
33 fDomain.fTop = SkScalarPin(domain.fTop, kFullRect.fTop, kFullRect.fBottom);
34 fDomain.fBottom = SkScalarPin(domain.fBottom, kFullRect.fTop, kFullRect.fBottom);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000035 SkASSERT(fDomain.fLeft <= fDomain.fRight);
36 SkASSERT(fDomain.fTop <= fDomain.fBottom);
37 }
38}
39
40//////////////////////////////////////////////////////////////////////////////
41
42void GrTextureDomain::GLDomain::sampleTexture(GrGLShaderBuilder* builder,
43 const GrTextureDomain& textureDomain,
44 const char* outColor,
45 const SkString& inCoords,
joshualittb0a8a372014-09-23 09:50:21 -070046 const GrGLProcessor::TextureSampler sampler,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000047 const char* inModulateColor) {
reed@google.comd7b1af62013-12-09 20:31:50 +000048 SkASSERT((Mode)-1 == fMode || textureDomain.mode() == fMode);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000049 SkDEBUGCODE(fMode = textureDomain.mode();)
50
joshualitt30ba4362014-08-21 20:18:45 -070051 GrGLProgramBuilder* program = builder->getProgramBuilder();
52
joshualitt5ae5fc52014-07-29 12:59:27 -070053 if (textureDomain.mode() != kIgnore_Mode && !fDomainUni.isValid()) {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000054 const char* name;
55 SkString uniName("TexDom");
56 if (textureDomain.fIndex >= 0) {
57 uniName.appendS32(textureDomain.fIndex);
58 }
bsalomon422f56f2014-12-09 10:18:12 -080059 fDomainUni = program->addUniform(GrGLProgramBuilder::kFragment_Visibility,
60 kVec4f_GrSLType, kDefault_GrSLPrecision,
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:
joshualitteb2a6762014-12-04 11:35:33 -0800170 GrGLTextureDomainEffect(const GrProcessor&);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000171
joshualitt15988992014-10-09 15:04:05 -0700172 virtual void emitCode(GrGLFPBuilder*,
joshualittb0a8a372014-09-23 09:50:21 -0700173 const GrFragmentProcessor&,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000174 const char* outputColor,
175 const char* inputColor,
176 const TransformedCoordsArray&,
mtklein36352bf2015-03-25 18:17:31 -0700177 const TextureSamplerArray&) override;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000178
mtklein36352bf2015-03-25 18:17:31 -0700179 void setData(const GrGLProgramDataManager&, const GrProcessor&) override;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000180
jvanverthcfc18862015-04-28 08:48:20 -0700181 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000182
183private:
184 GrTextureDomain::GLDomain fGLDomain;
joshualittb0a8a372014-09-23 09:50:21 -0700185 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000186};
187
joshualitteb2a6762014-12-04 11:35:33 -0800188GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrProcessor&) {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000189}
190
joshualitt15988992014-10-09 15:04:05 -0700191void GrGLTextureDomainEffect::emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700192 const GrFragmentProcessor& fp,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000193 const char* outputColor,
194 const char* inputColor,
195 const TransformedCoordsArray& coords,
196 const TextureSamplerArray& samplers) {
joshualittb0a8a372014-09-23 09:50:21 -0700197 const GrTextureDomainEffect& textureDomainEffect = fp.cast<GrTextureDomainEffect>();
joshualitt49586be2014-09-16 08:21:41 -0700198 const GrTextureDomain& domain = textureDomainEffect.textureDomain();
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000199
egdaniel29bee0f2015-04-29 11:54:42 -0700200 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700201 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0);
202 fGLDomain.sampleTexture(fsBuilder, domain, outputColor, coords2D, samplers[0], inputColor);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000203}
204
kkinnunen7510b222014-07-30 00:04:16 -0700205void GrGLTextureDomainEffect::setData(const GrGLProgramDataManager& pdman,
joshualittb0a8a372014-09-23 09:50:21 -0700206 const GrProcessor& processor) {
207 const GrTextureDomainEffect& textureDomainEffect = processor.cast<GrTextureDomainEffect>();
joshualitt49586be2014-09-16 08:21:41 -0700208 const GrTextureDomain& domain = textureDomainEffect.textureDomain();
joshualittb0a8a372014-09-23 09:50:21 -0700209 fGLDomain.setData(pdman, domain, processor.texture(0)->origin());
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000210}
211
jvanverthcfc18862015-04-28 08:48:20 -0700212void GrGLTextureDomainEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700213 GrProcessorKeyBuilder* b) {
214 const GrTextureDomain& domain = processor.cast<GrTextureDomainEffect>().textureDomain();
bsalomon63e99f72014-07-21 08:03:14 -0700215 b->add32(GrTextureDomain::GLDomain::DomainKey(domain));
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000216}
217
218
219///////////////////////////////////////////////////////////////////////////////
220
joshualittb0a8a372014-09-23 09:50:21 -0700221GrFragmentProcessor* GrTextureDomainEffect::Create(GrTexture* texture,
222 const SkMatrix& matrix,
223 const SkRect& domain,
224 GrTextureDomain::Mode mode,
225 GrTextureParams::FilterMode filterMode,
226 GrCoordSet coordSet) {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000227 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
bsalomon55fad7a2014-07-08 07:34:20 -0700233 return SkNEW_ARGS(GrTextureDomainEffect, (texture,
234 matrix,
235 domain,
236 mode,
237 filterMode,
238 coordSet));
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000239 }
240}
241
242GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture,
243 const SkMatrix& matrix,
244 const SkRect& domain,
245 GrTextureDomain::Mode mode,
246 GrTextureParams::FilterMode filterMode,
247 GrCoordSet coordSet)
248 : GrSingleTextureEffect(texture, matrix, filterMode, coordSet)
249 , fTextureDomain(domain, mode) {
joshualitt5ae5fc52014-07-29 12:59:27 -0700250 SkASSERT(mode != GrTextureDomain::kRepeat_Mode ||
251 filterMode == GrTextureParams::kNone_FilterMode);
joshualitteb2a6762014-12-04 11:35:33 -0800252 this->initClassID<GrTextureDomainEffect>();
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000253}
254
255GrTextureDomainEffect::~GrTextureDomainEffect() {
256
257}
258
jvanverthcfc18862015-04-28 08:48:20 -0700259void GrTextureDomainEffect::getGLProcessorKey(const GrGLSLCaps& caps,
joshualitteb2a6762014-12-04 11:35:33 -0800260 GrProcessorKeyBuilder* b) const {
261 GrGLTextureDomainEffect::GenKey(*this, caps, b);
262}
263
264GrGLFragmentProcessor* GrTextureDomainEffect::createGLInstance() const {
265 return SkNEW_ARGS(GrGLTextureDomainEffect, (*this));
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000266}
267
bsalomon0e08fc12014-10-15 08:19:04 -0700268bool GrTextureDomainEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700269 const GrTextureDomainEffect& s = sBase.cast<GrTextureDomainEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700270 return this->fTextureDomain == s.fTextureDomain;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000271}
272
egdaniel605dd0f2014-11-12 08:35:25 -0800273void GrTextureDomainEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000274 if (GrTextureDomain::kDecal_Mode == fTextureDomain.mode()) { // TODO: helper
egdanielf8449ba2014-11-25 10:24:56 -0800275 if (GrPixelConfigIsAlphaOnly(this->texture(0)->config())) {
joshualitt56995b52014-12-11 15:44:02 -0800276 inout->mulByUnknownSingleComponent();
egdanielf8449ba2014-11-25 10:24:56 -0800277 } else {
joshualitt56995b52014-12-11 15:44:02 -0800278 inout->mulByUnknownFourComponents();
egdanielf8449ba2014-11-25 10:24:56 -0800279 }
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000280 } else {
egdaniel1a8ecdf2014-10-03 06:24:12 -0700281 this->updateInvariantOutputForModulation(inout);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000282 }
283}
284
285///////////////////////////////////////////////////////////////////////////////
286
joshualittb0a8a372014-09-23 09:50:21 -0700287GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTextureDomainEffect);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000288
joshualitt0067ff52015-07-08 14:26:19 -0700289GrFragmentProcessor* GrTextureDomainEffect::TestCreate(GrProcessorTestData* d) {
290 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
291 GrProcessorUnitTest::kAlphaTextureIdx;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000292 SkRect domain;
joshualitt0067ff52015-07-08 14:26:19 -0700293 domain.fLeft = d->fRandom->nextUScalar1();
294 domain.fRight = d->fRandom->nextRangeScalar(domain.fLeft, SK_Scalar1);
295 domain.fTop = d->fRandom->nextUScalar1();
296 domain.fBottom = d->fRandom->nextRangeScalar(domain.fTop, SK_Scalar1);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000297 GrTextureDomain::Mode mode =
joshualitt0067ff52015-07-08 14:26:19 -0700298 (GrTextureDomain::Mode) d->fRandom->nextULessThan(GrTextureDomain::kModeCount);
299 const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom);
300 bool bilerp = mode != GrTextureDomain::kRepeat_Mode ? d->fRandom->nextBool() : false;
301 GrCoordSet coords = d->fRandom->nextBool() ? kLocal_GrCoordSet : kDevice_GrCoordSet;
302 return GrTextureDomainEffect::Create(d->fTextures[texIdx],
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000303 matrix,
304 domain,
305 mode,
306 bilerp ? GrTextureParams::kBilerp_FilterMode : GrTextureParams::kNone_FilterMode,
307 coords);
308}