blob: 760fd575072c86ddb15e8fd67ae75eed1a7172ba [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"
egdanielf5294392015-10-21 07:14:17 -070012#include "gl/GrGLContext.h"
wangyix6af0c932015-07-22 10:21:17 -070013#include "gl/GrGLFragmentProcessor.h"
joshualitteb2a6762014-12-04 11:35:33 -080014#include "gl/builders/GrGLProgramBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070015#include "glsl/GrGLSLProgramDataManager.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000016
17GrTextureDomain::GrTextureDomain(const SkRect& domain, Mode mode, int index)
18 : fIndex(index) {
19
20 static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1};
commit-bot@chromium.org26632632014-03-25 15:13:18 +000021 if (domain.contains(kFullRect) && kClamp_Mode == mode) {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000022 fMode = kIgnore_Mode;
23 } else {
24 fMode = mode;
25 }
26
27 if (fMode != kIgnore_Mode) {
28 // We don't currently handle domains that are empty or don't intersect the texture.
29 // It is OK if the domain rect is a line or point, but it should not be inverted. We do not
30 // handle rects that do not intersect the [0..1]x[0..1] rect.
31 SkASSERT(domain.fLeft <= domain.fRight);
32 SkASSERT(domain.fTop <= domain.fBottom);
senorblancod0d37ca2015-04-02 04:54:56 -070033 fDomain.fLeft = SkScalarPin(domain.fLeft, kFullRect.fLeft, kFullRect.fRight);
34 fDomain.fRight = SkScalarPin(domain.fRight, kFullRect.fLeft, kFullRect.fRight);
35 fDomain.fTop = SkScalarPin(domain.fTop, kFullRect.fTop, kFullRect.fBottom);
36 fDomain.fBottom = SkScalarPin(domain.fBottom, kFullRect.fTop, kFullRect.fBottom);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000037 SkASSERT(fDomain.fLeft <= fDomain.fRight);
38 SkASSERT(fDomain.fTop <= fDomain.fBottom);
39 }
40}
41
42//////////////////////////////////////////////////////////////////////////////
43
44void GrTextureDomain::GLDomain::sampleTexture(GrGLShaderBuilder* builder,
45 const GrTextureDomain& textureDomain,
46 const char* outColor,
47 const SkString& inCoords,
joshualittb0a8a372014-09-23 09:50:21 -070048 const GrGLProcessor::TextureSampler sampler,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000049 const char* inModulateColor) {
reed@google.comd7b1af62013-12-09 20:31:50 +000050 SkASSERT((Mode)-1 == fMode || textureDomain.mode() == fMode);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000051 SkDEBUGCODE(fMode = textureDomain.mode();)
52
joshualitt30ba4362014-08-21 20:18:45 -070053 GrGLProgramBuilder* program = builder->getProgramBuilder();
54
joshualitt5ae5fc52014-07-29 12:59:27 -070055 if (textureDomain.mode() != kIgnore_Mode && !fDomainUni.isValid()) {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000056 const char* name;
57 SkString uniName("TexDom");
58 if (textureDomain.fIndex >= 0) {
59 uniName.appendS32(textureDomain.fIndex);
60 }
bsalomon422f56f2014-12-09 10:18:12 -080061 fDomainUni = program->addUniform(GrGLProgramBuilder::kFragment_Visibility,
62 kVec4f_GrSLType, kDefault_GrSLPrecision,
63 uniName.c_str(), &name);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000064 fDomainName = name;
65 }
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000066
joshualitt5ae5fc52014-07-29 12:59:27 -070067 switch (textureDomain.mode()) {
68 case kIgnore_Mode: {
jvanverth3fc65602015-07-22 08:41:51 -070069 builder->codeAppendf("%s = ", outColor);
joshualitt30ba4362014-08-21 20:18:45 -070070 builder->appendTextureLookupAndModulate(inModulateColor, sampler,
joshualitt5ae5fc52014-07-29 12:59:27 -070071 inCoords.c_str());
jvanverth3fc65602015-07-22 08:41:51 -070072 builder->codeAppend(";");
joshualitt5ae5fc52014-07-29 12:59:27 -070073 break;
74 }
75 case kClamp_Mode: {
76 SkString clampedCoords;
jvanverth3fc65602015-07-22 08:41:51 -070077 clampedCoords.appendf("clamp(%s, %s.xy, %s.zw)",
joshualitt5ae5fc52014-07-29 12:59:27 -070078 inCoords.c_str(), fDomainName.c_str(), fDomainName.c_str());
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000079
jvanverth3fc65602015-07-22 08:41:51 -070080 builder->codeAppendf("%s = ", outColor);
joshualitt30ba4362014-08-21 20:18:45 -070081 builder->appendTextureLookupAndModulate(inModulateColor, sampler,
joshualitt5ae5fc52014-07-29 12:59:27 -070082 clampedCoords.c_str());
jvanverth3fc65602015-07-22 08:41:51 -070083 builder->codeAppend(";");
joshualitt5ae5fc52014-07-29 12:59:27 -070084 break;
85 }
86 case kDecal_Mode: {
87 // Add a block since we're going to declare variables.
joshualitt30ba4362014-08-21 20:18:45 -070088 GrGLShaderBuilder::ShaderBlock block(builder);
joshualitt5ae5fc52014-07-29 12:59:27 -070089
90 const char* domain = fDomainName.c_str();
egdaniel472d44e2015-10-22 08:20:00 -070091 if (!program->glslCaps()->canUseAnyFunctionInShader()) {
joshualitt5ae5fc52014-07-29 12:59:27 -070092 // On the NexusS and GalaxyNexus, the other path (with the 'any'
93 // call) causes the compilation error "Calls to any function that
94 // may require a gradient calculation inside a conditional block
95 // may return undefined results". This appears to be an issue with
96 // the 'any' call since even the simple "result=black; if (any())
97 // result=white;" code fails to compile.
jvanverth3fc65602015-07-22 08:41:51 -070098 builder->codeAppend("vec4 outside = vec4(0.0, 0.0, 0.0, 0.0);");
99 builder->codeAppend("vec4 inside = ");
joshualitt30ba4362014-08-21 20:18:45 -0700100 builder->appendTextureLookupAndModulate(inModulateColor, sampler,
joshualitt5ae5fc52014-07-29 12:59:27 -0700101 inCoords.c_str());
jvanverth3fc65602015-07-22 08:41:51 -0700102 builder->codeAppend(";");
103
egdaniel0d3f0612015-10-21 10:45:48 -0700104 builder->codeAppend(GrGLSLShaderVar::PrecisionString(program->glslCaps(),
105 kHigh_GrSLPrecision));
jvanverth3fc65602015-07-22 08:41:51 -0700106 builder->codeAppendf("float x = (%s).x;", inCoords.c_str());
egdaniel0d3f0612015-10-21 10:45:48 -0700107 builder->codeAppend(GrGLSLShaderVar::PrecisionString(program->glslCaps(),
108 kHigh_GrSLPrecision));
jvanverth3fc65602015-07-22 08:41:51 -0700109 builder->codeAppendf("float y = (%s).y;", inCoords.c_str());
joshualitt5ae5fc52014-07-29 12:59:27 -0700110
jvanverth3fc65602015-07-22 08:41:51 -0700111 builder->codeAppendf("x = abs(2.0*(x - %s.x)/(%s.z - %s.x) - 1.0);",
112 domain, domain, domain);
113 builder->codeAppendf("y = abs(2.0*(y - %s.y)/(%s.w - %s.y) - 1.0);",
114 domain, domain, domain);
115 builder->codeAppend("float blend = step(1.0, max(x, y));");
116 builder->codeAppendf("%s = mix(inside, outside, blend);", outColor);
joshualitt5ae5fc52014-07-29 12:59:27 -0700117 } else {
jvanverth3fc65602015-07-22 08:41:51 -0700118 builder->codeAppend("bvec4 outside;\n");
119 builder->codeAppendf("outside.xy = lessThan(%s, %s.xy);", inCoords.c_str(),
joshualitt5ae5fc52014-07-29 12:59:27 -0700120 domain);
jvanverth3fc65602015-07-22 08:41:51 -0700121 builder->codeAppendf("outside.zw = greaterThan(%s, %s.zw);", inCoords.c_str(),
joshualitt5ae5fc52014-07-29 12:59:27 -0700122 domain);
jvanverth3fc65602015-07-22 08:41:51 -0700123 builder->codeAppendf("%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.0) : ",
joshualitt5ae5fc52014-07-29 12:59:27 -0700124 outColor);
joshualitt30ba4362014-08-21 20:18:45 -0700125 builder->appendTextureLookupAndModulate(inModulateColor, sampler,
joshualitt5ae5fc52014-07-29 12:59:27 -0700126 inCoords.c_str());
jvanverth3fc65602015-07-22 08:41:51 -0700127 builder->codeAppend(";");
joshualitt5ae5fc52014-07-29 12:59:27 -0700128 }
129 break;
130 }
131 case kRepeat_Mode: {
132 SkString clampedCoords;
jvanverth3fc65602015-07-22 08:41:51 -0700133 clampedCoords.printf("mod(%s - %s.xy, %s.zw - %s.xy) + %s.xy",
joshualitt5ae5fc52014-07-29 12:59:27 -0700134 inCoords.c_str(), fDomainName.c_str(), fDomainName.c_str(),
135 fDomainName.c_str(), fDomainName.c_str());
136
jvanverth3fc65602015-07-22 08:41:51 -0700137 builder->codeAppendf("%s = ", outColor);
joshualitt30ba4362014-08-21 20:18:45 -0700138 builder->appendTextureLookupAndModulate(inModulateColor, sampler,
joshualitt5ae5fc52014-07-29 12:59:27 -0700139 clampedCoords.c_str());
jvanverth3fc65602015-07-22 08:41:51 -0700140 builder->codeAppend(";");
joshualitt5ae5fc52014-07-29 12:59:27 -0700141 break;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000142 }
143 }
144}
145
egdaniel018fb622015-10-28 07:26:40 -0700146void GrTextureDomain::GLDomain::setData(const GrGLSLProgramDataManager& pdman,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000147 const GrTextureDomain& textureDomain,
148 GrSurfaceOrigin textureOrigin) {
149 SkASSERT(textureDomain.mode() == fMode);
150 if (kIgnore_Mode != textureDomain.mode()) {
egdaniel018fb622015-10-28 07:26:40 -0700151 float values[kPrevDomainCount] = {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000152 SkScalarToFloat(textureDomain.domain().left()),
153 SkScalarToFloat(textureDomain.domain().top()),
154 SkScalarToFloat(textureDomain.domain().right()),
155 SkScalarToFloat(textureDomain.domain().bottom())
156 };
157 // vertical flip if necessary
158 if (kBottomLeft_GrSurfaceOrigin == textureOrigin) {
159 values[1] = 1.0f - values[1];
160 values[3] = 1.0f - values[3];
161 // The top and bottom were just flipped, so correct the ordering
162 // of elements so that values = (l, t, r, b).
163 SkTSwap(values[1], values[3]);
164 }
egdaniel018fb622015-10-28 07:26:40 -0700165 if (0 != memcmp(values, fPrevDomain, kPrevDomainCount * sizeof(float))) {
kkinnunen7510b222014-07-30 00:04:16 -0700166 pdman.set4fv(fDomainUni, 1, values);
egdaniel018fb622015-10-28 07:26:40 -0700167 memcpy(fPrevDomain, values, kPrevDomainCount * sizeof(float));
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000168 }
169 }
170}
171
172
173//////////////////////////////////////////////////////////////////////////////
174
joshualittb0a8a372014-09-23 09:50:21 -0700175class GrGLTextureDomainEffect : public GrGLFragmentProcessor {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000176public:
joshualitteb2a6762014-12-04 11:35:33 -0800177 GrGLTextureDomainEffect(const GrProcessor&);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000178
wangyix7c157a92015-07-22 15:08:53 -0700179 virtual void emitCode(EmitArgs&) 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
wangyixb1daa862015-08-18 11:29:31 -0700183protected:
egdaniel018fb622015-10-28 07:26:40 -0700184 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
wangyixb1daa862015-08-18 11:29:31 -0700185
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000186private:
187 GrTextureDomain::GLDomain fGLDomain;
joshualittb0a8a372014-09-23 09:50:21 -0700188 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000189};
190
joshualitteb2a6762014-12-04 11:35:33 -0800191GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrProcessor&) {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000192}
193
wangyix7c157a92015-07-22 15:08:53 -0700194void GrGLTextureDomainEffect::emitCode(EmitArgs& args) {
195 const GrTextureDomainEffect& textureDomainEffect = args.fFp.cast<GrTextureDomainEffect>();
joshualitt49586be2014-09-16 08:21:41 -0700196 const GrTextureDomain& domain = textureDomainEffect.textureDomain();
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000197
wangyix7c157a92015-07-22 15:08:53 -0700198 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder();
199 SkString coords2D = fsBuilder->ensureFSCoords2D(args.fCoords, 0);
200 fGLDomain.sampleTexture(fsBuilder, domain, args.fOutputColor, coords2D, args.fSamplers[0],
201 args.fInputColor);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000202}
203
egdaniel018fb622015-10-28 07:26:40 -0700204void GrGLTextureDomainEffect::onSetData(const GrGLSLProgramDataManager& pdman,
205 const GrProcessor& processor) {
joshualittb0a8a372014-09-23 09:50:21 -0700206 const GrTextureDomainEffect& textureDomainEffect = processor.cast<GrTextureDomainEffect>();
joshualitt49586be2014-09-16 08:21:41 -0700207 const GrTextureDomain& domain = textureDomainEffect.textureDomain();
joshualittb0a8a372014-09-23 09:50:21 -0700208 fGLDomain.setData(pdman, domain, processor.texture(0)->origin());
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000209}
210
jvanverthcfc18862015-04-28 08:48:20 -0700211void GrGLTextureDomainEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700212 GrProcessorKeyBuilder* b) {
213 const GrTextureDomain& domain = processor.cast<GrTextureDomainEffect>().textureDomain();
bsalomon63e99f72014-07-21 08:03:14 -0700214 b->add32(GrTextureDomain::GLDomain::DomainKey(domain));
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000215}
216
217
218///////////////////////////////////////////////////////////////////////////////
219
bsalomon0ba8c242015-10-07 09:20:28 -0700220const GrFragmentProcessor* GrTextureDomainEffect::Create(GrTexture* texture,
221 const SkMatrix& matrix,
222 const SkRect& domain,
223 GrTextureDomain::Mode mode,
224 GrTextureParams::FilterMode filterMode,
225 GrCoordSet coordSet) {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000226 static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1};
227 if (GrTextureDomain::kIgnore_Mode == mode ||
228 (GrTextureDomain::kClamp_Mode == mode && domain.contains(kFullRect))) {
bsalomon4a339522015-10-06 08:40:50 -0700229 return GrSimpleTextureEffect::Create(texture, matrix, filterMode);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000230 } else {
bsalomon4a339522015-10-06 08:40:50 -0700231 return new GrTextureDomainEffect(texture, matrix, domain, mode, filterMode, coordSet);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000232 }
233}
234
bsalomon4a339522015-10-06 08:40:50 -0700235GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000236 const SkMatrix& matrix,
237 const SkRect& domain,
238 GrTextureDomain::Mode mode,
239 GrTextureParams::FilterMode filterMode,
240 GrCoordSet coordSet)
bsalomon4a339522015-10-06 08:40:50 -0700241 : GrSingleTextureEffect(texture, matrix, filterMode, coordSet)
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000242 , fTextureDomain(domain, mode) {
joshualitt5ae5fc52014-07-29 12:59:27 -0700243 SkASSERT(mode != GrTextureDomain::kRepeat_Mode ||
244 filterMode == GrTextureParams::kNone_FilterMode);
joshualitteb2a6762014-12-04 11:35:33 -0800245 this->initClassID<GrTextureDomainEffect>();
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000246}
247
bsalomon0ba8c242015-10-07 09:20:28 -0700248GrTextureDomainEffect::~GrTextureDomainEffect() {}
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000249
wangyix4b3050b2015-08-04 07:59:37 -0700250void GrTextureDomainEffect::onGetGLProcessorKey(const GrGLSLCaps& caps,
bsalomon0ba8c242015-10-07 09:20:28 -0700251 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800252 GrGLTextureDomainEffect::GenKey(*this, caps, b);
253}
254
wangyixb1daa862015-08-18 11:29:31 -0700255GrGLFragmentProcessor* GrTextureDomainEffect::onCreateGLInstance() const {
halcanary385fe4d2015-08-26 13:07:48 -0700256 return new GrGLTextureDomainEffect(*this);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000257}
258
bsalomon0e08fc12014-10-15 08:19:04 -0700259bool GrTextureDomainEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700260 const GrTextureDomainEffect& s = sBase.cast<GrTextureDomainEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700261 return this->fTextureDomain == s.fTextureDomain;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000262}
263
egdaniel605dd0f2014-11-12 08:35:25 -0800264void GrTextureDomainEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000265 if (GrTextureDomain::kDecal_Mode == fTextureDomain.mode()) { // TODO: helper
egdanielf8449ba2014-11-25 10:24:56 -0800266 if (GrPixelConfigIsAlphaOnly(this->texture(0)->config())) {
joshualitt56995b52014-12-11 15:44:02 -0800267 inout->mulByUnknownSingleComponent();
egdanielf8449ba2014-11-25 10:24:56 -0800268 } else {
joshualitt56995b52014-12-11 15:44:02 -0800269 inout->mulByUnknownFourComponents();
egdanielf8449ba2014-11-25 10:24:56 -0800270 }
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000271 } else {
egdaniel1a8ecdf2014-10-03 06:24:12 -0700272 this->updateInvariantOutputForModulation(inout);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000273 }
274}
275
276///////////////////////////////////////////////////////////////////////////////
277
joshualittb0a8a372014-09-23 09:50:21 -0700278GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTextureDomainEffect);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000279
bsalomonc21b09e2015-08-28 18:46:56 -0700280const GrFragmentProcessor* GrTextureDomainEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700281 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
282 GrProcessorUnitTest::kAlphaTextureIdx;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000283 SkRect domain;
joshualitt0067ff52015-07-08 14:26:19 -0700284 domain.fLeft = d->fRandom->nextUScalar1();
285 domain.fRight = d->fRandom->nextRangeScalar(domain.fLeft, SK_Scalar1);
286 domain.fTop = d->fRandom->nextUScalar1();
287 domain.fBottom = d->fRandom->nextRangeScalar(domain.fTop, SK_Scalar1);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000288 GrTextureDomain::Mode mode =
joshualitt0067ff52015-07-08 14:26:19 -0700289 (GrTextureDomain::Mode) d->fRandom->nextULessThan(GrTextureDomain::kModeCount);
290 const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom);
291 bool bilerp = mode != GrTextureDomain::kRepeat_Mode ? d->fRandom->nextBool() : false;
292 GrCoordSet coords = d->fRandom->nextBool() ? kLocal_GrCoordSet : kDevice_GrCoordSet;
bsalomon0ba8c242015-10-07 09:20:28 -0700293 return GrTextureDomainEffect::Create(
294 d->fTextures[texIdx],
295 matrix,
296 domain,
297 mode,
298 bilerp ? GrTextureParams::kBilerp_FilterMode : GrTextureParams::kNone_FilterMode,
299 coords);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000300}