blob: 37e6eb4b25204b203002d42f1f9e8c6f5e613dbe [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:
reed@google.com67e7cde2013-03-20 17:47:16 +000018 GrGLSimpleTextureEffect(const GrBackendEffectFactory& factory, const GrEffectRef&)
19 : INHERITED (factory) {}
bsalomon@google.com68b58c92013-01-17 16:50:08 +000020
21 virtual void emitCode(GrGLShaderBuilder* builder,
reed@google.com67e7cde2013-03-20 17:47:16 +000022 const GrEffectStage&,
bsalomon@google.com68b58c92013-01-17 16:50:08 +000023 EffectKey key,
reed@google.com67e7cde2013-03-20 17:47:16 +000024 const char* vertexCoords,
bsalomon@google.com68b58c92013-01-17 16:50:08 +000025 const char* outputColor,
26 const char* inputColor,
27 const TextureSamplerArray& samplers) SK_OVERRIDE {
reed@google.com67e7cde2013-03-20 17:47:16 +000028 const char* coordName;
29 GrSLType coordType = fEffectMatrix.emitCode(builder, key, vertexCoords, &coordName);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000030 builder->fsCodeAppendf("\t%s = ", outputColor);
31 builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com68b58c92013-01-17 16:50:08 +000032 inputColor,
33 samplers[0],
reed@google.com67e7cde2013-03-20 17:47:16 +000034 coordName,
35 coordType);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000036 builder->fsCodeAppend(";\n");
bsalomon@google.com68b58c92013-01-17 16:50:08 +000037 }
38
reed@google.com67e7cde2013-03-20 17:47:16 +000039 static inline EffectKey GenKey(const GrEffectStage& stage, const GrGLCaps&) {
40 const GrSimpleTextureEffect& ste = GetEffectFromStage<GrSimpleTextureEffect>(stage);
41 return GrGLEffectMatrix::GenKey(ste.getMatrix(),
42 stage.getCoordChangeMatrix(),
43 ste.texture(0));
bsalomon@google.com68b58c92013-01-17 16:50:08 +000044 }
45
reed@google.com67e7cde2013-03-20 17:47:16 +000046 virtual void setData(const GrGLUniformManager& uman, const GrEffectStage& stage) SK_OVERRIDE {
47 const GrSimpleTextureEffect& ste = GetEffectFromStage<GrSimpleTextureEffect>(stage);
48 fEffectMatrix.setData(uman, ste.getMatrix(), stage.getCoordChangeMatrix(), ste.texture(0));
bsalomon@google.com68b58c92013-01-17 16:50:08 +000049 }
50
51private:
reed@google.com67e7cde2013-03-20 17:47:16 +000052 GrGLEffectMatrix fEffectMatrix;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000053 typedef GrGLEffect INHERITED;
54};
55
56///////////////////////////////////////////////////////////////////////////////
57
58void GrSimpleTextureEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
59 this->updateConstantColorComponentsForModulation(color, validFlags);
60}
61
62const GrBackendEffectFactory& GrSimpleTextureEffect::getFactory() const {
63 return GrTBackendEffectFactory<GrSimpleTextureEffect>::getInstance();
64}
65
66///////////////////////////////////////////////////////////////////////////////
67
68GR_DEFINE_EFFECT_TEST(GrSimpleTextureEffect);
69
bsalomon@google.com73a96942013-02-13 16:31:19 +000070GrEffectRef* GrSimpleTextureEffect::TestCreate(SkMWCRandom* random,
sugoi@google.come0e385c2013-03-11 18:50:03 +000071 GrContext*,
bsalomon@google.com68b58c92013-01-17 16:50:08 +000072 GrTexture* textures[]) {
73 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
74 GrEffectUnitTest::kAlphaTextureIdx;
reed@google.com67e7cde2013-03-20 17:47:16 +000075 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random);
76 return GrSimpleTextureEffect::Create(textures[texIdx], matrix);
bsalomon@google.com68b58c92013-01-17 16:50:08 +000077}