blob: a8eab3a771d0c857d00b45778d2411338354dc2a [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,
bsalomon@google.com68b58c92013-01-17 16:50:08 +000025 const char* outputColor,
26 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000027 const TransformedCoordsArray& coords,
bsalomon@google.com68b58c92013-01-17 16:50:08 +000028 const TextureSamplerArray& samplers) SK_OVERRIDE {
joshualitt15988992014-10-09 15:04:05 -070029 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -070030 fsBuilder->codeAppendf("\t%s = ", outputColor);
31 fsBuilder->appendTextureLookupAndModulate(inputColor,
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +000032 samplers[0],
bsalomon@google.com77af6802013-10-02 13:04:56 +000033 coords[0].c_str(),
joshualitt23e280d2014-09-18 12:26:38 -070034 coords[0].getType());
joshualitt30ba4362014-08-21 20:18:45 -070035 fsBuilder->codeAppend(";\n");
bsalomon@google.com68b58c92013-01-17 16:50:08 +000036 }
37
bsalomon@google.com68b58c92013-01-17 16:50:08 +000038private:
joshualittb0a8a372014-09-23 09:50:21 -070039 typedef GrGLFragmentProcessor INHERITED;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000040};
41
42///////////////////////////////////////////////////////////////////////////////
43
egdaniel605dd0f2014-11-12 08:35:25 -080044void GrSimpleTextureEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
egdaniel1a8ecdf2014-10-03 06:24:12 -070045 this->updateInvariantOutputForModulation(inout);
bsalomon@google.com68b58c92013-01-17 16:50:08 +000046}
47
joshualittb0a8a372014-09-23 09:50:21 -070048const GrBackendFragmentProcessorFactory& GrSimpleTextureEffect::getFactory() const {
49 return GrTBackendFragmentProcessorFactory<GrSimpleTextureEffect>::getInstance();
bsalomon@google.com68b58c92013-01-17 16:50:08 +000050}
51
52///////////////////////////////////////////////////////////////////////////////
53
joshualittb0a8a372014-09-23 09:50:21 -070054GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSimpleTextureEffect);
bsalomon@google.com68b58c92013-01-17 16:50:08 +000055
joshualittb0a8a372014-09-23 09:50:21 -070056GrFragmentProcessor* GrSimpleTextureEffect::TestCreate(SkRandom* random,
57 GrContext*,
58 const GrDrawTargetCaps&,
59 GrTexture* textures[]) {
60 int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
61 GrProcessorUnitTest::kAlphaTextureIdx;
bsalomon@google.comc7818882013-03-20 19:19:53 +000062 static const SkShader::TileMode kTileModes[] = {
63 SkShader::kClamp_TileMode,
64 SkShader::kRepeat_TileMode,
65 SkShader::kMirror_TileMode,
66 };
67 SkShader::TileMode tileModes[] = {
68 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
69 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
70 };
humper@google.comb86add12013-07-25 18:49:07 +000071 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode :
72 GrTextureParams::kNone_FilterMode);
bsalomon@google.comc7818882013-03-20 19:19:53 +000073
bsalomon@google.com77af6802013-10-02 13:04:56 +000074 static const GrCoordSet kCoordSets[] = {
75 kLocal_GrCoordSet,
76 kPosition_GrCoordSet
bsalomon@google.comc7818882013-03-20 19:19:53 +000077 };
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000078 GrCoordSet coordSet = kCoordSets[random->nextULessThan(SK_ARRAY_COUNT(kCoordSets))];
bsalomon@google.comc7818882013-03-20 19:19:53 +000079
joshualittb0a8a372014-09-23 09:50:21 -070080 const SkMatrix& matrix = GrProcessorUnitTest::TestMatrix(random);
bsalomon@google.com77af6802013-10-02 13:04:56 +000081 return GrSimpleTextureEffect::Create(textures[texIdx], matrix, coordSet);
bsalomon@google.com68b58c92013-01-17 16:50:08 +000082}