blob: 0f3b614dd7be1dbb169ede47b10926c5731ebcb6 [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.comce49e162012-11-01 13:50:50 +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.com021fc732012-10-25 12:47:42 +000019 : INHERITED (factory) {
bsalomon@google.comce49e162012-11-01 13:50:50 +000020 fRequiresTextureMatrix = false;
bsalomon@google.com021fc732012-10-25 12:47:42 +000021 }
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000022
bsalomon@google.com22a800a2012-10-26 19:16:46 +000023 virtual void emitCode(GrGLShaderBuilder* builder,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000024 const GrEffectStage&,
bsalomon@google.comce49e162012-11-01 13:50:50 +000025 EffectKey key,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000026 const char* vertexCoords,
27 const char* outputColor,
28 const char* inputColor,
29 const TextureSamplerArray& samplers) SK_OVERRIDE {
bsalomon@google.comce49e162012-11-01 13:50:50 +000030 const char* coordName;
31 GrSLType coordType = fEffectMatrix.emitCode(builder, key, vertexCoords, &coordName);
bsalomon@google.com868a8e72012-08-30 19:11:34 +000032 builder->fFSCode.appendf("\t%s = ", outputColor);
bsalomon@google.comce49e162012-11-01 13:50:50 +000033 builder->appendTextureLookupAndModulate(&builder->fFSCode,
34 inputColor,
35 samplers[0],
36 coordName,
37 coordType);
bsalomon@google.com868a8e72012-08-30 19:11:34 +000038 builder->fFSCode.append(";\n");
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000039 }
40
bsalomon@google.comce49e162012-11-01 13:50:50 +000041 static inline EffectKey GenKey(const GrEffectStage& stage, const GrGLCaps&) {
42 const GrSingleTextureEffect& ste =
43 static_cast<const GrSingleTextureEffect&>(*stage.getEffect());
44 return GrGLEffectMatrix::GenKey(ste.getMatrix(),
45 stage.getCoordChangeMatrix(),
46 ste.texture(0));
47 }
48
49 virtual void setData(const GrGLUniformManager& uman, const GrEffectStage& stage) SK_OVERRIDE {
50 const GrSingleTextureEffect& ste =
51 static_cast<const GrSingleTextureEffect&>(*stage.getEffect());
52 fEffectMatrix.setData(uman, ste.getMatrix(), stage.getCoordChangeMatrix(), ste.texture(0));
53 }
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000054
55private:
bsalomon@google.comce49e162012-11-01 13:50:50 +000056 GrGLEffectMatrix fEffectMatrix;
bsalomon@google.com22a800a2012-10-26 19:16:46 +000057 typedef GrGLEffect INHERITED;
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000058};
59
60///////////////////////////////////////////////////////////////////////////////
61
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000062GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture)
bsalomon@google.come6e62d12012-10-04 14:38:48 +000063 : INHERITED(1)
64 , fTextureAccess(texture) {
bsalomon@google.comce49e162012-11-01 13:50:50 +000065 fMatrix.reset();
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000066}
67
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000068GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture, bool bilerp)
bsalomon@google.come6e62d12012-10-04 14:38:48 +000069 : INHERITED(1)
70 , fTextureAccess(texture, bilerp) {
bsalomon@google.comce49e162012-11-01 13:50:50 +000071 fMatrix.reset();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000072}
73
74GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture, const GrTextureParams& params)
bsalomon@google.come6e62d12012-10-04 14:38:48 +000075 : INHERITED(1)
76 , fTextureAccess(texture, params) {
bsalomon@google.comce49e162012-11-01 13:50:50 +000077 fMatrix.reset();
78}
79
80GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture, const GrMatrix& m)
81 : INHERITED(1)
82 , fTextureAccess(texture)
83 , fMatrix(m) {
84}
85
86GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture, const GrMatrix& m, bool bilerp)
87 : INHERITED(1)
88 , fTextureAccess(texture, bilerp)
89 , fMatrix(m) {
90}
91
92GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture,
93 const GrMatrix& m,
94 const GrTextureParams& params)
95 : INHERITED(1)
96 , fTextureAccess(texture, params)
97 , fMatrix(m) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000098}
99
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000100GrSingleTextureEffect::~GrSingleTextureEffect() {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000101}
102
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000103const GrTextureAccess& GrSingleTextureEffect::textureAccess(int index) const {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000104 GrAssert(0 == index);
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000105 return fTextureAccess;
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000106}
107
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000108const GrBackendEffectFactory& GrSingleTextureEffect::getFactory() const {
109 return GrTBackendEffectFactory<GrSingleTextureEffect>::getInstance();
tomhudson@google.comaa72eab2012-07-19 18:01:07 +0000110}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000111
112///////////////////////////////////////////////////////////////////////////////
113
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000114GR_DEFINE_EFFECT_TEST(GrSingleTextureEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000115
bsalomon@google.coma469c282012-10-24 18:28:34 +0000116GrEffect* GrSingleTextureEffect::TestCreate(SkRandom* random,
117 GrContext* context,
118 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000119 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
120 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.comce49e162012-11-01 13:50:50 +0000121 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random);
122 return SkNEW_ARGS(GrSingleTextureEffect, (textures[texIdx], matrix));
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000123}