blob: c6a9a22a946183632bec88249c9208625fe1d12e [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);
31 fDomain.fLeft = SkMaxScalar(domain.fLeft, kFullRect.fLeft);
32 fDomain.fRight = SkMinScalar(domain.fRight, kFullRect.fRight);
33 fDomain.fTop = SkMaxScalar(domain.fTop, kFullRect.fTop);
34 fDomain.fBottom = SkMinScalar(domain.fBottom, kFullRect.fBottom);
35 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 }
joshualitt30ba4362014-08-21 20:18:45 -070059 fDomainUni = program->addUniform(GrGLProgramBuilder::kFragment_Visibility, kVec4f_GrSLType,
60 uniName.c_str(), &name);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000061 fDomainName = name;
62 }
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000063
joshualitt5ae5fc52014-07-29 12:59:27 -070064 switch (textureDomain.mode()) {
65 case kIgnore_Mode: {
joshualitt30ba4362014-08-21 20:18:45 -070066 builder->codeAppendf("\t%s = ", outColor);
67 builder->appendTextureLookupAndModulate(inModulateColor, sampler,
joshualitt5ae5fc52014-07-29 12:59:27 -070068 inCoords.c_str());
joshualitt30ba4362014-08-21 20:18:45 -070069 builder->codeAppend(";\n");
joshualitt5ae5fc52014-07-29 12:59:27 -070070 break;
71 }
72 case kClamp_Mode: {
73 SkString clampedCoords;
74 clampedCoords.appendf("\tclamp(%s, %s.xy, %s.zw)",
75 inCoords.c_str(), fDomainName.c_str(), fDomainName.c_str());
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000076
joshualitt30ba4362014-08-21 20:18:45 -070077 builder->codeAppendf("\t%s = ", outColor);
78 builder->appendTextureLookupAndModulate(inModulateColor, sampler,
joshualitt5ae5fc52014-07-29 12:59:27 -070079 clampedCoords.c_str());
joshualitt30ba4362014-08-21 20:18:45 -070080 builder->codeAppend(";\n");
joshualitt5ae5fc52014-07-29 12:59:27 -070081 break;
82 }
83 case kDecal_Mode: {
84 // Add a block since we're going to declare variables.
joshualitt30ba4362014-08-21 20:18:45 -070085 GrGLShaderBuilder::ShaderBlock block(builder);
joshualitt5ae5fc52014-07-29 12:59:27 -070086
87 const char* domain = fDomainName.c_str();
joshualitt30ba4362014-08-21 20:18:45 -070088 if (kImagination_GrGLVendor == program->ctxInfo().vendor()) {
joshualitt5ae5fc52014-07-29 12:59:27 -070089 // On the NexusS and GalaxyNexus, the other path (with the 'any'
90 // call) causes the compilation error "Calls to any function that
91 // may require a gradient calculation inside a conditional block
92 // may return undefined results". This appears to be an issue with
93 // the 'any' call since even the simple "result=black; if (any())
94 // result=white;" code fails to compile.
joshualitt30ba4362014-08-21 20:18:45 -070095 builder->codeAppend("\tvec4 outside = vec4(0.0, 0.0, 0.0, 0.0);\n");
96 builder->codeAppend("\tvec4 inside = ");
97 builder->appendTextureLookupAndModulate(inModulateColor, sampler,
joshualitt5ae5fc52014-07-29 12:59:27 -070098 inCoords.c_str());
joshualitt30ba4362014-08-21 20:18:45 -070099 builder->codeAppend(";\n");
100 builder->codeAppendf("\tfloat x = (%s).x;\n", inCoords.c_str());
101 builder->codeAppendf("\tfloat y = (%s).y;\n", inCoords.c_str());
joshualitt5ae5fc52014-07-29 12:59:27 -0700102
joshualitt30ba4362014-08-21 20:18:45 -0700103 builder->codeAppendf("\tx = abs(2.0*(x - %s.x)/(%s.z - %s.x) - 1.0);\n",
joshualittd9e183f2014-07-31 07:26:36 -0700104 domain, domain, domain);
joshualitt30ba4362014-08-21 20:18:45 -0700105 builder->codeAppendf("\ty = abs(2.0*(y - %s.y)/(%s.w - %s.y) - 1.0);\n",
joshualittd9e183f2014-07-31 07:26:36 -0700106 domain, domain, domain);
joshualitt30ba4362014-08-21 20:18:45 -0700107 builder->codeAppend("\tfloat blend = step(1.0, max(x, y));\n");
108 builder->codeAppendf("\t%s = mix(inside, outside, blend);\n", outColor);
joshualitt5ae5fc52014-07-29 12:59:27 -0700109 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700110 builder->codeAppend("\tbvec4 outside;\n");
111 builder->codeAppendf("\toutside.xy = lessThan(%s, %s.xy);\n", inCoords.c_str(),
joshualitt5ae5fc52014-07-29 12:59:27 -0700112 domain);
joshualitt30ba4362014-08-21 20:18:45 -0700113 builder->codeAppendf("\toutside.zw = greaterThan(%s, %s.zw);\n", inCoords.c_str(),
joshualitt5ae5fc52014-07-29 12:59:27 -0700114 domain);
joshualitt30ba4362014-08-21 20:18:45 -0700115 builder->codeAppendf("\t%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.0) : ",
joshualitt5ae5fc52014-07-29 12:59:27 -0700116 outColor);
joshualitt30ba4362014-08-21 20:18:45 -0700117 builder->appendTextureLookupAndModulate(inModulateColor, sampler,
joshualitt5ae5fc52014-07-29 12:59:27 -0700118 inCoords.c_str());
joshualitt30ba4362014-08-21 20:18:45 -0700119 builder->codeAppend(";\n");
joshualitt5ae5fc52014-07-29 12:59:27 -0700120 }
121 break;
122 }
123 case kRepeat_Mode: {
124 SkString clampedCoords;
125 clampedCoords.printf("\tmod(%s - %s.xy, %s.zw - %s.xy) + %s.xy",
126 inCoords.c_str(), fDomainName.c_str(), fDomainName.c_str(),
127 fDomainName.c_str(), fDomainName.c_str());
128
joshualitt30ba4362014-08-21 20:18:45 -0700129 builder->codeAppendf("\t%s = ", outColor);
130 builder->appendTextureLookupAndModulate(inModulateColor, sampler,
joshualitt5ae5fc52014-07-29 12:59:27 -0700131 clampedCoords.c_str());
joshualitt30ba4362014-08-21 20:18:45 -0700132 builder->codeAppend(";\n");
joshualitt5ae5fc52014-07-29 12:59:27 -0700133 break;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000134 }
135 }
136}
137
kkinnunen7510b222014-07-30 00:04:16 -0700138void GrTextureDomain::GLDomain::setData(const GrGLProgramDataManager& pdman,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000139 const GrTextureDomain& textureDomain,
140 GrSurfaceOrigin textureOrigin) {
141 SkASSERT(textureDomain.mode() == fMode);
142 if (kIgnore_Mode != textureDomain.mode()) {
143 GrGLfloat values[4] = {
144 SkScalarToFloat(textureDomain.domain().left()),
145 SkScalarToFloat(textureDomain.domain().top()),
146 SkScalarToFloat(textureDomain.domain().right()),
147 SkScalarToFloat(textureDomain.domain().bottom())
148 };
149 // vertical flip if necessary
150 if (kBottomLeft_GrSurfaceOrigin == textureOrigin) {
151 values[1] = 1.0f - values[1];
152 values[3] = 1.0f - values[3];
153 // The top and bottom were just flipped, so correct the ordering
154 // of elements so that values = (l, t, r, b).
155 SkTSwap(values[1], values[3]);
156 }
157 if (0 != memcmp(values, fPrevDomain, 4 * sizeof(GrGLfloat))) {
kkinnunen7510b222014-07-30 00:04:16 -0700158 pdman.set4fv(fDomainUni, 1, values);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000159 memcpy(fPrevDomain, values, 4 * sizeof(GrGLfloat));
160 }
161 }
162}
163
164
165//////////////////////////////////////////////////////////////////////////////
166
joshualittb0a8a372014-09-23 09:50:21 -0700167class GrGLTextureDomainEffect : public GrGLFragmentProcessor {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000168public:
joshualitteb2a6762014-12-04 11:35:33 -0800169 GrGLTextureDomainEffect(const GrProcessor&);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000170
joshualitt15988992014-10-09 15:04:05 -0700171 virtual void emitCode(GrGLFPBuilder*,
joshualittb0a8a372014-09-23 09:50:21 -0700172 const GrFragmentProcessor&,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000173 const char* outputColor,
174 const char* inputColor,
175 const TransformedCoordsArray&,
176 const TextureSamplerArray&) SK_OVERRIDE;
177
joshualittb0a8a372014-09-23 09:50:21 -0700178 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000179
joshualittb0a8a372014-09-23 09:50:21 -0700180 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000181
182private:
183 GrTextureDomain::GLDomain fGLDomain;
joshualittb0a8a372014-09-23 09:50:21 -0700184 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000185};
186
joshualitteb2a6762014-12-04 11:35:33 -0800187GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrProcessor&) {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000188}
189
joshualitt15988992014-10-09 15:04:05 -0700190void GrGLTextureDomainEffect::emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700191 const GrFragmentProcessor& fp,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000192 const char* outputColor,
193 const char* inputColor,
194 const TransformedCoordsArray& coords,
195 const TextureSamplerArray& samplers) {
joshualittb0a8a372014-09-23 09:50:21 -0700196 const GrTextureDomainEffect& textureDomainEffect = fp.cast<GrTextureDomainEffect>();
joshualitt49586be2014-09-16 08:21:41 -0700197 const GrTextureDomain& domain = textureDomainEffect.textureDomain();
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000198
joshualitt15988992014-10-09 15:04:05 -0700199 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700200 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0);
201 fGLDomain.sampleTexture(fsBuilder, domain, outputColor, coords2D, samplers[0], inputColor);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000202}
203
kkinnunen7510b222014-07-30 00:04:16 -0700204void GrGLTextureDomainEffect::setData(const GrGLProgramDataManager& pdman,
joshualittb0a8a372014-09-23 09:50:21 -0700205 const GrProcessor& processor) {
206 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
joshualittb0a8a372014-09-23 09:50:21 -0700211void GrGLTextureDomainEffect::GenKey(const GrProcessor& processor, const GrGLCaps&,
212 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
joshualittb0a8a372014-09-23 09:50:21 -0700220GrFragmentProcessor* 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))) {
229 return GrSimpleTextureEffect::Create(texture, matrix, filterMode);
230 } else {
231
bsalomon55fad7a2014-07-08 07:34:20 -0700232 return SkNEW_ARGS(GrTextureDomainEffect, (texture,
233 matrix,
234 domain,
235 mode,
236 filterMode,
237 coordSet));
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000238 }
239}
240
241GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture,
242 const SkMatrix& matrix,
243 const SkRect& domain,
244 GrTextureDomain::Mode mode,
245 GrTextureParams::FilterMode filterMode,
246 GrCoordSet coordSet)
247 : GrSingleTextureEffect(texture, matrix, filterMode, coordSet)
248 , fTextureDomain(domain, mode) {
joshualitt5ae5fc52014-07-29 12:59:27 -0700249 SkASSERT(mode != GrTextureDomain::kRepeat_Mode ||
250 filterMode == GrTextureParams::kNone_FilterMode);
joshualitteb2a6762014-12-04 11:35:33 -0800251 this->initClassID<GrTextureDomainEffect>();
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000252}
253
254GrTextureDomainEffect::~GrTextureDomainEffect() {
255
256}
257
joshualitteb2a6762014-12-04 11:35:33 -0800258void GrTextureDomainEffect::getGLProcessorKey(const GrGLCaps& caps,
259 GrProcessorKeyBuilder* b) const {
260 GrGLTextureDomainEffect::GenKey(*this, caps, b);
261}
262
263GrGLFragmentProcessor* GrTextureDomainEffect::createGLInstance() const {
264 return SkNEW_ARGS(GrGLTextureDomainEffect, (*this));
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000265}
266
bsalomon0e08fc12014-10-15 08:19:04 -0700267bool GrTextureDomainEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700268 const GrTextureDomainEffect& s = sBase.cast<GrTextureDomainEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700269 return this->fTextureDomain == s.fTextureDomain;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000270}
271
egdaniel605dd0f2014-11-12 08:35:25 -0800272void GrTextureDomainEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000273 if (GrTextureDomain::kDecal_Mode == fTextureDomain.mode()) { // TODO: helper
egdanielf8449ba2014-11-25 10:24:56 -0800274 if (GrPixelConfigIsAlphaOnly(this->texture(0)->config())) {
275 inout->mulByUnknownAlpha();
276 } else {
277 inout->mulByUnknownColor();
278 }
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000279 } else {
egdaniel1a8ecdf2014-10-03 06:24:12 -0700280 this->updateInvariantOutputForModulation(inout);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000281 }
282}
283
284///////////////////////////////////////////////////////////////////////////////
285
joshualittb0a8a372014-09-23 09:50:21 -0700286GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTextureDomainEffect);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000287
joshualittb0a8a372014-09-23 09:50:21 -0700288GrFragmentProcessor* GrTextureDomainEffect::TestCreate(SkRandom* random,
289 GrContext*,
290 const GrDrawTargetCaps&,
291 GrTexture* textures[]) {
292 int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
293 GrProcessorUnitTest::kAlphaTextureIdx;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000294 SkRect domain;
295 domain.fLeft = random->nextUScalar1();
296 domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1);
297 domain.fTop = random->nextUScalar1();
298 domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1);
299 GrTextureDomain::Mode mode =
300 (GrTextureDomain::Mode) random->nextULessThan(GrTextureDomain::kModeCount);
joshualittb0a8a372014-09-23 09:50:21 -0700301 const SkMatrix& matrix = GrProcessorUnitTest::TestMatrix(random);
joshualitt5ae5fc52014-07-29 12:59:27 -0700302 bool bilerp = mode != GrTextureDomain::kRepeat_Mode ? random->nextBool() : false;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000303 GrCoordSet coords = random->nextBool() ? kLocal_GrCoordSet : kPosition_GrCoordSet;
304 return GrTextureDomainEffect::Create(textures[texIdx],
305 matrix,
306 domain,
307 mode,
308 bilerp ? GrTextureParams::kBilerp_FilterMode : GrTextureParams::kNone_FilterMode,
309 coords);
310}