1 /*
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 "GrInvariantOutput.h"
10 #include "GrTexture.h"
11 #include "gl/GrGLCaps.h"
12 #include "gl/GrGLProcessor.h"
13 #include "gl/GrGLSL.h"
14 #include "gl/GrGLTexture.h"
15 #include "gl/builders/GrGLProgramBuilder.h"
16
17 class GrGLSimpleTextureEffect : public GrGLFragmentProcessor {
18 public:
GrGLSimpleTextureEffect(const GrProcessor &)19 GrGLSimpleTextureEffect(const GrProcessor&) {}
20
emitCode(GrGLFPBuilder * builder,const GrFragmentProcessor & fp,const char * outputColor,const char * inputColor,const TransformedCoordsArray & coords,const TextureSamplerArray & samplers)21 virtual void emitCode(GrGLFPBuilder* builder,
22 const GrFragmentProcessor& fp,
23 const char* outputColor,
24 const char* inputColor,
25 const TransformedCoordsArray& coords,
26 const TextureSamplerArray& samplers) override {
27 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
28 fsBuilder->codeAppendf("\t%s = ", outputColor);
29 fsBuilder->appendTextureLookupAndModulate(inputColor,
30 samplers[0],
31 coords[0].c_str(),
32 coords[0].getType());
33 fsBuilder->codeAppend(";\n");
34 }
35
36 private:
37 typedef GrGLFragmentProcessor INHERITED;
38 };
39
40 ///////////////////////////////////////////////////////////////////////////////
41
onComputeInvariantOutput(GrInvariantOutput * inout) const42 void GrSimpleTextureEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
43 this->updateInvariantOutputForModulation(inout);
44 }
45
getGLProcessorKey(const GrGLSLCaps & caps,GrProcessorKeyBuilder * b) const46 void GrSimpleTextureEffect::getGLProcessorKey(const GrGLSLCaps& caps,
47 GrProcessorKeyBuilder* b) const {
48 GrGLSimpleTextureEffect::GenKey(*this, caps, b);
49 }
50
createGLInstance() const51 GrGLFragmentProcessor* GrSimpleTextureEffect::createGLInstance() const {
52 return SkNEW_ARGS(GrGLSimpleTextureEffect, (*this));
53 }
54
55 ///////////////////////////////////////////////////////////////////////////////
56
57 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSimpleTextureEffect);
58
TestCreate(SkRandom * random,GrContext *,const GrDrawTargetCaps &,GrTexture * textures[])59 GrFragmentProcessor* GrSimpleTextureEffect::TestCreate(SkRandom* random,
60 GrContext*,
61 const GrDrawTargetCaps&,
62 GrTexture* textures[]) {
63 int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
64 GrProcessorUnitTest::kAlphaTextureIdx;
65 static const SkShader::TileMode kTileModes[] = {
66 SkShader::kClamp_TileMode,
67 SkShader::kRepeat_TileMode,
68 SkShader::kMirror_TileMode,
69 };
70 SkShader::TileMode tileModes[] = {
71 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
72 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
73 };
74 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode :
75 GrTextureParams::kNone_FilterMode);
76
77 static const GrCoordSet kCoordSets[] = {
78 kLocal_GrCoordSet,
79 kDevice_GrCoordSet
80 };
81 GrCoordSet coordSet = kCoordSets[random->nextULessThan(SK_ARRAY_COUNT(kCoordSets))];
82
83 const SkMatrix& matrix = GrTest::TestMatrix(random);
84 return GrSimpleTextureEffect::Create(textures[texIdx], matrix, coordSet);
85 }
86