blob: 0f6f4032a4953820e2f2c02ef5fc6d44a39274af [file] [log] [blame]
bsalomon@google.com68b58c92013-01-17 16:50:08 +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 "GrSimpleTextureEffect.h"
egdaniel605dd0f2014-11-12 08:35:25 -08009#include "GrInvariantOutput.h"
egdaniel605dd0f2014-11-12 08:35:25 -080010#include "GrTexture.h"
joshualitteb2a6762014-12-04 11:35:33 -080011#include "gl/GrGLCaps.h"
joshualittb0a8a372014-09-23 09:50:21 -070012#include "gl/GrGLProcessor.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000013#include "gl/GrGLTexture.h"
egdaniel605dd0f2014-11-12 08:35:25 -080014#include "gl/builders/GrGLProgramBuilder.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000015
joshualittb0a8a372014-09-23 09:50:21 -070016class GrGLSimpleTextureEffect : public GrGLFragmentProcessor {
bsalomon@google.com68b58c92013-01-17 16:50:08 +000017public:
joshualitteb2a6762014-12-04 11:35:33 -080018 GrGLSimpleTextureEffect(const GrProcessor&) {}
bsalomon@google.com68b58c92013-01-17 16:50:08 +000019
joshualitt15988992014-10-09 15:04:05 -070020 virtual void emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -070021 const GrFragmentProcessor& fp,
bsalomon@google.com68b58c92013-01-17 16:50:08 +000022 const char* outputColor,
23 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000024 const TransformedCoordsArray& coords,
mtklein36352bf2015-03-25 18:17:31 -070025 const TextureSamplerArray& samplers) override {
egdaniel29bee0f2015-04-29 11:54:42 -070026 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -070027 fsBuilder->codeAppendf("\t%s = ", outputColor);
28 fsBuilder->appendTextureLookupAndModulate(inputColor,
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +000029 samplers[0],
bsalomon@google.com77af6802013-10-02 13:04:56 +000030 coords[0].c_str(),
joshualitt23e280d2014-09-18 12:26:38 -070031 coords[0].getType());
joshualitt30ba4362014-08-21 20:18:45 -070032 fsBuilder->codeAppend(";\n");
bsalomon@google.com68b58c92013-01-17 16:50:08 +000033 }
34
bsalomon@google.com68b58c92013-01-17 16:50:08 +000035private:
joshualittb0a8a372014-09-23 09:50:21 -070036 typedef GrGLFragmentProcessor INHERITED;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000037};
38
39///////////////////////////////////////////////////////////////////////////////
40
egdaniel605dd0f2014-11-12 08:35:25 -080041void GrSimpleTextureEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
egdaniel1a8ecdf2014-10-03 06:24:12 -070042 this->updateInvariantOutputForModulation(inout);
bsalomon@google.com68b58c92013-01-17 16:50:08 +000043}
44
jvanverthcfc18862015-04-28 08:48:20 -070045void GrSimpleTextureEffect::getGLProcessorKey(const GrGLSLCaps& caps,
joshualitteb2a6762014-12-04 11:35:33 -080046 GrProcessorKeyBuilder* b) const {
47 GrGLSimpleTextureEffect::GenKey(*this, caps, b);
48}
49
50GrGLFragmentProcessor* GrSimpleTextureEffect::createGLInstance() const {
51 return SkNEW_ARGS(GrGLSimpleTextureEffect, (*this));
bsalomon@google.com68b58c92013-01-17 16:50:08 +000052}
53
54///////////////////////////////////////////////////////////////////////////////
55
joshualittb0a8a372014-09-23 09:50:21 -070056GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSimpleTextureEffect);
bsalomon@google.com68b58c92013-01-17 16:50:08 +000057
joshualitt0067ff52015-07-08 14:26:19 -070058GrFragmentProcessor* GrSimpleTextureEffect::TestCreate(GrProcessorTestData* d) {
59 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
60 GrProcessorUnitTest::kAlphaTextureIdx;
bsalomon@google.comc7818882013-03-20 19:19:53 +000061 static const SkShader::TileMode kTileModes[] = {
62 SkShader::kClamp_TileMode,
63 SkShader::kRepeat_TileMode,
64 SkShader::kMirror_TileMode,
65 };
66 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -070067 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
68 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
bsalomon@google.comc7818882013-03-20 19:19:53 +000069 };
joshualitt0067ff52015-07-08 14:26:19 -070070 GrTextureParams params(tileModes, d->fRandom->nextBool() ? GrTextureParams::kBilerp_FilterMode :
71 GrTextureParams::kNone_FilterMode);
bsalomon@google.comc7818882013-03-20 19:19:53 +000072
bsalomon@google.com77af6802013-10-02 13:04:56 +000073 static const GrCoordSet kCoordSets[] = {
74 kLocal_GrCoordSet,
bsalomon309d4d52014-12-18 10:17:44 -080075 kDevice_GrCoordSet
bsalomon@google.comc7818882013-03-20 19:19:53 +000076 };
joshualitt0067ff52015-07-08 14:26:19 -070077 GrCoordSet coordSet = kCoordSets[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kCoordSets))];
bsalomon@google.comc7818882013-03-20 19:19:53 +000078
joshualitt0067ff52015-07-08 14:26:19 -070079 const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom);
80 return GrSimpleTextureEffect::Create(d->fTextures[texIdx], matrix, coordSet);
bsalomon@google.com68b58c92013-01-17 16:50:08 +000081}