blob: 14f5b644727baf212d0069e2b0fd155dedfa6743 [file] [log] [blame]
tomhudson@google.comd0c1a062012-07-12 17:23:52 +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 "effects/GrSingleTextureEffect.h"
bsalomon@google.comd698f772012-10-25 13:22:00 +00009#include "gl/GrGLEffect.h"
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000010#include "gl/GrGLEffectMatrix.h"
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000011#include "gl/GrGLSL.h"
12#include "gl/GrGLTexture.h"
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000013#include "GrTBackendEffectFactory.h"
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000014#include "GrTexture.h"
15
bsalomon@google.com22a800a2012-10-26 19:16:46 +000016class GrGLSingleTextureEffect : public GrGLEffect {
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000017public:
bsalomon@google.com396e61f2012-10-25 19:00:29 +000018 GrGLSingleTextureEffect(const GrBackendEffectFactory& factory, const GrEffect&)
bsalomon@google.comdbe49f72012-11-05 16:36:02 +000019 : INHERITED (factory) {}
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000020
bsalomon@google.com22a800a2012-10-26 19:16:46 +000021 virtual void emitCode(GrGLShaderBuilder* builder,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000022 const GrEffectStage&,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000023 EffectKey key,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000024 const char* vertexCoords,
25 const char* outputColor,
26 const char* inputColor,
27 const TextureSamplerArray& samplers) SK_OVERRIDE {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000028 const char* coordName;
29 GrSLType coordType = fEffectMatrix.emitCode(builder, key, vertexCoords, &coordName);
bsalomon@google.com868a8e72012-08-30 19:11:34 +000030 builder->fFSCode.appendf("\t%s = ", outputColor);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000031 builder->appendTextureLookupAndModulate(&builder->fFSCode,
32 inputColor,
33 samplers[0],
34 coordName,
35 coordType);
bsalomon@google.com868a8e72012-08-30 19:11:34 +000036 builder->fFSCode.append(";\n");
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000037 }
38
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000039 static inline EffectKey GenKey(const GrEffectStage& stage, const GrGLCaps&) {
40 const GrSingleTextureEffect& ste =
41 static_cast<const GrSingleTextureEffect&>(*stage.getEffect());
42 return GrGLEffectMatrix::GenKey(ste.getMatrix(),
43 stage.getCoordChangeMatrix(),
44 ste.texture(0));
45 }
46
47 virtual void setData(const GrGLUniformManager& uman, const GrEffectStage& stage) SK_OVERRIDE {
48 const GrSingleTextureEffect& ste =
49 static_cast<const GrSingleTextureEffect&>(*stage.getEffect());
50 fEffectMatrix.setData(uman, ste.getMatrix(), stage.getCoordChangeMatrix(), ste.texture(0));
51 }
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000052
53private:
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000054 GrGLEffectMatrix fEffectMatrix;
bsalomon@google.com22a800a2012-10-26 19:16:46 +000055 typedef GrGLEffect INHERITED;
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000056};
57
58///////////////////////////////////////////////////////////////////////////////
59
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000060GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture)
bsalomon@google.com50db75c2013-01-11 13:54:30 +000061 : fTextureAccess(texture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000062 fMatrix.reset();
bsalomon@google.com50db75c2013-01-11 13:54:30 +000063 this->addTextureAccess(&fTextureAccess);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000064}
65
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000066GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture, bool bilerp)
bsalomon@google.com50db75c2013-01-11 13:54:30 +000067 : fTextureAccess(texture, bilerp) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000068 fMatrix.reset();
bsalomon@google.com50db75c2013-01-11 13:54:30 +000069 this->addTextureAccess(&fTextureAccess);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000070}
71
72GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture, const GrTextureParams& params)
bsalomon@google.com50db75c2013-01-11 13:54:30 +000073 : fTextureAccess(texture, params) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000074 fMatrix.reset();
bsalomon@google.com50db75c2013-01-11 13:54:30 +000075 this->addTextureAccess(&fTextureAccess);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000076}
77
bsalomon@google.comb9086a02012-11-01 18:02:54 +000078GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture, const SkMatrix& m)
bsalomon@google.com50db75c2013-01-11 13:54:30 +000079 : fTextureAccess(texture)
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000080 , fMatrix(m) {
bsalomon@google.com50db75c2013-01-11 13:54:30 +000081 this->addTextureAccess(&fTextureAccess);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000082}
83
bsalomon@google.comb9086a02012-11-01 18:02:54 +000084GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture, const SkMatrix& m, bool bilerp)
bsalomon@google.com50db75c2013-01-11 13:54:30 +000085 : fTextureAccess(texture, bilerp)
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000086 , fMatrix(m) {
bsalomon@google.com50db75c2013-01-11 13:54:30 +000087 this->addTextureAccess(&fTextureAccess);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000088}
89
90GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture,
bsalomon@google.comb9086a02012-11-01 18:02:54 +000091 const SkMatrix& m,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000092 const GrTextureParams& params)
bsalomon@google.com50db75c2013-01-11 13:54:30 +000093 : fTextureAccess(texture, params)
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000094 , fMatrix(m) {
bsalomon@google.com50db75c2013-01-11 13:54:30 +000095 this->addTextureAccess(&fTextureAccess);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000096}
97
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000098GrSingleTextureEffect::~GrSingleTextureEffect() {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000099}
100
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000101const GrBackendEffectFactory& GrSingleTextureEffect::getFactory() const {
102 return GrTBackendEffectFactory<GrSingleTextureEffect>::getInstance();
tomhudson@google.comaa72eab2012-07-19 18:01:07 +0000103}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000104
105///////////////////////////////////////////////////////////////////////////////
106
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000107GR_DEFINE_EFFECT_TEST(GrSingleTextureEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000108
bsalomon@google.coma469c282012-10-24 18:28:34 +0000109GrEffect* GrSingleTextureEffect::TestCreate(SkRandom* random,
110 GrContext* context,
111 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000112 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
113 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000114 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random);
115 return SkNEW_ARGS(GrSingleTextureEffect, (textures[texIdx], matrix));
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000116}