blob: 9c0ff354ce513716ce8d8114c602a86ce39ba7c9 [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"
10#include "GrTBackendProcessorFactory.h"
11#include "GrTexture.h"
joshualittb0a8a372014-09-23 09:50:21 -070012#include "gl/GrGLProcessor.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000013#include "gl/GrGLSL.h"
14#include "gl/GrGLTexture.h"
egdaniel605dd0f2014-11-12 08:35:25 -080015#include "gl/builders/GrGLProgramBuilder.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000016
joshualittb0a8a372014-09-23 09:50:21 -070017class GrGLSimpleTextureEffect : public GrGLFragmentProcessor {
bsalomon@google.com68b58c92013-01-17 16:50:08 +000018public:
joshualittb0a8a372014-09-23 09:50:21 -070019 GrGLSimpleTextureEffect(const GrBackendProcessorFactory& factory, const GrProcessor&)
bsalomon@google.com77af6802013-10-02 13:04:56 +000020 : INHERITED (factory) {
bsalomon@google.comc7818882013-03-20 19:19:53 +000021 }
bsalomon@google.com68b58c92013-01-17 16:50:08 +000022
joshualitt15988992014-10-09 15:04:05 -070023 virtual void emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -070024 const GrFragmentProcessor& fp,
25 const GrProcessorKey& key,
bsalomon@google.com68b58c92013-01-17 16:50:08 +000026 const char* outputColor,
27 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000028 const TransformedCoordsArray& coords,
bsalomon@google.com68b58c92013-01-17 16:50:08 +000029 const TextureSamplerArray& samplers) SK_OVERRIDE {
joshualitt15988992014-10-09 15:04:05 -070030 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -070031 fsBuilder->codeAppendf("\t%s = ", outputColor);
32 fsBuilder->appendTextureLookupAndModulate(inputColor,
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +000033 samplers[0],
bsalomon@google.com77af6802013-10-02 13:04:56 +000034 coords[0].c_str(),
joshualitt23e280d2014-09-18 12:26:38 -070035 coords[0].getType());
joshualitt30ba4362014-08-21 20:18:45 -070036 fsBuilder->codeAppend(";\n");
bsalomon@google.com68b58c92013-01-17 16:50:08 +000037 }
38
bsalomon@google.com68b58c92013-01-17 16:50:08 +000039private:
joshualittb0a8a372014-09-23 09:50:21 -070040 typedef GrGLFragmentProcessor INHERITED;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000041};
42
43///////////////////////////////////////////////////////////////////////////////
44
egdaniel605dd0f2014-11-12 08:35:25 -080045void GrSimpleTextureEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
egdaniel1a8ecdf2014-10-03 06:24:12 -070046 this->updateInvariantOutputForModulation(inout);
bsalomon@google.com68b58c92013-01-17 16:50:08 +000047}
48
joshualittb0a8a372014-09-23 09:50:21 -070049const GrBackendFragmentProcessorFactory& GrSimpleTextureEffect::getFactory() const {
50 return GrTBackendFragmentProcessorFactory<GrSimpleTextureEffect>::getInstance();
bsalomon@google.com68b58c92013-01-17 16:50:08 +000051}
52
53///////////////////////////////////////////////////////////////////////////////
54
joshualittb0a8a372014-09-23 09:50:21 -070055GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSimpleTextureEffect);
bsalomon@google.com68b58c92013-01-17 16:50:08 +000056
joshualittb0a8a372014-09-23 09:50:21 -070057GrFragmentProcessor* GrSimpleTextureEffect::TestCreate(SkRandom* random,
58 GrContext*,
59 const GrDrawTargetCaps&,
60 GrTexture* textures[]) {
61 int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
62 GrProcessorUnitTest::kAlphaTextureIdx;
bsalomon@google.comc7818882013-03-20 19:19:53 +000063 static const SkShader::TileMode kTileModes[] = {
64 SkShader::kClamp_TileMode,
65 SkShader::kRepeat_TileMode,
66 SkShader::kMirror_TileMode,
67 };
68 SkShader::TileMode tileModes[] = {
69 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
70 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
71 };
humper@google.comb86add12013-07-25 18:49:07 +000072 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode :
73 GrTextureParams::kNone_FilterMode);
bsalomon@google.comc7818882013-03-20 19:19:53 +000074
bsalomon@google.com77af6802013-10-02 13:04:56 +000075 static const GrCoordSet kCoordSets[] = {
76 kLocal_GrCoordSet,
77 kPosition_GrCoordSet
bsalomon@google.comc7818882013-03-20 19:19:53 +000078 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000079 GrCoordSet coordSet = kCoordSets[random->nextULessThan(SK_ARRAY_COUNT(kCoordSets))];
bsalomon@google.comc7818882013-03-20 19:19:53 +000080
joshualittb0a8a372014-09-23 09:50:21 -070081 const SkMatrix& matrix = GrProcessorUnitTest::TestMatrix(random);
bsalomon@google.com77af6802013-10-02 13:04:56 +000082 return GrSimpleTextureEffect::Create(textures[texIdx], matrix, coordSet);
bsalomon@google.com68b58c92013-01-17 16:50:08 +000083}