blob: cf08d3bcd335f9198e32b2c81f163b1596cb9663 [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)
19 : INHERITED (factory) {
20 GrEffect::CoordsType coordsType =
21 drawEffect.castEffect<GrSimpleTextureEffect>().coordsType();
22 if (GrEffect::kCustom_CoordsType != coordsType) {
23 SkNEW_IN_TLAZY(&fEffectMatrix, GrGLEffectMatrix, (coordsType));
24 }
25 }
bsalomon@google.com68b58c92013-01-17 16:50:08 +000026
27 virtual void emitCode(GrGLShaderBuilder* builder,
bsalomon@google.comc7818882013-03-20 19:19:53 +000028 const GrDrawEffect& drawEffect,
bsalomon@google.com68b58c92013-01-17 16:50:08 +000029 EffectKey key,
bsalomon@google.com68b58c92013-01-17 16:50:08 +000030 const char* outputColor,
31 const char* inputColor,
32 const TextureSamplerArray& samplers) SK_OVERRIDE {
bsalomon@google.comc7818882013-03-20 19:19:53 +000033 const GrSimpleTextureEffect& ste = drawEffect.castEffect<GrSimpleTextureEffect>();
commit-bot@chromium.org7ab7ca42013-08-28 15:59:13 +000034 SkString fsCoordName;
bsalomon@google.comc7818882013-03-20 19:19:53 +000035 GrSLType fsCoordSLType;
36 if (GrEffect::kCustom_CoordsType == ste.coordsType()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000037 SkASSERT(ste.getMatrix().isIdentity());
38 SkASSERT(1 == ste.numVertexAttribs());
bsalomon@google.comc7818882013-03-20 19:19:53 +000039 fsCoordSLType = kVec2f_GrSLType;
40 const char* vsVaryingName;
commit-bot@chromium.org7ab7ca42013-08-28 15:59:13 +000041 const char* fsVaryingNamePtr;
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +000042 GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder();
43 SkASSERT(NULL != vertexBuilder);
44 vertexBuilder->addVarying(kVec2f_GrSLType, "textureCoords", &vsVaryingName, &fsVaryingNamePtr);
commit-bot@chromium.org7ab7ca42013-08-28 15:59:13 +000045 fsCoordName = fsVaryingNamePtr;
bsalomon@google.comc7818882013-03-20 19:19:53 +000046 const char* attrName =
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +000047 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0])->c_str();
48 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, attrName);
bsalomon@google.comc7818882013-03-20 19:19:53 +000049 } else {
50 fsCoordSLType = fEffectMatrix.get()->emitCode(builder, key, &fsCoordName);
51 }
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000052 builder->fsCodeAppendf("\t%s = ", outputColor);
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +000053 builder->fsAppendTextureLookupAndModulate(inputColor,
54 samplers[0],
55 fsCoordName.c_str(),
56 fsCoordSLType);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000057 builder->fsCodeAppend(";\n");
bsalomon@google.com68b58c92013-01-17 16:50:08 +000058 }
59
bsalomon@google.comc7818882013-03-20 19:19:53 +000060 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
61 const GrSimpleTextureEffect& ste = drawEffect.castEffect<GrSimpleTextureEffect>();
62 if (GrEffect::kCustom_CoordsType == ste.coordsType()) {
63 return 1 << GrGLEffectMatrix::kKeyBits;
64 } else {
65 return GrGLEffectMatrix::GenKey(ste.getMatrix(),
66 drawEffect,
67 ste.coordsType(),
68 ste.texture(0));
69 }
bsalomon@google.com68b58c92013-01-17 16:50:08 +000070 }
71
bsalomon@google.comc7818882013-03-20 19:19:53 +000072 virtual void setData(const GrGLUniformManager& uman,
73 const GrDrawEffect& drawEffect) SK_OVERRIDE {
74 const GrSimpleTextureEffect& ste = drawEffect.castEffect<GrSimpleTextureEffect>();
75 if (GrEffect::kCustom_CoordsType == ste.coordsType()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000076 SkASSERT(ste.getMatrix().isIdentity());
bsalomon@google.comc7818882013-03-20 19:19:53 +000077 } else {
78 fEffectMatrix.get()->setData(uman, ste.getMatrix(), drawEffect, ste.texture(0));
79 }
bsalomon@google.com68b58c92013-01-17 16:50:08 +000080 }
81
82private:
bsalomon@google.comc7818882013-03-20 19:19:53 +000083 SkTLazy<GrGLEffectMatrix> fEffectMatrix;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000084 typedef GrGLEffect INHERITED;
85};
86
87///////////////////////////////////////////////////////////////////////////////
88
89void GrSimpleTextureEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
90 this->updateConstantColorComponentsForModulation(color, validFlags);
91}
92
93const GrBackendEffectFactory& GrSimpleTextureEffect::getFactory() const {
94 return GrTBackendEffectFactory<GrSimpleTextureEffect>::getInstance();
95}
96
97///////////////////////////////////////////////////////////////////////////////
98
99GR_DEFINE_EFFECT_TEST(GrSimpleTextureEffect);
100
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000101GrEffectRef* GrSimpleTextureEffect::TestCreate(SkRandom* random,
sugoi@google.come0e385c2013-03-11 18:50:03 +0000102 GrContext*,
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000103 const GrDrawTargetCaps&,
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000104 GrTexture* textures[]) {
105 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
106 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000107 static const SkShader::TileMode kTileModes[] = {
108 SkShader::kClamp_TileMode,
109 SkShader::kRepeat_TileMode,
110 SkShader::kMirror_TileMode,
111 };
112 SkShader::TileMode tileModes[] = {
113 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
114 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
115 };
humper@google.comb86add12013-07-25 18:49:07 +0000116 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode :
117 GrTextureParams::kNone_FilterMode);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000118
119 static const CoordsType kCoordsTypes[] = {
120 kLocal_CoordsType,
121 kPosition_CoordsType,
122 kCustom_CoordsType
123 };
124 CoordsType coordsType = kCoordsTypes[random->nextULessThan(GR_ARRAY_COUNT(kCoordsTypes))];
125
126 if (kCustom_CoordsType == coordsType) {
127 return GrSimpleTextureEffect::CreateWithCustomCoords(textures[texIdx], params);
128 } else {
129 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random);
130 return GrSimpleTextureEffect::Create(textures[texIdx], matrix);
131 }
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000132}