blob: 0ee6f78dd5e2cd98de30fb0002985de8226d83db [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"
9#include "gl/GrGLEffect.h"
10#include "gl/GrGLEffectMatrix.h"
11#include "gl/GrGLSL.h"
12#include "gl/GrGLTexture.h"
13#include "GrTBackendEffectFactory.h"
14#include "GrTexture.h"
15
16class GrGLSimpleTextureEffect : public GrGLEffect {
17public:
bsalomon@google.comc7818882013-03-20 19:19:53 +000018 GrGLSimpleTextureEffect(const GrBackendEffectFactory& factory, const GrDrawEffect& drawEffect)
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000019 : INHERITED (factory)
20 , fEffectMatrix(drawEffect.castEffect<GrSimpleTextureEffect>().coordsType()) {
bsalomon@google.comc7818882013-03-20 19:19:53 +000021 }
bsalomon@google.com68b58c92013-01-17 16:50:08 +000022
23 virtual void emitCode(GrGLShaderBuilder* builder,
bsalomon@google.comc7818882013-03-20 19:19:53 +000024 const GrDrawEffect& drawEffect,
bsalomon@google.com68b58c92013-01-17 16:50:08 +000025 EffectKey key,
bsalomon@google.com68b58c92013-01-17 16:50:08 +000026 const char* outputColor,
27 const char* inputColor,
28 const TextureSamplerArray& samplers) SK_OVERRIDE {
commit-bot@chromium.org7ab7ca42013-08-28 15:59:13 +000029 SkString fsCoordName;
bsalomon@google.comc7818882013-03-20 19:19:53 +000030 GrSLType fsCoordSLType;
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000031 fsCoordSLType = fEffectMatrix.emitCode(builder, key, &fsCoordName);
32
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000033 builder->fsCodeAppendf("\t%s = ", outputColor);
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +000034 builder->fsAppendTextureLookupAndModulate(inputColor,
35 samplers[0],
36 fsCoordName.c_str(),
37 fsCoordSLType);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000038 builder->fsCodeAppend(";\n");
bsalomon@google.com68b58c92013-01-17 16:50:08 +000039 }
40
bsalomon@google.comc7818882013-03-20 19:19:53 +000041 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
42 const GrSimpleTextureEffect& ste = drawEffect.castEffect<GrSimpleTextureEffect>();
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000043 return GrGLEffectMatrix::GenKey(ste.getMatrix(),
44 drawEffect,
45 ste.coordsType(),
46 ste.texture(0));
bsalomon@google.com68b58c92013-01-17 16:50:08 +000047 }
48
bsalomon@google.comc7818882013-03-20 19:19:53 +000049 virtual void setData(const GrGLUniformManager& uman,
50 const GrDrawEffect& drawEffect) SK_OVERRIDE {
51 const GrSimpleTextureEffect& ste = drawEffect.castEffect<GrSimpleTextureEffect>();
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000052 fEffectMatrix.setData(uman, ste.getMatrix(), drawEffect, ste.texture(0));
bsalomon@google.com68b58c92013-01-17 16:50:08 +000053 }
54
55private:
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000056 GrGLEffectMatrix fEffectMatrix;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000057 typedef GrGLEffect INHERITED;
58};
59
60///////////////////////////////////////////////////////////////////////////////
61
62void GrSimpleTextureEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
63 this->updateConstantColorComponentsForModulation(color, validFlags);
64}
65
66const GrBackendEffectFactory& GrSimpleTextureEffect::getFactory() const {
67 return GrTBackendEffectFactory<GrSimpleTextureEffect>::getInstance();
68}
69
70///////////////////////////////////////////////////////////////////////////////
71
72GR_DEFINE_EFFECT_TEST(GrSimpleTextureEffect);
73
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000074GrEffectRef* GrSimpleTextureEffect::TestCreate(SkRandom* random,
sugoi@google.come0e385c2013-03-11 18:50:03 +000075 GrContext*,
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000076 const GrDrawTargetCaps&,
bsalomon@google.com68b58c92013-01-17 16:50:08 +000077 GrTexture* textures[]) {
78 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
79 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.comc7818882013-03-20 19:19:53 +000080 static const SkShader::TileMode kTileModes[] = {
81 SkShader::kClamp_TileMode,
82 SkShader::kRepeat_TileMode,
83 SkShader::kMirror_TileMode,
84 };
85 SkShader::TileMode tileModes[] = {
86 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
87 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
88 };
humper@google.comb86add12013-07-25 18:49:07 +000089 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode :
90 GrTextureParams::kNone_FilterMode);
bsalomon@google.comc7818882013-03-20 19:19:53 +000091
92 static const CoordsType kCoordsTypes[] = {
93 kLocal_CoordsType,
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000094 kPosition_CoordsType
bsalomon@google.comc7818882013-03-20 19:19:53 +000095 };
96 CoordsType coordsType = kCoordsTypes[random->nextULessThan(GR_ARRAY_COUNT(kCoordsTypes))];
97
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +000098 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random);
99 return GrSimpleTextureEffect::Create(textures[texIdx], matrix, coordsType);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000100}